proper-lockfile
Advanced tools
Comparing version 1.2.0 to 2.0.0
{ | ||
"root": true, | ||
"extends": [ | ||
"@satazor/eslint-config/es5", | ||
"@satazor/eslint-config/addons/node" | ||
"@satazor/eslint-config/es6", | ||
"@satazor/eslint-config/addons/node", | ||
"@satazor/eslint-config/addons/node-v4-es6" | ||
] | ||
} |
121
index.js
'use strict'; | ||
var fs = require('graceful-fs'); | ||
var path = require('path'); | ||
var extend = require('extend'); | ||
var errcode = require('err-code'); | ||
var retry = require('retry'); | ||
var syncFs = require('./lib/syncFs'); | ||
const fs = require('graceful-fs'); | ||
const path = require('path'); | ||
const retry = require('retry'); | ||
const syncFs = require('./lib/syncFs'); | ||
var locks = {}; | ||
const locks = {}; | ||
function getLockFile(file) { | ||
return file + '.lock'; | ||
return `${file}.lock`; | ||
} | ||
@@ -28,3 +26,3 @@ | ||
// Use mkdir to create the lockfile (atomic operation) | ||
options.fs.mkdir(getLockFile(file), function (err) { | ||
options.fs.mkdir(getLockFile(file), (err) => { | ||
// If successful, we are done | ||
@@ -42,6 +40,6 @@ if (!err) { | ||
if (options.stale <= 0) { | ||
return callback(errcode('Lock file is already being hold', 'ELOCKED', { file: file })); | ||
return callback(Object.assign(new Error('Lock file is already being hold'), { code: 'ELOCKED', file })); | ||
} | ||
options.fs.stat(getLockFile(file), function (err, stat) { | ||
options.fs.stat(getLockFile(file), (err, stat) => { | ||
if (err) { | ||
@@ -51,3 +49,3 @@ // Retry if the lockfile has been removed (meanwhile) | ||
if (err.code === 'ENOENT') { | ||
return acquireLock(file, extend({}, options, { stale: 0 }), callback); | ||
return acquireLock(file, Object.assign({}, options, { stale: 0 }), callback); | ||
} | ||
@@ -59,3 +57,3 @@ | ||
if (!isLockStale(stat, options)) { | ||
return callback(errcode('Lock file is already being hold', 'ELOCKED', { file: file })); | ||
return callback(Object.assign(new Error('Lock file is already being hold'), { code: 'ELOCKED', file })); | ||
} | ||
@@ -65,3 +63,3 @@ | ||
// Skip stale check to avoid recursiveness | ||
removeLock(file, options, function (err) { | ||
removeLock(file, options, (err) => { | ||
if (err) { | ||
@@ -71,3 +69,3 @@ return callback(err); | ||
acquireLock(file, extend({}, options, { stale: 0 }), callback); | ||
acquireLock(file, Object.assign({}, options, { stale: 0 }), callback); | ||
}); | ||
@@ -84,3 +82,3 @@ }); | ||
// Remove lockfile, ignoring ENOENT errors | ||
options.fs.rmdir(getLockFile(file), function (err) { | ||
options.fs.rmdir(getLockFile(file), (err) => { | ||
if (err && err.code !== 'ENOENT') { | ||
@@ -95,3 +93,3 @@ return callback(err); | ||
function updateLock(file, options) { | ||
var lock = locks[file]; | ||
const lock = locks[file]; | ||
@@ -104,8 +102,8 @@ /* istanbul ignore next */ | ||
lock.updateDelay = lock.updateDelay || options.update; | ||
lock.updateTimeout = setTimeout(function () { | ||
var mtime = Date.now() / 1000; | ||
lock.updateTimeout = setTimeout(() => { | ||
const mtime = Date.now() / 1000; | ||
lock.updateTimeout = null; | ||
options.fs.utimes(getLockFile(file), mtime, mtime, function (err) { | ||
options.fs.utimes(getLockFile(file), mtime, mtime, (err) => { | ||
// Ignore if the lock was released | ||
@@ -119,4 +117,4 @@ if (lock.released) { | ||
lock.lastUpdate > Date.now() - options.stale * 2) { | ||
return compromisedLock(file, lock, | ||
errcode(lock.updateError || 'Unable to update lock within the stale threshold', 'ECOMPROMISED')); | ||
return compromisedLock(file, lock, Object.assign(new Error(lock.updateError || 'Unable to update lock within the stale \ | ||
threshold'), { code: 'ECOMPROMISED' })); | ||
} | ||
@@ -131,3 +129,3 @@ | ||
if (err.code === 'ENOENT') { | ||
return compromisedLock(file, lock, errcode(err, 'ECOMPROMISED')); | ||
return compromisedLock(file, lock, Object.assign(err, { code: 'ECOMPROMISED' })); | ||
} | ||
@@ -180,3 +178,3 @@ | ||
options = extend({ | ||
options = Object.assign({ | ||
stale: 10000, | ||
@@ -186,3 +184,3 @@ update: null, | ||
retries: 0, | ||
fs: fs, | ||
fs, | ||
}, options); | ||
@@ -198,5 +196,3 @@ | ||
// Resolve to a canonical file path | ||
canonicalPath(file, options, function (err, file) { | ||
var operation; | ||
canonicalPath(file, options, (err, file) => { | ||
if (err) { | ||
@@ -207,7 +203,6 @@ return callback(err); | ||
// Attempt to acquire the lock | ||
operation = retry.operation(options.retries); | ||
operation.attempt(function () { | ||
acquireLock(file, options, function (err) { | ||
var lock; | ||
const operation = retry.operation(options.retries); | ||
operation.attempt(() => { | ||
acquireLock(file, options, (err) => { | ||
if (operation.retry(err)) { | ||
@@ -222,5 +217,5 @@ return; | ||
// We now own the lock | ||
locks[file] = lock = { | ||
options: options, | ||
compromised: compromised, | ||
const lock = locks[file] = { | ||
options, | ||
compromised, | ||
lastUpdate: Date.now(), | ||
@@ -232,9 +227,10 @@ }; | ||
callback(null, function (releasedCallback) { | ||
callback(null, (releasedCallback) => { | ||
if (lock.released) { | ||
return releasedCallback && releasedCallback(errcode('Lock is already released', 'ERELEASED')); | ||
return releasedCallback && | ||
releasedCallback(Object.assign(new Error('Lock is already released'), { code: 'ERELEASED' })); | ||
} | ||
// Not necessary to use realpath twice when unlocking | ||
unlock(file, extend({}, options, { realpath: false }), releasedCallback); | ||
unlock(file, Object.assign({}, options, { realpath: false }), releasedCallback); | ||
}); | ||
@@ -252,4 +248,4 @@ }); | ||
options = extend({ | ||
fs: fs, | ||
options = Object.assign({ | ||
fs, | ||
realpath: true, | ||
@@ -261,5 +257,3 @@ }, options); | ||
// Resolve to a canonical file path | ||
canonicalPath(file, options, function (err, file) { | ||
var lock; | ||
canonicalPath(file, options, (err, file) => { | ||
if (err) { | ||
@@ -270,5 +264,6 @@ return callback(err); | ||
// Skip if the lock is not acquired | ||
lock = locks[file]; | ||
const lock = locks[file]; | ||
if (!lock) { | ||
return callback(errcode('Lock is not acquired/owned by you', 'ENOTACQUIRED')); | ||
return callback(Object.assign(new Error('Lock is not acquired/owned by you'), { code: 'ENOTACQUIRED' })); | ||
} | ||
@@ -285,5 +280,2 @@ | ||
function lockSync(file, options, compromised) { | ||
var err; | ||
var release; | ||
if (typeof options === 'function') { | ||
@@ -301,6 +293,9 @@ compromised = options; | ||
if (options.retries.retries) { | ||
throw errcode('Cannot use retries with the sync api', 'ESYNC'); | ||
throw Object.assign(new Error('Cannot use retries with the sync api'), { code: 'ESYNC' }); | ||
} | ||
lock(file, options, compromised, function (_err, _release) { | ||
let err; | ||
let release; | ||
lock(file, options, compromised, (_err, _release) => { | ||
err = _err; | ||
@@ -318,8 +313,8 @@ release = _release; | ||
function unlockSync(file, options) { | ||
var err; | ||
options = options || {}; | ||
options.fs = syncFs(options.fs || fs); | ||
unlock(file, options, function (_err) { | ||
let err; | ||
unlock(file, options, (_err) => { | ||
err = _err; | ||
@@ -339,6 +334,6 @@ }); | ||
options = extend({ | ||
options = Object.assign({ | ||
stale: 10000, | ||
realpath: true, | ||
fs: fs, | ||
fs, | ||
}, options); | ||
@@ -349,3 +344,3 @@ | ||
// Resolve to a canonical file path | ||
canonicalPath(file, options, function (err, file) { | ||
canonicalPath(file, options, (err, file) => { | ||
if (err) { | ||
@@ -356,3 +351,3 @@ return callback(err); | ||
// Check if lockfile exists | ||
options.fs.stat(getLockFile(file), function (err, stat) { | ||
options.fs.stat(getLockFile(file), (err, stat) => { | ||
if (err) { | ||
@@ -372,9 +367,9 @@ // if does not exist, file is not locked. Otherwise, callback with error | ||
function checkSync(file, options) { | ||
var err; | ||
var locked; | ||
options = options || {}; | ||
options.fs = syncFs(options.fs || fs); | ||
check(file, options, function (_err, _locked) { | ||
let err; | ||
let locked; | ||
check(file, options, (_err, _locked) => { | ||
err = _err; | ||
@@ -394,4 +389,4 @@ locked = _locked; | ||
/* istanbul ignore next */ | ||
process.on('exit', function () { | ||
Object.keys(locks).forEach(function (file) { | ||
process.on('exit', () => { | ||
Object.keys(locks).forEach((file) => { | ||
try { locks[file].options.fs.rmdirSync(getLockFile(file)); } catch (e) { /* empty */ } | ||
@@ -398,0 +393,0 @@ }); |
'use strict'; | ||
function makeSync(fs, name) { | ||
var fn = fs[name + 'Sync']; | ||
const fn = fs[`${name}Sync`]; | ||
return function () { | ||
var callback = arguments[arguments.length - 1]; | ||
var args = Array.prototype.slice.call(arguments, 0, -1); | ||
var ret; | ||
const callback = arguments[arguments.length - 1]; | ||
const args = Array.prototype.slice.call(arguments, 0, -1); | ||
let ret; | ||
@@ -22,8 +22,7 @@ try { | ||
function syncFs(fs) { | ||
var fns = ['mkdir', 'realpath', 'stat', 'rmdir', 'utimes']; | ||
var obj = {}; | ||
var key; | ||
const fns = ['mkdir', 'realpath', 'stat', 'rmdir', 'utimes']; | ||
const obj = {}; | ||
// Create the sync versions of the methods that we need | ||
fns.forEach(function (name) { | ||
fns.forEach((name) => { | ||
obj[name] = makeSync(fs, name); | ||
@@ -33,3 +32,3 @@ }); | ||
// Copy the rest of the functions | ||
for (key in fs) { | ||
for (const key in fs) { | ||
if (!obj[key]) { | ||
@@ -36,0 +35,0 @@ obj[key] = fs[key]; |
{ | ||
"name": "proper-lockfile", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A inter-process and inter-machine lockfile utility that works on a local or network file system.", | ||
@@ -8,5 +8,5 @@ "main": "index.js", | ||
"lint": "eslint '{*.js,lib/**/*.js,test/**/*.js}' --ignore-pattern=test/coverage", | ||
"test": "mocha --bail", | ||
"test-cov": "istanbul cover --dir test/coverage _mocha -- --bail && echo open test/coverage/lcov-report/index.html", | ||
"test-travis": "istanbul cover _mocha --report lcovonly -- --bail && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
"test": "mocha", | ||
"test-cov": "istanbul cover --dir test/coverage _mocha && echo open test/coverage/lcov-report/index.html", | ||
"test-travis": "istanbul cover _mocha --report lcovonly && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" | ||
}, | ||
@@ -33,4 +33,2 @@ "bugs": { | ||
"dependencies": { | ||
"err-code": "^1.0.0", | ||
"extend": "^3.0.0", | ||
"graceful-fs": "^4.1.2", | ||
@@ -42,3 +40,3 @@ "retry": "^0.10.0" | ||
"async": "^2.0.0", | ||
"buffered-spawn": "^2.0.4", | ||
"buffered-spawn": "^3.0.0", | ||
"coveralls": "^2.11.6", | ||
@@ -52,3 +50,6 @@ "eslint": "^3.5.0", | ||
"stable": "^0.1.5" | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
} | ||
} |
@@ -83,6 +83,8 @@ # proper-lockfile | ||
```js | ||
var lockfile = require('proper-lockfile'); | ||
const lockfile = require('proper-lockfile'); | ||
lockfile.lock('some/file', function (err, release) { | ||
if (err) throw err; // Lock failed | ||
lockfile.lock('some/file', (err) => { | ||
if (err) { | ||
throw err; // Lock failed | ||
} | ||
@@ -98,4 +100,6 @@ // Do something while the file is locked | ||
// At this point the lock was effectively released or an error | ||
// ocurred while removing it | ||
if (err) throw err; | ||
// occurred while removing it | ||
if (err) { | ||
throw err; | ||
} | ||
});*/ | ||
@@ -114,3 +118,2 @@ }); | ||
Available options: | ||
@@ -123,6 +126,8 @@ | ||
```js | ||
var lockfile = require('proper-lockfile'); | ||
const lockfile = require('proper-lockfile'); | ||
lockfile.lock('some/file', function (err) { | ||
if (err) throw err; | ||
lockfile.lock('some/file', (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
@@ -135,4 +140,6 @@ // Later.. | ||
// At this point the lock was effectively released or an error | ||
// ocurred while removing it | ||
if (err) throw err; | ||
// occurred while removing it | ||
if (err) { | ||
throw err; | ||
} | ||
});*/ | ||
@@ -145,3 +152,3 @@ }); | ||
Check if the file is locked and its lockfile is not stale. Callback is called with callback(error, isLocked). | ||
Available options: | ||
@@ -153,9 +160,12 @@ | ||
```js | ||
var lockfile = require('proper-lockfile'); | ||
const lockfile = require('proper-lockfile'); | ||
lockfile.check('some/file', function (err, isLocked) { | ||
if (err) throw err; | ||
lockfile.check('some/file', (err, isLocked) => { | ||
if (err) { | ||
throw err; | ||
} | ||
// isLocked will be true if 'some/file' is locked, otherwise will be false if not locked | ||
// isLocked will be true if 'some/file' is locked, false otherwise | ||
}); | ||
@@ -187,2 +197,3 @@ ``` | ||
```js | ||
@@ -192,8 +203,4 @@ // Map SIGINT & SIGTERM to process exit | ||
process | ||
.once('SIGINT', function () { | ||
process.exit(1); | ||
}) | ||
.once('SIGTERM', function () { | ||
process.exit(1); | ||
}); | ||
.once('SIGINT', () => process.exit(1)) | ||
.once('SIGTERM', () => process.exit(1)); | ||
``` | ||
@@ -200,0 +207,0 @@ |
'use strict'; | ||
var fs = require('fs'); | ||
var lockfile = require('../../'); | ||
const fs = require('fs'); | ||
const lockfile = require('../../'); | ||
var file = __dirname + '/../tmp'; | ||
const file = `${__dirname}/../tmp`; | ||
fs.writeFileSync(file, ''); | ||
lockfile.lock(file, function (err) { | ||
lockfile.lock(file, (err) => { | ||
if (err) { | ||
@@ -12,0 +12,0 @@ process.exit(25); |
'use strict'; | ||
var cluster = require('cluster'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var rimraf = require('rimraf'); | ||
var sort = require('stable'); | ||
var lockfile = require('../../'); | ||
const cluster = require('cluster'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const rimraf = require('rimraf'); | ||
const sort = require('stable'); | ||
const lockfile = require('../../'); | ||
var file = __dirname + '/../tmp'; | ||
const file = `${__dirname}/../tmp`; | ||
function printExcerpt(logs, index) { | ||
logs.slice(Math.max(0, index - 50), index + 50).forEach(function (log, index) { | ||
process.stdout.write((index + 1) + ' ' + log.timestamp + ' ' + log.message + '\n'); | ||
logs.slice(Math.max(0, index - 50), index + 50).forEach((log, index) => { | ||
process.stdout.write(`${index + 1} ${log.timestamp} ${log.message}\n`); | ||
}); | ||
@@ -19,18 +19,15 @@ } | ||
function master() { | ||
var logs = []; | ||
var numCPUs = os.cpus().length; | ||
var i; | ||
var acquired; | ||
const numCPUs = os.cpus().length; | ||
let logs = []; | ||
let acquired; | ||
fs.writeFileSync(file, ''); | ||
rimraf.sync(file + '.lock'); | ||
rimraf.sync(`${file}.lock`); | ||
logs = []; | ||
for (i = 0; i < numCPUs; i += 1) { | ||
for (let i = 0; i < numCPUs; i += 1) { | ||
cluster.fork(); | ||
} | ||
cluster.on('online', function (worker) { | ||
worker.on('message', function (data) { | ||
cluster.on('online', (worker) => { | ||
worker.on('message', (data) => { | ||
logs.push(data.toString().trim()); | ||
@@ -40,17 +37,13 @@ }); | ||
cluster.on('exit', function () { | ||
cluster.on('exit', () => { | ||
throw new Error('Child died prematurely'); | ||
}); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
cluster.removeAllListeners('exit'); | ||
Object.keys(cluster.workers).forEach(function (id) { | ||
cluster.workers[id].removeAllListeners('message').kill(); | ||
}); | ||
cluster.disconnect(function () { | ||
cluster.disconnect(() => { | ||
// Parse & sort logs | ||
logs = logs.map(function (log) { | ||
var split = log.split(' '); | ||
logs = logs.map((log) => { | ||
const split = log.split(' '); | ||
@@ -60,3 +53,3 @@ return { timestamp: Number(split[0]), message: split[1] }; | ||
logs = sort(logs, function (log1, log2) { | ||
logs = sort(logs, (log1, log2) => { | ||
if (log1.timestamp > log2.timestamp) { | ||
@@ -79,7 +72,7 @@ return 1; | ||
// Validate logs | ||
logs.forEach(function (log, index) { | ||
logs.forEach((log, index) => { | ||
switch (log.message) { | ||
case 'LOCK_ACQUIRED': | ||
if (acquired) { | ||
process.stdout.write('\nInconsistent at line ' + (index + 1) + '\n'); | ||
process.stdout.write(`\nInconsistent at line ${index + 1}\n`); | ||
printExcerpt(logs, index); | ||
@@ -94,3 +87,3 @@ | ||
if (!acquired) { | ||
process.stdout.write('\nInconsistent at line ' + (index + 1) + '\n'); | ||
process.stdout.write(`\nInconsistent at line ${index + 1}\n`); | ||
printExcerpt(logs, index); | ||
@@ -113,20 +106,20 @@ process.exit(1); | ||
function slave() { | ||
var tryLock; | ||
process.on('disconnect', () => process.exit(0)); | ||
tryLock = function () { | ||
setTimeout(function () { | ||
process.send(Date.now() + ' LOCK_TRY'); | ||
const tryLock = () => { | ||
setTimeout(() => { | ||
process.send(`${Date.now()} LOCK_TRY\n`); | ||
lockfile.lock(file, function (err, unlock) { | ||
lockfile.lock(file, (err, unlock) => { | ||
if (err) { | ||
process.send(Date.now() + ' LOCK_BUSY\n'); | ||
process.send(`${Date.now()} LOCK_BUSY\n`); | ||
return tryLock(); | ||
} | ||
process.send(Date.now() + ' LOCK_ACQUIRED\n'); | ||
process.send(`${Date.now()} LOCK_ACQUIRED\n`); | ||
setTimeout(function () { | ||
process.send(Date.now() + ' LOCK_RELEASED\n'); | ||
setTimeout(() => { | ||
process.send(`${Date.now()} LOCK_RELEASED\n`); | ||
unlock(function (err) { | ||
unlock((err) => { | ||
if (err) { | ||
@@ -133,0 +126,0 @@ throw err; |
'use strict'; | ||
var fs = require('fs'); | ||
var lockfile = require('../../'); | ||
const fs = require('fs'); | ||
const lockfile = require('../../'); | ||
var file = __dirname + '/../tmp'; | ||
const file = `${__dirname}/../tmp`; | ||
fs.writeFileSync(file, ''); | ||
lockfile.lock(file, function (err) { | ||
lockfile.lock(file, (err) => { | ||
if (err) { | ||
@@ -12,0 +12,0 @@ throw err; |
603
test/test.js
'use strict'; | ||
var fs = require('graceful-fs'); | ||
var path = require('path'); | ||
var cp = require('child_process'); | ||
var expect = require('expect.js'); | ||
var extend = require('extend'); | ||
var rimraf = require('rimraf'); | ||
var spawn = require('buffered-spawn'); | ||
var async = require('async'); | ||
var lockfile = require('../'); | ||
const fs = require('graceful-fs'); | ||
const path = require('path'); | ||
const cp = require('child_process'); | ||
const expect = require('expect.js'); | ||
const rimraf = require('rimraf'); | ||
const spawn = require('buffered-spawn'); | ||
const async = require('async'); | ||
const lockfile = require('../'); | ||
var lockfileContents = fs.readFileSync(__dirname + '/../index.js').toString(); | ||
var tmpFileRealPath = path.join(__dirname, 'tmp'); | ||
var tmpFile = path.relative(process.cwd(), tmpFileRealPath); | ||
var tmpFileLock = tmpFileRealPath + '.lock'; | ||
var tmpFileSymlinkRealPath = tmpFileRealPath + '_symlink'; | ||
var tmpFileSymlink = tmpFile + '_symlink'; | ||
var tmpFileSymlinkLock = tmpFileSymlinkRealPath + '.lock'; | ||
var tmpNonExistentFile = path.join(__dirname, 'nonexistentfile'); | ||
const lockfileContents = fs.readFileSync(`${__dirname}/../index.js`).toString(); | ||
const tmpFileRealPath = path.join(__dirname, 'tmp'); | ||
const tmpFile = path.relative(process.cwd(), tmpFileRealPath); | ||
const tmpFileLock = `${tmpFileRealPath}.lock`; | ||
const tmpFileSymlinkRealPath = `${tmpFileRealPath}_symlink`; | ||
const tmpFileSymlink = `${tmpFile}_symlink`; | ||
const tmpFileSymlinkLock = `${tmpFileSymlinkRealPath}.lock`; | ||
const tmpNonExistentFile = path.join(__dirname, 'nonexistentfile'); | ||
function clearLocks(callback) { | ||
var toUnlock = []; | ||
const toUnlock = []; | ||
toUnlock.push(function (callback) { | ||
lockfile.unlock(tmpFile, { realpath: false }, function (err) { | ||
toUnlock.push((callback) => { | ||
lockfile.unlock(tmpFile, { realpath: false }, (err) => { | ||
callback(!err || err.code === 'ENOTACQUIRED' ? null : err); | ||
@@ -31,4 +30,4 @@ }); | ||
toUnlock.push(function (callback) { | ||
lockfile.unlock(tmpNonExistentFile, { realpath: false }, function (err) { | ||
toUnlock.push((callback) => { | ||
lockfile.unlock(tmpNonExistentFile, { realpath: false }, (err) => { | ||
callback(!err || err.code === 'ENOTACQUIRED' ? null : err); | ||
@@ -38,4 +37,4 @@ }); | ||
toUnlock.push(function (callback) { | ||
lockfile.unlock(tmpFileSymlink, { realpath: false }, function (err) { | ||
toUnlock.push((callback) => { | ||
lockfile.unlock(tmpFileSymlink, { realpath: false }, (err) => { | ||
callback(!err || err.code === 'ENOTACQUIRED' ? null : err); | ||
@@ -46,4 +45,4 @@ }); | ||
if (fs.existsSync(tmpFileSymlink)) { | ||
toUnlock.push(function (callback) { | ||
lockfile.unlock(tmpFileSymlink, function (err) { | ||
toUnlock.push((callback) => { | ||
lockfile.unlock(tmpFileSymlink, (err) => { | ||
callback(!err || err.code === 'ENOTACQUIRED' ? null : err); | ||
@@ -54,3 +53,3 @@ }); | ||
async.parallel(toUnlock, function (err) { | ||
async.parallel(toUnlock, (err) => { | ||
if (err) { | ||
@@ -69,4 +68,4 @@ return callback(err); | ||
describe('.lock()', function () { | ||
beforeEach(function () { | ||
describe('.lock()', () => { | ||
beforeEach(() => { | ||
fs.writeFileSync(tmpFile, ''); | ||
@@ -78,6 +77,4 @@ rimraf.sync(tmpFileSymlink); | ||
this.timeout(5000); | ||
it('should fail if the file does not exist by default', function (next) { | ||
lockfile.lock(tmpNonExistentFile, function (err) { | ||
it('should fail if the file does not exist by default', (next) => { | ||
lockfile.lock(tmpNonExistentFile, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -90,4 +87,4 @@ expect(err.code).to.be('ENOENT'); | ||
it('should not fail if the file does not exist and realpath is false', function (next) { | ||
lockfile.lock(tmpNonExistentFile, { realpath: false }, function (err) { | ||
it('should not fail if the file does not exist and realpath is false', (next) => { | ||
lockfile.lock(tmpNonExistentFile, { realpath: false }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -99,4 +96,4 @@ | ||
it('should fail if impossible to create the lockfile', function (next) { | ||
lockfile.lock('nonexistentdir/nonexistentfile', { realpath: false }, function (err) { | ||
it('should fail if impossible to create the lockfile', (next) => { | ||
lockfile.lock('nonexistentdir/nonexistentfile', { realpath: false }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -109,4 +106,4 @@ expect(err.code).to.be('ENOENT'); | ||
it('should create the lockfile', function (next) { | ||
lockfile.lock(tmpFile, function (err) { | ||
it('should create the lockfile', (next) => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -119,7 +116,7 @@ expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
it('should fail if already locked', function (next) { | ||
lockfile.lock(tmpFile, function (err) { | ||
it('should fail if already locked', (next) => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -134,6 +131,4 @@ expect(err.code).to.be('ELOCKED'); | ||
it('should retry several times if retries were specified', function (next) { | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, function (err, unlock) { | ||
it('should retry several times if retries were specified', (next) => { | ||
lockfile.lock(tmpFile, (err, unlock) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -143,3 +138,3 @@ | ||
lockfile.lock(tmpFile, { retries: { retries: 5, maxTimeout: 1000 } }, function (err) { | ||
lockfile.lock(tmpFile, { retries: { retries: 5, maxTimeout: 1000 } }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -152,4 +147,4 @@ | ||
it('should use the custom fs', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should use the custom fs', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -161,3 +156,3 @@ customFs.realpath = function (path, callback) { | ||
lockfile.lock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -170,14 +165,14 @@ expect(err.message).to.be('foo'); | ||
it('should resolve symlinks by default', function (next) { | ||
it('should resolve symlinks by default', (next) => { | ||
// Create a symlink to the tmp file | ||
fs.symlinkSync(tmpFileRealPath, tmpFileSymlinkRealPath); | ||
lockfile.lock(tmpFileSymlink, function (err) { | ||
lockfile.lock(tmpFileSymlink, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.be.an(Error); | ||
expect(err.code).to.be('ELOCKED'); | ||
lockfile.lock(tmpFile + '/../../test/tmp', function (err) { | ||
lockfile.lock(`${tmpFile}/../../test/tmp`, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -192,13 +187,13 @@ expect(err.code).to.be('ELOCKED'); | ||
it('should not resolve symlinks if realpath is false', function (next) { | ||
it('should not resolve symlinks if realpath is false', (next) => { | ||
// Create a symlink to the tmp file | ||
fs.symlinkSync(tmpFileRealPath, tmpFileSymlinkRealPath); | ||
lockfile.lock(tmpFileSymlink, { realpath: false }, function (err) { | ||
lockfile.lock(tmpFileSymlink, { realpath: false }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile, { realpath: false }, function (err) { | ||
lockfile.lock(tmpFile, { realpath: false }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile + '/../../test/tmp', { realpath: false }, function (err) { | ||
lockfile.lock(`${tmpFile}/../../test/tmp`, { realpath: false }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -213,4 +208,4 @@ expect(err.code).to.be('ELOCKED'); | ||
it('should remove and acquire over stale locks', function (next) { | ||
var mtime = (Date.now() - 60000) / 1000; | ||
it('should remove and acquire over stale locks', (next) => { | ||
const mtime = (Date.now() - 60000) / 1000; | ||
@@ -220,3 +215,3 @@ fs.mkdirSync(tmpFileLock); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -229,5 +224,5 @@ expect(fs.statSync(tmpFileLock).mtime.getTime()).to.be.greaterThan(Date.now() - 3000); | ||
it('should retry if the lockfile was removed when verifying staleness', function (next) { | ||
var mtime = (Date.now() - 60000) / 1000; | ||
var customFs = extend({}, fs); | ||
it('should retry if the lockfile was removed when verifying staleness', (next) => { | ||
const mtime = (Date.now() - 60000) / 1000; | ||
const customFs = Object.assign({}, fs); | ||
@@ -243,3 +238,3 @@ customFs.stat = function (path, callback) { | ||
lockfile.lock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -252,10 +247,9 @@ expect(fs.statSync(tmpFileLock).mtime.getTime()).to.be.greaterThan(Date.now() - 3000); | ||
it('should retry if the lockfile was removed when verifying staleness (not recursively)', function (next) { | ||
var mtime = (Date.now() - 60000) / 1000; | ||
var customFs = extend({}, fs); | ||
it('should retry if the lockfile was removed when verifying staleness (not recursively)', (next) => { | ||
const mtime = (Date.now() - 60000) / 1000; | ||
const customFs = Object.assign({}, fs); | ||
customFs.stat = function (path, callback) { | ||
var err; | ||
const err = new Error(); | ||
err = new Error(); | ||
err.code = 'ENOENT'; | ||
@@ -269,3 +263,3 @@ | ||
lockfile.lock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -278,5 +272,5 @@ expect(err.code).to.be('ELOCKED'); | ||
it('should fail if stating the lockfile errors out when verifying staleness', function (next) { | ||
var mtime = (Date.now() - 60000) / 1000; | ||
var customFs = extend({}, fs); | ||
it('should fail if stating the lockfile errors out when verifying staleness', (next) => { | ||
const mtime = (Date.now() - 60000) / 1000; | ||
const customFs = Object.assign({}, fs); | ||
@@ -290,3 +284,3 @@ customFs.stat = function (path, callback) { | ||
lockfile.lock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -299,5 +293,5 @@ expect(err.message).to.be('foo'); | ||
it('should fail if removing a stale lockfile errors out', function (next) { | ||
var mtime = (Date.now() - 60000) / 1000; | ||
var customFs = extend({}, fs); | ||
it('should fail if removing a stale lockfile errors out', (next) => { | ||
const mtime = (Date.now() - 60000) / 1000; | ||
const customFs = Object.assign({}, fs); | ||
@@ -311,3 +305,3 @@ customFs.rmdir = function (path, callback) { | ||
lockfile.lock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -320,13 +314,11 @@ expect(err.message).to.be('foo'); | ||
it('should update the lockfile mtime automatically', function (next) { | ||
lockfile.lock(tmpFile, { update: 1000 }, function (err) { | ||
var mtime; | ||
it('should update the lockfile mtime automatically', (next) => { | ||
lockfile.lock(tmpFile, { update: 1000 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
mtime = fs.statSync(tmpFileLock).mtime; | ||
let mtime = fs.statSync(tmpFileLock).mtime; | ||
// First update occurs at 1000ms | ||
setTimeout(function () { | ||
var stat = fs.statSync(tmpFileLock); | ||
setTimeout(() => { | ||
const stat = fs.statSync(tmpFileLock); | ||
@@ -338,4 +330,4 @@ expect(stat.mtime.getTime()).to.be.greaterThan(mtime.getTime()); | ||
// Second update occurs at 2000ms | ||
setTimeout(function () { | ||
var stat = fs.statSync(tmpFileLock); | ||
setTimeout(() => { | ||
const stat = fs.statSync(tmpFileLock); | ||
@@ -350,7 +342,7 @@ expect(stat.mtime.getTime()).to.be.greaterThan(mtime.getTime()); | ||
it('should set stale to a minimum of 2000', function (next) { | ||
it('should set stale to a minimum of 2000', (next) => { | ||
fs.mkdirSync(tmpFileLock); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: 100 }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: 100 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -361,4 +353,4 @@ expect(err.code).to.be('ELOCKED'); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: 100 }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: 100 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -371,7 +363,7 @@ | ||
it('should set stale to a minimum of 2000 (falsy)', function (next) { | ||
it('should set stale to a minimum of 2000 (falsy)', (next) => { | ||
fs.mkdirSync(tmpFileLock); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: false }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: false }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -382,4 +374,4 @@ expect(err.code).to.be('ELOCKED'); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: false }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: false }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -392,4 +384,4 @@ | ||
it('should call the compromised function if ENOENT was detected when updating the lockfile mtime', function (next) { | ||
lockfile.lock(tmpFile, { update: 1000 }, function (err) { | ||
it('should call the compromised function if ENOENT was detected when updating the lockfile mtime', (next) => { | ||
lockfile.lock(tmpFile, { update: 1000 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -399,3 +391,3 @@ expect(err.code).to.be('ECOMPROMISED'); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -405,3 +397,3 @@ | ||
}, next); | ||
}, function (err) { | ||
}, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -413,4 +405,4 @@ | ||
it('should call the compromised function if failed to update the lockfile mtime too many times', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should call the compromised function if failed to update the lockfile mtime too many times', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -421,5 +413,3 @@ customFs.utimes = function (path, atime, mtime, callback) { | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, { fs: customFs, update: 1000, stale: 5000 }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs, update: 1000, stale: 5000 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -430,3 +420,3 @@ expect(err.message).to.contain('foo'); | ||
next(); | ||
}, function (err) { | ||
}, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -436,7 +426,7 @@ }); | ||
it('should call the compromised function if updating the lockfile took too much time', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should call the compromised function if updating the lockfile took too much time', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
customFs.utimes = function (path, atime, mtime, callback) { | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
callback(new Error('foo')); | ||
@@ -446,5 +436,3 @@ }, 6000); | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, { fs: customFs, update: 1000, stale: 5000 }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs, update: 1000, stale: 5000 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -456,3 +444,3 @@ expect(err.code).to.be('ECOMPROMISED'); | ||
next(); | ||
}, function (err) { | ||
}, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -462,7 +450,7 @@ }); | ||
it('should call the compromised function if lock was acquired by someone else due to staleness', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should call the compromised function if lock was acquired by someone else due to staleness', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
customFs.utimes = function (path, atime, mtime, callback) { | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
callback(new Error('foo')); | ||
@@ -472,5 +460,3 @@ }, 6000); | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, { fs: customFs, update: 1000, stale: 5000 }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs, update: 1000, stale: 5000 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -481,7 +467,7 @@ expect(err.code).to.be('ECOMPROMISED'); | ||
next(); | ||
}, function (err) { | ||
}, (err) => { | ||
expect(err).to.not.be.ok(); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: 5000 }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: 5000 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -493,15 +479,12 @@ }); | ||
it('should throw an error by default when the lock is compromised', function (next) { | ||
var originalException; | ||
it('should throw an error by default when the lock is compromised', (next) => { | ||
const originalException = process.listeners('uncaughtException').pop(); | ||
this.timeout(10000); | ||
originalException = process.listeners('uncaughtException').pop(); | ||
process.removeListener('uncaughtException', originalException); | ||
process.once('uncaughtException', function (err) { | ||
process.once('uncaughtException', (err) => { | ||
expect(err).to.be.an(Error); | ||
expect(err.code).to.be('ECOMPROMISED'); | ||
process.nextTick(function () { | ||
process.nextTick(() => { | ||
process.on('uncaughtException', originalException); | ||
@@ -512,3 +495,3 @@ next(); | ||
lockfile.lock(tmpFile, { update: 1000 }, function (err) { | ||
lockfile.lock(tmpFile, { update: 1000 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -520,13 +503,13 @@ | ||
it('should set update to a minimum of 1000', function (next) { | ||
lockfile.lock(tmpFile, { update: 100 }, function (err) { | ||
var mtime = fs.statSync(tmpFileLock).mtime.getTime(); | ||
it('should set update to a minimum of 1000', (next) => { | ||
lockfile.lock(tmpFile, { update: 100 }, (err) => { | ||
const mtime = fs.statSync(tmpFileLock).mtime.getTime(); | ||
expect(err).to.not.be.ok(); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
expect(mtime).to.equal(fs.statSync(tmpFileLock).mtime.getTime()); | ||
}, 200); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
expect(fs.statSync(tmpFileLock).mtime.getTime()).to.be.greaterThan(mtime); | ||
@@ -539,13 +522,13 @@ | ||
it('should set update to a minimum of 1000 (falsy)', function (next) { | ||
lockfile.lock(tmpFile, { update: false }, function (err) { | ||
var mtime = fs.statSync(tmpFileLock).mtime.getTime(); | ||
it('should set update to a minimum of 1000 (falsy)', (next) => { | ||
lockfile.lock(tmpFile, { update: false }, (err) => { | ||
const mtime = fs.statSync(tmpFileLock).mtime.getTime(); | ||
expect(err).to.not.be.ok(); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
expect(mtime).to.equal(fs.statSync(tmpFileLock).mtime.getTime()); | ||
}, 200); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
expect(fs.statSync(tmpFileLock).mtime.getTime()).to.be.greaterThan(mtime); | ||
@@ -558,13 +541,13 @@ | ||
it('should set update to a maximum of stale / 2', function (next) { | ||
lockfile.lock(tmpFile, { update: 6000, stale: 5000 }, function (err) { | ||
var mtime = fs.statSync(tmpFileLock).mtime.getTime(); | ||
it('should set update to a maximum of stale / 2', (next) => { | ||
lockfile.lock(tmpFile, { update: 6000, stale: 5000 }, (err) => { | ||
const mtime = fs.statSync(tmpFileLock).mtime.getTime(); | ||
expect(err).to.not.be.ok(); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
expect(fs.statSync(tmpFileLock).mtime.getTime()).to.equal(mtime); | ||
}, 2000); | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
expect(fs.statSync(tmpFileLock).mtime.getTime()).to.be.greaterThan(mtime); | ||
@@ -578,4 +561,4 @@ | ||
describe('.unlock()', function () { | ||
beforeEach(function () { | ||
describe('.unlock()', () => { | ||
beforeEach(() => { | ||
fs.writeFileSync(tmpFile, ''); | ||
@@ -587,6 +570,4 @@ rimraf.sync(tmpFileSymlink); | ||
this.timeout(5000); | ||
it('should fail if the lock is not acquired', function (next) { | ||
lockfile.unlock(tmpFile, function (err) { | ||
it('should fail if the lock is not acquired', (next) => { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -599,10 +580,10 @@ expect(err.code).to.be('ENOTACQUIRED'); | ||
it('should release the lock', function (next) { | ||
lockfile.lock(tmpFile, function (err) { | ||
it('should release the lock', (next) => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.unlock(tmpFile, function (err) { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -616,4 +597,4 @@ | ||
it('should release the lock (without callback)', function (next) { | ||
lockfile.lock(tmpFile, function (err) { | ||
it('should release the lock (without callback)', (next) => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -623,4 +604,4 @@ | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -634,8 +615,8 @@ | ||
it('should remove the lockfile', function (next) { | ||
lockfile.lock(tmpFile, function (err) { | ||
it('should remove the lockfile', (next) => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
lockfile.unlock(tmpFile, function (err) { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -649,4 +630,4 @@ expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
it('should fail if removing the lockfile errors out', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should fail if removing the lockfile errors out', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -657,6 +638,6 @@ customFs.rmdir = function (path, callback) { | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.unlock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.unlock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -670,5 +651,5 @@ expect(err.message).to.be('foo'); | ||
it('should ignore ENOENT errors when removing the lockfile', function (next) { | ||
var customFs = extend({}, fs); | ||
var called; | ||
it('should ignore ENOENT errors when removing the lockfile', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
let called; | ||
@@ -681,6 +662,6 @@ customFs.rmdir = function (path, callback) { | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.unlock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.unlock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -694,9 +675,7 @@ expect(called).to.be(true); | ||
it('should stop updating the lockfile mtime', function (next) { | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, { update: 2000 }, function (err) { | ||
it('should stop updating the lockfile mtime', (next) => { | ||
lockfile.lock(tmpFile, { update: 2000 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.unlock(tmpFile, function (err) { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -710,4 +689,4 @@ | ||
it('should stop updating the lockfile mtime (slow fs)', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should stop updating the lockfile mtime (slow fs)', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -718,9 +697,7 @@ customFs.utimes = function (path, atime, mtime, callback) { | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, { fs: customFs, update: 2000 }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs, update: 2000 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
setTimeout(function () { | ||
lockfile.unlock(tmpFile, function (err) { | ||
setTimeout(() => { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -734,4 +711,4 @@ }); | ||
it('should stop updating the lockfile mtime (slow fs + new lock)', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should stop updating the lockfile mtime (slow fs + new lock)', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -742,12 +719,10 @@ customFs.utimes = function (path, atime, mtime, callback) { | ||
this.timeout(10000); | ||
lockfile.lock(tmpFile, { fs: customFs, update: 2000 }, function (err) { | ||
lockfile.lock(tmpFile, { fs: customFs, update: 2000 }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
setTimeout(function () { | ||
lockfile.unlock(tmpFile, function (err) { | ||
setTimeout(() => { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -762,10 +737,10 @@ }); | ||
it('should resolve to a canonical path', function (next) { | ||
it('should resolve to a canonical path', (next) => { | ||
// Create a symlink to the tmp file | ||
fs.symlinkSync(tmpFileRealPath, tmpFileSymlinkRealPath); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.unlock(tmpFile, function (err) { | ||
lockfile.unlock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -779,4 +754,4 @@ expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
it('should use the custom fs', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should use the custom fs', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -788,3 +763,3 @@ customFs.realpath = function (path, callback) { | ||
lockfile.unlock(tmpFile, { fs: customFs }, function (err) { | ||
lockfile.unlock(tmpFile, { fs: customFs }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -798,4 +773,4 @@ expect(err.message).to.be('foo'); | ||
describe('.check()', function () { | ||
beforeEach(function () { | ||
describe('.check()', () => { | ||
beforeEach(() => { | ||
fs.writeFileSync(tmpFile, ''); | ||
@@ -807,6 +782,4 @@ rimraf.sync(tmpFileSymlink); | ||
this.timeout(5000); | ||
it('should fail if the file does not exist by default', function (next) { | ||
lockfile.check(tmpNonExistentFile, function (err) { | ||
it('should fail if the file does not exist by default', (next) => { | ||
lockfile.check(tmpNonExistentFile, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -819,4 +792,4 @@ expect(err.code).to.be('ENOENT'); | ||
it('should not fail if the file does not exist and realpath is false', function (next) { | ||
lockfile.check(tmpNonExistentFile, { realpath: false }, function (err) { | ||
it('should not fail if the file does not exist and realpath is false', (next) => { | ||
lockfile.check(tmpNonExistentFile, { realpath: false }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -828,7 +801,7 @@ | ||
it('should callback with true if file is locked', function (next) { | ||
lockfile.lock(tmpFile, function (err) { | ||
it('should callback with true if file is locked', (next) => { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.check(tmpFile, function (err, locked) { | ||
lockfile.check(tmpFile, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -841,4 +814,4 @@ expect(locked).to.be(true); | ||
it('should callback with false if file is not locked', function (next) { | ||
lockfile.check(tmpFile, function (err, locked) { | ||
it('should callback with false if file is not locked', (next) => { | ||
lockfile.check(tmpFile, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -850,4 +823,4 @@ expect(locked).to.be(false); | ||
it('should use the custom fs', function (next) { | ||
var customFs = extend({}, fs); | ||
it('should use the custom fs', (next) => { | ||
const customFs = Object.assign({}, fs); | ||
@@ -859,3 +832,3 @@ customFs.realpath = function (path, callback) { | ||
lockfile.check(tmpFile, { fs: customFs }, function (err, locked) { | ||
lockfile.check(tmpFile, { fs: customFs }, (err, locked) => { | ||
expect(err).to.be.an(Error); | ||
@@ -868,14 +841,14 @@ expect(locked).to.be(undefined); | ||
it('should resolve symlinks by default', function (next) { | ||
it('should resolve symlinks by default', (next) => { | ||
// Create a symlink to the tmp file | ||
fs.symlinkSync(tmpFileRealPath, tmpFileSymlinkRealPath); | ||
lockfile.lock(tmpFileSymlink, function (err) { | ||
lockfile.lock(tmpFileSymlink, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.check(tmpFile, function (err, locked) { | ||
lockfile.check(tmpFile, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
expect(locked).to.be(true); | ||
lockfile.check(tmpFile + '/../../test/tmp', function (err, locked) { | ||
lockfile.check(`${tmpFile}/../../test/tmp`, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -889,14 +862,14 @@ expect(locked).to.be(true); | ||
it('should not resolve symlinks if realpath is false', function (next) { | ||
it('should not resolve symlinks if realpath is false', (next) => { | ||
// Create a symlink to the tmp file | ||
fs.symlinkSync(tmpFileRealPath, tmpFileSymlinkRealPath); | ||
lockfile.lock(tmpFileSymlink, { realpath: false }, function (err) { | ||
lockfile.lock(tmpFileSymlink, { realpath: false }, (err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.check(tmpFile, { realpath: false }, function (err, locked) { | ||
lockfile.check(tmpFile, { realpath: false }, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
expect(locked).to.be(false); | ||
lockfile.check(tmpFile + '/../../test/tmp', { realpath: false }, function (err, locked) { | ||
lockfile.check(`${tmpFile}/../../test/tmp`, { realpath: false }, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -911,5 +884,5 @@ expect(locked).to.be(false); | ||
it('should fail if stating the lockfile errors out when verifying staleness', function (next) { | ||
var mtime = (Date.now() - 60000) / 1000; | ||
var customFs = extend({}, fs); | ||
it('should fail if stating the lockfile errors out when verifying staleness', (next) => { | ||
const mtime = (Date.now() - 60000) / 1000; | ||
const customFs = Object.assign({}, fs); | ||
@@ -923,3 +896,3 @@ customFs.stat = function (path, callback) { | ||
lockfile.check(tmpFile, { fs: customFs }, function (err, locked) { | ||
lockfile.check(tmpFile, { fs: customFs }, (err, locked) => { | ||
expect(err).to.be.an(Error); | ||
@@ -933,7 +906,7 @@ expect(err.message).to.be('foo'); | ||
it('should set stale to a minimum of 2000', function (next) { | ||
it('should set stale to a minimum of 2000', (next) => { | ||
fs.mkdirSync(tmpFileLock); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: 2000 }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: 2000 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -944,4 +917,4 @@ expect(err.code).to.be('ELOCKED'); | ||
setTimeout(function () { | ||
lockfile.check(tmpFile, { stale: 100 }, function (err, locked) { | ||
setTimeout(() => { | ||
lockfile.check(tmpFile, { stale: 100 }, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -955,7 +928,7 @@ expect(locked).to.equal(false); | ||
it('should set stale to a minimum of 2000 (falsy)', function (next) { | ||
it('should set stale to a minimum of 2000 (falsy)', (next) => { | ||
fs.mkdirSync(tmpFileLock); | ||
setTimeout(function () { | ||
lockfile.lock(tmpFile, { stale: 2000 }, function (err) { | ||
setTimeout(() => { | ||
lockfile.lock(tmpFile, { stale: 2000 }, (err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -966,4 +939,4 @@ expect(err.code).to.be('ELOCKED'); | ||
setTimeout(function () { | ||
lockfile.check(tmpFile, { stale: false }, function (err, locked) { | ||
setTimeout(() => { | ||
lockfile.check(tmpFile, { stale: false }, (err, locked) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -978,4 +951,4 @@ expect(locked).to.equal(false); | ||
describe('release()', function () { | ||
beforeEach(function () { | ||
describe('release()', () => { | ||
beforeEach(() => { | ||
fs.writeFileSync(tmpFile, ''); | ||
@@ -986,12 +959,10 @@ }); | ||
this.timeout(5000); | ||
it('should release the lock after calling the provided release function', function (next) { | ||
lockfile.lock(tmpFile, function (err, release) { | ||
it('should release the lock after calling the provided release function', (next) => { | ||
lockfile.lock(tmpFile, (err, release) => { | ||
expect(err).to.not.be.ok(); | ||
release(function (err) { | ||
release((err) => { | ||
expect(err).to.not.be.ok(); | ||
lockfile.lock(tmpFile, function (err) { | ||
lockfile.lock(tmpFile, (err) => { | ||
expect(err).to.not.be.ok(); | ||
@@ -1005,10 +976,10 @@ | ||
it('should fail when releasing twice', function (next) { | ||
lockfile.lock(tmpFile, function (err, release) { | ||
it('should fail when releasing twice', (next) => { | ||
lockfile.lock(tmpFile, (err, release) => { | ||
expect(err).to.not.be.ok(); | ||
release(function (err) { | ||
release((err) => { | ||
expect(err).to.not.be.ok(); | ||
release(function (err) { | ||
release((err) => { | ||
expect(err).to.be.an(Error); | ||
@@ -1024,4 +995,4 @@ expect(err.code).to.be('ERELEASED'); | ||
describe('sync api', function () { | ||
beforeEach(function () { | ||
describe('sync api', () => { | ||
beforeEach(() => { | ||
fs.writeFileSync(tmpFile, ''); | ||
@@ -1033,4 +1004,4 @@ rimraf.sync(tmpFileSymlink); | ||
it('should expose a working lockSync', function () { | ||
var release; | ||
it('should expose a working lockSync', () => { | ||
let release; | ||
@@ -1046,3 +1017,3 @@ // Test success | ||
// Test compromise being passed and no options | ||
release = lockfile.lockSync(tmpFile, function () {}); | ||
release = lockfile.lockSync(tmpFile, () => {}); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
@@ -1059,3 +1030,3 @@ release(); | ||
// Test both options and compromised being passed | ||
release = lockfile.lockSync(tmpFile, {}, function () {}); | ||
release = lockfile.lockSync(tmpFile, {}, () => {}); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
@@ -1067,3 +1038,3 @@ release(); | ||
lockfile.lockSync(tmpFile); | ||
expect(function () { | ||
expect(() => { | ||
lockfile.lockSync(tmpFile); | ||
@@ -1073,13 +1044,13 @@ }).to.throwException(/already being hold/); | ||
it('should not allow retries to be passed', function () { | ||
expect(function () { | ||
it('should not allow retries to be passed', () => { | ||
expect(() => { | ||
lockfile.lockSync(tmpFile, { retries: 10 }); | ||
}).to.throwException(/Cannot use retries/i); | ||
expect(function () { | ||
expect(() => { | ||
lockfile.lockSync(tmpFile, { retries: { retries: 10 } }); | ||
}).to.throwException(/Cannot use retries/i); | ||
expect(function () { | ||
var release = lockfile.lockSync(tmpFile, { retries: 0 }); | ||
expect(() => { | ||
const release = lockfile.lockSync(tmpFile, { retries: 0 }); | ||
@@ -1089,4 +1060,4 @@ release(); | ||
expect(function () { | ||
var release = lockfile.lockSync(tmpFile, { retries: { retries: 0 } }); | ||
expect(() => { | ||
const release = lockfile.lockSync(tmpFile, { retries: { retries: 0 } }); | ||
@@ -1097,3 +1068,3 @@ release(); | ||
it('should expose a working unlockSync', function () { | ||
it('should expose a working unlockSync', () => { | ||
// Test success | ||
@@ -1107,3 +1078,3 @@ lockfile.lockSync(tmpFile); | ||
// Test fail | ||
expect(function () { | ||
expect(() => { | ||
lockfile.unlockSync(tmpFile); | ||
@@ -1113,46 +1084,6 @@ }).to.throwException(/not acquired\/owned by you/); | ||
it('should update the lockfile mtime automatically', function (next) { | ||
var mtime; | ||
it('should expose a working checkSync', () => { | ||
let release; | ||
let locked; | ||
this.timeout(5000); | ||
lockfile.lockSync(tmpFile, { update: 1000 }); | ||
mtime = fs.statSync(tmpFileLock).mtime; | ||
// First update occurs at 1000ms | ||
setTimeout(function () { | ||
var stat = fs.statSync(tmpFileLock); | ||
expect(stat.mtime.getTime()).to.be.greaterThan(mtime.getTime()); | ||
mtime = stat.mtime; | ||
}, 1500); | ||
// Second update occurs at 2000ms | ||
setTimeout(function () { | ||
var stat = fs.statSync(tmpFileLock); | ||
expect(stat.mtime.getTime()).to.be.greaterThan(mtime.getTime()); | ||
mtime = stat.mtime; | ||
next(); | ||
}, 2500); | ||
}); | ||
it('should use a custom fs', function () { | ||
var customFs = extend({}, fs); | ||
var called; | ||
customFs.realpathSync = function () { | ||
called = true; | ||
return fs.realpathSync.apply(fs, arguments); | ||
}; | ||
lockfile.lockSync(tmpFile, { fs: customFs }); | ||
expect(called).to.be(true); | ||
}); | ||
it('should expose a working checkSync', function () { | ||
var release; | ||
var locked; | ||
// Test success unlocked | ||
@@ -1191,17 +1122,55 @@ locked = lockfile.checkSync(tmpFile); | ||
// Test fail with non-existent file | ||
expect(function () { | ||
expect(() => { | ||
lockfile.checkSync('nonexistentdir/nonexistentfile'); | ||
}).to.throwException(/ENOENT/); | ||
}); | ||
it('should update the lockfile mtime automatically', (next) => { | ||
let mtime; | ||
lockfile.lockSync(tmpFile, { update: 1000 }); | ||
mtime = fs.statSync(tmpFileLock).mtime; | ||
// First update occurs at 1000ms | ||
setTimeout(() => { | ||
const stat = fs.statSync(tmpFileLock); | ||
expect(stat.mtime.getTime()).to.be.greaterThan(mtime.getTime()); | ||
mtime = stat.mtime; | ||
}, 1500); | ||
// Second update occurs at 2000ms | ||
setTimeout(() => { | ||
const stat = fs.statSync(tmpFileLock); | ||
expect(stat.mtime.getTime()).to.be.greaterThan(mtime.getTime()); | ||
mtime = stat.mtime; | ||
next(); | ||
}, 2500); | ||
}); | ||
it('should use a custom fs', () => { | ||
const customFs = Object.assign({}, fs); | ||
let called; | ||
customFs.realpathSync = function () { | ||
called = true; | ||
return fs.realpathSync.apply(fs, arguments); | ||
}; | ||
lockfile.lockSync(tmpFile, { fs: customFs }); | ||
expect(called).to.be(true); | ||
}); | ||
}); | ||
describe('misc', function () { | ||
describe('misc', () => { | ||
afterEach(clearLocks); | ||
it('should not contain suspicious nodejs native fs calls', function () { | ||
it('should not contain suspicious nodejs native fs calls', () => { | ||
expect(/\s{2,}fs\.[a-z]+/i.test(lockfileContents)).to.be(false); | ||
}); | ||
it('should remove open locks if the process crashes', function (next) { | ||
cp.exec('node ' + __dirname + '/fixtures/crash.js', function (err, stdout, stderr) { | ||
it('should remove open locks if the process crashes', (next) => { | ||
cp.exec(`node ${__dirname}/fixtures/crash.js`, (err, stdout, stderr) => { | ||
if (!err) { | ||
@@ -1222,4 +1191,4 @@ return next(new Error('Should have failed')); | ||
it('should not hold the process if it has no more work to do', function (next) { | ||
spawn('node', [__dirname + '/fixtures/unref.js'], next); | ||
it('should not hold the process if it has no more work to do', (next) => { | ||
spawn('node', [`${__dirname}/fixtures/unref.js`], next); | ||
}); | ||
@@ -1230,3 +1199,3 @@ | ||
spawn('node', [__dirname + '/fixtures/stress.js'], function (err, stdout) { | ||
spawn('node', [`${__dirname}/fixtures/stress.js`], (err, stdout) => { | ||
if (err) { | ||
@@ -1236,3 +1205,3 @@ if (process.env.TRAVIS) { | ||
} else { | ||
fs.writeFileSync(__dirname + '/stress.log', stdout); | ||
fs.writeFileSync(`${__dirname}/stress.log`, stdout || ''); | ||
} | ||
@@ -1239,0 +1208,0 @@ |
Sorry, the diff of this file is not supported yet
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
2
15
211
60301
1332
- Removederr-code@^1.0.0
- Removedextend@^3.0.0
- Removederr-code@1.1.2(transitive)
- Removedextend@3.0.2(transitive)