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:
1 parent
4fc52578a5
commit
ef4a321175
7 files changed
+154
-80
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import type { BridgeManager } from '../bridge/manager';
|
||||
import type { NtqqHandler } from '../protocol/ntqq-handler';
|
||||
import type { PacketSink } from '../protocol/types';
|
||||
import { createLogger, type Logger } from '../utils/logger';
|
||||
import {
|
||||
injectHookProcess,
|
||||
@@ -16,8 +16,11 @@ export type { HookProcessInfo, HookProcessStatus } from './types';
|
||||
export type { HookProcessBaseInfo } from './injector';
|
||||
|
||||
export type HookManagerDeps = {
|
||||
ntqq: NtqqHandler;
|
||||
bridgeManager: BridgeManager;
|
||||
/** Sink for parsed packets from any live HookSession. Defaults to
|
||||
* `bridgeManager.onPacket` — every packet flows straight into the
|
||||
* per-UIN bridge dispatcher with no intermediate event emitter. */
|
||||
onPacket?: PacketSink;
|
||||
/** Native injector entrypoints. Defaults to the real native addon. */
|
||||
injector?: HookSessionDeps['injector'];
|
||||
/** Hook pipe-client factory. Defaults to `new QqHookClient(pid)`. */
|
||||
@@ -47,8 +50,8 @@ export type HookManagerDeps = {
|
||||
* real QQ.exe or a native addon.
|
||||
*/
|
||||
export class HookManager {
|
||||
private readonly ntqq: NtqqHandler;
|
||||
private readonly bridgeManager: BridgeManager;
|
||||
private readonly onPacket: PacketSink;
|
||||
private readonly injector: HookSessionDeps['injector'];
|
||||
private readonly makeClient: HookSessionDeps['makeClient'];
|
||||
private readonly pipeWatcher: PipeWatcher;
|
||||
@@ -61,8 +64,8 @@ export class HookManager {
|
||||
private disposed = false;
|
||||
|
||||
constructor(deps: HookManagerDeps) {
|
||||
this.ntqq = deps.ntqq;
|
||||
this.bridgeManager = deps.bridgeManager;
|
||||
this.onPacket = deps.onPacket ?? ((pkt) => deps.bridgeManager.onPacket(pkt));
|
||||
this.log = deps.log ?? createLogger('Hook');
|
||||
|
||||
this.injector = deps.injector ?? {
|
||||
@@ -194,7 +197,7 @@ export class HookManager {
|
||||
injector: this.injector,
|
||||
makeClient: this.makeClient,
|
||||
pipeWatcher: this.pipeWatcher,
|
||||
ntqq: this.ntqq,
|
||||
onPacket: this.onPacket,
|
||||
log: this.log,
|
||||
});
|
||||
session.attachProcessInfo({ name: defaultProcessName() });
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { EventEmitter } from 'events';
|
||||
import type { NtqqHandler } from '../protocol/ntqq-handler';
|
||||
import type { PacketSender } from '../protocol/packet-sender';
|
||||
import type { PacketSink } from '../protocol/types';
|
||||
import { createLogger, type Logger } from '../utils/logger';
|
||||
import { HookPacketClient } from './hook-packet-client';
|
||||
import type { HookInjectResult } from './injector';
|
||||
@@ -16,8 +16,10 @@ export type HookSessionDeps = {
|
||||
/** Sync fast-path check used by load() to skip re-injection when a
|
||||
* prior SnowLuma run left a working pipe behind. */
|
||||
pipeWatcher: { isPipeLive: (pid: number) => boolean };
|
||||
/** Optional packet sink. If provided, parsed packets are forwarded here. */
|
||||
ntqq?: NtqqHandler;
|
||||
/** Sink for parsed packets. Called with the BridgeManager-shaped
|
||||
* PacketInfo for every packet received while logged in. If omitted,
|
||||
* packets are dropped (useful in unit tests that don't care). */
|
||||
onPacket?: PacketSink;
|
||||
log?: Logger;
|
||||
};
|
||||
|
||||
@@ -44,7 +46,7 @@ export class HookSession extends EventEmitter {
|
||||
private readonly injector: HookSessionDeps['injector'];
|
||||
private readonly makeClient: HookSessionDeps['makeClient'];
|
||||
private readonly pipeWatcher: HookSessionDeps['pipeWatcher'];
|
||||
private readonly ntqq: NtqqHandler | null;
|
||||
private readonly onPacket: PacketSink | null;
|
||||
private readonly log: Logger;
|
||||
|
||||
private _status: HookProcessStatus = 'available';
|
||||
@@ -70,7 +72,7 @@ export class HookSession extends EventEmitter {
|
||||
this.injector = deps.injector;
|
||||
this.makeClient = deps.makeClient;
|
||||
this.pipeWatcher = deps.pipeWatcher;
|
||||
this.ntqq = deps.ntqq ?? null;
|
||||
this.onPacket = deps.onPacket ?? null;
|
||||
this.log = deps.log ?? createLogger('HookSession');
|
||||
}
|
||||
|
||||
@@ -395,7 +397,20 @@ export class HookSession extends EventEmitter {
|
||||
if (!this.loggedIn) return;
|
||||
const uin = packet.uin || this._uin;
|
||||
if (!isRealUin(uin)) return;
|
||||
this.ntqq?.onHookPacket(this.pid, { ...packet, uin });
|
||||
if (!this.onPacket) return;
|
||||
// Re-shape the hook-client wire packet into BridgeManager's PacketInfo
|
||||
// shape. Used to live in the deleted NtqqHandler.onHookPacket; field
|
||||
// renaming was that module's entire purpose, so it lives at the source
|
||||
// now (no need for a single-listener event-emitter in between).
|
||||
this.onPacket({
|
||||
pid: this.pid,
|
||||
uin,
|
||||
serviceCmd: packet.cmd,
|
||||
seqId: packet.seq,
|
||||
retCode: packet.error,
|
||||
fromClient: false,
|
||||
body: Buffer.from(packet.body),
|
||||
});
|
||||
}
|
||||
|
||||
private setStatus(status: HookProcessStatus, error: string): void {
|
||||
|
||||
Reference in new issue
Block a user