Files
SnowLuma/packages/proton/protobuf.d.ts
T
DrSmoothl 90f26ca4f4 fix(proto): emit state=0 on whole-ban unmute via new pb_optional marker
set_group_whole_ban false (unmute) failed with `OIDB error 1007` while
enabling worked (issue #70). OIDB (0x89A, 0) is shared by several commands
(SetSearch / SetAddOption / mute-all) disambiguated by body shape. Unmute
sends muteState.state=0, but proton omits zero scalars (proto3), leaving an
empty `muteState` the server can't classify. Enable sends 0xFFFFFFFF, which
is non-zero and survives — hence the asymmetry.

Add a `pb_optional<N, T>` marker (proto3 explicit presence): a scalar so
marked serialises even at its zero/default value. It is opt-in per field via
a NEW `explicitPresence` flag set ONLY by this marker — never reused from the
TS-`?`-derived `isOptional` (true for nearly every field). The encoder routes
explicit-presence scalars to a dedicated block; every `pb<>` / `pb_repeated<>`
field takes the untouched branches, so existing wire output is byte-identical
(proton's 99 existing tests, incl. byte-level round-trips, stay green).

Mark OidbMuteAllState.state `pb_optional<17, uint_32>` so unmute now puts
field 17 (=0) on the wire. New tests: proton pb_optional encode/round-trip
(incl. that sibling pb<> fields still omit zero), and a byte-level assertion
that mute-all unmute carries the state field.

Note: proton ships as a built Vite plugin (dist, gitignored); rebuild it
(`pnpm --filter @snowluma/proton build`) for the change to reach consumers.
2026-05-30 17:55:23 +08:00

70 lines
2.6 KiB
TypeScript

// ── Protobuf field markers ────────────────────────────────────────────
/** Marks a singular protobuf field: `name: pb<fieldNumber, Type>` */
export type pb<_ProtoNumber extends number, Type> = Type;
/** Marks a repeated protobuf field: `ids: pb_repeated<fieldNumber, Type>` → Type[] */
export type pb_repeated<_ProtoNumber extends number, Type> = Type[];
/**
* Marks a singular field with **explicit presence** (proto3 `optional`): it
* serialises whenever the value is present, INCLUDING its zero/default value
* — unlike `pb<>`, which omits zeros. Use when the wire peer needs the field
* present to disambiguate, e.g. an OIDB sub-command keyed on body shape where
* a zero must still appear on the wire. `name: pb_optional<fieldNumber, Type>`.
*/
export type pb_optional<_ProtoNumber extends number, Type> = Type;
// ── Protobuf primitive types ──────────────────────────────────────────
export type uint_32 = number;
export type int_32 = number;
export type uint_64 = bigint;
export type int_64 = bigint;
export type sint_32 = number;
export type sint_64 = bigint;
export type bool = boolean;
export type float = number;
export type double = number;
export type fixed_32 = number;
export type fixed_64 = bigint;
export type sfixed_32 = number;
export type sfixed_64 = bigint;
export type bytes = Uint8Array;
// ── Encode / decode (replaced at compile-time by the vite plugin) ────
export function protobuf_encode<T>(params: T): Uint8Array;
export function protobuf_decode<T>(data: Uint8Array): T;
// ── Optional runtime map fallback (disabled by default) ───────────────
export interface ProtobufRuntimeField {
name: string;
fieldNumber: number;
typeName: string;
wireType: 0 | 1 | 2 | 5;
isMessage: boolean;
isOptional: boolean;
isRepeated: boolean;
}
export interface ProtobufRuntimeMessage {
name: string;
fields: ProtobufRuntimeField[];
}
export interface ProtobufRuntimeCallSite {
file: string;
line: number;
column: number;
fnName: 'protobuf_encode' | 'protobuf_decode';
typeName: string;
}
export interface ProtobufRuntimeMap {
version: 1;
messages: ProtobufRuntimeMessage[];
callSites: ProtobufRuntimeCallSite[];
}
/** Enables map-based runtime dynamic code generation fallback. */
export function protobuf_enableRuntimeMapFallback(map: ProtobufRuntimeMap): void;
/** Disables map-based runtime fallback and clears generated function cache. */
export function protobuf_disableRuntimeMapFallback(): void;