8track icon indicating copy to clipboard operation
8track copied to clipboard

Support module-based scripts that don't provide FetchEvent

Open Everlag opened this issue 3 years ago • 1 comments

Module-based scripts don't receive a FetchEvent, rather they get a request and other assorted info.

https://developers.cloudflare.com/workers/cli-wrangler/configuration#modules

That makes it harder to use with 8track. So far we've been working with

      const e = ({
        request,
      } as any) as FetchEvent

but that's not great as it steps around type safety.

It'd be great if there was something like getResponseForEvent that took the request only.

Everlag avatar Jun 04 '21 16:06 Everlag

The issue w/ Workers using Durable Objects is that you can't use service-worker syntax with them. In order to export a Durable Object from your worker, you must use JS modules instead.

https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Turns out that the interface to these "targets" are slightly different, where the service-worker wants to create a fetch event handler and accepts a FetchEvent https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent, the modules syntax must export a fetch handler which CF Workers will send a different set of arguments, which are roughly unspecified https://developers.cloudflare.com/workers/learning/using-durable-objects#instantiating-and-communicating-with-a-durable-object.

ddowl avatar Jun 10 '21 20:06 ddowl