0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-30 22:34:10 -05:00

Refactor streams, removed not needed dependency

This commit is contained in:
Juan Picado @jotadeveloper 2017-05-26 07:21:30 +02:00
parent fa4952408a
commit 391e98de9f
No known key found for this signature in database
GPG key ID: 18AC54485952D158
2 changed files with 63 additions and 64 deletions

View file

@ -8,7 +8,7 @@ const Crypto = require('crypto');
const fs = require('fs'); const fs = require('fs');
const Error = require('http-errors'); const Error = require('http-errors');
const Path = require('path'); const Path = require('path');
const Stream = require('readable-stream'); const Stream = require('stream');
const URL = require('url'); const URL = require('url');
const fs_storage = require('./local-fs'); const fs_storage = require('./local-fs');
@ -380,7 +380,7 @@ class Storage {
addTarball(name, filename) { addTarball(name, filename) {
assert(Utils.validate_name(filename)); assert(Utils.validate_name(filename));
let stream = MyStreams.uploadTarballStream(); let stream = new MyStreams.UploadTarball();
let _transform = stream._transform; let _transform = stream._transform;
let length = 0; let length = 0;
let shasum = Crypto.createHash('sha1'); let shasum = Crypto.createHash('sha1');
@ -470,7 +470,7 @@ class Storage {
*/ */
getTarball(name, filename, callback) { getTarball(name, filename, callback) {
assert(Utils.validate_name(filename)); assert(Utils.validate_name(filename));
const stream = MyStreams.readTarballStream(); const stream = new MyStreams.ReadTarball();
stream.abort = function() { stream.abort = function() {
if (rstream) { if (rstream) {
rstream.abort(); rstream.abort();
@ -534,65 +534,6 @@ class Storage {
}); });
} }
/**
* This function allows to update the package thread-safely
Algorithm:
1. lock package.json for writing
2. read package.json
3. updateFn(pkg, cb), and wait for cb
4. write package.json.tmp
5. move package.json.tmp package.json
6. callback(err?)
* @param {*} name package name
* @param {*} updateFn function(package, cb) - update function
* @param {*} _callback callback that gets invoked after it's all updated
* @return {Function}
*/
_updatePackage(name, updateFn, _callback) {
const storage = this.storage(name);
if (!storage) {
return _callback( Error[404]('no such package available') );
}
storage.lock_and_read_json(info_file, (err, json) => {
let locked = false;
// callback that cleans up lock first
const callback = function(err) {
let _args = arguments;
if (locked) {
storage.unlock_file(info_file, function() {
// ignore any error from the unlock
_callback.apply(err, _args);
});
} else {
_callback.apply(null, _args);
}
};
if (!err) {
locked = true;
}
if (err) {
if (err.code === 'EAGAIN') {
return callback( Error[503]('resource temporarily unavailable') );
} else if (err.code === 'ENOENT') {
return callback( Error[404]('no such package available') );
} else {
return callback(err);
}
}
this._normalizePackage(json);
updateFn(json, (err) => {
if (err) {
return callback(err);
}
this._writePackage(name, json, callback);
});
});
}
/** /**
* Search a local package. * Search a local package.
* @param {*} startKey * @param {*} startKey
@ -789,6 +730,65 @@ class Storage {
return Error[500](); return Error[500]();
} }
/**
* This function allows to update the package thread-safely
Algorithm:
1. lock package.json for writing
2. read package.json
3. updateFn(pkg, cb), and wait for cb
4. write package.json.tmp
5. move package.json.tmp package.json
6. callback(err?)
* @param {*} name package name
* @param {*} updateFn function(package, cb) - update function
* @param {*} _callback callback that gets invoked after it's all updated
* @return {Function}
*/
_updatePackage(name, updateFn, _callback) {
const storage = this.storage(name);
if (!storage) {
return _callback( Error[404]('no such package available') );
}
storage.lock_and_read_json(info_file, (err, json) => {
let locked = false;
// callback that cleans up lock first
const callback = function(err) {
let _args = arguments;
if (locked) {
storage.unlock_file(info_file, function() {
// ignore any error from the unlock
_callback.apply(err, _args);
});
} else {
_callback.apply(null, _args);
}
};
if (!err) {
locked = true;
}
if (err) {
if (err.code === 'EAGAIN') {
return callback( Error[503]('resource temporarily unavailable') );
} else if (err.code === 'ENOENT') {
return callback( Error[404]('no such package available') );
} else {
return callback(err);
}
}
this._normalizePackage(json);
updateFn(json, (err) => {
if (err) {
return callback(err);
}
this._writePackage(name, json, callback);
});
});
}
/** /**
* Update the revision (_rev) string for a package. * Update the revision (_rev) string for a package.
* @param {*} name * @param {*} name

View file

@ -7,9 +7,8 @@ const semver = require('semver');
const Crypto = require('crypto'); const Crypto = require('crypto');
const _ = require('lodash'); const _ = require('lodash');
const Stream = require('stream'); const Stream = require('stream');
const LocalStorage = require('./local-storage');
const Search = require('./search'); const Search = require('./search');
const LocalStorage = require('./local-storage');
const Logger = require('./logger'); const Logger = require('./logger');
const MyStreams = require('./streams'); const MyStreams = require('./streams');
const Proxy = require('./up-storage'); const Proxy = require('./up-storage');