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: transfer response buffer on safari #1771

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions src/browser/setupWorker/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type ServiceWorkerOutgoingEventTypes =
| 'CLIENT_CLOSED'

export interface StringifiedResponse extends ResponseInit {
body: string | ReadableStream<Uint8Array> | null
body: string | ArrayBuffer | ReadableStream<Uint8Array> | null
}

/**
Expand Down Expand Up @@ -146,7 +146,10 @@ export interface SetupWorkerInternalContext {
ServiceWorkerMessage<EventType, ServiceWorkerIncomingEventsMap[EventType]>
>
}
useFallbackMode: boolean
supports: {
serviceWorkerApi: boolean
readableStreamTransfer: boolean
}
fallbackInterceptor?: Interceptor<HttpRequestEventMap>
}

Expand Down
12 changes: 8 additions & 4 deletions src/browser/setupWorker/setupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { SetupApi } from '~/core/SetupApi'
import { mergeRight } from '~/core/utils/internal/mergeRight'
import { LifeCycleEventsMap } from '~/core/sharedOptions'
import { SetupWorker } from './glossary'
import { supportsReadableStreamTransfer } from '../utils/supportsReadableStreamTransfer'

interface Listener {
target: EventTarget
Expand Down Expand Up @@ -142,8 +143,11 @@ export class SetupWorkerApi
})
},
},
useFallbackMode:
!('serviceWorker' in navigator) || location.protocol === 'file:',
supports: {
serviceWorkerApi:
Copy link
Member Author

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 to supports.serviceWorkerApi since now we check for two things on the side of browser's support.

!('serviceWorker' in navigator) || location.protocol === 'file:',
readableStreamTransfer: supportsReadableStreamTransfer(),
},
}

/**
Expand All @@ -156,11 +160,11 @@ export class SetupWorkerApi
},
})

this.startHandler = context.useFallbackMode
this.startHandler = context.supports.serviceWorkerApi
? createFallbackStart(context)
: createStartHandler(context)

this.stopHandler = context.useFallbackMode
this.stopHandler = context.supports.serviceWorkerApi
? createFallbackStop(context)
: createStop(context)

Expand Down
31 changes: 21 additions & 10 deletions src/browser/setupWorker/start/createRequestListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member Author

Choose a reason for hiding this comment

The 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 ArrayBuffer instead of Blob? I find it to be more correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like it works:
image

Copy link
Member Author

Choose a reason for hiding this comment

The 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 ReadableStream body while transferring that stream isn't supported by their browser. We will still use the fallback to ArrayBuffer but the developer may be confused lest we show a warning of what's happening.

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 }) => {
Expand Down
17 changes: 17 additions & 0 deletions src/browser/utils/supportsReadableStreamTransfer.ts
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
}
}
Loading