Registry / serialization / text-encoding-utf-8

text-encoding-utf-8

JSON →
library1.0.2jsnpmunverified

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.

npm install text-encoding-utf-8
INSTALL
IMPORT
SIG · TEXT-ENCODING-UTF-
T
text-encoding-utf-8
serializationjavascriptv1.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TextEncoder
✓ import { TextEncoder, TextDecoder } from 'text-encoding-utf-8';
✗ const { TextEncoder, TextDecoder } = require('text-encoding-utf-8');
While CommonJS `require` might work, the package's age (v1.x, 7+ years old) predates widespread native ESM support in Node.js. For browser environments, `<script src="encoding.js"></script>` makes globals available.
TextDecoder
✓ import { TextEncoder, TextDecoder } from 'text-encoding-utf-8';
✗ const TextDecoder = require('text-encoding-utf-8').TextDecoder;
A direct require of the package likely returns an object with both `TextEncoder` and `TextDecoder` properties, or it makes them global. Named imports are the most explicit way if using a bundler.
Global Polyfill
✓ import 'text-encoding-utf-8';
In older environments or when using the provided `encoding.js` script in HTML, importing the package as a side-effect (or loading the script) is intended to make `TextEncoder` and `TextDecoder` available globally.

Demonstrates basic UTF-8 encoding and decoding using TextEncoder and TextDecoder, including a conceptual example of streaming decode.

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);
Debug
Known issues
gotchaThis 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.
fix
Ensure that only 'utf-8' or 'UTF-8' are passed as encoding arguments to TextEncoder and TextDecoder constructors. If other encodings are needed, use a comprehensive polyfill or check for native API support.
affects: >=1.0.0
deprecatedThe 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.
fix
For new projects, avoid this polyfill entirely. For existing projects, consider removing it and relying on native browser/Node.js APIs. Use feature detection (`typeof TextEncoder !== 'undefined'`) if targeting very old environments that lack native support.
affects: >=1.0.0
gotchaUsing this polyfill in environments that already have native `TextEncoder` and `TextDecoder` can lead to conflicts, unexpected behavior, or simply redundant code execution.
fix
Implement robust feature detection to conditionally load or apply the polyfill only when `TextEncoder` or `TextDecoder` are genuinely missing. For example: `if (typeof TextEncoder === 'undefined') { require('text-encoding-utf-8'); }`.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Unknown encoding: 'iso-8859-1'
The `text-encoding-utf-8` polyfill is strictly limited to UTF-8 and does not support other character encodings.
fix
Modify 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
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.
fix
Ensure 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).
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
text-encoding-utf-8 — npm install text-encoding-utf-8 · libregistry