Skip to content

Commit

Permalink
fix: migration from main repository merge #306
Browse files Browse the repository at this point in the history
  • Loading branch information
@jotadeveloper authored and sergiohgz committed Aug 13, 2019
1 parent 072b7ca commit 8fbe86e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
40 changes: 20 additions & 20 deletions plugins/local-storage/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/local-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"mkdirp": "0.5.1"
},
"devDependencies": {
"@verdaccio/types": "0.0.3",
"@verdaccio/types": "0.0.4",
"babel-cli": "6.24.1",
"babel-core": "6.25.0",
"babel-eslint": "^7.2.3",
Expand Down
73 changes: 67 additions & 6 deletions plugins/local-storage/src/local-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {ILocalData, LocalStorage, Logger} from '@verdaccio/types';
path: string;
logger: Logger;
data: LocalStorage;
locked: boolean;

/**
* Load an parse the local json database.
Expand All @@ -22,34 +23,84 @@ import type {ILocalData, LocalStorage, Logger} from '@verdaccio/types';
constructor(path: string, logger: Logger) {
this.path = path;
this.logger = logger;
this.locked = false;
this.data = this._fetchLocalPackages();
}

/**
* Fetch local packages.
* @private
* @return {Object}
*/
_fetchLocalPackages() {
const emptyDatabase = {list: []};

try {
this.data = JSON.parse(fs.readFileSync(this.path, 'utf8'));
} catch (_) {
this.data = {list: []};
const dbFile = fs.readFileSync(this.path, 'utf8');

if (!dbFile) { // readFileSync is platform specific, FreeBSD might return null
return emptyDatabase;
}

const db = this._parseDatabase(dbFile);

if (!db) {
return emptyDatabase;
}

return db;
} catch (err) {
// readFileSync is platform specific, macOS, Linux and Windows thrown an error
// Only recreate if file not found to prevent data loss
if (err.code !== 'ENOENT') {
this.locked = true;
this.logger.error(
'Failed to read package database file, please check the error printed below:\n',
`File Path: ${this.path}\n\n ${err.message}`
);
}
return emptyDatabase;
}
}

/**
* Parse the local database.
* @param {Object} dbFile
* @private
* @return {Object}
*/
_parseDatabase(dbFile: any) {
try {
return JSON.parse(dbFile);
} catch (err) {
this.logger.error(`Package database file corrupted (invalid JSON), please check the error printed below.\nFile Path: ${this.path}`, err);
this.locked = true;
}
}

/**
* Add a new element.
* @param {*} name
* @return {Error|*}
*/
add(name: string) {
if (this.data.list.indexOf(name) === -1) {
this.data.list.push(name);
this.sync();
return this.sync();
}
}

/**
* Remove an element from the database.
* @param {*} name
* @return {Error|*}
*/
remove(name: string) {
const i = this.data.list.indexOf(name);
if (i !== -1) {
this.data.list.splice(i, 1);
}
this.sync();
return this.sync();
}

/**
Expand All @@ -62,16 +113,26 @@ import type {ILocalData, LocalStorage, Logger} from '@verdaccio/types';

/**
* Syncronize {create} database whether does not exist.
* @return {Error|*}
*/
sync() {
if (this.locked) {
this.logger.error('Database is locked, please check error message printed during startup to prevent data loss.');
return new Error('Verdaccio database is locked, please contact your administrator to checkout logs during verdaccio startup.');
}
// Uses sync to prevent ugly race condition
try {
mkdirp.sync(Path.dirname(this.path));
} catch (err) {
// perhaps a logger instance?
/* eslint no-empty:off */
}
fs.writeFileSync(this.path, JSON.stringify(this.data));

try {
fs.writeFileSync(this.path, JSON.stringify(this.data));
} catch (err) {
return err;
}
}

}
Expand Down
17 changes: 14 additions & 3 deletions plugins/local-storage/src/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Path from 'path';
import Stream from 'stream';
import UrlNode from 'url';
import _ from 'lodash';
// $FlowFixMe
import async from 'async';

import LocalFS from './local-fs';
Expand Down Expand Up @@ -130,6 +131,12 @@ class Storage implements IStorage {
}
this._normalizePackage(data);

let removeFailed = this.localList.remove(name);
if (removeFailed) {
// This will happen when database is locked
return callback(this.utils.ErrorCode.get422(removeFailed.message));
}

storage.unlink(pkgFileName, function(err) {
if (err) {
return callback(err);
Expand All @@ -156,7 +163,6 @@ class Storage implements IStorage {
});
});
});
this.localList.remove(name);
}

/**
Expand Down Expand Up @@ -193,7 +199,7 @@ class Storage implements IStorage {
url: version.dist.tarball,
sha: version.dist.shasum,
};

// $FlowFixMe
const upLink: string = version[Symbol.for('__verdaccio_uplink')];

if (_.isNil(upLink) === false) {
Expand Down Expand Up @@ -313,7 +319,12 @@ class Storage implements IStorage {

data.versions[version] = metadata;
this.utils.tag_version(data, version, tag);
this.localList.add(name);

let addFailed = this.localList.add(name);
if (addFailed) {
return cb(this.utils.ErrorCode.get422(addFailed.message));
}

cb();
}, callback);
}
Expand Down

0 comments on commit 8fbe86e

Please sign in to comment.