refactor(oidb): replace 3 positional helpers with one named runOidb
bridge-oidb.ts used to expose three thin functions —
makeOidbRequest / sendOidbAndCheck / sendOidbAndDecode — and every
action file repeated the same five-step envelope assembly with long
positional argument lists. Collapse them into one `runOidb(bridge,
call)` taking an OidbCall object: cmd / oidbCmd / subCmd / request
{ schema, value, isUid? } / response? { schema }.
Behavioural contract is preserved:
- No `response`: verify success + gotResponse, peek envelope retCode
iff response data is present (fire-and-check semantics).
- With `response`: require responseData, decode through the base
schema, validate retCode, return the inner body typed as TResp.
Migration touches 51 call sites across 9 source files (bridge-contacts,
actions/{group-file, group-admin, profile, group-message, friend,
interaction, misc}, web-actions/cookies) plus 9 test files that used
to assert on positional `.mock.calls[0]![4]` indices and now read
through `.mock.calls[0]![1].request.value` / `.cmd` / `.oidbCmd`.
Net effect: callers now name what each piece is for, and adding
cross-cutting OIDB behaviour (retry / timeout / per-cmd logging) is a
single-site change inside runOidb instead of a sweep across 9 files.
This commit is contained in:
19 files changed
+637
-549
No files matched your search
@@ -1,69 +1,90 @@
|
||||
// OIDB protocol helpers -- encode requests, send+check, send+decode.
|
||||
// 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.
|
||||
|
||||
import type { Bridge } from './bridge';
|
||||
import type { ProtoSchema } from '../protobuf/decode';
|
||||
import { protoEncode, protoDecode } from '../protobuf/decode';
|
||||
import { makeOidbBaseSchema } from './proto/oidb';
|
||||
|
||||
/**
|
||||
* Build a raw OIDB request buffer (base envelope wrapping a typed body).
|
||||
*/
|
||||
export function makeOidbRequest(
|
||||
command: number, subCommand: number,
|
||||
body: any, bodySchema: ProtoSchema, isUid = false,
|
||||
): Uint8Array {
|
||||
const baseSchema = makeOidbBaseSchema(bodySchema);
|
||||
return protoEncode({
|
||||
command,
|
||||
subCommand,
|
||||
errorCode: 0,
|
||||
body,
|
||||
errorMsg: '',
|
||||
reserved: isUid ? 1 : 0,
|
||||
}, baseSchema);
|
||||
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. A handful
|
||||
* of commands (group/friend list, rkey download) operate on
|
||||
* UID-keyed data and require this flag — the wire format is
|
||||
* otherwise identical.
|
||||
*/
|
||||
isUid?: boolean;
|
||||
};
|
||||
/** Omit to skip decoding the response body (fire-and-check). */
|
||||
response?: {
|
||||
schema: ProtoSchema;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an OIDB packet and verify it succeeded (no decode of body).
|
||||
* 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.
|
||||
*/
|
||||
export async function sendOidbAndCheck(
|
||||
bridge: Bridge,
|
||||
serviceCmd: string, command: number, subCommand: number,
|
||||
body: any, bodySchema: ProtoSchema, isUid = false,
|
||||
): Promise<void> {
|
||||
const request = makeOidbRequest(command, subCommand, body, bodySchema, isUid);
|
||||
const result = await bridge.sendRawPacket(serviceCmd, request);
|
||||
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);
|
||||
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');
|
||||
// Check OIDB error code if response present
|
||||
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 && (resp as any).errorCode && (resp as any).errorCode !== 0) {
|
||||
throw new Error(`OIDB error ${(resp as any).errorCode}: ${(resp as any).errorMsg ?? ''}`);
|
||||
}
|
||||
if (resp) throwIfOidbError(resp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an OIDB packet and decode the response body with the given schema.
|
||||
*/
|
||||
export async function sendOidbAndDecode<T>(
|
||||
bridge: Bridge,
|
||||
serviceCmd: string, command: number, subCommand: number,
|
||||
body: any, bodySchema: ProtoSchema, responseSchema: ProtoSchema, isUid = false,
|
||||
): Promise<T> {
|
||||
const request = makeOidbRequest(command, subCommand, body, bodySchema, isUid);
|
||||
const result = await bridge.sendRawPacket(serviceCmd, request);
|
||||
if (!result.success) throw new Error(result.errorMessage || 'packet send failed');
|
||||
if (!result.gotResponse || !result.responseData) throw new Error(result.errorMessage || 'no response');
|
||||
|
||||
const baseSchema = makeOidbBaseSchema(responseSchema);
|
||||
const resp = protoDecode(result.responseData, baseSchema);
|
||||
if (!resp) throw new Error('failed to decode OIDB response');
|
||||
if ((resp as any).errorCode && (resp as any).errorCode !== 0) {
|
||||
throw new Error(`OIDB error ${(resp as any).errorCode}: ${(resp as any).errorMsg ?? ''}`);
|
||||
}
|
||||
return (resp as any).body as T;
|
||||
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 ?? ''}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user