Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
fix: pass protocol to api client
Browse files Browse the repository at this point in the history
Depends on
- multiformats/js-uri-to-multiaddr#3
- ipfs-inactive/js-ipfs-http-client#935

Fixes ipfs/ipfs-webui#925

License: MIT
Signed-off-by: Oli Evans <oli@tableflip.io>
  • Loading branch information
olizilla committed Jan 25, 2019
1 parent fcabfa9 commit 4149e2d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/js-ipfs-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ async function tryApi ({ IpfsApi, apiAddress, defaultApiAddress, location, ipfsC
console.log('Trying ipfs-api at current origin', originAddress)
const res = await maybeApi({
apiAddress: originAddress,
apiOpts: {
protocol: location.protocol.slice(0, -1)
},
ipfsConnectionTest,
IpfsApi
})
Expand All @@ -37,9 +40,9 @@ async function tryApi ({ IpfsApi, apiAddress, defaultApiAddress, location, ipfsC
}

// Helper to construct and test an api client. Returns an js-ipfs-api instance or null
async function maybeApi ({ apiAddress, ipfsConnectionTest, IpfsApi }) {
async function maybeApi ({ apiAddress, apiOpts, ipfsConnectionTest, IpfsApi }) {
try {
const ipfs = new IpfsApi(apiAddress)
const ipfs = new IpfsApi(apiAddress, apiOpts)
await ipfsConnectionTest(ipfs)
return { ipfs, provider, apiAddress }
} catch (error) {
Expand Down
36 changes: 24 additions & 12 deletions src/js-ipfs-api/index.test.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,80 @@
/* global it, expect, jest */
const tryApi = require('./index.js')
const { URL } = require('url')
const IpfsApi = require('ipfs-http-client')

it('Should use the apiAddress', async (done) => {
const opts = {
apiAddress: '/ip4/1.1.1.1/tcp/1111',
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001',
location: new URL('http://localhost:5001'),
IpfsApi: jest.fn(),
IpfsApi: IpfsApi,
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true)
}
const res = await tryApi(opts)
expect(res.apiAddress).toEqual(opts.apiAddress)
expect(res.provider).toEqual('js-ipfs-api')
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1)
expect(opts.IpfsApi.mock.calls.length).toBe(1)
const config = res.ipfs.util.getEndpointConfig()
expect(config.host).toEqual('1.1.1.1')
expect(config.port).toEqual('1111')
expect(config.protocol).toEqual('http')
done()
})

it('Should use the location where hostname not localhost', async (done) => {
const opts = {
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001',
location: new URL('http://dev.local:5001'),
IpfsApi: jest.fn(),
IpfsApi: IpfsApi,
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true)
}
const res = await tryApi(opts)
expect(res.apiAddress).toEqual('/dnsaddr/dev.local/tcp/5001/http')
expect(res.apiAddress).toEqual('/dns4/dev.local/tcp/5001/http')
expect(res.provider).toEqual('js-ipfs-api')
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1)
expect(opts.IpfsApi.mock.calls.length).toBe(1)
const config = res.ipfs.util.getEndpointConfig()
expect(config.host).toEqual('dev.local')
expect(config.port).toEqual('5001')
expect(config.protocol).toEqual('http')
done()
})

it('Should use the location where port not 5001', async (done) => {
const opts = {
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001',
location: new URL('http://localhost:9999'),
IpfsApi: jest.fn(),
location: new URL('https://webui.ipfs.io'),
IpfsApi: IpfsApi,
ipfsConnectionTest: jest.fn().mockResolvedValueOnce(true)
}
const res = await tryApi(opts)
expect(res.apiAddress).toEqual('/dnsaddr/localhost/tcp/9999/http')
expect(res.apiAddress).toEqual('/dns4/webui.ipfs.io/tcp/443/https')
expect(res.provider).toEqual('js-ipfs-api')
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(1)
expect(opts.IpfsApi.mock.calls.length).toBe(1)
const config = res.ipfs.util.getEndpointConfig()
expect(config.host).toEqual('webui.ipfs.io')
expect(config.port).toEqual('443')
expect(config.protocol).toEqual('https')
done()
})

it('Should use the defaultApiAddress if location fails', async (done) => {
const opts = {
defaultApiAddress: '/ip4/127.0.0.1/tcp/5001',
location: new URL('http://astro.cat:5001'),
IpfsApi: jest.fn(),
IpfsApi: IpfsApi,
// location call fails, default ok
ipfsConnectionTest: jest.fn()
.mockRejectedValueOnce(new Error('nope'))
.mockResolvedValueOnce(true)
}
const res = await tryApi(opts)
console.log('res', res)
expect(res.apiAddress).toEqual(opts.defaultApiAddress)
expect(res.provider).toEqual('js-ipfs-api')
expect(opts.ipfsConnectionTest.mock.calls.length).toBe(2)
expect(opts.IpfsApi.mock.calls.length).toBe(2)
const config = res.ipfs.util.getEndpointConfig()
expect(config.host).toEqual('127.0.0.1')
expect(config.port).toEqual('5001')
expect(config.protocol).toEqual('http')
done()
})

0 comments on commit 4149e2d

Please sign in to comment.