tencent cloud

Tencent Cloud EdgeOne

Encoding

Download
Focus Mode
Font Size
Last updated: 2026-11-04 10:29:57
AI-Translated & Reviewed
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

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback