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

Commit 19dcf6c

Browse files
committed
refactor: replace custom fetch with ky-universal
This change simplifies code related to ad-hoc HTTP requests by switching from custom code to ky-universal. It is a drop-in replacement for fetch that works in browser and node that retries failed request License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 89e957f commit 19dcf6c

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"./src/core/runtime/add-from-fs-nodejs.js": "./src/core/runtime/add-from-fs-browser.js",
2020
"./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js",
2121
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
22-
"./src/core/runtime/fetch-nodejs.js": "./src/core/runtime/fetch-browser.js",
2322
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
2423
"./src/core/runtime/preload-nodejs.js": "./src/core/runtime/preload-browser.js",
2524
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
@@ -121,6 +120,8 @@
121120
"just-flatten-it": "^2.1.0",
122121
"just-safe-set": "^2.1.0",
123122
"kind-of": "^6.0.2",
123+
"ky": "~0.11.2",
124+
"ky-universal": "~0.2.2",
124125
"libp2p": "~0.25.4",
125126
"libp2p-bootstrap": "~0.9.3",
126127
"libp2p-crypto": "~0.16.0",
@@ -146,7 +147,6 @@
146147
"multicodec": "~0.5.1",
147148
"multihashes": "~0.4.14",
148149
"multihashing-async": "~0.6.0",
149-
"node-fetch": "^2.3.0",
150150
"p-queue": "^6.1.0",
151151
"peer-book": "~0.9.0",
152152
"peer-id": "~0.12.3",

src/core/components/files-regular/add-from-url.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { URL } = require('iso-url')
4-
const fetch = require('../../runtime/fetch-nodejs')
4+
const { default: ky } = require('ky-universal')
55

66
module.exports = (self) => {
77
return async (url, options, callback) => {
@@ -14,7 +14,10 @@ module.exports = (self) => {
1414

1515
try {
1616
const parsedUrl = new URL(url)
17-
const res = await fetch(url)
17+
const res = await ky(url, {
18+
timeout: 15000,
19+
retry: 3
20+
})
1821

1922
if (!res.ok) {
2023
throw new Error('unexpected status code: ' + res.status)

src/core/runtime/dns-browser.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const TLRU = require('../../utils/tlru')
55
const { default: PQueue } = require('p-queue')
6+
const { default: ky } = require('ky-universal')
67

78
// Avoid sending multiple queries for the same hostname by caching results
89
const cache = new TLRU(1000)
@@ -46,12 +47,9 @@ module.exports = (domain, opts, callback) => {
4647
url += `&${encodeURIComponent(prop)}=${encodeURIComponent(opts[prop])}`
4748
})
4849

49-
_httpQueue.add(() => fetch(url, { mode: 'cors' })
50-
.then((response) => response.json())
51-
.then((response) => {
52-
cache.set(query, response, ttl)
53-
setImmediate(() => unpackResponse(domain, response, callback))
54-
})
55-
.catch((err) => setImmediate(() => callback(err)))
56-
)
50+
_httpQueue.add(async () => {
51+
const response = await ky(url, { mode: 'cors' }).json()
52+
cache.set(query, response, ttl)
53+
setImmediate(() => unpackResponse(domain, response, callback))
54+
}).catch((err) => setImmediate(() => callback(err)))
5755
}

src/core/runtime/fetch-browser.js

-3
This file was deleted.

src/core/runtime/fetch-nodejs.js

-2
This file was deleted.

src/core/runtime/preload-browser.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
const { default: PQueue } = require('p-queue')
5+
const { default: ky } = require('ky-universal')
56
const debug = require('debug')
67

78
const log = debug('ipfs:preload')
@@ -17,15 +18,14 @@ module.exports = function preload (url, callback) {
1718
const controller = new AbortController()
1819
const signal = controller.signal
1920

20-
_httpQueue.add(() => fetch(url, { signal })
21-
.then(res => {
22-
if (!res.ok) {
23-
log.error('failed to preload', url, res.status, res.statusText)
24-
throw new Error(`failed to preload ${url}`)
25-
}
26-
setImmediate(callback)
27-
})
28-
).catch((err) => setImmediate(() => callback(err)))
21+
_httpQueue.add(async () => {
22+
const res = await ky(url, { signal })
23+
if (!res.ok) {
24+
log.error('failed to preload', url, res.status, res.statusText)
25+
throw new Error(`failed to preload ${url}`)
26+
}
27+
setImmediate(callback)
28+
}).catch((err) => setImmediate(() => callback(err)))
2929

3030
return {
3131
cancel: () => controller.abort()

0 commit comments

Comments
 (0)