feat(proton): finish OIDB migration — runOidb on protobuf_encode<OidbBase<T>>
Closes the OIDB phase of the proton migration. Every consumer that
talked to the OIDB service through the old runtime
`makeOidbBaseSchema()` factory now goes through the compile-time
generic `OidbBase<T>` instead — proton monomorphizes the codec per
call-site instantiation.
@snowluma/proton — two analyzer fixes uncovered by the migration:
1. Skip queuing call-site type-args that reference an *enclosing
generic function's* type parameter for monomorphization. Before
this, a wrapper like
export function encodeOidbEnv<T>(env: OidbBase<T>): Uint8Array {
return protobuf_encode<OidbBase<T>>(env);
}
would cause the analyzer to mint a literal `OidbBase__T` registry
entry with `body.typeName = 'T'`, which the post-walk wire-type
guard then tripped on. The enclosing-scope type params are tracked
on a stack as the walker descends into FunctionDeclaration,
FunctionExpression, ArrowFunction, MethodDeclaration nodes; matching
call sites are skipped (the wrapper-binding path is still
responsible for materialising concrete codecs at downstream call
sites).
2. Register *explicitly* empty interfaces (`interface X {}`) as
zero-field proto messages. OIDB has a handful of these
('this call has no meaningful response body' placeholders —
Oidb0xeb7Resp, Oidb0xcd4Resp, Oidb0x112aResp, Oidb0x8a0Resp,
Oidb0xf16Resp, OidbClientKeyReq). Proto3 wire allows zero-field
messages, and downstream code does `protobuf_decode<OidbBase<X>>`
where X is one of these — the wire-type guard would otherwise trip
on the missing `X` registration. Interfaces that have members but
no pb-marked fields are still NOT registered.
@snowluma/core — the OIDB-layer refactor itself:
bridge-oidb.ts ditches the runtime ProtoSchema factory and exposes:
- makeOidbEnvelope<T>(oidbCmd, subCmd, body, isUid) pure helper
- encodeOidbEnv<T>(env) pass-through
- decodeOidbEnv<T>(bytes) pass-through
- runOidb(bridge, cmd, envelopeBytes) non-generic raw
`encodeOidbEnv` / `decodeOidbEnv` are pure pass-through generic
wrappers — proton wrapper-binds them and substitutes every call site
with the inlined `protobuf_encode_OidbBase__X` / `protobuf_decode_…`
function. `runOidb` is non-generic (proton leaves it alone) so it can
be mocked in tests. `makeOidbEnvelope` is plain TS — also mockable for
introspection.
56 `runOidb` call sites across 11 files migrated to the new API:
- bridge-contacts.ts, actions/extras.ts, actions/friend.ts,
actions/group-admin.ts, actions/group-file.ts,
actions/group-message.ts, actions/interaction.ts,
actions/misc.ts, actions/profile.ts, web-actions/cookies.ts,
highway/pipeline.ts
pipeline.ts also drops `makeOidbBaseSchema(NTV2UploadRichMediaReqSchema)`
and the residual `protoEncode({...}, EncodableMediaMsgInfoSchema)` —
both go through proton now.
Tests:
- 8 test files migrated from `vi.mocked(decodeOidbEnv).mockReturnValueOnce`
to `vi.mocked(runOidb).mockResolvedValueOnce(protobuf_encode<OidbBase<X>>({body: …}))`
— the proton substitution means the old mock layer is bypassed at
build time, so the only mockable point is the bytes layer at `runOidb`.
- 2 new parity tests pinning `OidbBase<T>` ↔ legacy
`makeOidbBaseSchema(InnerSchema)` byte equivalence.
Verification:
- @snowluma/proton: 95/95 tests pass, tsc --noEmit clean
- @snowluma/core: 605/605 tests pass (network-manager.test.ts
fails to load due to a missing darwin-arm64 native binary in this
environment — pre-existing), tsc --noEmit clean
This commit is contained in:
25 files changed
+1144
-977
No files matched your search
@@ -1,96 +1,101 @@
|
||||
// One deep entry point for a single OIDB round-trip: build the envelope,
|
||||
// dispatch via Bridge.sendRawPacket, validate the response status, and
|
||||
// optionally decode the body. Replaces the three positional helpers
|
||||
// (makeOidbRequest / sendOidbAndCheck / sendOidbAndDecode) that used to
|
||||
// scatter the same five-step pattern across every action file.
|
||||
// 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<T>` (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<T>` / `decodeOidbEnv<T>`) 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 type { ProtoSchema } from '../protobuf/decode';
|
||||
import { protoEncode, protoDecode } from '../protobuf/decode';
|
||||
import { makeOidbBaseSchema } from './proto/oidb';
|
||||
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
|
||||
import type { OidbBase, OidbBaseMeta } from './proto/proton/oidb';
|
||||
|
||||
export interface OidbCall<TResp = void> {
|
||||
/** Service-cmd string sent on the wire, e.g. 'OidbSvcTrpcTcp.0x1253_1'. */
|
||||
cmd: string;
|
||||
/** OIDB command number, e.g. 0x1253. */
|
||||
oidbCmd: number;
|
||||
/** OIDB sub-command, e.g. 1. */
|
||||
subCmd: number;
|
||||
request: {
|
||||
schema: ProtoSchema;
|
||||
value: any;
|
||||
/**
|
||||
* Sets the OIDB envelope `reserved` field to 1 when true.
|
||||
*
|
||||
* The name is historical and misleading: empirically (see Lagrange
|
||||
* FetchStrangerByUin vs FetchStrangerByUid, fetchGroupList, our own
|
||||
* highway uploads) the `reserved = 1` flag indicates the UIN-form
|
||||
* variant of an OIDB call — it tells newer QQ NT to translate the
|
||||
* UIN in the body to an internal UID rather than running the
|
||||
* UID-form validator that expects a uid/openid string in the body.
|
||||
*
|
||||
* Omit (default false) for genuinely UID-keyed calls.
|
||||
*/
|
||||
isUid?: boolean;
|
||||
};
|
||||
/** Omit to skip decoding the response body (fire-and-check). */
|
||||
response?: {
|
||||
schema: ProtoSchema;
|
||||
/**
|
||||
* 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. Without `response`, returns void after
|
||||
* verifying the envelope's retCode. With `response`, decodes the
|
||||
* envelope, validates retCode, and returns the inner body.
|
||||
*
|
||||
* Throws on transport failure, missing response, decode failure, or any
|
||||
* non-zero envelope retCode. Action modules layer their own retCode
|
||||
* checks (on inner sub-replies like `upload.retCode`) on top of this.
|
||||
* Pass-through encoder for the OIDB envelope. Single type param, body
|
||||
* literally forwards its arg to `protobuf_encode<OidbBase<T>>` — proton
|
||||
* wrapper-binds this and replaces every `encodeOidbEnv<MyReq>(env)` call
|
||||
* site with `protobuf_encode_OidbBase__MyReq(env)` at build time.
|
||||
*/
|
||||
export function runOidb(bridge: Bridge, call: OidbCall<void> & { response?: undefined }): Promise<void>;
|
||||
export function runOidb<TResp>(bridge: Bridge, call: OidbCall<TResp> & { response: { schema: ProtoSchema } }): Promise<TResp>;
|
||||
export async function runOidb<TResp>(bridge: Bridge, call: OidbCall<TResp>): Promise<TResp | void> {
|
||||
const envelope = encodeOidbEnvelope(call);
|
||||
const result = await bridge.sendRawPacket(call.cmd, envelope);
|
||||
export function encodeOidbEnv<T>(env: OidbBase<T>): Uint8Array {
|
||||
return protobuf_encode<OidbBase<T>>(env);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass-through decoder for the OIDB envelope. Mirror of `encodeOidbEnv`
|
||||
* — proton monomorphizes `OidbBase<TResp>` per call site.
|
||||
*
|
||||
* Returns the full envelope; the caller usually wants `.body`.
|
||||
*/
|
||||
export function decodeOidbEnv<T>(bytes: Uint8Array): OidbBase<T> {
|
||||
return protobuf_decode<OidbBase<T>>(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(
|
||||
bridge: Bridge,
|
||||
cmd: string,
|
||||
envelopeBytes: Uint8Array,
|
||||
): Promise<Uint8Array> {
|
||||
const result = await bridge.sendRawPacket(cmd, envelopeBytes);
|
||||
if (!result.success) throw new Error(result.errorMessage || 'packet send failed');
|
||||
|
||||
if (call.response) {
|
||||
if (!result.gotResponse || !result.responseData) {
|
||||
throw new Error(result.errorMessage || 'no response');
|
||||
}
|
||||
const baseSchema = makeOidbBaseSchema(call.response.schema);
|
||||
const resp = protoDecode(result.responseData, baseSchema);
|
||||
if (!resp) throw new Error('failed to decode OIDB response');
|
||||
throwIfOidbError(resp);
|
||||
return (resp as any).body as TResp;
|
||||
}
|
||||
|
||||
// Fire-and-check: response data is optional, but if present we still
|
||||
// peek at the envelope so a server-side retCode doesn't silently slip past.
|
||||
if (!result.gotResponse) throw new Error(result.errorMessage || 'no response');
|
||||
if (result.responseData && result.responseData.length > 0) {
|
||||
const emptyBase = makeOidbBaseSchema({ _dummy: { field: 99, type: 'uint32' as const } });
|
||||
const resp = protoDecode(result.responseData, emptyBase);
|
||||
if (resp) throwIfOidbError(resp);
|
||||
}
|
||||
}
|
||||
|
||||
function encodeOidbEnvelope(call: OidbCall<unknown>): Uint8Array {
|
||||
const baseSchema = makeOidbBaseSchema(call.request.schema);
|
||||
return protoEncode({
|
||||
command: call.oidbCmd,
|
||||
subCommand: call.subCmd,
|
||||
errorCode: 0,
|
||||
body: call.request.value,
|
||||
errorMsg: '',
|
||||
reserved: call.request.isUid ? 1 : 0,
|
||||
}, baseSchema);
|
||||
}
|
||||
|
||||
function throwIfOidbError(resp: unknown): void {
|
||||
const code = (resp as any).errorCode;
|
||||
if (code && code !== 0) {
|
||||
throw new Error(`OIDB error ${code}: ${(resp as any).errorMsg ?? ''}`);
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user