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

Commit 25b6043

Browse files
authored
fix: increase default timeout and respect value passed to ky.extend (#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
1 parent c82b031 commit 25b6043

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

.aegir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const echoServer = EchoServer.createServer()
99
const echoServerStart = promisify(echoServer.start)
1010
const echoServerStop = promisify(echoServer.stop)
1111
module.exports = {
12-
bundlesize: { maxSize: '245kB' },
12+
bundlesize: { maxSize: '246kB' },
1313
webpack: {
1414
resolve: {
1515
mainFields: ['browser', 'main']

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"ky": "^0.15.0",
8282
"ky-universal": "^0.3.0",
8383
"lru-cache": "^5.1.1",
84-
"merge-options": "^1.0.1",
84+
"merge-options": "^2.0.0",
8585
"multiaddr": "^6.0.6",
8686
"multibase": "~0.6.0",
8787
"multicodec": "~0.5.1",

src/lib/configure.js

+28-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ky = require('ky-universal').default
55
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
66
const { toUri } = require('./multiaddr')
77
const errorHandler = require('./error-handler')
8+
const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
89

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

26+
// TODO configure ky to use config.fetch when this is released:
27+
// https://github.com./sindresorhus/ky/pull/153
28+
const defaults = {
29+
prefixUrl: config.apiAddr + config.apiPath,
30+
timeout: config.timeout || 60000 * 20,
31+
headers: config.headers,
32+
hooks: {
33+
afterResponse: [errorHandler]
34+
}
35+
}
36+
const k = ky.extend(defaults)
37+
const client = ['get', 'post', 'put', 'delete', 'patch', 'head']
38+
.reduce((client, key) => {
39+
client[key] = wrap(k[key], defaults)
40+
41+
return client
42+
}, wrap(k, defaults))
43+
2544
return create({
26-
// TODO configure ky to use config.fetch when this is released:
27-
// https://github.com./sindresorhus/ky/pull/153
28-
ky: ky.extend({
29-
prefixUrl: config.apiAddr + config.apiPath,
30-
timeout: config.timeout || 60 * 1000,
31-
headers: config.headers,
32-
hooks: {
33-
afterResponse: [errorHandler]
34-
}
35-
}),
45+
ky: client,
3646
...config
3747
})
3848
}
@@ -57,3 +67,11 @@ function getDefaultApiAddr ({ protocol, host, port }) {
5767

5868
return `${protocol || 'http'}://${host || 'localhost'}:${port || 5001}`
5969
}
70+
71+
// returns the passed function wrapped in a function that ignores
72+
// undefined values in the passed `options` object
73+
function wrap (fn, defaults) {
74+
return (input, options) => {
75+
return fn(input, mergeOptions(defaults, options))
76+
}
77+
}

0 commit comments

Comments
 (0)