Skip to content

Commit 12bc0c0

Browse files
authored
fix: Keep lightweight request even if accessing method, url or headers (#274)
1 parent 4dd3e5e commit 12bc0c0

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/request.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ export class Request extends GlobalRequest {
4242
}
4343
}
4444

45-
export type IncomingMessageWithWrapBodyStream = IncomingMessage & { [wrapBodyStream]: boolean }
46-
export const wrapBodyStream = Symbol('wrapBodyStream')
47-
const newRequestFromIncoming = (
48-
method: string,
49-
url: string,
50-
incoming: IncomingMessage | Http2ServerRequest,
51-
abortController: AbortController
52-
): Request => {
45+
const newHeadersFromIncoming = (incoming: IncomingMessage | Http2ServerRequest) => {
5346
const headerRecord: [string, string][] = []
5447
const rawHeaders = incoming.rawHeaders
5548
for (let i = 0; i < rawHeaders.length; i += 2) {
@@ -58,10 +51,21 @@ const newRequestFromIncoming = (
5851
headerRecord.push([key, value])
5952
}
6053
}
54+
return new Headers(headerRecord)
55+
}
6156

57+
export type IncomingMessageWithWrapBodyStream = IncomingMessage & { [wrapBodyStream]: boolean }
58+
export const wrapBodyStream = Symbol('wrapBodyStream')
59+
const newRequestFromIncoming = (
60+
method: string,
61+
url: string,
62+
headers: Headers,
63+
incoming: IncomingMessage | Http2ServerRequest,
64+
abortController: AbortController
65+
): Request => {
6266
const init = {
6367
method: method,
64-
headers: headerRecord,
68+
headers,
6569
signal: abortController.signal,
6670
} as RequestInit
6771

@@ -116,6 +120,7 @@ const getRequestCache = Symbol('getRequestCache')
116120
const requestCache = Symbol('requestCache')
117121
const incomingKey = Symbol('incomingKey')
118122
const urlKey = Symbol('urlKey')
123+
const headersKey = Symbol('headersKey')
119124
export const abortControllerKey = Symbol('abortControllerKey')
120125
export const getAbortController = Symbol('getAbortController')
121126

@@ -128,6 +133,10 @@ const requestPrototype: Record<string | symbol, any> = {
128133
return this[urlKey]
129134
},
130135

136+
get headers() {
137+
return (this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]))
138+
},
139+
131140
[getAbortController]() {
132141
this[getRequestCache]()
133142
return this[abortControllerKey]
@@ -138,6 +147,7 @@ const requestPrototype: Record<string | symbol, any> = {
138147
return (this[requestCache] ||= newRequestFromIncoming(
139148
this.method,
140149
this[urlKey],
150+
this.headers,
141151
this[incomingKey],
142152
this[abortControllerKey]
143153
))
@@ -149,7 +159,6 @@ const requestPrototype: Record<string | symbol, any> = {
149159
'cache',
150160
'credentials',
151161
'destination',
152-
'headers',
153162
'integrity',
154163
'mode',
155164
'redirect',

test/request.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ describe('Request', () => {
3535
expect(req.keepalive).toBe(false)
3636
})
3737

38+
it('Should not generate GlobalRequest when accessing method, url or headers', async () => {
39+
const req = newRequest({
40+
method: 'GET',
41+
url: '/',
42+
headers: {
43+
host: 'localhost',
44+
},
45+
rawHeaders: ['host', 'localhost'],
46+
} as IncomingMessage)
47+
48+
// keep lightweight request even if accessing method, url or headers
49+
expect(req.method).toBe('GET')
50+
expect(req.url).toBe('http://localhost/')
51+
expect(req.headers.get('host')).toBe('localhost')
52+
expect(req[abortControllerKey]).toBeUndefined()
53+
54+
// generate GlobalRequest
55+
expect(req.keepalive).toBe(false)
56+
expect(req[abortControllerKey]).toBeDefined()
57+
})
58+
3859
it('Should resolve double dots in URL', async () => {
3960
const req = newRequest({
4061
headers: {

0 commit comments

Comments
 (0)