mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-11-13 03:35:52 +01:00
210 lines
4.7 KiB
JavaScript
210 lines
4.7 KiB
JavaScript
var async = require('async');
|
|
var semver = require('semver');
|
|
var through = require('through');
|
|
var UError = require('./error').UserError;
|
|
var Local = require('./st-local');
|
|
var Proxy = require('./st-proxy');
|
|
var utils = require('./utils');
|
|
|
|
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(config.uplinks[p], config);
|
|
}
|
|
this.local = new Local(config);
|
|
|
|
return this;
|
|
}
|
|
|
|
Storage.prototype.add_package = function(name, metadata, callback) {
|
|
var self = this;
|
|
|
|
var uplinks = [];
|
|
for (var i in this.uplinks) {
|
|
if (this.config.proxy_access(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);
|
|
});
|
|
}
|
|
|
|
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
|
|
this.local.add_version(name, version, metadata, tag, callback);
|
|
}
|
|
|
|
Storage.prototype.add_tarball = function(name, filename) {
|
|
return this.local.add_tarball(name, filename);
|
|
}
|
|
|
|
Storage.prototype.get_tarball = function(name, filename, callback) {
|
|
var stream = through(function(data) {
|
|
this.queue(data);
|
|
}, function() {
|
|
this.queue(null);
|
|
});
|
|
|
|
var self = this;
|
|
|
|
// if someone requesting tarball, it means that we should already have some
|
|
// information about it, so fetching package info is unnecessary
|
|
|
|
// trying local first
|
|
var rstream = self.local.get_tarball(name, filename);
|
|
var is_open = false;
|
|
rstream.on('error', function(err) {
|
|
if (is_open || err.status !== 404) {
|
|
return stream.emit('error', err);
|
|
}
|
|
|
|
// local reported 404
|
|
var err404 = err;
|
|
var uplink = null;
|
|
rstream.destroy();
|
|
|
|
self.local.get_package(name, function(err, info) {
|
|
if (err) return stream.emit('error', err);
|
|
|
|
if (info._distfiles[filename] == null) {
|
|
return stream.emit('error', err404);
|
|
}
|
|
|
|
var file = info._distfiles[filename];
|
|
var uplink = null;
|
|
for (var p in self.uplinks) {
|
|
if (self.uplinks[p].can_fetch_url(file.url)) {
|
|
uplink = self.uplinks[p];
|
|
}
|
|
}
|
|
if (uplink == null) {
|
|
uplink = new Proxy({
|
|
url: file.url,
|
|
_autogenerated: true,
|
|
}, self.config);
|
|
}
|
|
|
|
var savestream = self.local.add_tarball(name, filename);
|
|
savestream.on('error', function(err) {
|
|
stream.emit('error', err);
|
|
});
|
|
|
|
var rstream2 = uplink.get_url(file.url);
|
|
rstream2.on('error', function(err) {
|
|
stream.emit('error', err);
|
|
});
|
|
rstream2.on('data', function(data) {
|
|
stream.write(data);
|
|
savestream.write(data);
|
|
});
|
|
rstream2.on('end', function() {
|
|
stream.end();
|
|
savestream.end();
|
|
});
|
|
});
|
|
});
|
|
rstream.on('open', function() {
|
|
is_open = true;
|
|
rstream.pipe(stream);
|
|
});
|
|
return stream;
|
|
}
|
|
|
|
Storage.prototype.get_package = function(name, callback) {
|
|
var self = this;
|
|
var uplinks = [this.local];
|
|
for (var i in this.uplinks) {
|
|
if (this.config.proxy_access(name, i)) {
|
|
uplinks.push(this.uplinks[i]);
|
|
}
|
|
}
|
|
|
|
var result = {
|
|
name: name,
|
|
versions: {},
|
|
'dist-tags': {},
|
|
};
|
|
var exists = false;
|
|
var latest;
|
|
|
|
async.map(uplinks, function(up, cb) {
|
|
up.get_package(name, function(err, up_res) {
|
|
if (err) return cb();
|
|
|
|
if (up === self.local) {
|
|
// file exists in local repo
|
|
exists = true;
|
|
}
|
|
|
|
try {
|
|
utils.validate_metadata(up_res, name);
|
|
} catch(err) {
|
|
return cb();
|
|
}
|
|
|
|
var this_version = up_res['dist-tags'].latest;
|
|
if (latest == null
|
|
|| (!semver.gt(latest, this_version) && this_version)) {
|
|
latest = this_version;
|
|
var is_latest = true;
|
|
}
|
|
|
|
['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];
|
|
}
|
|
}
|
|
});
|
|
|
|
// if we got to this point, assume that the correct package exists
|
|
// on the uplink
|
|
exists = true;
|
|
cb();
|
|
});
|
|
}, function(err) {
|
|
if (err) return callback(err);
|
|
if (!exists) {
|
|
return callback(new UError({
|
|
status: 404,
|
|
msg: 'no such package available'
|
|
}));
|
|
}
|
|
callback(null, result);
|
|
|
|
self.local.update_versions(name, result, function(){});
|
|
});
|
|
}
|
|
|
|
module.exports = Storage;
|
|
|