Skip to content

Commit cecf2da

Browse files
Merge pull request #109 from bufferoverflow/chore/style-fixes
style: remove useless ts-ignore, fix no-use-before-define
2 parents c57dd28 + 9768593 commit cecf2da

File tree

12 files changed

+71
-96
lines changed

12 files changed

+71
-96
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,9 @@
103103
"@verdaccio"
104104
],
105105
"rules": {
106-
"@typescript-eslint/no-var-requires": 0,
107106
"@typescript-eslint/ban-ts-ignore": 0,
108-
"@typescript-eslint/no-use-before-define": 0,
109107
"@typescript-eslint/no-explicit-any": 0,
110-
"@typescript-eslint/explicit-function-return-type": 0,
111-
"@typescript-eslint/explicit-member-accessibility": 0
108+
"@typescript-eslint/explicit-function-return-type": 0
112109
},
113110
"ignorePatterns": ["build/", "node_modules/"]
114111
}

src/authcache.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ import { Logger } from '@verdaccio/types';
77
import NodeCache from 'node-cache';
88

99
export class AuthCache {
10-
logger: Logger;
11-
ttl: number;
12-
storage: NodeCache;
10+
private logger: Logger;
11+
private ttl: number;
12+
private storage: NodeCache;
1313

14-
static get DEFAULT_TTL() {
14+
public static get DEFAULT_TTL() {
1515
return 300;
1616
}
1717

18-
static _generateKeyHash(username: string, password: string) {
18+
private static _generateKeyHash(username: string, password: string) {
1919
const sha = Crypto.createHash('sha256');
2020
sha.update(JSON.stringify({ username: username, password: password }));
2121
return sha.digest('hex');
2222
}
2323

24-
constructor(logger: Logger, ttl?: number) {
24+
public constructor(logger: Logger, ttl?: number) {
2525
this.logger = logger;
2626
this.ttl = ttl || AuthCache.DEFAULT_TTL;
2727

@@ -34,11 +34,11 @@ export class AuthCache {
3434
});
3535
}
3636

37-
findUser(username: string, password: string): UserData {
37+
public findUser(username: string, password: string): UserData {
3838
return this.storage.get(AuthCache._generateKeyHash(username, password)) as UserData;
3939
}
4040

41-
storeUser(username: string, password: string, userData: UserData): boolean {
41+
public storeUser(username: string, password: string, userData: UserData): boolean {
4242
return this.storage.set(AuthCache._generateKeyHash(username, password), userData);
4343
}
4444
}
@@ -48,20 +48,20 @@ export type UserDataGroups = {
4848
};
4949

5050
export class UserData {
51-
_username: string;
52-
_groups: UserDataGroups;
51+
private _username: string;
52+
private _groups: UserDataGroups;
5353

54-
get username(): string {
54+
public get username(): string {
5555
return this._username;
5656
}
57-
get groups(): UserDataGroups {
57+
public get groups(): UserDataGroups {
5858
return this._groups;
5959
}
60-
set groups(groups: UserDataGroups) {
60+
public set groups(groups: UserDataGroups) {
6161
this._groups = groups;
6262
}
6363

64-
constructor(username: string, groups: UserDataGroups) {
64+
public constructor(username: string, groups: UserDataGroups) {
6565
this._username = username;
6666
this._groups = groups;
6767
}

src/gitlab.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,14 @@ const BUILTIN_ACCESS_LEVEL_ANONYMOUS = ['$anonymous', '$all'];
3838
// Level to apply on 'allow_access' calls when a package definition does not define one
3939
const DEFAULT_ALLOW_ACCESS_LEVEL = ['$all'];
4040

41-
export interface VerdaccioGitLabPlugin extends IPluginAuth<VerdaccioGitlabConfig> {
42-
authCache: AuthCache;
43-
}
44-
45-
export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
46-
options: PluginOptions<VerdaccioGitlabConfig>;
47-
config: VerdaccioGitlabConfig;
48-
// @ts-ignore
49-
authCache: AuthCache;
50-
logger: Logger;
51-
publishLevel: VerdaccioGitlabAccessLevel;
52-
53-
constructor(config: VerdaccioGitlabConfig, options: PluginOptions<VerdaccioGitlabConfig>) {
41+
export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfig> {
42+
private options: PluginOptions<VerdaccioGitlabConfig>;
43+
private config: VerdaccioGitlabConfig;
44+
private authCache?: AuthCache;
45+
private logger: Logger;
46+
private publishLevel: VerdaccioGitlabAccessLevel;
47+
48+
public constructor(config: VerdaccioGitlabConfig, options: PluginOptions<VerdaccioGitlabConfig>) {
5449
this.logger = options.logger;
5550
this.config = config;
5651
this.options = options;
@@ -76,14 +71,13 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
7671
this.logger.info(`[gitlab] publish control level: ${this.publishLevel}`);
7772
}
7873

79-
authenticate(user: string, password: string, cb: Callback) {
74+
public authenticate(user: string, password: string, cb: Callback) {
8075
this.logger.trace(`[gitlab] authenticate called for user: ${user}`);
8176

8277
// Try to find the user groups in the cache
8378
const cachedUserGroups = this._getCachedUserGroups(user, password);
8479
if (cachedUserGroups) {
85-
// @ts-ignore
86-
this.logger.debug(`[gitlab] user: ${user} found in cache, authenticated with groups:`, cachedUserGroups);
80+
this.logger.debug(`[gitlab] user: ${user} found in cache, authenticated with groups:`, cachedUserGroups.toString());
8781
return cb(null, cachedUserGroups.publish);
8882
}
8983

@@ -108,8 +102,7 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
108102
// - for publish, the logged in user id and all the groups they can reach as configured with access level `$auth.gitlab.publish`
109103
const gitlabPublishQueryParams = { min_access_level: publishLevelId };
110104

111-
// @ts-ignore
112-
this.logger.trace('[gitlab] querying gitlab user groups with params:', gitlabPublishQueryParams);
105+
this.logger.trace('[gitlab] querying gitlab user groups with params:', gitlabPublishQueryParams.toString());
113106

114107
const groupsPromise = GitlabAPI.Groups.all(gitlabPublishQueryParams).then(groups => {
115108
return groups.filter(group => group.path === group.full_path).map(group => group.path);
@@ -125,8 +118,7 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
125118
this._setCachedUserGroups(user, password, { publish: realGroups });
126119

127120
this.logger.info(`[gitlab] user: ${user} successfully authenticated`);
128-
// @ts-ignore
129-
this.logger.debug(`[gitlab] user: ${user}, with groups:`, realGroups);
121+
this.logger.debug(`[gitlab] user: ${user}, with groups:`, realGroups.toString());
130122

131123
return cb(null, realGroups);
132124
})
@@ -141,17 +133,17 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
141133
});
142134
}
143135

144-
adduser(user: string, password: string, cb: Callback) {
136+
public adduser(user: string, password: string, cb: Callback) {
145137
this.logger.trace(`[gitlab] adduser called for user: ${user}`);
146138
return cb(null, true);
147139
}
148140

149-
changePassword(user: string, password: string, newPassword: string, cb: Callback) {
141+
public changePassword(user: string, password: string, newPassword: string, cb: Callback) {
150142
this.logger.trace(`[gitlab] changePassword called for user: ${user}`);
151143
return cb(getInternalError('You are using verdaccio-gitlab integration. Please change your password in gitlab'));
152144
}
153145

154-
allow_access(user: RemoteUser, _package: VerdaccioGitlabPackageAccess & PackageAccess, cb: Callback) {
146+
public allow_access(user: RemoteUser, _package: VerdaccioGitlabPackageAccess & PackageAccess, cb: Callback) {
155147
if (!_package.gitlab) return cb(null, false);
156148

157149
const packageAccess = _package.access && _package.access.length > 0 ? _package.access : DEFAULT_ALLOW_ACCESS_LEVEL;
@@ -172,7 +164,7 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
172164
}
173165
}
174166

175-
allow_publish(user: RemoteUser, _package: VerdaccioGitlabPackageAccess & PackageAccess, cb: Callback) {
167+
public allow_publish(user: RemoteUser, _package: VerdaccioGitlabPackageAccess & PackageAccess, cb: Callback) {
176168
if (!_package.gitlab) return cb(null, false);
177169

178170
const packageScopePermit = false;
@@ -206,7 +198,7 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
206198
}
207199
}
208200

209-
_matchGroupWithPackage(real_group: string, package_name: string): boolean {
201+
private _matchGroupWithPackage(real_group: string, package_name: string): boolean {
210202
if (real_group === package_name) {
211203
return true;
212204
}
@@ -231,15 +223,15 @@ export default class VerdaccioGitLab implements VerdaccioGitLabPlugin {
231223
return false;
232224
}
233225

234-
_getCachedUserGroups(username: string, password: string): UserDataGroups | null {
226+
private _getCachedUserGroups(username: string, password: string): UserDataGroups | null {
235227
if (!this.authCache) {
236228
return null;
237229
}
238230
const userData = this.authCache.findUser(username, password);
239231
return (userData || {}).groups || null;
240232
}
241233

242-
_setCachedUserGroups(username: string, password: string, groups: UserDataGroups): boolean {
234+
private _setCachedUserGroups(username: string, password: string, groups: UserDataGroups): boolean {
243235
if (!this.authCache) {
244236
return false;
245237
}

src/verdaccio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const globalTunnel = require('global-tunnel-ng');
1+
import globalTunnel from 'global-tunnel-ng';
22
globalTunnel.initialize();
33
require('verdaccio/build/lib/cli');

test/functional/fixtures/package.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/functional/fixtures/package.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {DOMAIN_SERVERS, PORT_SERVER_1, TARBALL} from '../config.functional';
2+
3+
export default function(name, version = '0.0.0', port = PORT_SERVER_1, ___domain= `http://${DOMAIN_SERVERS}:${port}`,
4+
fileName = TARBALL, readme = 'this is a readme'): any {
5+
return {
6+
name,
7+
version,
8+
readme,
9+
dist: {
10+
shasum: 'fake',
11+
tarball: `${___domain}/${encodeURIComponent(name)}/-/${fileName}`,
12+
},
13+
};
14+
}

test/functional/lib/environment.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ import { PORT_GITLAB_EXPRESS_MOCK, DOMAIN_SERVERS, PORT_SERVER_1 } from '../conf
1212
import GitlabServer from './mock_gitlab_server';
1313

1414
class FunctionalEnvironment extends NodeEnvironment {
15-
config: any;
15+
private config: any;
1616

17-
constructor(config: any) {
17+
public constructor(config: any) {
1818
super(config);
1919
}
2020

21-
async startWeb() {
21+
public async startWeb() {
2222
const gitlab: any = new GitlabServer();
2323

2424
return await gitlab.start(PORT_GITLAB_EXPRESS_MOCK);
2525
}
2626

27-
async setup() {
27+
public async setup() {
2828
const SILENCE_LOG = !process.env.VERDACCIO_DEBUG;
2929
// @ts-ignore
3030
const DEBUG_INJECT: boolean = process.env.VERDACCIO_DEBUG_INJECT ? process.env.VERDACCIO_DEBUG_INJECT : false;
@@ -64,7 +64,7 @@ class FunctionalEnvironment extends NodeEnvironment {
6464
this.global.__SERVERS__ = serverList;
6565
}
6666

67-
async teardown() {
67+
public async teardown() {
6868
await super.teardown();
6969
console.log(chalk.yellow('Teardown Test Environment.'));
7070
// shutdown verdaccio
@@ -75,7 +75,7 @@ class FunctionalEnvironment extends NodeEnvironment {
7575
this.global.__GITLAB_SERVER__.server.close();
7676
}
7777

78-
runScript(script: string) {
78+
private runScript(script: string) {
7979
return super.runScript(script);
8080
}
8181
}

test/functional/lib/mock_gitlab_server.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ import { GITLAB_DATA, CREDENTIALS } from '../config.functional';
66
import { GITLAB, HTTP_STATUS } from '../../lib/constants';
77

88
export default class GitlabServer {
9-
app: any;
10-
server: any;
11-
expectedToken: string;
9+
private app: any;
10+
private server: any;
11+
private expectedToken: string;
1212

13-
constructor() {
13+
public constructor() {
1414
this.app = express();
1515
this.expectedToken = CREDENTIALS.password;
1616
}
1717

18-
start(port: number): Promise<any> {
18+
public start(port: number): Promise<any> {
1919
return new Promise(resolve => {
20-
// @ts-ignore
2120
this.app.use(bodyParser.json());
22-
// @ts-ignore
2321
this.app.use(
2422
bodyParser.urlencoded({
2523
extended: true,
@@ -52,7 +50,7 @@ export default class GitlabServer {
5250
});
5351
}
5452

55-
_checkAuthentication(req: any, res: any, cb: any) {
53+
private _checkAuthentication(req: any, res: any, cb: any) {
5654
if (req.get('private-token') === this.expectedToken) {
5755
cb();
5856
} else {

test/functional/publish/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { CREDENTIALS, PACKAGE, WRONG_CREDENTIALS } from '../config.functional';
22
import { HTTP_STATUS } from '../../lib/constants';
3+
import fixturePkg from '../fixtures/package';
34

45
export default (server: any, gitlab: any) => {
5-
// eslint-disable-line no-unused-vars
6-
76
describe('package publish tests', () => {
87
beforeEach(() => {
98
return server.auth(CREDENTIALS.user, CREDENTIALS.password);
@@ -12,7 +11,7 @@ export default (server: any, gitlab: any) => {
1211
test('should deny publish of package when unauthenticated', () => {
1312
return server.auth(WRONG_CREDENTIALS.user, CREDENTIALS.password).then(() => {
1413
return server
15-
.putPackage(PACKAGE.NAME, require('../fixtures/package')(PACKAGE.NAME))
14+
.putPackage(PACKAGE.NAME, fixturePkg(PACKAGE.NAME))
1615
.status(HTTP_STATUS.FORBIDDEN)
1716
.then(body => {
1817
expect(body).toHaveProperty('error');
@@ -22,7 +21,7 @@ export default (server: any, gitlab: any) => {
2221

2322
test('should allow publish of package when gitlab groups match', () => {
2423
return server
25-
.putPackage(PACKAGE.GROUP_NAME, require('../fixtures/package')(PACKAGE.GROUP_NAME))
24+
.putPackage(PACKAGE.GROUP_NAME, fixturePkg(PACKAGE.GROUP_NAME))
2625
.status(HTTP_STATUS.CREATED)
2726
.body_ok(/created new package/)
2827
.then(body => {
@@ -35,7 +34,7 @@ export default (server: any, gitlab: any) => {
3534

3635
test('should allow publish of scoped package when gitlab groups match', () => {
3736
return server
38-
.putPackage(PACKAGE.SCOPED_GROUP_NAME, require('../fixtures/package')(PACKAGE.SCOPED_GROUP_NAME))
37+
.putPackage(PACKAGE.SCOPED_GROUP_NAME, fixturePkg(PACKAGE.SCOPED_GROUP_NAME))
3938
.status(HTTP_STATUS.CREATED)
4039
.body_ok(/created new package/)
4140
.then(body => {
@@ -48,7 +47,7 @@ export default (server: any, gitlab: any) => {
4847

4948
test('should allow publish of scoped package when gitlab projects match', () => {
5049
return server
51-
.putPackage(PACKAGE.SCOPED_PROJECT_NAME, require('../fixtures/package')(PACKAGE.SCOPED_PROJECT_NAME))
50+
.putPackage(PACKAGE.SCOPED_PROJECT_NAME, fixturePkg(PACKAGE.SCOPED_PROJECT_NAME))
5251
.status(HTTP_STATUS.CREATED)
5352
.body_ok(/created new package/)
5453
.then(body => {

0 commit comments

Comments
 (0)