mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-17 23:45:29 -05:00
Fix eslint, add jsdoc on storage lib
This commit is contained in:
parent
57692cd8b1
commit
445df5b30e
1 changed files with 88 additions and 43 deletions
113
lib/storage.js
113
lib/storage.js
|
@ -67,24 +67,36 @@ class Storage {
|
|||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Check whether a package it is already a local package
|
||||
* @param {*} cb the callback method
|
||||
*/
|
||||
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( Error[409]('this package is already present') );
|
||||
|
||||
if (err && err.status !== 404) {
|
||||
return cb(err);
|
||||
}
|
||||
if (results) {
|
||||
return cb( Error[409]('this package is already present') );
|
||||
}
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a package exist in any of the uplinks.
|
||||
* @param {*} cb the callback method
|
||||
*/
|
||||
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);
|
||||
|
||||
if (err && err.status !== 404) {
|
||||
return cb(err);
|
||||
}
|
||||
// checking package
|
||||
if (results) return cb( Error[409]('this package is already present') );
|
||||
|
||||
if (results) {
|
||||
return cb( Error[409]('this package is already present') );
|
||||
}
|
||||
for (let i=0; i<err_results.length; i++) {
|
||||
// checking error
|
||||
// if uplink fails with a status other than 404, we report failure
|
||||
|
@ -99,6 +111,10 @@ class Storage {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a package to the local database
|
||||
* @param {*} cb callback method
|
||||
*/
|
||||
function publish_package(cb) {
|
||||
self.local.add_package(name, metadata, callback);
|
||||
}
|
||||
|
@ -114,7 +130,7 @@ class Storage {
|
|||
* @param {*} callback
|
||||
*/
|
||||
add_version(name, version, metadata, tag, callback) {
|
||||
return this.local.add_version(name, version, metadata, tag, callback);
|
||||
this.local.add_version(name, version, metadata, tag, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -125,7 +141,7 @@ class Storage {
|
|||
* @param {*} callback
|
||||
*/
|
||||
merge_tags(name, tag_hash, callback) {
|
||||
return this.local.merge_tags(name, tag_hash, callback);
|
||||
this.local.merge_tags(name, tag_hash, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,7 +152,7 @@ class Storage {
|
|||
* @param {*} callback
|
||||
*/
|
||||
replace_tags(name, tag_hash, callback) {
|
||||
return this.local.replace_tags(name, tag_hash, callback);
|
||||
this.local.replace_tags(name, tag_hash, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -149,7 +165,7 @@ class Storage {
|
|||
* @param {*} callback
|
||||
*/
|
||||
change_package(name, metadata, revision, callback) {
|
||||
return this.local.change_package(name, metadata, revision, callback);
|
||||
this.local.change_package(name, metadata, revision, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,7 +176,7 @@ class Storage {
|
|||
* @param {*} callback
|
||||
*/
|
||||
remove_package(name, callback) {
|
||||
return this.local.remove_package(name, callback);
|
||||
this.local.remove_package(name, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -175,7 +191,7 @@ class Storage {
|
|||
* @param {*} callback
|
||||
*/
|
||||
remove_tarball(name, filename, revision, callback) {
|
||||
return this.local.remove_tarball(name, filename, revision, callback);
|
||||
this.local.remove_tarball(name, filename, revision, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -184,6 +200,7 @@ class Storage {
|
|||
Used storages: local (write)
|
||||
* @param {*} name
|
||||
* @param {*} filename
|
||||
* @return {Stream}
|
||||
*/
|
||||
add_tarball(name, filename) {
|
||||
return this.local.add_tarball(name, filename);
|
||||
|
@ -197,6 +214,7 @@ class Storage {
|
|||
Used storages: local || uplink (just one)
|
||||
* @param {*} name
|
||||
* @param {*} filename
|
||||
* @return {Stream}
|
||||
*/
|
||||
get_tarball(name, filename) {
|
||||
let stream = MyStreams.ReadTarballStream();
|
||||
|
@ -219,21 +237,19 @@ class Storage {
|
|||
let err404 = err;
|
||||
rstream.abort();
|
||||
rstream = null; // gc
|
||||
|
||||
self.local.get_package(name, function(err, info) {
|
||||
if (!err && info._distfiles && info._distfiles[filename] != null) {
|
||||
// information about this file exists locally
|
||||
serve_file(info._distfiles[filename]);
|
||||
} else {
|
||||
// we know nothing about this file, trying to get information elsewhere
|
||||
|
||||
self._sync_package_with_uplinks(name, info, {}, function(err, info) {
|
||||
if (err) return stream.emit('error', err);
|
||||
|
||||
if (err) {
|
||||
return stream.emit('error', err);
|
||||
}
|
||||
if (!info._distfiles || info._distfiles[filename] == null) {
|
||||
return stream.emit('error', err404);
|
||||
}
|
||||
|
||||
serve_file(info._distfiles[filename]);
|
||||
});
|
||||
}
|
||||
|
@ -248,6 +264,10 @@ class Storage {
|
|||
});
|
||||
return stream;
|
||||
|
||||
/**
|
||||
* Fetch and cache local/remote packages.
|
||||
* @param {Object} file define the package shape
|
||||
*/
|
||||
function serve_file(file) {
|
||||
let uplink = null;
|
||||
for (let p in self.uplinks) {
|
||||
|
@ -261,35 +281,46 @@ class Storage {
|
|||
_autogenerated: true,
|
||||
}, self.config);
|
||||
}
|
||||
|
||||
let savestream = self.local.add_tarball(name, filename);
|
||||
let on_open = function() {
|
||||
on_open = function() {}; // prevent it from being called twice
|
||||
// prevent it from being called twice
|
||||
on_open = function() {};
|
||||
let rstream2 = uplink.get_url(file.url);
|
||||
rstream2.on('error', function(err) {
|
||||
if (savestream) savestream.abort();
|
||||
if (savestream) {
|
||||
savestream.abort();
|
||||
}
|
||||
savestream = null;
|
||||
stream.emit('error', err);
|
||||
});
|
||||
rstream2.on('end', function() {
|
||||
if (savestream) savestream.done();
|
||||
if (savestream) {
|
||||
savestream.done();
|
||||
}
|
||||
});
|
||||
|
||||
rstream2.on('content-length', function(v) {
|
||||
stream.emit('content-length', v);
|
||||
if (savestream) savestream.emit('content-length', v);
|
||||
if (savestream) {
|
||||
savestream.emit('content-length', v);
|
||||
}
|
||||
});
|
||||
rstream2.pipe(stream);
|
||||
if (savestream) rstream2.pipe(savestream);
|
||||
if (savestream) {
|
||||
rstream2.pipe(savestream);
|
||||
}
|
||||
};
|
||||
|
||||
savestream.on('open', function() {
|
||||
on_open();
|
||||
});
|
||||
|
||||
savestream.on('error', function(err) {
|
||||
self.logger.warn( {err: err}
|
||||
, 'error saving file: @{err.message}\n@{err.stack}' );
|
||||
if (savestream) savestream.abort();
|
||||
if (savestream) {
|
||||
savestream.abort();
|
||||
}
|
||||
savestream = null;
|
||||
on_open();
|
||||
});
|
||||
|
@ -344,17 +375,21 @@ class Storage {
|
|||
Used storages: local && uplink (proxy_access)
|
||||
* @param {*} startkey
|
||||
* @param {*} options
|
||||
* @return {Stream}
|
||||
*/
|
||||
search(startkey, options) {
|
||||
let self = this;
|
||||
|
||||
// stream to write a tarball
|
||||
let stream = new Stream.PassThrough({objectMode: true});
|
||||
|
||||
async.eachSeries(Object.keys(this.uplinks), function(up_name, cb) {
|
||||
// shortcut: if `local=1` is supplied, don't call uplinks
|
||||
if (options.req.query.local !== undefined) return cb();
|
||||
|
||||
if (options.req.query.local !== undefined) {
|
||||
return cb();
|
||||
}
|
||||
// search by keyword for each uplink
|
||||
let lstream = self.uplinks[up_name].search(startkey, options);
|
||||
// join streams
|
||||
lstream.pipe(stream, {end: false});
|
||||
lstream.on('error', function(err) {
|
||||
self.logger.error({err: err}, 'uplink error: @{err.message}');
|
||||
|
@ -365,10 +400,15 @@ class Storage {
|
|||
});
|
||||
|
||||
stream.abort = function() {
|
||||
if (lstream.abort) lstream.abort();
|
||||
if (lstream.abort) {
|
||||
lstream.abort();
|
||||
}
|
||||
cb(), cb = function() {};
|
||||
};
|
||||
}, function() {
|
||||
},
|
||||
// executed after all series
|
||||
function() {
|
||||
// attach a local search results
|
||||
let lstream = self.local.search(startkey, options);
|
||||
stream.abort = function() {
|
||||
lstream.abort();
|
||||
|
@ -392,7 +432,7 @@ class Storage {
|
|||
let locals = this.config.localList.get();
|
||||
let packages = [];
|
||||
|
||||
var getPackage = function(i) {
|
||||
const getPackage = function(i) {
|
||||
self.local.get_package(locals[i], function(err, info) {
|
||||
if (!err) {
|
||||
let latest = info['dist-tags'].latest;
|
||||
|
@ -463,10 +503,13 @@ class Storage {
|
|||
}
|
||||
|
||||
up.get_package(name, _options, function(err, up_res, etag) {
|
||||
if (err && err.status === 304)
|
||||
if (err && err.status === 304) {
|
||||
pkginfo._uplinks[up.upname].fetched = Date.now();
|
||||
}
|
||||
|
||||
if (err || !up_res) return cb(null, [err || Error('no data')]);
|
||||
if (err || !up_res) {
|
||||
return cb(null, [err || Error('no data')]);
|
||||
}
|
||||
|
||||
try {
|
||||
Utils.validate_metadata(up_res, name);
|
||||
|
@ -483,6 +526,7 @@ class Storage {
|
|||
fetched: Date.now(),
|
||||
};
|
||||
for (let i in up_res.versions) {
|
||||
if (Object.prototype.hasOwnProperty.call(up_res.versions, i)) {
|
||||
// this won't be serialized to json,
|
||||
// kinda like an ES6 Symbol
|
||||
// FIXME: perhaps Symbol('_verdaccio_uplink') here?
|
||||
|
@ -493,6 +537,7 @@ class Storage {
|
|||
writable: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Storage._merge_versions(pkginfo, up_res, self.config);
|
||||
|
|
Loading…
Add table
Reference in a new issue