refactor(core): drop NtqqHandler shim, route packets directly to BridgeManager

NtqqHandler was a 54-line "shallow module": half of its public surface
(registerCmd by service-cmd) had zero callers — cmdHandlers was a Map
that nothing ever wrote to. The remaining half (registerCmdAll) had
exactly one subscriber: BridgeManager.bind() wiring its private
onPacket. So NtqqHandler was, in effect, a one-listener event emitter
plus a five-line field-rename from QqHookPacket to PacketInfo.

The deletion test concentrates the change rather than scattering it:

- HookSession.handlePacket inlines the field rename and pushes
  straight into a PacketSink dependency (PacketInfo => void).
  HookSession's ctor now takes `onPacket?: PacketSink` instead of
  `ntqq?: NtqqHandler` — the new shape is exactly what tests want
  (a vi.fn() spy with no event-emitter scaffolding).

- HookManager's `ntqq: NtqqHandler` constructor dep becomes
  `onPacket?: PacketSink`, defaulting to `bridgeManager.onPacket` so
  the common-case wiring stays one line at the call site.

- BridgeManager.bind(ntqq) is deleted; BridgeManager.onPacket becomes
  public (it was always the real entry point). The file header
  comment now describes onPacket as the single packet entry rather
  than "wires NtqqHandler".

- index.ts shrinks: no `new NtqqHandler()` and no `bridgeManager.bind`.
  `new HookManager({ bridgeManager })` covers both wires.

- packages/core/src/protocol/ntqq-handler.ts deleted.

Behavior preserved: same field rename (Buffer.from(packet.body) for
the body copy that QqHookClient already does, kept to match
byte-for-byte), same login + real-uin filter, same logging scope is
gone (`createLogger('NTQQ')` had no remaining call sites). PacketSink
type alias added to protocol/types.ts so the dep type stays readable
across HookSession + HookManager.

Adds 5 unit tests for the inlined packet path that the deleted module
never had: PacketInfo shape, session-uin fallback when packet.uin is
empty, pre-login drop, non-real-uin drop, and a no-sink no-op.

184 tests pass (was 179); only failing suite is the pre-existing
macOS-arm64 missing websocket native addon, unrelated.
This commit is contained in:
DrSmoothl committed 2026-05-12 14:56:38 +08:00
1 parent 4fc52578a5
commit ef4a321175
7 files changed
+154 -80

No files matched your search

@@ -1,54 +0,0 @@
import { createLogger } from '../utils/logger';
import type { PacketInfo } from './types';
const log = createLogger('NTQQ');
export type CmdCallback = (pkt: PacketInfo) => void;
export interface HookPacketInput {
seq: number;
error: number;
cmd: string;
uin: string;
body: Uint8Array;
}
export class NtqqHandler {
private cmdHandlers = new Map<string, CmdCallback[]>();
private cmdAllHandlers: CmdCallback[] = [];
registerCmd(cmd: string, cb: CmdCallback): void {
const arr = this.cmdHandlers.get(cmd) ?? [];
arr.push(cb);
this.cmdHandlers.set(cmd, arr);
}
registerCmdAll(cb: CmdCallback): void { this.cmdAllHandlers.push(cb); }
onHookPacket(pid: number, packet: HookPacketInput): void {
this.dispatchPacket({
pid,
uin: packet.uin,
serviceCmd: packet.cmd,
seqId: packet.seq,
retCode: packet.error,
fromClient: false,
body: Buffer.from(packet.body),
});
}
private dispatchPacket(pkt: PacketInfo): void {
const specific = this.cmdHandlers.get(pkt.serviceCmd);
if (specific) {
for (const cb of specific) {
try { cb(pkt); } catch (e) {
log.error('dispatch error for %s: %s', pkt.serviceCmd, e instanceof Error ? (e.stack ?? e.message) : String(e));
}
}
}
for (const cb of this.cmdAllHandlers) {
try { cb(pkt); } catch (e) {
log.error('dispatch error for %s: %s', pkt.serviceCmd, e instanceof Error ? (e.stack ?? e.message) : String(e));
}
}
}
}
+6
View File
@@ -8,3 +8,9 @@ export interface PacketInfo {
fromClient: boolean;
body: Uint8Array;
}
/**
* Packet sink injected into HookSession / HookManager. The default
* wiring forwards to BridgeManager.onPacket, but tests inject spies.
*/
export type PacketSink = (packet: PacketInfo) => void;