Stream: jco

Topic: fetch-event usage example


view this post on Zulip Milan (Feb 15 2025 at 16:55):

Is there an example using the fetch-event workflow discussed in componentizejs? It sounds nice and I imagine it means I can create a JS file using service worker fetch events:

addEventListener("fetch", (event) => {
  console.error('We did it!');
  return Response.json({
    "hello": "world"
  });
});

But the only examples I've seen like the recently added jco http example seem to use the raw wasi:http apis

view this post on Zulip Tomasz Andrzejak (Feb 17 2025 at 09:12):

Hey. You can check the e2e tests in the StarlingMonkey repository for an example of fetch API usage. E.g.: https://github.com/bytecodealliance/StarlingMonkey/blob/main/tests/e2e/blob/blob.js

The StarlingMonkey JS runtime. Contribute to bytecodealliance/StarlingMonkey development by creating an account on GitHub.

view this post on Zulip Milan (Feb 18 2025 at 05:30):

Appreciate it! Just the confidence that there are tests was enough to push forward. There are a few issues to file hopefully later this week with jco + componentizejs to make it work smoothly but I was able to get the following to work with a few workarounds:

(addEventListener as ServiceWorkerGlobalScope['addEventListener'])(
  "fetch", (event) =>
  event.respondWith(
    (async () => {
      try {
        const request = await fetch('https://httpbin.org/json');
        const result = await request.text();
        return new Response(`Hello from TypeScript Fetch!, result ${result}`);
      } catch (e) {
        return new Response('whoopsies');
      }
    })(),
  ),
);

Last updated: Feb 27 2025 at 23:03 UTC