import { DataViewReader, FieldType } from "./reader.mjs"; //#region src/packet.d.ts interface NumStartFinish { start: number; finish?: number; } interface BigStartFinish { start: bigint; finish?: bigint; } interface FlagSet { set: { [flag: string]: number; }; } interface BigFlagSet { set: { [flag: string]: bigint; }; } interface SimpleBitsConfig { convert?(value: V, name: string): W; } type BitsConfig = From extends (number | undefined) ? (NumStartFinish | FlagSet) & SimpleBitsConfig : From extends (bigint | undefined) ? (BigStartFinish | BigFlagSet) & SimpleBitsConfig : never; interface EasyReadOpts { /** * If specified, override the endiannes of the stream. */ littleEndian?: boolean; } type ConvertReadOpts = [G] extends [F] ? { convert?(value: F, name: string, temp: boolean): G; } : { /** * If specified, run the results through the given function before storing. * Required if the destination type is not the same as the read type. * * @param value Original read value. * @param name The field being stored to. May be ignored. * @param temp Is the field being stored to in temp rather than packet? * @returns Converted value. */ convert(value: F, name: string, temp: boolean): G; }; type ReadOpts = EasyReadOpts & ConvertReadOpts & { /** * If true, write to temp instead of packet. */ temp?: boolean; }; type NotTemp = EasyReadOpts & ConvertReadOpts & { temp?: false; }; type HasTemp = EasyReadOpts & ConvertReadOpts & { temp: true; }; type MatchingType = T[V] extends (U | undefined) ? V : never; /** * Capture fields from a packet in a way that allows accessing the previously- * read fields while reading subsequent fields. * * @template T Structure of the packet. * @template U Structure for other temporary fields that you want to reference, * but don't want in the final packet. */ declare class Packet { #private; constructor(reader: DataViewReader); /** * Is the packet in littleEndian mode by default? * * @type {boolean} */ get littleEndian(): boolean; set littleEndian(val: boolean); /** * Packet. Only the fields that you have already read may be accessed. * * @returns Possibly-incomplete packet, even though the type is complete. */ get packet(): T; /** * The current offset into the reader. * * @type {number} */ get offset(): number; /** * How many bytes are left to be read? * * @type {number} */ get left(): number; /** * Temporary storage. Only the fields that you have already read may be * accessed. * * @returns Possibly-incomplete temp data, even though the type is complete. */ get temp(): U; /** * Get the truncation mode of the underlying reader. * * @returns True if truncation allowed. */ get allowTruncation(): boolean; /** * Sets the truncation mode of the underlying reader. May not be set to * false. */ set allowTruncation(val: boolean); /** * Is this underlying reader truncated? * * @returns True if truncated. */ get truncated(): boolean; /** * Some higher-level processor has detected truncation. Must not be set * to false. */ set truncated(val: boolean); /** * Reset all packet data, temp data, and return reader to the start. * Mostly useful for testing. * * @returns This, for chaining. */ reset(): this; /** * Assert that all of the data been read. Throws an exception if extra * data. * * @returns This, for chaining. */ complete(): this; /** * Turn on truncation for this stream. * * @returns This, for chaining. */ enableTruncation(): this; /** * Skip over some bytes in the stream. * * @param length Number of bytes to skip. * @returns This, for chaining. */ skip(length: number): this; /** * Store all of the data that has yet to be read. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ unused(name: V, opts: NotTemp): this; unused(name: MatchingType): this; unused(name: V, opts: HasTemp): this; /** * Store some number of bytes. * * @param name Field to write to in packet or temp. * @param len Number of bytes to read. * @param opts Read options. * @returns This, for chaining. */ bytes(name: V, len: number, opts: NotTemp): this; bytes(name: MatchingType, len: number): this; bytes(name: V, len: number, opts: HasTemp): this; /** * Store some number of bytes, interpreted as an ASCII string. * * @param name Field to write to in packet or temp. * @param len Number of bytes to read. * @param opts Read options. * @returns This, for chaining. */ ascii(name: V, len: number, opts: NotTemp): this; ascii(name: MatchingType, len: number): this; ascii(name: V, len: number, opts: HasTemp): this; /** * Store some number of bytes, interpreted as a UTF8 string. * * @param name Field to write to in packet or temp. * @param len Number of bytes to read. * @param opts Read options. * @returns This, for chaining. */ utf8(name: V, len: number, opts: NotTemp): this; utf8(name: MatchingType, len: number): this; utf8(name: V, len: number, opts: HasTemp): this; /** * Store an unsigned 8 bit integer. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ u8(name: V, opts: NotTemp): this; u8(name: MatchingType): this; u8(name: V, opts: HasTemp): this; /** * Store an unsigned 16 bit integer. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ u16(name: V, opts: NotTemp): this; u16(name: MatchingType): this; u16(name: V, opts: HasTemp): this; /** * Store an unsigned 32 bit integer. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ u32(name: V, opts: NotTemp): this; u32(name: MatchingType): this; u32(name: V, opts: HasTemp): this; /** * Store an unsigned 64 bit integer as a bigint. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ u64(name: V, opts: NotTemp): this; u64(name: MatchingType): this; u64(name: V, opts: HasTemp): this; /** * Store a signed 8 bit integer. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ i8(name: V, opts: NotTemp): this; i8(name: MatchingType): this; i8(name: V, opts: HasTemp): this; /** * Store a signed 16 bit integer. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ i16(name: V, opts: NotTemp): this; i16(name: MatchingType): this; i16(name: V, opts: HasTemp): this; /** * Store a signed 32 bit integer. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ i32(name: V, opts: NotTemp): this; i32(name: MatchingType): this; i32(name: V, opts: HasTemp): this; /** * Store a signed 64 bit integer as a bigint. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ i64(name: V, opts: NotTemp): this; i64(name: MatchingType): this; i64(name: V, opts: HasTemp): this; /** * Store a 16 bit float. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ f16(name: V, opts: NotTemp): this; f16(name: MatchingType): this; f16(name: V, opts: HasTemp): this; /** * Store a 32 bit float. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ f32(name: V, opts: NotTemp): this; f32(name: MatchingType): this; f32(name: V, opts: HasTemp): this; /** * Store a 64 bit float. * * @param name Field to write to in packet or temp. * @param opts Read options. * @returns This, for chaining. */ f64(name: V, opts: NotTemp): this; f64(name: MatchingType): this; f64(name: V, opts: HasTemp): this; /** * Convenience function to repeat reading a given number of times. * * @param name Packet field name to read into, as an array. * @param num Number of times to call fn. * @param fn Function that reads. * @param opts Read options. * @returns This, for chaining. */ times(name: V, num: number, fn: (n: number) => FieldType, opts: NotTemp): this; times(name: MatchingType, num: number, fn: (n: number) => FieldType): this; times(name: V, num: number, fn: (n: number) => FieldType, opts: HasTemp): this; /** * Convenience function to perhaps execute a read. Does not call the * function if input was truncated. * * @param doIt Should fn be executed? * @param fn Run if doIt is true. * @returns This, for chaining. */ maybe(doIt: boolean, fn: () => void): this; /** * Repeat the given read until a condition fails. * * @param name Packet field name to read into, as an array. * @param keepGoing While this function returns true, keep calling read. * @param read The value returned from this function is added to the array. * @param opts Read options. */ while(name: V, keepGoing: (iteration: number, r: DataViewReader) => boolean, read: (iteration: number, r: DataViewReader) => T[V] extends (Iterable | undefined) ? E : never, opts?: EasyReadOpts): this; while(name: V, keepGoing: (iteration: number, r: DataViewReader) => boolean, read: (iteration: number, r: DataViewReader) => U[V] extends (Iterable | undefined) ? E : never, opts: HasTemp): this; /** * Copy some of the bits from one existing field to another. Does * not work for fields larger than 53 bits. For fields larger than * 32 bits, use bigints, as returned from u64(). Should only be * applied to unsigned from fields. Bits are numbered with 0 on the * right, MSB on the left. Start and finish can be in either order. * * @param desc Description of bits to capture. */ bits(desc: { from: T[V] extends number | bigint | undefined ? V : never; to: W; } & BitsConfig): this; bits(desc: { fromTemp: U[V] extends number | bigint ? V : never; to: W; } & BitsConfig): this; bits(desc: { from: T[V] extends number | bigint ? V : never; toTemp: W; } & BitsConfig): this; bits(desc: { fromTemp: U[V] extends number | bigint ? V : never; toTemp: W; } & BitsConfig): this; /** * Store a constant to the packet. * * @param name Field to write to in packet or temp. * @param val Any constant value. * @param opts Read options. */ constant(name: V, val: F, opts: NotTemp): this; constant(name: MatchingType, val: F): this; constant(name: V, val: F, opts: HasTemp): this; } //#endregion export { BigFlagSet, BigStartFinish, BitsConfig, ConvertReadOpts, DataViewReader, EasyReadOpts, type FieldType, FlagSet, HasTemp, MatchingType, NotTemp, NumStartFinish, Packet, ReadOpts, SimpleBitsConfig };