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) {
|
2013-09-27 04:20:38 -05:00
|
|
|
name = name.toLowerCase();
|
2013-05-31 01:26:11 -05:00
|
|
|
if (
|
2013-09-27 04:20:38 -05:00
|
|
|
name.charAt(0) === "." || // ".bin", etc.
|
2013-05-31 01:26:11 -05:00
|
|
|
name.match(/[\/@\s\+%:]/) ||
|
|
|
|
name !== encodeURIComponent(name) ||
|
|
|
|
name.toLowerCase() === "node_modules" ||
|
2013-09-27 04:20:38 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|