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

feat: send mocked response body as ReadableStream to the worker #1288

Merged
merged 5 commits into from Jul 2, 2022

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jun 11, 2022

Changes

  • Transfers mocked response data as a ReadableStream between the client and the worker. This way we support both text and non-text data like Uint8Array to be used as a mocked response body.

Roadmap

  • Add an integration test for ctx.delay() in Node. Removing setTimeout in handleRequest breaks response delays for Node but no tests catch that.
  • Add an integration test for fake timers that ensures that no timer advance is needed if the mocked response uses no delay.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 11, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 65752d0:

Sandbox Source
MSW React Configuration

resolve(transformedResponse as ResponseType)
}, response.delay ?? 0)
})
emitter.emit('request:end', request)
Copy link
Member Author

Choose a reason for hiding this comment

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

Delay is performed in the worker in order to affect the response timings.

@kettanaito kettanaito changed the title wip feat(worker): stream mocked responses Jun 11, 2022
@kettanaito kettanaito changed the title feat(worker): stream mocked responses feat: support ReadableStream response body Jun 12, 2022
}

await sleep(mockResponse.delay)
return resolve(new Response(stream, mockResponse))
Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha

A really important nuance here is to respond with a stream response straight away. This way the client will receive the response immediately and will be able to read the stream as the operation channel keeps pushing the chunks from the response resolver.


if (response) {
await new Promise<void>((resolve) => {
if (!response.delay) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Since I've removed a hard-coded setTimeout around responses in handleRequest, the Node part now needs to delay mocked responses manually. The worker has been always delaying the responses in the worker thread so that the mocked delay counts for server computing time.

@kettanaito
Copy link
Member Author

kettanaito commented Jun 16, 2022

No ctx.stream support possible (yet)

There's no ReadableStream in Node.js prior to version 17. We can't support using readable streams as mocked response bodies until version 17 is the minimal version of Node we support. I've tried writing a polyfill for a readable stream but it also depends on the writable stream for pipeTo/pipeThrough methods; the writable stream needs to know about AbortController, and here you are polyfilling half of the browser standard API.

We can still use a readable stream internally to stream mocked responses from the client to the worker. In Node, we'd have to convert mocked responses to Uint8Array (to preserve binary/buffers in mocks) and feed it to the respective patched modules.

@kettanaito
Copy link
Member Author

For Node, we can convert mocked response bodies to Buffer or similar, achieving the unified response body processing as with the ReadableStream in the browser.

@kettanaito kettanaito changed the title feat: support ReadableStream response body feat: send mocked response body as ReadableStream to the worker Jul 2, 2022
@kettanaito kettanaito marked this pull request as ready for review July 2, 2022 16:11
@kettanaito kettanaito merged commit 78c7d7e into main Jul 2, 2022
@kettanaito kettanaito deleted the feat/readable-stream branch July 2, 2022 18:46
@kettanaito
Copy link
Member Author

Released: v0.43.0 🎉

This has been released in v0.43.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant