WritableStreamDefaultWriter

Last update:2024-08-14 19:50:30

The WritableStreamDefaultWriter interface is used for writable stream operations. It is designed based on the Web APIs standard WritableStreamDefaultWriter interface.

Note: WritableStreamDefaultWriter objects cannot be directly constructed. Use the WritableStream.getWriter method to obtain one.

Description

A writer is used when you want to write directly to a WritableStream rather than piping data to it from a ReadableStream.

You can obtain a WritableStreamDefaultWriter object from a WritableStream as follows:

const { writable } = new TransformStream();
const writer = writable.getWriter();

Properties

closed

  • Type: Promise<void> (read-only)
  • Description: A promise that resolves when the stream is closed and rejects if an error occurs or the writer’s lock is released.

ready

  • Type: Promise<void> (read-only)
  • Description: A promise that resolves when the desired size of the stream’s internal queue changes from negative to positive, indicating that it is no longer applying backpressure.

desiredSize

  • Type: number (read-only)
  • Description: Returns the size, in bytes, required to fill the stream’s internal queue.

Methods

write()

writer.write(chunk: any): Promise<void>;

Parameters:

  • chunk: The data to write to the stream. This can be of various types like string, ArrayBuffer, or ArrayBufferView.

Return Value: Returns a Promise<void> that resolves if the write operation is successful.

Description: Writes the chunk data to the stream.

Note: Calling the write method to initiate another write operation before the previous write operation completes is not allowed.

The underlying stream may accept fewer kinds of types than any. It will throw an exception when encountering an unexpected type.

close()

writer.close(): Promise<void>;

Description: Attempts to close the writer. Remaining writes finish processing before the writer is closed. This method returns a promise that resolves with undefined if the writer successfully closes and processes the remaining writes or is rejected on any error.

abort()

writer.abort(reason?: string): Promise<string>;

Parameters:

  • reason (optional): A string representing the reason for aborting the stream.

Description: Aborts the stream. This method returns a promise that resolves with a response undefined. The reason is passed to the underlying sink’s abort algorithm. If this writable stream is one side of a TransformStream, then its abort algorithm causes the transform’s readable side to become errored with the reason.

Warning: Any data not yet written is lost upon abort.

releaseLock()

writer.releaseLock(): void;

Description: Releases the writer’s lock on the stream. Once released, the writer is no longer active. You can call this method before all pending write(chunk) calls are resolved. This allows you to queue a write operation, release the lock, and begin piping into the writable stream from another source, as shown in the example below:

let writer = writable.getWriter();
// Write a preamble.
writer.write(new TextEncoder().encode('foo bar'));
// While that’s still writing, pipe the rest of the body from somewhere else.
writer.releaseLock();
await someResponse.body.pipeTo(writable);

Code Example

function writeArrayToStream(array, writableStream) {
  const writer = writableStream.getWriter();
  array.forEach(chunk => writer.write(chunk).catch(() => {}));

  return writer.close();
}

writeArrayToStream([1, 2, 3, 4, 5], writableStream)
  .then(() => console.log('All done!'))
  .catch(e => console.error('Error with the stream: ' + e));

References

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!