mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-04-01 02:42:23 -05:00
refactor: remove extraneous else statements (#1671)
Co-authored-by: Juan Picado @jotadeveloper <juanpicado19@gmail.com>
This commit is contained in:
parent
3b31f39469
commit
a616250e33
15 changed files with 105 additions and 131 deletions
|
@ -15,9 +15,8 @@ export default function(storage: IStorageHandler): (req: $RequestExtend, res: Re
|
|||
return false;
|
||||
} else if (!isStar && !isExistlocalUsers) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return (req: $RequestExtend, res: Response, next: $NextFunctionVer): void => {
|
||||
|
|
|
@ -71,9 +71,8 @@ export default function(route: Router, auth: IAuth): void {
|
|||
|
||||
if (isUpdated) {
|
||||
return next(buildProfile(req.remote_user.name));
|
||||
} else {
|
||||
return next(ErrorCode.getInternalError(API_ERROR.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
return next(ErrorCode.getInternalError(API_ERROR.INTERNAL_SERVER_ERROR));
|
||||
}
|
||||
);
|
||||
} else if (_.isNil(tfa) === false) {
|
||||
|
|
|
@ -42,9 +42,8 @@ export default function(route: Router, auth: IAuth, storage: IStorageHandler, co
|
|||
logger.error({ error: error.msg }, 'token list has failed: @{error}');
|
||||
return next(ErrorCode.getCode(HTTP_STATUS.INTERNAL_ERROR, error.message));
|
||||
}
|
||||
} else {
|
||||
return next(ErrorCode.getUnauthorized());
|
||||
}
|
||||
return next(ErrorCode.getUnauthorized());
|
||||
});
|
||||
|
||||
route.post('/-/npm/v1/tokens', function(req: $RequestExtend, res: Response, next: $NextFunctionVer) {
|
||||
|
@ -59,47 +58,47 @@ export default function(route: Router, auth: IAuth, storage: IStorageHandler, co
|
|||
if (err) {
|
||||
const errorCode = err.message ? HTTP_STATUS.UNAUTHORIZED : HTTP_STATUS.INTERNAL_ERROR;
|
||||
return next(ErrorCode.getCode(errorCode, err.message));
|
||||
} else {
|
||||
req.remote_user = user;
|
||||
}
|
||||
|
||||
if (!_.isFunction(storage.saveToken)) {
|
||||
return next(ErrorCode.getCode(HTTP_STATUS.NOT_IMPLEMENTED, SUPPORT_ERRORS.STORAGE_NOT_IMPLEMENT));
|
||||
}
|
||||
req.remote_user = user;
|
||||
|
||||
try {
|
||||
const token = await getApiToken(auth, config, user, password);
|
||||
const key = stringToMD5(token);
|
||||
// TODO: use a utility here
|
||||
const maskedToken = mask(token, 5);
|
||||
const created = new Date().getTime();
|
||||
if (!_.isFunction(storage.saveToken)) {
|
||||
return next(ErrorCode.getCode(HTTP_STATUS.NOT_IMPLEMENTED, SUPPORT_ERRORS.STORAGE_NOT_IMPLEMENT));
|
||||
}
|
||||
|
||||
/**
|
||||
* cidr_whitelist: is not being used, we pass it through
|
||||
* token: we do not store the real token (it is generated once and retrieved to the user), just a mask of it.
|
||||
*/
|
||||
const saveToken: Token = {
|
||||
user: name,
|
||||
token: maskedToken,
|
||||
key,
|
||||
cidr: cidr_whitelist,
|
||||
readonly,
|
||||
created,
|
||||
};
|
||||
try {
|
||||
const token = await getApiToken(auth, config, user, password);
|
||||
const key = stringToMD5(token);
|
||||
// TODO: use a utility here
|
||||
const maskedToken = mask(token, 5);
|
||||
const created = new Date().getTime();
|
||||
|
||||
await storage.saveToken(saveToken);
|
||||
logger.debug({ key, name }, 'token @{key} was created for user @{name}');
|
||||
return next(normalizeToken({
|
||||
token,
|
||||
user: name,
|
||||
key: saveToken.key,
|
||||
cidr: cidr_whitelist,
|
||||
readonly,
|
||||
created: saveToken.created,
|
||||
}));
|
||||
} catch (error) {
|
||||
logger.error({ error: error.msg }, 'token creation has failed: @{error}');
|
||||
return next(ErrorCode.getCode(HTTP_STATUS.INTERNAL_ERROR, error.message));
|
||||
}
|
||||
/**
|
||||
* cidr_whitelist: is not being used, we pass it through
|
||||
* token: we do not store the real token (it is generated once and retrieved to the user), just a mask of it.
|
||||
*/
|
||||
const saveToken: Token = {
|
||||
user: name,
|
||||
token: maskedToken,
|
||||
key,
|
||||
cidr: cidr_whitelist,
|
||||
readonly,
|
||||
created,
|
||||
};
|
||||
|
||||
await storage.saveToken(saveToken);
|
||||
logger.debug({ key, name }, 'token @{key} was created for user @{name}');
|
||||
return next(normalizeToken({
|
||||
token,
|
||||
user: name,
|
||||
key: saveToken.key,
|
||||
cidr: cidr_whitelist,
|
||||
readonly,
|
||||
created: saveToken.created,
|
||||
}));
|
||||
} catch (error) {
|
||||
logger.error({ error: error.msg }, 'token creation has failed: @{error}');
|
||||
return next(ErrorCode.getCode(HTTP_STATUS.INTERNAL_ERROR, error.message));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -118,8 +117,7 @@ export default function(route: Router, auth: IAuth, storage: IStorageHandler, co
|
|||
logger.error({ error: error.msg }, 'token creation has failed: @{error}');
|
||||
return next(ErrorCode.getCode(HTTP_STATUS.INTERNAL_ERROR, error.message));
|
||||
}
|
||||
} else {
|
||||
return next(ErrorCode.getUnauthorized());
|
||||
}
|
||||
return next(ErrorCode.getUnauthorized());
|
||||
});
|
||||
}
|
||||
|
|
|
@ -163,9 +163,8 @@ export function final(body: FinalBody, req: $RequestExtend, res: $ResponseExtend
|
|||
res.socket.destroy();
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
res.send(body);
|
||||
|
|
|
@ -36,9 +36,8 @@ function addPackageWebApi(route: Router, storage: IStorageHandler, auth: IAuth,
|
|||
(err, allowed): void => {
|
||||
if (err) {
|
||||
resolve(false);
|
||||
} else {
|
||||
resolve(allowed);
|
||||
}
|
||||
resolve(allowed);
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
|
|
|
@ -156,18 +156,17 @@ export async function getApiToken(auth: IAuthWebUI, config: Config, remoteUser:
|
|||
return await new Promise((resolve): void => {
|
||||
resolve(auth.aesEncrypt(buildUserBuffer(remoteUser.name as string, aesPassword)).toString('base64'));
|
||||
});
|
||||
} else {
|
||||
// i am wiling to use here _.isNil but flow does not like it yet.
|
||||
const { jwt } = security.api;
|
||||
|
||||
if (jwt && jwt.sign) {
|
||||
return await auth.jwtEncrypt(remoteUser, jwt.sign);
|
||||
} else {
|
||||
return await new Promise((resolve): void => {
|
||||
resolve(auth.aesEncrypt(buildUserBuffer(remoteUser.name as string, aesPassword)).toString('base64'));
|
||||
});
|
||||
}
|
||||
}
|
||||
// i am wiling to use here _.isNil but flow does not like it yet.
|
||||
const { jwt } = security.api;
|
||||
|
||||
if (jwt && jwt.sign) {
|
||||
return await auth.jwtEncrypt(remoteUser, jwt.sign);
|
||||
}
|
||||
return await new Promise((resolve): void => {
|
||||
resolve(auth.aesEncrypt(buildUserBuffer(remoteUser.name as string, aesPassword)).toString('base64'));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
export function parseAuthTokenHeader(authorizationHeader: string): AuthTokenHeader {
|
||||
|
@ -219,9 +218,8 @@ export function verifyJWTPayload(token: string, secret: string): RemoteUser {
|
|||
// old tokens fails still remains in usage, thus
|
||||
// we return an anonymous user to force log in.
|
||||
return createAnonymousRemoteUser();
|
||||
} else {
|
||||
throw ErrorCode.getCode(HTTP_STATUS.UNAUTHORIZED, error.message);
|
||||
}
|
||||
throw ErrorCode.getCode(HTTP_STATUS.UNAUTHORIZED, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -242,11 +240,10 @@ export function getMiddlewareCredentials(security: Security, secret: string, aut
|
|||
}
|
||||
|
||||
return parsedCredentials;
|
||||
} else {
|
||||
const { scheme, token } = parseAuthTokenHeader(authorizationHeader);
|
||||
}
|
||||
const { scheme, token } = parseAuthTokenHeader(authorizationHeader);
|
||||
|
||||
if (_.isString(token) && scheme.toUpperCase() === TOKEN_BEARER.toUpperCase()) {
|
||||
return verifyJWTPayload(token, secret);
|
||||
}
|
||||
if (_.isString(token) && scheme.toUpperCase() === TOKEN_BEARER.toUpperCase()) {
|
||||
return verifyJWTPayload(token, secret);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,9 +73,8 @@ function updateStorageLinks(configLocation, defaultConfig): string {
|
|||
if (folderExists(dataDir)) {
|
||||
dataDir = Path.resolve(Path.join(dataDir, pkgJSON.name, 'storage'));
|
||||
return defaultConfig.replace(/^storage: .\/storage$/m, `storage: ${dataDir}`);
|
||||
} else {
|
||||
return defaultConfig;
|
||||
}
|
||||
return defaultConfig;
|
||||
}
|
||||
|
||||
function getConfigPaths(): SetupDirectory[] {
|
||||
|
|
|
@ -86,9 +86,8 @@ class LocalStorage implements IStorage {
|
|||
if (_.isNil(err) === false) {
|
||||
if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) {
|
||||
return callback(ErrorCode.getNotFound());
|
||||
} else {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
data = normalizePackage(data);
|
||||
|
@ -653,9 +652,8 @@ class LocalStorage implements IStorage {
|
|||
if (err) {
|
||||
if (err.code === STORAGE.NO_SUCH_FILE_ERROR || err.code === HTTP_STATUS.NOT_FOUND) {
|
||||
return callback(ErrorCode.getNotFound());
|
||||
} else {
|
||||
return callback(this._internalError(err, STORAGE.PACKAGE_FILE_NAME, 'error reading'));
|
||||
}
|
||||
return callback(this._internalError(err, STORAGE.PACKAGE_FILE_NAME, 'error reading'));
|
||||
}
|
||||
|
||||
callback(err, normalizePackage(result));
|
||||
|
@ -820,9 +818,8 @@ class LocalStorage implements IStorage {
|
|||
if (_.isNil(Storage)) {
|
||||
assert(this.config.storage, 'CONFIG: storage path not defined');
|
||||
return new LocalDatabase(this.config, logger);
|
||||
} else {
|
||||
return Storage as IPluginStorage<Config>;
|
||||
}
|
||||
return Storage as IPluginStorage<Config>;
|
||||
}
|
||||
|
||||
private _loadStorePlugin(): IPluginStorage<Config> | void {
|
||||
|
|
|
@ -30,9 +30,8 @@ export function printMessage(type, msg, templateObjects, hasColors) {
|
|||
const sub = subsystems[hasColors ? 0 : 1][templateObjects.sub] || subsystems[+!hasColors].default;
|
||||
if (hasColors) {
|
||||
return ` ${levels[type](pad(type, LEVEL_VALUE_MAX))}${white(`${sub} ${finalMessage}`)}`;
|
||||
} else {
|
||||
return ` ${pad(type, LEVEL_VALUE_MAX)}${sub} ${finalMessage}`;
|
||||
}
|
||||
return ` ${pad(type, LEVEL_VALUE_MAX)}${sub} ${finalMessage}`;
|
||||
}
|
||||
|
||||
export function fillInMsgTemplate(msg, obj: unknown, colors): string {
|
||||
|
@ -61,11 +60,9 @@ export function fillInMsgTemplate(msg, obj: unknown, colors): string {
|
|||
return str;
|
||||
} else if (is_error) {
|
||||
return red(str);
|
||||
} else {
|
||||
return green(str);
|
||||
}
|
||||
} else {
|
||||
return inspect(str, undefined, null, colors);
|
||||
return green(str);
|
||||
}
|
||||
return inspect(str, undefined, null, colors);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -68,10 +68,9 @@ export function notify(metadata: Package, config: Config, remoteUser: RemoteUser
|
|||
if (config.notify) {
|
||||
if (config.notify.content) {
|
||||
return sendNotification(metadata, (config.notify as unknown) as Notification, remoteUser, publishedPackage);
|
||||
} else {
|
||||
// multiple notifications endpoints PR #108
|
||||
return Promise.all(_.map(config.notify, key => sendNotification(metadata, key, remoteUser, publishedPackage)));
|
||||
}
|
||||
// multiple notifications endpoints PR #108
|
||||
return Promise.all(_.map(config.notify, key => sendNotification(metadata, key, remoteUser, publishedPackage)));
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
|
|
|
@ -11,14 +11,13 @@ export function notifyRequest(options: RequiredUriUrl, content): Promise<any | E
|
|||
const errorMessage = isNil(err) ? response.body : err.message;
|
||||
logger.error({ errorMessage }, 'notify service has thrown an error: @{errorMessage}');
|
||||
reject(errorMessage);
|
||||
} else {
|
||||
logger.info({ content }, 'A notification has been shipped: @{content}');
|
||||
if (isNil(body) === false) {
|
||||
logger.debug({ body }, ' body: @{body}');
|
||||
resolve(body);
|
||||
}
|
||||
reject(Error('body is missing'));
|
||||
}
|
||||
logger.info({ content }, 'A notification has been shipped: @{content}');
|
||||
if (isNil(body) === false) {
|
||||
logger.debug({ body }, ' body: @{body}');
|
||||
resolve(body);
|
||||
}
|
||||
reject(Error('body is missing'));
|
||||
});
|
||||
}
|
||||
);
|
||||
|
|
|
@ -553,9 +553,8 @@ class Storage implements IStorageHandler {
|
|||
|
||||
if (uplinkTimeoutError) {
|
||||
return callback(ErrorCode.getServiceUnavailable(), null, upLinksErrors);
|
||||
} else {
|
||||
return callback(ErrorCode.getNotFound(API_ERROR.NO_PACKAGE), null, upLinksErrors);
|
||||
}
|
||||
return callback(ErrorCode.getNotFound(API_ERROR.NO_PACKAGE), null, upLinksErrors);
|
||||
}
|
||||
|
||||
if (upLinks.length === 0) {
|
||||
|
|
|
@ -582,31 +582,30 @@ class ProxyStorage implements IProxy {
|
|||
private _statusCheck(alive?: boolean): boolean | void {
|
||||
if (arguments.length === 0) {
|
||||
return this._ifRequestFailure() === false;
|
||||
} else {
|
||||
if (alive) {
|
||||
if (this.failed_requests >= this.max_fails) {
|
||||
this.logger.warn(
|
||||
{
|
||||
host: this.url.host,
|
||||
},
|
||||
'host @{host} is back online'
|
||||
);
|
||||
}
|
||||
this.failed_requests = 0;
|
||||
} else {
|
||||
this.failed_requests++;
|
||||
if (this.failed_requests === this.max_fails) {
|
||||
this.logger.warn(
|
||||
{
|
||||
host: this.url.host,
|
||||
},
|
||||
'host @{host} is now offline'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.last_request_time = Date.now();
|
||||
}
|
||||
if (alive) {
|
||||
if (this.failed_requests >= this.max_fails) {
|
||||
this.logger.warn(
|
||||
{
|
||||
host: this.url.host,
|
||||
},
|
||||
'host @{host} is back online'
|
||||
);
|
||||
}
|
||||
this.failed_requests = 0;
|
||||
} else {
|
||||
this.failed_requests++;
|
||||
if (this.failed_requests === this.max_fails) {
|
||||
this.logger.warn(
|
||||
{
|
||||
host: this.url.host,
|
||||
},
|
||||
'host @{host} is now offline'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.last_request_time = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -95,10 +95,9 @@ export function validatePackage(name: string): boolean {
|
|||
if (nameList.length === 1) {
|
||||
// normal package
|
||||
return validateName(nameList[0]);
|
||||
} else {
|
||||
// scoped package
|
||||
return nameList[0][0] === '@' && validateName(nameList[0].slice(1)) && validateName(nameList[1]);
|
||||
}
|
||||
// scoped package
|
||||
return nameList[0][0] === '@' && validateName(nameList[0].slice(1)) && validateName(nameList[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -413,9 +412,8 @@ export function parseConfigFile(configPath: string): any {
|
|||
try {
|
||||
if (/\.ya?ml$/i.test(configPath)) {
|
||||
return YAML.safeLoad(fs.readFileSync(configPath, CHARACTER_ENCODING.UTF8));
|
||||
} else {
|
||||
return require(configPath);
|
||||
}
|
||||
return require(configPath);
|
||||
} catch (e) {
|
||||
if (e.code !== 'MODULE_NOT_FOUND') {
|
||||
e.message = APP_ERROR.CONFIG_NOT_VALID;
|
||||
|
|
|
@ -27,14 +27,10 @@ encodeURIComponent(
|
|||
* Generate gravatar url from email address
|
||||
*/
|
||||
export function generateGravatarUrl(email: string | void = '', online: boolean = true): string {
|
||||
if (online) {
|
||||
if (_.isString(email) && _.size(email) > 0) {
|
||||
email = email.trim().toLocaleLowerCase();
|
||||
const emailMD5 = stringToMD5(email);
|
||||
return `https://www.gravatar.com/avatar/${emailMD5}`;
|
||||
}
|
||||
return GENERIC_AVATAR;
|
||||
} else {
|
||||
return GENERIC_AVATAR;
|
||||
if (online && _.isString(email) && _.size(email) > 0) {
|
||||
email = email.trim().toLocaleLowerCase();
|
||||
const emailMD5 = stringToMD5(email);
|
||||
return `https://www.gravatar.com/avatar/${emailMD5}`;
|
||||
}
|
||||
return GENERIC_AVATAR;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue