Skip to content

Commit f0738ea

Browse files
committed
refactor: Tweaks newResponse and responsePrototype to be more like the original Response class.
1 parent 5d8da3b commit f0738ea

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

src/listener.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i
1111
const globalResponse = global.Response
1212
const responsePrototype: Record<string, any> = {
1313
getResponseCache() {
14-
this.__cache = undefined
15-
return (this.responseCache ||= new globalResponse(this.body, this.init))
14+
delete this.__cache
15+
return (this.responseCache ||= new globalResponse(this.__body, this.__init))
1616
},
1717
get body() {
1818
return this.getResponseCache().body
@@ -67,19 +67,18 @@ const responsePrototype: Record<string, any> = {
6767
},
6868
}
6969

70-
function newResponse(body: BodyInit | null, init?: ResponseInit): Response {
71-
const res = {
72-
body,
73-
init,
70+
function newResponse(this: Response, body: BodyInit | null, init?: ResponseInit) {
71+
Object.assign(this, {
7472
status: init?.status || 200,
75-
__cache:
76-
typeof body === 'string'
77-
? [body, (init?.headers || {}) as Record<string, string>]
78-
: undefined,
79-
} as unknown as Response
80-
Object.setPrototypeOf(res, responsePrototype)
81-
return res
73+
__body: body,
74+
__init: init,
75+
__cache: [body, (init?.headers || {}) as Record<string, string>],
76+
})
77+
if (typeof body !== 'string') {
78+
delete (this as any).__cache
79+
}
8280
}
81+
newResponse.prototype = responsePrototype
8382
Object.defineProperty(global, 'Response', {
8483
value: newResponse,
8584
})

0 commit comments

Comments
 (0)