-
Notifications
You must be signed in to change notification settings - Fork 161
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
Idiomatic Way to Explicitly Tell an Underlying Transform Stream to Flush to the Sink #960
Comments
Currently the way you'd do this is by having the Zlib transform decide for itself to flush based on a timeout. This is obviously not good as the transform has no context about whether to flush or not. I assume we'd want this to work in a pipe. Consider this pipe: source
.pipeThrough(a)
.pipeThrough(zlib)
.pipeThrough(b)
.pipeTo(destination); Who is responsible for issuing the signal? Only I'm going to assume we get around that problem by telling Sketching what the API would look like, In order for the sync signal to pass through
This is a lot of additional complexity, and we'd need some compelling use cases to justify it. |
My use case is React server rendering. TBH, all my current use cases where gzip/encryption comes up are for using web Streams as the API on the server instead of Node.js streams. While being able to use the same API in a SW. It would be a blocker for adopting the Stream API on the server but not in a browser client or SW. I’m not sure how strong the motivation is to support that use case. One possible solution to this would be to fork/patch the server versions of this API. Eg in Node.js. Others might have more compelling use cases on the client though. |
Server-side use cases are still important. I don't want a permanent fork of the standard on the server-side, but maybe a temporary addition to the API to explore how well it works in the server environment would be a good way to explore this area. |
@sebmarkbage could you give a code example of the scenario in question? Is the scenario something like this? event.respondWith(new Response(someReadableStream.pipeThrough(someBufferingTransform))); And when you say "the source knows that now is a good time to flush to the underlying sink", is "the source" in this example |
Yes specifically it would be something like: let reactStream = React.renderToReadableStream(reactComponents);
event.respondWith(new Response(reactStream.pipeThrough(gzipTransform)); Possibly with an extra The |
Thanks. I was vaguely hoping we could do this as part of the piping process. E.g. using In that case @ricea's sketch seems like the obvious design. I wonder if generalizing it so that you could propagate more messages (of which |
Related: ricea/compressstream-explainer#3. |
This is useful for WebCodecs: https://github.com/WICG/web-codecs/blob/master/explainer.md. So I'm considering increasing the priority of this request. In addition to passing a sync down a pipe from the source, it might also be useful to have a method on TransformStream to be able to start a sync at a particular point in a pipe. @MattiasBuelens can I ask for your opinion on this as another implementer? |
It's probably worth laying out the goals that WebCodes has for a flush implementation:
|
I think I'm not sure about the name. I would have liked |
Bikeshedding in moderation is welcome 😸 My current position is that the use cases are sufficiently compelling to add I'm worried that adding |
More context on how we're using this in React: reactwg/react-18#91 |
Sometimes transform streams keep some internal state and keeps a buffer window. It then waits to flush that buffer to the readable stream until the buffer window is full. Examples of this include compression and encryption libraries.
This can however cause problems when the source stream has to wait a long time for the next piece of data but the receiver can do something useful with it. For example, generating a HTML document that can be displayed in partial pieces, or sending a list of chat messages while waiting for the next message. In that case, the source knows that now is a good time to flush to the underlying sink even if that causes inefficient use of the buffer window.
In Node.js's Zlib module that is solved by exposing an explicit
flush()
method on the Writable. I believe .NET has a similar convention for all Streams.I haven't seen an idiomatic way to solve this with Streams.
This gets particularly awkward with Service Workers because there is no
WritableStream
for Responses. So there isn't really anything to callflush
on. So if the idea is to stick withReadableStream
being the only way, then this use case would need to be modeled by having theReadableStream
communicate this flush signal intent somehow.The text was updated successfully, but these errors were encountered: