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

Support for UploadWorker as a module #794

Closed
mekanoe opened this issue Jan 31, 2022 · 1 comment · Fixed by #1010
Closed

Support for UploadWorker as a module #794

mekanoe opened this issue Jan 31, 2022 · 1 comment · Fixed by #1010

Comments

@mekanoe
Copy link

mekanoe commented Jan 31, 2022

Current cloudflare-go version

0.31.0

Description

Workers supports uploading ES Module syntax now. This might be lightly documented in API docs, but ESM-based workers are uploaded with Content-Type: application/javascript+module, as seen when uploading from the Workers editor in the dashboard.

cloudflare-go doesn't have any way to make the distinction, and would need to be added around here for UploadWorker and here via UploadWorkerWithBindings

The likely best option, in my opinion, is to

  • Add a Module: bool field to WorkerScriptParams
  • Change the call signature of UploadWorker to UploadWorker(ctx context.Context, requestParams *WorkerRequestParams, data *WorkerScriptParams), and ignore the Bindings field.

Attempting to upload a valid worker without this sort of change results in a response:

{
  "code": 10021,
  "message": "Uncaught SyntaxError: Unexpected token 'export'\n  at line 1\n"
}

Use cases

Uploading Worker assets from Terraform

Potential cloudflare-go usage

scriptParams := &WorkerScriptParams{
  Script: `export default { async fetch() { return new Response('hello-world') } }`,
  Bindings: workerBindings, // ignored in UploadWorker()
  Module: true, // changes `application/javascript` to `application/javascript+module`
}

client.UploadWorkerWithBindings(ctx, requestParams, scriptParams)
client.UploadWorker(ctx, requestParams, scriptParams)

References

@moritonal
Copy link

moritonal commented Jul 1, 2022

Just here to agree that this is a infuriating blocker when trying to use Cloud Workers and deploy them via Terraform. Notably because it's the default format outputted by Wrangler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants