|
| 1 | +import { FetchResponse } from '@mswjs/interceptors' |
1 | 2 | import type {
|
2 | 3 | ServiceWorkerIncomingEventsMap,
|
3 | 4 | SetupWorkerInternalContext,
|
4 | 5 | } from '../glossary'
|
5 | 6 | import type { ServiceWorkerMessage } from './utils/createMessageChannel'
|
6 |
| -import { isResponseWithoutBody } from '@mswjs/interceptors' |
7 | 7 |
|
8 | 8 | export function createResponseListener(context: SetupWorkerInternalContext) {
|
9 | 9 | return (
|
@@ -35,32 +35,27 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
|
35 | 35 | const response =
|
36 | 36 | responseJson.status === 0
|
37 | 37 | ? Response.error()
|
38 |
| - : new Response( |
| 38 | + : new FetchResponse( |
39 | 39 | /**
|
40 | 40 | * Responses may be streams here, but when we create a response object
|
41 | 41 | * with null-body status codes, like 204, 205, 304 Response will
|
42 | 42 | * throw when passed a non-null body, so ensure it's null here
|
43 | 43 | * for those codes
|
44 | 44 | */
|
45 |
| - isResponseWithoutBody(responseJson.status) |
46 |
| - ? null |
47 |
| - : responseJson.body, |
48 |
| - responseJson, |
| 45 | + FetchResponse.isResponseWithBody(responseJson.status) |
| 46 | + ? responseJson.body |
| 47 | + : null, |
| 48 | + { |
| 49 | + ...responseJson, |
| 50 | + /** |
| 51 | + * Set response URL if it's not set already. |
| 52 | + * @see https://github.com/mswjs/msw/issues/2030 |
| 53 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url |
| 54 | + */ |
| 55 | + url: request.url, |
| 56 | + }, |
49 | 57 | )
|
50 | 58 |
|
51 |
| - /** |
52 |
| - * Set response URL if it's not set already. |
53 |
| - * @see https://github.com/mswjs/msw/issues/2030 |
54 |
| - * @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url |
55 |
| - */ |
56 |
| - if (!response.url) { |
57 |
| - Object.defineProperty(response, 'url', { |
58 |
| - value: request.url, |
59 |
| - enumerable: true, |
60 |
| - writable: false, |
61 |
| - }) |
62 |
| - } |
63 |
| - |
64 | 59 | context.emitter.emit(
|
65 | 60 | responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
|
66 | 61 | {
|
|
0 commit comments