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

Error when initializing Headers in the JavaScript kit

Angelmmiguel opened this issue · comments

Describe the bug

The JavaScript kit allows you to define the response headers. You can pass a second argument to the Response type that may include a headers key with the response headers. When I pass a Headers object to the the Response, the server returns an error because the response is not properly formatted:

let headers = new Headers({});
return new Response("Hello World", { headers });

When running this example, I get the following error:

<p>There was an error running this function</p>

The output from the worker is the following:

{"data":"Hello World","headers":{"headers":{}},"status":200,"kv":{}}

Reproduction steps

  1. Create a new index.js file with the following content:
const reply = (_request) => {
  // Return the response
  let headers = new Headers({});
  return new Response("Hello World", { headers });
}

// Subscribe to the Fetch event
addEventListener("fetch", event => {
  return event.respondWith(reply(event.request));
});
  1. Run wws
  2. Try to get the response from the worker

Expected behavior

When the Response type receives a Headers instance, it should reuse it instead of trying to build a new Headers instance. This is a potential implementation:

if (options.headers instanceof Headers) {
  this.headers = options.headers;
} else {
  this.headers = new Headers(options.headers || {});
}

I can also check if options.headers is an Object before calling the new Headers method with options.headers.

Additional context

No response