0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/lib/utils.js

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2013-05-31 17:57:28 -05:00
var assert = require('assert');
2013-06-18 13:14:55 -05:00
var URL = require('url');
2013-05-31 01:26:11 -05:00
// from normalize-package-data/lib/fixer.js
module.exports.validate_name = function(name) {
name = name.toLowerCase();
2013-05-31 01:26:11 -05:00
if (
name.charAt(0) === "." || // ".bin", etc.
2013-05-31 01:26:11 -05:00
name.match(/[\/@\s\+%:]/) ||
name !== encodeURIComponent(name) ||
name.toLowerCase() === "node_modules" ||
name.toLowerCase() === "__proto__" ||
2013-05-31 01:26:11 -05:00
name.toLowerCase() === "favicon.ico"
) {
return false;
} else {
return true;
}
}
2013-05-31 17:57:28 -05:00
function is_object(obj) {
return typeof(obj) === 'object' && !Array.isArray(obj);
}
module.exports.validate_metadata = function(object, name) {
assert(is_object(object));
assert.equal(object.name, name);
if (!is_object(object['dist-tags'])) {
object['dist-tags'] = {};
}
if (!is_object(object['versions'])) {
object['versions'] = {};
}
return object;
}
2013-06-19 11:58:16 -05:00
module.exports.parse_tarball_url = function(_url) {
var url = URL.parse(_url);
var path = url.path.replace(/^\//, '').split('/');
if (path.length >= 3 && path[path.length-2] === '-') {
var filename = path.pop();
var pkgpath = '/' + filename; // tarball name
pkgpath = '/' + path.pop() + pkgpath; // "-"
pkgpath = '/' + path.pop() + pkgpath; // package.name
} else {
return null;
}
return {
protocol: url.protocol,
host: url.host,
prepath: '/' + path.join('/'),
pkgpath: pkgpath,
filename: filename,
};
}
2013-06-18 13:14:55 -05:00
module.exports.filter_tarball_urls = function(pkg, req, config) {
function filter(_url) {
if (!req.headers.host) return _url;
2013-06-19 11:58:16 -05:00
var url = module.exports.parse_tarball_url(_url);
// weird url, just return it
if (url == null) return _url;
2013-06-18 13:14:55 -05:00
if (config.url_prefix != null) {
var result = config.url_prefix.replace(/\/$/, '');
} else {
var result = req.protocol + '://' + req.headers.host;
}
2013-06-19 11:58:16 -05:00
return result + url.pkgpath;
2013-06-18 13:14:55 -05:00
}
for (var ver in pkg.versions) {
if (pkg.versions[ver].dist != null
&& pkg.versions[ver].dist.tarball != null) {
2013-06-19 11:58:16 -05:00
pkg.versions[ver].dist.__sinopia_orig_tarball = pkg.versions[ver].dist.tarball;
2013-06-18 13:14:55 -05:00
pkg.versions[ver].dist.tarball = filter(pkg.versions[ver].dist.tarball);
}
}
return pkg;
}