Skip to content

Commit

Permalink
fix: use native fetch if available (#62)
Browse files Browse the repository at this point in the history
If you are in the Electron Renderer process, native fetch is available but it's ignored because the renderer does not respect the browser field of package.json, so we end up with `node-fetch` that uses a browser polyfill of the node `http` module which is very slow on Electron.

Instead use native fetch if it's available or `node-fetch` if not, unless you're on the Electron Main process in which case use `electron-fetch` which uses the Electron `net` module which is faster than the node `http` module in that environment.
  • Loading branch information
achingbrain authored Oct 10, 2020
1 parent 78edc8b commit 9b0ff2f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"./src/text-decoder.js": "./src/text-decoder.browser.js",
"./src/temp-dir.js": "./src/temp-dir.browser.js",
"./src/path-join.js": "./src/path-join.browser.js",
"./test/files/glob-source.spec.js": false
"./test/files/glob-source.spec.js": false,
"@achingbrain/electron-fetch": false
},
"repository": "github:ipfs/js-ipfs-utils",
"scripts": {
Expand All @@ -34,15 +35,18 @@
"license": "MIT",
"dependencies": {
"abort-controller": "^3.0.0",
"any-signal": "^1.1.0",
"any-signal": "^2.1.0",
"buffer": "^5.6.0",
"@achingbrain/electron-fetch": "^1.7.2",
"err-code": "^2.0.0",
"fs-extra": "^9.0.1",
"is-electron": "^2.2.0",
"iso-url": "^0.4.7",
"it-glob": "0.0.8",
"merge-options": "^2.0.0",
"nanoid": "^3.1.3",
"native-abort-controller": "0.0.3",
"native-fetch": "^2.0.0",
"node-fetch": "^2.6.0",
"stream-to-it": "^0.2.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

const { isElectronMain } = require('./env')

if (isElectronMain) {
module.exports = require('@achingbrain/electron-fetch')
} else {
// use window.fetch if it is available, fall back to node-fetch if not
module.exports = require('native-fetch')
}
11 changes: 6 additions & 5 deletions src/http.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-disable no-undef */
'use strict'

const fetch = require('node-fetch')
const {
default: fetch,
Request,
Headers
} = require('./fetch')
const merge = require('merge-options').bind({ ignoreUndefined: true })
const { URL, URLSearchParams } = require('iso-url')
const TextDecoder = require('./text-decoder')
const AbortController = require('abort-controller')
const AbortController = require('native-abort-controller')
const anySignal = require('any-signal')

const Request = fetch.Request
const Headers = fetch.Headers

class TimeoutError extends Error {
constructor () {
super('Request timed out')
Expand Down
2 changes: 1 addition & 1 deletion test/http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { expect } = require('aegir/utils/chai')
const HTTP = require('../src/http')
const toStream = require('it-to-stream')
const delay = require('delay')
const AbortController = require('abort-controller')
const AbortController = require('native-abort-controller')
const drain = require('it-drain')
const all = require('it-all')
const { isBrowser, isWebWorker } = require('../src/env')
Expand Down

0 comments on commit 9b0ff2f

Please sign in to comment.