refactor(oidb): thread ctx into serialize/deserialize for self-contained namespaces
The first pass at namespace-per-cmd left a smell: namespaces that
needed identity/uid resolution had to declare `Deps` AND then rewrite
their own spec inside `invoke` via a closure-over-resolved-value:
// before
export const serialize = (p: Params, targetUid: string) => ({...});
export const invoke = async (deps, params) => {
const targetUid = await deps.resolveUserUid(params.userId);
return invokeOidb(deps, {
...Self,
serialize: p => serialize(p, targetUid), // ← escape hatch
}, params);
};
Reading "what does this cmd do on the wire?" required reading the
namespace AND the closure rewrite — the namespace wasn't really
self-contained, just a forwarder of two pure functions.
Fix: thread `ctx` (the namespace's full capability surface) into
`serialize` and `deserialize` directly. `serialize` may now return
`Promise<TReq>` so identity / uid lookups happen in place:
// after
export const serialize = async (ctx: Deps, p: Params) => ({
groupUin: p.groupId, type: 1,
body: {
targetUid: await ctx.resolveUserUid(p.userId, p.groupId),
duration: p.duration,
},
});
export const invoke = (deps, params) => invokeOidb(deps, Self, params);
OidbCallSpec gains a `TCtx extends OidbSender` generic; invokeOidb's
first arg becomes `ctx: TCtx` (the sender capability is just a slice
of ctx now). buildOidbRequest / parseOidbResponse follow the same
shape. Async serialize is awaited inside invokeOidb.
17 namespaces had closure rewrites; all 17 collapse to plain
`invokeOidb(deps, Self, params)` invokes. Only one closure remains —
`fetch-user-profile` binds `params.uin` into deserialize as a server-
echo fallback; kept as a documented exception (the rare case where
deserialize wants request context, not ctx).
Wire bytes preserved across all migrations. 326 bridge tests + 302
core tests pass; lint clean.
This commit is contained in:
80 files changed
+416
-397
No files matched your search
@@ -23,12 +23,12 @@ describe('FetchDownloadRkeys namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('requests the (private=10, group=20, fallback=2) image scopes', () => {
|
||||
const out = FetchDownloadRkeys.serialize({});
|
||||
const out = FetchDownloadRkeys.serialize({} as any, {});
|
||||
expect(out.downloadRkey?.types).toEqual([10, 20, 2]);
|
||||
});
|
||||
|
||||
it('packages the fixed reqHead { common, scene, client }', () => {
|
||||
const out = FetchDownloadRkeys.serialize({});
|
||||
const out = FetchDownloadRkeys.serialize({} as any, {});
|
||||
expect(out.reqHead).toEqual({
|
||||
common: { requestId: 1, command: 202 },
|
||||
scene: { requestType: 2, businessType: 1, sceneType: 0 },
|
||||
@@ -39,18 +39,18 @@ describe('FetchDownloadRkeys namespace', () => {
|
||||
|
||||
describe('deserialize', () => {
|
||||
it('throws when respHead.retCode is non-zero', () => {
|
||||
expect(() => FetchDownloadRkeys.deserialize({
|
||||
expect(() => FetchDownloadRkeys.deserialize({} as any, {
|
||||
respHead: { retCode: 42, message: 'bad' },
|
||||
} as any)).toThrow('bad');
|
||||
});
|
||||
|
||||
it('passes through on retCode = 0', () => {
|
||||
const body = { respHead: { retCode: 0 }, downloadRkey: { rkeys: [] } };
|
||||
expect(FetchDownloadRkeys.deserialize(body as any)).toBe(body);
|
||||
expect(FetchDownloadRkeys.deserialize({} as any, body as any)).toBe(body);
|
||||
});
|
||||
|
||||
it('falls back to a generic message when none is provided', () => {
|
||||
expect(() => FetchDownloadRkeys.deserialize({
|
||||
expect(() => FetchDownloadRkeys.deserialize({} as any, {
|
||||
respHead: { retCode: 1 },
|
||||
} as any)).toThrow('fetch download rkey failed');
|
||||
});
|
||||
|
||||
@@ -22,19 +22,19 @@ describe('FetchFriendListPage namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('omits nextUin on the first page', () => {
|
||||
const out = FetchFriendListPage.serialize({ nextUin: null }) as any;
|
||||
const out = FetchFriendListPage.serialize({} as any, { nextUin: null }) as any;
|
||||
expect(out.nextUin).toBeUndefined();
|
||||
expect(out.friendCount).toBe(300);
|
||||
expect(out.field4).toBe(0);
|
||||
});
|
||||
|
||||
it('includes nextUin sub-message on follow-up pages', () => {
|
||||
const out = FetchFriendListPage.serialize({ nextUin: 10001 }) as any;
|
||||
const out = FetchFriendListPage.serialize({} as any, { nextUin: 10001 }) as any;
|
||||
expect(out.nextUin).toEqual({ uin: 10001 });
|
||||
});
|
||||
|
||||
it('emits the verbatim property request list (codes 100/101/102/103/20002/27394)', () => {
|
||||
const out = FetchFriendListPage.serialize({ nextUin: null }) as any;
|
||||
const out = FetchFriendListPage.serialize({} as any, { nextUin: null }) as any;
|
||||
expect(out.body).toEqual([
|
||||
{ type: 1, number: { numbers: [103, 102, 20002, 27394] } },
|
||||
{ type: 4, number: { numbers: [100, 101, 102] } },
|
||||
|
||||
@@ -19,18 +19,18 @@ describe('FetchGroupMemberListPage namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('omits the token on the first page', () => {
|
||||
const out = FetchGroupMemberListPage.serialize({ groupId: 12345, token: '' }) as any;
|
||||
const out = FetchGroupMemberListPage.serialize({} as any, { groupId: 12345, token: '' }) as any;
|
||||
expect(out.token).toBeUndefined();
|
||||
expect(out.groupUin).toBe(12345);
|
||||
});
|
||||
|
||||
it('threads the server-issued token into follow-up pages', () => {
|
||||
const out = FetchGroupMemberListPage.serialize({ groupId: 12345, token: 'next-cursor' }) as any;
|
||||
const out = FetchGroupMemberListPage.serialize({} as any, { groupId: 12345, token: 'next-cursor' }) as any;
|
||||
expect(out.token).toBe('next-cursor');
|
||||
});
|
||||
|
||||
it('always requests the full member-field projection', () => {
|
||||
const out = FetchGroupMemberListPage.serialize({ groupId: 1, token: '' }) as any;
|
||||
const out = FetchGroupMemberListPage.serialize({} as any, { groupId: 1, token: '' }) as any;
|
||||
expect(out.body).toMatchObject({
|
||||
memberName: true, memberCard: true, level: true,
|
||||
joinTimestamp: true, lastMsgTimestamp: true, shutUpTimestamp: true,
|
||||
|
||||
@@ -27,8 +27,8 @@ describe('FetchGroupRequests namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('always sends count=20 / field2=0', () => {
|
||||
expect(FetchGroupRequests.serialize({ filtered: false })).toEqual({ count: 20, field2: 0 });
|
||||
expect(FetchGroupRequests.serialize({ filtered: true })).toEqual({ count: 20, field2: 0 });
|
||||
expect(FetchGroupRequests.serialize({} as any, { filtered: false })).toEqual({ count: 20, field2: 0 });
|
||||
expect(FetchGroupRequests.serialize({} as any, { filtered: true })).toEqual({ count: 20, field2: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user