0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-24 23:55:46 -05:00

refactor: local storage methods cleanup for getTarball

This commit is contained in:
Juan Picado @jotadeveloper 2017-07-30 12:34:23 +02:00
parent 4e2853bb79
commit 5b52447408
No known key found for this signature in database
GPG key ID: 18AC54485952D158

View file

@ -510,42 +510,69 @@ class LocalStorage {
* Get a tarball. * Get a tarball.
* @param {*} name * @param {*} name
* @param {*} filename * @param {*} filename
* @param {*} callback * @return {ReadTarball}
* @return {Function}
*/ */
getTarball(name, filename, callback) { getTarball(name, filename) {
assert(Utils.validate_name(filename)); assert(Utils.validate_name(filename));
const storage = this.storage(name);
if (_.isNil(storage)) {
return this._createFailureStreamResponse();
}
return this._streamSuccessReadTarBall(storage, filename);
}
/**
* Return a stream that emits a read failure.
* @private
* @return {ReadTarball}
*/
_createFailureStreamResponse() {
const stream = new customStream.ReadTarball(); const stream = new customStream.ReadTarball();
process.nextTick(function() {
stream.emit('error', createError[404]('no such file available'));
});
return stream;
}
/**
* Return a stream that emits the tarball data
* @param {Object} storage
* @param {String} filename
* @private
* @return {ReadTarball}
*/
_streamSuccessReadTarBall(storage, filename) {
const stream = new customStream.ReadTarball();
const readTarballStream = storage.createReadStream(filename);
stream.abort = function() { stream.abort = function() {
if (rstream) { if (_.isNil(readTarballStream) === false) {
rstream.abort(); readTarballStream.abort();
} }
}; };
let storage = this.storage(name); readTarballStream.on('error', function(err) {
if (!storage) {
process.nextTick(function() {
stream.emit('error', createError[404]('no such file available'));
});
return stream;
}
/* eslint no-var: "off" */
var rstream = storage.createReadStream(filename);
rstream.on('error', function(err) {
if (err && err.code === noSuchFile) { if (err && err.code === noSuchFile) {
stream.emit('error', createError(404, 'no such file available')); stream.emit('error', createError(404, 'no such file available'));
} else { } else {
stream.emit('error', err); stream.emit('error', err);
} }
}); });
rstream.on('content-length', function(v) {
readTarballStream.on('content-length', function(v) {
stream.emit('content-length', v); stream.emit('content-length', v);
}); });
rstream.on('open', function() {
readTarballStream.on('open', function() {
// re-emitting open because it's handled in storage.js // re-emitting open because it's handled in storage.js
stream.emit('open'); stream.emit('open');
rstream.pipe(stream); readTarballStream.pipe(stream);
}); });
return stream; return stream;
} }