-
-
Notifications
You must be signed in to change notification settings - Fork 527
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
Support ReadableStream as a mocked response body #1336
Comments
Trying to find a common ground API for readable streams.
|
|
Looks like we should be able to construct a readable stream in the handlers with the help of The remaining challenge is how to transfer those streams to the worker. With #1288, I've tried constructing a stream on the client and on the worker, and manually piping it via Chrome supports sending |
Implementing By the way, at this moment the signature for ctx.body permits Lines 10 to 12 in 2633fa9
However using it logs an error with link to this issue. |
This would be a great addition for SSE, this is a constant problem for mocking our frontend and if we could do this it would be much cleaner. |
Looks like the only browser that didn't implement Transferable objects yet is Safari.. not surprising |
Readable steams are already working in the Fetch API branch. Glad to hear it may resolve other issues as well. |
For reference, support Fetch API specification is here: #1436 |
I just tested the v0.0.0-fetch.rc-3 and the which is based on mock test and is a bit different than example in this issue description: https://github.com/mswjs/msw/pull/1436/files#diff-cc6c833284918f1602b3c897fb29381fd3803581ae18da15e795e8612360bef9 Also I guess this is no longer required: Lines 91 to 94 in 2633fa9
@kettanaito Thank You for pointing me into right direction. |
@piotr-cz, I'm glad to hear that. There's a lot more to Fetch API support that it seems. Readable streams support is a fantastic addition to MSW capabilities. I'm working hard on making that release happen sometime this year. As with many things, this also takes longer than expected due to tech debt. |
Hello everyone. Thanks for the Readable streams support. As for now I understand that you pass it as ArrayBuffer to the worker. Novertheless I've encounter some problem on stream cancellation support. Dou you know any workarounds how we can determine from the worker side that the client app closed/cancelled the stream to be able to release access to the stream source? For example, if I have setInterval working to provide stream data and I want to clear it when the client signals that the stream is to be closed. |
Hi, @Yurii-Yanovitsky. The current implementation converts the mock response into ArrayBuffer and sends that to the worker thread in a single message. If I understand correctly, your scenario is:
I don't think I see how that is possible, given that In any case, the current behavior is largely irrelevant because once #1404 lands, the library will be using actual streams between the client and the worker (see this). In that scenario, if you cancel the mocked readable stream mid-response, it should free the resources correctly, since that's going to also cancel the stream that's being sent to the worker. |
Released: v2.0.0 🎉This has been released in v2.0.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Scope
Adds a new behavior
Compatibility
Feature description
Context
Proposal
It'd be great to support
ReadableStream
as a mocked response body.Challenges
ReadableStream
in Node 14-17. Node 17+ has utils like.toWeb()
but I have no idea if they return a compatible API.postMessage()
API (client-worker channel). They can be re-constructed but that leads to its own issues.ReadbleStream
can't be easily polyfilled (I've tried). Its methods likepipeTo
andtea
operate onWritableStream
as input, so this requires that to be polyfilled as well. Suddenly, you're polyfilling half of the browser.API
The main challenge for the API is to allow developers to construct readable streams the same way for browsers and Node. This comes from the principle that the same set of handlers can be used across environments, so handlers mustn't rely on environment-specific API like
window.ReadableStream
.Here's an example of a
ctx.stream()
API that useswindow.ReadableStream
browser API:Resources
TransferableStream
in a worker to respond with itReadableStream
class in NodeThe text was updated successfully, but these errors were encountered: