From 5bf3e3bf5f9a6d32c79410c11abd539ec4c2ddc5 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Fri, 15 Nov 2024 12:53:56 +0100 Subject: [PATCH] fix: support non-configurable responses (#2360) --- package.json | 2 +- pnpm-lock.yaml | 8 ++--- .../start/createResponseListener.ts | 33 ++++++++----------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index b3a40a70c..120573fe2 100644 --- a/package.json +++ b/package.json @@ -143,7 +143,7 @@ "@bundled-es-modules/statuses": "^1.0.1", "@bundled-es-modules/tough-cookie": "^0.1.6", "@inquirer/confirm": "^5.0.0", - "@mswjs/interceptors": "^0.36.5", + "@mswjs/interceptors": "^0.37.0", "@open-draft/deferred-promise": "^2.2.0", "@open-draft/until": "^2.1.0", "@types/cookie": "^0.6.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96e9a96d2..252e22306 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -18,8 +18,8 @@ dependencies: specifier: ^5.0.0 version: 5.0.1(@types/node@18.19.28) '@mswjs/interceptors': - specifier: ^0.36.5 - version: 0.36.10 + specifier: ^0.37.0 + version: 0.37.0 '@open-draft/deferred-promise': specifier: ^2.2.0 version: 2.2.0 @@ -1888,8 +1888,8 @@ packages: - utf-8-validate dev: true - /@mswjs/interceptors@0.36.10: - resolution: {integrity: sha512-GXrJgakgJW3DWKueebkvtYgGKkxA7s0u5B0P5syJM5rvQUnrpLPigvci8Hukl7yEM+sU06l+er2Fgvx/gmiRgg==} + /@mswjs/interceptors@0.37.0: + resolution: {integrity: sha512-lDiHQMCBV9qz8c7+zxaNFQtWWaSogTYkqJ3Pg+FGYYC76nsfSxkMQ0df8fojyz16E+w4vp57NLjN2muNG7LugQ==} engines: {node: '>=18'} dependencies: '@open-draft/deferred-promise': 2.2.0 diff --git a/src/browser/setupWorker/start/createResponseListener.ts b/src/browser/setupWorker/start/createResponseListener.ts index 222635d27..81a70bab9 100644 --- a/src/browser/setupWorker/start/createResponseListener.ts +++ b/src/browser/setupWorker/start/createResponseListener.ts @@ -1,9 +1,9 @@ +import { FetchResponse } from '@mswjs/interceptors' import type { ServiceWorkerIncomingEventsMap, SetupWorkerInternalContext, } from '../glossary' import type { ServiceWorkerMessage } from './utils/createMessageChannel' -import { isResponseWithoutBody } from '@mswjs/interceptors' export function createResponseListener(context: SetupWorkerInternalContext) { return ( @@ -35,32 +35,27 @@ export function createResponseListener(context: SetupWorkerInternalContext) { const response = responseJson.status === 0 ? Response.error() - : new Response( + : new FetchResponse( /** * Responses may be streams here, but when we create a response object * with null-body status codes, like 204, 205, 304 Response will * throw when passed a non-null body, so ensure it's null here * for those codes */ - isResponseWithoutBody(responseJson.status) - ? null - : responseJson.body, - responseJson, + FetchResponse.isResponseWithBody(responseJson.status) + ? responseJson.body + : null, + { + ...responseJson, + /** + * Set response URL if it's not set already. + * @see https://github.com/mswjs/msw/issues/2030 + * @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url + */ + url: request.url, + }, ) - /** - * Set response URL if it's not set already. - * @see https://github.com/mswjs/msw/issues/2030 - * @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url - */ - if (!response.url) { - Object.defineProperty(response, 'url', { - value: request.url, - enumerable: true, - writable: false, - }) - } - context.emitter.emit( responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass', {