2013-10-26 14:18:36 +02:00
|
|
|
var assert = require('assert')
|
2013-12-27 14:06:30 +01:00
|
|
|
, semver = require('semver')
|
|
|
|
, Logger = require('./logger')
|
2013-10-26 14:18:36 +02:00
|
|
|
, URL = require('url')
|
2013-05-31 08:26:11 +02:00
|
|
|
|
|
|
|
// from normalize-package-data/lib/fixer.js
|
|
|
|
module.exports.validate_name = function(name) {
|
2013-12-15 00:09:55 +01:00
|
|
|
if (typeof(name) !== 'string') return false
|
2013-10-26 14:18:36 +02:00
|
|
|
name = name.toLowerCase()
|
2013-05-31 08:26:11 +02:00
|
|
|
if (
|
2014-02-23 18:20:50 +01:00
|
|
|
name.charAt(0) === '.' || // ".bin", etc.
|
|
|
|
name.charAt(0) === '-' || // "-" is reserved by couchdb
|
2013-05-31 08:26:11 +02:00
|
|
|
name.match(/[\/@\s\+%:]/) ||
|
|
|
|
name !== encodeURIComponent(name) ||
|
2014-02-23 18:20:50 +01:00
|
|
|
name === 'node_modules' ||
|
|
|
|
name === '__proto__' ||
|
|
|
|
name === 'package.json' ||
|
|
|
|
name === 'favicon.ico'
|
2013-05-31 08:26:11 +02:00
|
|
|
) {
|
2013-10-26 14:18:36 +02:00
|
|
|
return false
|
2013-05-31 08:26:11 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
return true
|
2013-05-31 08:26:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 09:29:57 +02:00
|
|
|
module.exports.is_object = function(obj) {
|
|
|
|
return typeof(obj) === 'object' && obj !== null && !Array.isArray(obj)
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.validate_metadata = function(object, name) {
|
2013-12-07 23:55:50 +01:00
|
|
|
assert(module.exports.is_object(object), 'not a json object')
|
2013-10-26 14:18:36 +02:00
|
|
|
assert.equal(object.name, name)
|
|
|
|
|
2013-10-22 09:29:57 +02:00
|
|
|
if (!module.exports.is_object(object['dist-tags'])) {
|
2013-10-26 14:18:36 +02:00
|
|
|
object['dist-tags'] = {}
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
|
2013-10-22 09:29:57 +02:00
|
|
|
if (!module.exports.is_object(object['versions'])) {
|
2013-10-26 14:18:36 +02:00
|
|
|
object['versions'] = {}
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
|
|
|
|
return object
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 18:58:16 +02:00
|
|
|
module.exports.parse_tarball_url = function(_url) {
|
2013-10-26 14:18:36 +02:00
|
|
|
var url = URL.parse(_url)
|
2013-06-19 18:58:16 +02:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
var path = url.path.replace(/^\//, '').split('/')
|
2013-06-19 18:58:16 +02:00
|
|
|
if (path.length >= 3 && path[path.length-2] === '-') {
|
2013-10-26 14:18:36 +02:00
|
|
|
var filename = path.pop()
|
|
|
|
, pkgpath = '/' + filename // tarball name
|
|
|
|
pkgpath = '/' + path.pop() + pkgpath // "-"
|
|
|
|
pkgpath = '/' + path.pop() + pkgpath // package.name
|
2013-06-19 18:58:16 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
return null
|
2013-06-19 18:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
protocol: url.protocol,
|
|
|
|
host: url.host,
|
|
|
|
prepath: '/' + path.join('/'),
|
|
|
|
pkgpath: pkgpath,
|
|
|
|
filename: filename,
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-06-19 18:58:16 +02:00
|
|
|
}
|
|
|
|
|
2013-06-18 20:14:55 +02:00
|
|
|
module.exports.filter_tarball_urls = function(pkg, req, config) {
|
|
|
|
function filter(_url) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (!req.headers.host) return _url
|
2013-06-18 20:14:55 +02:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
var url = module.exports.parse_tarball_url(_url)
|
2013-06-19 18:58:16 +02:00
|
|
|
// weird url, just return it
|
2013-10-26 14:18:36 +02:00
|
|
|
if (url == null) return _url
|
2013-06-19 18:58:16 +02:00
|
|
|
|
2013-06-18 20:14:55 +02:00
|
|
|
if (config.url_prefix != null) {
|
2013-10-26 14:18:36 +02:00
|
|
|
var result = config.url_prefix.replace(/\/$/, '')
|
2013-06-18 20:14:55 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
var result = req.protocol + '://' + req.headers.host
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
return result + url.pkgpath
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var ver in pkg.versions) {
|
2013-12-23 01:14:57 +01:00
|
|
|
var dist = pkg.versions[ver].dist
|
|
|
|
if (dist != null && dist.tarball != null) {
|
2014-04-13 04:04:48 +02:00
|
|
|
//dist.__sinopia_orig_tarball = dist.tarball
|
2013-12-23 01:14:57 +01:00
|
|
|
dist.tarball = filter(dist.tarball)
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
return pkg
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-03-29 03:31:34 +01:00
|
|
|
function can_add_tag(tag, config) {
|
|
|
|
if (!tag) return false
|
|
|
|
if (tag === 'latest' && config.ignore_latest_tag) return false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.tag_version = function(data, version, tag, config) {
|
|
|
|
if (!can_add_tag(tag, config)) return
|
|
|
|
|
2013-12-27 14:06:30 +01:00
|
|
|
switch(typeof(data['dist-tags'][tag])) {
|
|
|
|
case 'string':
|
|
|
|
data['dist-tags'][tag] = [data['dist-tags'][tag]]
|
|
|
|
break
|
|
|
|
case 'object': // array
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
data['dist-tags'][tag] = []
|
|
|
|
}
|
|
|
|
if (data['dist-tags'][tag].indexOf(version) === -1) {
|
|
|
|
data['dist-tags'][tag].push(version)
|
|
|
|
data['dist-tags'][tag] = module.exports.semver_sort(data['dist-tags'][tag])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-18 19:57:44 +01:00
|
|
|
// 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]
|
|
|
|
|
|
|
|
try {
|
|
|
|
version = semver.parse(version, true)
|
|
|
|
for (var k in object.versions) {
|
|
|
|
if (version.compare(semver.parse(k, true)) === 0) {
|
|
|
|
return object.versions[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 14:06:30 +01:00
|
|
|
// function filters out bad semver versions and sorts the array
|
|
|
|
module.exports.semver_sort = 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)
|
|
|
|
}
|
|
|
|
|