0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-30 22:34:10 -05:00

refactoring: move uplink.get_package() calls to it's own function

This commit is contained in:
Alex Kocharin 2013-12-13 20:50:41 +04:00
parent 19e4d78197
commit ede09b5c77

View file

@ -1,5 +1,6 @@
var async = require('async')
, semver = require('semver')
, assert = require('assert')
, UError = require('./error').UserError
, Local = require('./local-storage')
, Proxy = require('./up-storage')
@ -53,17 +54,36 @@ Storage.prototype.add_package = function(name, metadata, callback) {
// - when we publishing package, we only publish it to some of them
// so all requests are necessary
check_package(function(err) {
check_package_local(function(err) {
if (err) return callback(err)
publish_package(function(err) {
check_package_remote(function(err) {
if (err) return callback(err)
callback()
publish_package(function(err) {
if (err) return callback(err)
callback()
})
})
})
function check_package(cb) {
self.get_package(name, function(err, results, err_results) {
function check_package_local(cb) {
self.local.get_package(name, {}, function(err, results) {
if (err && err.status !== 404) return cb(err)
if (results) {
return cb(new UError({
status: 409,
msg: 'this package is already present'
}))
}
cb()
})
}
function check_package_remote(cb) {
self._sync_package_with_uplinks(name, null, {}, function(err, results, err_results) {
// something weird
if (err && err.status !== 404) return cb(err)
@ -388,9 +408,6 @@ Storage.prototype.get_tarball = function(name, filename) {
Storage.prototype.get_package = function(name, options, callback) {
if (typeof(options) === 'function') callback = options, options = {}
// NOTE: callback(err, result, _uplink_errors)
// _uplink_errors is an array of errors used internally
// XXX: move it to another function maybe?
var self = this
self.local.get_package(name, options, function(err, data) {
@ -399,86 +416,102 @@ Storage.prototype.get_package = function(name, options, callback) {
return cb(err)
}
var uplinks = []
for (var i in self.uplinks) {
if (self.config.proxy_access(name, i)) {
uplinks.push(self.uplinks[i])
self._sync_package_with_uplinks(name, data, options, function(err, result, uplink_errors) {
if (err) return callback(err)
var whitelist = ['_rev', 'name', 'versions', 'dist-tags']
for (var i in result) {
if (!~whitelist.indexOf(i)) delete result[i]
}
}
var result = data || {
result['dist-tags'].latest = Storage._semver_sort(Object.keys(result.versions))
for (var i in result['dist-tags']) {
if (Array.isArray(result['dist-tags'][i])) {
result['dist-tags'][i] = result['dist-tags'][i][result['dist-tags'][i].length-1]
if (result['dist-tags'][i] == null) delete result['dist-tags'][i]
}
}
callback(null, result, uplink_errors)
})
})
}
// function fetches package information from uplinks and synchronizes it with local data
// if package is available locally, it MUST be provided in pkginfo
// returns callback(err, result, uplink_errors)
Storage.prototype._sync_package_with_uplinks = function(name, pkginfo, options, callback) {
var self = this
if (!pkginfo) {
var exists = false
pkginfo = {
name: name,
versions: {},
'dist-tags': {},
_uplinks: {},
}
var exists = !err
var latest = result['dist-tags'].latest
} else {
var exists = true
}
async.map(uplinks, function(up, cb) {
var _options = Object.create(options)
if (utils.is_object(result._uplinks[up.upname]))
_options.etag = result._uplinks[up.upname].etag
var uplinks = []
for (var i in self.uplinks) {
if (self.config.proxy_access(name, i)) {
uplinks.push(self.uplinks[i])
}
}
up.get_package(name, _options, function(err, up_res, etag) {
if (err || !up_res) return cb(null, [err || new Error('no data')])
async.map(uplinks, function(up, cb) {
var _options = Object.create(options)
if (utils.is_object(pkginfo._uplinks[up.upname]))
_options.etag = pkginfo._uplinks[up.upname].etag
try {
utils.validate_metadata(up_res, name)
} catch(err) {
self.logger.error({
sub: 'out',
err: err,
}, 'package.json validating error @{!err.message}\n@{err.stack}')
return cb(null, [err])
}
up.get_package(name, _options, function(err, up_res, etag) {
if (err || !up_res) return cb(null, [err || new Error('no data')])
result._uplinks[up.upname] = {
etag: etag
}
try {
Storage._merge_versions(result, up_res)
} catch(err) {
self.logger.error({
sub: 'out',
err: err,
}, 'package.json parsing error @{!err.message}\n@{err.stack}')
return cb(null, [err])
}
// if we got to this point, assume that the correct package exists
// on the uplink
exists = true
cb()
})
}, function(err, uplink_errors) {
if (err) return callback(err)
if (!exists) {
return callback(new UError({
status: 404,
msg: 'no such package available'
}), null, uplink_errors)
try {
utils.validate_metadata(up_res, name)
} catch(err) {
self.logger.error({
sub: 'out',
err: err,
}, 'package.json validating error @{!err.message}\n@{err.stack}')
return cb(null, [err])
}
self.local.update_versions(name, result, function(err) {
if (err) return callback(err)
pkginfo._uplinks[up.upname] = {
etag: etag
}
var whitelist = ['_rev', 'name', 'versions', 'dist-tags']
for (var i in result) {
if (!~whitelist.indexOf(i)) delete result[i]
}
try {
Storage._merge_versions(pkginfo, up_res)
} catch(err) {
self.logger.error({
sub: 'out',
err: err,
}, 'package.json parsing error @{!err.message}\n@{err.stack}')
return cb(null, [err])
}
result['dist-tags'].latest = Storage._semver_sort(Object.keys(result.versions))
for (var i in result['dist-tags']) {
if (Array.isArray(result['dist-tags'][i])) {
result['dist-tags'][i] = result['dist-tags'][i][result['dist-tags'][i].length-1]
if (result['dist-tags'][i] == null) delete result['dist-tags'][i]
}
}
// if we got to this point, assume that the correct package exists
// on the uplink
exists = true
cb()
})
}, function(err, uplink_errors) {
assert(!err && Array.isArray(uplink_errors))
callback(null, result, uplink_errors)
})
if (!exists) {
return callback(new UError({
status: 404,
msg: 'no such package available'
}), null, uplink_errors)
}
self.local.update_versions(name, pkginfo, function(err) {
if (err) return callback(err)
return callback(null, pkginfo, uplink_errors)
})
})
}