|
| 1 | +export const TEXT_ENCODING_AVAILABLE = |
| 2 | + process.env.TEXT_ENCODING !== "never" && (typeof TextEncoder !== "undefined" && typeof TextDecoder !== "undefined"); |
| 3 | + |
1 | 4 | export function utf8Count(str: string): number { |
2 | 5 | const strLength = str.length; |
3 | 6 |
|
@@ -38,7 +41,7 @@ export function utf8Count(str: string): number { |
38 | 41 | return byteLength; |
39 | 42 | } |
40 | 43 |
|
41 | | -export function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void { |
| 44 | +export function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: number): void { |
42 | 45 | const strLength = str.length; |
43 | 46 | let offset = outputOffset; |
44 | 47 | let pos = 0; |
@@ -81,6 +84,22 @@ export function utf8Encode(str: string, output: Uint8Array, outputOffset: number |
81 | 84 | } |
82 | 85 | } |
83 | 86 |
|
| 87 | +const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined; |
| 88 | +export const TEXT_ENCODER_THRESHOLD = process.env.TEXT_ENCODING !== "force" ? 200 : 0; |
| 89 | + |
| 90 | +function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void { |
| 91 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 92 | + output.set(sharedTextEncoder!.encode(str), outputOffset); |
| 93 | +} |
| 94 | + |
| 95 | +function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void { |
| 96 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 97 | + sharedTextEncoder!.encodeInto(str, output.subarray(outputOffset)); |
| 98 | +} |
| 99 | + |
| 100 | +export const utf8EncodeTE = |
| 101 | + sharedTextEncoder && sharedTextEncoder.encodeInto ? utf8EncodeTEencodeInto : utf8EncodeTEencode; |
| 102 | + |
84 | 103 | const CHUNK_SIZE = 0x10_000; |
85 | 104 |
|
86 | 105 | export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength: number): string { |
@@ -132,8 +151,7 @@ export function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength: |
132 | 151 | return result; |
133 | 152 | } |
134 | 153 |
|
135 | | -const sharedTextDecoder = typeof TextDecoder !== "undefined" ? new TextDecoder() : null; |
136 | | -export const TEXT_DECODER_AVAILABLE = process.env.TEXT_DECODER !== "never" && !!sharedTextDecoder; |
| 154 | +const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null; |
137 | 155 | export const TEXT_DECODER_THRESHOLD = process.env.TEXT_DECODER !== "force" ? 200 : 0; |
138 | 156 |
|
139 | 157 | export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string { |
|
0 commit comments