tus-js-client
tus-js-client copied to clipboard
tus-js-client failing to run in Cloudflare Workers
Describe the bug
I'm attempting to leverage tus for large file uploads using Cloudflare Workers. Simply running
const tus = require("tus-js-client")
yields window is not defined
.
I've tried various hacks like defining window in global scope to an empty object, trying conditional importing, but I can't seem to get around this. Any suggestions?
import * as tus from "tus-js-client";
const tus = require("tus-js-client");
These yield the same result
I have never used Cloudflare Workers, so my knowledge is limited. However, it seems that they implement a limited set of browser-like APIs (such as WebSocket, Web Crypto, Fetch etc) but not a full browser environment. Hence window
is not defined.
So it seems as if tus-js-client is currently not compatible with Cloudflare Workers, but I think it would be possible to achieve support for that. Two things need to be changed at least:
- tus-js-client accesses the window object from time to time for feature detection. It assumes that window exists but with additional checks where window is actually defined, tus-js-client should also run without window being available. Btw, what is the stack trace of your exception?
- tus-js-client uses the XMLHttpRequest API for making requests. Cloudflare Workers does not support it but only the newer Fetch API: https://community.cloudflare.com/t/xmlhttprequest-is-not-defined/133866 It is possible to implement a HTTP Stack for tus-js-client based on the Fetch API, so that tus-js-client can also make HTTP requests inside Cloudflare Workers. See https://github.com/tus/tus-js-client/blob/master/docs/api.md#httpstack
All in all, tus-js-client could run on Workers. Let me know if you are interested in helping with the above points. I don't have enough time to work on them on my own, but I am more than willing to assist you.
Hey @Acconut , apologies for the late response. Thanks for the detailed reply! Once I get a bit of time, I can circle back to this and try and throw up a PR.
@Schachte Great to hear back from you! Let me know if you need further assistance!