// One deep entry point for a single OIDB round-trip: dispatch a // pre-encoded envelope via Bridge.sendRawPacket, validate the envelope // retCode, and return the raw response bytes. // // History note. The legacy implementation used a runtime // `makeOidbBaseSchema(InnerSchema)` factory and ran a runtime // schema-walking encoder. Proton is compile-time, so the envelope is // the generic interface `OidbBase` (proto/proton/oidb.ts). Proton's // wrapper-binding requires *pass-through* wrappers (the wrapper's first // arg must be forwarded unchanged to the inner codec — see // proton/src/transform/replacer.ts), so this file exposes thin // pass-through helpers (`encodeOidbEnv` / `decodeOidbEnv`) that // proton monomorphizes at every call site. `runOidb` itself is // non-generic — it just sends pre-encoded bytes and peeks at the // envelope retCode via the non-generic `OidbBaseMeta` view. import type { Bridge } from './bridge'; import { protobuf_decode } from '@snowluma/proton'; import type { OidbBase, OidbBaseMeta } from './proto/proton/oidb'; /** * Build the OidbBase-shaped TS value. Pure helper, no protobuf * encoding happens here — pair with `encodeOidbEnv` to produce the * wire bytes. * * The `isUid` flag sets the envelope `reserved` field to 1; despite the * name (kept for back-compat with the legacy API), `reserved = 1` * empirically signals the UIN-form variant of an OIDB call. Omit * (default false) for genuinely UID-keyed calls. */ export function makeOidbEnvelope( oidbCmd: number, subCmd: number, body: T, isUid: boolean = false, ): OidbBase { return { command: oidbCmd, subCommand: subCmd, errorCode: 0, body, errorMsg: '', reserved: isUid ? 1 : 0, }; } /** * Issue one OIDB request and return the raw response bytes. * * Typical usage: * const env = makeOidbEnvelope(0x1253, 1, value); * const buf = await runOidb(bridge, cmd, encodeOidbEnv(env)); * const resp = decodeOidbEnv(buf).body; * * Fire-and-check (no body decode): * await runOidb(bridge, cmd, encodeOidbEnv(env)); * * Always peeks at the envelope errorCode via the non-generic * `OidbBaseMeta` view so a server-side rejection cannot slip past * silently. Returns the raw response bytes (empty `Uint8Array` if the * packet carried no payload). */ export async function runOidb( bridge: Bridge, cmd: string, envelopeBytes: Uint8Array, timeoutMs?: number, ): Promise { const result = await bridge.sendRawPacket(cmd, envelopeBytes, timeoutMs); if (!result.success) throw new Error(result.errorMessage || 'packet send failed'); if (!result.gotResponse) throw new Error(result.errorMessage || 'no response'); const bytes = result.responseData ?? new Uint8Array(0); if (bytes.length > 0) { const meta = protobuf_decode(bytes); const code = meta?.errorCode; if (code && code !== 0) { throw new Error(`OIDB error ${code}: ${meta?.errorMsg ?? ''}`); } } return bytes; }