Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
text-decoding
Advanced tools
[fork] TextEncoder and TextDecoder (Polyfill for the Encoding Living Standard's API) For Node.JS.
The text-decoding npm package provides an implementation of the Encoding Living Standard API for decoding text. This is particularly useful for environments where the TextDecoder interface is not available, such as in certain Node.js versions or specific browsers. It allows for decoding of text from various encoded formats into a string format that JavaScript can work with.
Decoding text from UTF-8
This feature allows for decoding of text from a UTF-8 encoded Uint8Array to a JavaScript string. It's useful for reading binary data that represents text.
"use strict";\nconst { TextDecoder } = require('text-decoding');\nconst uint8Array = new Uint8Array([72, 101, 108, 108, 111]);\nconst decoder = new TextDecoder('utf-8');\nconst text = decoder.decode(uint8Array);\nconsole.log(text); // Outputs: Hello
Decoding text with options
This feature demonstrates decoding with options such as 'ignoreBOM' for ignoring Byte Order Mark and 'fatal' for throwing an error on invalid input. It's useful for more controlled decoding scenarios.
"use strict";\nconst { TextDecoder } = require('text-decoding');\nconst uint8Array = new Uint8Array([226, 130, 172]);\nconst decoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });\nconst text = decoder.decode(uint8Array);\nconsole.log(text); // Outputs: €
iconv-lite is a popular package for character encoding conversion in Node.js. Unlike text-decoding, which focuses on decoding according to the Encoding Living Standard, iconv-lite supports a wider range of character encodings. It's more versatile for applications that need to work with various encodings beyond what's commonly supported by web standards.
The buffer package provides Node.js Buffer support in the browser. While not directly comparable to text-decoding, it includes functionality for handling binary data and can be used in conjunction with text encoding and decoding operations. It's more focused on binary data manipulation rather than specifically on text decoding.
text-decoding
is a fork of Polyfill for the Encoding Living Standard's API (text-encoding
) For Node.JS.
This is a polyfill for the Encoding Living Standard API for the Web, allowing encoding and decoding of textual data to and from Typed Array buffers for binary data in JavaScript.
By default it adheres to the spec and does not support encoding to legacy encodings, only decoding. It is also implemented to match the specification's algorithms, rather than for performance.
yarn add text-decoding
class TextDecoder
class TextEncoder
const EncodingIndexes
getEncoding(label: string): { name: string, labels: Array<string> }
The package is available by importing its named classes and functions:
import { TextEncoder, TextDecoder, EncodingIndexes, getEncoding } from 'text-decoding'
class TextDecoder
Decodes a Uint8Array into a string.
Source | Output |
---|---|
|
|
class TextEncoder
Encodes a string into Uint8Array
for the given encoding.
As required by the specification, only encoding to utf-8 is supported. If you want to try it out, you can force a non-standard behavior by passing the NONSTANDARD_allowLegacyEncoding
option to TextEncoder and a label. For example:
import { TextEncoder } from 'text-decoding'
const uint8array = new TextEncoder(
'windows-1252', { NONSTANDARD_allowLegacyEncoding: true })
.encode('hello world')
console.log(uint8array)
Uint8Array [ 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ]
const EncodingIndexes
This is a map of indexes used for encoding.
getEncoding(
label: string,
): { name: string, labels: Array<string> }
Returns the normalised name of the encoding and its associated labels.
Source | Output |
---|---|
|
|
All encodings from the Encoding specification are supported:
utf-8 ibm866 iso-8859-2 iso-8859-3 iso-8859-4 iso-8859-5 iso-8859-6 iso-8859-7 iso-8859-8 iso-8859-8-i iso-8859-10 iso-8859-13 iso-8859-14 iso-8859-15 iso-8859-16 koi8-r koi8-u macintosh windows-874 windows-1250 windows-1251 windows-1252 windows-1253 windows-1254 windows-1255 windows-1256 windows-1257 windows-1258 x-mac-cyrillic gb18030 hz-gb-2312 big5 euc-jp iso-2022-jp shift_jis euc-kr replacement utf-16be utf-16le x-user-defined
(Some encodings may be supported under other names, e.g. ascii, iso-8859-1, etc. See Encoding for additional labels for each encoding.)
Original Work By Joshua Bell under dual Unlicense/Apache-2.0 license.
The encoding indexes, algorithms, and many comments in the code derive from the Encoding Standard https://encoding.spec.whatwg.org/
© Art Deco for Idio 2019 | Tech Nation Visa Sucks |
---|
FAQs
[fork] TextEncoder and TextDecoder (Polyfill for the Encoding Living Standard's API) For Node.JS.
We found that text-decoding demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.