Skip to content

Commit 5d54543

Browse files
authored
refactor: code cleanup (#3194)
1 parent cc61ffe commit 5d54543

File tree

5 files changed

+17
-53
lines changed

5 files changed

+17
-53
lines changed

docs/docs/api/Util.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Receives a header object and returns the parsed value.
88

99
Arguments:
1010

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

1313
- **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.
1414

15-
Returns: `Record<string, string | string[]>` If **headers** is an object, it is **headers**. Otherwise, if **obj** is specified, it is equivalent to **obj**.
15+
Returns: `Record<string, string | string[]>` If **obj** is specified, it is equivalent to **obj**.
1616

1717
## `headerNameToString(value)`
1818

lib/web/fetch/index.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,20 +2102,16 @@ async function httpNetworkFetch (
21022102

21032103
const headersList = new HeadersList()
21042104

2105-
// For H2, the rawHeaders are a plain JS object
2106-
// We distinguish between them and iterate accordingly
2107-
if (Array.isArray(rawHeaders)) {
2108-
for (let i = 0; i < rawHeaders.length; i += 2) {
2109-
headersList.append(bufferToLowerCasedHeaderName(rawHeaders[i]), rawHeaders[i + 1].toString('latin1'), true)
2110-
}
2111-
const contentEncoding = headersList.get('content-encoding', true)
2112-
if (contentEncoding) {
2113-
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
2114-
// "All content-coding values are case-insensitive..."
2115-
codings = contentEncoding.toLowerCase().split(',').map((x) => x.trim())
2116-
}
2117-
location = headersList.get('location', true)
2105+
for (let i = 0; i < rawHeaders.length; i += 2) {
2106+
headersList.append(bufferToLowerCasedHeaderName(rawHeaders[i]), rawHeaders[i + 1].toString('latin1'), true)
2107+
}
2108+
const contentEncoding = headersList.get('content-encoding', true)
2109+
if (contentEncoding) {
2110+
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
2111+
// "All content-coding values are case-insensitive..."
2112+
codings = contentEncoding.toLowerCase().split(',').map((x) => x.trim())
21182113
}
2114+
location = headersList.get('location', true)
21192115

21202116
this.body = new Readable({ read: resume })
21212117

@@ -2125,7 +2121,7 @@ async function httpNetworkFetch (
21252121
redirectStatusSet.has(status)
21262122

21272123
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
2128-
if (request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !willFollow) {
2124+
if (codings.length !== 0 && request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !willFollow) {
21292125
for (let i = 0; i < codings.length; ++i) {
21302126
const coding = codings[i]
21312127
// https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2

test/client-request.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ test('request dump with abort signal', async (t) => {
9898
method: 'GET'
9999
}, (err, { body }) => {
100100
t.ifError(err)
101-
let ac
102-
if (!global.AbortController) {
103-
const { AbortController } = require('abort-controller')
104-
ac = new AbortController()
105-
} else {
106-
ac = new AbortController()
107-
}
101+
const ac = new AbortController()
108102
body.dump({ signal: ac.signal }).catch((err) => {
109103
t.strictEqual(err.name, 'AbortError')
110104
server.close()

test/types/util.test-d.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import { expectAssignable } from 'tsd';
22
import { util } from '../../types/util';
33

4-
expectAssignable<Record<string, string | string[]>>(
5-
util.parseHeaders({ 'content-type': 'text/plain' })
6-
);
7-
8-
expectAssignable<Record<string, string | string[]>>(
9-
//@ts-ignore
10-
util.parseHeaders({ 'content-type': 'text/plain' }, {})
11-
);
12-
13-
expectAssignable<Record<string, string | string[]>>(
14-
util.parseHeaders({} as Record<string, string> | string[], {})
15-
);
16-
174
expectAssignable<Record<string, string | string[]>>(
185
util.parseHeaders(['content-type', 'text/plain'])
196
);

types/util.d.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,11 @@ export namespace util {
88
/**
99
* Receives a header object and returns the parsed value.
1010
* @param headers Header object
11+
* @param obj Object to specify a proxy object. Used to assign parsed values.
12+
* @returns If `obj` is specified, it is equivalent to `obj`.
1113
*/
1214
export function parseHeaders(
13-
headers:
14-
| Record<string, string | string[]>
15-
| (Buffer | string | (Buffer | string)[])[]
16-
): Record<string, string | string[]>;
17-
/**
18-
* Receives a header object and returns the parsed value.
19-
* @param headers Header object
20-
* @param obj Object to specify a proxy object. Used to assign parsed values. But, if `headers` is an object, it is not used.
21-
* @returns If `headers` is an object, it is `headers`. Otherwise, if `obj` is specified, it is equivalent to `obj`.
22-
*/
23-
export function parseHeaders<
24-
H extends
25-
| Record<string, string | string[]>
26-
| (Buffer | string | (Buffer | string)[])[]
27-
>(
28-
headers: H,
29-
obj?: H extends any[] ? Record<string, string | string[]> : never
15+
headers: (Buffer | string | (Buffer | string)[])[],
16+
obj?: Record<string, string | string[]>
3017
): Record<string, string | string[]>;
3118
}

0 commit comments

Comments
 (0)