@@ -15,26 +15,43 @@ module.exports = (self) => {
15
15
const repoOpen = ! self . _repo . closed
16
16
17
17
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 )
19
21
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
20
25
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 ( / n o t f o u n d / ) || // indexeddb
46
+ err . message . match ( / E N O E N T / ) || // fs
47
+ err . message . match ( / N o v a l u e / ) // memory
48
+ ) {
49
+ return cb ( null , false )
34
50
}
35
- cb ( )
51
+ return cb ( err )
36
52
}
37
- ] , cb )
53
+ cb ( null , res )
54
+ } )
38
55
}
39
56
40
57
const done = ( err ) => {
@@ -47,27 +64,22 @@ module.exports = (self) => {
47
64
48
65
const tasks = [ ]
49
66
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 ) => {
60
69
if ( err ) {
61
70
return done ( err )
62
71
}
63
72
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
67
79
}
68
80
81
+ // Need to set config
69
82
if ( setConfig ) {
70
- self . log ( 'boot:setConfig' )
71
83
if ( ! hasRepo ) {
72
84
console . log ( 'WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"' )
73
85
} else {
@@ -83,8 +95,8 @@ module.exports = (self) => {
83
95
}
84
96
}
85
97
98
+ // Need to start up the node
86
99
if ( doStart ) {
87
- self . log ( 'boot:doStart' )
88
100
if ( ! hasRepo ) {
89
101
console . log ( 'WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"' )
90
102
return done ( new Error ( 'Uninitalized repo' ) )
@@ -93,6 +105,7 @@ module.exports = (self) => {
93
105
}
94
106
}
95
107
108
+ // Do the actual boot sequence
96
109
series ( tasks , done )
97
- }
110
+ } )
98
111
}
0 commit comments