Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

feat: make js-ipfs cli and daemon behave like go-ipfs #1067

Merged
merged 6 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ module.exports = {
print('Daemon is ready')
})

process.on('SIGINT', () => {
print('Received interrupt signal, shutting down..')
const cleanup = () => {
print(`Received interrupt signal, shutting down..`)
httpAPI.stop((err) => {
if (err) {
throw err
}
process.exit(0)
})
})
}

// listen for graceful termination
process.on('SIGTERM', cleanup)
process.on('SIGINT', cleanup)
}
}
16 changes: 9 additions & 7 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ module.exports = function preStart (self) {
(config, id, cb) => {
self._peerInfo = new PeerInfo(id)

config.Addresses.Swarm.forEach((addr) => {
let ma = multiaddr(addr)
if (config.Addresses && config.Addresses.Swarm) {
config.Addresses.Swarm.forEach((addr) => {
let ma = multiaddr(addr)

if (ma.getPeerId()) {
ma = ma.encapsulate('/ipfs/' + self._peerInfo.id.toB58String())
}
if (ma.getPeerId()) {
ma = ma.encapsulate('/ipfs/' + self._peerInfo.id.toB58String())
}

self._peerInfo.multiaddrs.add(ma)
})
self._peerInfo.multiaddrs.add(ma)
})
}

cb()
}
Expand Down
86 changes: 86 additions & 0 deletions test/cli/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,104 @@
const expect = require('chai').expect
const clean = require('../utils/clean')
const ipfsCmd = require('../utils/ipfs-exec')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const os = require('os')
const fs = require('fs')
const path = require('path')

const isWindows = os.platform() === 'win32'

const checkLock = (repo, cb) => {
// skip on windows
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326983530
if (!isWindows) {
if (fs.existsSync(path.join(repo, 'repo.lock'))) {
cb(new Error('repo.lock not removed'))
}
if (fs.existsSync(path.join(repo, 'api'))) {
cb(new Error('api file not removed'))
}
}
cb()
}

describe('daemon', () => {
let repoPath
let ipfs

const killSig = (sig) => {
let proc = null
return ipfs('init').then(() => {
proc = ipfs('daemon')
return new Promise((resolve, reject) => {
pull(
toPull(proc.stdout),
pull.collect((err, res) => {
expect(err).to.not.exist()
const data = res.toString()
if (data.includes(`Daemon is ready`)) {
if (proc.kill(sig)) {
resolve()
} else {
reject(new Error(`Unable to ${sig} process`))
}
}
})
)

pull(
toPull(proc.stderr),
pull.collect((err, res) => {
expect(err).to.not.exist()
const data = res.toString()
if (data.length > 0) {
reject(new Error(data))
}
})
)
})
})
}

beforeEach(() => {
repoPath = '/tmp/ipfs-test-not-found-' + Math.random().toString().substring(2, 8)
ipfs = ipfsCmd(repoPath)
})

afterEach(() => clean(repoPath))

it(`don't crash if Addresses.Swarm is empty`, function (done) {
this.timeout(20000)
ipfs('init').then(() => {
return ipfs('config', 'Addresses', JSON.stringify({
API: '/ip4/0.0.0.0/tcp/0',
Gateway: '/ip4/0.0.0.0/tcp/0'
}), '--json')
}).then(() => {
return ipfs('daemon')
}).then((res) => {
expect(res).to.have.string('Daemon is ready')
done()
}).catch((err) => {
done(err)
})
})

it(`should handle SIGINT gracefully`, function (done) {
this.timeout(20000)
killSig('SIGINT').then(() => {
checkLock(repoPath, done)
}).catch(done)
})

it(`should handle SIGTERM gracefully`, function (done) {
this.timeout(20000)
killSig('SIGTERM').then(() => {
checkLock(repoPath, done)
}).catch(done)
})

it('gives error if user hasn\'t run init before', (done) => {
const expectedError = 'no initialized ipfs repo found in ' + repoPath

Expand Down