feat(proton): migrate action.ts schemas + bridge.ts + element-builder.ts
Mirrors all 11 schemas in bridge/proto/action.ts as proton interface
forms and switches the two consumers (element-builder.ts and the
send-message paths in bridge.ts) over to the new compile-time API.
Migrated schemas (proton/action.ts):
RoutingC2C, RoutingGroup, RoutingHead, SendContentHead,
MessageControl, SendRichText, SendMessageBody, SendMessageRequest,
SendMessageResponse, MentionExtraSend, MarkdownData
Consumer switches:
- element-builder.ts: protobuf_encode<MentionExtraSend> for @ mention
pbReserve payload; protobuf_encode<MarkdownData> for markdown elem
- bridge.ts: protobuf_encode<SendMessageRequest> +
protobuf_decode<SendMessageResponse> for both sendGroupMessage and
sendPrivateMessage
The `ProtoElem` type alias in element-builder.ts (Partial<ProtoDecoded<typeof
ElemSchema>>) stays as-is — it's a pure TS type used internally for
building elem objects and doesn't affect runtime behaviour. Replacing
it with the proton Elem type is a larger refactor that can come later
when there's a reason to.
Verification:
- 6 new parity tests for action schemas (RoutingHead c2c+grp,
SendMessageRequest full shape, SendMessageResponse, MentionExtraSend,
MarkdownData)
- 591/591 core test suite passes — the existing send-message and
element-builder tests implicitly validated wire format equivalence
- tsc --noEmit clean
This commit is contained in:
4 files changed
+195
-24
No files matched your search
@@ -7,16 +7,16 @@ import type { ForwardNodePayload, QQEventVariant, MessageElement } from './event
|
|||||||
import type { FriendInfo, QQGroupInfo, GroupMemberInfo, UserProfileInfo, GroupRequestInfo } from './qq-info';
|
import type { FriendInfo, QQGroupInfo, GroupMemberInfo, UserProfileInfo, GroupRequestInfo } from './qq-info';
|
||||||
import { MSG_PUSH_CMD, parseMsgPush } from './msg-push';
|
import { MSG_PUSH_CMD, parseMsgPush } from './msg-push';
|
||||||
import type { PacketSender, SendPacketResult } from '../protocol/packet-sender';
|
import type { PacketSender, SendPacketResult } from '../protocol/packet-sender';
|
||||||
import { protoEncode, protoDecode } from '../protobuf/decode';
|
import { protobuf_encode, protobuf_decode } from '@snowluma/proton';
|
||||||
import { buildSendElems } from './element-builder';
|
import { buildSendElems } from './element-builder';
|
||||||
import { IdentityService } from './identity-service';
|
import { IdentityService } from './identity-service';
|
||||||
import type { BridgeInterface } from './bridge-interface';
|
import type { BridgeInterface } from './bridge-interface';
|
||||||
import { IncomingPacketPipeline, type CmdParser } from './packet-pipeline';
|
import { IncomingPacketPipeline, type CmdParser } from './packet-pipeline';
|
||||||
import { createLogger } from '../utils/logger';
|
import { createLogger } from '../utils/logger';
|
||||||
import {
|
import type {
|
||||||
SendMessageRequestSchema,
|
SendMessageRequest,
|
||||||
SendMessageResponseSchema,
|
SendMessageResponse,
|
||||||
} from './proto/action';
|
} from './proto/proton/action';
|
||||||
|
|
||||||
// Delegated modules
|
// Delegated modules
|
||||||
import {
|
import {
|
||||||
@@ -282,13 +282,13 @@ export class Bridge implements BridgeInterface {
|
|||||||
const protoElems = await buildSendElems(elements, { bridge: this, groupId });
|
const protoElems = await buildSendElems(elements, { bridge: this, groupId });
|
||||||
const random = this.nextMessageRandom();
|
const random = this.nextMessageRandom();
|
||||||
|
|
||||||
const request = protoEncode({
|
const request = protobuf_encode<SendMessageRequest>({
|
||||||
routingHead: {
|
routingHead: {
|
||||||
grp: { groupCode: BigInt(groupId) },
|
grp: { groupCode: BigInt(groupId) },
|
||||||
} as any,
|
},
|
||||||
contentHead: {
|
contentHead: {
|
||||||
type: 1,
|
type: 1,
|
||||||
} as any,
|
},
|
||||||
messageBody: {
|
messageBody: {
|
||||||
richText: {
|
richText: {
|
||||||
elems: protoElems,
|
elems: protoElems,
|
||||||
@@ -300,7 +300,7 @@ export class Bridge implements BridgeInterface {
|
|||||||
via: 0,
|
via: 0,
|
||||||
dataStatist: 0,
|
dataStatist: 0,
|
||||||
multiSendSeq: 0,
|
multiSendSeq: 0,
|
||||||
}, SendMessageRequestSchema);
|
});
|
||||||
|
|
||||||
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
|
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
|
||||||
|
|
||||||
@@ -308,7 +308,7 @@ export class Bridge implements BridgeInterface {
|
|||||||
throw new Error(`send group message failed: ${result.errorMessage || 'no response'}`);
|
throw new Error(`send group message failed: ${result.errorMessage || 'no response'}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = protoDecode(result.responseData, SendMessageResponseSchema);
|
const response = protobuf_decode<SendMessageResponse>(result.responseData);
|
||||||
if (!response) {
|
if (!response) {
|
||||||
throw new Error('failed to decode SendMessageResponse');
|
throw new Error('failed to decode SendMessageResponse');
|
||||||
}
|
}
|
||||||
@@ -343,18 +343,18 @@ export class Bridge implements BridgeInterface {
|
|||||||
const random = this.nextMessageRandom();
|
const random = this.nextMessageRandom();
|
||||||
const clientSeq = this.nextClientSequence();
|
const clientSeq = this.nextClientSequence();
|
||||||
|
|
||||||
const request = protoEncode({
|
const request = protobuf_encode<SendMessageRequest>({
|
||||||
routingHead: {
|
routingHead: {
|
||||||
c2c: {
|
c2c: {
|
||||||
uin: userUin,
|
uin: userUin,
|
||||||
...(userUid ? { uid: userUid } : {}),
|
...(userUid ? { uid: userUid } : {}),
|
||||||
},
|
},
|
||||||
} as any,
|
},
|
||||||
contentHead: {
|
contentHead: {
|
||||||
type: 1,
|
type: 1,
|
||||||
subType: 0,
|
subType: 0,
|
||||||
c2cCmd: 11,
|
c2cCmd: 11,
|
||||||
} as any,
|
},
|
||||||
messageBody: {
|
messageBody: {
|
||||||
richText: {
|
richText: {
|
||||||
elems: protoElems,
|
elems: protoElems,
|
||||||
@@ -367,9 +367,9 @@ export class Bridge implements BridgeInterface {
|
|||||||
dataStatist: 0,
|
dataStatist: 0,
|
||||||
ctrl: {
|
ctrl: {
|
||||||
msgFlag: Math.floor(Date.now() / 1000),
|
msgFlag: Math.floor(Date.now() / 1000),
|
||||||
} as any,
|
},
|
||||||
multiSendSeq: 0,
|
multiSendSeq: 0,
|
||||||
}, SendMessageRequestSchema);
|
});
|
||||||
|
|
||||||
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
|
const result = await this.sendRawPacket(Bridge.SEND_MSG_CMD, request);
|
||||||
|
|
||||||
@@ -377,7 +377,7 @@ export class Bridge implements BridgeInterface {
|
|||||||
throw new Error(`send private message failed: ${result.errorMessage || 'no response'}`);
|
throw new Error(`send private message failed: ${result.errorMessage || 'no response'}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = protoDecode(result.responseData, SendMessageResponseSchema);
|
const response = protobuf_decode<SendMessageResponse>(result.responseData);
|
||||||
if (!response) {
|
if (!response) {
|
||||||
throw new Error('failed to decode SendMessageResponse');
|
throw new Error('failed to decode SendMessageResponse');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,14 @@
|
|||||||
import type { Bridge } from './bridge';
|
import type { Bridge } from './bridge';
|
||||||
import type { MessageElement } from './events';
|
import type { MessageElement } from './events';
|
||||||
import type { ProtoDecoded } from '../protobuf/decode';
|
import type { ProtoDecoded } from '../protobuf/decode';
|
||||||
import { protoEncode } from '../protobuf/decode';
|
import { protobuf_encode } from '@snowluma/proton';
|
||||||
import {
|
import {
|
||||||
ElemSchema,
|
ElemSchema,
|
||||||
} from './proto/element';
|
} from './proto/element';
|
||||||
import {
|
import type {
|
||||||
MentionExtraSendSchema,
|
MentionExtraSend,
|
||||||
MarkdownDataSchema,
|
MarkdownData,
|
||||||
} from './proto/action';
|
} from './proto/proton/action';
|
||||||
import { uploadImageMsgInfo } from './highway/image-upload';
|
import { uploadImageMsgInfo } from './highway/image-upload';
|
||||||
import { uploadPttMsgInfo } from './highway/ptt-upload';
|
import { uploadPttMsgInfo } from './highway/ptt-upload';
|
||||||
import { uploadVideoMsgInfo } from './highway/video-upload';
|
import { uploadVideoMsgInfo } from './highway/video-upload';
|
||||||
@@ -51,12 +51,12 @@ function makeMentionElem(element: MessageElement, ctx?: SendContext): ProtoElem
|
|||||||
const mentionAll = element.uid === 'all' || element.targetUin === 0;
|
const mentionAll = element.uid === 'all' || element.targetUin === 0;
|
||||||
const targetUin = element.targetUin ?? 0;
|
const targetUin = element.targetUin ?? 0;
|
||||||
|
|
||||||
const extra = protoEncode({
|
const extra = protobuf_encode<MentionExtraSend>({
|
||||||
type: mentionAll ? 1 : 2,
|
type: mentionAll ? 1 : 2,
|
||||||
uin: mentionAll ? 0 : targetUin,
|
uin: mentionAll ? 0 : targetUin,
|
||||||
field5: 0,
|
field5: 0,
|
||||||
uid: mentionAll ? 'all' : (element.uid ?? ''),
|
uid: mentionAll ? 'all' : (element.uid ?? ''),
|
||||||
}, MentionExtraSendSchema);
|
});
|
||||||
|
|
||||||
// Prefer an explicit display string from the caller; otherwise look the
|
// Prefer an explicit display string from the caller; otherwise look the
|
||||||
// target up in the roster so QQ renders `@昵称` instead of `@QQ号`.
|
// target up in the roster so QQ renders `@昵称` instead of `@QQ号`.
|
||||||
@@ -128,7 +128,7 @@ function makeXmlElem(element: MessageElement): ProtoElem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function makeMarkdownElem(element: MessageElement): ProtoElem {
|
function makeMarkdownElem(element: MessageElement): ProtoElem {
|
||||||
const data = protoEncode({ content: element.text ?? '' }, MarkdownDataSchema);
|
const data = protobuf_encode<MarkdownData>({ content: element.text ?? '' });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
commonElem: {
|
commonElem: {
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
// Proton (compile-time) form of bridge/proto/action.ts.
|
||||||
|
// One-to-one mirror; legacy `*Schema` constants stay alongside for back-compat.
|
||||||
|
|
||||||
|
import type { pb, pb_repeated, int_32, uint_32, uint_64, bytes } from '@snowluma/proton';
|
||||||
|
import type { Elem } from './element';
|
||||||
|
|
||||||
|
// ── Routing ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface RoutingC2C {
|
||||||
|
uin?: pb<1, uint_32>;
|
||||||
|
uid?: pb<2, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RoutingGroup {
|
||||||
|
groupCode?: pb<1, uint_64>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RoutingHead {
|
||||||
|
c2c?: pb<1, RoutingC2C>;
|
||||||
|
grp?: pb<2, RoutingGroup>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Content Head (for send) ─────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface SendContentHead {
|
||||||
|
type?: pb<1, uint_32>;
|
||||||
|
subType?: pb<2, uint_32>;
|
||||||
|
c2cCmd?: pb<3, uint_32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Message Control ─────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface MessageControl {
|
||||||
|
msgFlag?: pb<1, int_32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── RichText (for send — only elems) ────────────────────────────────
|
||||||
|
|
||||||
|
export interface SendRichText {
|
||||||
|
elems?: pb_repeated<2, Elem>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MessageBody (for send) ──────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface SendMessageBody {
|
||||||
|
richText?: pb<1, SendRichText>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── SendMessageRequest ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface SendMessageRequest {
|
||||||
|
routingHead?: pb<1, RoutingHead>;
|
||||||
|
contentHead?: pb<2, SendContentHead>;
|
||||||
|
messageBody?: pb<3, SendMessageBody>;
|
||||||
|
clientSequence?: pb<4, uint_32>;
|
||||||
|
random?: pb<5, uint_32>;
|
||||||
|
syncCookie?: pb<6, bytes>;
|
||||||
|
via?: pb<8, uint_32>;
|
||||||
|
dataStatist?: pb<9, uint_32>;
|
||||||
|
ctrl?: pb<12, MessageControl>;
|
||||||
|
multiSendSeq?: pb<14, uint_32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── SendMessageResponse ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface SendMessageResponse {
|
||||||
|
result?: pb<1, int_32>;
|
||||||
|
errMsg?: pb<2, string>;
|
||||||
|
timestamp1?: pb<3, uint_32>;
|
||||||
|
field10?: pb<10, uint_32>;
|
||||||
|
groupSequence?: pb<11, uint_32>;
|
||||||
|
timestamp2?: pb<12, uint_32>;
|
||||||
|
privateSequence?: pb<14, uint_32>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MentionExtra (for building @mention text element) ───────────────
|
||||||
|
//
|
||||||
|
// Same wire shape as element.ts's MentionExtra (which is used for decoding
|
||||||
|
// inbound mentions) — kept under a distinct name to mark the send-side
|
||||||
|
// usage, matching the legacy convention.
|
||||||
|
|
||||||
|
export interface MentionExtraSend {
|
||||||
|
type?: pb<3, int_32>;
|
||||||
|
uin?: pb<4, uint_32>;
|
||||||
|
field5?: pb<5, int_32>;
|
||||||
|
uid?: pb<9, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── MarkdownData ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface MarkdownData {
|
||||||
|
content?: pb<1, string>;
|
||||||
|
}
|
||||||
@@ -36,6 +36,13 @@ import {
|
|||||||
FriendRequestSchema,
|
FriendRequestSchema,
|
||||||
GroupJoinSchema,
|
GroupJoinSchema,
|
||||||
} from '../src/bridge/proto/notify';
|
} from '../src/bridge/proto/notify';
|
||||||
|
import {
|
||||||
|
RoutingHeadSchema,
|
||||||
|
SendMessageRequestSchema,
|
||||||
|
SendMessageResponseSchema,
|
||||||
|
MentionExtraSendSchema,
|
||||||
|
MarkdownDataSchema,
|
||||||
|
} from '../src/bridge/proto/action';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
SendLongMsgResp,
|
SendLongMsgResp,
|
||||||
@@ -66,6 +73,13 @@ import type {
|
|||||||
FriendRequest as FriendRequestNotify,
|
FriendRequest as FriendRequestNotify,
|
||||||
GroupJoin,
|
GroupJoin,
|
||||||
} from '../src/bridge/proto/proton/notify';
|
} from '../src/bridge/proto/proton/notify';
|
||||||
|
import type {
|
||||||
|
RoutingHead,
|
||||||
|
SendMessageRequest,
|
||||||
|
SendMessageResponse,
|
||||||
|
MentionExtraSend,
|
||||||
|
MarkdownData,
|
||||||
|
} from '../src/bridge/proto/proton/action';
|
||||||
|
|
||||||
function hex(buf: Uint8Array): string {
|
function hex(buf: Uint8Array): string {
|
||||||
return Buffer.from(buf).toString('hex');
|
return Buffer.from(buf).toString('hex');
|
||||||
@@ -385,6 +399,70 @@ describe('proton ↔ legacy parity (longmsg subset)', () => {
|
|||||||
expect(hex(proton)).toBe(hex(legacy));
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── action.ts coverage ─────────────────────────────────────────────
|
||||||
|
|
||||||
|
it('encodes RoutingHead (c2c variant) identically', () => {
|
||||||
|
const data: RoutingHead = { c2c: { uin: 10001, uid: 'u_a' } };
|
||||||
|
const legacy = protoEncode(data as any, RoutingHeadSchema);
|
||||||
|
const proton = protobuf_encode<RoutingHead>(data);
|
||||||
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes RoutingHead (group variant) identically', () => {
|
||||||
|
const data: RoutingHead = { grp: { groupCode: 555n } };
|
||||||
|
const legacy = protoEncode(data as any, RoutingHeadSchema);
|
||||||
|
const proton = protobuf_encode<RoutingHead>(data);
|
||||||
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes SendMessageRequest (group with two text elements) identically', () => {
|
||||||
|
const data: SendMessageRequest = {
|
||||||
|
routingHead: { grp: { groupCode: 555n } },
|
||||||
|
contentHead: { type: 1, subType: 1, c2cCmd: 11 },
|
||||||
|
messageBody: {
|
||||||
|
richText: {
|
||||||
|
elems: [
|
||||||
|
{ text: { str: 'hi ' } },
|
||||||
|
{ text: { str: 'world', link: 'lnk' } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
clientSequence: 1,
|
||||||
|
random: 12345,
|
||||||
|
syncCookie: new Uint8Array([0xaa, 0xbb]),
|
||||||
|
via: 1,
|
||||||
|
dataStatist: 1,
|
||||||
|
multiSendSeq: 1,
|
||||||
|
};
|
||||||
|
const legacy = protoEncode(data as any, SendMessageRequestSchema);
|
||||||
|
const proton = protobuf_encode<SendMessageRequest>(data);
|
||||||
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes SendMessageResponse identically', () => {
|
||||||
|
const data: SendMessageResponse = {
|
||||||
|
result: 1, errMsg: 'ok', timestamp1: 1234567890,
|
||||||
|
field10: 1, groupSequence: 100, timestamp2: 1234567891, privateSequence: 200,
|
||||||
|
};
|
||||||
|
const legacy = protoEncode(data as any, SendMessageResponseSchema);
|
||||||
|
const proton = protobuf_encode<SendMessageResponse>(data);
|
||||||
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes MentionExtraSend identically', () => {
|
||||||
|
const data: MentionExtraSend = { type: 2, uin: 10001, field5: 1, uid: 'u_a' };
|
||||||
|
const legacy = protoEncode(data as any, MentionExtraSendSchema);
|
||||||
|
const proton = protobuf_encode<MentionExtraSend>(data);
|
||||||
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('encodes MarkdownData identically', () => {
|
||||||
|
const data: MarkdownData = { content: '**bold** _italic_' };
|
||||||
|
const legacy = protoEncode(data as any, MarkdownDataSchema);
|
||||||
|
const proton = protobuf_encode<MarkdownData>(data);
|
||||||
|
expect(hex(proton)).toBe(hex(legacy));
|
||||||
|
});
|
||||||
|
|
||||||
// ── Documented divergence: proto3 default-value omission ────────────
|
// ── Documented divergence: proto3 default-value omission ────────────
|
||||||
|
|
||||||
it('proton omits default-valued numeric fields (legacy emits them)', () => {
|
it('proton omits default-valued numeric fields (legacy emits them)', () => {
|
||||||
|
|||||||
Reference in new issue
Block a user