import { Check } from '@/util/validate.js' import { ByteRangeError, HexValidationError, IntegerBoundsError, SizeError, ValidationError } from '@/util/error.js' import type { Bytes } from '@/types.js' export namespace Assert { export function within_size ( data : number[] | Uint8Array, size : number ) : void { if (data.length > size) { throw new SizeError(`Data is larger than array size: ${data.length} > ${size}`) } } export function is_hex (hex : string) : void { if (hex.match(/[^a-fA-F0-9]/) !== null) { throw new HexValidationError(`Invalid characters in hex string: ${hex}`) } if (hex.length % 2 !== 0) { throw new HexValidationError(`Length of hex string is invalid: ${hex.length}`) } } export function is_bytes ( bytes : Bytes ) : asserts bytes is Bytes { if (!Check.is_bytes(bytes)) { throw new ByteRangeError(`Bytes contains invalid elements: ${String(bytes)}`) } } export function is_json (str : string) : void { try { JSON.parse(str) } catch { throw new ValidationError('JSON string is invalid!') } } export function is_safe_int (num : number) : void { if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) { throw new IntegerBoundsError('Number exceeds safe bounds!') } } }