diff --git a/src/_assert.ts b/src/_assert.ts index 9826c22..fb75c97 100644 --- a/src/_assert.ts +++ b/src/_assert.ts @@ -1,13 +1,13 @@ function number(n: number) { - if (!Number.isSafeInteger(n) || n < 0) throw new Error(`Wrong positive integer: ${n}`); + if (!Number.isSafeInteger(n) || n < 0) throw new Error(`positive integer expected, not ${n}`); } function bool(b: boolean) { - if (typeof b !== 'boolean') throw new Error(`Expected boolean, not ${b}`); + if (typeof b !== 'boolean') throw new Error(`boolean expected, not ${b}`); } // copied from utils -function isBytes(a: unknown): a is Uint8Array { +export function isBytes(a: unknown): a is Uint8Array { return ( a instanceof Uint8Array || (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array') @@ -15,9 +15,9 @@ function isBytes(a: unknown): a is Uint8Array { } function bytes(b: Uint8Array | undefined, ...lengths: number[]) { - if (!isBytes(b)) throw new Error('Expected Uint8Array'); + if (!isBytes(b)) throw new Error('Uint8Array expected'); if (lengths.length > 0 && !lengths.includes(b.length)) - throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`); + throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`); } type Hash = { diff --git a/src/utils.ts b/src/utils.ts index ef9968e..03582db 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,6 +7,7 @@ // Makes the utils un-importable in browsers without a bundler. // Once node.js 18 is deprecated (2025-04-30), we can just drop the import. import { crypto } from '@noble/hashes/crypto'; +import { isBytes, bytes as abytes } from './_assert.js'; // prettier-ignore export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array | @@ -17,13 +18,6 @@ export const u8 = (arr: TypedArray) => new Uint8Array(arr.buffer, arr.byteOffset export const u32 = (arr: TypedArray) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4)); -function isBytes(a: unknown): a is Uint8Array { - return ( - a instanceof Uint8Array || - (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array') - ); -} - // Cast array to view export const createView = (arr: TypedArray) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength); @@ -44,6 +38,8 @@ export const byteSwap = (word: number) => // Conditionally byte swap if on a big-endian platform export const byteSwapIfBE = isLE ? (n: number) => n : (n: number) => byteSwap(n); +export { isBytes }; + // In place byte swap for Uint32Array export function byteSwap32(arr: Uint32Array) { for (let i = 0; i < arr.length; i++) { @@ -59,7 +55,7 @@ const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => * @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123' */ export function bytesToHex(bytes: Uint8Array): string { - if (!isBytes(bytes)) throw new Error('Uint8Array expected'); + abytes(bytes); // pre-caching improves the speed 6x let hex = ''; for (let i = 0; i < bytes.length; i++) { @@ -136,7 +132,7 @@ export type Input = Uint8Array | string; */ export function toBytes(data: Input): Uint8Array { if (typeof data === 'string') data = utf8ToBytes(data); - if (!isBytes(data)) throw new Error(`expected Uint8Array, got ${typeof data}`); + abytes(data); return data; } @@ -147,7 +143,7 @@ export function concatBytes(...arrays: Uint8Array[]): Uint8Array { let sum = 0; for (let i = 0; i < arrays.length; i++) { const a = arrays[i]; - if (!isBytes(a)) throw new Error('Uint8Array expected'); + abytes(a); sum += a.length; } const res = new Uint8Array(sum); diff --git a/test/utils.test.js b/test/utils.test.js index 8f35634..5a8e9c9 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,7 +1,7 @@ const assert = require('assert'); const { should } = require('micro-should'); const { optional, integer, gen } = require('./generator'); -const { byteSwap, byteSwapIfBE, byteSwap32, isLE } = require('../utils.js'); +const { byteSwap, byteSwapIfBE, byteSwap32, isLE } = require('../utils'); // Here goes test for tests... should(`Test generator`, () => {