Description
Is your feature request related to a problem? Please describe.
Cloudflare recently introduced the ability to maintain websocket connections with workers, in addition to a stateful storage solution called 'Durable Objects'. For workers, these objects are accessible through a env
argument passed in during the fetch
invocation. More information about durable objects and their utilization can be found here.
https://developers.cloudflare.com/workers/learning/using-durable-objects
Describe the solution you'd like
In my opinion, the cloudflare worker adapter should expose a way to mutate and interact with this env
object passed in by cloudflare during a function invocation.
Describe alternatives you've considered
I have not found any alternate ways of using durable objects without modifying the adapter.
How important is this feature to you?
This feature is fairly important to me personally, since it would dramatically simplify my stack if my state coordination can be done in the same worker.
Activity
DrewRidley commentedon Jun 22, 2021
I can look into this feature in the coming days to see how feasible it would be to introduce it as a PR, however, if the issue requires more significant changes to the adapter I might want to wait and consult some others to learn more about the impact this feature request might have.
DrewRidley commentedon Jun 23, 2021
I have done some more research and found a few areas the adapter can be improved. Namely, the documentation needs a significant upgrade. The documentation is confusing for new users and does not explain what the
toml
file should look like. Secondly, the adapter does not currently usekv-asset-handler
to its full potential and does not set any browser cache headers on the static files served. At the very least, the adapter needs to include some way for users to be able to specify their cache preferences for static files. In light of all of these changes, I have determined that its best to refactor the module to output code in theesm
format, rather than thenode
format previously used. This will have numerous advantages, but the primary motivation for implementation was that it is ESM modules are a prerequisite to using Durable Objects.Unfortunately, I am struggling with getting ESBuild to bundle the dependencies of a sample project in when using
format: 'esm'
.format: node
works perfectly, but when changing the format, the dependency resolution strategy is somehow altered.Any help or clarification would be much appreciated!
DrewRidley commentedon Jun 23, 2021
As a short update;
I managed to get the module syntax fully compiled. I am just waiting for cloudflare to merge my PR of
kv-asset-handler
which introduces support for the module syntax there. The adapter also has some new configuration options so my PR will include some changes to the documentation.Most notably however, my PR will introduce a breaking change. This will likely need to be discussed further with a larger group of the community. The reason I would like to introduce the breaking change is that eventually cloudflare plans to deprecate the old syntax, and force usage of their new modular syntax. For this reason, I think it makes sense for the modular syntax to be the default for all workers created using this adapter.
Luckily, the breaking change in question can easily be mitigated with a clear and concise message informing the users of how to adapt their project to compile, and it only requires a minor two line change to their
wrangler.toml
. In all, I think this breaking change will serve to better the adapters position in providing users with another deployment option for their sveltekit sites.DrewRidley commentedon Jun 23, 2021
I have the code ready for a PR, I just need to wait until this PR is merged first.
cloudflare/kv-asset-handler#200
So I will be following this PR closely and will submit mine here on sveltekit once it has been merged and pushed to NPM
lukeed commentedon Jun 23, 2021
So, few things going on here:
(@DrewRidley and I also spoke in Discord a little bit, so this won't be anything new)
Durable Object (DO) development is heavily tied to the ESM format for Workers. So this means that while DOs are in beta (they still are), the ESM format is in beta too, by extension.
The adapter should 100% be outputting the ESM variant code only – so that the developer has the freedom to opt into DOs if/when they see fit. However, SvelteKit shouldn't be doing this until DO/ESM development is out of beta. Personally, I have no idea if there are other breaking changes on the docket for either of these, but DO is still under "beta" label intentionally, reserving that right for "last-second" changes.
The
kv-asset-handler
package will surely be updated once ESM/DOs are out of beta – maybe before (I don't know). I'm sure DO going into GA will be the signal that a lot of projects were waiting for before making compatibility changes, my personal projects included.@DrewRidley Changing the
platform
option for esbuild changes its resolution paths; see here. I usenone
& control the resolution via hardcodedmainFields
andconditions
values.drozycki commentedon Dec 26, 2021
@lukeed @DrewRidley Durable Objects are now GA. Do you have the PR?
DrewRidley commentedon Jan 26, 2022
Sorry for the delayed response! It looks like the best way to support durable objects is to have a folder of durables that is detected and injected by the adapter. I can have a PR done within the next few days. As for websockets it's probably best that we transparently pass them through in the adapter, and let the user implement them using hooks.
tdreyno commentedon May 4, 2022
Any update on this @DrewRidley?
Rich-Harris commentedon May 12, 2022
adapter-cloudflare-workers
is now ESM-based, and provides access toenv
andcontext
(viaevent.platform
). Aside from inconsistency between dev and build, which is covered by #3535 and #4292, is there anything more to do here?tdreyno commentedon May 12, 2022
I'm interest in this part. A home for the DO classes and getting them compiled correctly. Seemingly need to be concatenated?
tylerbrostrom commentedon May 13, 2022
Problem
Durable Object classes have to be defined as named exports on the
main
module (i.e. the Worker module). Users don’t have a way to edit themain
module provided by the adapter (short of creating a custom adapter).With Wrangler 2, this is no longer an issue.
Solution
Unlike Wrangler 1, Wrangler 2 can resolve es modules. Simply re-export
default
from the outputmain
module like so……and update
wrangler.toml
.Rich-Harris commentedon May 16, 2022
If you change
main
to./custom-worker-entry.js
, then./.cloudflare/worker.js
will no longer be created, so I don't think this will work?It seems like we would probably need a way to tell the adapter where our DOs are defined, so that it can then inject something like
(which I assume works? will confess only passing familiarity with DOs)
34 remaining items