UTF-8 byte stream. See the MDN official documentation for TextEncoder. // TextEncoder constructor, which takes no parameters.new TextEncoder(): TextEncoder;
// encoder.encodingreadonly encoding: string;
UTF-8.encoder.encode(input?: string | undefined): Uint8Array;
UTF-8 byte stream.Parameter Name | Type | Required | Description |
input | string | undefined | No | Encoder input value. |
encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
UTF-8 byte stream, and writes it to the byte array specified by the destination parameter.Parameter Name | Type | Required | Description |
input | string | Yes | Encoder input value. |
destination | Yes | Encoder output value. |
Property Name | Type | Description |
read | number | Number of UTF-16 units converted to UTF-8. |
written | number |
new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextDecoder;
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 |
Property Name | Type | Default Value | Description |
fatal | boolean | false | Indicates whether to throw an exception when decoding fails. |
ignoreBOM | boolean | false |
// decoder.encodingreadonly encoding: string;
// decoder.fatalreadonly fatal: boolean;
// decoder.ignoreBOMreadonly ignoreBOM: boolean;
decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
buffer parameter is 100M. An exception is thrown if the length is exceeded.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. |
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. |
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));});
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback