-
Notifications
You must be signed in to change notification settings - Fork 296
add opts param to IpfsAPI, allow 'protocol' config #67
Changes from 5 commits
b138ee1
8babda6
bbcdd5b
35d7b19
b5c63df
3f841b9
a0a487f
697899f
2ad3d3d
a4514ae
5547c07
ece0634
ed11c60
0270462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,23 +7,33 @@ const Wreck = require('wreck') | |
|
||
exports = module.exports = IpfsAPI | ||
|
||
function IpfsAPI (host_or_multiaddr, port) { | ||
function IpfsAPI (host_or_multiaddr, port, opts) { | ||
const self = this | ||
const config = getConfig() | ||
|
||
if (!(self instanceof IpfsAPI)) { | ||
return new IpfsAPI(host_or_multiaddr, port) | ||
return new IpfsAPI(host_or_multiaddr, port, opts) | ||
} | ||
|
||
try { | ||
const maddr = multiaddr(host_or_multiaddr).nodeAddress() | ||
config.host = maddr.address | ||
config.port = maddr.port | ||
} catch (e) { | ||
config.host = host_or_multiaddr | ||
config.port = port || config.port | ||
if (typeof host_or_multiaddr === 'string') { | ||
config.host = host_or_multiaddr | ||
config.port = port && typeof port !== 'object' ? port : config.port | ||
} | ||
} | ||
|
||
let lastIndex = arguments.length | ||
while (!opts && lastIndex-- > 0) { | ||
opts = arguments[lastIndex] | ||
if (opts) break | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely clear what you are doing above could you explain please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's preventing config.host or config.port from being set to the opts object, thus preserving the defaults if needed |
||
|
||
Object.assign(config, opts) | ||
|
||
// autoconfigure in browser | ||
if (!config.host && | ||
typeof window !== 'undefined') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
const ipfsAPI = require('../src/index.js') | ||
const assert = require('assert') | ||
|
||
describe('ipfsAPI constructor tests', () => { | ||
describe('parameter permuations', () => { | ||
const apiAddrs = require('./tmp-disposable-nodes-addrs.json') | ||
const apiAddr = apiAddrs.a.split('/') | ||
|
||
function clientWorks (client, done) { | ||
client.id((err, res) => { | ||
if (err) throw err | ||
const id = res | ||
assert(id.ID) | ||
assert(id.PublicKey) | ||
done() | ||
}) | ||
} | ||
|
||
it('opts', (done) => { | ||
clientWorks(ipfsAPI({ | ||
host: apiAddr[2], | ||
port: apiAddr[4], | ||
protocol: 'http' | ||
}), done) | ||
}) | ||
|
||
it('mutliaddr, opts', (done) => { | ||
clientWorks(ipfsAPI( | ||
apiAddrs.a, | ||
{protocol: 'http'} | ||
), done) | ||
}) | ||
|
||
it('host, port', (done) => { | ||
clientWorks(ipfsAPI( | ||
apiAddr[2], | ||
apiAddr[4] | ||
), done) | ||
}) | ||
|
||
it('host, port, opts', (done) => { | ||
clientWorks(ipfsAPI( | ||
apiAddr[2], | ||
apiAddr[4], | ||
{protocol: 'http'} | ||
), done) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to rename the files to all in end
.spec.js
, so we can dotest/*.spec.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That way you can pass the same glob to the
mocha
config intasks/test.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Added