Skip to content

Commit

Permalink
fix: fix lock for node 11 (#181)
Browse files Browse the repository at this point in the history
* fix: fix lock for node 11

* fix: remove unused _isLocked method

* chore: fix lint error
  • Loading branch information
hugomrdias authored and jacobheun committed Nov 19, 2018
1 parent 9fac46d commit bec2a5d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
14 changes: 0 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
42 changes: 9 additions & 33 deletions src/lock.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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))
}
4 changes: 2 additions & 2 deletions test/lock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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
Expand Down
47 changes: 26 additions & 21 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Empty file removed test/test-repo/repo.lock
Empty file.

0 comments on commit bec2a5d

Please sign in to comment.