@verdaccio/local-storage
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -46,6 +46,37 @@ 'use strict'; | ||
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 || _load_fs()).default.readFileSync(this.path, 'utf8')); | ||
} catch (_) { | ||
this.data = { list: [] }; | ||
const dbFile = (_fs || _load_fs()).default.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; | ||
} | ||
@@ -55,4 +86,20 @@ } | ||
/** | ||
* Parse the local database. | ||
* @param {Object} dbFile | ||
* @private | ||
* @return {Object} | ||
*/ | ||
_parseDatabase(dbFile) { | ||
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|*} | ||
*/ | ||
@@ -62,3 +109,3 @@ add(name) { | ||
this.data.list.push(name); | ||
this.sync(); | ||
return this.sync(); | ||
} | ||
@@ -70,2 +117,3 @@ } | ||
* @param {*} name | ||
* @return {Error|*} | ||
*/ | ||
@@ -77,3 +125,3 @@ remove(name) { | ||
} | ||
this.sync(); | ||
return this.sync(); | ||
} | ||
@@ -91,4 +139,9 @@ | ||
* 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 | ||
@@ -101,3 +154,8 @@ try { | ||
} | ||
(_fs || _load_fs()).default.writeFileSync(this.path, (0, (_stringify || _load_stringify()).default)(this.data)); | ||
try { | ||
(_fs || _load_fs()).default.writeFileSync(this.path, (0, (_stringify || _load_stringify()).default)(this.data)); | ||
} catch (err) { | ||
return err; | ||
} | ||
} | ||
@@ -104,0 +162,0 @@ |
@@ -89,2 +89,3 @@ 'use strict'; | ||
// $FlowFixMe | ||
const pkgFileName = 'package.json'; | ||
@@ -186,2 +187,8 @@ | ||
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) { | ||
@@ -213,3 +220,2 @@ if (err) { | ||
}); | ||
this.localList.remove(name); | ||
} | ||
@@ -251,3 +257,3 @@ | ||
}; | ||
// $FlowFixMe | ||
const upLink = version[(0, (_for || _load_for()).default)('__verdaccio_uplink')]; | ||
@@ -367,3 +373,8 @@ | ||
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(); | ||
@@ -698,3 +709,3 @@ }, callback); | ||
if (stats.mtimeMs > parseInt(startKey, 10)) { | ||
if (stats.mtime.getTime() > parseInt(startKey, 10)) { | ||
this.getPackageMetadata(item.name, (err, data) => { | ||
@@ -701,0 +712,0 @@ if (err) { |
{ | ||
"name": "@verdaccio/local-storage", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "local storage implementation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42577
1240