Skip to content

Commit 9058d97

Browse files
committed
feat(lock): allow for custom lock and no lock options
fix: failing tests
1 parent 07d2630 commit 9058d97

File tree

5 files changed

+115
-8
lines changed

5 files changed

+115
-8
lines changed

src/default-options-browser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ module.exports = {
2525
datastore: {
2626
db: require('level-js')
2727
}
28-
}
28+
},
29+
locker: null
2930
}

src/default-options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ module.exports = {
1919
},
2020
keys: {
2121
}
22-
}
22+
},
23+
locker: null
2324
}

src/index.js

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ class IpfsRepo {
4444
this.closed = true
4545
this.path = repoPath
4646

47-
this._locker = lockers[this.options.lock]
48-
assert(this._locker, 'Unknown lock type: ' + this.options.lock)
47+
this._locker = this._getLocker()
4948

5049
this.root = backends.create('root', this.path, this.options)
5150
this.version = version(this.root)
@@ -88,7 +87,7 @@ class IpfsRepo {
8887
waterfall([
8988
(cb) => this.root.open(ignoringAlreadyOpened(cb)),
9089
(cb) => this._isInitialized(cb),
91-
(cb) => this._locker.lock(this.path, cb),
90+
(cb) => this._openLock(this.path, cb),
9291
(lck, cb) => {
9392
log('aquired repo.lock')
9493
this.lockfile = lck
@@ -121,7 +120,7 @@ class IpfsRepo {
121120
}
122121
], (err) => {
123122
if (err && this.lockfile) {
124-
this.lockfile.close((err2) => {
123+
this._closeLock((err2) => {
125124
if (!err2) {
126125
this.lockfile = null
127126
} else {
@@ -135,6 +134,65 @@ class IpfsRepo {
135134
})
136135
}
137136

137+
/**
138+
* Returns the repo locker to be used. Null will be returned if no locker is requested
139+
*
140+
* @private
141+
* @returns {Locker}
142+
*/
143+
_getLocker () {
144+
if (this.options.locker === false) {
145+
return null
146+
} else if (this.options.locker) {
147+
return this.options.locker
148+
}
149+
150+
assert(lockers[this.options.lock], 'Unknown lock type: ' + this.options.lock)
151+
return lockers[this.options.lock]
152+
}
153+
154+
/**
155+
* Creates a lock on the repo if a locker is specified. The lockfile object will
156+
* be returned in the callback if one has been created.
157+
*
158+
* @param {string} path
159+
* @param {function(Error, lockfile)} callback
160+
* @returns {void}
161+
*/
162+
_openLock (path, callback) {
163+
if (this._locker) {
164+
return this._locker.lock(path, callback)
165+
}
166+
callback(null, null)
167+
}
168+
169+
/**
170+
* Closes the lock on the repo
171+
*
172+
* @param {function(Error)} callback
173+
* @returns {void}
174+
*/
175+
_closeLock (callback) {
176+
if (this.lockfile) {
177+
return this.lockfile.close(callback)
178+
}
179+
callback()
180+
}
181+
182+
/**
183+
* Gets the status of the lock on the repo
184+
*
185+
* @param {string} path
186+
* @param {function(Error, boolean)} callback
187+
* @returns {void}
188+
*/
189+
_isLocked (path, callback) {
190+
if (this._locker) {
191+
return this._locker.locked(path, callback)
192+
}
193+
callback(null, false)
194+
}
195+
138196
/**
139197
* Check if the repo is already initialized.
140198
*
@@ -186,7 +244,7 @@ class IpfsRepo {
186244
(cb) => {
187245
log('unlocking')
188246
this.closed = true
189-
this.lockfile.close(cb)
247+
this._closeLock(cb)
190248
},
191249
(cb) => {
192250
this.lockfile = null

test/node.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ const IPFSRepo = require('../src')
1313
describe('IPFS Repo Tests onNode.js', () => {
1414
require('./options-test')
1515

16+
const customLocker = {
17+
isLocked: false,
18+
lock: (path, callback) => {
19+
this.isLocked = true
20+
callback(null, {
21+
close: (cb) => {
22+
this.locked = false
23+
cb(null)
24+
}
25+
})
26+
},
27+
locked: (callback) => {
28+
callback(null, this.isLocked)
29+
}
30+
}
31+
1632
const repos = [{
1733
name: 'default inited',
1834
opts: undefined,
@@ -25,6 +41,16 @@ describe('IPFS Repo Tests onNode.js', () => {
2541
lock: 'memory'
2642
},
2743
init: true
44+
}, {
45+
name: 'custom locker',
46+
opts: {
47+
locker: customLocker
48+
}
49+
}, {
50+
name: 'no locker',
51+
opts: {
52+
locker: false
53+
}
2854
}, {
2955
name: 'default existing',
3056
opts: undefined,

test/options-test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ describe('custom options tests', () => {
2828
const repo = new Repo(repoPath)
2929
expect(repo.options).to.deep.equal(expectedRepoOptions())
3030
})
31+
32+
it('allows for no locker', () => {
33+
const repo = new Repo(repoPath, {
34+
locker: false
35+
})
36+
expect(repo._locker).to.equal(null)
37+
})
38+
39+
it('allows for a custom locker', () => {
40+
const locker = {
41+
lock: (path, callback) => { },
42+
locked: (path, callback) => { }
43+
}
44+
45+
const repo = new Repo(repoPath, {
46+
locker
47+
})
48+
49+
expect(repo._getLocker()).to.deep.equal(locker)
50+
})
3151
})
3252

3353
function noop () {}
@@ -52,7 +72,8 @@ function expectedRepoOptions () {
5272
sharding: true,
5373
extension: '.data'
5474
}
55-
}
75+
},
76+
locker: null
5677
}
5778

5879
if (process.browser) {

0 commit comments

Comments
 (0)