diff --git a/.vscode/settings.json b/.vscode/settings.json index 647e8da..b320871 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,6 @@ ], "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" - } + }, + "typescript.tsdk": "node_modules/typescript/lib" } \ No newline at end of file diff --git a/src/response.ts b/src/response.ts index 2c68b8e..5d4b0ac 100644 --- a/src/response.ts +++ b/src/response.ts @@ -12,39 +12,49 @@ export class Response { #body?: BodyInit | null #init?: ResponseInit + constructor(body?: BodyInit | null, init?: ResponseInit) { + this.#body = body + + if (init instanceof Response) { + this.handleResponseInitFromAnotherResponse(init) + } else { + this.#init = init + } + + this.handleBodyStringOrStream(body, init) + } + private get cache(): typeof GlobalResponse { delete (this as any)[cacheKey] return ((this as any)[responseCache] ||= new GlobalResponse(this.#body, this.#init)) } - constructor(body?: BodyInit | null, init?: ResponseInit) { - this.#body = body - if (init instanceof Response) { - const cachedGlobalResponse = (init as any)[responseCache] - if (cachedGlobalResponse) { - this.#init = cachedGlobalResponse - // instantiate GlobalResponse cache and this object always returns value from global.Response - this.cache - return - } else { - this.#init = init.#init - } + private handleResponseInitFromAnotherResponse(init: Response): void { + const cachedGlobalResponse = (init as any)[responseCache] + + if (cachedGlobalResponse) { + this.#init = cachedGlobalResponse + this.cache } else { - this.#init = init + this.#init = init.#init } + } - if (typeof body === 'string' || body instanceof ReadableStream) { - let headers = (init?.headers || { 'content-type': 'text/plain; charset=UTF-8' }) as - | Record - | Headers - | OutgoingHttpHeaders - if (headers instanceof Headers) { - headers = buildOutgoingHttpHeaders(headers) - } + private handleBodyStringOrStream(body?: BodyInit | null, init?: ResponseInit): void { + const headers = this.buildHeaders(init?.headers) + if (typeof body === 'string' || body instanceof ReadableStream) { ;(this as any)[cacheKey] = [init?.status || 200, body, headers] } } + + private buildHeaders(headers?: HeadersInit): HeadersInit | OutgoingHttpHeaders { + if (headers instanceof Headers) { + return buildOutgoingHttpHeaders(headers) + } + + return headers || { 'content-type': 'text/plain;charset=UTF-8' } + } } ;[ 'body',