0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-03-18 02:22:46 -05:00

refactor(storage): extract common library need it for plugins

This commit is contained in:
Juan Picado @jotadeveloper 2017-07-08 16:17:09 +02:00
parent 770ef82fe1
commit 2187a9e8b8
No known key found for this signature in database
GPG key ID: 18AC54485952D158
6 changed files with 2390 additions and 664 deletions

View file

@ -1,152 +0,0 @@
'use strict';
/**
* file-locking.js - file system locking (replaces fs-ext)
*/
const async = require('async');
const locker = require('lockfile');
const fs = require('fs');
const path = require('path');
// locks a file by creating a lock file
const lockFile = function(name, next) {
const lockFileName = `${name}.lock`;
const lockOpts = {
wait: 1000, // time (ms) to wait when checking for stale locks
pollPeriod: 100, // how often (ms) to re-check stale locks
stale: 5 * 60 * 1000, // locks are considered stale after 5 minutes
retries: 100, // number of times to attempt to create a lock
retryWait: 100, // time (ms) between tries
};
async.series({
statdir: function(callback) {
// test to see if the directory exists
fs.stat(path.dirname(name), function(err, stats) {
if (err) {
callback(err);
} else if (!stats.isDirectory()) {
callback(new Error(path.dirname(name) + ' is not a directory'));
} else {
callback(null);
}
});
},
statfile: function(callback) {
// test to see if the file to lock exists
fs.stat(name, function(err, stats) {
if (err) {
callback(err);
} else if (!stats.isFile()) {
callback(new Error(path.dirname(name) + ' is not a file'));
} else {
callback(null);
}
});
},
lockfile: function(callback) {
// try to lock the file
locker.lock(lockFileName, lockOpts, callback);
},
}, function(err) {
if (err) {
// lock failed
return next(err);
}
// lock succeeded
return next(null);
});
};
// unlocks file by removing existing lock file
const unlockFile= function(name, next) {
const lockFileName = `${name}.lock`;
locker.unlock(lockFileName, function(err) {
if (err) {
return next(err);
}
return next(null);
});
};
/**
* Reads a local file, which involves
* optionally taking a lock
* reading the file contents
* optionally parsing JSON contents
* @param {*} name
* @param {*} options
* @param {*} next
*/
function readFile(name, options, next) {
if (typeof options === 'function' && next === null) {
next = options;
options = {};
}
options = options || {};
options.lock = options.lock || false;
options.parse = options.parse || false;
const lock = function(callback) {
if (!options.lock) {
return callback(null);
}
lockFile(name, function(err) {
if (err) {
return callback(err);
}
return callback(null);
});
};
const read = function(callback) {
fs.readFile(name, 'utf8', function(err, contents) {
if (err) {
return callback(err);
}
callback(null, contents);
});
};
const parseJSON = function(contents, callback) {
if (!options.parse) {
return callback(null, contents);
}
try {
contents = JSON.parse(contents);
return callback(null, contents);
} catch (err) {
return callback(err);
}
};
async.waterfall([
lock,
read,
parseJSON,
],
function(err, result) {
if (err) {
return next(err);
} else {
return next(null, result);
}
});
}
exports.lockFile = lockFile;
exports.unlockFile = unlockFile;
exports.readFile = readFile;

View file

@ -5,7 +5,7 @@
let crypto = require('crypto');
let crypt3 = require('./crypt3');
let md5 = require('apache-md5');
let locker = require('../../file-locking');
let locker = require('@verdaccio/file-locking');
// this function neither unlocks file nor closes it
// it'll have to be done manually later

View file

@ -7,7 +7,7 @@ const path = require('path');
const createError = require('http-errors');
const mkdirp = require('mkdirp');
const MyStream = require('../streams');
const locker = require('../../file-locking');
const locker = require('@verdaccio/file-locking');
const fileExist = 'EEXISTS';
const noSuchFile = 'ENOENT';
@ -201,7 +201,10 @@ const lock_and_read = function(name, cb) {
};
const lockAndReadJSON = function(name, cb) {
locker.readFile(name, {lock: true, parse: true}, function(err, res) {
locker.readFile(name, {
lock: true,
parse: true,
}, function(err, res) {
if (err) {
return cb(err);
}

2785
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -15,6 +15,7 @@
"verdaccio": "./bin/verdaccio"
},
"dependencies": {
"@verdaccio/file-locking": "^0.0.3",
"JSONStream": "^1.1.1",
"apache-md5": "^1.1.2",
"async": "^2.0.1",

107
yarn.lock
View file

@ -2,6 +2,14 @@
# yarn lockfile v1
"@verdaccio/file-locking@^0.0.3":
version "0.0.3"
resolved "https://registry.npmjs.org/@verdaccio/file-locking/-/file-locking-0.0.3.tgz#32d3702ff5184d1d3893f0f1d47629ad33d1c113"
dependencies:
babel-polyfill "6.23.0"
lockfile "1.0.3"
lodash "4.17.4"
JSONStream@^1.0.3, JSONStream@^1.1.1:
version "1.3.1"
resolved "https://registry.npmjs.org/JSONStream/-/JSONStream-1.3.1.tgz#707f761e01dae9e16f1bcf93703b78c70966579a"
@ -252,6 +260,14 @@ babel-messages@^6.23.0:
dependencies:
babel-runtime "^6.22.0"
babel-polyfill@6.23.0:
version "6.23.0"
resolved "https://registry.npmjs.org/babel-polyfill/-/babel-polyfill-6.23.0.tgz#8364ca62df8eafb830499f699177466c3b03499d"
dependencies:
babel-runtime "^6.22.0"
core-js "^2.4.0"
regenerator-runtime "^0.10.0"
babel-runtime@^6.22.0:
version "6.23.0"
resolved "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.23.0.tgz#0a9489f144de70efb3ce4300accdb329e2fc543b"
@ -320,10 +336,6 @@ block-stream@*:
dependencies:
inherits "~2.0.0"
bluebird@^2.3, bluebird@^2.9.x:
version "2.11.0"
resolved "https://registry.npmjs.org/bluebird/-/bluebird-2.11.0.tgz#534b9033c022c9579c56ba3b3e5a5caafbb650e1"
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.6"
resolved "https://registry.npmjs.org/bn.js/-/bn.js-4.11.6.tgz#53344adb14617a13f6e8dd2ce28905d1c0ba3215"
@ -737,18 +749,6 @@ co@^4.6.0:
version "4.6.0"
resolved "https://registry.npmjs.org/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
codacy-coverage@^2.0.2:
version "2.0.2"
resolved "https://registry.npmjs.org/codacy-coverage/-/codacy-coverage-2.0.2.tgz#394f2f3c0e2b8ee924281e633df51e29b94dd8d9"
dependencies:
bluebird "^2.9.x"
commander "^2.x"
joi "^6.4.x"
lcov-parse "0.x"
lodash "^4.17.4"
log-driver "^1.x"
request-promise "^0.x"
code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
@ -784,7 +784,7 @@ combined-stream@^1.0.5, combined-stream@~1.0.5:
dependencies:
delayed-stream "~1.0.0"
commander@2.9.0, commander@^2.9.0, commander@^2.x:
commander@2.9.0, commander@^2.9.0:
version "2.9.0"
resolved "https://registry.npmjs.org/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4"
dependencies:
@ -876,16 +876,6 @@ core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
coveralls@^2.13.0:
version "2.13.1"
resolved "https://registry.npmjs.org/coveralls/-/coveralls-2.13.1.tgz#d70bb9acc1835ec4f063ff9dac5423c17b11f178"
dependencies:
js-yaml "3.6.1"
lcov-parse "0.0.10"
log-driver "1.2.5"
minimist "1.2.0"
request "2.79.0"
create-ecdh@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.0.tgz#888c723596cdf7612f6498233eebd7a35301737d"
@ -2179,10 +2169,6 @@ isarray@~0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
isemail@1.x.x:
version "1.2.0"
resolved "https://registry.npmjs.org/isemail/-/isemail-1.2.0.tgz#be03df8cc3e29de4d2c5df6501263f1fa4595e9a"
isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
@ -2253,26 +2239,10 @@ jodid25519@^1.0.0:
dependencies:
jsbn "~0.1.0"
joi@^6.4.x:
version "6.10.1"
resolved "https://registry.npmjs.org/joi/-/joi-6.10.1.tgz#4d50c318079122000fe5f16af1ff8e1917b77e06"
dependencies:
hoek "2.x.x"
isemail "1.x.x"
moment "2.x.x"
topo "1.x.x"
js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
js-yaml@3.6.1:
version "3.6.1"
resolved "https://registry.npmjs.org/js-yaml/-/js-yaml-3.6.1.tgz#6e5fe67d8b205ce4d22fad05b7781e8dadcc4b30"
dependencies:
argparse "^1.0.7"
esprima "^2.6.0"
"js-yaml@3.x >=3.2", js-yaml@^3.5.1, js-yaml@^3.6.0:
version "3.8.3"
resolved "https://registry.npmjs.org/js-yaml/-/js-yaml-3.8.3.tgz#33a05ec481c850c8875929166fe1beb61c728766"
@ -2368,10 +2338,6 @@ lcid@^1.0.0:
dependencies:
invert-kv "^1.0.0"
lcov-parse@0.0.10, lcov-parse@0.x:
version "0.0.10"
resolved "https://registry.npmjs.org/lcov-parse/-/lcov-parse-0.0.10.tgz#1b0b8ff9ac9c7889250582b70b71315d9da6d9a3"
less@~2.7.1:
version "2.7.2"
resolved "https://registry.npmjs.org/less/-/less-2.7.2.tgz#368d6cc73e1fb03981183280918743c5dcf9b3df"
@ -2418,7 +2384,7 @@ load-json-file@^1.0.0:
pinkie-promise "^2.0.0"
strip-bom "^2.0.0"
lockfile@^1.0.1:
lockfile@1.0.3, lockfile@^1.0.1:
version "1.0.3"
resolved "https://registry.npmjs.org/lockfile/-/lockfile-1.0.3.tgz#2638fc39a0331e9cac1a04b71799931c9c50df79"
@ -2473,14 +2439,14 @@ lodash.memoize@~3.0.3:
version "3.0.4"
resolved "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-3.0.4.tgz#2dcbd2c287cbc0a55cc42328bd0c736150d53e3f"
lodash@^3.10.0, lodash@^3.10.1, lodash@~3.10.1:
version "3.10.1"
resolved "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0, lodash@^4.8.2:
lodash@4.17.4, lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0, lodash@^4.8.2:
version "4.17.4"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
lodash@^3.10.1, lodash@~3.10.1:
version "3.10.1"
resolved "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
lodash@~4.16.4:
version "4.16.6"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.16.6.tgz#d22c9ac660288f3843e16ba7d2b5d06cca27d777"
@ -2489,10 +2455,6 @@ lodash@~4.3.0:
version "4.3.0"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.3.0.tgz#efd9c4a6ec53f3b05412429915c3e4824e4d25a4"
log-driver@1.2.5, log-driver@^1.x:
version "1.2.5"
resolved "https://registry.npmjs.org/log-driver/-/log-driver-1.2.5.tgz#7ae4ec257302fd790d557cb10c97100d857b0056"
longest@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
@ -2639,7 +2601,7 @@ minimist@0.0.8, minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
minimist@1.2.0, minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0:
minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
@ -2689,7 +2651,7 @@ module-deps@^4.0.8:
through2 "^2.0.0"
xtend "^4.0.0"
moment@2.x.x, moment@^2.10.6:
moment@^2.10.6:
version "2.18.1"
resolved "https://registry.npmjs.org/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
@ -3165,7 +3127,7 @@ read-pkg@^1.0.0:
normalize-package-data "^2.3.2"
path-type "^1.0.0"
"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.2, readable-stream@^2.1.2, readable-stream@^2.1.4, readable-stream@^2.1.5, readable-stream@^2.2.6:
"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.2, readable-stream@^2.1.4, readable-stream@^2.1.5, readable-stream@^2.2.6:
version "2.2.9"
resolved "https://registry.npmjs.org/readable-stream/-/readable-stream-2.2.9.tgz#cf78ec6f4a6d1eb43d26488cac97f042e74b7fc8"
dependencies:
@ -3260,15 +3222,6 @@ repeating@^2.0.0:
dependencies:
is-finite "^1.0.0"
request-promise@^0.x:
version "0.4.3"
resolved "https://registry.npmjs.org/request-promise/-/request-promise-0.4.3.tgz#3c8ddc82f06f8908d720aede1d6794258e22121c"
dependencies:
bluebird "^2.3"
chalk "^1.1.0"
lodash "^3.10.0"
request "^2.34"
request@2.79.0:
version "2.79.0"
resolved "https://registry.npmjs.org/request/-/request-2.79.0.tgz#4dfe5bf6be8b8cdc37fcf93e04b65577722710de"
@ -3294,7 +3247,7 @@ request@2.79.0:
tunnel-agent "~0.4.1"
uuid "^3.0.0"
request@^2.34, request@^2.72.0, request@^2.81.0:
request@^2.72.0, request@^2.81.0:
version "2.81.0"
resolved "https://registry.npmjs.org/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
dependencies:
@ -3750,12 +3703,6 @@ to-fast-properties@^1.0.1:
version "1.0.2"
resolved "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.2.tgz#f3f5c0c3ba7299a7ef99427e44633257ade43320"
topo@1.x.x:
version "1.1.0"
resolved "https://registry.npmjs.org/topo/-/topo-1.1.0.tgz#e9d751615d1bb87dc865db182fa1ca0a5ef536d5"
dependencies:
hoek "2.x.x"
tough-cookie@~2.3.0:
version "2.3.2"
resolved "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"