Socket
Socket
Sign inDemoInstall

fast-text-encoding

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-text-encoding - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

.travis.yml

11

package.json
{
"name": "fast-text-encoding",
"version": "1.0.1",
"version": "1.0.2",
"description": "Fast polyfill for TextEncoder and TextDecoder, only supports utf-8",
"main": "text.min.js",
"repository": "https://github.com/samthor/fast-text-encoder.git",
"repository": "https://github.com/samthor/fast-text-encoding.git",
"author": "Sam Thorogood <sam.thorogood@gmail.com>",
"license": "Apache-2",
"license": "Apache-2.0",
"devDependencies": {
"chai": "^4.2.0",
"google-closure-compiler": "^20200406.0.0",
"mocha": "^7.1.0"
},
"scripts": {
"compile": "./compile.sh",
"test": "mocha"
}
}

@@ -0,1 +1,3 @@

[![Build](https://api.travis-ci.org/samthor/fast-text-encoding.svg?branch=master)](https://travis-ci.org/samthor/fast-text-encoding)
This is a fast polyfill for [`TextEncoder`][1] and [`TextDecoder`][2], which let you encode and decode JavaScript strings into UTF-8 bytes.

@@ -10,5 +12,11 @@

Include the minified inside a `script` tag or as an ES6 Module for its side-effects.
It will create `TextEncoder` and `TextDecoder` if the symbols are missing on `window`.
Install as "fast-text-encoding" via your favourite package manager.
You only need this polyfill if you're supporting older browsers like IE, legacy Edge, ancient Chrome and Firefox, or Node before v11.
## Browser
Include the minified code inside a `script` tag or as an ES6 Module for its side effects.
It will create `TextEncoder` and `TextDecoder` if the symbols are missing on `window` or `global.`
```html

@@ -18,15 +26,16 @@ <script src="node_modules/fast-text-encoding/text.min.js"></script>

import './node_modules/fast-text-encoding/text.min.js';
// confidently do something with TextEncoder \o/
import 'fast-text-encoding'; // or perhaps this
// confidently do something with TextEncoder or TextDecoder \o/
</script>
```
**Note**: Always include `text.min.js`, as it's compiled to ES5 for older environments.
⚠️ You'll probably want to depend on `text.min.js`, as it's compiled to ES5 for older environments.
## Node
Install via NPM or Yarn (name "fast-text-encoding"), and then import purely for side effects:
You only need this polyfill in Node before v11.
However, you can use `Buffer` to provide the same functionality (but not conforming to any spec) in versions even older than that.
```js
// don't need to save this anywhere, just require before use
require('fast-text-encoding');
require('fast-text-encoding'); // just require me before use

@@ -37,10 +46,2 @@ const buffer = new TextEncoder().encode('Turn me into UTF-8!');

However, note that `Buffer.from('Turn me into UTF-8!')` is Node's native version of the text encoding functionality.
You can probably massage [`Buffer`](https://nodejs.org/api/buffer.html) into acting like `TextEncoder` and `TextDecoder`.
# Supports
Built for IE11, Edge and Node environments.
Not required for Chrome, Firefox etc, which have native implementations.
# Release

@@ -47,0 +48,0 @@

@@ -49,3 +49,3 @@ /*

*/
FastTextEncoder.prototype.encode = function(string, options={stream: false}) {
FastTextEncoder.prototype['encode'] = function(string, options={stream: false}) {
if (options.stream) {

@@ -102,4 +102,3 @@ throw new Error(`Failed to encode: the 'stream' option is unsupported.`);

} else {
// FIXME: do we care
continue;
continue; // out of range
}

@@ -121,3 +120,3 @@

function FastTextDecoder(utfLabel='utf-8', options={fatal: false}) {
if (validUtfLabels.indexOf(utfLabel.toLowerCase()) == -1) {
if (validUtfLabels.indexOf(utfLabel.toLowerCase()) === -1) {
throw new RangeError(

@@ -142,3 +141,3 @@ `Failed to construct 'TextDecoder': The encoding label provided ('${utfLabel}') is invalid.`);

*/
FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) {
FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false}) {
if (options['stream']) {

@@ -148,10 +147,12 @@ throw new Error(`Failed to decode: the 'stream' option is unsupported.`);

// Accept Uint8Array's as-is.
let bytes = buffer;
// Look for ArrayBufferView, which isn't a real type, but basically represents
// all the valid TypedArray types plus DataView. They all have ".buffer" as
// an instance of ArrayBuffer.
if (buffer.buffer instanceof ArrayBuffer) {
buffer = buffer.buffer;
if (!(bytes instanceof Uint8Array) && bytes.buffer instanceof ArrayBuffer) {
bytes = new Uint8Array(buffer.buffer);
}
let bytes = new Uint8Array(buffer);
let pos = 0;

@@ -180,2 +181,6 @@ let pending = [];

// The native TextDecoder will generate "REPLACEMENT CHARACTER" where the
// input data is invalid. Here, we blindly parse the data even if it's
// wrong: e.g., if a 3-byte sequence doesn't have two valid continuations.
const byte1 = bytes[pos++];

@@ -189,7 +194,7 @@ if (byte1 === 0) {

pending.push(((byte1 & 0x1f) << 6) | byte2);
} else if ((byte1 & 0xf0) === 0xe0) {
} else if ((byte1 & 0xf0) === 0xe0) { // 3-byte
const byte2 = bytes[pos++] & 0x3f;
const byte3 = bytes[pos++] & 0x3f;
pending.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
} else if ((byte1 & 0xf8) === 0xf0) {
} else if ((byte1 & 0xf8) === 0xf0) { // 4-byte
const byte2 = bytes[pos++] & 0x3f;

@@ -209,3 +214,3 @@ const byte3 = bytes[pos++] & 0x3f;

} else {
// FIXME: we're ignoring this
// invalid initial byte
}

@@ -212,0 +217,0 @@ }

@@ -1,5 +0,5 @@

(function(l){function m(){}function k(b,a){b=void 0===b?"utf-8":b;a=void 0===a?{fatal:!1}:a;if(-1==n.indexOf(b.toLowerCase()))throw new RangeError("Failed to construct 'TextDecoder': The encoding label provided ('"+b+"') is invalid.");if(a.fatal)throw Error("Failed to construct 'TextDecoder': the 'fatal' option is unsupported.");}if(l.TextEncoder&&l.TextDecoder)return!1;var n=["utf-8","utf8","unicode-1-1-utf-8"];Object.defineProperty(m.prototype,"encoding",{value:"utf-8"});m.prototype.encode=function(b,
a){a=void 0===a?{stream:!1}:a;if(a.stream)throw Error("Failed to encode: the 'stream' option is unsupported.");a=0;for(var g=b.length,f=0,c=Math.max(32,g+(g>>1)+7),e=new Uint8Array(c>>3<<3);a<g;){var d=b.charCodeAt(a++);if(55296<=d&&56319>=d){if(a<g){var h=b.charCodeAt(a);56320===(h&64512)&&(++a,d=((d&1023)<<10)+(h&1023)+65536)}if(55296<=d&&56319>=d)continue}f+4>e.length&&(c+=8,c*=1+a/b.length*2,c=c>>3<<3,h=new Uint8Array(c),h.set(e),e=h);if(0===(d&4294967168))e[f++]=d;else{if(0===(d&4294965248))e[f++]=
d>>6&31|192;else if(0===(d&4294901760))e[f++]=d>>12&15|224,e[f++]=d>>6&63|128;else if(0===(d&4292870144))e[f++]=d>>18&7|240,e[f++]=d>>12&63|128,e[f++]=d>>6&63|128;else continue;e[f++]=d&63|128}}return e.slice?e.slice(0,f):e.subarray(0,f)};Object.defineProperty(k.prototype,"encoding",{value:"utf-8"});Object.defineProperty(k.prototype,"fatal",{value:!1});Object.defineProperty(k.prototype,"ignoreBOM",{value:!1});k.prototype.decode=function(b,a){a=void 0===a?{stream:!1}:a;if(a.stream)throw Error("Failed to decode: the 'stream' option is unsupported.");
b.buffer instanceof ArrayBuffer&&(b=b.buffer);b=new Uint8Array(b);a=0;for(var g=[],f=[];;){var c=a<b.length;if(!c||a&65536){f.push(String.fromCharCode.apply(null,g));if(!c)return f.join("");g=[];b=b.subarray(a);a=0}c=b[a++];if(0===c)g.push(0);else if(0===(c&128))g.push(c);else if(192===(c&224)){var e=b[a++]&63;g.push((c&31)<<6|e)}else if(224===(c&240)){e=b[a++]&63;var d=b[a++]&63;g.push((c&31)<<12|e<<6|d)}else if(240===(c&248)){e=b[a++]&63;d=b[a++]&63;var h=b[a++]&63;c=(c&7)<<18|e<<12|d<<6|h;65535<
c&&(c-=65536,g.push(c>>>10&1023|55296),c=56320|c&1023);g.push(c)}}};l.TextEncoder=m;l.TextDecoder=k})("undefined"!==typeof window?window:"undefined"!==typeof global?global:this);
(function(l){function m(){}function k(c,a){c=void 0===c?"utf-8":c;a=void 0===a?{fatal:!1}:a;if(-1===n.indexOf(c.toLowerCase()))throw new RangeError("Failed to construct 'TextDecoder': The encoding label provided ('"+c+"') is invalid.");if(a.fatal)throw Error("Failed to construct 'TextDecoder': the 'fatal' option is unsupported.");}if(l.TextEncoder&&l.TextDecoder)return!1;var n=["utf-8","utf8","unicode-1-1-utf-8"];Object.defineProperty(m.prototype,"encoding",{value:"utf-8"});m.prototype.encode=function(c,
a){a=void 0===a?{stream:!1}:a;if(a.stream)throw Error("Failed to encode: the 'stream' option is unsupported.");a=0;for(var g=c.length,f=0,b=Math.max(32,g+(g>>1)+7),e=new Uint8Array(b>>3<<3);a<g;){var d=c.charCodeAt(a++);if(55296<=d&&56319>=d){if(a<g){var h=c.charCodeAt(a);56320===(h&64512)&&(++a,d=((d&1023)<<10)+(h&1023)+65536)}if(55296<=d&&56319>=d)continue}f+4>e.length&&(b+=8,b*=1+a/c.length*2,b=b>>3<<3,h=new Uint8Array(b),h.set(e),e=h);if(0===(d&4294967168))e[f++]=d;else{if(0===(d&4294965248))e[f++]=
d>>6&31|192;else if(0===(d&4294901760))e[f++]=d>>12&15|224,e[f++]=d>>6&63|128;else if(0===(d&4292870144))e[f++]=d>>18&7|240,e[f++]=d>>12&63|128,e[f++]=d>>6&63|128;else continue;e[f++]=d&63|128}}return e.slice?e.slice(0,f):e.subarray(0,f)};Object.defineProperty(k.prototype,"encoding",{value:"utf-8"});Object.defineProperty(k.prototype,"fatal",{value:!1});Object.defineProperty(k.prototype,"ignoreBOM",{value:!1});k.prototype.decode=function(c,a){a=void 0===a?{stream:!1}:a;if(a.stream)throw Error("Failed to decode: the 'stream' option is unsupported.");
a=c;!(a instanceof Uint8Array)&&a.buffer instanceof ArrayBuffer&&(a=new Uint8Array(c.buffer));c=0;for(var g=[],f=[];;){var b=c<a.length;if(!b||c&65536){f.push(String.fromCharCode.apply(null,g));if(!b)return f.join("");g=[];a=a.subarray(c);c=0}b=a[c++];if(0===b)g.push(0);else if(0===(b&128))g.push(b);else if(192===(b&224)){var e=a[c++]&63;g.push((b&31)<<6|e)}else if(224===(b&240)){e=a[c++]&63;var d=a[c++]&63;g.push((b&31)<<12|e<<6|d)}else if(240===(b&248)){e=a[c++]&63;d=a[c++]&63;var h=a[c++]&63;b=
(b&7)<<18|e<<12|d<<6|h;65535<b&&(b-=65536,g.push(b>>>10&1023|55296),b=56320|b&1023);g.push(b)}}};l.TextEncoder=m;l.TextDecoder=k})("undefined"!==typeof window?window:"undefined"!==typeof global?global:this);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc