11'use strict'
22
33const waterfall = require ( 'async/waterfall' )
4- const series = require ( 'async/series' )
5- const extend = require ( 'deep-extend' )
64const RepoErrors = require ( 'ipfs-repo' ) . errors
75
86// Boot an IPFS node depending on the options set
@@ -11,110 +9,79 @@ module.exports = (self) => {
119 const options = self . _options
1210 const doInit = options . init
1311 const doStart = options . start
14- const config = options . config
15- const setConfig = config && typeof config === 'object'
16- const repoOpen = ! self . _repo . closed
1712
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+ }
2923
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 )
3727 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 ) )
3834 }
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.
4535
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 ) )
6042 }
61- cb ( null , res )
62- } )
63- }
6443
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 ) => {
6654 if ( err ) {
6755 return self . emit ( 'error' , err )
6856 }
69- self . log ( 'boot:done ' )
57+ self . log ( 'booted ' )
7058 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+ }
8061
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+ }
8866
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.
9971
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+ }
10676
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+ }
11685
117- // Do the actual boot sequence
118- series ( tasks , done )
119- } )
86+ return false
12087}
0 commit comments