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

refactor: code cleanup #3194

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/api/Util.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Receives a header object and returns the parsed value.

Arguments:

- **headers** `Record<string, string | string[]> | (Buffer | string | (Buffer | string)[])[]` (required) - Header object.
- **headers** `(Buffer | string | (Buffer | string)[])[]` (required) - Header object.

- **obj** `Record<string, string | string[]>` (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<string, string | string[]>` If **headers** is an object, it is **headers**. Otherwise, if **obj** is specified, it is equivalent to **obj**.
Returns: `Record<string, string | string[]>` If **obj** is specified, it is equivalent to **obj**.

## `headerNameToString(value)`

Expand Down
24 changes: 10 additions & 14 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 0 additions & 13 deletions test/types/util.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import { expectAssignable } from 'tsd';
import { util } from '../../types/util';

expectAssignable<Record<string, string | string[]>>(
util.parseHeaders({ 'content-type': 'text/plain' })
);

expectAssignable<Record<string, string | string[]>>(
//@ts-ignore
util.parseHeaders({ 'content-type': 'text/plain' }, {})
);

expectAssignable<Record<string, string | string[]>>(
util.parseHeaders({} as Record<string, string> | string[], {})
);

expectAssignable<Record<string, string | string[]>>(
util.parseHeaders(['content-type', 'text/plain'])
);
Expand Down
21 changes: 4 additions & 17 deletions types/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | string[]>
| (Buffer | string | (Buffer | string)[])[]
): Record<string, string | string[]>;
/**
* 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<string, string | string[]>
| (Buffer | string | (Buffer | string)[])[]
>(
headers: H,
obj?: H extends any[] ? Record<string, string | string[]> : never
headers: (Buffer | string | (Buffer | string)[])[],
obj?: Record<string, string | string[]>
): Record<string, string | string[]>;
}
Loading