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

Commit 0784c5c

Browse files
committed
Merge pull request #67 from ckeenan/extra-opts
add opts param to IpfsAPI, allow 'protocol' config
2 parents 3f841b9 + 0270462 commit 0784c5c

10 files changed

+939
-345
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ $ npm install --save ipfs-api
2222
var ipfsAPI = require('ipfs-api')
2323

2424
// connect to ipfs daemon API server
25-
var ipfs = ipfsAPI('localhost', '5001') // leaving out the arguments will default to these values
25+
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}) // leaving out the arguments will default to these values
26+
27+
// or connect with multiaddr
28+
var ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')
29+
30+
// or using options
31+
var ipfs = ipfsAPI({host: 'localhost', port: '5001', procotol: 'http'})
2632
```
2733

2834
### In the Browser through browserify

dist/ipfsapi.js

Lines changed: 843 additions & 321 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ipfsapi.min.js

Lines changed: 19 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function (config) {
4747
basePath: '',
4848
frameworks: ['mocha'],
4949
files: [
50-
'test/tests.js'
50+
'test/*.spec.js'
5151
],
5252
exclude: [],
5353
preprocessors: {

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports = module.exports = () => {
77
'api-path': '/api/v0/',
88
'user-agent': `/node-${pkg.name}/${pkg.version}/`,
99
'host': 'localhost',
10-
'port': '5001'
10+
'port': '5001',
11+
'protocol': 'http'
1112
}
1213
}

src/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,33 @@ const ndjson = require('ndjson')
88

99
exports = module.exports = IpfsAPI
1010

11-
function IpfsAPI (host_or_multiaddr, port) {
11+
function IpfsAPI (host_or_multiaddr, port, opts) {
1212
const self = this
1313
const config = getConfig()
1414

1515
if (!(self instanceof IpfsAPI)) {
16-
return new IpfsAPI(host_or_multiaddr, port)
16+
return new IpfsAPI(host_or_multiaddr, port, opts)
1717
}
1818

1919
try {
2020
const maddr = multiaddr(host_or_multiaddr).nodeAddress()
2121
config.host = maddr.address
2222
config.port = maddr.port
2323
} catch (e) {
24-
config.host = host_or_multiaddr
25-
config.port = port || config.port
24+
if (typeof host_or_multiaddr === 'string') {
25+
config.host = host_or_multiaddr
26+
config.port = port && typeof port !== 'object' ? port : config.port
27+
}
28+
}
29+
30+
let lastIndex = arguments.length
31+
while (!opts && lastIndex-- > 0) {
32+
opts = arguments[lastIndex]
33+
if (opts) break
2634
}
2735

36+
Object.assign(config, opts)
37+
2838
// autoconfigure in browser
2939
if (!config.host &&
3040
typeof window !== 'undefined') {

src/request-api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
8989

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

tasks/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ gulp.task('test:browser', done => {
3434
})
3535

3636
gulp.task('mocha', () => {
37-
return gulp.src('test/tests.js')
37+
return gulp.src('test/*.spec.js')
3838
.pipe($.mocha())
3939
})
4040

test/constructor.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict'
2+
3+
const ipfsAPI = require('../src/index.js')
4+
const assert = require('assert')
5+
6+
describe('ipfsAPI constructor tests', () => {
7+
describe('parameter permuations', () => {
8+
const apiAddrs = require('./tmp-disposable-nodes-addrs.json')
9+
const apiAddr = apiAddrs.a.split('/')
10+
11+
function clientWorks (client, done) {
12+
client.id((err, res) => {
13+
if (err) throw err
14+
const id = res
15+
assert(id.ID)
16+
assert(id.PublicKey)
17+
done()
18+
})
19+
}
20+
21+
it('opts', (done) => {
22+
clientWorks(ipfsAPI({
23+
host: apiAddr[2],
24+
port: apiAddr[4],
25+
protocol: 'http'
26+
}), done)
27+
})
28+
29+
it('mutliaddr, opts', (done) => {
30+
clientWorks(ipfsAPI(
31+
apiAddrs.a,
32+
{protocol: 'http'}
33+
), done)
34+
})
35+
36+
it('host, port', (done) => {
37+
clientWorks(ipfsAPI(
38+
apiAddr[2],
39+
apiAddr[4]
40+
), done)
41+
})
42+
43+
it('host, port, opts', (done) => {
44+
clientWorks(ipfsAPI(
45+
apiAddr[2],
46+
apiAddr[4],
47+
{protocol: 'http'}
48+
), done)
49+
})
50+
})
51+
})
File renamed without changes.

0 commit comments

Comments
 (0)