Merge pull request #644 from verdaccio/fix-610

fix: flow performance issues with lodash #610
This commit is contained in:
Juan Picado @jotadeveloper 2018-04-04 19:44:49 +02:00 committed by GitHub
commit 6a5acd2c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 95 deletions

View File

@ -51,7 +51,7 @@
"@commitlint/cli": "6.1.3",
"@commitlint/config-conventional": "6.1.3",
"@commitlint/travis-cli": "6.1.3",
"@verdaccio/types": "2.0.2",
"@verdaccio/types": "2.0.3",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
"babel-eslint": "8.2.2",
@ -90,7 +90,7 @@
"eslint-plugin-react": "7.7.0",
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.11",
"flow-bin": "0.67.1",
"flow-bin": "0.69.0",
"flow-runtime": "0.17.0",
"friendly-errors-webpack-plugin": "1.6.1",
"github-markdown-css": "2.10.0",

View File

@ -4,7 +4,6 @@ import {loadPlugin} from '../lib/plugin-loader';
import Crypto from 'crypto';
import jwt from 'jsonwebtoken';
import {ErrorCode} from './utils';
const Error = require('http-errors');
import type {Config, Logger, Callback} from '@verdaccio/types';
import type {$Response, NextFunction} from 'express';
@ -47,7 +46,9 @@ class Auth {
return prev;
}, false);
if (ok) return cb(null, true);
if (ok) {
return cb(null, true);
}
if (user.name) {
cb(ErrorCode.get403('user ' + user.name + ' is not allowed to ' + action + ' package ' + pkg.name));
@ -373,7 +374,7 @@ class Auth {
try {
decoded = jwt.verify(token, this.secret);
} catch (err) {
throw Error[401](err.message);
throw ErrorCode.getCode(401, err.message);
}
return decoded;

View File

@ -8,6 +8,8 @@ const Utils = require('./utils');
const pkginfo = require('pkginfo')(module); // eslint-disable-line no-unused-vars
const pkgVersion = module.exports.version;
const pkgName = module.exports.name;
const strategicConfigProps = ['users', 'uplinks', 'packages'];
const allowedEnvConfig = ['http_proxy', 'https_proxy', 'no_proxy'];
/**
* [[a, [b, c]], d] -> [a, b, c, d]
@ -16,7 +18,7 @@ const pkgName = module.exports.name;
*/
function flatten(array) {
let result = [];
for (let i=0; i<array.length; i++) {
for (let i=0; i < array.length; i++) {
if (Array.isArray(array[i])) {
/* eslint prefer-spread: "off" */
result.push.apply(result, flatten(array[i]));
@ -27,6 +29,39 @@ function flatten(array) {
return result;
}
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;
}
/**
* Normalise user list.
* @return {Array}
*/
function normalizeUserlist() {
let result = [];
/* eslint prefer-rest-params: "off" */
for (let i=0; i < arguments.length; i++) {
if (arguments[i] == null) {
continue;
}
// if it's a string, split it to array
if (typeof(arguments[i]) === 'string') {
result.push(arguments[i].split(/\s+/));
} else if (Array.isArray(arguments[i])) {
result.push(arguments[i]);
} else {
throw Error('CONFIG: bad package acl (array or string expected): ' + JSON.stringify(arguments[i]));
}
}
return flatten(result);
}
/**
* Coordinates the application configuration
*/
@ -37,21 +72,6 @@ class Config {
*/
constructor(config) {
const self = this;
for (let i in config) {
if (self[i] == null) {
self[i] = config[i];
}
}
if (!self.user_agent) {
self.user_agent = `${pkgName}/${pkgVersion}`;
}
// some weird shell scripts are valid yaml files parsed as string
assert.equal(typeof(config), 'object', 'CONFIG: it doesn\'t look like a valid config file');
assert(self.storage, 'CONFIG: storage path not defined');
const users = {
all: true,
anonymous: true,
@ -60,23 +80,32 @@ class Config {
none: true,
};
const check_user_or_uplink = function(arg) {
assert(arg !== 'all' && arg !== 'owner'
&& arg !== 'anonymous' && arg !== 'undefined' && arg !== 'none', 'CONFIG: reserved user/uplink name: ' + arg);
assert(!arg.match(/\s/), 'CONFIG: invalid user name: ' + arg);
assert(users[arg] == null, 'CONFIG: duplicate user/uplink name: ' + arg);
users[arg] = true;
};
for (let configProp in config) {
if (self[configProp] == null) {
self[configProp] = config[configProp];
}
// sanity check for strategic config properties
['users', 'uplinks', 'packages'].forEach(function(x) {
}
if (!self.user_agent) {
self.user_agent = `${pkgName}/${pkgVersion}`;
}
// some weird shell scripts are valid yaml files parsed as string
assert.equal(typeof(config), 'object', 'CONFIG: it doesn\'t look like a valid config file');
assert(self.storage, 'CONFIG: storage path not defined');
// sanity check for strategic config properties
strategicConfigProps.forEach(function(x) {
if (self[x] == null) self[x] = {};
assert(Utils.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)) {
check_user_or_uplink(i);
checkUserOrUplink(i, users);
}
}
// sanity check for uplinks
@ -86,47 +115,26 @@ class Config {
self.uplinks[i].cache = true;
}
if (Object.prototype.hasOwnProperty.call(self.uplinks, i)) {
check_user_or_uplink(i);
}
}
for (let i in self.users) {
if (Object.prototype.hasOwnProperty.call(self.users, i)) {
assert(self.users[i].password, 'CONFIG: no password for user: ' + i);
assert(typeof(self.users[i].password) === 'string' &&
self.users[i].password.match(/^[a-f0-9]{40}$/)
, 'CONFIG: wrong password format for user: ' + i + ', sha1 expected');
}
}
for (let i in self.uplinks) {
if (Object.prototype.hasOwnProperty.call(self.uplinks, i)) {
assert(self.uplinks[i].url, 'CONFIG: no url for uplink: ' + i);
assert( typeof(self.uplinks[i].url) === 'string'
, 'CONFIG: wrong url format for uplink: ' + i);
self.uplinks[i].url = self.uplinks[i].url.replace(/\/$/, '');
checkUserOrUplink(i, users);
}
}
/**
* Normalise user list.
* @return {Array}
*/
function normalize_userlist() {
let result = [];
/* eslint prefer-rest-params: "off" */
for (let i=0; i<arguments.length; i++) {
if (arguments[i] == null) continue;
// if it's a string, split it to array
if (typeof(arguments[i]) === 'string') {
result.push(arguments[i].split(/\s+/));
} else if (Array.isArray(arguments[i])) {
result.push(arguments[i]);
} else {
throw Error('CONFIG: bad package acl (array or string expected): ' + JSON.stringify(arguments[i]));
}
for (let user in self.users) {
if (Object.prototype.hasOwnProperty.call(self.users, user)) {
assert(self.users[user].password, 'CONFIG: no password for user: ' + user);
assert(typeof(self.users[user].password) === 'string' &&
self.users[user].password.match(/^[a-f0-9]{40}$/)
, 'CONFIG: wrong password format for user: ' + user + ', sha1 expected');
}
}
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(/\/$/, '');
}
return flatten(result);
}
// add a default rule for all packages to make writing plugins easier
@ -134,35 +142,26 @@ class Config {
self.packages['**'] = {};
}
for (let i in self.packages) {
if (Object.prototype.hasOwnProperty.call(self.packages, i)) {
for (let pkg in self.packages) {
if (Object.prototype.hasOwnProperty.call(self.packages, pkg)) {
assert(
typeof(self.packages[i]) === 'object' &&
!Array.isArray(self.packages[i])
, 'CONFIG: bad "'+i+'" package description (object expected)');
typeof(self.packages[pkg]) === 'object' &&
!Array.isArray(self.packages[pkg])
, 'CONFIG: bad "'+pkg+'" package description (object expected)');
self.packages[i].access = normalize_userlist(
self.packages[i].allow_access,
self.packages[i].access
);
delete self.packages[i].allow_access;
self.packages[pkg].access = normalizeUserlist(self.packages[pkg].allow_access, self.packages[pkg].access);
delete self.packages[pkg].allow_access;
self.packages[i].publish = normalize_userlist(
self.packages[i].allow_publish,
self.packages[i].publish
);
delete self.packages[i].allow_publish;
self.packages[pkg].publish = normalizeUserlist(self.packages[pkg].allow_publish, self.packages[pkg].publish);
delete self.packages[pkg].allow_publish;
self.packages[i].proxy = normalize_userlist(
self.packages[i].proxy_access,
self.packages[i].proxy
);
delete self.packages[i].proxy_access;
self.packages[pkg].proxy = normalizeUserlist(self.packages[pkg].proxy_access, self.packages[pkg].proxy);
delete self.packages[pkg].proxy_access;
}
}
// loading these from ENV if aren't in config
['http_proxy', 'https_proxy', 'no_proxy'].forEach((function(v) {
allowedEnvConfig.forEach((function(v) {
if (!(v in self)) {
self[v] = process.env[v] || process.env[v.toUpperCase()];
}

View File

@ -242,9 +242,9 @@
version "1.0.0"
resolved "https://registry.npmjs.org/@verdaccio/streams/-/streams-1.0.0.tgz#d5d24c6747208728b9fd16b908e3932c3fb1f864"
"@verdaccio/types@2.0.2":
version "2.0.2"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-2.0.2.tgz#2a60faa458bbb5eaf3cdb6db1ef95c8d2e2fa5ed"
"@verdaccio/types@2.0.3":
version "2.0.3"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-2.0.3.tgz#ae72544af5f867975bc68d8862b040611f1b80ed"
JSONStream@1.3.2, JSONStream@^1.0.4:
version "1.3.2"
@ -3652,9 +3652,9 @@ flatten@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
flow-bin@0.67.1:
version "0.67.1"
resolved "https://registry.npmjs.org/flow-bin/-/flow-bin-0.67.1.tgz#eabb7197cce870ac9442cfd04251c7ddc30377db"
flow-bin@0.69.0:
version "0.69.0"
resolved "https://registry.npmjs.org/flow-bin/-/flow-bin-0.69.0.tgz#053159a684a6051fcbf0b71a2eb19a9679082da6"
flow-config-parser@^0.3.0:
version "0.3.0"