From c4f6aa599f9f08c3ea7866058d08d741bcdffd91 Mon Sep 17 00:00:00 2001 From: SameRainbows Date: Thu, 12 Nov 2026 22:22:20 +0300 Subject: [PATCH 1/2] fix: support native Request and Response with Symbol.hasInstance --- src/request.ts | 10 ++++++++++ src/response.ts | 14 ++++++++++++-- test/request.test.ts | 11 +++++++++++ test/response.test.ts | 4 ++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/request.ts b/src/request.ts index 8fd890ea..4d41ca25 100644 --- a/src/request.ts +++ b/src/request.ts @@ -22,6 +22,16 @@ export const toRequestError = (e: unknown): RequestError => { export const GlobalRequest = global.Request export class Request extends GlobalRequest { + static [Symbol.hasInstance](this: unknown, instance: unknown): boolean { + if (this === Request) { + return ( + instance instanceof GlobalRequest || + Function.prototype[Symbol.hasInstance].call(this, instance) + ) + } + return Function.prototype[Symbol.hasInstance].call(this, instance) + } + constructor(input: string | Request, options?: RequestInit) { if (typeof input === 'object' && getRequestCache in input) { // Match native Request behavior: diff --git a/src/response.ts b/src/response.ts index 2ba92cd0..2e45aff8 100644 --- a/src/response.ts +++ b/src/response.ts @@ -22,7 +22,17 @@ interface LightResponse { export const GlobalResponse = global.Response export class Response { #body?: BodyInit | null - #init?: ResponseInit; + #init?: ResponseInit + + static [Symbol.hasInstance](this: unknown, instance: unknown): boolean { + if (this === Response) { + return ( + instance instanceof GlobalResponse || + Function.prototype[Symbol.hasInstance].call(this, instance) + ) + } + return Function.prototype[Symbol.hasInstance].call(this, instance) + } [getResponseCache](): globalThis.Response { // If `cacheKey` has been populated with a live `Headers` instance, the @@ -54,7 +64,7 @@ export class Response { this[getResponseCache]() return } - this.#init = init instanceof Response ? init.#init : init + this.#init = #init in init ? init.#init : init headers = new Headers(init.headers) } else { this.#init = init diff --git a/test/request.test.ts b/test/request.test.ts index 5ce7e957..2e38bd9f 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -1124,6 +1124,17 @@ describe('Request', () => { expect(req).toBeInstanceOf(GlobalRequest) }) + it('should allow native GlobalRequest to be instance of Request', () => { + const globalReq = new GlobalRequest('http://localhost/') + expect(globalReq).toBeInstanceOf(Request) + + class NextRequest extends Request {} + expect(globalReq).not.toBeInstanceOf(NextRequest) + expect(new Request('http://localhost/')).not.toBeInstanceOf(NextRequest) + expect(new NextRequest('http://localhost/')).toBeInstanceOf(NextRequest) + expect(new NextRequest('http://localhost/')).toBeInstanceOf(Request) + }) + it('should be success to create instance from old light weight instance', async () => { const req = newRequest({ method: 'GET', diff --git a/test/response.test.ts b/test/response.test.ts index 48835420..f0ec3cf2 100644 --- a/test/response.test.ts +++ b/test/response.test.ts @@ -55,10 +55,12 @@ describe('Response', () => { // response prototype chain not changed expect(new Response()).toBeInstanceOf(GlobalResponse) + expect(new GlobalResponse()).toBeInstanceOf(Response) // `fetch()` and `Response` are not changed const fetchRes = await fetch(`http://localhost:${port}`) expect(new Response()).toBeInstanceOf(fetchRes.constructor) + expect(fetchRes).toBeInstanceOf(Response) const resJson = await fetchRes.json() expect(fetchRes.headers.get('content-type')).toEqual('application/json charset=UTF-8') expect(resJson).toEqual({ status: 'ok' }) @@ -76,6 +78,8 @@ describe('Response', () => { // support other class to extends from Response expect(new NextResponse()).toBeInstanceOf(Response) + expect(new GlobalResponse()).not.toBeInstanceOf(NextResponse) + expect(new Response()).not.toBeInstanceOf(NextResponse) }) it('Should not lose header data', async () => { From 6ba10f5c6e42c1d499e6ee76696250ecb2de5b2a Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 17 Nov 2026 17:06:53 +0900 Subject: [PATCH 2/2] test: use a native Response as init --- test/response.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/response.test.ts b/test/response.test.ts index f0ec3cf2..5c7785e5 100644 --- a/test/response.test.ts +++ b/test/response.test.ts @@ -82,6 +82,17 @@ describe('Response', () => { expect(new Response()).not.toBeInstanceOf(NextResponse) }) + it('Should use a native Response from fetch() as init', async () => { + const fetchRes = await fetch(`http://localhost:${port}`) + const res = new Response('body', fetchRes) + expect(res.status).toBe(200) + expect(res.headers.get('content-type')).toBe('application/json charset=UTF-8') + expect(await res.text()).toBe('body') + + const created = new Response('body', new GlobalResponse(null, { status: 201 })) + expect(created.status).toBe(201) + }) + it('Should not lose header data', async () => { const parentResponse = new Response('OK', { headers: {