Skip to content
Myr

fix(proxy): fix header overwrite (#3922)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* header key is case-insensitive * delete header if value is null or undefined

authored by Taku Amano · committed by GitHub · parent e7fbc3e · Browse files · 2 files+48 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+48 added−9 removed

  • tests:+22 added−1 removed
  • other:+26 added−8 removed

2 files+48 added−9 removed

modified:src/helper/proxy/index.test.ts+22 added-1 removed
modified:src/helper/proxy/index.ts+26 added-8 removed
47 unmodified lines
48 headers,49 }50}5152const preprocessRequestInit = (requestInit: RequestInit): RequestInit => {53 if (54 !requestInit.headers ||55 Array.isArray(requestInit.headers) ||56 requestInit.headers instanceof Headers57 ) {58 return requestInit59 }6061 const headers = new Headers()62 for (const [key, value] of Object.entries(requestInit.headers)) {63 if (value == null) {64 // delete header if value is null or undefined65 headers.delete(key)66 } else {67 headers.set(key, value)68 }69 }70 requestInit.headers = headers71 return requestInit72}7374/**75 * Fetch API wrapper for proxy.76 * The parameters and return value are the same as for `fetch` (except for the proxy-specific options).77 *
32 unmodified lines
110 */111export const proxy: ProxyFetch = async (input, proxyInit) => {112 const { raw, ...requestInit } = proxyInit ?? {}11392 const req = new Request(93 input,94 // @ts-expect-error `headers` in `requestInit` is not compatible with HeadersInit95 {96 ...buildRequestInitFromRequest(raw),97 ...requestInit,98 }99 )114 const req = new Request(input, {115 ...buildRequestInitFromRequest(raw),116 ...preprocessRequestInit(requestInit as RequestInit),117 })118 req.headers.delete('accept-encoding')119120 const res = await fetch(req)121 const resHeaders = new Headers(res.headers)
14 unmodified lines