Skip to content

Commit

Permalink
Remove dependency on window to work in Web Workers (#660)
Browse files Browse the repository at this point in the history
* Bugfix - replace window global with self

* Adjust changes to better fit linter

* Remove additional references to `window`

---------

Co-authored-by: Marius Kleidl <[email protected]>
  • Loading branch information
rashyad and Acconut authored Dec 21, 2023
1 parent 0a8300f commit 2239aec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 5 additions & 3 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ class Upload extends BaseUpload {
}
}

const { XMLHttpRequest, Blob } = window

const isSupported = XMLHttpRequest && Blob && typeof Blob.prototype.slice === 'function'
// Note: We don't reference `window` here because these classes also exist in a Web Worker's context.
const isSupported =
typeof XMLHttpRequest === 'function' &&
typeof Blob === 'function' &&
typeof Blob.prototype.slice === 'function'

export {
Upload,
Expand Down
1 change: 1 addition & 0 deletions lib/browser/urlStorage.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 4 additions & 8 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 2239aec

Please sign in to comment.