Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gateway address to the returned IPFS api #145

Merged
merged 4 commits into from
Feb 16, 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
39 changes: 33 additions & 6 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,32 @@ class Node {
this.clean = true
this.env = Object.assign({}, process.env, {IPFS_PATH: path})
this.disposable = disposable
this._apiAddr = null
this._gatewayAddr = null

if (this.opts.env) {
Object.assign(this.env, this.opts.env)
}
}

/**
* Get the address of connected IPFS API.
*
* @returns {Multiaddr}
*/
get apiAddr () {
return this._apiAddr
}

/**
* Get the address of connected IPFS HTTP Gateway.
*
* @returns {Multiaddr}
*/
get gatewayAddr () {
return this._gatewayAddr
}

_run (args, opts, callback) {
return exec(this.exec, args, opts, callback)
}
Expand Down Expand Up @@ -197,14 +217,21 @@ class Node {
}
},
data: (data) => {
const match = String(data).trim().match(/API server listening on (.*)/)
const str = String(data).trim()
const match = str.match(/API server listening on (.*)/)
const gwmatch = str.match(/Gateway (.*) listening on (.*)/)

if (match) {
this.apiAddr = match[1]
const addr = multiaddr(this.apiAddr).nodeAddress()
this.api = ipfs(this.apiAddr)
this.api.apiHost = addr.address
this.api.apiPort = addr.port
this._apiAddr = multiaddr(match[1])
this.api = ipfs(match[1])
this.api.apiHost = this.apiAddr.nodeAddress().address
this.api.apiPort = this.apiAddr.nodeAddress().port

if (gwmatch) {
this._gatewayAddr = multiaddr(gwmatch[2])
this.api.gatewayHost = this.gatewayAddr.nodeAddress().address
this.api.gatewayPort = this.gatewayAddr.nodeAddress().port
}

callback(null, this.api)
}
Expand Down
34 changes: 34 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const async = require('async')
const expect = require('chai').expect
const ipfsApi = require('ipfs-api')
const mh = require('multihashes')
const multiaddr = require('multiaddr')
const fs = require('fs')
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
Expand Down Expand Up @@ -425,6 +426,39 @@ describe('daemons', () => {
], done)
})

it('starts the daemon and returns valid API and gateway addresses', (done) => {
let daemon
const dir = os.tmpdir() + `/${Math.ceil(Math.random() * 100)}`
async.waterfall([
(cb) => ipfsd.local(dir, cb),
(node, cb) => {
daemon = node
node.init((err) => cb(err, node))
},
(node, cb) => node.startDaemon((err, api) => cb(err, api))
], (err, res) => {
expect(err).to.not.exist

expect(daemon).to.have.property('apiAddr')
expect(daemon).to.have.property('gatewayAddr')
expect(daemon.apiAddr).to.be.instanceof(multiaddr)
expect(daemon.gatewayAddr).to.be.instanceof(multiaddr)
expect(daemon.apiAddr).to.not.equal(null)
expect(daemon.gatewayAddr).to.not.equal(null)

expect(res).to.have.property('apiHost')
expect(res).to.have.property('apiPort')
expect(res).to.have.property('gatewayHost')
expect(res).to.have.property('gatewayPort')
expect(res.apiHost).to.equal('127.0.0.1')
expect(res.apiPort).to.equal('5001')
expect(res.gatewayHost).to.equal('127.0.0.1')
expect(res.gatewayPort).to.equal('8080')

daemon.stopDaemon(done)
})
})

it('allows passing flags', (done) => {
ipfsd.disposable((err, node) => {
expect(err).to.not.exist
Expand Down