From 7ac6b30bdb394552068b549e5d4a2bd95ae48ce5 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 18 Oct 2019 17:43:39 +0100 Subject: [PATCH 1/4] 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. --- src/add/index.js | 2 +- src/block/rm-async-iterator.js | 2 +- src/cat.js | 2 +- src/config/profiles/apply.js | 2 +- src/config/profiles/list.js | 2 +- src/files-regular/get.js | 2 +- src/files-regular/ls.js | 2 +- src/files-regular/refs-local.js | 2 +- src/files-regular/refs.js | 2 +- src/lib/configure.js | 2 +- src/pubsub/ls.js | 2 +- src/pubsub/peers.js | 2 +- src/pubsub/publish.js | 2 +- src/pubsub/subscribe.js | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/add/index.js b/src/add/index.js index ea85a6a0b..86e7acf41 100644 --- a/src/add/index.js +++ b/src/add/index.js @@ -31,7 +31,7 @@ module.exports = configure(({ ky }) => { if (options.preload !== null) searchParams.set('preload', options.preload) const res = await ky.post('add', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams, diff --git a/src/block/rm-async-iterator.js b/src/block/rm-async-iterator.js index b934df32b..ff881a36e 100644 --- a/src/block/rm-async-iterator.js +++ b/src/block/rm-async-iterator.js @@ -24,7 +24,7 @@ module.exports = configure(({ ky }) => { }) const res = await ky.post('block/rm', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams diff --git a/src/cat.js b/src/cat.js index 496257e84..cbd6d5785 100644 --- a/src/cat.js +++ b/src/cat.js @@ -21,7 +21,7 @@ module.exports = configure(({ ky }) => { if (options.length) searchParams.set('length', options.length) const res = await ky.get('cat', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams diff --git a/src/config/profiles/apply.js b/src/config/profiles/apply.js index e4b05560b..a21892b4a 100644 --- a/src/config/profiles/apply.js +++ b/src/config/profiles/apply.js @@ -8,7 +8,7 @@ module.exports = configure(({ ky }) => { options = options || {} const res = await ky.post('config/profile/apply', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams: { diff --git a/src/config/profiles/list.js b/src/config/profiles/list.js index dbfa579cf..f8a360c9b 100644 --- a/src/config/profiles/list.js +++ b/src/config/profiles/list.js @@ -9,7 +9,7 @@ module.exports = configure(({ ky }) => { options = options || {} const res = await ky.get('config/profile/list', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers }) diff --git a/src/files-regular/get.js b/src/files-regular/get.js index 6c942648b..d91d1e0f0 100644 --- a/src/files-regular/get.js +++ b/src/files-regular/get.js @@ -37,7 +37,7 @@ module.exports = configure(({ ky }) => { } const res = await ky.get('get', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams diff --git a/src/files-regular/ls.js b/src/files-regular/ls.js index 0f13f556d..cda4faaac 100644 --- a/src/files-regular/ls.js +++ b/src/files-regular/ls.js @@ -32,7 +32,7 @@ module.exports = configure(({ ky }) => { } const res = await ky.get('ls', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams diff --git a/src/files-regular/refs-local.js b/src/files-regular/refs-local.js index efb8d32d2..7bb712bfc 100644 --- a/src/files-regular/refs-local.js +++ b/src/files-regular/refs-local.js @@ -10,7 +10,7 @@ module.exports = configure(({ ky }) => { options = options || {} const res = await ky.get('refs/local', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers }) diff --git a/src/files-regular/refs.js b/src/files-regular/refs.js index c6136ede5..50b2ea651 100644 --- a/src/files-regular/refs.js +++ b/src/files-regular/refs.js @@ -50,7 +50,7 @@ module.exports = configure(({ ky }) => { } const res = await ky.get('refs', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams diff --git a/src/lib/configure.js b/src/lib/configure.js index a9036d1cd..4a2047840 100644 --- a/src/lib/configure.js +++ b/src/lib/configure.js @@ -27,7 +27,7 @@ module.exports = create => config => { // https://github.com/sindresorhus/ky/pull/153 ky: ky.extend({ prefixUrl: config.apiAddr + config.apiPath, - timeout: config.timeout || 60 * 1000, + timeout: config.timeout || false, headers: config.headers, hooks: { afterResponse: [errorHandler] diff --git a/src/pubsub/ls.js b/src/pubsub/ls.js index 177dcd491..888583914 100644 --- a/src/pubsub/ls.js +++ b/src/pubsub/ls.js @@ -7,7 +7,7 @@ module.exports = configure(({ ky }) => { options = options || {} const { Strings } = await ky.get('pubsub/ls', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams: options.searchParams diff --git a/src/pubsub/peers.js b/src/pubsub/peers.js index bdeca60e4..536ef0fcb 100644 --- a/src/pubsub/peers.js +++ b/src/pubsub/peers.js @@ -15,7 +15,7 @@ module.exports = configure(({ ky }) => { searchParams.set('arg', topic) const { Strings } = await ky.get('pubsub/peers', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams diff --git a/src/pubsub/publish.js b/src/pubsub/publish.js index a41c8fba0..275c840cd 100644 --- a/src/pubsub/publish.js +++ b/src/pubsub/publish.js @@ -12,7 +12,7 @@ module.exports = configure(({ ky }) => { searchParams.set('arg', topic) const res = await ky.post(`pubsub/pub?${searchParams}&arg=${encodeBuffer(data)}`, { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers }).text() diff --git a/src/pubsub/subscribe.js b/src/pubsub/subscribe.js index 7950a274a..5ad9fb7b2 100644 --- a/src/pubsub/subscribe.js +++ b/src/pubsub/subscribe.js @@ -38,7 +38,7 @@ module.exports = configure((config) => { try { res = await ky.post('pubsub/sub', { - timeout: options.timeout, + timeout: options.timeout || false, signal: options.signal, headers: options.headers, searchParams From cc40ce5f7152e2b26c5e128bd0a744fbb814dd9d Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sat, 19 Oct 2019 09:43:11 +0100 Subject: [PATCH 2/4] fix: use `ignoreUndefined` merge-options option --- package.json | 2 +- src/add/index.js | 2 +- src/block/rm-async-iterator.js | 2 +- src/cat.js | 2 +- src/config/profiles/apply.js | 2 +- src/config/profiles/list.js | 2 +- src/files-regular/get.js | 2 +- src/files-regular/ls.js | 2 +- src/files-regular/refs-local.js | 2 +- src/files-regular/refs.js | 2 +- src/lib/configure.js | 38 ++++++++++++++++++++++++--------- src/pubsub/ls.js | 2 +- src/pubsub/peers.js | 2 +- src/pubsub/publish.js | 2 +- src/pubsub/subscribe.js | 2 +- 15 files changed, 42 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 1c446e65f..34d149240 100644 --- a/package.json +++ b/package.json @@ -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": "github:achingbrain/merge-options#add-ignore-undefined-config-option", "multiaddr": "^6.0.6", "multibase": "~0.6.0", "multicodec": "~0.5.1", diff --git a/src/add/index.js b/src/add/index.js index 86e7acf41..ea85a6a0b 100644 --- a/src/add/index.js +++ b/src/add/index.js @@ -31,7 +31,7 @@ module.exports = configure(({ ky }) => { if (options.preload !== null) searchParams.set('preload', options.preload) const res = await ky.post('add', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams, diff --git a/src/block/rm-async-iterator.js b/src/block/rm-async-iterator.js index ff881a36e..b934df32b 100644 --- a/src/block/rm-async-iterator.js +++ b/src/block/rm-async-iterator.js @@ -24,7 +24,7 @@ module.exports = configure(({ ky }) => { }) const res = await ky.post('block/rm', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams diff --git a/src/cat.js b/src/cat.js index cbd6d5785..496257e84 100644 --- a/src/cat.js +++ b/src/cat.js @@ -21,7 +21,7 @@ module.exports = configure(({ ky }) => { if (options.length) searchParams.set('length', options.length) const res = await ky.get('cat', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams diff --git a/src/config/profiles/apply.js b/src/config/profiles/apply.js index a21892b4a..e4b05560b 100644 --- a/src/config/profiles/apply.js +++ b/src/config/profiles/apply.js @@ -8,7 +8,7 @@ module.exports = configure(({ ky }) => { options = options || {} const res = await ky.post('config/profile/apply', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams: { diff --git a/src/config/profiles/list.js b/src/config/profiles/list.js index f8a360c9b..dbfa579cf 100644 --- a/src/config/profiles/list.js +++ b/src/config/profiles/list.js @@ -9,7 +9,7 @@ module.exports = configure(({ ky }) => { options = options || {} const res = await ky.get('config/profile/list', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers }) diff --git a/src/files-regular/get.js b/src/files-regular/get.js index d91d1e0f0..6c942648b 100644 --- a/src/files-regular/get.js +++ b/src/files-regular/get.js @@ -37,7 +37,7 @@ module.exports = configure(({ ky }) => { } const res = await ky.get('get', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams diff --git a/src/files-regular/ls.js b/src/files-regular/ls.js index cda4faaac..0f13f556d 100644 --- a/src/files-regular/ls.js +++ b/src/files-regular/ls.js @@ -32,7 +32,7 @@ module.exports = configure(({ ky }) => { } const res = await ky.get('ls', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams diff --git a/src/files-regular/refs-local.js b/src/files-regular/refs-local.js index 7bb712bfc..efb8d32d2 100644 --- a/src/files-regular/refs-local.js +++ b/src/files-regular/refs-local.js @@ -10,7 +10,7 @@ module.exports = configure(({ ky }) => { options = options || {} const res = await ky.get('refs/local', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers }) diff --git a/src/files-regular/refs.js b/src/files-regular/refs.js index 50b2ea651..c6136ede5 100644 --- a/src/files-regular/refs.js +++ b/src/files-regular/refs.js @@ -50,7 +50,7 @@ module.exports = configure(({ ky }) => { } const res = await ky.get('refs', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams diff --git a/src/lib/configure.js b/src/lib/configure.js index 4a2047840..539c52bbd 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') // 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 || false, - 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.call({ ignoreUndefined: true }, (defaults, options))) + } +} diff --git a/src/pubsub/ls.js b/src/pubsub/ls.js index 888583914..177dcd491 100644 --- a/src/pubsub/ls.js +++ b/src/pubsub/ls.js @@ -7,7 +7,7 @@ module.exports = configure(({ ky }) => { options = options || {} const { Strings } = await ky.get('pubsub/ls', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams: options.searchParams diff --git a/src/pubsub/peers.js b/src/pubsub/peers.js index 536ef0fcb..bdeca60e4 100644 --- a/src/pubsub/peers.js +++ b/src/pubsub/peers.js @@ -15,7 +15,7 @@ module.exports = configure(({ ky }) => { searchParams.set('arg', topic) const { Strings } = await ky.get('pubsub/peers', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams diff --git a/src/pubsub/publish.js b/src/pubsub/publish.js index 275c840cd..a41c8fba0 100644 --- a/src/pubsub/publish.js +++ b/src/pubsub/publish.js @@ -12,7 +12,7 @@ module.exports = configure(({ ky }) => { searchParams.set('arg', topic) const res = await ky.post(`pubsub/pub?${searchParams}&arg=${encodeBuffer(data)}`, { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers }).text() diff --git a/src/pubsub/subscribe.js b/src/pubsub/subscribe.js index 5ad9fb7b2..7950a274a 100644 --- a/src/pubsub/subscribe.js +++ b/src/pubsub/subscribe.js @@ -38,7 +38,7 @@ module.exports = configure((config) => { try { res = await ky.post('pubsub/sub', { - timeout: options.timeout || false, + timeout: options.timeout, signal: options.signal, headers: options.headers, searchParams From 4955d338c3e0867503e7a83ca1e419fc83f5f26c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sat, 19 Oct 2019 11:14:15 +0100 Subject: [PATCH 3/4] chore: update bundle size --- .aegir.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'] From af66a0aef0e11801017ae1b487c85a7be88dffa6 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sat, 19 Oct 2019 19:46:51 +0100 Subject: [PATCH 4/4] chore: remove git url --- package.json | 2 +- src/lib/configure.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 34d149240..4c608ba11 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "ky": "^0.15.0", "ky-universal": "^0.3.0", "lru-cache": "^5.1.1", - "merge-options": "github:achingbrain/merge-options#add-ignore-undefined-config-option", + "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 539c52bbd..8401683f9 100644 --- a/src/lib/configure.js +++ b/src/lib/configure.js @@ -5,7 +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') +const mergeOptions = require('merge-options').bind({ ignoreUndefined: true }) // Set default configuration and call create function with them module.exports = create => config => { @@ -72,6 +72,6 @@ function getDefaultApiAddr ({ protocol, host, port }) { // undefined values in the passed `options` object function wrap (fn, defaults) { return (input, options) => { - return fn(input, mergeOptions.call({ ignoreUndefined: true }, (defaults, options))) + return fn(input, mergeOptions(defaults, options)) } }