WritableStreamDefaultWriter
The WritableStreamDefaultWriter interface of Azion Runtime: its constructor, properties, and the methods that write to, close, abort, and release a stream.
The WritableStreamDefaultWriter interface is part of the Streams API of Azion Runtime. A call to WritableStream.getWriter() returns one and locks the stream to it, so no other writer can write to the underlying destination until the lock is released. For more information, refer to WritableStreamDefaultWriter on MDN Web Docs.
Constructor
The WritableStreamDefaultWriter() constructor creates a writer for the stream you pass to it:
The stream parameter is the WritableStream the writer writes to. Calling stream.getWriter() returns an object of the same type.
Properties
| Property | Type | Description |
|---|---|---|
closed | Promise | Fulfills when the stream closes, and rejects when the stream errors or the lock of the writer is released. Use it to run code when the stream ends. |
desiredSize | Number | Size still needed to fill the internal queue of the stream. A stream created with new CountQueuingStrategy({ highWaterMark: 2 }) reports 2 before the first write. |
ready | Promise | Resolves when the desired size of the internal queue changes from zero or less to a positive value. That change signals that the stream no longer applies backpressure. |
Methods
| Method | Description |
|---|---|
writer.abort() | Aborts the stream. The producer can no longer write to it, the stream moves to an error state at once, and queued writes are discarded. |
writer.close() | Closes the writable stream of the writer. Returns a promise. |
writer.releaseLock() | Releases the lock of the writer on its stream, and the writer stops being active. After the call, locked on the stream is false. If the stream is errored when the lock is released, the writer reports the same error from then on; otherwise, it reports the stream as closed. |
writer.write(chunk) | Writes chunk to the stream and its underlying sink. Returns a promise that resolves or rejects to report whether the write succeeded. |
Example
This handler creates a WritableStream whose sink collects every chunk, gets a writer, writes two chunks, and closes the writer. It returns the state of the stream and the writer before the writes, with the chunks the sink received:
The function returns these values. The stream is locked to the writer, and the sink receives both chunks in order: