The WritableStream interface is a part of the Streams API that provides a standardized way to write streaming data to a sink (destination). It comes with its own queuing and backpressure mechanism to ensure that the data is written in an efficient way.
In an Azion function, a WritableStream lets you produce output incrementally instead of buffering an entire response in memory. This is especially useful when piping or transforming large payloads, because the built-in backpressure keeps the function from overwhelming the destination while data streams through close to the end user.
Constructor
WritableStream() Creates a new writable object.
Instance properties
WritableStream.locked A boolean indicating whether the WritableStream is locked to a writer.
Instance methods
WritableStream.abort() Aborts the stream, signaling that the producer can no longer successfully write to the stream and it’s to be immediately moved to an error state, with any queued writes discarded.
WritableStream.close() Closes the stream.
WritableStream.getWriter() Returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released.
Use cases
- Streaming a large or generated response body to the client chunk by chunk instead of holding it all in memory.
- Acting as the writable end of a
TransformStreamwhen you need to rewrite or filter a payload as it passes through a function. - Piping data from an upstream
ReadableStreaminto a sink while the built-in backpressure throttles the producer to match the consumer. - Aborting an in-progress write with
abort()when an error occurs, so partial or invalid output is discarded cleanly.
Related resources
For more information on WritableStream visit MDN Web Docs.