From f97b2462b445e782dd9f8fd28d12af6b9c11a711 Mon Sep 17 00:00:00 2001 From: rashyad Date: Mon, 11 Dec 2023 14:48:06 +0400 Subject: [PATCH 1/3] Bugfix - replace window global with self --- lib/browser/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/browser/index.js b/lib/browser/index.js index 6359507f..049ca533 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -28,9 +28,12 @@ class Upload extends BaseUpload { } } -const { XMLHttpRequest, Blob } = window - -const isSupported = XMLHttpRequest && Blob && typeof Blob.prototype.slice === 'function' +/* eslint-disable no-restricted-globals, valid-typeof */ +const isSupported = + typeof self !== undefined && + 'XMLHttpRequest' in self && + 'Blob' in self && + typeof Blob.prototype.slice === 'function' export { Upload, From 21db4eeb11f30d2bb24e10799ea258f30bc11f17 Mon Sep 17 00:00:00 2001 From: Marius Kleidl Date: Thu, 21 Dec 2023 12:18:21 +0100 Subject: [PATCH 2/3] Adjust changes to better fit linter --- lib/browser/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/browser/index.js b/lib/browser/index.js index 049ca533..a3908545 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -28,11 +28,10 @@ class Upload extends BaseUpload { } } -/* eslint-disable no-restricted-globals, valid-typeof */ +// Note: We don't reference `window` here because these classes also exist in a Web Worker's context. const isSupported = - typeof self !== undefined && - 'XMLHttpRequest' in self && - 'Blob' in self && + typeof XMLHttpRequest === 'function' && + typeof Blob === 'function' && typeof Blob.prototype.slice === 'function' export { From c8942cceadeaa392813cfe7f3255a96ef7cf7294 Mon Sep 17 00:00:00 2001 From: Marius Kleidl Date: Thu, 21 Dec 2023 12:18:37 +0100 Subject: [PATCH 3/3] Remove additional references to `window` --- lib/browser/urlStorage.js | 1 + lib/upload.js | 12 ++++-------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/browser/urlStorage.js b/lib/browser/urlStorage.js index 257a29aa..3def8a76 100644 --- a/lib/browser/urlStorage.js +++ b/lib/browser/urlStorage.js @@ -1,5 +1,6 @@ let hasStorage = false try { + // Note: localStorage does not exist in the Web Worker's context, so we must use window here. hasStorage = 'localStorage' in window // Attempt to store and read entries from the local storage to detect Private diff --git a/lib/upload.js b/lib/upload.js index 61289da4..d632bc94 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -984,14 +984,10 @@ async function sendRequest(req, body, options) { */ function isOnline() { let online = true - if ( - typeof window !== 'undefined' && - // eslint-disable-next-line no-undef - 'navigator' in window && - // eslint-disable-next-line no-undef - window.navigator.onLine === false - ) { - // eslint-disable-line no-undef + // Note: We don't reference `window` here because the navigator object also exists + // in a Web Worker's context. + // eslint-disable-next-line no-undef + if (typeof navigator !== 'undefined' && navigator.onLine === false) { online = false }