encoding
The encoding module is available to use in your EdgeWorkers code bundles to handle text in various character encodings, including legacy non-UTF-8.
TextEncoder Object
The TextEncoder object takes a stream of code points as input and emits a stream of UTF-8 bytes.
This example takes a string and converts it to a Uint8Array array of bytes. The string representation of the array is returned as the body.
TextEncoder methods
TextEncoder()
Constructor for a new TextEncoder object.
encode()
Converts the input string into a stream of UTF-8 bytes and returns a Uint8Array containing UTF-8 encoded text. The available argument is a String to encode.
TextEncoder properties
encoding
The name of the encoding algorithm used by the specific encoder. This is a read-only string UTF-8 value.
TextDecoder Object
The TextDecoder Object represents a decoder for a specific text encoding, such as UTF-8, ISO-8859-2, KOI8-R, and GBK. A decoder takes a stream of bytes as input and emits a stream of code points.
The following example takes a Uint8Array array of bytes and decodes it into the original string.
This example demonstrates how to decode data that’s provided in chunks.
📘 The stream: true is passed into the
decode()method when you push chunks one by one, and the parameter is missing in the last call todecode().
TextDecoder methods
TextDecoderOptions dictionary
If the specified value is not a Boolean, it’s converted to Boolean.
TextDecoder()
Constructs a new TextDecoder object.
decode()
Returns a string containing the text decoded with the method of the specific TextDecoder object.
TextDecoder properties
📘 Whitespaces are ignored in all
TextDecoderfunctions.
fatal
The fatal flag passed into the constructor. This is a read-only Boolean value.
ignoreBOM
The ignoreBOM flag passed into the constructor. This is a read-only Boolean value.
encoding
The name of the decoder describing the method that the TextDecoder will use. This is a read-only String value.
encoding methods
atob()
Takes a string of base64 encoded data and returns a decoded ASCII binary string.
btoa()
Takes a string, performs a base64 encoding on it and returns an ASCII string.
base64
The base64 object provides the base64 decoding and the base64 encoding method.
base64.decode()
The decode() method takes a base64 encoded string and by default, returns an Uint8Array of bytes. You can specify the output format.
The return value depends on the outputFormat. If you specify bytesBuffer: Uint8Array - the decoded bytes are returned in an Uint8Array. If you specify string: A primitive string, containing the decoded bytes in each character.
base64.encode()
The encode() method takes a Uint8Array of bytes and returns an encoded base64 string.
The return value is a primitive string, containing the encoded bytes in each character.
base64url
The base64url object provides the base64url decoding method and the base64url encoding method.
base64url.decode()
The decode() method takes a base64url encoded string and returns an Uint8Array of bytes (default format). You can also specify an optional argument for output format to obtain the desired output type.
Return value depends on the outputFormat:
bytesBuffer: Uint8Array - the decoded bytes in an Uint8Array.
or
string: A primitive string, containing the decoded bytes in each character.
base64url.encode()
The encode() method takes a Uint8Array of bytes and returns an encoded base64url String.
base16
Base16 encoding is the standard case-insensitive hex encoding and may be referred to as “base16” or “hex”.
base16.decode()
The base16.decode() method takes a base16 encoded string and returns a Uint8Array of bytes.
You can also specify the outputFormat to obtain the desired output type.
-
bytesBuffer: Uint8Array - the decoded bytes in an Uint8Array.
-
string: A primitive string, containing the decoded bytes in each character.
base16.encode()
The base16.encode() method takes an Uint8Array of bytes and returns an encoded base16 String.