Skip to content

Commit

Permalink
fix: support non-configurable responses (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Nov 15, 2024
1 parent c40eac6 commit 5bf3e3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 14 additions & 19 deletions src/browser/setupWorker/start/createResponseListener.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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',
{
Expand Down

0 comments on commit 5bf3e3b

Please sign in to comment.