vmware-labs / wasm-workers-server

🚀 Develop and run serverless applications on WebAssembly

Home Page:https://workers.wasmlabs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support modules in JavaScript workers

Angelmmiguel opened this issue · comments

Is your feature request related to a problem? Please describe.

At a first approach, the JavaScript workers follow the Web Workers API. It requires to subscribe to a specific event and then, process the request and provide a response:

// Subscribe to the Fetch event
addEventListener("fetch", event => {
return event.respondWith(reply(event.request));
});

However, the current JavaScript SDK emulates this behavior, so it doesn't provide any Web API behind it. In addition to that, other platforms like Cloudflare and Vercel started to simplify this syntax in favor of ECMA modules:

export default {
  async fetch(request) {
    const body = `Hello world!`;
    return new Response(body);
  },
};

This syntax is simpler and more convenient.

Describe the solution you'd like

I want to write workers using the ECMA modules approach, while keeping compatibility with existing modules based on the Web Workers API.

It requires to change the current JavaScript SDK to:

  • Manage ECMA modules
  • Detect the current approach
  • Run the worker to get the response

Describe alternatives you've considered

No response

Additional context

No response