From a2ebc1af31ce9fccbd5c20beaf82dbdeab18f16e Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 2 Jun 2016 21:46:46 +0200 Subject: [PATCH] feat: upgrade to go-ipfs 0.4.3 This also stops the crash when stopping a daemon. --- package.json | 13 +++++----- src/node.js | 59 ++++++++++++++++++++++++++++-------------- test/fixtures/test.txt | 1 + test/index.spec.js | 5 ++-- 4 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 test/fixtures/test.txt diff --git a/package.json b/package.json index 23da73cd..2bacb084 100644 --- a/package.json +++ b/package.json @@ -44,18 +44,19 @@ ], "license": "MIT", "dependencies": { - "go-ipfs-dep": "0.4.1", + "bl": "^1.1.2", + "go-ipfs-dep": "0.4.3", "ipfs-api": "^9.0.0", - "multiaddr": "^2.0.0", - "rimraf": "^2.4.5", + "multiaddr": "^2.0.3", + "rimraf": "^2.5.4", "run-series": "^1.1.4", "shutdown": "^0.2.4", "subcomandante": "^1.0.5" }, "devDependencies": { - "aegir": "^8.0.1", + "aegir": "^8.1.2", "mkdirp": "^0.5.1", - "pre-commit": "^1.1.2" + "pre-commit": "^1.1.3" }, "repository": { "type": "git", @@ -69,4 +70,4 @@ "example": "examples", "test": "test" } -} \ No newline at end of file +} diff --git a/src/node.js b/src/node.js index 31b15630..565faa89 100644 --- a/src/node.js +++ b/src/node.js @@ -9,17 +9,19 @@ const rimraf = require('rimraf') const shutdown = require('shutdown') const path = require('path') const join = path.join - -const rootPath = process.env.testpath ? process.env.testpath : __dirname +const bl = require('bl') const ipfsDefaultPath = findIpfsExecutable() const GRACE_PERIOD = 7500 // amount of ms to wait before sigkill function findIpfsExecutable () { - let npm3Path = path.join(rootPath, '../../', '/go-ipfs-dep/go-ipfs/ipfs') + const rootPath = process.env.testpath ? process.env.testpath : __dirname - let npm2Path = path.join(rootPath, '../', 'node_modules/go-ipfs-dep/go-ipfs/ipfs') + const appRoot = path.join(rootPath, '..') + const depPath = path.join('go-ipfs-dep', 'go-ipfs', 'ipfs') + const npm3Path = path.join(appRoot, '../', depPath) + const npm2Path = path.join(appRoot, 'node_modules', depPath) try { fs.statSync(npm3Path) @@ -43,8 +45,9 @@ function configureNode (node, conf, done) { // Consistent error handling function parseConfig (path, done) { + const file = fs.readFileSync(join(path, 'config')) + try { - const file = fs.readFileSync(join(path, 'config')) const parsed = JSON.parse(file) done(null, parsed) } catch (err) { @@ -66,13 +69,15 @@ module.exports = class Node { } _run (args, envArg, done) { - let result = '' run(this.exec, args, envArg) .on('error', done) - .on('data', (data) => { - result += data - }) - .on('end', () => done(null, result.trim())) + .pipe(bl((err, result) => { + if (err) { + return done(err) + } + + done(null, result.toString().trim()) + })) } init (initOpts, done) { @@ -90,15 +95,20 @@ module.exports = class Node { run(this.exec, ['init', '-b', keySize], {env: this.env}) .on('error', done) - .on('data', () => {}) // let it flow - .on('end', () => { + .pipe(bl((err, buf) => { + if (err) return done(err) + configureNode(this, this.opts, (err) => { - if (err) return done(err) + if (err) { + return done(err) + } + this.clean = false this.initialized = true + done(null, this) }) - }) + })) if (this.disposable) { shutdown.addHandler('disposable', 1, this.shutdown.bind(this)) @@ -192,14 +202,24 @@ module.exports = class Node { const api = ipfs(node.apiAddr) api.apiHost = addr.address api.apiPort = addr.port + + // We are happyly listening, so let's not hide other errors + node.subprocess.removeListener('error', onErr) + done2(null, api) } }) } stopDaemon (done) { - if (!done) done = () => {} - if (!this.subprocess) return done(null) + if (!done) { + done = () => {} + } + + if (!this.subprocess) { + return done() + } + this.killProcess(done) } @@ -208,12 +228,13 @@ module.exports = class Node { const subprocess = this.subprocess const timeout = setTimeout(() => { subprocess.kill('SIGKILL') - done(null) + done() }, GRACE_PERIOD) subprocess.on('close', () => { clearTimeout(timeout) - done(null) + this.subprocess = null + done() }) subprocess.kill('SIGTERM') @@ -236,7 +257,7 @@ module.exports = class Node { setConfig (key, value, done) { run(this.exec, ['config', key, value, '--json'], {env: this.env}) .on('error', done) - .on('data', (data) => {}) + .on('data', () => {}) .on('end', () => done()) } diff --git a/test/fixtures/test.txt b/test/fixtures/test.txt new file mode 100644 index 00000000..95d09f2b --- /dev/null +++ b/test/fixtures/test.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/test/index.spec.js b/test/index.spec.js index 7e1e81de..a4d19064 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -345,14 +345,13 @@ describe('ipfs-api version', function () { }) }) - // NOTE: if you change ../src/, the hash will need to be changed it('uses the correct ipfs-api', (done) => { - ipfs.util.addFromFs(path.join(__dirname, '../src'), { recursive: true }, (err, res) => { + ipfs.util.addFromFs(path.join(__dirname, 'fixtures'), { recursive: true }, (err, res) => { if (err) throw err const added = res[res.length - 1] assert(added) - assert.equal(added.hash, 'QmUNYnr4nm9Zfr6utTD9rVdSf9ASRguqHDciwvsc2gsH8y') + assert.equal(added.hash, 'QmXkiTdnfRJjiQREtF5dWf2X4V9awNHQSn9YGofwVY4qUU') done() }) })