var async = require('async') , semver = require('semver') , UError = require('./error').UserError , Local = require('./local-storage') , Proxy = require('./up-storage') , mystreams = require('./streams') , utils = require('./utils') , transaction = require('./transaction') , Logger = require('./logger') // // Implements Storage interface // (same for storage.js, local-storage.js, up-storage.js) // function Storage(config) { if (!(this instanceof Storage)) return new Storage(config) this.config = config // we support a number of uplinks, but only one local storage // Proxy and Local classes should have similar API interfaces this.uplinks = {} for (var p in config.uplinks) { this.uplinks[p] = new Proxy(config.uplinks[p], config) this.uplinks[p].upname = p } this.local = new Local(config) this.logger = Logger.logger.child() return this } // // Add a {name} package to a system // // Function checks if package with the same name is available from uplinks. // If it isn't, we create package metadata locally and send requests to do // the same to all uplinks with write access. If all actions succeeded, we // report success, if just one uplink fails, we abort. // // TODO: if a package is uploaded to uplink1, but upload to uplink2 fails, // we report failure, but package is not removed from uplink1. This might // require manual intervention. // // Used storages: local (write) && uplinks (proxy_access, r/o) && // uplinks (proxy_publish, write) // Storage.prototype.add_package = function(name, metadata, callback) { var self = this // NOTE: // - when we checking package for existance, we ask ALL uplinks // - when we publishing package, we only publish it to some of them // so all requests are necessary check_package(function(err) { if (err) return callback(err) publish_package(function(err) { if (err) return callback(err) callback() }) }) function check_package(cb) { self.get_package(name, function(err, results, err_results) { // something weird if (err && err.status !== 404) return cb(err) // checking package if (results) { return cb(new UError({ status: 409, msg: 'this package is already present' })) } for (var i=0; i= 500)) { // report internal errors right away return cb(err) } var uplinks = [] for (var i in self.uplinks) { if (self.config.proxy_access(name, i)) { uplinks.push(self.uplinks[i]) } } var result = data || { name: name, versions: {}, 'dist-tags': {}, _uplinks: {}, } var exists = !err var latest = result['dist-tags'].latest 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 up.get_package(name, _options, function(err, up_res, etag) { if (err || !up_res) return cb(null, [err || new Error('no data')]) 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]) } 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) } self.local.update_versions(name, result, function(err) { if (err) return callback(err) var whitelist = ['_rev', 'name', 'versions', 'dist-tags'] for (var i in result) { if (!~whitelist.indexOf(i)) delete result[i] } 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 gets a local info and an info from uplinks and tries to merge it // exported for unit tests only Storage._merge_versions = function(local, up) { // copy new versions to a cache // NOTE: if a certain version was updated, we can't refresh it reliably for (var i in up.versions) { if (local.versions[i] == null) { local.versions[i] = up.versions[i] } } // refresh dist-tags for (var i in up['dist-tags']) { if (i === 'latest') continue switch(typeof(local['dist-tags'][i])) { case 'string': local['dist-tags'][i] = [local['dist-tags'][i]] break case 'object': // array break default: local['dist-tags'][i] = [] } if (local['dist-tags'][i].indexOf(up['dist-tags'][i]) === -1) { local['dist-tags'][i].push(up['dist-tags'][i]) local['dist-tags'][i] = Storage._semver_sort(local['dist-tags'][i]) } } } // function filters out bad semver versions and sorts the array // exported for unit tests only Storage._semver_sort = function semver_sort(array) { return array .filter(function(x) { return semver.parse(x, true) != null }) .sort(semver.compareLoose) .map(String) } module.exports = Storage