2017-04-23 20:02:26 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const async = require('async');
|
|
|
|
const Error = require('http-errors');
|
|
|
|
const Stream = require('stream');
|
|
|
|
const Local = require('./local-storage');
|
|
|
|
const Logger = require('./logger');
|
|
|
|
const MyStreams = require('./streams');
|
|
|
|
const Proxy = require('./up-storage');
|
|
|
|
const Utils = require('./utils');
|
2013-05-31 08:26:11 +02:00
|
|
|
|
2017-04-30 09:58:11 +02:00
|
|
|
/**
|
|
|
|
* Implements Storage interface
|
|
|
|
* (same for storage.js, local-storage.js, up-storage.js).
|
|
|
|
*/
|
2017-04-19 20:45:48 +02:00
|
|
|
class Storage {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} config
|
|
|
|
*/
|
|
|
|
constructor(config) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.config = config;
|
2017-04-19 20:45:48 +02:00
|
|
|
// we support a number of uplinks, but only one local storage
|
|
|
|
// Proxy and Local classes should have similar API interfaces
|
2017-04-23 20:02:26 +02:00
|
|
|
this.uplinks = {};
|
2017-04-19 20:45:48 +02:00
|
|
|
for (let p in config.uplinks) {
|
|
|
|
// instance for each up-link definition
|
2017-04-23 20:02:26 +02:00
|
|
|
this.uplinks[p] = new Proxy(config.uplinks[p], config);
|
|
|
|
this.uplinks[p].upname = p;
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
// an instance for local storage
|
2017-04-23 20:02:26 +02:00
|
|
|
this.local = new Local(config);
|
2017-04-19 20:45:48 +02:00
|
|
|
this.logger = Logger.logger.child();
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2013-05-31 08:26:11 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* 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 locally
|
|
|
|
Used storages: local (write) && uplinks
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} metadata
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
add_package(name, metadata, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let self = this;
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
// 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_local(function(err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return callback(err);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
check_package_remote(function(err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return callback(err);
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
publish_package(function(err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return callback(err);
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
function check_package_local(cb) {
|
|
|
|
self.local.get_package(name, {}, function(err, results) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err && err.status !== 404) return cb(err);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
if (results) return cb( Error[409]('this package is already present') );
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
cb();
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
function check_package_remote(cb) {
|
|
|
|
self._sync_package_with_uplinks(name, null, {}, function(err, results, err_results) {
|
|
|
|
// something weird
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err && err.status !== 404) return cb(err);
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
// checking package
|
2017-04-23 20:02:26 +02:00
|
|
|
if (results) return cb( Error[409]('this package is already present') );
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
for (let i=0; i<err_results.length; i++) {
|
2017-04-19 20:45:48 +02:00
|
|
|
// checking error
|
|
|
|
// if uplink fails with a status other than 404, we report failure
|
|
|
|
if (err_results[i][0] != null) {
|
|
|
|
if (err_results[i][0].status !== 404) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return cb( Error[503]('one of the uplinks is down, refuse to publish') );
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
return cb();
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
function publish_package(cb) {
|
2017-04-23 20:02:26 +02:00
|
|
|
self.local.add_package(name, metadata, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2013-06-01 00:57:28 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Add a new version of package {name} to a system
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} version
|
|
|
|
* @param {*} metadata
|
|
|
|
* @param {*} tag
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
add_version(name, version, metadata, tag, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.add_version(name, version, metadata, tag, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-06-01 00:57:28 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Tags a package version with a provided tag
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} tag_hash
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
merge_tags(name, tag_hash, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.merge_tags(name, tag_hash, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2015-05-10 17:23:31 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Tags a package version with a provided tag
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} tag_hash
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
replace_tags(name, tag_hash, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.replace_tags(name, tag_hash, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-12-27 14:06:30 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Change an existing package (i.e. unpublish one version)
|
|
|
|
Function changes a package info from local storage and all uplinks with write access./
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} metadata
|
|
|
|
* @param {*} revision
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
change_package(name, metadata, revision, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.change_package(name, metadata, revision, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-10-23 08:15:17 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Remove a package from a system
|
|
|
|
Function removes a package from local storage
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
remove_package(name, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.remove_package(name, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-10-23 08:15:17 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
Remove a tarball from a system
|
|
|
|
Function removes a tarball from local storage.
|
|
|
|
Tarball in question should not be linked to in any existing
|
|
|
|
versions, i.e. package version should be unpublished first.
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} filename
|
|
|
|
* @param {*} revision
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
remove_tarball(name, filename, revision, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.remove_tarball(name, filename, revision, callback);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-10-06 10:27:50 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Upload a tarball for {name} package
|
|
|
|
Function is syncronous and returns a WritableStream
|
|
|
|
Used storages: local (write)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} filename
|
|
|
|
*/
|
|
|
|
add_tarball(name, filename) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return this.local.add_tarball(name, filename);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-06-01 00:57:28 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
Get a tarball from a storage for {name} package
|
|
|
|
Function is syncronous and returns a ReadableStream
|
|
|
|
Function tries to read tarball locally, if it fails then it reads package
|
|
|
|
information in order to figure out where we can get this tarball from
|
|
|
|
Used storages: local || uplink (just one)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} filename
|
|
|
|
*/
|
|
|
|
get_tarball(name, filename) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let stream = MyStreams.ReadTarballStream();
|
|
|
|
stream.abort = function() {};
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let self = this;
|
2017-04-19 20:45:48 +02: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
|
2017-04-23 20:02:26 +02:00
|
|
|
let rstream = self.local.get_tarball(name, filename);
|
|
|
|
let is_open = false;
|
2017-04-19 20:45:48 +02:00
|
|
|
rstream.on('error', function(err) {
|
|
|
|
if (is_open || err.status !== 404) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return stream.emit('error', err);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
// local reported 404
|
2017-04-23 20:02:26 +02:00
|
|
|
let err404 = err;
|
|
|
|
rstream.abort();
|
|
|
|
rstream = null; // gc
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
self.local.get_package(name, function(err, info) {
|
|
|
|
if (!err && info._distfiles && info._distfiles[filename] != null) {
|
|
|
|
// information about this file exists locally
|
2017-04-23 20:02:26 +02:00
|
|
|
serve_file(info._distfiles[filename]);
|
2017-04-19 20:45:48 +02:00
|
|
|
} else {
|
|
|
|
// we know nothing about this file, trying to get information elsewhere
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
self._sync_package_with_uplinks(name, info, {}, function(err, info) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return stream.emit('error', err);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
if (!info._distfiles || info._distfiles[filename] == null) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return stream.emit('error', err404);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
serve_file(info._distfiles[filename]);
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
rstream.on('content-length', function(v) {
|
2017-04-23 20:02:26 +02:00
|
|
|
stream.emit('content-length', v);
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
rstream.on('open', function() {
|
2017-04-23 20:02:26 +02:00
|
|
|
is_open = true;
|
|
|
|
rstream.pipe(stream);
|
|
|
|
});
|
|
|
|
return stream;
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
function serve_file(file) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let uplink = null;
|
|
|
|
for (let p in self.uplinks) {
|
2017-04-19 20:45:48 +02:00
|
|
|
if (self.uplinks[p].can_fetch_url(file.url)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
uplink = self.uplinks[p];
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uplink == null) {
|
|
|
|
uplink = Proxy({
|
|
|
|
url: file.url,
|
|
|
|
_autogenerated: true,
|
2017-04-23 20:02:26 +02:00
|
|
|
}, self.config);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let savestream = self.local.add_tarball(name, filename);
|
2017-04-19 20:45:48 +02:00
|
|
|
var on_open = function() {
|
2017-04-23 20:02:26 +02:00
|
|
|
on_open = function() {}; // prevent it from being called twice
|
|
|
|
let rstream2 = uplink.get_url(file.url);
|
2017-04-19 20:45:48 +02:00
|
|
|
rstream2.on('error', function(err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (savestream) savestream.abort();
|
|
|
|
savestream = null;
|
|
|
|
stream.emit('error', err);
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
rstream2.on('end', function() {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (savestream) savestream.done();
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
rstream2.on('content-length', function(v) {
|
2017-04-23 20:02:26 +02:00
|
|
|
stream.emit('content-length', v);
|
|
|
|
if (savestream) savestream.emit('content-length', v);
|
|
|
|
});
|
|
|
|
rstream2.pipe(stream);
|
|
|
|
if (savestream) rstream2.pipe(savestream);
|
|
|
|
};
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
savestream.on('open', function() {
|
2017-04-23 20:02:26 +02:00
|
|
|
on_open();
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
savestream.on('error', function(err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
self.logger.warn( {err: err}
|
|
|
|
, 'error saving file: @{err.message}\n@{err.stack}' );
|
|
|
|
if (savestream) savestream.abort();
|
|
|
|
savestream = null;
|
|
|
|
on_open();
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
Retrieve a package metadata for {name} package
|
|
|
|
Function invokes local.get_package and uplink.get_package for every
|
|
|
|
uplink with proxy_access rights against {name} and combines results
|
|
|
|
into one json object
|
|
|
|
Used storages: local && uplink (proxy_access)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} options
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
get_package(name, options, callback) {
|
|
|
|
if (typeof(options) === 'function') {
|
|
|
|
callback = options, options = {};
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
this.local.get_package(name, options, (err, data) => {
|
|
|
|
if (err && (!err.status || err.status >= 500)) {
|
|
|
|
// report internal errors right away
|
2017-04-23 20:02:26 +02:00
|
|
|
return callback(err);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
this._sync_package_with_uplinks(name, data, options, function(err, result, uplink_errors) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return callback(err);
|
|
|
|
const whitelist = ['_rev', 'name', 'versions', 'dist-tags', 'readme'];
|
|
|
|
for (let i in result) {
|
|
|
|
if (whitelist.indexOf(i) === -1) delete result[i];
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
Utils.normalize_dist_tags(result);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
// npm can throw if this field doesn't exist
|
2017-04-23 20:02:26 +02:00
|
|
|
result._attachments = {};
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
callback(null, result, uplink_errors);
|
|
|
|
});
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-04-12 19:20:26 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
Retrieve remote and local packages more recent than {startkey}
|
|
|
|
Function streams all packages from all uplinks first, and then
|
|
|
|
local packages.
|
|
|
|
Note that local packages could override registry ones just because
|
|
|
|
they appear in JSON last. That's a trade-off we make to avoid
|
|
|
|
memory issues.
|
|
|
|
Used storages: local && uplink (proxy_access)
|
|
|
|
* @param {*} startkey
|
|
|
|
* @param {*} options
|
|
|
|
*/
|
|
|
|
search(startkey, options) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let self = this;
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let stream = new Stream.PassThrough({objectMode: true});
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
async.eachSeries(Object.keys(this.uplinks), function(up_name, cb) {
|
|
|
|
// shortcut: if `local=1` is supplied, don't call uplinks
|
2017-04-23 20:02:26 +02:00
|
|
|
if (options.req.query.local !== undefined) return cb();
|
|
|
|
|
|
|
|
let lstream = self.uplinks[up_name].search(startkey, options);
|
|
|
|
lstream.pipe(stream, {end: false});
|
|
|
|
lstream.on('error', function(err) {
|
|
|
|
self.logger.error({err: err}, 'uplink error: @{err.message}');
|
|
|
|
cb(), cb = function() {};
|
|
|
|
});
|
|
|
|
lstream.on('end', function() {
|
|
|
|
cb(), cb = function() {};
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.abort = function() {
|
|
|
|
if (lstream.abort) lstream.abort();
|
|
|
|
cb(), cb = function() {};
|
|
|
|
};
|
|
|
|
}, function() {
|
|
|
|
let lstream = self.local.search(startkey, options);
|
|
|
|
stream.abort = function() {
|
|
|
|
lstream.abort();
|
|
|
|
};
|
|
|
|
lstream.pipe(stream, {end: true});
|
|
|
|
lstream.on('error', function(err) {
|
|
|
|
self.logger.error({err: err}, 'search error: @{err.message}');
|
|
|
|
stream.end();
|
|
|
|
});
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
return stream;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
get_local(callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let self = this;
|
|
|
|
let locals = this.config.localList.get();
|
|
|
|
let packages = [];
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
var getPackage = function(i) {
|
|
|
|
self.local.get_package(locals[i], function(err, info) {
|
|
|
|
if (!err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let latest = info['dist-tags'].latest;
|
2017-04-19 20:45:48 +02:00
|
|
|
if (latest && info.versions[latest]) {
|
2017-04-23 20:02:26 +02:00
|
|
|
packages.push(info.versions[latest]);
|
2017-04-19 20:45:48 +02:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
self.logger.warn( {package: locals[i]}
|
|
|
|
, 'package @{package} does not have a "latest" tag?' );
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
if (i >= locals.length - 1) {
|
2017-04-23 20:02:26 +02:00
|
|
|
callback(null, packages);
|
2017-04-19 20:45:48 +02:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
getPackage(i + 1);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
|
|
|
};
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
if (locals.length) {
|
2017-04-23 20:02:26 +02:00
|
|
|
getPackage(0);
|
2017-04-19 20:45:48 +02:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
callback(null, []);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
* @param {*} name
|
|
|
|
* @param {*} pkginfo
|
|
|
|
* @param {*} options
|
|
|
|
* @param {*} callback
|
|
|
|
*/
|
|
|
|
_sync_package_with_uplinks(name, pkginfo, options, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let self = this;
|
2017-04-19 20:45:48 +02:00
|
|
|
let exists = false;
|
|
|
|
if (!pkginfo) {
|
2017-04-23 20:02:26 +02:00
|
|
|
exists = false;
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
pkginfo = {
|
2017-04-23 20:02:26 +02:00
|
|
|
'name': name,
|
|
|
|
'versions': {},
|
|
|
|
'dist-tags': {},
|
|
|
|
'_uplinks': {},
|
|
|
|
};
|
2017-04-19 20:45:48 +02:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
exists = true;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let uplinks = [];
|
2017-04-19 20:45:48 +02:00
|
|
|
for (let i in self.uplinks) {
|
|
|
|
if (self.config.can_proxy_to(name, i)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
uplinks.push(self.uplinks[i]);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
async.map(uplinks, function(up, cb) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let _options = Object.assign({}, options);
|
2017-04-19 20:45:48 +02:00
|
|
|
if (Utils.is_object(pkginfo._uplinks[up.upname])) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let fetched = pkginfo._uplinks[up.upname].fetched;
|
2017-04-19 20:45:48 +02:00
|
|
|
if (fetched && fetched > (Date.now() - up.maxage)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return cb();
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
_options.etag = pkginfo._uplinks[up.upname].etag;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
up.get_package(name, _options, function(err, up_res, etag) {
|
|
|
|
if (err && err.status === 304)
|
2017-04-23 20:02:26 +02:00
|
|
|
pkginfo._uplinks[up.upname].fetched = Date.now();
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err || !up_res) return cb(null, [err || Error('no data')]);
|
2014-12-22 22:27:03 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
try {
|
2017-04-23 20:02:26 +02:00
|
|
|
Utils.validate_metadata(up_res, name);
|
2017-04-19 20:45:48 +02:00
|
|
|
} catch(err) {
|
|
|
|
self.logger.error({
|
|
|
|
sub: 'out',
|
|
|
|
err: err,
|
2017-04-23 20:02:26 +02:00
|
|
|
}, 'package.json validating error @{!err.message}\n@{err.stack}');
|
|
|
|
return cb(null, [err]);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
pkginfo._uplinks[up.upname] = {
|
|
|
|
etag: etag,
|
2017-04-23 20:02:26 +02:00
|
|
|
fetched: Date.now(),
|
|
|
|
};
|
2017-04-19 20:45:48 +02:00
|
|
|
for (let i in up_res.versions) {
|
|
|
|
// this won't be serialized to json,
|
|
|
|
// kinda like an ES6 Symbol
|
2017-04-23 20:02:26 +02:00
|
|
|
// FIXME: perhaps Symbol('_verdaccio_uplink') here?
|
2017-04-19 20:45:48 +02:00
|
|
|
Object.defineProperty(up_res.versions[i], '_verdaccio_uplink', {
|
2017-04-23 20:02:26 +02:00
|
|
|
value: up.upname,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: false,
|
|
|
|
writable: true,
|
2017-04-19 20:45:48 +02:00
|
|
|
});
|
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
try {
|
|
|
|
Storage._merge_versions(pkginfo, up_res, self.config);
|
|
|
|
} catch(err) {
|
|
|
|
self.logger.error({
|
|
|
|
sub: 'out',
|
|
|
|
err: err,
|
2017-04-23 20:02:26 +02:00
|
|
|
}, 'package.json parsing error @{!err.message}\n@{err.stack}');
|
|
|
|
return cb(null, [err]);
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
// if we got to this point, assume that the correct package exists
|
|
|
|
// on the uplink
|
2017-04-23 20:02:26 +02:00
|
|
|
exists = true;
|
|
|
|
cb();
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
}, function(err, uplink_errors) {
|
2017-04-23 20:02:26 +02:00
|
|
|
assert(!err && Array.isArray(uplink_errors));
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
return callback( Error[404]('no such package available')
|
|
|
|
, null
|
2017-04-23 20:02:26 +02:00
|
|
|
, uplink_errors );
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.local.update_versions(name, pkginfo, function(err, pkginfo) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return callback(err);
|
|
|
|
return callback(null, pkginfo, uplink_errors);
|
|
|
|
});
|
|
|
|
});
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2013-05-31 08:26:11 +02:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
/**
|
|
|
|
* Function gets a local info and an info from uplinks and tries to merge it
|
|
|
|
exported for unit tests only.
|
|
|
|
* @param {*} local
|
|
|
|
* @param {*} up
|
|
|
|
* @param {*} config
|
|
|
|
*/
|
|
|
|
static _merge_versions(local, up, config) {
|
|
|
|
// 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) {
|
2017-04-23 20:02:26 +02:00
|
|
|
local.versions[i] = up.versions[i];
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
// refresh dist-tags
|
|
|
|
for (var i in up['dist-tags']) {
|
|
|
|
if (local['dist-tags'][i] !== up['dist-tags'][i]) {
|
2017-04-23 20:02:26 +02:00
|
|
|
local['dist-tags'][i] = up['dist-tags'][i];
|
2017-04-19 20:45:48 +02:00
|
|
|
if (i === 'latest') {
|
|
|
|
// if remote has more fresh package, we should borrow its readme
|
2017-04-23 20:02:26 +02:00
|
|
|
local.readme = up.readme;
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
2016-04-18 21:53:00 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2013-12-07 23:55:50 +01:00
|
|
|
}
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
module.exports = Storage;
|