Skip to content

Commit bec2a5d

Browse files
hugomrdiasjacobheun
authored andcommitted
fix: fix lock for node 11 (#181)
* fix: fix lock for node 11 * fix: remove unused _isLocked method * chore: fix lint error
1 parent 9fac46d commit bec2a5d

File tree

6 files changed

+38
-71
lines changed

6 files changed

+38
-71
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
"debug": "^4.1.0",
6060
"interface-datastore": "~0.6.0",
6161
"ipfs-block": "~0.7.1",
62-
"lock-me": "^1.0.4",
6362
"lodash.get": "^4.4.2",
6463
"lodash.has": "^4.5.2",
6564
"lodash.set": "^4.3.2",
6665
"multiaddr": "^5.0.0",
66+
"proper-lockfile": "^3.2.0",
6767
"pull-stream": "^3.6.9",
6868
"sort-keys": "^2.0.0"
6969
},

src/index.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,6 @@ class IpfsRepo {
188188
callback()
189189
}
190190

191-
/**
192-
* Gets the status of the lock on the repo
193-
*
194-
* @param {string} path
195-
* @param {function(Error, boolean)} callback
196-
* @returns {void}
197-
*/
198-
_isLocked (path, callback) {
199-
if (this._locker) {
200-
return this._locker.locked(path, callback)
201-
}
202-
callback(null, false)
203-
}
204-
205191
/**
206192
* Check if the repo is already initialized.
207193
*

src/lock.js

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
'use strict'
22

3-
const Lock = require('lock-me')
43
const path = require('path')
54
const debug = require('debug')
6-
const fs = require('fs')
5+
const { lock } = require('proper-lockfile')
76

87
const log = debug('repo:lock')
98

109
const lockFile = 'repo.lock'
11-
const lock = new Lock()
1210

1311
/**
1412
* Lock the repo in the given dir.
@@ -20,36 +18,14 @@ const lock = new Lock()
2018
exports.lock = (dir, callback) => {
2119
const file = path.join(dir, lockFile)
2220
log('locking %s', file)
23-
lock(file, callback)
24-
}
25-
26-
/**
27-
* Check if the repo in the given directory is locked.
28-
*
29-
* @param {string} dir
30-
* @param {function(Error, bool)} callback
31-
* @returns {void}
32-
*/
33-
exports.locked = (dir, callback) => {
34-
const file = path.join(dir, lockFile)
35-
log('checking lock: %s')
36-
37-
if (!fs.existsSync(file)) {
38-
log('file does not exist: %s', file)
39-
}
40-
41-
lock(file, (err, lck) => {
42-
if (err) {
43-
log('already locked: %s', err.message)
44-
return callback(null, true)
45-
}
4621

47-
log('no one has a lock')
48-
lck.close((err) => {
49-
if (err) {
50-
return callback(err)
51-
}
52-
callback(null, false)
22+
lock(dir, {lockfilePath: file})
23+
.then(release => {
24+
callback(null, {close: (cb) => {
25+
release()
26+
.then(() => cb())
27+
.catch(err => cb(err))
28+
}})
5329
})
54-
})
30+
.catch(err => callback(err))
5531
}

test/lock-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = (repo) => {
3232
const mochaExceptionHandler = process.listeners('uncaughtException').pop()
3333
process.removeListener('uncaughtException', mochaExceptionHandler)
3434
process.once('uncaughtException', function (err) {
35-
expect(err.message).to.match(/already held|IO error/)
35+
expect(err.message).to.match(/already held|IO error|already being hold/)
3636
})
3737

3838
series([
@@ -49,7 +49,7 @@ module.exports = (repo) => {
4949
], function (err) {
5050
// There will be no listeners if the uncaughtException was triggered
5151
if (process.listeners('uncaughtException').length > 0) {
52-
expect(err.message).to.match(/already locked|already held|ENOENT/)
52+
expect(err.message).to.match(/already locked|already held|already being hold|ELOCKED/)
5353
}
5454

5555
// Reset listeners to maintain test integrity

test/node.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,34 @@ describe('IPFS Repo Tests onNode.js', () => {
4343
}
4444
}
4545

46-
const repos = [{
47-
name: 'default inited',
48-
opts: undefined,
49-
init: true
50-
}, {
51-
name: 'memory',
52-
opts: {
53-
fs: require('interface-datastore').MemoryDatastore,
54-
level: require('memdown'),
55-
lock: 'memory'
46+
const repos = [
47+
{
48+
name: 'default inited',
49+
opts: undefined,
50+
init: true
5651
},
57-
init: true
58-
}, {
59-
name: 'custom locker',
60-
opts: {
61-
lock: customLock
52+
{
53+
name: 'memory',
54+
opts: {
55+
fs: require('interface-datastore').MemoryDatastore,
56+
level: require('memdown'),
57+
lock: 'memory'
58+
},
59+
init: true
6260
},
63-
init: true
64-
}, {
65-
name: 'default existing',
66-
opts: undefined,
67-
init: false
68-
}]
61+
{
62+
name: 'custom locker',
63+
opts: {
64+
lock: customLock
65+
},
66+
init: true
67+
},
68+
{
69+
name: 'default existing',
70+
opts: undefined,
71+
init: false
72+
}
73+
]
6974
repos.forEach((r) => describe(r.name, () => {
7075
const testRepoPath = path.join(__dirname, 'test-repo')
7176
const date = Date.now().toString()

test/test-repo/repo.lock

Whitespace-only changes.

0 commit comments

Comments
 (0)