Skip to content

Commit e028a9e

Browse files
committed
fix: fix lock for node 11
1 parent 9fac46d commit e028a9e

File tree

5 files changed

+45
-38
lines changed

5 files changed

+45
-38
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/lock.js

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

3-
const Lock = require('lock-me')
43
const path = require('path')
54
const debug = require('debug')
65
const fs = require('fs')
6+
const { lock, check } = require('proper-lockfile')
77

88
const log = debug('repo:lock')
99

1010
const lockFile = 'repo.lock'
11-
const lock = new Lock()
1211

1312
/**
1413
* Lock the repo in the given dir.
@@ -20,7 +19,16 @@ const lock = new Lock()
2019
exports.lock = (dir, callback) => {
2120
const file = path.join(dir, lockFile)
2221
log('locking %s', file)
23-
lock(file, callback)
22+
23+
lock(dir, {lockfilePath: file})
24+
.then(release => {
25+
callback(null, {close: (cb) => {
26+
release()
27+
.then(() => cb())
28+
.catch(err => cb(err))
29+
}})
30+
})
31+
.catch(err => callback(err))
2432
}
2533

2634
/**
@@ -38,18 +46,12 @@ exports.locked = (dir, callback) => {
3846
log('file does not exist: %s', file)
3947
}
4048

41-
lock(file, (err, lck) => {
42-
if (err) {
43-
log('already locked: %s', err.message)
44-
return callback(null, true)
45-
}
46-
47-
log('no one has a lock')
48-
lck.close((err) => {
49-
if (err) {
50-
return callback(err)
49+
check(dir, { lockfilePath: file })
50+
.then(islocked => {
51+
if (islocked) {
52+
return callback(null, true)
5153
}
5254
callback(null, false)
5355
})
54-
})
56+
.catch(err => callback(err))
5557
}

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)