From: Chris Duncan Date: Thu, 3 Jul 2025 19:33:07 +0000 (-0700) Subject: Revert utf8 encoders for use in workers. Fix buffer convert export. X-Git-Tag: v0.10.5~136^2~13 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=506c7b0eac25d97fa1f063019e84765d355e5b27;p=libnemo.git Revert utf8 encoders for use in workers. Fix buffer convert export. --- diff --git a/src/lib/convert.ts b/src/lib/convert.ts index 2811916..038a9d6 100644 --- a/src/lib/convert.ts +++ b/src/lib/convert.ts @@ -3,9 +3,6 @@ import { ALPHABET } from './constants' -const encoder = new TextEncoder() -const decoder = new TextDecoder() - export class base32 { /** * Converts a base32 string to a Uint8Array of bytes. @@ -209,7 +206,7 @@ export class bytes { * @returns {string} UTF-8 encoded text string */ static toUtf8 (bytes: Uint8Array): string { - return decoder.decode(bytes) + return new TextDecoder().decode(bytes) } } @@ -327,6 +324,22 @@ export class hex { } } +export class obj { + /** + * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes. + * + * @param {object} obj - Object to convert + * @returns {Uint8Array} Byte array representation of the input object + */ + static toBytes (obj: { [key: number]: number }): Uint8Array { + const values = Object.keys(obj) + .map(key => +key) + .sort((a, b) => a - b) + .map(i => obj[i]) + return new Uint8Array(values) + } +} + export class utf8 { /** * Convert a UTF-8 text string to a Uint8Array of bytes. @@ -335,7 +348,7 @@ export class utf8 { * @returns {Uint8Array} Byte array representation of the input string */ static toBytes (utf8: string): Uint8Array { - return encoder.encode(utf8) + return new TextEncoder().encode(utf8) } /** @@ -349,25 +362,10 @@ export class utf8 { } } -export class obj { - /** - * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes. - * - * @param {object} obj - Object to convert - * @returns {Uint8Array} Byte array representation of the input object - */ - static toBytes (obj: { [key: number]: number }): Uint8Array { - const values = Object.keys(obj) - .map(key => +key) - .sort((a, b) => a - b) - .map(i => obj[i]) - return new Uint8Array(values) - } -} - export default ` const base32 = ${base32} const bin = ${bin} + const buffer = ${buffer} const bytes = ${bytes} const dec = ${dec} const hex = ${hex}