This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
boot.js
111 lines (98 loc) · 2.99 KB
/
boot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'
const waterfall = require('async/waterfall')
const series = require('async/series')
const extend = require('deep-extend')
// Boot an IPFS node depending on the options set
module.exports = (self) => {
self.log('booting')
const options = self._options
const doInit = options.init
const doStart = options.start
const config = options.config
const setConfig = config && typeof config === 'object'
const repoOpen = !self._repo.closed
const customInitOptions = typeof options.init === 'object' ? options.init : {}
const initOptions = Object.assign({ bits: 2048, pass: self._options.pass }, customInitOptions)
// Checks if a repo exists, and if so opens it
// Will return callback with a bool indicating the existence
// of the repo
const maybeOpenRepo = (cb) => {
// nothing to do
if (repoOpen) {
return cb(null, true)
}
series([
(cb) => self._repo.open(cb),
(cb) => self.preStart(cb),
(cb) => {
self.log('initialized')
self.state.initialized()
cb(null, true)
}
], (err, res) => {
if (err) {
// If the error is that no repo exists,
// which happens when the version file is not found
// we just want to signal that no repo exist, not
// fail the whole process.
// TODO: improve datastore and ipfs-repo implemenations so this error is a bit more unified
if (err.message.match(/not found/) || // indexeddb
err.message.match(/ENOENT/) || // fs
err.message.match(/No value/) // memory
) {
return cb(null, false)
}
return cb(err)
}
cb(null, res)
})
}
const done = (err) => {
if (err) {
return self.emit('error', err)
}
self.log('boot:done')
self.emit('ready')
}
const tasks = []
// check if there as a repo and if so open it
maybeOpenRepo((err, hasRepo) => {
if (err) {
return done(err)
}
// No repo, but need should init one
if (doInit && !hasRepo) {
tasks.push((cb) => self.init(initOptions, cb))
// we know we will have a repo for all follwing tasks
// if the above succeeds
hasRepo = true
}
// Need to set config
if (setConfig) {
if (!hasRepo) {
console.log('WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"')
} else {
tasks.push((cb) => {
waterfall([
(cb) => self.config.get(cb),
(config, cb) => {
extend(config, options.config)
self.config.replace(config, cb)
}
], cb)
})
}
}
// Need to start up the node
if (doStart) {
if (!hasRepo) {
console.log('WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"')
return done(new Error('Uninitalized repo'))
} else {
tasks.push((cb) => self.start(cb))
}
}
// Do the actual boot sequence
series(tasks, done)
})
}