1
1
'use strict'
2
2
3
- const defaults = require ( 'lodash.defaultsdeep' )
4
- const clone = require ( 'lodash.clone' )
5
3
const series = require ( 'async/series' )
6
- const path = require ( 'path' )
7
- const once = require ( 'once' )
4
+ const merge = require ( 'merge-options' )
8
5
const tmpDir = require ( './utils/tmp-dir' )
9
- const repoUtils = require ( './utils/repo/nodejs' )
10
6
const InProc = require ( './ipfsd-in-proc' )
11
7
const defaultConfig = require ( './defaults/config.json' )
12
- const defaultOptions = require ( './defaults/options.json' )
13
8
14
9
/** @ignore @typedef {import("./index").SpawnOptions } SpawnOptions */
15
10
@@ -38,7 +33,6 @@ class FactoryInProc {
38
33
*
39
34
* @param {string } type - the type of the node
40
35
* @param {function(Error, string): void } callback
41
- * @returns {void }
42
36
*/
43
37
tmpDir ( type , callback ) {
44
38
callback ( null , tmpDir ( true ) )
@@ -49,7 +43,6 @@ class FactoryInProc {
49
43
*
50
44
* @param {Object } [options={}]
51
45
* @param {function(Error, string): void } callback
52
- * @returns {void }
53
46
*/
54
47
version ( options , callback ) {
55
48
if ( typeof options === 'function' ) {
@@ -58,79 +51,51 @@ class FactoryInProc {
58
51
}
59
52
60
53
const node = new InProc ( options )
61
- node . once ( 'ready' , ( ) => {
62
- node . version ( callback )
63
- } )
54
+ node . version ( callback )
64
55
}
65
56
66
57
/**
67
58
* Spawn JSIPFS instances
68
59
*
69
- * @param {SpawnOptions } [opts ={}] - various config options and ipfs config parameters
60
+ * @param {SpawnOptions } [options ={}] - various config options and ipfs config parameters
70
61
* @param {function(Error, InProc): void } callback - a callback that receives an array with an `ipfs-instance` attached to the node and a `Node`
71
62
* @returns {void }
72
63
*/
73
- spawn ( opts , callback ) {
74
- if ( typeof opts === 'function' ) {
75
- callback = opts
76
- opts = defaultOptions
64
+ spawn ( options , callback ) {
65
+ if ( typeof options === 'function' ) {
66
+ callback = options
67
+ options = { }
77
68
}
78
69
79
- const options = defaults ( { } , opts , defaultOptions )
80
- options . init = typeof options . init !== 'undefined'
81
- ? options . init
82
- : true
83
-
84
- if ( options . disposable ) {
85
- options . config = defaults ( { } , options . config , defaultConfig )
86
- } else {
87
- const nonDisposableConfig = clone ( defaultConfig )
88
- options . init = false
89
- options . start = false
90
-
91
- const defaultRepo = path . join (
92
- process . env . HOME || process . env . USERPROFILE || '' ,
93
- options . isJs ? '.jsipfs' : '.ipfs'
94
- )
95
-
96
- options . repoPath = options . repoPath || ( process . env . IPFS_PATH || defaultRepo )
97
- options . config = defaults ( { } , options . config , nonDisposableConfig )
98
- }
70
+ const daemonOptions = merge ( {
71
+ exec : this . options . exec ,
72
+ type : this . options . type ,
73
+ IpfsApi : this . options . IpfsApi ,
74
+ disposable : true ,
75
+ start : options . disposable !== false ,
76
+ init : options . disposable !== false ,
77
+ config : defaultConfig
78
+ } , options )
99
79
100
80
if ( options . defaultAddrs ) {
101
- delete options . config . Addresses
81
+ delete daemonOptions . config . Addresses
102
82
}
103
83
104
- options . type = this . options . type
105
- options . exec = options . exec || this . options . exec
106
-
107
- if ( typeof options . exec !== 'function' ) {
84
+ if ( typeof this . options . exec !== 'function' ) {
108
85
return callback ( new Error ( `'type' proc requires 'exec' to be a coderef` ) )
109
86
}
110
87
111
- const node = new InProc ( options )
112
- const callbackOnce = once ( ( err ) => {
113
- if ( err ) {
114
- return callback ( err )
115
- }
116
- callback ( null , node )
117
- } )
118
- node . once ( 'error' , callbackOnce )
88
+ const node = new InProc ( daemonOptions )
119
89
120
90
series ( [
121
- ( cb ) => node . once ( 'ready' , cb ) ,
122
- ( cb ) => repoUtils . repoExists ( node . path , ( err , initialized ) => {
123
- if ( err ) { return cb ( err ) }
124
- node . initialized = initialized
125
- cb ( )
126
- } ) ,
127
- ( cb ) => options . init
128
- ? node . init ( cb )
129
- : cb ( ) ,
130
- ( cb ) => options . start
131
- ? node . start ( options . args , cb )
132
- : cb ( )
133
- ] , callbackOnce )
91
+ daemonOptions . init && ( cb => node . init ( daemonOptions . initOptions , cb ) ) ,
92
+ daemonOptions . start && ( cb => node . start ( daemonOptions . args , cb ) )
93
+ ] . filter ( Boolean ) ,
94
+ ( err ) => {
95
+ if ( err ) { return callback ( err ) }
96
+
97
+ callback ( null , node )
98
+ } )
134
99
}
135
100
}
136
101
0 commit comments