@@ -9,17 +9,19 @@ const rimraf = require('rimraf')
9
9
const shutdown = require ( 'shutdown' )
10
10
const path = require ( 'path' )
11
11
const join = path . join
12
-
13
- const rootPath = process . env . testpath ? process . env . testpath : __dirname
12
+ const bl = require ( 'bl' )
14
13
15
14
const ipfsDefaultPath = findIpfsExecutable ( )
16
15
17
16
const GRACE_PERIOD = 7500 // amount of ms to wait before sigkill
18
17
19
18
function findIpfsExecutable ( ) {
20
- let npm3Path = path . join ( rootPath , '../../' , '/go-ipfs-dep/go-ipfs/ipfs' )
19
+ const rootPath = process . env . testpath ? process . env . testpath : __dirname
21
20
22
- let npm2Path = path . join ( rootPath , '../' , 'node_modules/go-ipfs-dep/go-ipfs/ipfs' )
21
+ const appRoot = path . join ( rootPath , '..' )
22
+ const depPath = path . join ( 'go-ipfs-dep' , 'go-ipfs' , 'ipfs' )
23
+ const npm3Path = path . join ( appRoot , '../' , depPath )
24
+ const npm2Path = path . join ( appRoot , 'node_modules' , depPath )
23
25
24
26
try {
25
27
fs . statSync ( npm3Path )
@@ -43,8 +45,9 @@ function configureNode (node, conf, done) {
43
45
44
46
// Consistent error handling
45
47
function parseConfig ( path , done ) {
48
+ const file = fs . readFileSync ( join ( path , 'config' ) )
49
+
46
50
try {
47
- const file = fs . readFileSync ( join ( path , 'config' ) )
48
51
const parsed = JSON . parse ( file )
49
52
done ( null , parsed )
50
53
} catch ( err ) {
@@ -66,13 +69,15 @@ module.exports = class Node {
66
69
}
67
70
68
71
_run ( args , envArg , done ) {
69
- let result = ''
70
72
run ( this . exec , args , envArg )
71
73
. on ( 'error' , done )
72
- . on ( 'data' , ( data ) => {
73
- result += data
74
- } )
75
- . on ( 'end' , ( ) => done ( null , result . trim ( ) ) )
74
+ . pipe ( bl ( ( err , result ) => {
75
+ if ( err ) {
76
+ return done ( err )
77
+ }
78
+
79
+ done ( null , result . toString ( ) . trim ( ) )
80
+ } ) )
76
81
}
77
82
78
83
init ( initOpts , done ) {
@@ -90,15 +95,20 @@ module.exports = class Node {
90
95
91
96
run ( this . exec , [ 'init' , '-b' , keySize ] , { env : this . env } )
92
97
. on ( 'error' , done )
93
- . on ( 'data' , ( ) => { } ) // let it flow
94
- . on ( 'end' , ( ) => {
98
+ . pipe ( bl ( ( err , buf ) => {
99
+ if ( err ) return done ( err )
100
+
95
101
configureNode ( this , this . opts , ( err ) => {
96
- if ( err ) return done ( err )
102
+ if ( err ) {
103
+ return done ( err )
104
+ }
105
+
97
106
this . clean = false
98
107
this . initialized = true
108
+
99
109
done ( null , this )
100
110
} )
101
- } )
111
+ } ) )
102
112
103
113
if ( this . disposable ) {
104
114
shutdown . addHandler ( 'disposable' , 1 , this . shutdown . bind ( this ) )
@@ -192,14 +202,24 @@ module.exports = class Node {
192
202
const api = ipfs ( node . apiAddr )
193
203
api . apiHost = addr . address
194
204
api . apiPort = addr . port
205
+
206
+ // We are happyly listening, so let's not hide other errors
207
+ node . subprocess . removeListener ( 'error' , onErr )
208
+
195
209
done2 ( null , api )
196
210
}
197
211
} )
198
212
}
199
213
200
214
stopDaemon ( done ) {
201
- if ( ! done ) done = ( ) => { }
202
- if ( ! this . subprocess ) return done ( null )
215
+ if ( ! done ) {
216
+ done = ( ) => { }
217
+ }
218
+
219
+ if ( ! this . subprocess ) {
220
+ return done ( )
221
+ }
222
+
203
223
this . killProcess ( done )
204
224
}
205
225
@@ -208,12 +228,13 @@ module.exports = class Node {
208
228
const subprocess = this . subprocess
209
229
const timeout = setTimeout ( ( ) => {
210
230
subprocess . kill ( 'SIGKILL' )
211
- done ( null )
231
+ done ( )
212
232
} , GRACE_PERIOD )
213
233
214
234
subprocess . on ( 'close' , ( ) => {
215
235
clearTimeout ( timeout )
216
- done ( null )
236
+ this . subprocess = null
237
+ done ( )
217
238
} )
218
239
219
240
subprocess . kill ( 'SIGTERM' )
@@ -236,7 +257,7 @@ module.exports = class Node {
236
257
setConfig ( key , value , done ) {
237
258
run ( this . exec , [ 'config' , key , value , '--json' ] , { env : this . env } )
238
259
. on ( 'error' , done )
239
- . on ( 'data' , ( data ) => { } )
260
+ . on ( 'data' , ( ) => { } )
240
261
. on ( 'end' , ( ) => done ( ) )
241
262
}
242
263
0 commit comments