Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: pass timeout arg to server
Browse files Browse the repository at this point in the history
Refactors how we pass url params to the API, all args are now normalised
in one place.  If a timeout is passed it is passed on to the server as
a url search param.

N.b. this does mean that timeouts are set in the client and on the server
so it's not always clear which will expire first, though one will expire.

We do our best to ensure the errors are the same, though when the API
is streaming there's no way to tell if the request completed successfully
or if the timeout occured as the timeout message arrives in http trailers.
  • Loading branch information
achingbrain committed Apr 10, 2020
1 parent 943d4a8 commit ab6b976
Show file tree
Hide file tree
Showing 97 changed files with 532 additions and 353 deletions.
14 changes: 13 additions & 1 deletion packages/interface-ipfs-core/src/dht/find-provs.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,20 @@ module.exports = (common, options) => {
}

const cidV0 = await fakeCid()
const start = Date.now()
let res

await expect(all(nodeA.dht.findProvs(cidV0, options))).to.be.rejected()
try {
res = await all(nodeA.dht.findProvs(cidV0, options))
} catch (err) {
// rejected by http client
expect(err).to.have.property('name', 'TimeoutError')
return
}

// rejected by the server, errors don't work over http - https://github.com/ipfs/js-ipfs/issues/2519
expect(res).to.be.an('array').with.lengthOf(0)
expect(Date.now() - start).to.be.lessThan(100)
})
})
}
4 changes: 2 additions & 2 deletions packages/ipfs-http-client/src/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module.exports = configure((api) => {
const progressFn = options.progress

const res = await api.post('add', {
searchParams: toUrlSearchParams(null, {
...options,
searchParams: toUrlSearchParams({
'stream-channels': true,
...options,
progress: Boolean(progressFn)
}),
timeout: options.timeout,
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
const { BigNumber } = require('bignumber.js')
const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
const res = await api.post('bitswap/stat', {
searchParams: options,
searchParams: toUrlSearchParams(options),
timeout: options.timeout,
signal: options.signal
})
Expand Down
8 changes: 5 additions & 3 deletions packages/ipfs-http-client/src/bitswap/unwant.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (cid, options = {}) => {
options.arg = typeof cid === 'string' ? cid : new CID(cid).toString()

const res = await api.post('bitswap/unwant', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: typeof cid === 'string' ? cid : new CID(cid).toString(),
...options
})
})

return res.json()
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (peer, options = {}) => {
Expand All @@ -12,7 +13,7 @@ module.exports = configure(api => {
const res = await (await api.post('bitswap/wantlist', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams(options)
})).json()

return (res.Keys || []).map(k => new CID(k['/']))
Expand Down
11 changes: 7 additions & 4 deletions packages/ipfs-http-client/src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ const Block = require('ipfs-block')
const CID = require('cids')
const { Buffer } = require('buffer')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (cid, options = {}) => {
cid = new CID(cid)
options.arg = cid.toString()

const rsp = await api.post('block/get', {
const res = await api.post('block/get', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: cid.toString(),
...options
})
})

return new Block(Buffer.from(await rsp.arrayBuffer()), cid)
return new Block(Buffer.from(await res.arrayBuffer()), cid)
}
})
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const CID = require('cids')
const multihash = require('multihashes')
const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
async function put (data, options = {}) {
Expand Down Expand Up @@ -36,7 +37,7 @@ module.exports = configure(api => {
const response = await api.post('block/put', {
timeout: options.timeout,
signal: options.signal,
searchParams: options,
searchParams: toUrlSearchParams(options),
...(
await multipartRequest(data)
)
Expand Down
21 changes: 6 additions & 15 deletions packages/ipfs-http-client/src/block/rm.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
'use strict'

const CID = require('cids')
const merge = require('merge-options')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async function * rm (cid, options = {}) {
if (!Array.isArray(cid)) {
cid = [cid]
}

options = merge(
options,
{
'stream-channels': true
}
)

const searchParams = new URLSearchParams(options)

cid.forEach(cid => {
searchParams.append('arg', new CID(cid).toString())
})

const res = await api.post('block/rm', {
timeout: options.timeout,
signal: options.signal,
searchParams: searchParams
searchParams: toUrlSearchParams({
arg: cid.map(cid => new CID(cid).toString()),
'stream-channels': true,
...options
})
})

for await (const removed of res.ndjson()) {
Expand Down
14 changes: 8 additions & 6 deletions packages/ipfs-http-client/src/block/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

const CID = require('cids')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (cid, options = {}) => {
options.arg = (new CID(cid)).toString()

const response = await api.post('block/stat', {
const res = await api.post('block/stat', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: new CID(cid).toString(),
...options
})
})
const res = await response.json()
const data = await res.json()

return { cid: new CID(res.Key), size: res.Size }
return { cid: new CID(data.Key), size: data.Size }
}
})
8 changes: 5 additions & 3 deletions packages/ipfs-http-client/src/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Multiaddr = require('multiaddr')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (addr, options = {}) => {
Expand All @@ -10,12 +11,13 @@ module.exports = configure(api => {
addr = null
}

options.arg = addr

const res = await api.post('bootstrap/add', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: addr,
...options
})
})

return res.json()
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/bootstrap/list.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
const res = await api.post('bootstrap/list', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams(options)
})

return res.json()
Expand Down
8 changes: 5 additions & 3 deletions packages/ipfs-http-client/src/bootstrap/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Multiaddr = require('multiaddr')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (addr, options = {}) => {
Expand All @@ -10,12 +11,13 @@ module.exports = configure(api => {
addr = null
}

options.arg = addr

const res = await api.post('bootstrap/rm', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: addr,
...options
})
})

return res.json()
Expand Down
14 changes: 5 additions & 9 deletions packages/ipfs-http-client/src/cat.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
'use strict'

const CID = require('cids')
const merge = require('merge-options')
const configure = require('./lib/configure')
const toUrlSearchParams = require('./lib/to-url-search-params')

module.exports = configure(api => {
return async function * cat (path, options = {}) {
options = merge(
options,
{
arg: typeof path === 'string' ? path : new CID(path).toString()
}
)

const res = await api.post('cat', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: typeof path === 'string' ? path : new CID(path).toString(),
...options
})
})

yield * res.iterator()
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/commands.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const configure = require('./lib/configure')
const toUrlSearchParams = require('./lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
const res = await api.post('commands', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams(options)
})

return res.json()
Expand Down
10 changes: 7 additions & 3 deletions packages/ipfs-http-client/src/config/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (key, options = {}) => {
Expand All @@ -10,12 +11,15 @@ module.exports = configure(api => {
}

const url = key ? 'config' : 'config/show'
const rsp = await api.post(url, {
const res = await api.post(url, {
timeout: options.timeout,
signal: options.signal,
searchParams: { arg: key }
searchParams: toUrlSearchParams({
arg: key,
...options
})
})
const data = await rsp.json()
const data = await res.json()

return key ? data.Value : data
}
Expand Down
13 changes: 8 additions & 5 deletions packages/ipfs-http-client/src/config/profiles/apply.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use strict'

const configure = require('../../lib/configure')
const toUrlSearchParams = require('../../lib/to-url-search-params')

module.exports = configure(api => {
return async (profile, options = {}) => {
options.arg = profile
const response = await api.post('config/profile/apply', {
const res = await api.post('config/profile/apply', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams({
arg: profile,
...options
})
})
const res = await response.json()
const data = await res.json()

return {
original: res.OldCfg, updated: res.NewCfg
original: data.OldCfg, updated: data.NewCfg
}
}
})
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/config/profiles/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const toCamel = require('../../lib/object-to-camel')
const configure = require('../../lib/configure')
const toUrlSearchParams = require('../../lib/to-url-search-params')

module.exports = configure(api => {
return async (options = {}) => {
const res = await api.post('config/profile/list', {
timeout: options.timeout,
signal: options.signal,
searchParams: options
searchParams: toUrlSearchParams(options)
})

const data = await res.json()
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/config/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
const { Buffer } = require('buffer')
const multipartRequest = require('../lib/multipart-request')
const configure = require('../lib/configure')
const toUrlSearchParams = require('../lib/to-url-search-params')

module.exports = configure(api => {
return async (config, options = {}) => {
const res = await api.post('config/replace', {
timeout: options.timeout,
signal: options.signal,
searchParams: options,
searchParams: toUrlSearchParams(options),
...(
await multipartRequest(Buffer.from(JSON.stringify(config)))
)
Expand Down
Loading

0 comments on commit ab6b976

Please sign in to comment.