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 modules in JavaScript workers #149

Closed
3 tasks done
Angelmmiguel opened this issue May 31, 2023 · 3 comments · Fixed by #236
Closed
3 tasks done

Support modules in JavaScript workers #149

Angelmmiguel opened this issue May 31, 2023 · 3 comments · Fixed by #236
Assignees
Labels
🚀 enhancement New feature or request 🔨 sdks Issues related to language SDKs
Milestone

Comments

@Angelmmiguel
Copy link
Contributor

Angelmmiguel commented May 31, 2023

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

@Angelmmiguel Angelmmiguel added 🚀 enhancement New feature or request 🔨 sdks Issues related to language SDKs labels May 31, 2023
@Angelmmiguel Angelmmiguel added this to the v1.3.0 milestone Jun 2, 2023
@Angelmmiguel Angelmmiguel self-assigned this Jun 2, 2023
@Angelmmiguel Angelmmiguel removed this from the v1.3.0 milestone Jun 14, 2023
@mtt-artis
Copy link
Contributor

Why export an object and not the fetch function directly?
Is it a standard that emerges from other platforms?

@Angelmmiguel
Copy link
Contributor Author

Angelmmiguel commented Aug 21, 2023

Why export an object and not the fetch function directly? Is it a standard that emerges from other platforms?

Yes! One of the goals of wws is to keep compatibility with other platforms. For example, you can take a worker from wws and run it in Cloudflare or Fastly. We're following the WinterCG standard and taking Cloudflare as a reference for the syntax on the JavaScript workers.

In the examples, they use this syntax. I assume they want to return an object open to support future methods / use cases.

@Angelmmiguel
Copy link
Contributor Author

I'm really happy to close this task! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 enhancement New feature or request 🔨 sdks Issues related to language SDKs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants