WritableStream

최신 업데이트: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.

Description

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();

Properties

locked

  • Type: boolean (read-only)
  • Description: Indicates whether the writable stream is locked to a writer.

A stream can be locked in the following situations:

  • A stream can have at most one active writer. The stream remains locked until the writer calls the releaseLock() method.
  • A stream is locked during a pipe operation until the pipe operation completes.

highWaterMark

  • Type: number (read-only)
  • Description: Represents the size of the writable buffer in bytes. The default value is 32KB, and the maximum value is 256KB. If the value exceeds the maximum, it will be automatically adjusted to 256KB.

Methods

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.

Code Example

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.

References

이 문서의 내용이 도움이 되었습니까?
아니오
정상적으로 제출되었습니다.피드백을 주셔서 감사합니다.앞으로도 개선을 위해 노력하겠습니다.