Skip to content

fix(ClientRequest): add rawHeaders symbol to response listener instance#704

Closed
mikicho wants to merge 4 commits intomainfrom
Michael/pass-raw-headers-to-response-listener
Closed

fix(ClientRequest): add rawHeaders symbol to response listener instance#704
mikicho wants to merge 4 commits intomainfrom
Michael/pass-raw-headers-to-response-listener

Conversation

@mikicho
Copy link
Copy Markdown
Member

@mikicho mikicho commented Feb 4, 2025

@kettanaito I hope I didn't create an abomination 😅 let me know if this is the way and if there is a more elegant way to achieve this.
The problem is that FetchResponse did not use our Response proxy, resulting in no rawHeaders symbol being created in the instance. By utilizing Reflect.construct, I ensured it went through the proxy as intended.

TODO:

  1. There is a type error in the test
  2. should/can we export the kRawHeaders symbol for the test or use getOwnPropertySymbols?
  3. Add proper comments on the extends & Reflect.construct trick if it's a valid option.

@kettanaito
Copy link
Copy Markdown
Member

Hi, @mikicho. Thank you so much for opening this.

The problem is that FetchResponse did not use our Response proxy, resulting in no rawHeaders symbol being created in the instance.

So, is it a timing issue? When FetchResponse is constructed, the proxy for recording headers isn't applied yet? Or something else?


super(finalBody, {
// construct the response manually so it will use the Response proxy for raw headers
const response = Reflect.construct(Response, [finalBody, {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern with this is that it messes up the prototype chain. We can no longer do things like x instanceof FetchResponse because the class constructor returns an arbitrary object (a plain Fetch API Response instance).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

@mikicho mikicho requested a review from kettanaito March 16, 2025 20:38
@mikicho
Copy link
Copy Markdown
Member Author

mikicho commented Mar 16, 2025

@kettanaito WDYT?

@kettanaito kettanaito changed the title fix(ClientRequest): add rawHeaders symbol to response listener instance fix(ClientRequest): add rawHeaders symbol to response listener instance Apr 4, 2025
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('original')
expect(Reflect.get(response.headers, kRawHeaders)).toContainEqual(['x-response-type', 'original'])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I don't quite like about this change is this assertion. We shouldn't tap into internal symbols anywhere.

Is there any other way to check that the fix is working?

Copy link
Copy Markdown
Member Author

@mikicho mikicho Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't think of another way. This is what I plan to do in nock as well, to extract the rawHeaders from the response instance in the response listener.

Do you have another direction, even a general one?

@kettanaito
Copy link
Copy Markdown
Member

@mikicho, this looks good! I have only one comment to share: #704 (comment). Would love for us to update the tests not to reference internal symbols.

@kettanaito kettanaito mentioned this pull request Apr 14, 2025
2 tasks
@kettanaito
Copy link
Copy Markdown
Member

I believe this should be obsolete now that we have #719. Let's test-drive it in Nock and then close this pull request.

@mikicho
Copy link
Copy Markdown
Member Author

mikicho commented Apr 16, 2025

@kettanaito, this is about the response raw headers, not the request's.

@mikicho
Copy link
Copy Markdown
Member Author

mikicho commented Apr 19, 2025

Closing this one favor of the rawResponse approach

@mikicho mikicho closed this Apr 19, 2025
@mikicho mikicho deleted the Michael/pass-raw-headers-to-response-listener branch April 26, 2025 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants