Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on window to work in Web Workers #660

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading