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

+3 -7
View File
@@ -1,8 +1,8 @@
// BridgeManager — manages per-UIN Bridge sessions.
// Wires NtqqHandler packets to appropriate Bridge instances.
// onPacket() is the single entry point for parsed hook packets;
// HookManager wires it as the default packet sink for every HookSession.
// Port of src/bridge/include/bridge/manager.h + src/bridge/src/manager.cpp
import type { NtqqHandler } from '../protocol/ntqq-handler';
import type { PacketInfo } from '../protocol/types';
import { Bridge } from './bridge';
import { IdentityService } from './identity-service';
@@ -31,10 +31,6 @@ export class BridgeManager {
setSessionStartedCallback(cb: SessionStartedCallback): void { this.onSessionStarted_ = cb; }
setSessionClosedCallback(cb: SessionClosedCallback): void { this.onSessionClosed_ = cb; }
bind(ntqq: NtqqHandler): void {
ntqq.registerCmdAll((pkt: PacketInfo) => this.onPacket(pkt));
}
onPidDisconnected(pid: number): void {
this.pidPacketClients_.delete(pid);
const uin = this.pidToUin_.get(pid);
@@ -76,7 +72,7 @@ export class BridgeManager {
}
}
private onPacket(pkt: PacketInfo): void {
onPacket(pkt: PacketInfo): void {
if (!pkt.uin || !BridgeManager.isRealUin(pkt.uin)) return;
const uin = pkt.uin;