Skip to content

Commit a2ebc1a

Browse files
feat: upgrade to go-ipfs 0.4.3
This also stops the crash when stopping a daemon.
1 parent a9a54c3 commit a2ebc1a

File tree

4 files changed

+50
-28
lines changed

4 files changed

+50
-28
lines changed

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@
4444
],
4545
"license": "MIT",
4646
"dependencies": {
47-
"go-ipfs-dep": "0.4.1",
47+
"bl": "^1.1.2",
48+
"go-ipfs-dep": "0.4.3",
4849
"ipfs-api": "^9.0.0",
49-
"multiaddr": "^2.0.0",
50-
"rimraf": "^2.4.5",
50+
"multiaddr": "^2.0.3",
51+
"rimraf": "^2.5.4",
5152
"run-series": "^1.1.4",
5253
"shutdown": "^0.2.4",
5354
"subcomandante": "^1.0.5"
5455
},
5556
"devDependencies": {
56-
"aegir": "^8.0.1",
57+
"aegir": "^8.1.2",
5758
"mkdirp": "^0.5.1",
58-
"pre-commit": "^1.1.2"
59+
"pre-commit": "^1.1.3"
5960
},
6061
"repository": {
6162
"type": "git",
@@ -69,4 +70,4 @@
6970
"example": "examples",
7071
"test": "test"
7172
}
72-
}
73+
}

src/node.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ const rimraf = require('rimraf')
99
const shutdown = require('shutdown')
1010
const path = require('path')
1111
const join = path.join
12-
13-
const rootPath = process.env.testpath ? process.env.testpath : __dirname
12+
const bl = require('bl')
1413

1514
const ipfsDefaultPath = findIpfsExecutable()
1615

1716
const GRACE_PERIOD = 7500 // amount of ms to wait before sigkill
1817

1918
function findIpfsExecutable () {
20-
let npm3Path = path.join(rootPath, '../../', '/go-ipfs-dep/go-ipfs/ipfs')
19+
const rootPath = process.env.testpath ? process.env.testpath : __dirname
2120

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)
2325

2426
try {
2527
fs.statSync(npm3Path)
@@ -43,8 +45,9 @@ function configureNode (node, conf, done) {
4345

4446
// Consistent error handling
4547
function parseConfig (path, done) {
48+
const file = fs.readFileSync(join(path, 'config'))
49+
4650
try {
47-
const file = fs.readFileSync(join(path, 'config'))
4851
const parsed = JSON.parse(file)
4952
done(null, parsed)
5053
} catch (err) {
@@ -66,13 +69,15 @@ module.exports = class Node {
6669
}
6770

6871
_run (args, envArg, done) {
69-
let result = ''
7072
run(this.exec, args, envArg)
7173
.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+
}))
7681
}
7782

7883
init (initOpts, done) {
@@ -90,15 +95,20 @@ module.exports = class Node {
9095

9196
run(this.exec, ['init', '-b', keySize], {env: this.env})
9297
.on('error', done)
93-
.on('data', () => {}) // let it flow
94-
.on('end', () => {
98+
.pipe(bl((err, buf) => {
99+
if (err) return done(err)
100+
95101
configureNode(this, this.opts, (err) => {
96-
if (err) return done(err)
102+
if (err) {
103+
return done(err)
104+
}
105+
97106
this.clean = false
98107
this.initialized = true
108+
99109
done(null, this)
100110
})
101-
})
111+
}))
102112

103113
if (this.disposable) {
104114
shutdown.addHandler('disposable', 1, this.shutdown.bind(this))
@@ -192,14 +202,24 @@ module.exports = class Node {
192202
const api = ipfs(node.apiAddr)
193203
api.apiHost = addr.address
194204
api.apiPort = addr.port
205+
206+
// We are happyly listening, so let's not hide other errors
207+
node.subprocess.removeListener('error', onErr)
208+
195209
done2(null, api)
196210
}
197211
})
198212
}
199213

200214
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+
203223
this.killProcess(done)
204224
}
205225

@@ -208,12 +228,13 @@ module.exports = class Node {
208228
const subprocess = this.subprocess
209229
const timeout = setTimeout(() => {
210230
subprocess.kill('SIGKILL')
211-
done(null)
231+
done()
212232
}, GRACE_PERIOD)
213233

214234
subprocess.on('close', () => {
215235
clearTimeout(timeout)
216-
done(null)
236+
this.subprocess = null
237+
done()
217238
})
218239

219240
subprocess.kill('SIGTERM')
@@ -236,7 +257,7 @@ module.exports = class Node {
236257
setConfig (key, value, done) {
237258
run(this.exec, ['config', key, value, '--json'], {env: this.env})
238259
.on('error', done)
239-
.on('data', (data) => {})
260+
.on('data', () => {})
240261
.on('end', () => done())
241262
}
242263

test/fixtures/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

test/index.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,13 @@ describe('ipfs-api version', function () {
345345
})
346346
})
347347

348-
// NOTE: if you change ../src/, the hash will need to be changed
349348
it('uses the correct ipfs-api', (done) => {
350-
ipfs.util.addFromFs(path.join(__dirname, '../src'), { recursive: true }, (err, res) => {
349+
ipfs.util.addFromFs(path.join(__dirname, 'fixtures'), { recursive: true }, (err, res) => {
351350
if (err) throw err
352351

353352
const added = res[res.length - 1]
354353
assert(added)
355-
assert.equal(added.hash, 'QmUNYnr4nm9Zfr6utTD9rVdSf9ASRguqHDciwvsc2gsH8y')
354+
assert.equal(added.hash, 'QmXkiTdnfRJjiQREtF5dWf2X4V9awNHQSn9YGofwVY4qUU')
356355
done()
357356
})
358357
})

0 commit comments

Comments
 (0)