Closed rashyad closed 11 months ago
Thanks for the report! You are right that window
does not exist in the environment for Web Workers. Maybe we should use self
instead for detecting support: https://developer.mozilla.org/en-US/docs/Web/API/Window/self and https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self
The following code could be a replacement without depending on window
, although I didn't test it yet:
const isSupported = typeof self !== undefined && 'XMLHttpRequest' in self && 'Blob' in self && typeof Blob.prototype.slice === 'function'
Could you try it out and open a PR if it works?
Tested your code and it works fine with worker. The test-node
and test-puppeteer
run just fine.
However, I did not manage to create a specific demo for worker or a specific test for worker.
Here is my PR https://github.com/tus/tus-js-client/pull/660
May I know the status of this? Anything that I need to do on my side?
I didn't have the time yet to try it out but I plan on doing so in the next few days
I would also love to get this merged if it would solve this issue. If any additional help is required, I'm happy to lend a hand.
I added a few more commits to your PR and merged it. Can you please test the latest main branch from GitHub and let me know if it works? If so, I can release a new version.
Tested the main branch locally and works fine using with worker
Thanks for the confirmation! I just published v4.0.1, which includes this patch.
Describe the bug The bug is regarding the
window
global object that is available when running in browser. However, when trying to run the upload process in a worker thread on client side, the window object is not defined in the worker global scope.In the file /lib/browser/index.js, line 31 - 33, the code uses the window global object to check for
XMLHttpRequest
andBlob
to determine theisSupported
property.Running this in worker will return
window is not defined
since the code assume thatwindow
is available. I understand the thoughts here since its supposed to be running in browser. I supposed we should put a check instead of assuming thatwindow
is available.To Reproduce
new Worker()
window not defined
error in browser logs. You can copy the upload code part from/demos/browser/demo.js
Expected behavior The upload should work on both the main thread and worker thread on browser. I supposed we should not be assuming that window would always be defined or something..
Just to add, I have it working locally where I modified the file
/lib/browser/index.js
like so,Built the code and it works fine. The upload works on both the main thread and worker thread. However, this might not be the best fix.