Skip to content

Commit 729f05a

Browse files
committed
Simplify the use of Text Encoding features
1 parent e31af06 commit 729f05a

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/Decoder.ts

Lines changed: 2 additions & 2 deletions
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 } from "./utils/int";
4-
import { utf8DecodeJs, TEXT_ENCODING_AVAILABLE, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
4+
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
55
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
66
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
77

@@ -492,7 +492,7 @@ export class Decoder<ContextType> {
492492
let object: string;
493493
if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) {
494494
object = this.keyDecoder.decode(this.bytes, offset, byteLength);
495-
} else if (TEXT_ENCODING_AVAILABLE && byteLength > TEXT_DECODER_THRESHOLD) {
495+
} else if (byteLength > TEXT_DECODER_THRESHOLD) {
496496
object = utf8DecodeTD(this.bytes, offset, byteLength);
497497
} else {
498498
object = utf8DecodeJs(this.bytes, offset, byteLength);

src/Encoder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { utf8EncodeJs, utf8Count, TEXT_ENCODING_AVAILABLE, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8";
1+
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8";
22
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
33
import { setInt64, setUint64 } from "./utils/int";
44
import { ensureUint8Array } from "./utils/typedArrays";
@@ -169,7 +169,7 @@ export class Encoder<ContextType> {
169169
const maxHeaderSize = 1 + 4;
170170
const strLength = object.length;
171171

172-
if (TEXT_ENCODING_AVAILABLE && strLength > TEXT_ENCODER_THRESHOLD) {
172+
if (strLength > TEXT_ENCODER_THRESHOLD) {
173173
const byteLength = utf8Count(object);
174174
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength);
175175
this.writeStringHeader(byteLength);

src/utils/utf8.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
export const TEXT_ENCODING_AVAILABLE =
1+
const TEXT_ENCODING_AVAILABLE =
22
typeof process !== "undefined" &&
33
process.env.TEXT_ENCODING !== "never" &&
44
typeof TextEncoder !== "undefined" &&
55
typeof TextDecoder !== "undefined";
66

7+
const STR_SIZE_MAX = 0xffff_ffff; // uint32_max
8+
79
export function utf8Count(str: string): number {
810
const strLength = str.length;
911

@@ -88,7 +90,11 @@ export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: numb
8890
}
8991

9092
const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined;
91-
export const TEXT_ENCODER_THRESHOLD = typeof process !== "undefined" && process.env.TEXT_ENCODING !== "force" ? 200 : 0;
93+
export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
94+
? STR_SIZE_MAX
95+
: typeof process !== "undefined" && process.env.TEXT_ENCODING !== "force"
96+
? 200
97+
: 0;
9298

9399
function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void {
94100
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -154,7 +160,11 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength:
154160
}
155161

156162
const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null;
157-
export const TEXT_DECODER_THRESHOLD = typeof process !== "undefined" && process.env.TEXT_DECODER !== "force" ? 200 : 0;
163+
export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE
164+
? STR_SIZE_MAX
165+
: typeof process !== "undefined" && process.env.TEXT_DECODER !== "force"
166+
? 200
167+
: 0;
158168

159169
export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string {
160170
const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);

0 commit comments

Comments
 (0)