Files
SnowLuma/packages/proton/src/runtime-map.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

73 lines
2.1 KiB
TypeScript

import type { MessageRegistry, ProtobufField, ProtobufMessage } from './ast/types.js';
export type RuntimeMapFnName = 'protobuf_encode' | 'protobuf_decode';
export interface RuntimeMapCallSite {
file: string;
line: number;
column: number;
fnName: RuntimeMapFnName;
typeName: string;
}
export interface ProtobufRuntimeMap {
version: 1;
messages: ProtobufMessage[];
callSites: RuntimeMapCallSite[];
}
function cloneField(field: ProtobufField): ProtobufField {
return {
name: field.name,
fieldNumber: field.fieldNumber,
typeName: field.typeName,
wireType: field.wireType,
isMessage: field.isMessage,
isOptional: field.isOptional,
isRepeated: field.isRepeated,
explicitPresence: field.explicitPresence ?? false,
};
}
function cloneMessage(msg: ProtobufMessage): ProtobufMessage {
return {
name: msg.name,
fields: msg.fields.map(cloneField),
};
}
export function registryToRuntimeMapMessages(registry: MessageRegistry): ProtobufMessage[] {
return [...registry.values()].map(cloneMessage);
}
export function runtimeMapToRegistry(map: ProtobufRuntimeMap): MessageRegistry {
const registry: MessageRegistry = new Map();
for (const msg of map.messages) {
registry.set(msg.name, cloneMessage(msg));
}
return registry;
}
export function normalizeRuntimeMapPath(filePath: string): string {
const noQuery = filePath.replace(/[?#].*$/, '');
return noQuery.replace(/\\/g, '/').replace(/^file:\/\//, '');
}
export function createRuntimeMap(map: { messages: MessageRegistry; callSites: RuntimeMapCallSite[] }): ProtobufRuntimeMap {
const dedupedCallSites = new Map<string, RuntimeMapCallSite>();
for (const cs of map.callSites) {
const normalized = {
...cs,
file: normalizeRuntimeMapPath(cs.file),
};
const key = `${normalized.file}:${normalized.line}:${normalized.column}:${normalized.fnName}:${normalized.typeName}`;
dedupedCallSites.set(key, normalized);
}
return {
version: 1,
messages: registryToRuntimeMapMessages(map.messages),
callSites: [...dedupedCallSites.values()],
};
}