Skip to content

Commit 1c91d8e

Browse files
committed
drop IE11 support: TextEncoder is supported in all the modern JS engines
1 parent bd0db9c commit 1c91d8e

File tree

6 files changed

+36
-59
lines changed

6 files changed

+36
-59
lines changed

.nycrc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"include": ["src/**/*.ts"],
3-
"extension": [".ts"],
2+
"include": ["src/**/*.ts", "src/**/*.mts"],
3+
"extension": [".ts", ".mtx"],
44
"reporter": [],
55
"sourceMap": true,
66
"instrument": true

package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
"prepublishOnly": "run-p 'test:dist:*' && npm run test:browser",
1717
"clean": "rimraf build dist dist.*",
1818
"test": "mocha 'test/**/*.test.ts'",
19-
"test:purejs": "TEXT_ENCODING=never mocha 'test/**/*.test.ts'",
20-
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.ts'",
21-
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es5-purejs.json npm run test:purejs -- --reporter=dot",
22-
"test:cover": "npm run cover:clean && npm-run-all 'test:cover:*' && npm run cover:report",
23-
"test:cover:purejs": "npx nyc --no-clean npm run test:purejs",
24-
"test:cover:te": "npx nyc --no-clean npm run test:te",
19+
"test:cover": "npm run cover:clean && npx nyc --no-clean npm run 'test' && npm run cover:report",
2520
"test:deno": "deno test test/deno_test.ts",
2621
"test:fuzz": "npm exec --yes -- jsfuzz@git+https://gitlab.com/gitlab-org/security-products/analyzers/fuzzers/jsfuzz.git --fuzzTime 60 --no-versifier test/decode.jsfuzz.js corpus",
2722
"cover:clean": "rimraf .nyc_output coverage/",

src/Decoder.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { prettyByte } from "./utils/prettyByte";
22
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
33
import { getInt64, getUint64, UINT32_MAX } from "./utils/int";
4-
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
4+
import { utf8Decode } from "./utils/utf8";
55
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
66
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
77
import { DecodeError } from "./DecodeError";
@@ -507,10 +507,8 @@ export class Decoder<ContextType = undefined> {
507507
let object: string;
508508
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) {
509509
object = this.keyDecoder.decode(this.bytes, offset, byteLength);
510-
} else if (byteLength > TEXT_DECODER_THRESHOLD) {
511-
object = utf8DecodeTD(this.bytes, offset, byteLength);
512510
} else {
513-
object = utf8DecodeJs(this.bytes, offset, byteLength);
511+
object = utf8Decode(this.bytes, offset, byteLength);
514512
}
515513
this.pos += headerOffset + byteLength;
516514
return object;

src/Encoder.ts

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8";
1+
import { utf8Count, utf8Encode } from "./utils/utf8";
22
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
33
import { setInt64, setUint64 } from "./utils/int";
44
import { ensureUint8Array } from "./utils/typedArrays";
@@ -177,21 +177,12 @@ export class Encoder<ContextType = undefined> {
177177

178178
private encodeString(object: string) {
179179
const maxHeaderSize = 1 + 4;
180-
const strLength = object.length;
181-
182-
if (strLength > TEXT_ENCODER_THRESHOLD) {
183-
const byteLength = utf8Count(object);
184-
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
185-
this.writeStringHeader(byteLength);
186-
utf8EncodeTE(object, this.bytes, this.pos);
187-
this.pos += byteLength;
188-
} else {
189-
const byteLength = utf8Count(object);
190-
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
191-
this.writeStringHeader(byteLength);
192-
utf8EncodeJs(object, this.bytes, this.pos);
193-
this.pos += byteLength;
194-
}
180+
181+
const byteLength = utf8Count(object);
182+
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
183+
this.writeStringHeader(byteLength);
184+
utf8Encode(object, this.bytes, this.pos);
185+
this.pos += byteLength;
195186
}
196187

197188
private encodeObject(object: unknown, depth: number) {

src/utils/utf8.ts

+24-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
22
import { UINT32_MAX } from "./int";
33

4-
const TEXT_ENCODING_AVAILABLE =
5-
(typeof process === "undefined" || process?.env?.["TEXT_ENCODING"] !== "never") &&
6-
typeof TextEncoder !== "undefined" &&
7-
typeof TextDecoder !== "undefined";
8-
94
export function utf8Count(str: string): number {
105
const strLength = str.length;
116

@@ -89,22 +84,26 @@ export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: numb
8984
}
9085
}
9186

92-
const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined;
93-
export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
94-
? UINT32_MAX
95-
: typeof process !== "undefined" && process?.env?.["TEXT_ENCODING"] !== "force"
96-
? 200
97-
: 0;
87+
const sharedTextEncoder = new TextEncoder();
88+
const TEXT_ENCODER_THRESHOLD = 200;
9889

9990
function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void {
100-
output.set(sharedTextEncoder!.encode(str), outputOffset);
91+
output.set(sharedTextEncoder.encode(str), outputOffset);
10192
}
10293

10394
function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void {
104-
sharedTextEncoder!.encodeInto(str, output.subarray(outputOffset));
95+
sharedTextEncoder.encodeInto(str, output.subarray(outputOffset));
10596
}
10697

107-
export const utf8EncodeTE = sharedTextEncoder?.encodeInto ? utf8EncodeTEencodeInto : utf8EncodeTEencode;
98+
export const utf8EncodeTE = (sharedTextEncoder.encodeInto as unknown) ? utf8EncodeTEencodeInto : utf8EncodeTEencode;
99+
100+
export function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void {
101+
if (str.length > TEXT_ENCODER_THRESHOLD) {
102+
utf8EncodeTE(str, output, outputOffset);
103+
} else {
104+
utf8EncodeJs(str, output, outputOffset);
105+
}
106+
}
108107

109108
const CHUNK_SIZE = 0x1_000;
110109

@@ -157,14 +156,18 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength:
157156
return result;
158157
}
159158

160-
const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null;
161-
export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
162-
? UINT32_MAX
163-
: typeof process !== "undefined" && process?.env?.["TEXT_DECODER"] !== "force"
164-
? 200
165-
: 0;
159+
const sharedTextDecoder = new TextDecoder();
160+
const TEXT_DECODER_THRESHOLD = 200;
166161

167162
export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
168163
const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
169-
return sharedTextDecoder!.decode(stringBytes);
164+
return sharedTextDecoder.decode(stringBytes);
165+
}
166+
167+
export function utf8Decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
168+
if (byteLength > TEXT_DECODER_THRESHOLD) {
169+
return utf8DecodeTD(bytes, inputOffset, byteLength);
170+
} else {
171+
return utf8DecodeJs(bytes, inputOffset, byteLength);
172+
}
170173
}

tsconfig.test-dist-es5-purejs.json

-10
This file was deleted.

0 commit comments

Comments
 (0)