大网数据广告活动
Web 标准与前端开发

ReadableByteStreamController

· 52CE 我爱测

The **`ReadableByteStreamController`** interface of the Streams API represents a controller for a readable byte stream. It allows control of the state and internal queue of a ReadableStream with an underlying byte source, and enables efficient zero-copy transfer of data from the

Streams

The ReadableByteStreamController interface of the Streams API represents a controller for a readable byte stream. It allows control of the state and internal queue of a ReadableStream with an underlying byte source, and enables efficient zero-copy transfer of data from the underlying source to a consumer when the stream's internal queue is empty.

An instance of this controller type is created if an underlyingSource object with the property type="bytes" is passed as an argument to the ReadableStream() constructor. The underlyingSource object may also define start() and pull() callback functions. These are called with the controller as a parameter, in order to set up the underlying source, and request data when needed.

The underlying source uses the controller to supply data to the stream via its byobRequest property or enqueue() method. byobRequest is a ReadableStreamBYOBRequest object that represents a pending request from a consumer to make a zero-copy transfer of data direct to a consumer. byobRequest must be used to copy data if it exists (do not use enqueue() in this case)! If the underlying source needs to pass data to the stream and byobRequest is null then the source can call enqueue() to add the data to the stream's internal queues.

Note that the byobRequest is only created in "BYOB mode" when there is a request from a reader and the stream's internal queue is empty. "BYOB mode" is enabled when using a ReadableStreamBYOBReader (typically constructed by calling ReadableStream.getReader() with the argument { mode: 'byob' }). It is also enabled when using a default reader and autoAllocateChunkSize is specified in the ReadableStream() constructor.

An underlying byte source can also use the controller to close() the stream when all the data has been sent and report errors from the underlying source using error(). The controller's desiredSize property is used to apply "backpressure", informing the underlying source of the size of the internal queue (small values indicate that the queue is filling up, hinting to the underlying source that it is be desirable to pause or throttle the inflow).

Note that even though the controller is primarily used by the underlying byte source, there is no reason it cannot be stored used by other parts of the system to signal the stream.

Constructor

None. ReadableByteStreamController instances are automatically created if an underlyingSource with the property type="bytes" is passed to the ReadableStream() constructor.

Instance properties

  • ReadableByteStreamController.byobRequest
    • : Returns the current BYOB pull request, or null if there no outstanding request.
  • ReadableByteStreamController.desiredSize
    • : Returns the desired size required to fill the stream's internal queue.

Instance methods

  • ReadableByteStreamController.close()
    • : Closes the associated stream.
  • ReadableByteStreamController.enqueue()
    • : Enqueues a given chunk in the associated stream.
  • ReadableByteStreamController.error()
    • : Causes any future interactions with the associated stream to error.

Examples

The controller is used by an underlying source to transfer or enqueue data, to signal that the stream has no more data (has closed) or has errored. It is also used to signal the underlying source from "upstream" of the desired data rate, using ReadableByteStreamController.desiredSize.

The example in Using readable byte streams, in particular Creating a readable socket push byte stream, show most of these cases.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。