From 85df5f47e5e138e0c8d5a1f7645eb5a9f2ac1eb4 Mon Sep 17 00:00:00 2001 From: Joe Sweeney Date: Mon, 19 Apr 2021 02:51:13 -0600 Subject: [PATCH 1/3] Use existing fetch if XMLHttpRequest not available --- fetch.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fetch.js b/fetch.js index 5050a247..374cb5d4 100644 --- a/fetch.js +++ b/fetch.js @@ -498,6 +498,14 @@ try { } export function fetch(input, init) { + if (XMLHttpRequest === 'undefined') { + if (globalThis !== 'undefined') { + if ('fetch' in globalThis) { + return globalThis.fetch(input, init) + } + } + } + return new Promise(function(resolve, reject) { var request = new Request(input, init) From 954b5a0230d72f78bd60e6b6061cec11f0b37867 Mon Sep 17 00:00:00 2001 From: Joe Sweeney Date: Mon, 19 Apr 2021 12:57:54 -0600 Subject: [PATCH 2/3] Account for variations in global object --- fetch.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/fetch.js b/fetch.js index 374cb5d4..b5d517e3 100644 --- a/fetch.js +++ b/fetch.js @@ -605,9 +605,19 @@ export function fetch(input, init) { fetch.polyfill = true -if (!global.fetch) { - global.fetch = fetch - global.Headers = Headers - global.Request = Request - global.Response = Response +var getGlobal = function () { + if (typeof globalThis !== 'undefined') { return globalThis } + if (typeof self !== 'undefined') { return self } + if (typeof window !== 'undefined') { return window } + if (typeof global !== 'undefined') { return global } + throw new Error('unable to locate global object') +} + +var globals = getGlobal() + +if (!globals.fetch) { + globals.fetch = fetch + globals.Headers = Headers + globals.Request = Request + globals.Response = Response } From db7451261e9290cc181edba10146028d932cbe8f Mon Sep 17 00:00:00 2001 From: Joe Sweeney Date: Mon, 19 Apr 2021 12:59:33 -0600 Subject: [PATCH 3/3] Remove old attempt at fixing fetch --- fetch.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/fetch.js b/fetch.js index b5d517e3..8f9f27e4 100644 --- a/fetch.js +++ b/fetch.js @@ -498,14 +498,6 @@ try { } export function fetch(input, init) { - if (XMLHttpRequest === 'undefined') { - if (globalThis !== 'undefined') { - if ('fetch' in globalThis) { - return globalThis.fetch(input, init) - } - } - } - return new Promise(function(resolve, reject) { var request = new Request(input, init)