Merge pull request #471 from verdaccio/feature-storage-plugin

refactor: resolve local storage path on the plugin it self
This commit is contained in:
Juan Picado @jotadeveloper 2018-01-13 17:04:21 +01:00 committed by GitHub
commit c6c02f8c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 50 deletions

View File

@ -16,9 +16,8 @@
},
"dependencies": {
"@verdaccio/file-locking": "0.0.5",
"@verdaccio/local-storage": "0.1.0",
"@verdaccio/local-storage": "0.1.2",
"@verdaccio/streams": "0.0.2",
"@verdaccio/types": "0.1.0",
"JSONStream": "^1.1.1",
"apache-md5": "^1.1.2",
"async": "^2.6.0",
@ -49,6 +48,7 @@
"unix-crypt-td-js": "^1.0.0"
},
"devDependencies": {
"@verdaccio/types": "0.1.1",
"axios": "0.17.1",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
@ -84,8 +84,8 @@
"eslint-plugin-babel": "4.1.2",
"eslint-plugin-flowtype": "2.39.1",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-react": "7.5.1",
"eslint-plugin-jest": "^21.2.0",
"eslint-plugin-react": "7.5.1",
"extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.5",
"flow-bin": "0.52.0",
@ -138,6 +138,7 @@
"pretest": "npm run code:build",
"test": "cross-env NODE_ENV=test BABEL_ENV=test jest --maxWorkers 2",
"test:unit": "cross-env NODE_ENV=test BABEL_ENV=test jest '(/test/unit.*\\.spec|/test/webui/.*\\.spec)\\.js' --maxWorkers 2",
"test:func": "cross-env NODE_ENV=test BABEL_ENV=test jest '(/test/functional.*\\.func)\\.js' --maxWorkers 2",
"pre:ci": "npm run lint && npm run build:webui",
"coverage:publish": "codecov",
"lint": "npm run flow && eslint .",

View File

@ -71,13 +71,13 @@ class LocalStorage implements IStorage {
}
addPackage(name: string, pkg: Package, callback: Callback) {
const storage: IPackageStorage = this._getLocalStorage(name);
const storage: any = this._getLocalStorage(name);
if (_.isNil(storage)) {
return callback( Utils.ErrorCode.get404('this package cannot be added'));
}
storage.createPackage(pkgFileName, generatePackageTemplate(name), (err) => {
storage.createPackage(name, generatePackageTemplate(name), (err) => {
if (_.isNull(err) === false && err.code === fileExist) {
return callback( Utils.ErrorCode.get409());
}
@ -98,13 +98,13 @@ class LocalStorage implements IStorage {
* @return {Function}
*/
removePackage(name: string, callback: Callback) {
let storage: IPackageStorage = this._getLocalStorage(name);
let storage: any = this._getLocalStorage(name);
if (_.isNil(storage)) {
return callback( Utils.ErrorCode.get404());
}
storage.readPackage(pkgFileName, (err, data) => {
storage.readPackage(name, (err, data) => {
if (_.isNil(err) === false) {
if (err.code === noSuchFile) {
return callback( Utils.ErrorCode.get404());
@ -121,7 +121,6 @@ class LocalStorage implements IStorage {
// This will happen when database is locked
return callback(Utils.ErrorCode.get422(removeFailed.message));
}
storage.deletePackage(pkgFileName, (err) => {
if (err) {
return callback(err);
@ -407,6 +406,7 @@ class LocalStorage implements IStorage {
const uploadStream = new UploadTarball();
const _transform = uploadStream._transform;
const storage = this._getLocalStorage(name);
uploadStream.abort = function() {};
uploadStream.done = function() {};
@ -527,7 +527,7 @@ class LocalStorage implements IStorage {
* @private
* @return {ReadTarball}
*/
_streamSuccessReadTarBall(storage: IPackageStorage, filename: string) {
_streamSuccessReadTarBall(storage: any, filename: string) {
const stream = new ReadTarball();
const readTarballStream = storage.readTarball(filename);
const e404 = Utils.ErrorCode.get404;
@ -572,7 +572,7 @@ class LocalStorage implements IStorage {
return callback( Utils.ErrorCode.get404() );
}
this._readPackage(storage, callback);
this._readPackage(name, storage, callback);
}
/**
@ -645,14 +645,7 @@ class LocalStorage implements IStorage {
* @return {Object}
*/
_getLocalStorage(packageInfo: string): IPackageStorage {
const path: string = this._getLocalStoragePath(this.config.getMatchedPackagesSpec(packageInfo).storage);
if (_.isString(path) === false) {
this.logger.debug( {name: packageInfo}, 'this package has no storage defined: @{name}' );
return;
}
return this.localData.getPackageStorage(packageInfo, path);
return this.localData.getPackageStorage(packageInfo);
}
/**
@ -660,8 +653,8 @@ class LocalStorage implements IStorage {
* @param {Object} storage
* @param {Function} callback
*/
_readPackage(storage: IPackageStorage, callback: Callback) {
storage.readPackage(pkgFileName, (err, result) => {
_readPackage(name: string, storage: any, callback: Callback) {
storage.readPackage(name, (err, result) => {
if (err) {
if (err.code === noSuchFile) {
return callback( Utils.ErrorCode.get404() );
@ -674,20 +667,6 @@ class LocalStorage implements IStorage {
});
}
/**
* Verify the right local storage location.
* @param {String} path
* @return {String}
* @private
*/
_getLocalStoragePath(path: string): string {
if (_.isNil(path) === false) {
return path;
}
return this.config.storage;
}
/**
* Walks through each package and calls `on_package` on them.
* @param {*} onPackage
@ -751,12 +730,12 @@ class LocalStorage implements IStorage {
* @return {Function}
*/
_readCreatePackage(name: string, callback: Callback) {
const storage: IPackageStorage = this._getLocalStorage(name);
const storage: any = this._getLocalStorage(name);
if (_.isNil(storage)) {
return this._createNewPackage(name, callback);
}
storage.readPackage(pkgFileName, (err, data) => {
storage.readPackage(name, (err, data) => {
// TODO: race condition
if (_.isNil(err) === false) {
if (err.code === noSuchFile) {
@ -812,11 +791,11 @@ class LocalStorage implements IStorage {
* @return {Function}
*/
_writePackage(name: string, json: Package, callback: Callback) {
const storage: IPackageStorage = this._getLocalStorage(name);
const storage: any = this._getLocalStorage(name);
if (_.isNil(storage)) {
return callback();
}
storage.savePackage(pkgFileName, this._setDefaultRevision(json), callback);
storage.savePackage(name, this._setDefaultRevision(json), callback);
}
_setDefaultRevision(json: Package) {
@ -830,7 +809,7 @@ class LocalStorage implements IStorage {
return json;
}
_deleteAttachments(storage: IPackageStorage, attachments: string[], callback: Callback): void {
_deleteAttachments(storage: any, attachments: string[], callback: Callback): void {
const unlinkNext = function(cb) {
if (_.isEmpty(attachments)) {
return cb();

View File

@ -27,6 +27,10 @@ function isES6(plugin) {
return Object.keys(plugin).includes('default');
}
function mergeConfig(appConfig, pluginConfig) {
return _.merge(appConfig, pluginConfig);
}
/**
* Load a plugin following the rules
* - First try to load from the internal directory plugins (which will disappear soon or later).
@ -74,7 +78,7 @@ function loadPlugin(config, plugin_configs, params, sanity_check) {
}
/* eslint new-cap:off */
plugin = isES6(plugin) ? new plugin.default(plugin_configs[p], params) : plugin(plugin_configs[p], params);
plugin = isES6(plugin) ? new plugin.default(mergeConfig(config, plugin_configs[p]), params) : plugin(plugin_configs[p], params);
/* eslint new-cap:off */
if (plugin === null || !sanity_check(plugin)) {

View File

@ -59,31 +59,31 @@
version "8.5.7"
resolved "https://registry.npmjs.org/@types/node/-/node-8.5.7.tgz#9c498c35af354dcfbca3790fb2e81129e93cf0e2"
"@verdaccio/file-locking@0.0.5", "@verdaccio/file-locking@^0.0.5":
"@verdaccio/file-locking@0.0.5":
version "0.0.5"
resolved "https://registry.npmjs.org/@verdaccio/file-locking/-/file-locking-0.0.5.tgz#6172dfa2f7094a1da8e4c14906bfa33836b5713d"
dependencies:
lockfile "1.0.3"
lodash "4.17.4"
"@verdaccio/local-storage@0.1.0":
version "0.1.0"
resolved "https://registry.npmjs.org/@verdaccio/local-storage/-/local-storage-0.1.0.tgz#bab877e5d07bea926c97f251bddf2bfa9edd7026"
"@verdaccio/local-storage@0.1.2":
version "0.1.2"
resolved "https://registry.npmjs.org/@verdaccio/local-storage/-/local-storage-0.1.2.tgz#02698392af1c1e7951e9e4fc7de1d61ca1bc4483"
dependencies:
"@verdaccio/file-locking" "^0.0.5"
"@verdaccio/streams" "^0.0.2"
"@verdaccio/file-locking" "0.0.5"
"@verdaccio/streams" "0.0.2"
async "2.5.0"
http-errors "1.6.2"
lodash "4.17.4"
mkdirp "0.5.1"
"@verdaccio/streams@0.0.2", "@verdaccio/streams@^0.0.2":
"@verdaccio/streams@0.0.2":
version "0.0.2"
resolved "https://registry.npmjs.org/@verdaccio/streams/-/streams-0.0.2.tgz#72cd65449e657b462a1ca094f663cad9ea872427"
"@verdaccio/types@0.1.0":
version "0.1.0"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-0.1.0.tgz#2a0a6066bbbb7841d29298463e761147fb1f7f00"
"@verdaccio/types@0.1.1":
version "0.1.1"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-0.1.1.tgz#0801494fbd0284f6e92d94c85b90d97ba9dfa1c8"
JSONStream@^1.0.4, JSONStream@^1.1.1:
version "1.3.2"