Last update:2024-08-14 19:50:29
The WritableStream interface represents a writable stream of data, also known as the writable side of a stream. It is designed based on the Web APIs standard WritableStream interface and can be used in Edge Cloud Apps Functions.
Note: You cannot directly construct a WritableStream object. Use the TransformStream constructor to obtain one.
A WritableStream represents a destination for writing data. It provides methods for writing chunks of data, closing the stream, and aborting the stream.
You can obtain a WritableStream object from a TransformStream as follows:
const { readable, writable } = new TransformStream();
lockedboolean (read-only)A stream can be locked in the following situations:
releaseLock() method.highWaterMarknumber (read-only)Note: All methods below require the current stream to be in an unlocked state; otherwise, an exception will be thrown.
getWriter()writable.getWriter(): WritableStreamDefaultWriter;
Return Value: Returns a WritableStreamDefaultWriter object.
Description: Creates a writer and locks the current stream until the writer calls the releaseLock() method to release the lock.
close()writable.close(): Promise<void>;
Description: Closes the current stream.
abort()writable.abort(reason?: string): Promise<string>;
Parameters:
reason (optional): A string representing the reason for aborting the stream.Description: Aborts the current stream.
The WritableStream object is typically obtained as the writable property of a TransformStream. A common way to write data to a WritableStream is to pipe a ReadableStream to it.
readableStream
.pipeTo(writableStream)
.then(() => console.log('All data successfully written!'))
.catch(e => console.error('Something went wrong!', e));
To write to a WritableStream directly, you must use its writer.
const writer = writableStream.getWriter();
writer.write(data);
Refer to the WritableStreamDefaultWriter documentation for further details.