0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -05:00

Merge pull request #176 from verdaccio/clean-eslint

Clean eslint in a couple of files
This commit is contained in:
jotadeveloper 2017-04-27 06:58:04 +02:00 committed by GitHub
commit b264e9f3df
8 changed files with 569 additions and 391 deletions

View file

@ -1,209 +1,260 @@
/* eslint prefer-rest-params: "off" */
/* eslint prefer-spread: "off" */
'use strict'; 'use strict';
let assert = require('assert'); const assert = require('assert');
let Crypto = require('crypto'); const Crypto = require('crypto');
let Error = require('http-errors'); const Error = require('http-errors');
let minimatch = require('minimatch'); const minimatch = require('minimatch');
let Path = require('path'); const Path = require('path');
let LocalData = require('./local-data'); const LocalData = require('./local-data');
var Utils = require('./utils'); const Utils = require('./utils');
var Utils = require('./utils'); const pkginfo = require('pkginfo')(module); // eslint-disable-line no-unused-vars
let pkginfo = require('pkginfo')(module); // eslint-disable-line no-unused-vars const pkgVersion = module.exports.version;
let pkgVersion = module.exports.version; const pkgName = module.exports.name;
let pkgName = module.exports.name;
// [[a, [b, c]], d] -> [a, b, c, d] /**
* [[a, [b, c]], d] -> [a, b, c, d]
* @param {*} array
* @return {Array}
*/
function flatten(array) { function flatten(array) {
let result = []; let result = [];
for (let i=0; i<array.length; i++) { for (let i=0; i<array.length; i++) {
if (Array.isArray(array[i])) { if (Array.isArray(array[i])) {
result.push.apply(result, flatten(array[i])); result.push.apply(result, flatten(array[i]));
} else { } else {
result.push(array[i]); result.push(array[i]);
} }
} }
return result; return result;
} }
function Config(config) { const parse_interval_table = {
let self = Object.create(Config.prototype); '': 1000,
for (var i in config) { 'ms': 1,
if (self[i] == null) self[i] = config[i]; 's': 1000,
} 'm': 60*1000,
'h': 60*60*1000,
'd': 86400000,
'w': 7*86400000,
'M': 30*86400000,
'y': 365*86400000,
};
if (!self.user_agent) { /**
self.user_agent = `${pkgName}/${pkgVersion}`; * Parse an internal string to number
} * @param {*} interval
* @return {Number}
// 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'); function parse_interval(interval) {
if (typeof(interval) === 'number') {
assert(self.storage, 'CONFIG: storage path not defined'); return interval * 1000;
self.localList = new LocalData( }
Path.join( let result = 0;
Path.resolve(Path.dirname(self.self_path || ''), self.storage), let last_suffix = Infinity;
'.sinopia-db.json' interval.split(/\s+/).forEach(function(x) {
) if (!x) return;
); let m = x.match(/^((0|[1-9][0-9]*)(\.[0-9]+)?)(ms|s|m|h|d|w|M|y|)$/);
if (!self.secret) { if (!m
self.secret = self.localList.data.secret; || parse_interval_table[m[4]] >= last_suffix
|| (m[4] === '' && last_suffix !== Infinity)) {
if (!self.secret) { throw Error('invalid interval: ' + interval);
self.secret = Crypto.pseudoRandomBytes(32).toString('hex'); }
self.localList.data.secret = self.secret; last_suffix = parse_interval_table[m[4]];
self.localList.sync(); result += Number(m[1]) * parse_interval_table[m[4]];
} });
} return result;
let users = {
'all': true,
'anonymous': true,
'undefined': true,
'owner': true,
'none': true,
};
let 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;
}
;['users', 'uplinks', 'packages'].forEach(function(x) {
if (self[x] == null) self[x] = {};
assert(Utils.is_object(self[x]), 'CONFIG: bad "'+x+'" value (object expected)');
});
for (var i in self.users) {
check_user_or_uplink(i);
}
for (var i in self.uplinks) {
check_user_or_uplink(i);
}
for (var i in self.users) {
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 (var i in self.uplinks) {
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(/\/$/, '');
}
function normalize_userlist() {
let result = [];
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);
}
// add a default rule for all packages to make writing plugins easier
if (self.packages['**'] == null) {
self.packages['**'] = {};
}
for (var i in self.packages) {
assert(
typeof(self.packages[i]) === 'object' &&
!Array.isArray(self.packages[i])
, 'CONFIG: bad "'+i+'" 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[i].publish = normalize_userlist(
self.packages[i].allow_publish,
self.packages[i].publish
);
delete self.packages[i].allow_publish;
self.packages[i].proxy = normalize_userlist(
self.packages[i].proxy_access,
self.packages[i].proxy
);
delete self.packages[i].proxy_access;
}
// loading these from ENV if aren't in config
['http_proxy', 'https_proxy', 'no_proxy'].forEach((function(v) {
if (!(v in self)) {
self[v] = process.env[v] || process.env[v.toUpperCase()];
}
}));
// unique identifier of self server (or a cluster), used to avoid loops
if (!self.server_id) {
self.server_id = Crypto.pseudoRandomBytes(6).toString('hex');
}
return self;
} }
Config.prototype.can_proxy_to = function(pkg, uplink) { /**
return (this.get_package_spec(pkg).proxy || []).reduce(function(prev, curr) { * Coordinates the application configuration
if (uplink === curr) return true; */
return prev; class Config {
}, false);
};
Config.prototype.get_package_spec = function(pkg) { /**
for (let i in this.packages) { * Constructor
if (minimatch.makeRe(i).exec(pkg)) { * @param {*} config config the content
return this.packages[i]; */
} constructor(config) {
} const self = this;
return {}; 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');
// local data handler is linked with the configuration handler
self.localList = new LocalData(
Path.join(
Path.resolve(Path.dirname(self.self_path || ''), self.storage),
// FUTURE: the database might be parameterizable from config.yaml
'.sinopia-db.json'
)
);
// it generates a secret key
// FUTURE: this might be an external secret key, perhaps whitin config file?
if (!self.secret) {
self.secret = self.localList.data.secret;
if (!self.secret) {
self.secret = Crypto.pseudoRandomBytes(32).toString('hex');
self.localList.data.secret = self.secret;
self.localList.sync();
}
}
const users = {
'all': true,
'anonymous': true,
'undefined': true,
'owner': true,
'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;
}
// sanity check for strategic config properties
;['users', 'uplinks', 'packages'].forEach(function(x) {
if (self[x] == null) self[x] = {};
assert(Utils.is_object(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);
}
}
// sanity check for uplinks
for (let i in self.uplinks) {
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(/\/$/, '');
}
}
/**
* Normalise user list.
* @return {Array}
*/
function normalize_userlist() {
let result = [];
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);
}
// add a default rule for all packages to make writing plugins easier
if (self.packages['**'] == null) {
self.packages['**'] = {};
}
for (let i in self.packages) {
if (Object.prototype.hasOwnProperty.call(self.packages, i)) {
assert(
typeof(self.packages[i]) === 'object' &&
!Array.isArray(self.packages[i])
, 'CONFIG: bad "'+i+'" 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[i].publish = normalize_userlist(
self.packages[i].allow_publish,
self.packages[i].publish
);
delete self.packages[i].allow_publish;
self.packages[i].proxy = normalize_userlist(
self.packages[i].proxy_access,
self.packages[i].proxy
);
delete self.packages[i].proxy_access;
}
}
// loading these from ENV if aren't in config
['http_proxy', 'https_proxy', 'no_proxy'].forEach((function(v) {
if (!(v in self)) {
self[v] = process.env[v] || process.env[v.toUpperCase()];
}
}));
// unique identifier of self server (or a cluster), used to avoid loops
if (!self.server_id) {
self.server_id = Crypto.pseudoRandomBytes(6).toString('hex');
}
}
/**
* Check whether an uplink can proxy
* @param {*} pkg
* @param {*} uplink
* @return {Boolean}
*/
can_proxy_to(pkg, uplink) {
return (this.get_package_spec(pkg).proxy || []).reduce(function(prev, curr) {
if (uplink === curr) return true;
return prev;
}, false);
}
/**
* Check for package spec
* @param {*} pkg
* @return {Object}
*/
get_package_spec(pkg) {
for (let i in this.packages) {
if (minimatch.makeRe(i).exec(pkg)) {
return this.packages[i];
}
}
return {};
}
}
module.exports = Config; module.exports = Config;
module.exports.parse_interval = parse_interval;
let parse_interval_table = {
'': 1000,
'ms': 1,
's': 1000,
'm': 60*1000,
'h': 60*60*1000,
'd': 86400000,
'w': 7*86400000,
'M': 30*86400000,
'y': 365*86400000,
};
module.exports.parse_interval = function(interval) {
if (typeof(interval) === 'number') return interval * 1000;
let result = 0;
let last_suffix = Infinity;
interval.split(/\s+/).forEach(function(x) {
if (!x) return;
let m = x.match(/^((0|[1-9][0-9]*)(\.[0-9]+)?)(ms|s|m|h|d|w|M|y|)$/);
if (!m
|| parse_interval_table[m[4]] >= last_suffix
|| (m[4] === '' && last_suffix !== Infinity)) {
throw Error('invalid interval: ' + interval);
}
last_suffix = parse_interval_table[m[4]];
result += Number(m[1]) * parse_interval_table[m[4]];
});
return result;
};

View file

@ -13,7 +13,7 @@ let Storage = require('./storage');
module.exports = function(config_hash) { module.exports = function(config_hash) {
Logger.setup(config_hash.logs); Logger.setup(config_hash.logs);
let config = Config(config_hash); let config = new Config(config_hash);
let storage = new Storage(config); let storage = new Storage(config);
let auth = Auth(config); let auth = Auth(config);
let app = express(); let app = express();

View file

@ -1,7 +1,12 @@
'use strict'; 'use strict';
let Path = require('path'); const Path = require('path');
/**
* Requires a module.
* @param {*} path the module's path
* @return {Object}
*/
function try_load(path) { function try_load(path) {
try { try {
return require(path); return require(path);
@ -13,6 +18,17 @@ function try_load(path) {
} }
} }
/**
* Load a plugin following the rules
* - First try to load from the internal directory plugins (which will disappear soon or later).
* - A seccond attempt from node_modules, in case to have multiple match as for instance verdaccio-ldap
* and sinopia-ldap. All verdaccio prefix will have preferences.
* @param {*} config a reference of the configuration settings
* @param {*} plugin_configs
* @param {*} params a set of params to initialise the plugin
* @param {*} sanity_check callback that check the shape that should fulfill the plugin
* @return {Array} list of plugins
*/
function load_plugins(config, plugin_configs, params, sanity_check) { function load_plugins(config, plugin_configs, params, sanity_check) {
let plugins = Object.keys(plugin_configs || {}).map(function(p) { let plugins = Object.keys(plugin_configs || {}).map(function(p) {
let plugin; let plugin;

View file

@ -1,8 +1,17 @@
/* eslint no-invalid-this: "off" */
'use strict'; 'use strict';
const lunr = require('lunr'); const lunr = require('lunr');
/**
* Handle the search Indexer.
*/
class Search { class Search {
/**
* Constructor.
*/
constructor() { constructor() {
this.index = lunr(function() { this.index = lunr(function() {
this.field('name', {boost: 10}); this.field('name', {boost: 10});
@ -12,6 +21,13 @@ class Search {
}); });
} }
/**
* Performs a query to the indexer.
* If the keyword is a * it returns all local elements
* otherwise performs a search
* @param {*} q the keyword
* @return {Array} list of results.
*/
query(q) { query(q) {
return q === '*' return q === '*'
? this.storage.config.localList.get().map( function( pkg ) { ? this.storage.config.localList.get().map( function( pkg ) {
@ -19,6 +35,10 @@ class Search {
}) : this.index.search(q); }) : this.index.search(q);
} }
/**
* Add a new element to index
* @param {*} pkg the package
*/
add(pkg) { add(pkg) {
this.index.add({ this.index.add({
id: pkg.name, id: pkg.name,
@ -28,10 +48,17 @@ class Search {
}); });
} }
/**
* Remove an element from the index.
* @param {*} name the id element
*/
remove(name) { remove(name) {
this.index.remove({id: name}); this.index.remove({id: name});
} }
/**
* Force a reindex.
*/
reindex() { reindex() {
let self = this; let self = this;
this.storage.get_local(function(err, packages) { this.storage.get_local(function(err, packages) {
@ -43,6 +70,10 @@ class Search {
}); });
} }
/**
* Set up the {Storage}
* @param {*} storage An storage reference.
*/
configureStorage(storage) { configureStorage(storage) {
this.storage = storage; this.storage = storage;
this.reindex(); this.reindex();

View file

@ -1,3 +1,5 @@
/* eslint prefer-rest-params: "off" */
'use strict'; 'use strict';
// see https://secure.flickr.com/photos/girliemac/sets/72157628409467125 // see https://secure.flickr.com/photos/girliemac/sets/72157628409467125

View file

@ -1,16 +1,15 @@
'use strict'; 'use strict';
let Stream = require('stream'); const Stream = require('stream');
let Util = require('util'); const Util = require('util');
module.exports.ReadTarballStream = ReadTarball; /**
module.exports.UploadTarballStream = UploadTarball; * This stream is used to read tarballs from repository.
* @param {*} options
// * @return {Object}
// This stream is used to read tarballs from repository */
//
function ReadTarball(options) { function ReadTarball(options) {
let self = new Stream.PassThrough(options); const self = new Stream.PassThrough(options);
Object.setPrototypeOf(self, ReadTarball.prototype); Object.setPrototypeOf(self, ReadTarball.prototype);
// called when data is not needed anymore // called when data is not needed anymore
@ -21,11 +20,13 @@ function ReadTarball(options) {
Util.inherits(ReadTarball, Stream.PassThrough); Util.inherits(ReadTarball, Stream.PassThrough);
// /**
// This stream is used to upload tarballs to a repository * This stream is used to upload tarballs to a repository.
// * @param {*} options
* @return {Object}
*/
function UploadTarball(options) { function UploadTarball(options) {
let self = new Stream.PassThrough(options); const self = new Stream.PassThrough(options);
Object.setPrototypeOf(self, UploadTarball.prototype); Object.setPrototypeOf(self, UploadTarball.prototype);
// called when user closes connection before upload finishes // called when user closes connection before upload finishes
@ -39,10 +40,12 @@ function UploadTarball(options) {
Util.inherits(UploadTarball, Stream.PassThrough); Util.inherits(UploadTarball, Stream.PassThrough);
// /**
// This function intercepts abstract calls and replays them allowing * This function intercepts abstract calls and replays them allowing.
// us to attach those functions after we are ready to do so * us to attach those functions after we are ready to do so
// * @param {*} self
* @param {*} name
*/
function add_abstract_method(self, name) { function add_abstract_method(self, name) {
self._called_methods = self._called_methods || {}; self._called_methods = self._called_methods || {};
self.__defineGetter__(name, function() { self.__defineGetter__(name, function() {
@ -60,3 +63,5 @@ function add_abstract_method(self, name) {
}); });
} }
module.exports.ReadTarballStream = ReadTarball;
module.exports.UploadTarballStream = UploadTarball;

View file

@ -1,197 +1,270 @@
'use strict'; 'use strict';
let assert = require('assert'); const assert = require('assert');
let Semver = require('semver'); const Semver = require('semver');
let URL = require('url'); const URL = require('url');
let Logger = require('./logger'); const Logger = require('./logger');
module.exports.validate_package = function(name) { /**
name = name.split('/', 2); * Validate a package.
if (name.length === 1) { * @param {*} name
// normal package * @return {Boolean} whether the package is valid or not
return module.exports.validate_name(name[0]); */
} else { function validate_package(name) {
// scoped package name = name.split('/', 2);
return name[0][0] === '@' if (name.length === 1) {
&& module.exports.validate_name(name[0].slice(1)) // normal package
&& module.exports.validate_name(name[1]); return module.exports.validate_name(name[0]);
} } else {
}; // scoped package
return name[0][0] === '@'
&& module.exports.validate_name(name[0].slice(1))
&& module.exports.validate_name(name[1]);
}
}
// from normalize-package-data/lib/fixer.js /**
module.exports.validate_name = function(name) { * From normalize-package-data/lib/fixer.js
if (typeof(name) !== 'string') return false; * @param {*} name the package name
name = name.toLowerCase(); * @return {Boolean} whether is valid or not
*/
function validate_name(name) {
if (typeof(name) !== 'string') {
return false;
}
name = name.toLowerCase();
// all URL-safe characters and "@" for issue #75 // all URL-safe characters and "@" for issue #75
if (!name.match(/^[-a-zA-Z0-9_.!~*'()@]+$/) if (!name.match(/^[-a-zA-Z0-9_.!~*'()@]+$/)
|| name.charAt(0) === '.' // ".bin", etc. || name.charAt(0) === '.' // ".bin", etc.
|| name.charAt(0) === '-' // "-" is reserved by couchdb || name.charAt(0) === '-' // "-" is reserved by couchdb
|| name === 'node_modules' || name === 'node_modules'
|| name === '__proto__' || name === '__proto__'
|| name === 'package.json' || name === 'package.json'
|| name === 'favicon.ico' || name === 'favicon.ico'
) { ) {
return false; return false;
} else { } else {
return true; return true;
} }
}; }
module.exports.is_object = function(obj) { /**
return typeof(obj) === 'object' && obj !== null && !Array.isArray(obj); * Check whether an element is an Object
}; * @param {*} obj the element
* @return {Boolean}
*/
function is_object(obj) {
return typeof(obj) === 'object' && obj !== null && !Array.isArray(obj);
}
module.exports.validate_metadata = function(object, name) { /**
assert(module.exports.is_object(object), 'not a json object'); * Validate the package metadata, add additional properties whether are missing within
assert.equal(object.name, name); * the metadata properties.
* @param {*} object
* @param {*} name
* @return {Object} the object with additional properties as dist-tags ad versions
*/
function validate_metadata(object, name) {
assert(module.exports.is_object(object), 'not a json object');
assert.equal(object.name, name);
if (!module.exports.is_object(object['dist-tags'])) { if (!module.exports.is_object(object['dist-tags'])) {
object['dist-tags'] = {}; object['dist-tags'] = {};
} }
if (!module.exports.is_object(object['versions'])) { if (!module.exports.is_object(object['versions'])) {
object['versions'] = {}; object['versions'] = {};
} }
return object; return object;
}; }
module.exports.filter_tarball_urls = function(pkg, req, config) { /**
function filter(_url) { * Iterate a packages's versions and filter each original tarbal url.
if (!req.headers.host) return _url; * @param {*} pkg
* @param {*} req
* @param {*} config
* @return {String} a filtered package
*/
function filter_tarball_urls(pkg, req, config) {
/**
* Filter a tarball url.
* @param {*} _url
* @return {String} a parsed url
*/
const filter = function(_url) {
if (!req.headers.host) {
return _url;
}
const filename = URL.parse(_url).pathname.replace(/^.*\//, '');
let result;
if (config.url_prefix != null) {
result = config.url_prefix.replace(/\/$/, '');
} else {
result = `${req.protocol}://${req.headers.host}`;
}
return `${result}/${pkg.name.replace(/\//g, '%2f')}/-/${filename}`;
};
let filename = URL.parse(_url).pathname.replace(/^.*\//, ''); for (let ver in pkg.versions) {
if (Object.prototype.hasOwnProperty.call(pkg.versions, ver)) {
const dist = pkg.versions[ver].dist;
if (dist != null && dist.tarball != null) {
// dist.__verdaccio_orig_tarball = dist.tarball
dist.tarball = filter(dist.tarball);
}
}
}
return pkg;
}
if (config.url_prefix != null) { /**
var result = config.url_prefix.replace(/\/$/, ''); * Create a tag for a package
} else { * @param {*} data
var result = req.protocol + '://' + req.headers.host; * @param {*} version
} * @param {*} tag
* @return {Boolean} whether a package has been tagged
*/
function tag_version(data, version, tag) {
if (tag) {
if (data['dist-tags'][tag] !== version) {
if (Semver.parse(version, true)) {
// valid version - store
data['dist-tags'][tag] = version;
return true;
}
}
Logger.logger.warn({ver: version, tag: tag}, 'ignoring bad version @{ver} in @{tag}');
if (tag && data['dist-tags'][tag]) {
delete data['dist-tags'][tag];
}
}
return false;
}
return `${result}/${pkg.name.replace(/\//g, '%2f')}/-/${filename}`; /**
} * Gets version from a package object taking into account semver weirdness.
* @param {*} object
* @param {*} version
* @return {String} return the semantic version of a package
*/
function get_version(object, version) {
// this condition must allow cast
if (object.versions[version] != null) {
return object.versions[version];
}
try {
version = Semver.parse(version, true);
for (let k in object.versions) {
if (version.compare(Semver.parse(k, true)) === 0) {
return object.versions[k];
}
}
} catch (err) {
return undefined;
}
}
for (let ver in pkg.versions) { /**
let dist = pkg.versions[ver].dist; * Parse an internet address
if (dist != null && dist.tarball != null) { * Allow:
// dist.__verdaccio_orig_tarball = dist.tarball - https:localhost:1234 - protocol + host + port
dist.tarball = filter(dist.tarball); - localhost:1234 - host + port
} - 1234 - port
} - http::1234 - protocol + port
return pkg; - https://localhost:443/ - full url + https
}; - http://[::1]:443/ - ipv6
- unix:/tmp/http.sock - unix sockets
- https://unix:/tmp/http.sock - unix sockets (https)
* @param {*} addr the internet address definition
* @return {Object|Null} literal object that represent the address parsed
*/
function parse_address(addr) {
//
// TODO: refactor it to something more reasonable?
//
// protocol : // ( host )|( ipv6 ): port /
let m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr);
module.exports.tag_version = function(data, version, tag) { if (m) return {
if (tag) { proto: m[2] || 'http',
if (data['dist-tags'][tag] !== version) { host: m[6] || m[7] || 'localhost',
if (Semver.parse(version, true)) { port: m[8] || '4873',
// valid version - store };
data['dist-tags'][tag] = version;
return true;
}
}
Logger.logger.warn({ver: version, tag: tag}, 'ignoring bad version @{ver} in @{tag}');
if (tag && data['dist-tags'][tag]) {
delete data['dist-tags'][tag];
}
}
return false;
};
// gets version from a package object taking into account semver weirdness m = /^((https?):(\/\/)?)?unix:(.*)$/.exec(addr);
module.exports.get_version = function(object, version) {
if (object.versions[version] != null) return object.versions[version];
try { if (m) {
version = Semver.parse(version, true); return {
for (let k in object.versions) { proto: m[2] || 'http',
if (version.compare(Semver.parse(k, true)) === 0) { path: m[4],
return object.versions[k]; };
} }
}
} catch (err) {
return undefined;
}
};
module.exports.parse_address = function parse_address(addr) { return null;
// }
// Allow:
//
// - https:localhost:1234 - protocol + host + port
// - localhost:1234 - host + port
// - 1234 - port
// - http::1234 - protocol + port
// - https://localhost:443/ - full url + https
// - http://[::1]:443/ - ipv6
// - unix:/tmp/http.sock - unix sockets
// - https://unix:/tmp/http.sock - unix sockets (https)
//
// TODO: refactor it to something more reasonable?
//
// protocol : // ( host )|( ipv6 ): port /
var m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr);
if (m) return { /**
proto: m[2] || 'http', * Function filters out bad semver versions and sorts the array.
host: m[6] || m[7] || 'localhost', * @param {*} array
port: m[8] || '4873', * @return {Array} sorted Array
}; */
function semver_sort(array) {
return array
.filter(function(x) {
if (!Semver.parse(x, true)) {
Logger.logger.warn( {ver: x}, 'ignoring bad version @{ver}' );
return false;
}
return true;
})
.sort(Semver.compareLoose)
.map(String);
}
var m = /^((https?):(\/\/)?)?unix:(.*)$/.exec(addr); /**
* Flatten arrays of tags.
* @param {*} data
*/
function normalize_dist_tags(data) {
let sorted;
if (!data['dist-tags'].latest) {
// overwrite latest with highest known version based on semver sort
sorted = module.exports.semver_sort(Object.keys(data.versions));
if (sorted && sorted.length) {
data['dist-tags'].latest = sorted.pop();
}
}
if (m) return { for (let tag in data['dist-tags']) {
proto: m[2] || 'http', if (Array.isArray(data['dist-tags'][tag])) {
path: m[4], if (data['dist-tags'][tag].length) {
}; // sort array
sorted = module.exports.semver_sort(data['dist-tags'][tag]);
if (sorted.length) {
// use highest version based on semver sort
data['dist-tags'][tag] = sorted.pop();
}
} else {
delete data['dist-tags'][tag];
}
} else if (typeof data['dist-tags'][tag] === 'string') {
if (!Semver.parse(data['dist-tags'][tag], true)) {
// if the version is invalid, delete the dist-tag entry
delete data['dist-tags'][tag];
}
}
}
}
return null; module.exports.semver_sort = semver_sort;
}; module.exports.parse_address = parse_address;
module.exports.get_version = get_version;
// function filters out bad semver versions and sorts the array module.exports.normalize_dist_tags = normalize_dist_tags;
module.exports.semver_sort = function semver_sort(array) { module.exports.tag_version = tag_version;
return array module.exports.filter_tarball_urls = filter_tarball_urls;
.filter(function(x) { module.exports.validate_metadata = validate_metadata;
if (!Semver.parse(x, true)) { module.exports.is_object = is_object;
Logger.logger.warn( {ver: x}, 'ignoring bad version @{ver}' ); module.exports.validate_name = validate_name;
return false; module.exports.validate_package = validate_package;
}
return true;
})
.sort(Semver.compareLoose)
.map(String);
};
// flatten arrays of tags
module.exports.normalize_dist_tags = function(data) {
let sorted;
if (!data['dist-tags'].latest) {
// overwrite latest with highest known version based on semver sort
sorted = module.exports.semver_sort(Object.keys(data.versions));
if (sorted && sorted.length) {
data['dist-tags'].latest = sorted.pop();
}
}
for (let tag in data['dist-tags']) {
if (Array.isArray(data['dist-tags'][tag])) {
if (data['dist-tags'][tag].length) {
// sort array
sorted = module.exports.semver_sort(data['dist-tags'][tag]);
if (sorted.length) {
// use highest version based on semver sort
data['dist-tags'][tag] = sorted.pop();
}
} else {
delete data['dist-tags'][tag];
}
} else if (typeof data['dist-tags'][tag] === 'string') {
if (!Semver.parse(data['dist-tags'][tag], true)) {
// if the version is invalid, delete the dist-tag entry
delete data['dist-tags'][tag];
}
}
}
};

View file

@ -34,7 +34,7 @@ let packages = [
describe('search', function() { describe('search', function() {
before(function() { before(function() {
let config = Config(config_hash); let config = new Config(config_hash);
this.storage = new Storage(config); this.storage = new Storage(config);
Search.configureStorage(this.storage); Search.configureStorage(this.storage);
packages.map(function(item) { packages.map(function(item) {