In the Node.js Web Streams API, awaiting transformStream.writable.getWriter().write() results in unexpected behavior. #144102
Replies: 2 comments 1 reply
-
It seems like the issue is related to backpressure handling in the Web Streams API. When you await transformStream.writable.getWriter().write(chunk), the await pauses until the stream is ready for more data, which can block subsequent chunks and interrupt real-time flow like SSE. The reason your code blocks when awaiting transformWriter.write() in the loop has to do with the behavior of the WritableStreamDefaultWriter.write() method when used with Node.js streams, especially in the context of SSE. When you await each write call, you’re waiting for it to fully complete before proceeding to the next iteration, effectively serializing the loop in a way that prevents any further execution until the promise resolves. In this case, it causes the loop to stall as each write operation waits for the previous to finish. If you need to manage backpressure (stream readiness), handle it with transformStream.writable.close() after all chunks are written. |
Beta Was this translation helpful? Give feedback.
-
This issue with await transformStream.writable.getWriter().write() in the Node.js Web Streams API might stem from how the writable stream handles backpressure or the state of the stream. If the writable stream is not ready to accept more data, it could lead to unexpected behavior or errors. To troubleshoot, ensure that you're properly managing the writable stream's state. You might want to check if the stream is closed or errored before writing. Additionally, consider using await writer.ready to wait for the stream to be ready before writing again. If you provide more details about the specific behavior you're encountering, I’d be happy to help further! |
Beta Was this translation helpful? Give feedback.
-
App
Next.js
+TS
Feature
A file upload feature allows uploading in chunks using streaming. Each chunk is saved to disk in the format chunk_1, chunk_2..., chunk_n. Once the client has sent all chunks, it sends a merge request. Upon receiving this request, the server performs the merge operation. During the merge process, the server sends progress updates via Server-Sent Events (SSE).
Issue
I'm using
Node.js
Web Streams API
TransformStream
to implement SSE for sending progress updates. Thewrite()
method of thetransformStream.writable.getWriter().write(chunk)
returns a promise, but when I await it, the loop seems to pause, as if it's blocked (without any errors), and the log message only prints once. If I don't await thewrite()
method, everything works as expected, and the functionality runs smoothly. Why does awaiting thewrite()
method cause this issue?Client
API
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions