-
-
Notifications
You must be signed in to change notification settings - Fork 534
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: transfer response buffer on safari #1771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,18 +47,29 @@ export const createRequestListener = ( | |
// ".log()" method of the request handler. | ||
const responseClone = response.clone() | ||
const responseInit = toResponseInit(response) | ||
const responseStream = responseClone.body | ||
|
||
messageChannel.postMessage( | ||
'MOCK_RESPONSE', | ||
{ | ||
/** | ||
* @note Safari doesn't support transferring a "ReadableStream". | ||
* Check that the browser supports that before sending it to the worker. | ||
*/ | ||
if (context.supports.readableStreamTransfer) { | ||
const responseStream = response.body | ||
messageChannel.postMessage( | ||
'MOCK_RESPONSE', | ||
{ | ||
...responseInit, | ||
body: responseStream, | ||
}, | ||
responseStream ? [responseStream] : undefined, | ||
) | ||
} else { | ||
// As a fallback, send the response body buffer to the worker. | ||
const responseBuffer = await responseClone.arrayBuffer() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thepassle, would this work for your use case if we read the mocked response body as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! In the future, we should print a warning if the developer is mocking a |
||
messageChannel.postMessage('MOCK_RESPONSE', { | ||
...responseInit, | ||
body: responseStream, | ||
}, | ||
// Transfer response's buffer so it could | ||
// be sent over to the worker. | ||
responseStream ? [responseStream] : undefined, | ||
) | ||
body: responseBuffer, | ||
}) | ||
} | ||
|
||
if (!options.quiet) { | ||
context.emitter.once('response:mocked', ({ response }) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Returns a boolean indicating whether the current browser | ||
* supports `ReadableStream` as a `Transferable` when posting | ||
* messages. | ||
*/ | ||
export function supportsReadableStreamTransfer() { | ||
try { | ||
const stream = new ReadableStream({ | ||
start: (controller) => controller.close(), | ||
}) | ||
const message = new MessageChannel() | ||
message.port1.postMessage(stream, [stream]) | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good chance to move
useFallbackMode
tosupports.serviceWorkerApi
since now we check for two things on the side of browser's support.