Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Safari): send explicit null response bodies #1833

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/browser/setupWorker/start/createRequestListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,31 @@ export const createRequestListener = (
* Check that the browser supports that before sending it to the worker.
*/
if (context.supports.readableStreamTransfer) {
const responseStream = response.body
const responseStreamOrNull = response.body

messageChannel.postMessage(
'MOCK_RESPONSE',
{
...responseInit,
body: responseStream,
body: responseStreamOrNull,
},
responseStream ? [responseStream] : undefined,
responseStreamOrNull ? [responseStreamOrNull] : undefined,
)
} else {
// As a fallback, send the response body buffer to the worker.
const responseBuffer = await responseClone.arrayBuffer()
/**
* @note If we are here, this means the current environment doesn't
* support "ReadableStream" as transferable. In that case,
* attempt to read the non-empty response body as ArrayBuffer, if it's not empty.
* @see https://github.com/mswjs/msw/issues/1827
*/
const responseBufferOrNull =
response.body === null
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the actual fix.

? null
: await responseClone.arrayBuffer()

messageChannel.postMessage('MOCK_RESPONSE', {
...responseInit,
body: responseBuffer,
body: responseBufferOrNull,
})
}

Expand Down
Loading