diff --git a/package.json b/package.json index 29e901c9..6b6018b9 100644 --- a/package.json +++ b/package.json @@ -59,11 +59,11 @@ "debug": "^4.1.0", "interface-datastore": "~0.6.0", "ipfs-block": "~0.7.1", - "lock-me": "^1.0.4", "lodash.get": "^4.4.2", "lodash.has": "^4.5.2", "lodash.set": "^4.3.2", "multiaddr": "^5.0.0", + "proper-lockfile": "^3.2.0", "pull-stream": "^3.6.9", "sort-keys": "^2.0.0" }, diff --git a/src/index.js b/src/index.js index 21ef6c13..610506d1 100644 --- a/src/index.js +++ b/src/index.js @@ -188,20 +188,6 @@ class IpfsRepo { callback() } - /** - * Gets the status of the lock on the repo - * - * @param {string} path - * @param {function(Error, boolean)} callback - * @returns {void} - */ - _isLocked (path, callback) { - if (this._locker) { - return this._locker.locked(path, callback) - } - callback(null, false) - } - /** * Check if the repo is already initialized. * diff --git a/src/lock.js b/src/lock.js index dc6513fa..9f14a66d 100644 --- a/src/lock.js +++ b/src/lock.js @@ -1,14 +1,12 @@ 'use strict' -const Lock = require('lock-me') const path = require('path') const debug = require('debug') -const fs = require('fs') +const { lock } = require('proper-lockfile') const log = debug('repo:lock') const lockFile = 'repo.lock' -const lock = new Lock() /** * Lock the repo in the given dir. @@ -20,36 +18,14 @@ const lock = new Lock() exports.lock = (dir, callback) => { const file = path.join(dir, lockFile) log('locking %s', file) - lock(file, callback) -} - -/** - * Check if the repo in the given directory is locked. - * - * @param {string} dir - * @param {function(Error, bool)} callback - * @returns {void} - */ -exports.locked = (dir, callback) => { - const file = path.join(dir, lockFile) - log('checking lock: %s') - - if (!fs.existsSync(file)) { - log('file does not exist: %s', file) - } - - lock(file, (err, lck) => { - if (err) { - log('already locked: %s', err.message) - return callback(null, true) - } - log('no one has a lock') - lck.close((err) => { - if (err) { - return callback(err) - } - callback(null, false) + lock(dir, {lockfilePath: file}) + .then(release => { + callback(null, {close: (cb) => { + release() + .then(() => cb()) + .catch(err => cb(err)) + }}) }) - }) + .catch(err => callback(err)) } diff --git a/test/lock-test.js b/test/lock-test.js index 630c2642..84d3e485 100644 --- a/test/lock-test.js +++ b/test/lock-test.js @@ -32,7 +32,7 @@ module.exports = (repo) => { const mochaExceptionHandler = process.listeners('uncaughtException').pop() process.removeListener('uncaughtException', mochaExceptionHandler) process.once('uncaughtException', function (err) { - expect(err.message).to.match(/already held|IO error/) + expect(err.message).to.match(/already held|IO error|already being hold/) }) series([ @@ -49,7 +49,7 @@ module.exports = (repo) => { ], function (err) { // There will be no listeners if the uncaughtException was triggered if (process.listeners('uncaughtException').length > 0) { - expect(err.message).to.match(/already locked|already held|ENOENT/) + expect(err.message).to.match(/already locked|already held|already being hold|ELOCKED/) } // Reset listeners to maintain test integrity diff --git a/test/node.js b/test/node.js index 62451c1d..3f62f14b 100644 --- a/test/node.js +++ b/test/node.js @@ -43,29 +43,34 @@ describe('IPFS Repo Tests onNode.js', () => { } } - const repos = [{ - name: 'default inited', - opts: undefined, - init: true - }, { - name: 'memory', - opts: { - fs: require('interface-datastore').MemoryDatastore, - level: require('memdown'), - lock: 'memory' + const repos = [ + { + name: 'default inited', + opts: undefined, + init: true }, - init: true - }, { - name: 'custom locker', - opts: { - lock: customLock + { + name: 'memory', + opts: { + fs: require('interface-datastore').MemoryDatastore, + level: require('memdown'), + lock: 'memory' + }, + init: true }, - init: true - }, { - name: 'default existing', - opts: undefined, - init: false - }] + { + name: 'custom locker', + opts: { + lock: customLock + }, + init: true + }, + { + name: 'default existing', + opts: undefined, + init: false + } + ] repos.forEach((r) => describe(r.name, () => { const testRepoPath = path.join(__dirname, 'test-repo') const date = Date.now().toString() diff --git a/test/test-repo/repo.lock b/test/test-repo/repo.lock deleted file mode 100644 index e69de29b..00000000