边缘开发者平台
  • 边缘函数
    • 概述
    • 快速指引
    • 操作指引
      • 函数管理
      • 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

Encoding

基于 Web APIs 标准 TextEncoder、TextDecoder 进行设计,实现了编码器与解码器。

TextEncoder

编码器,接受码点流作为输入,并输出 UTF-8 字节流。请参考 MDN 官方文档 TextEncoder。

构造函数

// TextEncoder 构造函数,不接受任何参数。
const encoder = new TextEncoder();

属性

// encoder.encoding
readonly encoding: string;
编码器的编码类型,当前值仅为 UTF-8。

方法

encode

encoder.encode(input?: string | undefined): Uint8Array
接受码点流作为输入,并输出 UTF-8 字节流。
注意:
input 最大长度为 300M,超出长度会抛出异常。
encoder.encode 参数
参数名称
类型
必填
说明
input
string | undefined
否
编码器输入值。

encodeInto

encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
接受码点流作为输入,输出 UTF-8 字节流,并写入到参数 destination 字节数组中。
参数
参数名称
类型
必填
说明
input
string
是
编码器输入值。
destination
是
编码器输出值。
返回值 EncodeIntoResult
属性名
类型
说明
read
number
已转换为 UTF-8 的 UTF-16 单元数。
written
number
目标 Uint8Array 中修改的字节数。

TextDecoder

解码器。将字节流作为输入,并提供码点流作为输出。请参考 MDN 官方文档 TextDecoder。

构造方法

const decoder = new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextEncoder;

参数

注意:
参数 label ,下述的值暂不支持:
iso-8859-16。
hz-gb-2312。
csiso2022kr,iso-2022-kr。
参数名称
类型
必填
说明
label
string | undefined
否
解码器类型,默认值为 UTF-8。可选的 label 值参考 MDN 官方文档
options
DecoderOptions | undefined
否
解码器配置项

DecoderOptions

解码器配置项如下所示。
属性名
类型
默认值
说明
fatal
boolean
false
标识解码失败时是否抛出异常
ignoreBOM
boolean
false
标识是否忽略 byte-order marker

属性

encoding

// decoder.encoding
readonly encoding: string;
解码器类型。

fatal

// decoder.fatal
readonly fatal: boolean;
当解码失败,标识是否抛出异常。

ignoreBOM

// decoder.ignoreBOM
readonly ignoreBOM: boolean;
标识是否忽略 byte-order marker。

方法

decode

const result = decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
注意:
参数 buffer 最大长度为 100M,超出长度会抛出异常。
参数名称
类型
必填
说明
buffer
否
待解码的字节流。
buffer 最大长度为 100M,超出长度会抛出异。
options
否
执行解码配置项。

DecodeOptions

执行解码配置项如下所示。
属性名
类型
默认值
说明
stream
boolean
false
设置流式解码,默认为 false ,取值说明如下。
true
表示以 chunk 的方式处理数据,即流式解码。
false
表示 chunk 已结束或未使用 chunk 处理数据,即非流式解码。

示例代码

function handleEvent(event) {
// 编码器
const encoder = new TextEncoder();
const encodeText = encoder.encode('hello world');

// 解码器
const decoder = new TextDecoder();
const decodeText = decoder.decode(encodeText);

// 客户端响应内容
const response = new Response(JSON.stringify({
encodeText: encodeText.toString(),
decodeText,
}));

return response;
}

addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event));
});

相关参考