]> git.codecow.com Git - nano25519.git/commitdiff
Restore typed array captures.
authorChris Duncan <chris@codecow.com>
Thu, 15 Oct 2026 22:22:01 +0000 (15:22 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 15 Oct 2026 22:22:01 +0000 (15:22 -0700)
src/lib/bytes.ts

index d0aa12c866ca2040c8650496ef71e134f5b181e2..2a431efd9635d1373b30a2f0dc6760420a15ef3c 100644 (file)
@@ -18,10 +18,12 @@ import { Nano25519RangeError, Nano25519TypeError, } from './errors'
 export type Bytes = Uint8Array<ArrayBuffer>
 export type Words = Uint32Array<ArrayBuffer>
 
-const TypedArray = Object.getPrototypeOf(Uint8Array.prototype)
+const Bytes = Uint8Array
+const Words = Uint32Array
+const TypedArray = Object.getPrototypeOf(Bytes.prototype)
 
-const bytesSet = Function.prototype.call.bind(Uint8Array.prototype.set)
-const bytesFill = Function.prototype.call.bind(Uint8Array.prototype.fill)
+const bytesSet = Function.prototype.call.bind(Bytes.prototype.set)
+const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill)
 
 /** An accessor's getter, taking the object to read as its argument. */
 function getter (target: object, key: PropertyKey): (self: unknown) => any {
@@ -77,7 +79,7 @@ export function isBytes (a: unknown): a is Bytes {
 }
 
 /** True on little-endian system which aligns with WASM spec. */
-export const isLE = new Uint8Array(getBuffer(new Uint32Array([1])))[0] === 1
+export const isLE = new Bytes(getBuffer(new Words([1])))[0] === 1
 
 /**
  * Enables reading multiple bytes in a single load which is more efficient than
@@ -95,7 +97,7 @@ export function words (bytes: Bytes): Words {
        if (offset !== 0) {
                throw new Nano25519RangeError('Unexpected byte offset', { cause: offset })
        }
-       return new Uint32Array(getBuffer(bytes) as ArrayBuffer, offset, getByteLength(bytes) >> 2)
+       return new Words(getBuffer(bytes) as ArrayBuffer, offset, getByteLength(bytes) >> 2)
 }
 
 const charCodeAt = Function.prototype.call.bind(String.prototype.charCodeAt)
@@ -173,7 +175,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n
                if (value.length < (byteLengthMin << 1) || value.length > (byteLengthMax << 1)) {
                        throw new Nano25519TypeError(`Invalid hexadecimal length ${value.length} for ${name}`)
                }
-               if (typeof out === 'undefined') out = new Uint8Array(value.length >> 1)
+               if (typeof out === 'undefined') out = new Bytes(value.length >> 1)
                hexToBytes(out, value)
                return out
        }
@@ -186,7 +188,7 @@ export function normalize (name: string, byteLengthMin: number, byteLengthMax: n
                if (byteLength > byteLengthMax) {
                        throw new Nano25519TypeError(`${name} must be no more than ${byteLengthMax} bytes`)
                }
-               if (typeof out === 'undefined') out = new Uint8Array(byteLength)
+               if (typeof out === 'undefined') out = new Bytes(byteLength)
                copy(out, value)
                return out
        }