|
8 | 8 | * - Please do NOT serve this file on production. |
9 | 9 | */ |
10 | 10 |
|
11 | | -const PACKAGE_VERSION = '2.4.7' |
12 | | -const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423' |
| 11 | +const PACKAGE_VERSION = '2.6.6' |
| 12 | +const INTEGRITY_CHECKSUM = 'ca7800994cc8bfb5eb961e037c877074' |
13 | 13 | const IS_MOCKED_RESPONSE = Symbol('isMockedResponse') |
14 | 14 | const activeClientIds = new Set() |
15 | 15 |
|
@@ -62,7 +62,12 @@ self.addEventListener('message', async function (event) { |
62 | 62 |
|
63 | 63 | sendToClient(client, { |
64 | 64 | type: 'MOCKING_ENABLED', |
65 | | - payload: true, |
| 65 | + payload: { |
| 66 | + client: { |
| 67 | + id: client.id, |
| 68 | + frameType: client.frameType, |
| 69 | + }, |
| 70 | + }, |
66 | 71 | }) |
67 | 72 | break |
68 | 73 | } |
@@ -155,6 +160,10 @@ async function handleRequest(event, requestId) { |
155 | 160 | async function resolveMainClient(event) { |
156 | 161 | const client = await self.clients.get(event.clientId) |
157 | 162 |
|
| 163 | + if (activeClientIds.has(event.clientId)) { |
| 164 | + return client |
| 165 | + } |
| 166 | + |
158 | 167 | if (client?.frameType === 'top-level') { |
159 | 168 | return client |
160 | 169 | } |
@@ -183,12 +192,14 @@ async function getResponse(event, client, requestId) { |
183 | 192 | const requestClone = request.clone() |
184 | 193 |
|
185 | 194 | function passthrough() { |
186 | | - const headers = Object.fromEntries(requestClone.headers.entries()) |
187 | | - |
188 | | - // Remove internal MSW request header so the passthrough request |
189 | | - // complies with any potential CORS preflight checks on the server. |
190 | | - // Some servers forbid unknown request headers. |
191 | | - delete headers['x-msw-intention'] |
| 195 | + // Cast the request headers to a new Headers instance |
| 196 | + // so the headers can be manipulated with. |
| 197 | + const headers = new Headers(requestClone.headers) |
| 198 | + |
| 199 | + // Remove the "accept" header value that marked this request as passthrough. |
| 200 | + // This prevents request alteration and also keeps it compliant with the |
| 201 | + // user-defined CORS policies. |
| 202 | + headers.delete('accept', 'msw/passthrough') |
192 | 203 |
|
193 | 204 | return fetch(requestClone, { headers }) |
194 | 205 | } |
|
0 commit comments