refactor(oidb): migrate web.ts OIDB cmds to namespaces

2 OIDB cmds moved (the only OIDB calls in this file — the rest are
HTTP cookie / qun.qq.com / qzone web requests):

  - ForceFetchClientKey (0x102A_1) — used by ptlogin2 jump flow to
                                     swap clientKey → cookie jar
  - GetPskey            (0x102A_0) — p_skey fallback when ptlogin2
                                     omits it

The private `forceFetchClientKeyInner` / `getPSkey` helpers in
WebApi are now one-line forwarders into the namespaces. Facade
methods (`getCookies`, `getCsrfToken`, `getCredentials`, etc.) and
all HTTP-only cmds untouched.

12 new namespace-level tests covering deserialize edge cases
(empty server response, missing fields, dropped invalid items)
and e2e wire routing.

Verification:
  - typecheck green
  - @snowluma/bridge: 20 files / 165 tests
  - @snowluma/core: 302 tests
Wire bytes unchanged.
This commit is contained in:
DrSmoothl committed 2026-05-22 23:40:17 +08:00
1 parent 58e8081420
commit 8356121518
6 files changed
+233 -44

No files matched your search

+8
View File
@@ -107,6 +107,14 @@
"types": "./src/oidb-services/misc/send-group-sign.ts",
"default": "./src/oidb-services/misc/send-group-sign.ts"
},
"./oidb-services/web/force-fetch-client-key": {
"types": "./src/oidb-services/web/force-fetch-client-key.ts",
"default": "./src/oidb-services/web/force-fetch-client-key.ts"
},
"./oidb-services/web/get-pskey": {
"types": "./src/oidb-services/web/get-pskey.ts",
"default": "./src/oidb-services/web/get-pskey.ts"
},
"./bridge-context": {
"types": "./src/bridge-context.ts",
"default": "./src/bridge-context.ts"
@@ -0,0 +1,44 @@
// 0x102A_1 — fetch a fresh `clientKey` (consumed by the ptlogin2 jump
// URL flow that swaps it for cookie-jar entries on qq.com subdomains).
//
// The server's `keyIndex` field is occasionally empty in the wild;
// every NapCat-derived implementation falls back to the literal "19"
// in that case — origin unknown but the value works.
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbClientKeyReq, OidbClientKeyResp } from '@snowluma/proto-defs/oidb-actions/base';
import { invokeOidb, type OidbSender } from '../../oidb-service';
export interface ClientKeyInfo {
clientKey: string;
keyIndex: string;
expireTime: string;
}
export namespace ForceFetchClientKey {
export const command = 0x102A;
export const subCommand = 1;
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Params {}
export type Deps = OidbSender;
export const serialize = (_: Params): OidbClientKeyReq => ({});
export const deserialize = (body: OidbClientKeyResp): ClientKeyInfo => ({
clientKey: body.clientKey || '',
keyIndex: String(body.keyIndex || '19'),
expireTime: String(body.expireTime || '1800'),
});
export const encode = (env: OidbBase<OidbClientKeyReq>): Uint8Array =>
protobuf_encode<OidbBase<OidbClientKeyReq>>(env);
export const decode = (bytes: Uint8Array): OidbBase<OidbClientKeyResp> =>
protobuf_decode<OidbBase<OidbClientKeyResp>>(bytes);
export const invoke = (deps: Deps): Promise<ClientKeyInfo> =>
invokeOidb(deps, ForceFetchClientKey, {});
}
@@ -0,0 +1,48 @@
// 0x102A_0 — request p_skey cookies for a list of qq.com subdomains.
// Used as the fallback when ptlogin2 jump doesn't emit a p_skey on
// the cookie set (intermittent server behaviour).
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbGetPskeyReq, OidbGetPskeyResp } from '@snowluma/proto-defs/oidb-actions/base';
import { invokeOidb, type OidbSender } from '../../oidb-service';
export namespace GetPskey {
export const command = 0x102A;
export const subCommand = 0;
export interface Params {
domainList: string[];
}
export interface Result {
domainPskeyMap: Map<string, string>;
}
export type Deps = OidbSender;
export const serialize = (p: Params): OidbGetPskeyReq => ({
domainList: p.domainList,
});
export const deserialize = (body: OidbGetPskeyResp): Result => {
const domainPskeyMap = new Map<string, string>();
if (body.pskeyItems && Array.isArray(body.pskeyItems)) {
for (const item of body.pskeyItems) {
if (item.domain && item.pskey) {
domainPskeyMap.set(item.domain, item.pskey);
}
}
}
return { domainPskeyMap };
};
export const encode = (env: OidbBase<OidbGetPskeyReq>): Uint8Array =>
protobuf_encode<OidbBase<OidbGetPskeyReq>>(env);
export const decode = (bytes: Uint8Array): OidbBase<OidbGetPskeyResp> =>
protobuf_decode<OidbBase<OidbGetPskeyResp>>(bytes);
export const invoke = (deps: Deps, params: Params): Promise<Result> =>
invokeOidb(deps, GetPskey, params);
}
@@ -0,0 +1,49 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbClientKeyResp } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { ForceFetchClientKey } from '../../../src/oidb-services/web/force-fetch-client-key';
function makeSender(body?: OidbClientKeyResp) {
const responseData = body !== undefined
? Buffer.from(protobuf_encode<OidbBase<OidbClientKeyResp>>({ body }))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('ForceFetchClientKey namespace', () => {
it('declares 0x102A_1', () => {
expect(ForceFetchClientKey.command).toBe(0x102A);
expect(ForceFetchClientKey.subCommand).toBe(1);
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0x102a_1', async () => {
const sender = makeSender({ clientKey: 'ck', keyIndex: 19, expireTime: 1800 });
await ForceFetchClientKey.invoke(sender);
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x102a_1');
});
it('returns the parsed clientKey / keyIndex / expireTime', async () => {
const sender = makeSender({ clientKey: 'KEY', keyIndex: 7, expireTime: 600 });
const out = await ForceFetchClientKey.invoke(sender);
expect(out).toEqual({ clientKey: 'KEY', keyIndex: '7', expireTime: '600' });
});
it('falls back to keyIndex="19" and expireTime="1800" when server omits them', async () => {
const sender = makeSender({ clientKey: 'KEY' });
const out = await ForceFetchClientKey.invoke(sender);
expect(out.keyIndex).toBe('19');
expect(out.expireTime).toBe('1800');
});
it('returns empty clientKey when server omits it', async () => {
const sender = makeSender({});
const out = await ForceFetchClientKey.invoke(sender);
expect(out.clientKey).toBe('');
});
});
});
@@ -0,0 +1,75 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbGetPskeyReq, OidbGetPskeyResp } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { GetPskey } from '../../../src/oidb-services/web/get-pskey';
function makeSender(body?: OidbGetPskeyResp) {
const responseData = body !== undefined
? Buffer.from(protobuf_encode<OidbBase<OidbGetPskeyResp>>({ body }))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('GetPskey namespace', () => {
it('declares 0x102A_0', () => {
expect(GetPskey.command).toBe(0x102A);
expect(GetPskey.subCommand).toBe(0);
});
describe('serialize', () => {
it('passes the domainList through verbatim', () => {
const out = GetPskey.serialize({ domainList: ['qun.qq.com', 'qzone.qq.com'] });
expect(out.domainList).toEqual(['qun.qq.com', 'qzone.qq.com']);
});
});
describe('deserialize', () => {
it('builds a Map from server pskeyItems', () => {
const { domainPskeyMap } = GetPskey.deserialize({
pskeyItems: [
{ domain: 'qun.qq.com', pskey: 'pskey-qun' },
{ domain: 'qzone.qq.com', pskey: 'pskey-qzone' },
],
});
expect(domainPskeyMap.get('qun.qq.com')).toBe('pskey-qun');
expect(domainPskeyMap.get('qzone.qq.com')).toBe('pskey-qzone');
});
it('drops entries with missing domain or pskey', () => {
const { domainPskeyMap } = GetPskey.deserialize({
pskeyItems: [
{ domain: 'qun.qq.com', pskey: 'ok' },
{ domain: 'missing-pskey.com' },
{ pskey: 'missing-domain' },
] as any,
});
expect(domainPskeyMap.size).toBe(1);
expect(domainPskeyMap.get('qun.qq.com')).toBe('ok');
});
it('returns empty Map when pskeyItems is absent', () => {
const { domainPskeyMap } = GetPskey.deserialize({});
expect(domainPskeyMap.size).toBe(0);
});
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0x102a_0', async () => {
const sender = makeSender({ pskeyItems: [] });
await GetPskey.invoke(sender, { domainList: ['qq.com'] });
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x102a_0');
});
it('encodes the domain list into the request body', async () => {
const sender = makeSender({ pskeyItems: [] });
await GetPskey.invoke(sender, { domainList: ['qun.qq.com'] });
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbGetPskeyReq>>(bytes);
expect(env.body?.domainList).toEqual(['qun.qq.com']);
});
});
});
+9 -44
View File
@@ -4,17 +4,8 @@
// The pure HTTP transport stays in `web/*.ts` and stays unchanged —
// only the Bridge-binding helpers move here.
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
OidbClientKeyReq,
OidbClientKeyResp,
OidbGetPskeyReq,
OidbGetPskeyResp,
} from '@snowluma/proto-defs/oidb-actions/base';
import type { BridgeContext } from '../bridge-context';
import type { Bridge } from '../bridge';
import { makeOidbEnvelope, runOidb } from '@snowluma/bridge/bridge-oidb';
import { getGroupEssenceMsg, getGroupEssenceMsgAll } from '@snowluma/bridge/web/group-essence';
import { getHonorListWebAPI, WebHonorType } from '@snowluma/bridge/web/group-honor';
import {
@@ -24,49 +15,23 @@ import {
uploadGroupNoticeImage,
} from '@snowluma/bridge/web/group-notice';
import { RequestUtil } from '@snowluma/bridge/web/request-util';
// Migrated OIDB cmds — the two private helpers below are thin
// forwarders into the namespaces.
import { ForceFetchClientKey, type ClientKeyInfo as NamespaceClientKeyInfo } from '@snowluma/bridge/oidb-services/web/force-fetch-client-key';
import { GetPskey } from '@snowluma/bridge/oidb-services/web/get-pskey';
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
export interface ClientKeyInfo {
clientKey: string;
keyIndex: string;
expireTime: string;
}
export type ClientKeyInfo = NamespaceClientKeyInfo;
// ─────────────── private helpers (cookie acquisition) ───────────────
async function forceFetchClientKeyInner(bridge: Bridge): Promise<ClientKeyInfo> {
const env = makeOidbEnvelope<OidbClientKeyReq>(0x102A, 1, {});
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x102a_1', protobuf_encode<OidbBase<OidbClientKeyReq>>(env));
const resp = protobuf_decode<OidbBase<OidbClientKeyResp>>(respBytes).body;
const clientKey = resp?.clientKey || '';
// keyIndex falls back to "19" — origin unknown but the value is what
// every NapCat-derived implementation uses when the server omits it.
const keyIndex = String(resp?.keyIndex || '19');
return {
clientKey,
keyIndex,
expireTime: String(resp?.expireTime || '1800'),
};
function forceFetchClientKeyInner(bridge: Bridge): Promise<ClientKeyInfo> {
return ForceFetchClientKey.invoke(bridge);
}
async function getPSkey(bridge: Bridge, domainList: string[]): Promise<{ domainPskeyMap: Map<string, string> }> {
const env = makeOidbEnvelope<OidbGetPskeyReq>(0x102A, 0, { domainList });
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x102a_0', protobuf_encode<OidbBase<OidbGetPskeyReq>>(env));
const resp = protobuf_decode<OidbBase<OidbGetPskeyResp>>(respBytes).body;
const domainPskeyMap = new Map<string, string>();
if (resp?.pskeyItems && Array.isArray(resp.pskeyItems)) {
for (const item of resp.pskeyItems) {
if (item.domain && item.pskey) {
domainPskeyMap.set(item.domain, item.pskey);
}
}
}
return { domainPskeyMap };
function getPSkey(bridge: Bridge, domainList: string[]): Promise<{ domainPskeyMap: Map<string, string> }> {
return GetPskey.invoke(bridge, { domainList });
}
async function getCookies(bridge: Bridge, domain: string): Promise<Record<string, string>> {