1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

refactor: config utils

This commit is contained in:
Juan Picado @jotadeveloper 2018-07-01 10:12:38 +02:00
parent 23f76b1b1d
commit 3917b19976
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
2 changed files with 60 additions and 55 deletions

@ -5,6 +5,14 @@ import minimatch from 'minimatch';
import assert from 'assert';
import {ErrorCode} from './utils';
const BLACKLIST = {
all: true,
anonymous: true,
undefined: true,
owner: true,
none: true,
};
/**
* Normalise user list.
* @return {Array}
@ -41,6 +49,45 @@ export function getMatchedPackagesSpec(packages: any, pkg: any) {
return {};
}
export function uplinkSanityCheck(uplinks: any, users: any = BLACKLIST) {
const newUplinks = _.clone(uplinks);
let newUsers = _.clone(users);
for (let uplink in newUplinks) {
if (Object.prototype.hasOwnProperty.call(newUplinks, uplink)) {
if (_.isNil(newUplinks[uplink].cache)) {
newUplinks[uplink].cache = true;
}
newUsers = sanityCheckNames(uplink, newUsers);
}
}
return newUplinks;
}
export function sanityCheckNames(item: string, users: any) {
assert(item !== 'all' && item !== 'owner' && item !== 'anonymous' && item !== 'undefined' && item !== 'none', 'CONFIG: reserved user/uplink name: ' + item);
assert(!item.match(/\s/), 'CONFIG: invalid user name: ' + item);
assert(users[item] == null, 'CONFIG: duplicate user/uplink name: ' + item);
users[item] = true;
return users;
}
export function sanityCheckUplinksProps(configUpLinks: any) {
const uplinks = _.clone(configUpLinks);
for (let uplink in uplinks) {
if (Object.prototype.hasOwnProperty.call(uplinks, uplink)) {
assert(uplinks[uplink].url, 'CONFIG: no url for uplink: ' + uplink);
assert( _.isString(uplinks[uplink].url), 'CONFIG: wrong url format for uplink: ' + uplink);
uplinks[uplink].url = uplinks[uplink].url.replace(/\/$/, '');
}
}
return uplinks;
}
export function normalisePackageAccess(packages: any): any {
const normalizedPkgs: any = {...packages};
// add a default rule for all packages to make writing plugins easier

@ -1,42 +1,23 @@
import _ from 'lodash';
import assert from 'assert';
import minimatch from 'minimatch';
import {generateRandomHexString} from './crypto-utils';
import {normalisePackageAccess} from './config-utils';
import {getUserAgent} from './utils';
import {normalisePackageAccess, sanityCheckUplinksProps, uplinkSanityCheck} from './config-utils';
import {getUserAgent, isObject} from './utils';
import {APP_ERROR} from './constants';
const assert = require('assert');
const _ = require('lodash');
const minimatch = require('minimatch');
const Utils = require('./utils');
const LoggerApi = require('./logger');
const strategicConfigProps = ['users', 'uplinks', 'packages'];
const strategicConfigProps = ['uplinks', 'packages'];
const allowedEnvConfig = ['http_proxy', 'https_proxy', 'no_proxy'];
function checkUserOrUplink(item, users) {
assert(item !== 'all' && item !== 'owner'
&& item !== 'anonymous' && item !== 'undefined' && item !== 'none', 'CONFIG: reserved user/uplink name: ' + item);
assert(!item.match(/\s/), 'CONFIG: invalid user name: ' + item);
assert(users[item] == null, 'CONFIG: duplicate user/uplink name: ' + item);
users[item] = true;
}
/**
* Coordinates the application configuration
*/
class Config {
/**
* @param {*} config config the content
*/
constructor(config) {
const self = this;
this.logger = LoggerApi.logger;
const users = {
all: true,
anonymous: true,
undefined: true,
owner: true,
none: true,
};
for (let configProp in config) {
if (self[configProp] == null) {
@ -56,41 +37,18 @@ class Config {
if (self[x] == null) {
self[x] = {};
}
assert(Utils.isObject(self[x]), `CONFIG: bad "${x}" value (object expected)`);
assert(isObject(self[x]), `CONFIG: bad "${x}" value (object expected)`);
});
// sanity check for users
for (let i in self.users) {
if (Object.prototype.hasOwnProperty.call(self.users, i)) {
checkUserOrUplink(i, users);
}
}
// sanity check for uplinks
/* eslint guard-for-in: 0 */
for (let i in self.uplinks) {
if (self.uplinks[i].cache == null) {
self.uplinks[i].cache = true;
}
if (Object.prototype.hasOwnProperty.call(self.uplinks, i)) {
checkUserOrUplink(i, users);
}
}
this.uplinks = sanityCheckUplinksProps(uplinkSanityCheck(this.uplinks));
if (_.isNil(this.users) === false) {
this.logger.warn(`[users]: property on configuration file
is not longer supported, property being ignored`);
}
for (let uplink in self.uplinks) {
if (Object.prototype.hasOwnProperty.call(self.uplinks, uplink)) {
assert(self.uplinks[uplink].url, 'CONFIG: no url for uplink: ' + uplink);
assert( typeof(self.uplinks[uplink].url) === 'string'
, 'CONFIG: wrong url format for uplink: ' + uplink);
self.uplinks[uplink].url = self.uplinks[uplink].url.replace(/\/$/, '');
}
}
self.packages = normalisePackageAccess(self.packages);
this.packages = normalisePackageAccess(self.packages);
// loading these from ENV if aren't in config
allowedEnvConfig.forEach((function(v) {
@ -100,8 +58,8 @@ class Config {
}));
// unique identifier of self server (or a cluster), used to avoid loops
if (!self.server_id) {
self.server_id = generateRandomHexString(6);
if (!this.server_id) {
this.server_id = generateRandomHexString(6);
}
}