UTF-8 Text Encoding Polyfill
text-encoding-utf-8 is a partial polyfill for the Web's Encoding Living Standard API, specifically designed to provide UTF-8 encoding and decoding capabilities for environments lacking native `TextEncoder` and `TextDecoder` implementations, such as older versions of Safari on iOS. The current stable version is 1.0.2, last updated over seven years ago. Its key differentiator is its strict focus on UTF-8 only, resulting in a smaller footprint compared to full polyfills like `text-encoding`. While it served a critical role historically, modern browsers and Node.js environments universally support `TextEncoder` and `TextDecoder` natively, rendering this polyfill largely obsolete for new development.
Common errors
-
Error: Unknown encoding: 'iso-8859-1'
cause The `text-encoding-utf-8` polyfill is strictly limited to UTF-8 and does not support other character encodings.fixModify your code to only use 'utf-8' or 'UTF-8' as the encoding string. If other encodings are required, this polyfill is not suitable, and you should use a more comprehensive library or native APIs. -
ReferenceError: TextEncoder is not defined
cause The `TextEncoder` (or `TextDecoder`) global object was not made available. This can happen if the polyfill script was not properly loaded, or if the import mechanism didn't expose the globals as expected.fixEnsure the polyfill script (`encoding.js`) is loaded in HTML, or for Node.js/bundlers, verify the import statement is correct (`import 'text-encoding-utf-8';` for global patching, or `import { TextEncoder } from 'text-encoding-utf-8';` for named imports).
Warnings
- gotcha This polyfill only supports UTF-8. Attempting to use any other encoding (e.g., 'iso-8859-1', 'gbk') will result in an error or unexpected behavior, as it will fall back to UTF-8 without explicit error.
- deprecated The package is effectively unmaintained, with the last publish over seven years ago. Modern browsers (Chrome, Firefox, Edge, Safari) and Node.js environments have native, highly optimized implementations of TextEncoder and TextDecoder. Relying on this polyfill can introduce unnecessary bundle size and potential compatibility issues.
- gotcha Using this polyfill in environments that already have native `TextEncoder` and `TextDecoder` can lead to conflicts, unexpected behavior, or simply redundant code execution.
Install
-
npm install text-encoding-utf-8 -
yarn add text-encoding-utf-8 -
pnpm add text-encoding-utf-8
Imports
- TextEncoder
const { TextEncoder, TextDecoder } = require('text-encoding-utf-8');import { TextEncoder, TextDecoder } from 'text-encoding-utf-8'; - TextDecoder
const TextDecoder = require('text-encoding-utf-8').TextDecoder;import { TextEncoder, TextDecoder } from 'text-encoding-utf-8'; - Global Polyfill
import 'text-encoding-utf-8';
Quickstart
import { TextEncoder, TextDecoder } from 'text-encoding-utf-8';
// Basic UTF-8 Encoding
const encoder = new TextEncoder('utf-8');
const originalString = 'Hello, world! 👋';
const encodedBytes = encoder.encode(originalString);
console.log('Original String:', originalString);
console.log('Encoded Bytes (Uint8Array):', encodedBytes);
// Basic UTF-8 Decoding
const decoder = new TextDecoder('utf-8');
const decodedString = decoder.decode(encodedBytes);
console.log('Decoded String:', decodedString);
// Streaming Decode (example concept - next_chunk needs implementation)
let streamDecoder = new TextDecoder('utf-8');
let resultString = '';
// Imagine 'chunk1', 'chunk2', etc., are Uint8Arrays from a stream
const chunk1 = encoder.encode('This is the first part. ');
const chunk2 = encoder.encode('And this is the second part.');
resultString += streamDecoder.decode(chunk1, { stream: true });
resultString += streamDecoder.decode(chunk2, { stream: true });
resultString += streamDecoder.decode(); // Finish the stream
console.log('Stream Decoded String:', resultString);