Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed May 2, 2024
1 parent e93dbd5 commit d9210e7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 46 deletions.
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
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[]>;
}

0 comments on commit d9210e7

Please sign in to comment.