lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -40,7 +40,11 @@ import { makeOidbEnvelope } from './bridge-oidb';
|
||||
* `Pick<BridgeContext, 'identity' | …>` and intersects with this.
|
||||
*/
|
||||
export interface OidbSender {
|
||||
sendRawPacket(serviceCmd: string, body: Uint8Array, timeoutMs?: number): Promise<SendPacketResult>;
|
||||
sendRawPacket(
|
||||
serviceCmd: string,
|
||||
body: Uint8Array,
|
||||
timeoutMs?: number,
|
||||
): Promise<SendPacketResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,63 +61,64 @@ export interface OidbSender {
|
||||
* are given. `resolveCommand` likewise wins over static `command`.
|
||||
*/
|
||||
export interface OidbCallSpec<TCtx extends OidbSender, TReq, TResp, TParams, TResult> {
|
||||
command: number;
|
||||
resolveCommand?(params: TParams, ctx: TCtx): number;
|
||||
subCommand?: number;
|
||||
resolveSubCommand?(params: TParams, ctx: TCtx): number;
|
||||
/** Packet and body envelope codes treated as success in addition to 0. */
|
||||
acceptedEnvelopeCodes?: readonly number[];
|
||||
/** Set OIDB envelope `reserved = 1` (UIN-form variant, see makeOidbEnvelope's isUid). */
|
||||
uinForm?: boolean;
|
||||
/** Override the default `OidbSvcTrpcTcp.0xNNNN_N` wire name. A few
|
||||
* cmds route through different SSO names (e.g. 0xE17_0 lives at
|
||||
* `MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0`); supply a function
|
||||
* that takes the resolved `(cmd, subCmd)` and returns the actual
|
||||
* wire name. Omit for the default scheme. */
|
||||
wireName?(command: number, subCommand: number): string;
|
||||
/** Business params → wire-shaped request body. May be async (e.g.
|
||||
* when ctx.resolveUserUid hits the cache or the network). */
|
||||
serialize(ctx: TCtx, params: TParams): TReq | Promise<TReq>;
|
||||
/** Wire-shaped response body → business result. */
|
||||
deserialize(ctx: TCtx, body: TResp): TResult;
|
||||
/** Per-type protobuf encoder — concrete generic call so the Vite plugin can monomorphize. */
|
||||
encode(env: OidbBase<TReq>): Uint8Array;
|
||||
/** Per-type protobuf decoder. */
|
||||
decode(bytes: Uint8Array): OidbBase<TResp>;
|
||||
command: number;
|
||||
resolveCommand?(params: TParams, ctx: TCtx): number;
|
||||
subCommand?: number;
|
||||
resolveSubCommand?(params: TParams, ctx: TCtx): number;
|
||||
/** Packet and body envelope codes treated as success in addition to 0. */
|
||||
acceptedEnvelopeCodes?: readonly number[];
|
||||
/** Set OIDB envelope `reserved = 1` (UIN-form variant, see makeOidbEnvelope's isUid). */
|
||||
uinForm?: boolean;
|
||||
/** Override the default `OidbSvcTrpcTcp.0xNNNN_N` wire name. A few
|
||||
* cmds route through different SSO names (e.g. 0xE17_0 lives at
|
||||
* `MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0`); supply a function
|
||||
* that takes the resolved `(cmd, subCmd)` and returns the actual
|
||||
* wire name. Omit for the default scheme. */
|
||||
wireName?(command: number, subCommand: number): string;
|
||||
/** Business params → wire-shaped request body. May be async (e.g.
|
||||
* when ctx.resolveUserUid hits the cache or the network). */
|
||||
serialize(ctx: TCtx, params: TParams): TReq | Promise<TReq>;
|
||||
/** Wire-shaped response body → business result. */
|
||||
deserialize(ctx: TCtx, body: TResp): TResult;
|
||||
/** Per-type protobuf encoder — concrete generic call so the Vite plugin can monomorphize. */
|
||||
encode(env: OidbBase<TReq>): Uint8Array;
|
||||
/** Per-type protobuf decoder. */
|
||||
decode(bytes: Uint8Array): OidbBase<TResp>;
|
||||
}
|
||||
|
||||
function resolveOidbCall<TCtx extends OidbSender, TReq, TResp, TParams, TResult>(
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
params: TParams,
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
params: TParams,
|
||||
ctx: TCtx,
|
||||
): { command: number; subCommand: number } {
|
||||
const command = spec.resolveCommand
|
||||
? spec.resolveCommand(params, ctx)
|
||||
: spec.command;
|
||||
const subCommand = spec.resolveSubCommand
|
||||
? spec.resolveSubCommand(params, ctx)
|
||||
: spec.subCommand!;
|
||||
return { command, subCommand };
|
||||
const command = spec.resolveCommand ? spec.resolveCommand(params, ctx) : spec.command;
|
||||
const subCommand = spec.resolveSubCommand
|
||||
? spec.resolveSubCommand(params, ctx)
|
||||
: spec.subCommand!;
|
||||
return { command, subCommand };
|
||||
}
|
||||
|
||||
function isAcceptedEnvelopeCode<TCtx extends OidbSender, TReq, TResp, TParams, TResult>(
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
code: number | null | undefined,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
code: number | null | undefined,
|
||||
): boolean {
|
||||
if (code == null || code === 0) return true;
|
||||
return spec.acceptedEnvelopeCodes?.includes(code) === true;
|
||||
if (code == null || code === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return spec.acceptedEnvelopeCodes?.includes(code) === true;
|
||||
}
|
||||
|
||||
export class OidbError extends Error {
|
||||
constructor(
|
||||
readonly code: number,
|
||||
readonly serverMsg: string,
|
||||
readonly command: number,
|
||||
readonly subCommand: number,
|
||||
) {
|
||||
super(`OIDB error ${code} on 0x${command.toString(16)}_${subCommand}: ${serverMsg}`);
|
||||
this.name = 'OidbError';
|
||||
}
|
||||
constructor(
|
||||
readonly code: number,
|
||||
readonly serverMsg: string,
|
||||
readonly command: number,
|
||||
readonly subCommand: number,
|
||||
) {
|
||||
super(`OIDB error ${code} on 0x${command.toString(16)}_${subCommand}: ${serverMsg}`);
|
||||
this.name = 'OidbError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,57 +132,63 @@ export class OidbError extends Error {
|
||||
* than forcing the caller to pre-bake values into the params.
|
||||
*/
|
||||
export async function invokeOidb<TCtx extends OidbSender, TReq, TResp, TParams, TResult>(
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
params: TParams,
|
||||
timeoutMs?: number,
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
params: TParams,
|
||||
timeoutMs?: number,
|
||||
): Promise<TResult> {
|
||||
const { command, subCommand } = resolveOidbCall(spec, params, ctx);
|
||||
const reqBody = await spec.serialize(ctx, params);
|
||||
const env = makeOidbEnvelope(command, subCommand, reqBody, spec.uinForm ?? false);
|
||||
const reqBytes = spec.encode(env);
|
||||
const wireName = spec.wireName
|
||||
? spec.wireName(command, subCommand)
|
||||
: `OidbSvcTrpcTcp.0x${command.toString(16)}_${subCommand}`;
|
||||
const { command, subCommand } = resolveOidbCall(spec, params, ctx);
|
||||
const reqBody = await spec.serialize(ctx, params);
|
||||
const env = makeOidbEnvelope(command, subCommand, reqBody, spec.uinForm ?? false);
|
||||
const reqBytes = spec.encode(env);
|
||||
const wireName = spec.wireName
|
||||
? spec.wireName(command, subCommand)
|
||||
: `OidbSvcTrpcTcp.0x${command.toString(16)}_${subCommand}`;
|
||||
|
||||
const result = timeoutMs === undefined
|
||||
? await ctx.sendRawPacket(wireName, reqBytes)
|
||||
: await ctx.sendRawPacket(wireName, reqBytes, timeoutMs);
|
||||
if (!result.gotResponse) throw new Error(result.errorMessage || 'no response');
|
||||
const result =
|
||||
timeoutMs === undefined
|
||||
? await ctx.sendRawPacket(wireName, reqBytes)
|
||||
: await ctx.sendRawPacket(wireName, reqBytes, timeoutMs);
|
||||
|
||||
if (!result.success) {
|
||||
if (result.errorCode && result.errorCode !== 0) {
|
||||
if (!isAcceptedEnvelopeCode(spec, result.errorCode)) {
|
||||
throw new OidbError(
|
||||
result.errorCode,
|
||||
result.errorMessage || '',
|
||||
command,
|
||||
subCommand
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// success=false without an OIDB error code = the packet send itself failed;
|
||||
// surface it instead of silently resolving with an undecoded response.
|
||||
throw new Error(result.errorMessage || 'packet send failed');
|
||||
if (!result.gotResponse) {
|
||||
throw new Error(result.errorMessage || 'no response');
|
||||
}
|
||||
}
|
||||
|
||||
const respBytes = result.responseData ?? new Uint8Array(0);
|
||||
if (respBytes.length > 0) {
|
||||
const meta = protobuf_decode<OidbBaseMeta>(respBytes);
|
||||
const code = meta?.errorCode;
|
||||
if (code && code !== 0 && !isAcceptedEnvelopeCode(spec, code)) {
|
||||
throw new OidbError(code, meta?.errorMsg ?? '', command, subCommand);
|
||||
if (!result.success) {
|
||||
if (result.errorCode && result.errorCode !== 0) {
|
||||
if (!isAcceptedEnvelopeCode(spec, result.errorCode)) {
|
||||
throw new OidbError(
|
||||
result.errorCode,
|
||||
result.errorMessage || '',
|
||||
command,
|
||||
subCommand,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// success=false without an OIDB error code = the packet send itself failed;
|
||||
// surface it instead of silently resolving with an undecoded response.
|
||||
throw new Error(result.errorMessage || 'packet send failed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Server may legitimately respond with an empty envelope (ack-only
|
||||
// cmds like 0x9082_1 — the response carries only `errorCode = 0`
|
||||
// and no body). Substitute an empty placeholder so `deserialize`
|
||||
// always receives a defined object; specs that read fields off the
|
||||
// body must already guard with `?? []` etc.
|
||||
const respBody = spec.decode(respBytes).body ?? ({} as TResp);
|
||||
return spec.deserialize(ctx, respBody);
|
||||
const respBytes = result.responseData ?? new Uint8Array(0);
|
||||
|
||||
if (respBytes.length > 0) {
|
||||
const meta = protobuf_decode<OidbBaseMeta>(respBytes);
|
||||
const code = meta?.errorCode;
|
||||
|
||||
if (code && code !== 0 && !isAcceptedEnvelopeCode(spec, code)) {
|
||||
throw new OidbError(code, meta?.errorMsg ?? '', command, subCommand);
|
||||
}
|
||||
}
|
||||
|
||||
// Server may legitimately respond with an empty envelope (ack-only
|
||||
// cmds like 0x9082_1 — the response carries only `errorCode = 0`
|
||||
// and no body). Substitute an empty placeholder so `deserialize`
|
||||
// always receives a defined object; specs that read fields off the
|
||||
// body must already guard with `?? []` etc.
|
||||
const respBody = spec.decode(respBytes).body ?? ({} as TResp);
|
||||
return spec.deserialize(ctx, respBody);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,18 +198,18 @@ export async function invokeOidb<TCtx extends OidbSender, TReq, TResp, TParams,
|
||||
* because matching `invokeOidb`'s shape avoids two call patterns).
|
||||
*/
|
||||
export async function buildOidbRequest<TCtx extends OidbSender, TReq, TResp, TParams, TResult>(
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
params: TParams,
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
params: TParams,
|
||||
): Promise<{ wireName: string; bytes: Uint8Array }> {
|
||||
const { command, subCommand } = resolveOidbCall(spec, params, ctx);
|
||||
const reqBody = await spec.serialize(ctx, params);
|
||||
const env = makeOidbEnvelope(command, subCommand, reqBody, spec.uinForm ?? false);
|
||||
const bytes = spec.encode(env);
|
||||
const wireName = spec.wireName
|
||||
? spec.wireName(command, subCommand)
|
||||
: `OidbSvcTrpcTcp.0x${command.toString(16)}_${subCommand}`;
|
||||
return { wireName, bytes };
|
||||
const { command, subCommand } = resolveOidbCall(spec, params, ctx);
|
||||
const reqBody = await spec.serialize(ctx, params);
|
||||
const env = makeOidbEnvelope(command, subCommand, reqBody, spec.uinForm ?? false);
|
||||
const bytes = spec.encode(env);
|
||||
const wireName = spec.wireName
|
||||
? spec.wireName(command, subCommand)
|
||||
: `OidbSvcTrpcTcp.0x${command.toString(16)}_${subCommand}`;
|
||||
return { wireName, bytes };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,9 +217,9 @@ export async function buildOidbRequest<TCtx extends OidbSender, TReq, TResp, TPa
|
||||
* deserialize path — symmetric debug helper for buildOidbRequest.
|
||||
*/
|
||||
export function parseOidbResponse<TCtx extends OidbSender, TReq, TResp, TParams, TResult>(
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
bytes: Uint8Array,
|
||||
ctx: TCtx,
|
||||
spec: OidbCallSpec<TCtx, TReq, TResp, TParams, TResult>,
|
||||
bytes: Uint8Array,
|
||||
): TResult {
|
||||
return spec.deserialize(ctx, spec.decode(bytes).body ?? ({} as TResp));
|
||||
return spec.deserialize(ctx, spec.decode(bytes).body ?? ({} as TResp));
|
||||
}
|
||||
Reference in new issue
Block a user