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:
parent
fa4952408a
commit
391e98de9f
2 changed files with 63 additions and 64 deletions
|
@ -8,7 +8,7 @@ const Crypto = require('crypto');
|
|||
const fs = require('fs');
|
||||
const Error = require('http-errors');
|
||||
const Path = require('path');
|
||||
const Stream = require('readable-stream');
|
||||
const Stream = require('stream');
|
||||
const URL = require('url');
|
||||
|
||||
const fs_storage = require('./local-fs');
|
||||
|
@ -380,7 +380,7 @@ class Storage {
|
|||
addTarball(name, filename) {
|
||||
assert(Utils.validate_name(filename));
|
||||
|
||||
let stream = MyStreams.uploadTarballStream();
|
||||
let stream = new MyStreams.UploadTarball();
|
||||
let _transform = stream._transform;
|
||||
let length = 0;
|
||||
let shasum = Crypto.createHash('sha1');
|
||||
|
@ -470,7 +470,7 @@ class Storage {
|
|||
*/
|
||||
getTarball(name, filename, callback) {
|
||||
assert(Utils.validate_name(filename));
|
||||
const stream = MyStreams.readTarballStream();
|
||||
const stream = new MyStreams.ReadTarball();
|
||||
stream.abort = function() {
|
||||
if (rstream) {
|
||||
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.
|
||||
* @param {*} startKey
|
||||
|
@ -789,6 +730,65 @@ class Storage {
|
|||
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.
|
||||
* @param {*} name
|
||||
|
|
|
@ -7,9 +7,8 @@ const semver = require('semver');
|
|||
const Crypto = require('crypto');
|
||||
const _ = require('lodash');
|
||||
const Stream = require('stream');
|
||||
const LocalStorage = require('./local-storage');
|
||||
|
||||
const Search = require('./search');
|
||||
const LocalStorage = require('./local-storage');
|
||||
const Logger = require('./logger');
|
||||
const MyStreams = require('./streams');
|
||||
const Proxy = require('./up-storage');
|
||||
|
|
Loading…
Reference in a new issue