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

Error when initializing Headers in the JavaScript kit #233

Closed
Angelmmiguel opened this issue Oct 16, 2023 · 0 comments · Fixed by #235
Closed

Error when initializing Headers in the JavaScript kit #233

Angelmmiguel opened this issue Oct 16, 2023 · 0 comments · Fixed by #235
Assignees
Labels
🐛 bug Something isn't working 🔨 sdks Issues related to language SDKs
Milestone

Comments

@Angelmmiguel
Copy link
Contributor

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

@Angelmmiguel Angelmmiguel added 🐛 bug Something isn't working 🔨 sdks Issues related to language SDKs labels Oct 16, 2023
@Angelmmiguel Angelmmiguel added this to the 1.7.0 milestone Oct 16, 2023
@Angelmmiguel Angelmmiguel self-assigned this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🔨 sdks Issues related to language SDKs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant