Skip to content

Commit

Permalink
fix: increase default timeout and respect value passed to ky.extend (
Browse files Browse the repository at this point in the history
…ipfs#1130)

* fix: disable timeout if not set

Some of our operations take a really long time, if we don't set a
timeout for `ky` we get the default of 10 seconds. This PR sets
the timeout to `false` if one is not explicitly passed which
disables it.

Nb. I had to add the default to `false` to every invocation. Looking
at the code it should be enough to do it in `src/lib/configure.js`
but it doesn't seem to be.

* fix: use `ignoreUndefined` merge-options option

* chore: update bundle size

* chore: remove git url
  • Loading branch information
achingbrain authored Oct 21, 2019
1 parent c82b031 commit 25b6043
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const echoServer = EchoServer.createServer()
const echoServerStart = promisify(echoServer.start)
const echoServerStop = promisify(echoServer.stop)
module.exports = {
bundlesize: { maxSize: '245kB' },
bundlesize: { maxSize: '246kB' },
webpack: {
resolve: {
mainFields: ['browser', 'main']
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"ky": "^0.15.0",
"ky-universal": "^0.3.0",
"lru-cache": "^5.1.1",
"merge-options": "^1.0.1",
"merge-options": "^2.0.0",
"multiaddr": "^6.0.6",
"multibase": "~0.6.0",
"multicodec": "~0.5.1",
Expand Down
38 changes: 28 additions & 10 deletions src/lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ky = require('ky-universal').default
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
const { toUri } = require('./multiaddr')
const errorHandler = require('./error-handler')
const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })

// Set default configuration and call create function with them
module.exports = create => config => {
Expand All @@ -22,17 +23,26 @@ module.exports = create => config => {
config.apiAddr = config.apiAddr.startsWith('/') ? toUri(config.apiAddr) : config.apiAddr
config.apiPath = config.apiPath || config['api-path'] || '/api/v0'

// TODO configure ky to use config.fetch when this is released:
// https://github.com/sindresorhus/ky/pull/153
const defaults = {
prefixUrl: config.apiAddr + config.apiPath,
timeout: config.timeout || 60000 * 20,
headers: config.headers,
hooks: {
afterResponse: [errorHandler]
}
}
const k = ky.extend(defaults)
const client = ['get', 'post', 'put', 'delete', 'patch', 'head']
.reduce((client, key) => {
client[key] = wrap(k[key], defaults)

return client
}, wrap(k, defaults))

return create({
// TODO configure ky to use config.fetch when this is released:
// https://github.com/sindresorhus/ky/pull/153
ky: ky.extend({
prefixUrl: config.apiAddr + config.apiPath,
timeout: config.timeout || 60 * 1000,
headers: config.headers,
hooks: {
afterResponse: [errorHandler]
}
}),
ky: client,
...config
})
}
Expand All @@ -57,3 +67,11 @@ function getDefaultApiAddr ({ protocol, host, port }) {

return `${protocol || 'http'}://${host || 'localhost'}:${port || 5001}`
}

// returns the passed function wrapped in a function that ignores
// undefined values in the passed `options` object
function wrap (fn, defaults) {
return (input, options) => {
return fn(input, mergeOptions(defaults, options))
}
}

0 comments on commit 25b6043

Please sign in to comment.