Overview
The Encoding APIs provide:TextEncoder- Encode strings to UTF-8 bytesTextDecoder- Decode bytes to strings with multiple encodingsTextEncoderStream- Streaming text encodingTextDecoderStream- Streaming text decoding- Support for legacy encodings (ISO-8859, Windows-1252, etc.)
src/workerd/api/encoding.h and encoding.c++
TextEncoder
Encode strings to UTF-8 bytes:src/workerd/api/encoding.h:133
encodeInto()
Encode directly into an existing buffer:encode() when you have a pre-allocated buffer.
Source: src/workerd/api/encoding.h:147
TextDecoder
Decode bytes to strings with support for multiple encodings:src/workerd/api/encoding.h:59
Supported encodings
TextDecoder supports many encodings through ICU:src/workerd/api/encoding.h:17
Constructor options
Customize decoder behavior:src/workerd/api/encoding.h:63
Streaming decoding
Decode data across multiple chunks:src/workerd/api/encoding.h:70
TextEncoderStream
Encode text streams to byte streams:src/workerd/api/streams/encoding.h and encoding.c++
TextDecoderStream
Decode byte streams to text streams:Stream options
Configure the decoder stream:Common patterns
Converting between strings and bytes
Base64 encoding
Combine with base64 for data URLs:Reading text files
Processing CSV with encoding
Streaming text transformation
Handling invalid data
Non-fatal decoding
Replace invalid sequences with replacement character:Fatal decoding
Throw on invalid sequences:Best practices
Reuse encoder/decoder instances
Reuse encoder/decoder instances
Create once and reuse for better performance:
Use encodeInto() for large buffers
Use encodeInto() for large buffers
More efficient when you have pre-allocated buffers:
Specify encoding explicitly
Specify encoding explicitly
Always specify the encoding when decoding:
Use streams for large data
Use streams for large data
Stream large text data instead of loading it all:
Implementation details
The Encoding APIs are implemented in:src/workerd/api/encoding.h/.c++- TextEncoder and TextDecoder (180 lines)src/workerd/api/encoding-legacy.h/.c++- Legacy encoding supportsrc/workerd/api/encoding-shared.h- Shared encoding utilitiessrc/workerd/api/streams/encoding.h/.c++- TextEncoderStream and TextDecoderStream
src/workerd/api/encoding.h:20
Character encoding reference
Common encodings supported:- UTF-8 - Universal, variable-width (1-4 bytes)
- UTF-16LE - 16-bit little-endian
- UTF-16BE - 16-bit big-endian
- ISO-8859-1 - Latin-1 (Western European)
- ISO-8859-2 - Latin-2 (Central European)
- Windows-1252 - Windows Western European
- GBK - Simplified Chinese
- Shift_JIS - Japanese
- EUC-KR - Korean
Related APIs
- Streams API - TextEncoderStream and TextDecoderStream
- Crypto API - Often used with TextEncoder for hashing
- Fetch API - Response.text() uses TextDecoder internally