Skip to content

Commit

Permalink
feat: don't store bound global fetch (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR authored Aug 9, 2024
1 parent c31c447 commit 24556d8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion fetch.browser.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = fetch.bind(globalThis)
module.exports = (i, ...r) => globalThis.fetch(i, ...r)
12 changes: 9 additions & 3 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ if (typeof process !== 'undefined' && process) {
// Node.js or Electron with Node.js integration
if (process.type === 'renderer' || process.type === 'worker') {
// Electron renderer with Node.js integration
module.exports = fetch
module.exports = (i, ...r) => globalThis.fetch(i, ...r)
} else {
// Node.js or Electron browser process
module.exports = typeof fetch === 'undefined' ? require('node-fetch') : fetch.bind(globalThis)
if (typeof fetch === 'undefined') {
// Fall back to node-fetch
module.exports = require('node-fetch')
} else {
// Prefer Node.js fetch if exists
module.exports = (i, ...r) => globalThis.fetch(i, ...r)
}
}
} else {
// Browser or Electron without Node.js integration
module.exports = fetch
module.exports = (i, ...r) => globalThis.fetch(i, ...r)
}
8 changes: 7 additions & 1 deletion fetch.native.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
'use strict'

module.exports = typeof fetch === 'undefined' ? require('node-fetch') : fetch.bind(globalThis)
if (typeof fetch === 'undefined') {
// Fall back to node-fetch
module.exports = require('node-fetch')
} else {
// Prefer native / Node.js fetch if exists
module.exports = (i, ...r) => globalThis.fetch(i, ...r)
}
2 changes: 1 addition & 1 deletion fetchival.browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const createFetchival = require('./create-fetchival')

module.exports = createFetchival({
fetch: typeof fetch === 'undefined' ? null : fetch.bind(globalThis),
fetch: typeof fetch === 'undefined' ? null : (i, ...r) => globalThis.fetch(i, ...r),
})

0 comments on commit 24556d8

Please sign in to comment.