import { CUSTOM_INSPECT, Inspect, InspectOptions } from "./inspect.mjs"; import { Pretty } from "@cto.af/utils"; //#region src/reader.d.ts type FieldType = number | bigint | string | boolean | Uint8Array | FieldType[]; interface ReaderOptions { /** Initial offset. The reset() method will ignore this. */ offset?: number; /** Read in littleEndian order, which is OPPOSITE from Network Byte Order. */ littleEndian?: boolean; /** If there is an error in UTF8 encoding, use the replacement character. */ ignoreUTF8errors?: boolean; /** If true, do not throw exception on truncation. */ allowTruncation?: boolean; } type RequiredRederOptions = Required; interface NumberReader { read: 'u8' | 'u16' | 'u32' | 'i8' | 'i16' | 'i32' | 'f16' | 'f32' | 'f64'; littleEndian?: boolean; convert?(n: number, tmp: Temp, dvr: DataViewReader): W; } interface BigIntReader { read: 'u64' | 'i64'; littleEndian?: boolean; convert?(n: bigint, tmp: Temp, dvr: DataViewReader): W; } interface StringReader { read: 'ascii' | 'utf8'; length(temp: Temp, dvr: DataViewReader): number; convert?(n: string, tmp: Temp, dvr: DataViewReader): W; } interface BytesReader { read: 'bytes'; length(tmp: Temp, dvr: DataViewReader): number; convert?(n: Uint8Array, tmp: Temp, dvr: DataViewReader): W; } interface ConstantReader { read: 'constant'; value(tmp: Temp, dvr: DataViewReader): W; } type Reader = NumberReader | BigIntReader | StringReader | BytesReader | ConstantReader; type ReaderType = T extends { convert(n: any): infer W; } ? W : T extends ConstantReader ? X : T extends NumberReader ? number : T extends BigIntReader ? bigint : T extends StringReader ? string : T extends BytesReader ? Uint8Array : never; interface Temp { [K: string]: unknown; } declare const SIZE: unique symbol; type StructDefinition = { [K: string]: Reader; } & { [SIZE]?: number; }; type Struct = Pretty<{ [K in keyof T as K extends `_${string}` ? never : K]: ReaderType }>; /** * Treat a Uint8Array as a stream to read typed data from, sequentially. * Note that this is not an actual ReadableStream in either the nodejs * sense or the web sense. */ declare class DataViewReader { #private; static readonly defaultOptions: RequiredRederOptions; /** * Invalid value for reading a signed 64-bit integer. This will allow * all bitflags to still be zero while signalling the error. Effectively * NaN for 64-bit bigints. */ static readonly BAD_I64: bigint; /** * Construct new stream. Relatively lightweight, creating a new DataView * over the Uint8Array's ArrayBuffer is the heaviest part. * * @param bytes Bytes to read. * @param opts Options. */ constructor(bytes: Uint8Array, opts?: ReaderOptions); get littleEndian(): boolean; set littleEndian(val: boolean); /** * Original bytes. * * @returns Original. */ get original(): Uint8Array; /** * Current offset. * * @returns Current. */ get offset(): number; /** * Have we read all the bytes yet? * @returns True if finished. */ get finished(): boolean; /** * Is truncation allowed? * * @returns Truncation allowed. */ get allowTruncation(): boolean; /** * Set truncation mode. May not turn it off, once it's on. */ set allowTruncation(val: boolean); /** * If true, truncation is allowed, and this reader has detected truncation. * @returns Truncation state. */ get truncated(): boolean; /** * Allowed to be set from outside the class if some higher layer wants to * stop all further reads. May only be set to true. */ set truncated(val: boolean); /** * Go to a particular offset in the buffer. * * @param offset The new offset. * @throws {Error} If truncation is allowed, since truncation state would be * lost. */ seek(offset?: number): void; /** * Reset to the beginning of the input. Ignores the initial offset, so if * you want to restart at the same place, call seek(initialOffset). * Sets the truncation state back to false. */ reset(): void; /** * All of the bytes that have not been used yet. If complete, returns * an empty array. * * @returns Byte array. */ unused(): Uint8Array; /** * Skip some number of bytes without manipulating them. * * @param length Number of bytes to skip. */ skip(length: number): void; /** * Get a chunk of the original buffer. * * Advances the current read position by length bytes. * * @param length How many bytes? * @returns A subarray of the original buffer, without copying. */ bytes(length: number): Uint8Array; /** * Get a chunk of the buffer as 8-bit ASCII text. This is only useful for * ancient protocols such as DNS. If the top bit is set, you get equivalent * Unicode characters, which should be Latin-1. * * Advances the current read position by length bytes. * * @param length Number of bytes. * @returns String. */ ascii(length: number): string; /** * Read a number of *bytes* as a UTF-8 encoded string. Use the * ignoreUTF8errors option to avoid throwing exceptions on invalid UTF-8 * and get replacement characters instead. However, by default, Postel * was wrong. * * Advances the current read position by length bytes. * * @param length Number of bytes. * @returns Unicode string. */ utf8(length: number): string; /** * Get an unsigned byte. Advances the current read position by 1 byte. * Returns NaN if truncation is allowed and the packet was truncated. * * @returns Number. */ u8(): number; /** * Get a two-byte unsigned integer. Advances the current read position by 2 * bytes. Returns NaN if truncation is allowed and the packet was truncated. * * @param littleEndian Override stream's endianness. * @returns Number. */ u16(littleEndian?: boolean): number; /** * Get a four-byte unsigned integer. Advances the current read position by 4 * bytes. Returns NaN if truncation is allowed and the packet was truncated. * * @param littleEndian Override stream's endianness. * @returns Number. */ u32(littleEndian?: boolean): number; /** * Get an eight-byte unsigned integer. Advances the current read position by 8 * bytes. Returns -1n if truncation is allowed and the packet was truncated. * * @param littleEndian Override stream's endianness. * @returns Bigint, since 2**64 > 2**53. */ u64(littleEndian?: boolean): bigint; /** * Get a signed byte. Advances the current read position by 1 byte. * Returns NaN if truncation is allowed and the packet was truncated. * * @returns Number. */ i8(): number; /** * Get a two-byte signed integer. Advances the current read position by 2 * bytes. Returns NaN if truncation is allowed and the packet was truncated. * * @param littleEndian Override stream's endianness. * @returns Number. */ i16(littleEndian?: boolean): number; /** * Get a four-byte signed integer. Advances the current read position by 4 * bytes. Returns NaN if truncation is allowed and the packet was truncated. * * @param littleEndian Override stream's endianness. * @returns Number. */ i32(littleEndian?: boolean): number; /** * Get a eight-byte signed integer. Advances the current read position by 8 * bytes. Returns DataViewReader.BAD_I64 if truncation is allowed and the * packet was truncated. * * @param littleEndian Override stream's endianness. * @returns Bigint. */ i64(littleEndian?: boolean): bigint; /** * Get a half-precision floating point number. On older JS runtimes, uses a * local implementation of f16. Returns NaN if truncation is allowed and * the packet was truncated. * * Advances the current read position by 2 bytes. * * @param littleEndian Override stream's endianness. * @returns Number. */ f16(littleEndian?: boolean): number; /** * Get a single-precision floating point number. Returns NaN if truncation * is allowed and the packet was truncated. * * Advances the current read position by 4 bytes. * * @param littleEndian Override stream's endianness. * @returns Number. */ f32(littleEndian?: boolean): number; /** * Get a double-precision floating point number. Returns NaN if truncation * is allowed and the packet was truncated. * * Advances the current read position by 8 bytes. * * @param littleEndian Override stream's endianness. * @returns Number. */ f64(littleEndian?: boolean): number; /** * Convenience function to repeat reading a given number of times. * * @param num Number of times to call fn. * @param fn Function that reads. * @returns Array of results. */ times(num: number, fn: (n: number) => T): T[]; struct(description: T): Struct; /** * If the current buffer has not been completely read, throws an error. * Does not throw error if truncation is allowed. * * @throws {ExtraBytesError} When extra data. */ complete(): void; /** * When outputting with `console.log('%O', this)` or at the node REPL, * see the current chunk structure. * * @param depth Current depth. * @param options Options for writing, generated by util.inpect. * @param inspect Local copy of util.inspect, so there is no node dependency. * @returns Formatted string. */ [CUSTOM_INSPECT](depth: number, options: InspectOptions, inspect: Inspect): string; } //#endregion export { BigIntReader, BytesReader, ConstantReader, DataViewReader, FieldType, NumberReader, Reader, ReaderOptions, ReaderType, RequiredRederOptions, SIZE, StringReader, Struct, StructDefinition, Temp };