From 7eee3a4f11b0981097838c470f66030c2e56c597 Mon Sep 17 00:00:00 2001 From: David Ortner Date: Wed, 8 Jan 2025 00:50:42 +0100 Subject: [PATCH] fix: [#1669] Adds null check for if browser frame isnt available in Response during tear down of the Browser --- packages/happy-dom/src/fetch/Response.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/happy-dom/src/fetch/Response.ts b/packages/happy-dom/src/fetch/Response.ts index 04a34219..718ec322 100644 --- a/packages/happy-dom/src/fetch/Response.ts +++ b/packages/happy-dom/src/fetch/Response.ts @@ -105,6 +105,12 @@ export default class Response implements Response { } const browserFrame = new WindowBrowserContext(window).getBrowserFrame(); + + // No browser frame means that the browser is being teared down. + if (!browserFrame) { + return new ArrayBuffer(0); + } + const asyncTaskManager = browserFrame[PropertySymbol.asyncTaskManager]; (this.bodyUsed) = true; @@ -161,6 +167,12 @@ export default class Response implements Response { } const browserFrame = new WindowBrowserContext(window).getBrowserFrame(); + + // No browser frame means that the browser is being teared down. + if (!browserFrame) { + return Buffer.alloc(0); + } + const asyncTaskManager = browserFrame[PropertySymbol.asyncTaskManager]; (this.bodyUsed) = true; @@ -203,6 +215,12 @@ export default class Response implements Response { } const browserFrame = new WindowBrowserContext(window).getBrowserFrame(); + + // No browser frame means that the browser is being teared down. + if (!browserFrame) { + return ''; + } + const asyncTaskManager = browserFrame[PropertySymbol.asyncTaskManager]; (this.bodyUsed) = true; @@ -247,6 +265,12 @@ export default class Response implements Response { public async formData(): Promise { const window = this[PropertySymbol.window]; const browserFrame = new WindowBrowserContext(window).getBrowserFrame(); + + // No browser frame means that the browser is being teared down. + if (!browserFrame) { + return new window.FormData(); + } + const asyncTaskManager = browserFrame[PropertySymbol.asyncTaskManager]; const contentType = this.headers.get('Content-Type');