proper-lockfile
Advanced tools
Comparing version 0.6.1 to 1.0.0
43
index.js
@@ -8,2 +8,3 @@ 'use strict'; | ||
var retry = require('retry'); | ||
var syncFs = require('./lib/syncFs'); | ||
@@ -250,2 +251,42 @@ var locks = {}; | ||
function lockSync(file, options, compromised) { | ||
var err, | ||
release; | ||
if (typeof options === 'function') { | ||
compromised = options; | ||
options = null; | ||
} | ||
options = options || {}; | ||
options.fs = syncFs(options.fs || fs); | ||
lock(file, options, compromised, function (_err, _release) { | ||
err = _err; | ||
release = _release; | ||
}); | ||
if (err) { | ||
throw err; | ||
} | ||
return release; | ||
} | ||
function unlockSync(file, options) { | ||
var err; | ||
options = options || {}; | ||
options.fs = syncFs(options.fs || fs); | ||
unlock(file, options, function (_err) { | ||
err = _err; | ||
}); | ||
if (err) { | ||
throw err; | ||
} | ||
} | ||
// Remove acquired locks on exit | ||
@@ -262,1 +303,3 @@ /* istanbul ignore next */ | ||
module.exports.unlock = unlock; | ||
module.exports.lockSync = lockSync; | ||
module.exports.unlockSync = unlockSync; |
{ | ||
"name": "proper-lockfile", | ||
"version": "0.6.1", | ||
"version": "1.0.0", | ||
"description": "A inter-process and inter-machine lockfile utility that works on a local or network file system.", | ||
@@ -39,3 +39,3 @@ "main": "index.js", | ||
"async": "^0.9.0", | ||
"buffered-spawn": "^0.2.2", | ||
"buffered-spawn": "^1.1.0", | ||
"expect.js": "^0.3.1", | ||
@@ -42,0 +42,0 @@ "istanbul": "^0.3.0", |
@@ -124,2 +124,14 @@ # proper-lockfile [![Build Status](https://travis-ci.org/IndigoUnited/node-proper-lockfile.svg?branch=master)](https://travis-ci.org/IndigoUnited/node-proper-lockfile) [![Coverage Status](https://coveralls.io/repos/IndigoUnited/node-proper-lockfile/badge.png?branch=master)](https://coveralls.io/r/IndigoUnited/node-proper-lockfile?branch=master) | ||
### .lockSync(file, [options], [compromised]) | ||
Sync version of `.lock()`. | ||
Returns the `release` function or throws on err; | ||
### .unlockSync(file, [options]) | ||
Sync version of `.unlock()`. | ||
Throws on err; | ||
## Tests | ||
@@ -126,0 +138,0 @@ |
@@ -38,2 +38,4 @@ 'use strict'; | ||
setTimeout(function () { | ||
cluster.removeAllListeners('exit'); | ||
Object.keys(cluster.workers).forEach(function (id) { | ||
@@ -40,0 +42,0 @@ cluster.workers[id].removeAllListeners('message').kill(); |
@@ -579,3 +579,2 @@ 'use strict'; | ||
it('should remove the lockfile', function (next) { | ||
@@ -775,2 +774,99 @@ lockfile.lock(tmpFile, function (err) { | ||
describe('sync api', function () { | ||
beforeEach(function () { | ||
fs.writeFileSync(tmpFile, ''); | ||
rimraf.sync(tmpFileSymlink); | ||
}); | ||
afterEach(clearLocks); | ||
it('should expose a working lockSync', function () { | ||
var release; | ||
// Test success | ||
release = lockfile.lockSync(tmpFile); | ||
expect(release).to.be.a('function'); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
release(); | ||
expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
// Test compromise being passed and no options | ||
release = lockfile.lockSync(tmpFile, function () {}); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
release(); | ||
expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
// Test options being passed and no compromised | ||
release = lockfile.lockSync(tmpFile, {}); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
release(); | ||
expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
// Test both options and compromised being passed | ||
release = lockfile.lockSync(tmpFile, {}, function () {}); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
release(); | ||
expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
// Test fail | ||
lockfile.lockSync(tmpFile); | ||
expect(function () { | ||
lockfile.lockSync(tmpFile); | ||
}).to.throwException(/already being hold/); | ||
}); | ||
it('should expose a working unlockSync', function () { | ||
// Test success | ||
lockfile.lockSync(tmpFile); | ||
expect(fs.existsSync(tmpFileLock)).to.be(true); | ||
lockfile.unlockSync(tmpFile); | ||
expect(fs.existsSync(tmpFileLock)).to.be(false); | ||
// Test fail | ||
expect(function () { | ||
lockfile.unlockSync(tmpFile); | ||
}).to.throwException(/not acquired\/owned by you/); | ||
}); | ||
it('should update the lockfile mtime automatically', function (next) { | ||
var mtime; | ||
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), | ||
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 () { | ||
@@ -777,0 +873,0 @@ afterEach(clearLocks); |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51036
14
1091
1
146