refactor: code cleanup

This commit is contained in:
Simon Chan 2024-06-02 01:55:26 +08:00
parent c440e83828
commit 721b6c0da6
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
68 changed files with 1161 additions and 1036 deletions

View file

@ -80,15 +80,15 @@ interface GlobalExtension {
const { TextEncoder, TextDecoder } = globalThis as unknown as GlobalExtension;
const Utf8Encoder = new TextEncoder();
const Utf8Decoder = new TextDecoder();
const SharedEncoder = new TextEncoder();
const SharedDecoder = new TextDecoder();
export function encodeUtf8(input: string): Uint8Array {
return Utf8Encoder.encode(input);
return SharedEncoder.encode(input);
}
export function decodeUtf8(buffer: ArrayBufferView | ArrayBuffer): string {
// `TextDecoder` has internal states in stream mode,
// but we don't use stream mode here, so it's safe to reuse the instance
return Utf8Decoder.decode(buffer);
// but this method is not for stream mode, so the instance can be reused
return SharedDecoder.decode(buffer);
}