]> git.codecow.com Git - libnemo.git/commitdiff
Revert utf8 encoders for use in workers. Fix buffer convert export.
authorChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 19:33:07 +0000 (12:33 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 19:33:07 +0000 (12:33 -0700)
src/lib/convert.ts

index 28119169a34fc828bb56ab5c30efa96e4abde0ae..038a9d6f5ae1ce708d20aa433d4b3a65ac78c489 100644 (file)
@@ -3,9 +3,6 @@
 \r
 import { ALPHABET } from './constants'\r
 \r
-const encoder = new TextEncoder()\r
-const decoder = new TextDecoder()\r
-\r
 export class base32 {\r
        /**\r
        * Converts a base32 string to a Uint8Array of bytes.\r
@@ -209,7 +206,7 @@ export class bytes {
        * @returns {string} UTF-8 encoded text string\r
        */\r
        static toUtf8 (bytes: Uint8Array): string {\r
-               return decoder.decode(bytes)\r
+               return new TextDecoder().decode(bytes)\r
        }\r
 }\r
 \r
@@ -327,6 +324,22 @@ export class hex {
        }\r
 }\r
 \r
+export class obj {\r
+       /**\r
+       * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes.\r
+       *\r
+       * @param {object} obj - Object to convert\r
+       * @returns {Uint8Array} Byte array representation of the input object\r
+       */\r
+       static toBytes (obj: { [key: number]: number }): Uint8Array {\r
+               const values = Object.keys(obj)\r
+                       .map(key => +key)\r
+                       .sort((a, b) => a - b)\r
+                       .map(i => obj[i])\r
+               return new Uint8Array(values)\r
+       }\r
+}\r
+\r
 export class utf8 {\r
        /**\r
        * Convert a UTF-8 text string to a Uint8Array of bytes.\r
@@ -335,7 +348,7 @@ export class utf8 {
        * @returns {Uint8Array} Byte array representation of the input string\r
        */\r
        static toBytes (utf8: string): Uint8Array {\r
-               return encoder.encode(utf8)\r
+               return new TextEncoder().encode(utf8)\r
        }\r
 \r
        /**\r
@@ -349,25 +362,10 @@ export class utf8 {
        }\r
 }\r
 \r
-export class obj {\r
-       /**\r
-       * Convert a numerically-indexed object of 8-bit values to a Uint8Array of bytes.\r
-       *\r
-       * @param {object} obj - Object to convert\r
-       * @returns {Uint8Array} Byte array representation of the input object\r
-       */\r
-       static toBytes (obj: { [key: number]: number }): Uint8Array {\r
-               const values = Object.keys(obj)\r
-                       .map(key => +key)\r
-                       .sort((a, b) => a - b)\r
-                       .map(i => obj[i])\r
-               return new Uint8Array(values)\r
-       }\r
-}\r
-\r
 export default `\r
        const base32 = ${base32}\r
        const bin = ${bin}\r
+       const buffer = ${buffer}\r
        const bytes = ${bytes}\r
        const dec = ${dec}\r
        const hex = ${hex}\r