Skip to content

Conversation

@BarryThePenguin
Copy link
Contributor

@BarryThePenguin BarryThePenguin commented Feb 16, 2025

  • Adds a failing test using the documentation as an example
  • Remove undefined headers from ProxyRequestInit
  • Handle duplicate headers from Request and ProxyRequestInit

Fixes #3920

@codecov
Copy link

codecov bot commented Feb 16, 2025

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.31%. Comparing base (e7fbc3e) to head (d52bde4).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/helper/proxy/index.ts 95.65% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3921   +/-   ##
=======================================
  Coverage   91.31%   91.31%           
=======================================
  Files         168      168           
  Lines       10673    10686   +13     
  Branches     3167     3077   -90     
=======================================
+ Hits         9746     9758   +12     
- Misses        926      927    +1     
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@BarryThePenguin BarryThePenguin force-pushed the fix/remove-undefined-proxy-headers branch from 69b33a0 to 32dbe49 Compare February 16, 2025 03:39
@BarryThePenguin BarryThePenguin force-pushed the fix/remove-undefined-proxy-headers branch from 32dbe49 to d52bde4 Compare February 16, 2025 03:55
Comment on lines -90 to -99
const { raw, ...requestInit } = proxyInit ?? {}

const req = new Request(
input,
// @ts-expect-error `headers` in `requestInit` is not compatible with HeadersInit
{
...buildRequestInitFromRequest(raw),
...requestInit,
}
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Re: #3919, my basic understanding of the spread operator is that new objects are created and all the keys are copied. This avoids that extra overhead

Comment on lines +57 to +58
// @ts-expect-error `headers` in `requestInit` is not compatible with HeadersInit
return requestInit
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what lines 94 and 97 were doing previously..

const req = new Request(
input,
// @ts-expect-error `headers` in `requestInit` is not compatible with HeadersInit
{
...buildRequestInitFromRequest(raw),
...requestInit,
}
)

Comment on lines +71 to 72
...requestInit,
headers,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously the headers from buildRequestInitFromRequest(raw) were being overwritten by ...requestInit on line 97

{
...buildRequestInitFromRequest(raw),
...requestInit,
}

Comment on lines +43 to +47
if (value === undefined) {
headers.delete(name)
} else {
headers.append(name, value)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unsure if this is right..

It's deleting a header when the value is undefined, otherwise the value is appended. This means values in both the ProxyRequestInit and the request will be merged, rather than the value from the request overwriting the value from ProxyRequestInit, for example.

})
const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]
expect(req.headers.get('Authorization')).toBeNull()
expect(req.headers.get('X-Request-Id')).toBe('123, 123')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Duplicate request headers using Header#append. Alternatively, using Header#set

Comment on lines 68 to 72
method: request.method,
body: request.body,
duplex: request.body ? 'half' : undefined,
...requestInit,
headers,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at this again, maybe this should actually be..

Suggested change
method: request.method,
body: request.body,
duplex: request.body ? 'half' : undefined,
...requestInit,
headers,
...requestInit,
method: request.method,
body: request.body,
duplex: request.body ? 'half' : undefined,
headers,

..to avoid requestInit overwriting any of method, body, duplex and headers?

@usualoma usualoma mentioned this pull request Feb 16, 2025
3 tasks
@BarryThePenguin BarryThePenguin deleted the fix/remove-undefined-proxy-headers branch February 16, 2025 21:13
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.

helper/proxy not removing undefined headers from ProxyRequestInit

1 participant