1
1
'use strict'
2
2
3
3
const waterfall = require ( 'async/waterfall' )
4
- const series = require ( 'async/series' )
5
- const extend = require ( 'deep-extend' )
6
4
const RepoErrors = require ( 'ipfs-repo' ) . errors
7
5
8
6
// Boot an IPFS node depending on the options set
@@ -11,110 +9,79 @@ module.exports = (self) => {
11
9
const options = self . _options
12
10
const doInit = options . init
13
11
const doStart = options . start
14
- const config = options . config
15
- const setConfig = config && typeof config === 'object'
16
- const repoOpen = ! self . _repo . closed
17
12
18
- const customInitOptions = typeof options . init === 'object' ? options . init : { }
19
- const initOptions = Object . assign ( { bits : 2048 , pass : self . _options . pass } , customInitOptions )
20
-
21
- // Checks if a repo exists, and if so opens it
22
- // Will return callback with a bool indicating the existence
23
- // of the repo
24
- const maybeOpenRepo = ( cb ) => {
25
- // nothing to do
26
- if ( repoOpen ) {
27
- return cb ( null , true )
28
- }
13
+ // Do the actual boot sequence
14
+ waterfall ( [
15
+ // Checks if a repo exists, and if so opens it
16
+ // Will return callback with a bool indicating the existence
17
+ // of the repo
18
+ ( cb ) => {
19
+ // nothing to do
20
+ if ( ! self . _repo . closed ) {
21
+ return cb ( null , true )
22
+ }
29
23
30
- series ( [
31
- ( cb ) => self . _repo . open ( cb ) ,
32
- ( cb ) => self . pin . _load ( cb ) ,
33
- ( cb ) => self . preStart ( cb ) ,
34
- ( cb ) => {
35
- self . log ( 'initialized' )
36
- self . state . initialized ( )
24
+ self . _repo . open ( ( err , res ) => {
25
+ if ( isRepoUninitializedError ( err ) ) return cb ( null , false )
26
+ if ( err ) return cb ( err )
37
27
cb ( null , true )
28
+ } )
29
+ } ,
30
+ ( repoOpened , cb ) => {
31
+ // Init with existing initialized, opened, repo
32
+ if ( repoOpened ) {
33
+ return self . init ( { repo : self . _repo } , ( err ) => cb ( err ) )
38
34
}
39
- ] , ( err , res ) => {
40
- if ( err ) {
41
- // If the error is that no repo exists,
42
- // which happens when the version file is not found
43
- // we just want to signal that no repo exist, not
44
- // fail the whole process.
45
35
46
- // Use standardized errors as much as possible
47
- if ( err . code === RepoErrors . ERR_REPO_NOT_INITIALIZED ) {
48
- return cb ( null , false )
49
- }
50
-
51
- // TODO: As error codes continue to be standardized, this logic can be phase out;
52
- // it is here to maintain compatability
53
- if ( err . message . match ( / n o t f o u n d / ) || // indexeddb
54
- err . message . match ( / E N O E N T / ) || // fs
55
- err . message . match ( / N o v a l u e / ) // memory
56
- ) {
57
- return cb ( null , false )
58
- }
59
- return cb ( err )
36
+ if ( doInit ) {
37
+ const initOptions = Object . assign (
38
+ { bits : 2048 , pass : self . _options . pass } ,
39
+ typeof options . init === 'object' ? options . init : { }
40
+ )
41
+ return self . init ( initOptions , ( err ) => cb ( err ) )
60
42
}
61
- cb ( null , res )
62
- } )
63
- }
64
43
65
- const done = ( err ) => {
44
+ cb ( )
45
+ } ,
46
+ ( cb ) => {
47
+ // No problem, we don't have to start the node
48
+ if ( ! doStart ) {
49
+ return cb ( )
50
+ }
51
+ self . start ( cb )
52
+ }
53
+ ] , ( err ) => {
66
54
if ( err ) {
67
55
return self . emit ( 'error' , err )
68
56
}
69
- self . log ( 'boot:done ' )
57
+ self . log ( 'booted ' )
70
58
self . emit ( 'ready' )
71
- }
72
-
73
- const tasks = [ ]
74
-
75
- // check if there as a repo and if so open it
76
- maybeOpenRepo ( ( err , hasRepo ) => {
77
- if ( err ) {
78
- return done ( err )
79
- }
59
+ } )
60
+ }
80
61
81
- // No repo, but need should init one
82
- if ( doInit && ! hasRepo ) {
83
- tasks . push ( ( cb ) => self . init ( initOptions , cb ) )
84
- // we know we will have a repo for all follwing tasks
85
- // if the above succeeds
86
- hasRepo = true
87
- }
62
+ function isRepoUninitializedError ( err ) {
63
+ if ( ! err ) {
64
+ return false
65
+ }
88
66
89
- // Need to set config
90
- if ( setConfig ) {
91
- if ( ! hasRepo ) {
92
- console . log ( 'WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"' )
93
- } else {
94
- tasks . push ( ( cb ) => {
95
- waterfall ( [
96
- ( cb ) => self . config . get ( cb ) ,
97
- ( config , cb ) => {
98
- extend ( config , options . config )
67
+ // If the error is that no repo exists,
68
+ // which happens when the version file is not found
69
+ // we just want to signal that no repo exist, not
70
+ // fail the whole process.
99
71
100
- self . config . replace ( config , cb )
101
- }
102
- ] , cb )
103
- } )
104
- }
105
- }
72
+ // Use standardized errors as much as possible
73
+ if ( err . code === RepoErrors . ERR_REPO_NOT_INITIALIZED ) {
74
+ return true
75
+ }
106
76
107
- // Need to start up the node
108
- if ( doStart ) {
109
- if ( ! hasRepo ) {
110
- console . log ( 'WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"' )
111
- return done ( new Error ( 'Uninitalized repo' ) )
112
- } else {
113
- tasks . push ( ( cb ) => self . start ( cb ) )
114
- }
115
- }
77
+ // TODO: As error codes continue to be standardized, this logic can be phase out;
78
+ // it is here to maintain compatability
79
+ if ( err . message . match ( / n o t f o u n d / ) || // indexeddb
80
+ err . message . match ( / E N O E N T / ) || // fs
81
+ err . message . match ( / N o v a l u e / ) // memory
82
+ ) {
83
+ return true
84
+ }
116
85
117
- // Do the actual boot sequence
118
- series ( tasks , done )
119
- } )
86
+ return false
120
87
}
0 commit comments