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.encodingreadonly 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 |
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:
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 |
Properties
encoding
// decoder.encodingreadonly encoding: string;
Decoder type.
fatal
// decoder.fatalreadonly fatal: boolean;
Indicates whether to throw an exception when decoding fails.
ignoreBOM
// decoder.ignoreBOMreadonly ignoreBOM: boolean;
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) {// Encoderconst encoder = new TextEncoder();const encodeText = encoder.encode('hello world');// Decoderconst decoder = new TextDecoder();const decodeText = decoder.decode(encodeText);// Client response contentconst response = new Response(JSON.stringify({encodeText: encodeText.toString(),decodeText,}));return response;}addEventListener('fetch', (event) => {event.respondWith(handleEvent(event));});