Skip to content

Commit 2239aec

Browse files
rashyadMarius Kleidl
andauthored
Remove dependency on window to work in Web Workers (#660)
* Bugfix - replace window global with self * Adjust changes to better fit linter * Remove additional references to `window` --------- Co-authored-by: Marius Kleidl <[email protected]>
1 parent 0a8300f commit 2239aec

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

lib/browser/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ class Upload extends BaseUpload {
2828
}
2929
}
3030

31-
const { XMLHttpRequest, Blob } = window
32-
33-
const isSupported = XMLHttpRequest && Blob && typeof Blob.prototype.slice === 'function'
31+
// Note: We don't reference `window` here because these classes also exist in a Web Worker's context.
32+
const isSupported =
33+
typeof XMLHttpRequest === 'function' &&
34+
typeof Blob === 'function' &&
35+
typeof Blob.prototype.slice === 'function'
3436

3537
export {
3638
Upload,

lib/browser/urlStorage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
let hasStorage = false
22
try {
3+
// Note: localStorage does not exist in the Web Worker's context, so we must use window here.
34
hasStorage = 'localStorage' in window
45

56
// Attempt to store and read entries from the local storage to detect Private

lib/upload.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -984,14 +984,10 @@ async function sendRequest(req, body, options) {
984984
*/
985985
function isOnline() {
986986
let online = true
987-
if (
988-
typeof window !== 'undefined' &&
989-
// eslint-disable-next-line no-undef
990-
'navigator' in window &&
991-
// eslint-disable-next-line no-undef
992-
window.navigator.onLine === false
993-
) {
994-
// eslint-disable-line no-undef
987+
// Note: We don't reference `window` here because the navigator object also exists
988+
// in a Web Worker's context.
989+
// eslint-disable-next-line no-undef
990+
if (typeof navigator !== 'undefined' && navigator.onLine === false) {
995991
online = false
996992
}
997993

0 commit comments

Comments
 (0)