0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

refactor: utils as an import instead constructor argument

This commit is contained in:
Juan Picado @jotadeveloper 2017-12-16 22:16:45 +01:00 committed by juanpicado
parent 4b8193041b
commit 724e372246
2 changed files with 44 additions and 45 deletions

View file

@ -11,6 +11,7 @@ import UrlNode from 'url';
import _ from 'lodash'; import _ from 'lodash';
// $FlowFixMe // $FlowFixMe
import async from 'async'; import async from 'async';
import * as Utils from './utils';
import LocalDatabase from '@verdaccio/local-storage'; import LocalDatabase from '@verdaccio/local-storage';
import {UploadTarball, ReadTarball} from '@verdaccio/streams'; import {UploadTarball, ReadTarball} from '@verdaccio/streams';
@ -23,7 +24,6 @@ import type {
DistFile, DistFile,
Callback, Callback,
Logger, Logger,
Utils,
} from '@verdaccio/types'; } from '@verdaccio/types';
import type { import type {
ILocalFS, ILocalFS,
@ -60,26 +60,25 @@ class Storage implements IStorage {
localData: ILocalData; localData: ILocalData;
logger: Logger; logger: Logger;
constructor(config: Config, logger: Logger, utils: Utils) { constructor(config: Config, logger: Logger) {
this.localData = new LocalDatabase(config, logger); this.localData = new LocalDatabase(config, logger);
this.logger = logger.child({sub: 'fs'}); this.logger = logger.child({sub: 'fs'});
this.config = config; this.config = config;
this.utils = utils;
} }
addPackage(name: string, info: Package, callback: Callback) { addPackage(name: string, info: Package, callback: Callback) {
const storage: ILocalFS = this._getLocalStorage(name); const storage: ILocalFS = this._getLocalStorage(name);
if (_.isNil(storage)) { if (_.isNil(storage)) {
return callback( this.utils.ErrorCode.get404('this package cannot be added')); return callback( Utils.ErrorCode.get404('this package cannot be added'));
} }
storage.createJSON(pkgFileName, generatePackageTemplate(name), (err) => { storage.createJSON(pkgFileName, generatePackageTemplate(name), (err) => {
if (err && err.code === fileExist) { if (err && err.code === fileExist) {
return callback( this.utils.ErrorCode.get409()); return callback( Utils.ErrorCode.get409());
} }
const latest = this.utils.getLatestVersion(info); const latest = Utils.getLatestVersion(info);
if (_.isNil(latest) === false && info.versions[latest]) { if (_.isNil(latest) === false && info.versions[latest]) {
return callback(null, info.versions[latest]); return callback(null, info.versions[latest]);
@ -97,13 +96,13 @@ class Storage implements IStorage {
removePackage(name: string, callback: Callback) { removePackage(name: string, callback: Callback) {
let storage: ILocalFS = this._getLocalStorage(name); let storage: ILocalFS = this._getLocalStorage(name);
if (_.isNil(storage)) { if (_.isNil(storage)) {
return callback( this.utils.ErrorCode.get404()); return callback( Utils.ErrorCode.get404());
} }
storage.readJSON(pkgFileName, (err, data) => { storage.readJSON(pkgFileName, (err, data) => {
if (_.isNil(err) === false) { if (_.isNil(err) === false) {
if (err.code === noSuchFile) { if (err.code === noSuchFile) {
return callback( this.utils.ErrorCode.get404()); return callback( Utils.ErrorCode.get404());
} else { } else {
return callback(err); return callback(err);
} }
@ -114,7 +113,7 @@ class Storage implements IStorage {
if (removeFailed) { if (removeFailed) {
// This will happen when database is locked // This will happen when database is locked
return callback(this.utils.ErrorCode.get422(removeFailed.message)); return callback(Utils.ErrorCode.get422(removeFailed.message));
} }
storage.deleteJSON(pkgFileName, (err) => { storage.deleteJSON(pkgFileName, (err) => {
@ -201,7 +200,7 @@ class Storage implements IStorage {
} }
for (let up in packageInfo._uplinks) { for (let up in packageInfo._uplinks) {
if (Object.prototype.hasOwnProperty.call(packageInfo._uplinks, up)) { if (Object.prototype.hasOwnProperty.call(packageInfo._uplinks, up)) {
const need_change = !this.utils.is_object(packageLocalJson._uplinks[up]) const need_change = !Utils.is_object(packageLocalJson._uplinks[up])
|| packageInfo._uplinks[up].etag !== packageLocalJson._uplinks[up].etag || packageInfo._uplinks[up].etag !== packageLocalJson._uplinks[up].etag
|| packageInfo._uplinks[up].fetched !== packageLocalJson._uplinks[up].fetched; || packageInfo._uplinks[up].fetched !== packageLocalJson._uplinks[up].fetched;
@ -273,19 +272,19 @@ class Storage implements IStorage {
delete metadata.readme; delete metadata.readme;
if (data.versions[version] != null) { if (data.versions[version] != null) {
return cb( this.utils.ErrorCode.get409() ); return cb( Utils.ErrorCode.get409() );
} }
// if uploaded tarball has a different shasum, it's very likely that we have some kind of error // if uploaded tarball has a different shasum, it's very likely that we have some kind of error
if (this.utils.is_object(metadata.dist) && _.isString(metadata.dist.tarball)) { if (Utils.is_object(metadata.dist) && _.isString(metadata.dist.tarball)) {
let tarball = metadata.dist.tarball.replace(/.*\//, ''); let tarball = metadata.dist.tarball.replace(/.*\//, '');
if (this.utils.is_object(data._attachments[tarball])) { if (Utils.is_object(data._attachments[tarball])) {
if (_.isNil(data._attachments[tarball].shasum) === false && _.isNil(metadata.dist.shasum) === false) { if (_.isNil(data._attachments[tarball].shasum) === false && _.isNil(metadata.dist.shasum) === false) {
if (data._attachments[tarball].shasum != metadata.dist.shasum) { if (data._attachments[tarball].shasum != metadata.dist.shasum) {
const errorMessage = `shasum error, ${data._attachments[tarball].shasum} != ${metadata.dist.shasum}`; const errorMessage = `shasum error, ${data._attachments[tarball].shasum} != ${metadata.dist.shasum}`;
return cb( this.utils.ErrorCode.get400(errorMessage) ); return cb( Utils.ErrorCode.get400(errorMessage) );
} }
} }
@ -302,11 +301,11 @@ class Storage implements IStorage {
} }
data.versions[version] = metadata; data.versions[version] = metadata;
this.utils.tag_version(data, version, tag); Utils.tag_version(data, version, tag);
let addFailed = this.localData.add(name); let addFailed = this.localData.add(name);
if (addFailed) { if (addFailed) {
return cb(this.utils.ErrorCode.get422(addFailed.message)); return cb(Utils.ErrorCode.get422(addFailed.message));
} }
cb(); cb();
@ -331,7 +330,7 @@ class Storage implements IStorage {
return cb( this._getVersionNotFound() ); return cb( this._getVersionNotFound() );
} }
const key: string = tags[t]; const key: string = tags[t];
this.utils.tag_version(data, key, t); Utils.tag_version(data, key, t);
} }
cb(); cb();
}, callback); }, callback);
@ -343,7 +342,7 @@ class Storage implements IStorage {
* @private * @private
*/ */
_getVersionNotFound() { _getVersionNotFound() {
return this.utils.ErrorCode.get404('this version doesn\'t exist'); return Utils.ErrorCode.get404('this version doesn\'t exist');
} }
/** /**
* Return file no available * Return file no available
@ -351,7 +350,7 @@ class Storage implements IStorage {
* @private * @private
*/ */
_getFileNotAvailable() { _getFileNotAvailable() {
return this.utils.ErrorCode.get404('no such file available'); return Utils.ErrorCode.get404('no such file available');
} }
/** /**
@ -366,8 +365,8 @@ class Storage implements IStorage {
changePackage(name: string, changePackage(name: string,
metadata: Package, metadata: Package,
revision?: string, callback: Callback) { revision?: string, callback: Callback) {
if (!this.utils.is_object(metadata.versions) || !this.utils.is_object(metadata['dist-tags'])) { if (!Utils.is_object(metadata.versions) || !Utils.is_object(metadata['dist-tags'])) {
return callback( this.utils.ErrorCode.get422()); return callback( Utils.ErrorCode.get422());
} }
this._updatePackage(name, (data, cb) => { this._updatePackage(name, (data, cb) => {
@ -402,7 +401,7 @@ class Storage implements IStorage {
*/ */
removeTarball(name: string, filename: string, removeTarball(name: string, filename: string,
revision: string, callback: Callback) { revision: string, callback: Callback) {
assert(this.utils.validate_name(filename)); assert(Utils.validate_name(filename));
this._updatePackage(name, (data, cb) => { this._updatePackage(name, (data, cb) => {
if (data._attachments[filename]) { if (data._attachments[filename]) {
@ -430,7 +429,7 @@ class Storage implements IStorage {
* @return {Stream} * @return {Stream}
*/ */
addTarball(name: string, filename: string) { addTarball(name: string, filename: string) {
assert(this.utils.validate_name(filename)); assert(Utils.validate_name(filename));
let length = 0; let length = 0;
const shaOneHash = Crypto.createHash('sha1'); const shaOneHash = Crypto.createHash('sha1');
@ -449,7 +448,7 @@ class Storage implements IStorage {
if (name === pkgFileName || name === '__proto__') { if (name === pkgFileName || name === '__proto__') {
process.nextTick(() => { process.nextTick(() => {
uploadStream.emit('error', this.utils.ErrorCode.get403()); uploadStream.emit('error', Utils.ErrorCode.get403());
}); });
return uploadStream; return uploadStream;
} }
@ -465,7 +464,7 @@ class Storage implements IStorage {
writeStream.on('error', (err) => { writeStream.on('error', (err) => {
if (err.code === fileExist) { if (err.code === fileExist) {
uploadStream.emit('error', this.utils.ErrorCode.get409()); uploadStream.emit('error', Utils.ErrorCode.get409());
} else if (err.code === noSuchFile) { } else if (err.code === noSuchFile) {
// check if package exists to throw an appropriate message // check if package exists to throw an appropriate message
this.getPackageMetadata(name, function(_err, res) { this.getPackageMetadata(name, function(_err, res) {
@ -506,7 +505,7 @@ class Storage implements IStorage {
uploadStream.done = function() { uploadStream.done = function() {
if (!length) { if (!length) {
uploadStream.emit('error', this.utils.ErrorCode.get422('refusing to accept zero-length file')); uploadStream.emit('error', Utils.ErrorCode.get422('refusing to accept zero-length file'));
writeStream.abort(); writeStream.abort();
} else { } else {
writeStream.done(); writeStream.done();
@ -525,7 +524,7 @@ class Storage implements IStorage {
* @return {ReadTarball} * @return {ReadTarball}
*/ */
getTarball(name: string, filename: string) { getTarball(name: string, filename: string) {
assert(this.utils.validate_name(filename)); assert(Utils.validate_name(filename));
const storage: ILocalFS = this._getLocalStorage(name); const storage: ILocalFS = this._getLocalStorage(name);
@ -560,7 +559,7 @@ class Storage implements IStorage {
_streamSuccessReadTarBall(storage: ILocalFS, filename: string) { _streamSuccessReadTarBall(storage: ILocalFS, filename: string) {
const stream = new ReadTarball(); const stream = new ReadTarball();
const readTarballStream = storage.createReadStream(filename); const readTarballStream = storage.createReadStream(filename);
const e404 = this.utils.ErrorCode.get404; const e404 = Utils.ErrorCode.get404;
stream.abort = function() { stream.abort = function() {
if (_.isNil(readTarballStream) === false) { if (_.isNil(readTarballStream) === false) {
@ -599,7 +598,7 @@ class Storage implements IStorage {
const storage = this._getLocalStorage(name); const storage = this._getLocalStorage(name);
if (_.isNil(storage)) { if (_.isNil(storage)) {
return callback( this.utils.ErrorCode.get404() ); return callback( Utils.ErrorCode.get404() );
} }
this._readJSON(storage, callback); this._readJSON(storage, callback);
@ -626,7 +625,7 @@ class Storage implements IStorage {
return cb(err); return cb(err);
} }
const listVersions: Array<string> = Object.keys(data.versions); const listVersions: Array<string> = Object.keys(data.versions);
const versions: Array<string> = this.utils.semver_sort(listVersions); const versions: Array<string> = Utils.semver_sort(listVersions);
const latest: string = data['dist-tags'] && data['dist-tags'].latest ? data['dist-tags'].latest : versions.pop(); const latest: string = data['dist-tags'] && data['dist-tags'].latest ? data['dist-tags'].latest : versions.pop();
if (data.versions[latest]) { if (data.versions[latest]) {
@ -693,7 +692,7 @@ class Storage implements IStorage {
storage.readJSON(pkgFileName, (err, result) => { storage.readJSON(pkgFileName, (err, result) => {
if (err) { if (err) {
if (err.code === noSuchFile) { if (err.code === noSuchFile) {
return callback( this.utils.ErrorCode.get404() ); return callback( Utils.ErrorCode.get404() );
} else { } else {
return callback(this._internalError(err, pkgFileName, 'error reading')); return callback(this._internalError(err, pkgFileName, 'error reading'));
} }
@ -710,20 +709,20 @@ class Storage implements IStorage {
* @private * @private
*/ */
__getLocalStoragePath(path: string): string { __getLocalStoragePath(path: string): string {
if (_.isNil(path)) { if (_.isNil(path) === false) {
path = this.config.storage;
}
return path; return path;
} }
return this.config.storage;
}
/** /**
* Walks through each package and calls `on_package` on them. * Walks through each package and calls `on_package` on them.
* @param {*} onPackage * @param {*} onPackage
* @param {*} on_end * @param {*} on_end
*/ */
_eachPackage(onPackage: Callback, on_end: Callback) { _eachPackage(onPackage: Callback, on_end: Callback) {
let storages = {}; const storages = {};
let utils = this.utils;
storages[this.config.storage] = true; storages[this.config.storage] = true;
if (this.config.packages) { if (this.config.packages) {
@ -750,7 +749,7 @@ class Storage implements IStorage {
} }
async.eachSeries(files, (file2, cb) => { async.eachSeries(files, (file2, cb) => {
if (utils.validate_name(file2)) { if (Utils.validate_name(file2)) {
onPackage({ onPackage({
name: `${file}/${file2}`, name: `${file}/${file2}`,
path: Path.resolve(base, storage, file, file2), path: Path.resolve(base, storage, file, file2),
@ -760,7 +759,7 @@ class Storage implements IStorage {
} }
}, cb); }, cb);
}); });
} else if (utils.validate_name(file)) { } else if (Utils.validate_name(file)) {
onPackage({ onPackage({
name: file, name: file,
path: Path.resolve(base, storage, file), path: Path.resolve(base, storage, file),
@ -781,7 +780,7 @@ class Storage implements IStorage {
const pkgProperties = ['versions', 'dist-tags', '_distfiles', '_attachments', '_uplinks', 'time']; const pkgProperties = ['versions', 'dist-tags', '_distfiles', '_attachments', '_uplinks', 'time'];
pkgProperties.forEach((key) => { pkgProperties.forEach((key) => {
if (_.isNil(this.utils.is_object(pkg[key]))) { if (_.isNil(Utils.is_object(pkg[key]))) {
pkg[key] = {}; pkg[key] = {};
} }
}); });
@ -790,7 +789,7 @@ class Storage implements IStorage {
pkg._rev = DEFAULT_REVISION; pkg._rev = DEFAULT_REVISION;
} }
// normalize dist-tags // normalize dist-tags
this.utils.normalize_dist_tags(pkg); Utils.normalize_dist_tags(pkg);
} }
@ -841,7 +840,7 @@ class Storage implements IStorage {
this.logger.error( {err: err, file: file}, this.logger.error( {err: err, file: file},
message + ' @{file}: @{!err.message}' ); message + ' @{file}: @{!err.message}' );
return this.utils.ErrorCode.get500(); return Utils.ErrorCode.get500();
} }
/** /**
@ -862,7 +861,7 @@ class Storage implements IStorage {
const storage: ILocalFS = this._getLocalStorage(name); const storage: ILocalFS = this._getLocalStorage(name);
if (!storage) { if (!storage) {
return _callback( this.utils.ErrorCode.get404() ); return _callback( Utils.ErrorCode.get404() );
} }
storage.lockAndReadJSON(pkgFileName, (err, json) => { storage.lockAndReadJSON(pkgFileName, (err, json) => {
@ -887,9 +886,9 @@ class Storage implements IStorage {
if (err) { if (err) {
if (err.code === resourceNotAvailable) { if (err.code === resourceNotAvailable) {
return callback( this.utils.ErrorCode.get503() ); return callback( Utils.ErrorCode.get503() );
} else if (err.code === noSuchFile) { } else if (err.code === noSuchFile) {
return callback( this.utils.ErrorCode.get404() ); return callback( Utils.ErrorCode.get404() );
} else { } else {
return callback(err); return callback(err);
} }

View file

@ -36,7 +36,7 @@ class Storage {
constructor(config) { constructor(config) {
this.config = config; this.config = config;
this._setupUpLinks(this.config); this._setupUpLinks(this.config);
this.localStorage = new LocalStorage(config, Logger.logger, Utils); this.localStorage = new LocalStorage(config, Logger.logger);
this.logger = Logger.logger.child(); this.logger = Logger.logger.child();
} }