#Is it possible to upload a worker script (w/ ESM syntax) using the API?

6 messages · Page 1 of 1 (latest)

tired ore
#

According to the API docs, uploaded scripts have to be in service worker syntax – and I can confirm this: scripts in ESM syntax can be deployed via the browser editor but not via the API.

Is this going to be fixed? Is there a way to work around this?

thanks!

grizzled nova
#

The API fully supports ESM syntax

#

That's what Wrangler and the dashboard use

tired ore
#

Unfortunately, I don't think that's the case. If I try to deploy my script via the API, I get the following error response:

{
  result: null,
  success: false,
  errors: [
    {
      code: 10021,
      message: "Uncaught SyntaxError: Unexpected token 'export'\n  at worker.js:432\n"
    }
  ],
  messages: []
}

If I paste the exact same code into the dashboard editor, it works. Also, the API doc explicitly states that only the service worker syntax is supported:

https://developers.cloudflare.com/api/operations/worker-script-upload-worker-module#request-body

glossy slate
#

It is possible. I upload workers with multiple modules using the API. I define the modules as separate mutipart/form-data components, define the form boundary manually, manually combine the modules as multipart data with the proper heading for each. You will also need to include a meta-data defining the name of your main module and bindings. It appears the documentation has limited info; I figured it with some exprimentation.