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

Add jsdoc and fix lint warnings for utils.js

This commit is contained in:
Juan Picado 2017-04-27 00:35:25 +02:00
parent 7dde31546e
commit 5a17a04090
No known key found for this signature in database
GPG key ID: 18AC54485952D158

View file

@ -5,193 +5,265 @@ let Semver = require('semver');
let URL = require('url'); let URL = require('url');
let Logger = require('./logger'); let 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;
}
let 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) {
let 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 (m) return { if (!data['dist-tags'].latest) {
proto: m[2] || 'http', // overwrite latest with highest known version based on semver sort
path: m[4], sorted = module.exports.semver_sort(Object.keys(data.versions));
}; if (sorted && sorted.length) {
data['dist-tags'].latest = sorted.pop();
}
}
return null; 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];
}
}
}
}
// function filters out bad semver versions and sorts the array module.exports.semver_sort = semver_sort;
module.exports.semver_sort = function semver_sort(array) { module.exports.parse_address = parse_address;
return array module.exports.get_version = get_version;
.filter(function(x) { module.exports.normalize_dist_tags = normalize_dist_tags;
if (!Semver.parse(x, true)) { module.exports.tag_version = tag_version;
Logger.logger.warn( {ver: x}, 'ignoring bad version @{ver}' ); module.exports.filter_tarball_urls = filter_tarball_urls;
return false; module.exports.validate_metadata = validate_metadata;
} module.exports.is_object = is_object;
return true; module.exports.validate_name = validate_name;
}) module.exports.validate_package = validate_package;
.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];
}
}
}
};