/** * Low-level protobuf wire-format primitives: varint, zigzag, fixed32/64, * length-delimited bytes, and tag (field-number + wire-type) encoding. * * Mirrors `google.golang.org/protobuf/encoding/protowire` at the call sites * used by the schema-free `pb` codec. */ declare const enum WireType { Varint = 0, Fixed64 = 1, LengthDelimited = 2, StartGroup = 3, EndGroup = 4, Fixed32 = 5 } declare class Writer { private buf; private pos; constructor(initialCapacity?: number); private grow; /** Length so far. */ get length(): number; finish(): Uint8Array; /** Append a single byte. */ byte(b: number): void; /** Append raw bytes (without a length prefix). */ raw(bytes: Uint8Array): void; /** * Write an unsigned varint. Accepts a JS number for values up to 2^53, * or a bigint for the full uint64 range. */ varint(value: number | bigint): void; private varintBig; /** Write a signed varint with zigzag encoding (number variant). */ zigzag32(value: number): void; /** Write a signed varint with zigzag encoding (bigint variant). */ zigzag64(value: bigint): void; /** Little-endian fixed 32-bit unsigned integer. */ fixed32(value: number): void; /** Little-endian fixed 64-bit unsigned integer. */ fixed64(value: bigint): void; /** IEEE 754 32-bit float, little-endian. */ float(value: number): void; /** IEEE 754 64-bit double, little-endian. */ double(value: number): void; /** UTF-8 length-prefixed string. */ string(value: string): void; /** Length-prefixed byte sequence. */ bytes(value: Uint8Array): void; /** Tag = field_number << 3 | wire_type (encoded as varint). */ tag(fieldNumber: number, wireType: WireType): void; } declare class Reader { readonly data: Uint8Array; pos: number; private dv; constructor(data: Uint8Array, start?: number, end?: number); eof(): boolean; remaining(): number; /** Read an unsigned varint as bigint (always — caller narrows). */ varintBig(): bigint; /** Read an unsigned varint as a JS number. Throws if value > 2^53 - 1. */ varint(): number; /** Read a signed zigzag varint as a JS number (32-bit). */ zigzag32(): number; /** Read a signed zigzag varint as a bigint (64-bit). */ zigzag64(): bigint; /** Read 4 bytes little-endian as unsigned 32-bit. */ fixed32(): number; /** Read 8 bytes little-endian as bigint (uint64). */ fixed64(): bigint; float(): number; double(): number; /** Length-prefixed bytes — returns a copy. */ bytes(): Uint8Array; /** Length-prefixed bytes — returns a view into the underlying buffer (no copy). */ bytesView(): Uint8Array; /** UTF-8 length-prefixed string. */ string(): string; /** Decode a tag varint into { fieldNumber, wireType }. */ tag(): { fieldNumber: number; wireType: WireType; }; /** Skip the value of a field with the given wire type. */ skip(wireType: WireType): void; } /** * Schema-driven protobuf binary marshal/unmarshal. * * Mirrors the Go `encoding/pb` package's struct-tag approach, but with * an explicit field schema (TS has no struct tags or runtime field metadata). * * Wire-format choices match proto3 semantics: * - int32 / int64: plain varint, with negative values sign-extended to a * 10-byte uint64 (proto3 `int32` / `int64`). * - sint32 / sint64: zigzag varint (proto3 `sint32` / `sint64`); more * compact for negative values. * - uint32 / uint64: plain varint. * - bool: varint (0/1). * - float: fixed32; double: fixed64. * - string and bytes: length-delimited. * - nested messages: length-delimited. * - repeated fields: one tag+value per element (non-packed). * * Maps follow the proto3 spec: `repeated MapEntry { key = 1; value = 2; }`. */ type ScalarKind = "bool" | "int32" | "int64" | "sint32" | "sint64" | "uint32" | "uint64" | "float" | "double" | "string" | "bytes"; /** Map keys may not be float/double/bytes per the proto3 spec. */ type MapKeyKind = "bool" | "int32" | "int64" | "sint32" | "sint64" | "uint32" | "uint64" | "string"; type Kind = ScalarKind | { readonly message: CodecBase; }; interface FieldSpec { readonly number: number; readonly name: string; readonly kind: Kind; /** Mark a non-map field as repeated. Mutually exclusive with `mapKey`. */ readonly repeated?: boolean; /** Set to make this field a map. The field's `kind` is the value's kind. */ readonly mapKey?: MapKeyKind; } /** * Type-erased codec interface used internally for nested-message encoding. * Public users see {@link MessageCodec} which preserves the value type. */ interface CodecBase { readonly fields: ReadonlyArray; readonly byNumber: ReadonlyMap; create(): unknown; marshalInto(w: Writer, value: unknown): void; unmarshalFrom(r: Reader, end: number, into: unknown): void; } interface MessageCodec extends CodecBase { create(): T; marshal(value: T): Uint8Array; unmarshal(data: Uint8Array): T; } interface DefineMessageOpts { readonly fields: ReadonlyArray; /** Factory invoked by unmarshal to produce a target instance. Defaults to `{}`. */ readonly create?: () => T; } declare function defineMessage(opts: DefineMessageOpts): MessageCodec; declare function marshal(value: T, codec: MessageCodec): Uint8Array; declare function unmarshal(data: Uint8Array, codec: MessageCodec): T; export { type CodecBase as C, type DefineMessageOpts as D, type FieldSpec as F, type Kind as K, type MapKeyKind as M, Reader as R, type ScalarKind as S, WireType as W, type MessageCodec as a, Writer as b, defineMessage as d, marshal as m, unmarshal as u };