diff --git a/docs/docs/api/Util.md b/docs/docs/api/Util.md index 2393d079dfc..53b96e3ed3f 100644 --- a/docs/docs/api/Util.md +++ b/docs/docs/api/Util.md @@ -8,11 +8,11 @@ Receives a header object and returns the parsed value. Arguments: -- **headers** `Record | (Buffer | string | (Buffer | string)[])[]` (required) - Header object. +- **headers** `(Buffer | string | (Buffer | string)[])[]` (required) - Header object. - **obj** `Record` (optional) - Object to specify a proxy object. The parsed value is assigned to this object. But, if **headers** is an object, it is not used. -Returns: `Record` If **headers** is an object, it is **headers**. Otherwise, if **obj** is specified, it is equivalent to **obj**. +Returns: `Record` If **obj** is specified, it is equivalent to **obj**. ## `headerNameToString(value)` diff --git a/lib/web/fetch/index.js b/lib/web/fetch/index.js index 82ccb26865c..e7e7acfb0ea 100644 --- a/lib/web/fetch/index.js +++ b/lib/web/fetch/index.js @@ -2102,20 +2102,16 @@ async function httpNetworkFetch ( const headersList = new HeadersList() - // For H2, the rawHeaders are a plain JS object - // We distinguish between them and iterate accordingly - if (Array.isArray(rawHeaders)) { - for (let i = 0; i < rawHeaders.length; i += 2) { - headersList.append(bufferToLowerCasedHeaderName(rawHeaders[i]), rawHeaders[i + 1].toString('latin1'), true) - } - const contentEncoding = headersList.get('content-encoding', true) - if (contentEncoding) { - // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1 - // "All content-coding values are case-insensitive..." - codings = contentEncoding.toLowerCase().split(',').map((x) => x.trim()) - } - location = headersList.get('location', true) + for (let i = 0; i < rawHeaders.length; i += 2) { + headersList.append(bufferToLowerCasedHeaderName(rawHeaders[i]), rawHeaders[i + 1].toString('latin1'), true) + } + const contentEncoding = headersList.get('content-encoding', true) + if (contentEncoding) { + // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1 + // "All content-coding values are case-insensitive..." + codings = contentEncoding.toLowerCase().split(',').map((x) => x.trim()) } + location = headersList.get('location', true) this.body = new Readable({ read: resume }) @@ -2125,7 +2121,7 @@ async function httpNetworkFetch ( redirectStatusSet.has(status) // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding - if (request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !willFollow) { + if (codings.length !== 0 && request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !willFollow) { for (let i = 0; i < codings.length; ++i) { const coding = codings[i] // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2 diff --git a/test/client-request.js b/test/client-request.js index 4071b484c50..2bd38201a72 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -98,13 +98,7 @@ test('request dump with abort signal', async (t) => { method: 'GET' }, (err, { body }) => { t.ifError(err) - let ac - if (!global.AbortController) { - const { AbortController } = require('abort-controller') - ac = new AbortController() - } else { - ac = new AbortController() - } + const ac = new AbortController() body.dump({ signal: ac.signal }).catch((err) => { t.strictEqual(err.name, 'AbortError') server.close() diff --git a/test/types/util.test-d.ts b/test/types/util.test-d.ts index 9879fd31507..1dd59d84108 100644 --- a/test/types/util.test-d.ts +++ b/test/types/util.test-d.ts @@ -1,19 +1,6 @@ import { expectAssignable } from 'tsd'; import { util } from '../../types/util'; -expectAssignable>( - util.parseHeaders({ 'content-type': 'text/plain' }) -); - -expectAssignable>( - //@ts-ignore - util.parseHeaders({ 'content-type': 'text/plain' }, {}) -); - -expectAssignable>( - util.parseHeaders({} as Record | string[], {}) -); - expectAssignable>( util.parseHeaders(['content-type', 'text/plain']) ); diff --git a/types/util.d.ts b/types/util.d.ts index 2a604148fd8..77cf1473a24 100644 --- a/types/util.d.ts +++ b/types/util.d.ts @@ -8,24 +8,11 @@ export namespace util { /** * Receives a header object and returns the parsed value. * @param headers Header object + * @param obj Object to specify a proxy object. Used to assign parsed values. + * @returns If `obj` is specified, it is equivalent to `obj`. */ export function parseHeaders( - headers: - | Record - | (Buffer | string | (Buffer | string)[])[] - ): Record; - /** - * Receives a header object and returns the parsed value. - * @param headers Header object - * @param obj Object to specify a proxy object. Used to assign parsed values. But, if `headers` is an object, it is not used. - * @returns If `headers` is an object, it is `headers`. Otherwise, if `obj` is specified, it is equivalent to `obj`. - */ - export function parseHeaders< - H extends - | Record - | (Buffer | string | (Buffer | string)[])[] - >( - headers: H, - obj?: H extends any[] ? Record : never + headers: (Buffer | string | (Buffer | string)[])[], + obj?: Record ): Record; }