Skip to content

Commit

Permalink
feat: upgrade to go-ipfs 0.4.3
Browse files Browse the repository at this point in the history
This also stops the crash when stopping a daemon.
  • Loading branch information
dignifiedquire committed Sep 29, 2016
1 parent a9a54c3 commit a2ebc1a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -69,4 +70,4 @@
"example": "examples",
"test": "test"
}
}
}
59 changes: 40 additions & 19 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}

Expand All @@ -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')
Expand All @@ -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())
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
5 changes: 2 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Expand Down

0 comments on commit a2ebc1a

Please sign in to comment.