Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Web Debugging
      • Function Trigger
      • Environment Variable
      • 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
    • Sample Functions
      • Example Overview
      • Obtaining Client URL Information
      • Customization Based on Client Geo Location
      • Obtaining Client Geo Location Information
      • Batch Redirect
      • URL rewrite based on regular expressions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
      • Complex origin-pull URL rewriting
      • Web Bot Auth
    • Practical Tutorial
      • 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
      • Adaptive Image Format Conversion via Edge Functions
      • 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

Designed based on the Web APIs standards TextEncoder and TextDecoder, it implements an encoder and a decoder.

TextEncoder

The encoder receives a stream of code points as input and outputs a UTF-8 byte stream. See the MDN official documentation for TextEncoder.

Constructor

// TextEncoder constructor, which takes no parameters.
new TextEncoder(): TextEncoder;

Properties

// encoder.encoding
readonly encoding: string;
The encoding type of the encoder currently supports only UTF-8.

Methods

encode

encoder.encode(input?: string | undefined): Uint8Array;
It receives a stream of code points as input and outputs a UTF-8 byte stream.
Attention
The maximum length of input is 300M. An exception is thrown if the length is exceeded.
Parameters
Parameter Name
Type
Required
Description
input
string | undefined
No
Encoder input value.

encodeInto

encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
It receives a stream of code points as input, outputs a UTF-8 byte stream, and writes it to the byte array specified by the destination parameter.
Parameters
Parameter Name
Type
Required
Description
input
string
Yes
Encoder input value.
destination
Yes
Encoder output value.
Return Value EncodeIntoResult
Property Name
Type
Description
read
number
Number of UTF-16 units converted to UTF-8.
written
number
Number of bytes modified in the target Uint8Array.

TextDecoder

Decoder. It takes a byte stream as input and provides a stream of code points as output. See the MDN official documentation for TextDecoder.

Constructor

new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextDecoder;

Parameters

Note:
The following values for the label parameter are not supported yet:
iso-8859-16.
hz-gb-2312.
csiso2022kr,iso-2022-kr.
Parameter Name
Type
Required
Description
label
string | undefined
No
Decoder type. The default value is UTF-8. For optional label values, refer to the MDN official documentation.
options
DecoderOptions | undefined
No
Decoder configuration item

DecoderOptions

The decoder configuration options are as follows:
Property Name
Type
Default Value
Description
fatal
boolean
false
Indicates whether to throw an exception when decoding fails.
ignoreBOM
boolean
false
Indicates whether to ignore the byte-order mark.

Properties

encoding

// decoder.encoding
readonly encoding: string;
Decoder type.

fatal

// decoder.fatal
readonly fatal: boolean;
Indicates whether to throw an exception when decoding fails.

ignoreBOM

// decoder.ignoreBOM
readonly ignoreBOM: boolean;
Indicates whether to ignore the byte-order mark.

Methods

decode

decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
Attention
The maximum length of the buffer parameter is 100M. An exception is thrown if the length is exceeded.
Parameters
Parameter Name
Type
Required
Description
buffer
No
Byte stream to be decoded.
The maximum length of the buffer is 100M. An exception is thrown if the length is exceeded.
options
No
Configuration item for performing decoding.
DecodeOptions
The decoding configuration options are as follows:
Property Name
Type
Default Value
Description
stream
boolean
false
Enables streaming decoding. The default value is false. The values are described as follows.
true
Indicates that data is processed in chunk mode, that is, streaming decoding.
false
Indicates that the chunk has ended or data is not processed in chunk mode, that is, non-streaming decoding.

Sample Code

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

// Decoder
const decoder = new TextDecoder();
const decodeText = decoder.decode(encodeText);

// Client response content
const response = new Response(JSON.stringify({
encodeText: encodeText.toString(),
decodeText,
}));

return response;
}

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

References