Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 835e993

Browse files
victorbdaviddias
authored andcommitted
Loosen up init requirements: accept already initialised repos (#808)
* feat: only try to initialize if not init already
1 parent 2aaf7ed commit 835e993

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

src/core/boot.js

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,43 @@ module.exports = (self) => {
1515
const repoOpen = !self._repo.closed
1616

1717
const customInitOptions = typeof options.init === 'object' ? options.init : {}
18-
const initOptions = Object.assign({ bits: 2048 }, customInitOptions)
18+
const initOptions = Object.assign({
19+
bits: 2048
20+
}, customInitOptions)
1921

22+
// Checks if a repo exists, and if so opens it
23+
// Will return callback with a bool indicating the existence
24+
// of the repo
2025
const maybeOpenRepo = (cb) => {
21-
waterfall([
22-
(cb) => self._repo.exists(cb),
23-
(exists, cb) => {
24-
self.log('boot:maybeOpen', exists, repoOpen)
25-
if (exists && !repoOpen) {
26-
return series([
27-
(cb) => self._repo.open(cb),
28-
(cb) => self.preStart(cb),
29-
(cb) => {
30-
self.state.initialized()
31-
cb()
32-
}
33-
], cb)
26+
// nothing to do
27+
if (repoOpen) {
28+
return cb(null, true)
29+
}
30+
31+
series([
32+
(cb) => self._repo.open(cb),
33+
(cb) => self.preStart(cb),
34+
(cb) => {
35+
self.state.initialized()
36+
cb(null, true)
37+
}
38+
], (err, res) => {
39+
if (err) {
40+
// If the error is that no repo exists,
41+
// which happens when the version file is not found
42+
// we just want to signal that no repo exist, not
43+
// fail the whole process.
44+
// TODO: improve datastore and ipfs-repo implemenations so this error is a bit more unified
45+
if (err.message.match(/not found/) || // indexeddb
46+
err.message.match(/ENOENT/) || // fs
47+
err.message.match(/No value/) // memory
48+
) {
49+
return cb(null, false)
3450
}
35-
cb()
51+
return cb(err)
3652
}
37-
], cb)
53+
cb(null, res)
54+
})
3855
}
3956

4057
const done = (err) => {
@@ -47,27 +64,22 @@ module.exports = (self) => {
4764

4865
const tasks = []
4966

50-
if (doInit) {
51-
self.log('boot:doInit')
52-
tasks.push((cb) => self.init(initOptions, cb))
53-
next(null, true)
54-
} else if (!repoOpen) {
55-
self._repo.exists(next)
56-
}
57-
58-
function next (err, hasRepo) {
59-
self.log('boot:next')
67+
// check if there as a repo and if so open it
68+
maybeOpenRepo((err, hasRepo) => {
6069
if (err) {
6170
return done(err)
6271
}
6372

64-
if (hasRepo && !doInit) {
65-
self.log('boot:maybeopenreop')
66-
tasks.push(maybeOpenRepo)
73+
// No repo, but need should init one
74+
if (doInit && !hasRepo) {
75+
tasks.push((cb) => self.init(initOptions, cb))
76+
// we know we will have a repo for all follwing tasks
77+
// if the above succeeds
78+
hasRepo = true
6779
}
6880

81+
// Need to set config
6982
if (setConfig) {
70-
self.log('boot:setConfig')
7183
if (!hasRepo) {
7284
console.log('WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"')
7385
} else {
@@ -83,8 +95,8 @@ module.exports = (self) => {
8395
}
8496
}
8597

98+
// Need to start up the node
8699
if (doStart) {
87-
self.log('boot:doStart')
88100
if (!hasRepo) {
89101
console.log('WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"')
90102
return done(new Error('Uninitalized repo'))
@@ -93,6 +105,7 @@ module.exports = (self) => {
93105
}
94106
}
95107

108+
// Do the actual boot sequence
96109
series(tasks, done)
97-
}
110+
})
98111
}

test/core/create-node.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,24 @@ describe('create node', () => {
203203
(cb) => node.stop(cb)
204204
], done)
205205
})
206+
207+
it('can start node twice without crash', (done) => {
208+
const options = {
209+
repo: createTempRepo(),
210+
config: {
211+
Bootstrap: []
212+
}
213+
}
214+
let node = new IPFS(options)
215+
series([
216+
(cb) => node.once('start', cb),
217+
(cb) => node.stop(cb),
218+
(cb) => {
219+
node = new IPFS(options)
220+
node.on('error', cb)
221+
node.once('start', cb)
222+
},
223+
(cb) => node.stop(cb)
224+
], done)
225+
})
206226
})

0 commit comments

Comments
 (0)