Files
SnowLuma/packages/protocol/src/bridge-oidb.ts
T
DrSmoothl c16064d7e3 refactor(packages): rename @snowluma/bridge → @snowluma/protocol
The package previously named "bridge" houses the protocol-byte layer
(OIDB cmds, msg-push decoders, highway uploaders, element-builder,
event bus). The name "bridge" suggested transport/lifecycle, which it
isn't — that role lives in core/hook and core/bridge/manager. Rename
to match what's actually inside.

This is a pure rename: zero logic changes, all 73 import sites updated
mechanically and TS-verified. core/onebot dependencies key updated.
ESLint override path adjusted for the moved oidb-services directory.

Rename detection threshold: git log --find-renames=20% recommended
when following history across this commit — content-replacement
(@snowluma/bridge → @snowluma/protocol) dropped per-file similarity
below git's default 50% on import-heavy files.
2026-05-23 23:31:03 +08:00

70 lines
2.3 KiB
TypeScript

import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase, OidbBaseMeta } from '@snowluma/proto-defs/oidb';
// Sender capability is the minimal interface needed by runOidb. The
// concrete BridgeContext (and the Bridge class extending it) is
// structurally a superset, so callers keep passing `bridge` unchanged.
import type { OidbSender } from './oidb-service';
/**
* Build the OidbBase<T>-shaped TS value. Pure helper, no protobuf
* encoding happens here — pair with `encodeOidbEnv<T>` 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<T>(
oidbCmd: number,
subCmd: number,
body: T,
isUid: boolean = false,
): OidbBase<T> {
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<MyReq>(0x1253, 1, value);
* const buf = await runOidb(bridge, cmd, encodeOidbEnv<MyReq>(env));
* const resp = decodeOidbEnv<MyResp>(buf).body;
*
* Fire-and-check (no body decode):
* await runOidb(bridge, cmd, encodeOidbEnv<MyReq>(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(
sender: OidbSender,
cmd: string,
envelopeBytes: Uint8Array,
timeoutMs?: number,
): Promise<Uint8Array> {
const result = await sender.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<OidbBaseMeta>(bytes);
const code = meta?.errorCode;
if (code && code !== 0) {
throw new Error(`OIDB error ${code}: ${meta?.errorMsg ?? ''}`);
}
}
return bytes;
}