边缘开发者平台
  • 边缘函数
    • 概述
    • 快速指引
    • 操作指引
      • 函数管理
      • Web调试
      • 触发配置
      • 环境变量
      • Code Replica
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • 示例函数
      • 示例概述
      • 获取客户端URL信息
      • 基于客户端地理位置的自定义
      • 获取客户端地理位置信息
      • 批量重定向
      • URL rewrite based on regular expressions
      • 返回 HTML 页面
      • 返回 JSON
      • Fetch 远程资源
      • 请求头鉴权
      • 修改响应头
      • AB 测试
      • 设置 Cookie
      • 基于请求区域重定向
      • Cache API 使用
      • 缓存 POST 请求
      • 流式响应
      • 合并资源流式响应
      • 防篡改校验
      • m3u8 改写与鉴权
      • 图片自适应缩放
      • 图片自适应 WebP
      • 自定义 Referer 限制规则
      • 远程鉴权
      • HMAC 数字签名
      • 自定义下载文件名
      • 获取客户端 IP
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • 最佳实践
      • Overview
      • Origin retrieval based on user IP/geographic location
        • EdgeOne Implementation of Session Persistence Based on Client IP Addresses
        • EdgeOne Implementation of Origin-Pull Based on Client's Geo Location
      • APK dynamic packaging
        • EdgeOne enables dynamic packaging of Android APKs.
          • Feature Overview
          • Step 1: Preprocess the Android APK Parent Package
          • Step 2: Write the Channel Information into the APK Package with EdgeOne Edge Functions
      • Canary Release and Region-specific Execution
      • 通过边缘函数实现自适应图片格式转换
      • Two Ways to Implement CDN Origin-pull Via Edge Function: Fetch and Passthrough
  • KV Storage
    • Overview
    • Operation Guide
  • Edge reasoning
    • Edge Inference Overview
    • Quick Guide

ReadableStream

ReadableStream 可读流,也称为可读端,基于 Web APIs 标准 ReadableStream 进行设计。
注意:
不支持直接构造 ReadableStream 对象,使用 TransformStream 构造得到。

描述

// 使用 TransformStream 构造得到 ReadableStream 对象
const { readable } = new TransformStream();

属性

// readable.locked
readonly locked: boolean;
标识流是否锁定。
说明:
流处于锁定状态的情况有:
一个流最多有一个激活的 reader,在 reader 调用 releaseLock() 方法前,该流均处于锁定状态。
流处于管道传输中,会处于锁定状态,直至管道传输结束。

方法

注意:
使用下述所有方法,要求当前流处于非锁定状态,否则会抛出异常。

getReader

readable.getReader(options?: ReaderOptions): ReadableStreamDefaultReader | ReadableStreamBYOBReader;
创建一个 Reader, 并锁定当前流,直至 Reader 调用 releaseLock() 释放锁。

参数

参数名称
类型
必填
说明
options
是
生成 Reader 的配置项。

ReaderOptions

ReaderOptions 对象属性如下所示。
属性名
类型
必填
说明
mode
string
否
Reader 类型,默认值为 undefined,取值说明如下。
undefined
创建 ReadableStreamDefaultReader 类型的 Reader。
byob
创建 ReadableStreamBYOBReader 类型的 Reader。

pipeThrough

readable.pipeThrough(transfromStream: TransfromStream, options?: PipeToOptions): ReadableStream;
流的管道处理。将当前可读流数据传输到参数 transfromStream 的 writable 端,并返回 transfromStream 的 readable 端。
注意:
在管道传输过程中,会对当前流 writable 端进行锁定。

参数

参数名称
类型
必填
说明
transfromStream
是
当前流传输到的目标对象。
options
是
流处理配置项。

PipeToOptions

流处理配置项如下所示:
属性名
类型
必填
说明
preventClose
boolean
否
取值 true 时,表示可读流的关闭,不会导致可写流关闭。
preventAbort
boolean
否
取值 true 时,表示可读流发生错误,不会导致可写流中止。
preventCancel
boolean
否
取值 true 时,表示可写流的错误,不会导致结束可读流。
signal
否
当 `signal` 被 abort 时,将会中止正在进行的传输。

pipeTo

readable.pipeTo(destination: WritableStream, options?: PipeToOptions): Promise<void>;
流的管道处理,将当前可读流传输到 destination 可写流。
注意:
在管道传输过程中,会对当前流 destination 进行锁定。

参数

参数名称
类型
必填
说明
destination
是
可写流。
options
是
流处理配置项。

tee

readable.tee(): [ReadableStream, ReadableStream];
将当前流派发出两个独立的可读流。

cancel

readable.cancel(reason?: string): Promise<string>;
结束当前流。

相关参考