边缘开发者平台
  • 边缘函数
    • 概述
    • 快速指引
    • 操作指引
      • 函数管理
      • Web调试
      • 触发配置
      • 环境变量
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • 示例函数
      • 示例概述
      • 301重定向
      • 获取客户端URL信息
      • 基于客户端地理位置的自定义
      • 获取客户端地理位置信息
      • 批量重定向
      • 返回 HTML 页面
      • 返回 JSON
      • Fetch 远程资源
      • 请求头鉴权
      • 修改响应头
      • AB 测试
      • 设置 Cookie
      • 基于请求区域重定向
      • Cache API 使用
      • 缓存 POST 请求
      • 流式响应
      • 合并资源流式响应
      • 防篡改校验
      • m3u8 改写与鉴权
      • 图片自适应缩放
      • 图片自适应 WebP
      • 自定义 Referer 限制规则
      • 远程鉴权
      • HMAC 数字签名
      • 自定义下载文件名
      • 获取客户端 IP
    • 最佳实践
      • 通过边缘函数实现自适应图片格式转换
当前内容仅提供英语版本,中文版我们将尽快补充,感谢您的理解。

ReadableStream

The ReadableStream API represents a readable stream or readable end. It is designed based on the standard Web API ReadableStream.
Note:
A ReadableStream object cannot be constructed directly. You can use TransformStream to construct a ReadableStream object.

Overview

// Use TransformStream to construct a ReadableStream object.
const { readable } = new TransformStream();

Attributes

// readable.locked
readonly locked: boolean;
The locked attribute indicates whether a stream is locked.
Note:
A stream is locked in the following scenarios:
The stream has no more than one activated reader. Before the reader calls the releaseLock() method, the stream is locked.
The stream is in being piped. The stream is locked until the piping ends.

Methods

Note:
Before you use any of the following methods, make sure that the stream is not locked. Otherwise, an exception is returned.

getReader

readable.getReader(options?: ReaderOptions): ReadableStreamDefaultReader | ReadableStreamBYOBReader;
The getReader() method creates a reader and locks the current stream until the reader calls the releaseLock() method.

Parameters

Parameter
Type
Required
Description
options
Yes
The configuration items for generating the reader.

ReaderOptions

The following table describes the parameters of the ReaderOptions object.
Parameter
Type
Required
Description
mode
string
No
Reader The type of the `reader`. Default value: undefined. Valid values:
undefined
Create a reader of the ReadableStreamDefaultReader type.
byob
Create a reader of the ReadableStreamBYOBReader type.

pipeThrough

readable.pipeThrough(transformStream: TransformStream, options?: PipeToOptions): ReadableStream;
The pipeThrough() method pipes the data of the current readable stream to the writable side of the transformStream and returns the readable side of the transformStream.
Note:
During the piping, the writable side of the current stream is locked.

Parameters

Parameter
Type
Required
Description
transformStream
Yes
The destination to which the current stream is piped.
options
Yes
The configuration items for piping the stream.

PipeToOptions

The following table describes the configuration items for piping the stream.
Parameter
Type
Required
Description
preventClose
boolean
No
If the value is true, the writable stream is not closed along with the readable stream.
preventAbort
boolean
No
If the value is true, the writable stream is not stopped when an exception occurs on the readable stream.
preventCancel
boolean
No
If the value is true, the writable stream is not closed when the readable stream is incorrect.
signal
No
If `signal` is stopped, ongoing pipe operations are stopped.

pipeTo

readable.pipeTo(destination: WritableStream, options?: PipeToOptions): Promise<void>;
The pipeTo() method pipes the current readable stream to the destination writable stream.
Note:
During the piping, the destination of the current stream is locked.

Parameters

Parameter
Type
Required
Description
destination
Yes
The writable stream.
options
Yes
The configuration items for piping the stream.

tee

readable.tee(): [ReadableStream, ReadableStream];
The tee() method tees the current readable stream and returns two independent branches.

cancel

readable.cancel(reason?: string): Promise<string>;
The cancel() method ends the current stream.

References