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:
parent
7dde31546e
commit
5a17a04090
1 changed files with 241 additions and 169 deletions
170
lib/utils.js
170
lib/utils.js
|
@ -5,7 +5,12 @@ let Semver = require('semver');
|
|||
let URL = require('url');
|
||||
let Logger = require('./logger');
|
||||
|
||||
module.exports.validate_package = function(name) {
|
||||
/**
|
||||
* Validate a package.
|
||||
* @param {*} name
|
||||
* @return {Boolean} whether the package is valid or not
|
||||
*/
|
||||
function validate_package(name) {
|
||||
name = name.split('/', 2);
|
||||
if (name.length === 1) {
|
||||
// normal package
|
||||
|
@ -16,11 +21,17 @@ module.exports.validate_package = function(name) {
|
|||
&& 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) {
|
||||
if (typeof(name) !== 'string') return false;
|
||||
/**
|
||||
* From normalize-package-data/lib/fixer.js
|
||||
* @param {*} name the package name
|
||||
* @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
|
||||
|
@ -36,13 +47,25 @@ module.exports.validate_name = function(name) {
|
|||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.is_object = function(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) {
|
||||
/**
|
||||
* Validate the package metadata, add additional properties whether are missing within
|
||||
* 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);
|
||||
|
||||
|
@ -55,22 +78,34 @@ module.exports.validate_metadata = function(object, name) {
|
|||
}
|
||||
|
||||
return object;
|
||||
};
|
||||
|
||||
module.exports.filter_tarball_urls = function(pkg, req, config) {
|
||||
function filter(_url) {
|
||||
if (!req.headers.host) return _url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterate a packages's versions and filter each original tarbal 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) {
|
||||
var result = config.url_prefix.replace(/\/$/, '');
|
||||
result = config.url_prefix.replace(/\/$/, '');
|
||||
} else {
|
||||
var result = req.protocol + '://' + req.headers.host;
|
||||
result = req.protocol + '://' + req.headers.host;
|
||||
}
|
||||
|
||||
return `${result}/${pkg.name.replace(/\//g, '%2f')}/-/${filename}`;
|
||||
}
|
||||
};
|
||||
|
||||
for (let ver in pkg.versions) {
|
||||
let dist = pkg.versions[ver].dist;
|
||||
|
@ -80,9 +115,16 @@ module.exports.filter_tarball_urls = function(pkg, req, config) {
|
|||
}
|
||||
}
|
||||
return pkg;
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.tag_version = function(data, version, tag) {
|
||||
/**
|
||||
* Create a tag for a package
|
||||
* @param {*} data
|
||||
* @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)) {
|
||||
|
@ -97,12 +139,19 @@ module.exports.tag_version = function(data, version, tag) {
|
|||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// gets version from a package object taking into account semver weirdness
|
||||
module.exports.get_version = function(object, version) {
|
||||
if (object.versions[version] != null) return object.versions[version];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
@ -113,25 +162,28 @@ module.exports.get_version = function(object, version) {
|
|||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.parse_address = function parse_address(addr) {
|
||||
//
|
||||
// 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)
|
||||
/**
|
||||
* Parse an internet address
|
||||
* 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)
|
||||
* @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 /
|
||||
var m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr);
|
||||
let m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr);
|
||||
|
||||
if (m) return {
|
||||
proto: m[2] || 'http',
|
||||
|
@ -139,18 +191,24 @@ module.exports.parse_address = function parse_address(addr) {
|
|||
port: m[8] || '4873',
|
||||
};
|
||||
|
||||
var m = /^((https?):(\/\/)?)?unix:(.*)$/.exec(addr);
|
||||
m = /^((https?):(\/\/)?)?unix:(.*)$/.exec(addr);
|
||||
|
||||
if (m) return {
|
||||
if (m) {
|
||||
return {
|
||||
proto: m[2] || 'http',
|
||||
path: m[4],
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
// function filters out bad semver versions and sorts the array
|
||||
module.exports.semver_sort = function semver_sort(array) {
|
||||
/**
|
||||
* Function filters out bad semver versions and sorts the array.
|
||||
* @param {*} array
|
||||
* @return {Array} sorted Array
|
||||
*/
|
||||
function semver_sort(array) {
|
||||
return array
|
||||
.filter(function(x) {
|
||||
if (!Semver.parse(x, true)) {
|
||||
|
@ -161,10 +219,13 @@ module.exports.semver_sort = function semver_sort(array) {
|
|||
})
|
||||
.sort(Semver.compareLoose)
|
||||
.map(String);
|
||||
};
|
||||
}
|
||||
|
||||
// flatten arrays of tags
|
||||
module.exports.normalize_dist_tags = function(data) {
|
||||
/**
|
||||
* Flatten arrays of tags.
|
||||
* @param {*} data
|
||||
*/
|
||||
function normalize_dist_tags(data) {
|
||||
let sorted;
|
||||
|
||||
if (!data['dist-tags'].latest) {
|
||||
|
@ -194,4 +255,15 @@ module.exports.normalize_dist_tags = function(data) {
|
|||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.semver_sort = semver_sort;
|
||||
module.exports.parse_address = parse_address;
|
||||
module.exports.get_version = get_version;
|
||||
module.exports.normalize_dist_tags = normalize_dist_tags;
|
||||
module.exports.tag_version = tag_version;
|
||||
module.exports.filter_tarball_urls = filter_tarball_urls;
|
||||
module.exports.validate_metadata = validate_metadata;
|
||||
module.exports.is_object = is_object;
|
||||
module.exports.validate_name = validate_name;
|
||||
module.exports.validate_package = validate_package;
|
||||
|
|
Loading…
Add table
Reference in a new issue