Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

add opts param to IpfsAPI, allow 'protocol' config #67

Merged
merged 14 commits into from
Nov 22, 2015
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ $ npm install --save ipfs-api
var ipfsAPI = require('ipfs-api')

// connect to ipfs daemon API server
var ipfs = ipfsAPI('localhost', '5001') // leaving out the arguments will default to these values
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}) // leaving out the arguments will default to these values

// or connect with multiaddr
var ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')

// or using options
var ipfs = ipfsAPI({host: 'localhost', port: '5001', procotol: 'http'})
```

### In the Browser through browserify
Expand Down
1,164 changes: 843 additions & 321 deletions dist/ipfsapi.js

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions dist/ipfsapi.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function (config) {
basePath: '',
frameworks: ['mocha'],
files: [
'test/tests.js'
'test/*.spec.js'
],
exclude: [],
preprocessors: {
Expand Down
5 changes: 3 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const pkg = require('../package.json')
exports = module.exports = () => {
return {
'api-path': '/api/v0/',
'user-agent': `/node-$pkg.name}/${pkg.version}/`,
'user-agent': `/node-${pkg.name}/${pkg.version}/`,
'host': 'localhost',
'port': '5001'
'port': '5001',
'protocol': 'http'
}
}
18 changes: 14 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ const ndjson = require('ndjson')

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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely clear what you are doing above could you explain please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Setting host or port to opts is not problematic if you set host or port in opts. If those aren't set the defaults are overridden with the opts object and we can't connect
e.g. so ipfsAPI({protocol: 'http'}) will work


Object.assign(config, opts)

// autoconfigure in browser
if (!config.host &&
typeof window !== 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion src/request-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {

const opts = {
method: files ? 'POST' : 'GET',
uri: `http://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
uri: `${config.protocol}://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
headers: {}
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gulp.task('test:browser', done => {
})

gulp.task('mocha', () => {
return gulp.src('test/tests.js')
return gulp.src('test/*.spec.js')
.pipe($.mocha())
})

Expand Down
51 changes: 51 additions & 0 deletions test/constructor.spec.js
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)
})
})
})
File renamed without changes.