From 586cb4910b3f3fb510817ccadfcaba969e676412 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 21 Oct 2019 15:30:16 +0100 Subject: [PATCH] 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 --- .aegir.js | 2 +- package.json | 2 +- src/lib/configure.js | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/.aegir.js b/.aegir.js index 9a75337d1..847d73a9b 100644 --- a/.aegir.js +++ b/.aegir.js @@ -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'] diff --git a/package.json b/package.json index 7dfe6848a..a67045a26 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "ky": "^0.14.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", diff --git a/src/lib/configure.js b/src/lib/configure.js index a9036d1cd..8401683f9 100644 --- a/src/lib/configure.js +++ b/src/lib/configure.js @@ -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 => { @@ -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 }) } @@ -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)) + } +}