]> git.codecow.com Git - libnemo.git/commitdiff
Fix conversion of detached buffers to hexadecimal.
authorChris Duncan <chris@zoso.dev>
Tue, 15 Jul 2025 20:42:07 +0000 (13:42 -0700)
committerChris Duncan <chris@zoso.dev>
Tue, 15 Jul 2025 20:42:07 +0000 (13:42 -0700)
src/lib/convert.ts

index 5c347aeab426b4f529f2a246a05ddb70fe41bae5..ab35a732262537658fab2a327d5d3690011bc289 100644 (file)
@@ -10,7 +10,7 @@ export class base32 {
        * @param {string} base32 - String to convert\r
        * @returns {Uint8Array} Byte array representation of the input string\r
        */\r
-       static toBytes (base32: string): Uint8Array {\r
+       static toBytes (base32: string): Uint8Array<ArrayBuffer> {\r
                const leftover = (base32.length * 5) % 8\r
                const offset = leftover === 0\r
                        ? 0\r
@@ -82,7 +82,8 @@ export class bytes {
        *\r
        * @param bytes - Buffer or bytes to erase\r
        */\r
-       static erase (bytes: ArrayBuffer | Uint8Array<ArrayBuffer>): void {\r
+       static erase (bytes?: ArrayBuffer | Uint8Array<ArrayBuffer> | null): void {\r
+               if (bytes == null) return\r
                if (bytes instanceof ArrayBuffer) {\r
                        if (bytes.detached) return\r
                        bytes = new Uint8Array(bytes)\r
@@ -99,11 +100,17 @@ export class bytes {
        * @param {number}[padding=0] - Minimum length of the resulting array padded as necessary with starting 0 values\r
        * @returns {number[]} Decimal array representation of the input value\r
        */\r
-       static toArray (bytes: Uint8Array, padding: number = 0): number[] {\r
+       static toArray (bytes: Uint8Array<ArrayBuffer>, padding: number = 0): number[] {\r
                if (!(bytes instanceof Uint8Array)) {\r
                        throw new TypeError('bytes must be Uint8Array')\r
                }\r
-               return [...bytes.values()]\r
+               const byteArray = [...bytes.values()]\r
+               padding -= bytes.byteLength\r
+               if (padding > 0) {\r
+                       const zeros = new Array(padding)\r
+                       byteArray.splice(0, 0, ...zeros)\r
+               }\r
+               return byteArray\r
        }\r
        /**\r
        * Converts a Uint8Aarray of bytes to a base32 string.\r
@@ -139,7 +146,7 @@ export class bytes {
        * @param {Uint8Array} bytes - Byte array to convert\r
        * @returns {string} Binary string representation of the input value\r
        */\r
-       static toBin (bytes: Uint8Array): string {\r
+       static toBin (bytes: Uint8Array<ArrayBuffer>): string {\r
                return [...bytes].map(b => b.toString(2).padStart(8, '0')).join('')\r
        }\r
 \r
@@ -171,6 +178,7 @@ export class bytes {
        * @returns {string} Hexadecimal string representation of the input bytes\r
        */\r
        static toHex (bytes: Uint8Array): string {\r
+               if (bytes.buffer instanceof ArrayBuffer && bytes.buffer.detached) return ''\r
                const byteArray = [...bytes].map(byte => byte.toString(16).padStart(2, '0'))\r
                return byteArray.join('').toUpperCase()\r
        }\r
@@ -181,7 +189,7 @@ export class bytes {
        * @param {Uint8Array} bytes - Byte array to convert\r
        * @returns {string} UTF-8 encoded text string\r
        */\r
-       static toUtf8 (bytes: Uint8Array): string {\r
+       static toUtf8 (bytes: Uint8Array<ArrayBuffer>): string {\r
                return new TextDecoder().decode(bytes)\r
        }\r
 }\r
@@ -214,7 +222,7 @@ export class dec {
        * @param {number} [padding=0] - Minimum length of the resulting array padded as necessary with starting 0x00 bytes\r
        * @returns {Uint8Array} Byte array representation of the input decimal\r
        */\r
-       static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array {\r
+       static toBytes (decimal: bigint | number | string, padding: number = 0): Uint8Array<ArrayBuffer> {\r
                if (decimal == null) {\r
                        throw new TypeError(`Failed to convert '${decimal}' from decimal to bytes`)\r
                }\r