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

151 lines
3.5 KiB
JavaScript
Raw Normal View History

2013-06-07 20:16:28 -05:00
var async = require('async');
var semver = require('semver');
2013-05-31 17:57:28 -05:00
var UError = require('./error').UserError;
2013-06-13 09:21:14 -05:00
var Local = require('./st-local');
2013-06-07 20:16:28 -05:00
var Proxy = require('./st-proxy');
var utils = require('./utils');
2013-05-31 01:26:11 -05:00
2013-06-07 20:16:28 -05:00
function Storage(config) {
if (!(this instanceof Storage)) return new Storage(config);
this.config = config;
this.uplinks = {};
for (var p in config.uplinks) {
this.uplinks[p] = new Proxy(p, config);
2013-05-31 17:57:28 -05:00
}
2013-06-13 09:21:14 -05:00
this.local = new Local(config);
2013-06-07 20:16:28 -05:00
return this;
2013-05-31 01:26:11 -05:00
}
2013-06-07 20:16:28 -05:00
Storage.prototype.add_package = function(name, metadata, callback) {
2013-06-14 03:34:29 -05:00
var self = this;
var uplinks = [];
for (var i in this.uplinks) {
if (this.config.allow_proxy(name, i)) {
uplinks.push(this.uplinks[i]);
}
}
async.map(uplinks, function(up, cb) {
up.get_package(name, function(err, res) {
cb(null, [err, res]);
});
}, function(err, results) {
for (var i=0; i<results.length; i++) {
// checking error
// if uplink fails with a status other than 404, we report failure
if (results[i][0] != null) {
if (results[i][0].status !== 404) {
return callback(new UError({
status: 503,
msg: 'one of the uplinks is down, refuse to publish'
}));
}
}
// checking package
if (results[i][1] != null) {
return callback(new UError({
status: 409,
msg: 'this package is already present'
}));
}
}
self.local.add_package(name, metadata, callback);
});
2013-05-31 17:57:28 -05:00
}
2013-06-07 20:16:28 -05:00
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
2013-06-13 09:21:14 -05:00
this.local.add_version(name, version, metadata, tag, callback);
2013-06-07 20:16:28 -05:00
}
2013-05-31 17:57:28 -05:00
2013-06-07 20:16:28 -05:00
Storage.prototype.add_tarball = function(name, filename, stream, callback) {
2013-06-13 09:21:14 -05:00
this.local.add_tarball(name, filename, stream, callback);
2013-05-31 17:57:28 -05:00
}
2013-06-07 20:16:28 -05:00
Storage.prototype.get_tarball = function(name, filename, callback) {
2013-06-18 13:14:55 -05:00
// if someone requesting tarball, it means that we should already have some
// information about it, so fetching package info is unnecessary
// trying local first
this.local.get_tarball(name, filename, function(err, results) {
if (err && err.status !== 404) return callback(err);
if (!err && results != null) return callback(err, results);
return callback(err, results);
/*TODO:// local reported 404
this.local.get_package(name, function(err, info) {
if (err) return callback(err);
});*/
});
2013-05-31 17:57:28 -05:00
}
2013-06-07 20:16:28 -05:00
Storage.prototype.get_package = function(name, callback) {
2013-06-18 13:14:55 -05:00
var self = this;
2013-06-13 09:21:14 -05:00
var uplinks = [this.local];
2013-06-07 20:16:28 -05:00
for (var i in this.uplinks) {
if (this.config.allow_proxy(name, i)) {
uplinks.push(this.uplinks[i]);
2013-05-31 17:57:28 -05:00
}
2013-06-07 20:16:28 -05:00
}
var result = {
name: name,
versions: {},
'dist-tags': {},
};
2013-06-14 03:34:29 -05:00
var exists = false;
2013-06-07 20:16:28 -05:00
var latest;
2013-05-31 01:26:11 -05:00
2013-06-07 20:16:28 -05:00
async.map(uplinks, function(up, cb) {
up.get_package(name, function(err, up_res) {
if (err) return cb();
try {
utils.validate_metadata(up_res, name);
} catch(err) {
return cb();
}
2013-06-14 02:56:02 -05:00
var this_version = up_res['dist-tags'].latest;
if (!semver.gt(latest, this_version) && this_version) {
latest = this_version;
var is_latest = true;
}
2013-06-07 20:16:28 -05:00
['versions', 'dist-tags'].forEach(function(key) {
for (var i in up_res[key]) {
if (!result[key][i] || is_latest) {
result[key][i] = up_res[key][i];
}
}
});
2013-06-14 03:34:29 -05:00
// if we got to this point, assume that the correct package exists
// on the uplink
exists = true;
2013-06-07 20:16:28 -05:00
cb();
});
}, function(err) {
if (err) return callback(err);
2013-06-14 03:34:29 -05:00
if (!exists) {
2013-05-31 17:57:28 -05:00
return callback(new UError({
status: 404,
msg: 'no such package available'
}));
}
2013-06-07 20:16:28 -05:00
callback(null, result);
2013-06-18 13:14:55 -05:00
self.local.update_versions(name, result, function(){});
2013-05-31 17:57:28 -05:00
});
2013-05-31 01:26:11 -05:00
}
2013-06-07 20:16:28 -05:00
module.exports = Storage;