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,8 +1,8 @@
|
|||||||
// BridgeManager — manages per-UIN Bridge sessions.
|
// 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
|
// 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 type { PacketInfo } from '../protocol/types';
|
||||||
import { Bridge } from './bridge';
|
import { Bridge } from './bridge';
|
||||||
import { IdentityService } from './identity-service';
|
import { IdentityService } from './identity-service';
|
||||||
@@ -31,10 +31,6 @@ export class BridgeManager {
|
|||||||
setSessionStartedCallback(cb: SessionStartedCallback): void { this.onSessionStarted_ = cb; }
|
setSessionStartedCallback(cb: SessionStartedCallback): void { this.onSessionStarted_ = cb; }
|
||||||
setSessionClosedCallback(cb: SessionClosedCallback): void { this.onSessionClosed_ = cb; }
|
setSessionClosedCallback(cb: SessionClosedCallback): void { this.onSessionClosed_ = cb; }
|
||||||
|
|
||||||
bind(ntqq: NtqqHandler): void {
|
|
||||||
ntqq.registerCmdAll((pkt: PacketInfo) => this.onPacket(pkt));
|
|
||||||
}
|
|
||||||
|
|
||||||
onPidDisconnected(pid: number): void {
|
onPidDisconnected(pid: number): void {
|
||||||
this.pidPacketClients_.delete(pid);
|
this.pidPacketClients_.delete(pid);
|
||||||
const uin = this.pidToUin_.get(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;
|
if (!pkt.uin || !BridgeManager.isRealUin(pkt.uin)) return;
|
||||||
const uin = pkt.uin;
|
const uin = pkt.uin;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { BridgeManager } from '../bridge/manager';
|
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 { createLogger, type Logger } from '../utils/logger';
|
||||||
import {
|
import {
|
||||||
injectHookProcess,
|
injectHookProcess,
|
||||||
@@ -16,8 +16,11 @@ export type { HookProcessInfo, HookProcessStatus } from './types';
|
|||||||
export type { HookProcessBaseInfo } from './injector';
|
export type { HookProcessBaseInfo } from './injector';
|
||||||
|
|
||||||
export type HookManagerDeps = {
|
export type HookManagerDeps = {
|
||||||
ntqq: NtqqHandler;
|
|
||||||
bridgeManager: BridgeManager;
|
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. */
|
/** Native injector entrypoints. Defaults to the real native addon. */
|
||||||
injector?: HookSessionDeps['injector'];
|
injector?: HookSessionDeps['injector'];
|
||||||
/** Hook pipe-client factory. Defaults to `new QqHookClient(pid)`. */
|
/** Hook pipe-client factory. Defaults to `new QqHookClient(pid)`. */
|
||||||
@@ -47,8 +50,8 @@ export type HookManagerDeps = {
|
|||||||
* real QQ.exe or a native addon.
|
* real QQ.exe or a native addon.
|
||||||
*/
|
*/
|
||||||
export class HookManager {
|
export class HookManager {
|
||||||
private readonly ntqq: NtqqHandler;
|
|
||||||
private readonly bridgeManager: BridgeManager;
|
private readonly bridgeManager: BridgeManager;
|
||||||
|
private readonly onPacket: PacketSink;
|
||||||
private readonly injector: HookSessionDeps['injector'];
|
private readonly injector: HookSessionDeps['injector'];
|
||||||
private readonly makeClient: HookSessionDeps['makeClient'];
|
private readonly makeClient: HookSessionDeps['makeClient'];
|
||||||
private readonly pipeWatcher: PipeWatcher;
|
private readonly pipeWatcher: PipeWatcher;
|
||||||
@@ -61,8 +64,8 @@ export class HookManager {
|
|||||||
private disposed = false;
|
private disposed = false;
|
||||||
|
|
||||||
constructor(deps: HookManagerDeps) {
|
constructor(deps: HookManagerDeps) {
|
||||||
this.ntqq = deps.ntqq;
|
|
||||||
this.bridgeManager = deps.bridgeManager;
|
this.bridgeManager = deps.bridgeManager;
|
||||||
|
this.onPacket = deps.onPacket ?? ((pkt) => deps.bridgeManager.onPacket(pkt));
|
||||||
this.log = deps.log ?? createLogger('Hook');
|
this.log = deps.log ?? createLogger('Hook');
|
||||||
|
|
||||||
this.injector = deps.injector ?? {
|
this.injector = deps.injector ?? {
|
||||||
@@ -194,7 +197,7 @@ export class HookManager {
|
|||||||
injector: this.injector,
|
injector: this.injector,
|
||||||
makeClient: this.makeClient,
|
makeClient: this.makeClient,
|
||||||
pipeWatcher: this.pipeWatcher,
|
pipeWatcher: this.pipeWatcher,
|
||||||
ntqq: this.ntqq,
|
onPacket: this.onPacket,
|
||||||
log: this.log,
|
log: this.log,
|
||||||
});
|
});
|
||||||
session.attachProcessInfo({ name: defaultProcessName() });
|
session.attachProcessInfo({ name: defaultProcessName() });
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import type { NtqqHandler } from '../protocol/ntqq-handler';
|
|
||||||
import type { PacketSender } from '../protocol/packet-sender';
|
import type { PacketSender } from '../protocol/packet-sender';
|
||||||
|
import type { PacketSink } from '../protocol/types';
|
||||||
import { createLogger, type Logger } from '../utils/logger';
|
import { createLogger, type Logger } from '../utils/logger';
|
||||||
import { HookPacketClient } from './hook-packet-client';
|
import { HookPacketClient } from './hook-packet-client';
|
||||||
import type { HookInjectResult } from './injector';
|
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
|
/** Sync fast-path check used by load() to skip re-injection when a
|
||||||
* prior SnowLuma run left a working pipe behind. */
|
* prior SnowLuma run left a working pipe behind. */
|
||||||
pipeWatcher: { isPipeLive: (pid: number) => boolean };
|
pipeWatcher: { isPipeLive: (pid: number) => boolean };
|
||||||
/** Optional packet sink. If provided, parsed packets are forwarded here. */
|
/** Sink for parsed packets. Called with the BridgeManager-shaped
|
||||||
ntqq?: NtqqHandler;
|
* 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;
|
log?: Logger;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,7 +46,7 @@ export class HookSession extends EventEmitter {
|
|||||||
private readonly injector: HookSessionDeps['injector'];
|
private readonly injector: HookSessionDeps['injector'];
|
||||||
private readonly makeClient: HookSessionDeps['makeClient'];
|
private readonly makeClient: HookSessionDeps['makeClient'];
|
||||||
private readonly pipeWatcher: HookSessionDeps['pipeWatcher'];
|
private readonly pipeWatcher: HookSessionDeps['pipeWatcher'];
|
||||||
private readonly ntqq: NtqqHandler | null;
|
private readonly onPacket: PacketSink | null;
|
||||||
private readonly log: Logger;
|
private readonly log: Logger;
|
||||||
|
|
||||||
private _status: HookProcessStatus = 'available';
|
private _status: HookProcessStatus = 'available';
|
||||||
@@ -70,7 +72,7 @@ export class HookSession extends EventEmitter {
|
|||||||
this.injector = deps.injector;
|
this.injector = deps.injector;
|
||||||
this.makeClient = deps.makeClient;
|
this.makeClient = deps.makeClient;
|
||||||
this.pipeWatcher = deps.pipeWatcher;
|
this.pipeWatcher = deps.pipeWatcher;
|
||||||
this.ntqq = deps.ntqq ?? null;
|
this.onPacket = deps.onPacket ?? null;
|
||||||
this.log = deps.log ?? createLogger('HookSession');
|
this.log = deps.log ?? createLogger('HookSession');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -395,7 +397,20 @@ export class HookSession extends EventEmitter {
|
|||||||
if (!this.loggedIn) return;
|
if (!this.loggedIn) return;
|
||||||
const uin = packet.uin || this._uin;
|
const uin = packet.uin || this._uin;
|
||||||
if (!isRealUin(uin)) return;
|
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 {
|
private setStatus(status: HookProcessStatus, error: string): void {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { NtqqHandler } from './protocol/ntqq-handler';
|
|
||||||
import { BridgeManager } from './bridge/manager';
|
import { BridgeManager } from './bridge/manager';
|
||||||
import { OneBotManager } from './onebot/manager';
|
import { OneBotManager } from './onebot/manager';
|
||||||
import { loadRuntimeConfig } from './common/runtime';
|
import { loadRuntimeConfig } from './common/runtime';
|
||||||
@@ -11,13 +10,13 @@ const log = createLogger('App');
|
|||||||
async function main() {
|
async function main() {
|
||||||
log.info('SnowLuma starting');
|
log.info('SnowLuma starting');
|
||||||
|
|
||||||
const ntqq = new NtqqHandler();
|
|
||||||
const bridgeManager = new BridgeManager();
|
const bridgeManager = new BridgeManager();
|
||||||
const oneBotManager = new OneBotManager();
|
const oneBotManager = new OneBotManager();
|
||||||
const hookManager = new HookManager({ ntqq, bridgeManager });
|
// HookManager defaults its packet sink to bridgeManager.onPacket, so
|
||||||
|
// every parsed hook packet reaches the per-UIN bridge dispatcher with
|
||||||
|
// no intermediate layer.
|
||||||
|
const hookManager = new HookManager({ bridgeManager });
|
||||||
|
|
||||||
// Bind bridge manager to NTQQ handler (receives all parsed packets)
|
|
||||||
bridgeManager.bind(ntqq);
|
|
||||||
oneBotManager.bind(bridgeManager);
|
oneBotManager.bind(bridgeManager);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,3 +8,9 @@ export interface PacketInfo {
|
|||||||
fromClient: boolean;
|
fromClient: boolean;
|
||||||
body: Uint8Array;
|
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;
|
||||||
@@ -2,7 +2,8 @@ import { describe, it, expect, vi } from 'vitest';
|
|||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { HookSession } from '../src/hook/hook-session';
|
import { HookSession } from '../src/hook/hook-session';
|
||||||
import type { ManualMapHandle } from '../src/hook/injector';
|
import type { ManualMapHandle } from '../src/hook/injector';
|
||||||
import type { QqHookClient } from '../src/hook/qq-hook-client';
|
import type { QqHookClient, QqHookPacket } from '../src/hook/qq-hook-client';
|
||||||
|
import type { PacketSink } from '../src/protocol/types';
|
||||||
|
|
||||||
const DUMMY_HANDLE: ManualMapHandle = { base: 0n, entry: 0n, exceptionTable: 0n, size: 0 };
|
const DUMMY_HANDLE: ManualMapHandle = { base: 0n, entry: 0n, exceptionTable: 0n, size: 0 };
|
||||||
|
|
||||||
@@ -30,9 +31,18 @@ class FakeClient extends EventEmitter {
|
|||||||
this.loginState = { loggedIn: true, uin, uinNumber: BigInt(uin) };
|
this.loginState = { loggedIn: true, uin, uinNumber: BigInt(uin) };
|
||||||
this.emit('loginState', { ...this.loginState });
|
this.emit('loginState', { ...this.loginState });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
firePacket(packet: QqHookPacket): void {
|
||||||
|
this.emit('packet', packet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeSession(opts: { pid?: number; pipeLive?: boolean; clientFailsConnect?: boolean } = {}) {
|
function makeSession(opts: {
|
||||||
|
pid?: number;
|
||||||
|
pipeLive?: boolean;
|
||||||
|
clientFailsConnect?: boolean;
|
||||||
|
onPacket?: PacketSink;
|
||||||
|
} = {}) {
|
||||||
const pid = opts.pid ?? 1234;
|
const pid = opts.pid ?? 1234;
|
||||||
let pipeLive = opts.pipeLive ?? false;
|
let pipeLive = opts.pipeLive ?? false;
|
||||||
const clients: FakeClient[] = [];
|
const clients: FakeClient[] = [];
|
||||||
@@ -51,13 +61,14 @@ function makeSession(opts: { pid?: number; pipeLive?: boolean; clientFailsConnec
|
|||||||
return c as unknown as QqHookClient;
|
return c as unknown as QqHookClient;
|
||||||
},
|
},
|
||||||
pipeWatcher: { isPipeLive: () => pipeLive },
|
pipeWatcher: { isPipeLive: () => pipeLive },
|
||||||
|
onPacket: opts.onPacket,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
session,
|
session,
|
||||||
injector,
|
injector,
|
||||||
clients,
|
clients,
|
||||||
currentClient: () => clients[clients.length - 1],
|
currentClient: () => clients[clients.length - 1]!,
|
||||||
setPipeLive: (v: boolean) => { pipeLive = v; },
|
setPipeLive: (v: boolean) => { pipeLive = v; },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -307,3 +318,101 @@ describe('HookSession — process gone', () => {
|
|||||||
expect(events).toEqual(['disposed']);
|
expect(events).toEqual(['disposed']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('HookSession — packet forwarding', () => {
|
||||||
|
// These tests cover the field-rename + filter logic that used to live
|
||||||
|
// inside the deleted NtqqHandler.onHookPacket. Inlining it here means
|
||||||
|
// we can exercise the transformation with a plain vi.fn() sink, no
|
||||||
|
// event-emitter machinery required.
|
||||||
|
|
||||||
|
it('forwards parsed packets to onPacket sink with PacketInfo shape', async () => {
|
||||||
|
const onPacket = vi.fn();
|
||||||
|
const ctx = makeSession({ pipeLive: true, onPacket });
|
||||||
|
await ctx.session.load();
|
||||||
|
ctx.session.onPipeUp();
|
||||||
|
await flush();
|
||||||
|
ctx.currentClient().fireLogin('10001');
|
||||||
|
|
||||||
|
ctx.currentClient().firePacket({
|
||||||
|
seq: 42,
|
||||||
|
error: 0,
|
||||||
|
cmd: 'trpc.msg.olpush.OlPushService.MsgPush',
|
||||||
|
uin: '10001',
|
||||||
|
body: Buffer.from([0x01, 0x02, 0x03]),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onPacket).toHaveBeenCalledOnce();
|
||||||
|
const pkt = onPacket.mock.calls[0]![0];
|
||||||
|
expect(pkt).toMatchObject({
|
||||||
|
pid: 1234,
|
||||||
|
uin: '10001',
|
||||||
|
serviceCmd: 'trpc.msg.olpush.OlPushService.MsgPush',
|
||||||
|
seqId: 42,
|
||||||
|
retCode: 0,
|
||||||
|
fromClient: false,
|
||||||
|
});
|
||||||
|
expect(Buffer.isBuffer(pkt.body)).toBe(true);
|
||||||
|
expect([...pkt.body]).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to session uin when packet.uin is empty', async () => {
|
||||||
|
const onPacket = vi.fn();
|
||||||
|
const ctx = makeSession({ pipeLive: true, onPacket });
|
||||||
|
await ctx.session.load();
|
||||||
|
ctx.session.onPipeUp();
|
||||||
|
await flush();
|
||||||
|
ctx.currentClient().fireLogin('10001');
|
||||||
|
|
||||||
|
ctx.currentClient().firePacket({
|
||||||
|
seq: 1, error: 0, cmd: 'foo', uin: '', body: Buffer.alloc(0),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onPacket.mock.calls[0]![0].uin).toBe('10001');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops packets received before login', async () => {
|
||||||
|
const onPacket = vi.fn();
|
||||||
|
const ctx = makeSession({ pipeLive: true, onPacket });
|
||||||
|
await ctx.session.load();
|
||||||
|
ctx.session.onPipeUp();
|
||||||
|
await flush();
|
||||||
|
// No login fired yet.
|
||||||
|
|
||||||
|
ctx.currentClient().firePacket({
|
||||||
|
seq: 1, error: 0, cmd: 'foo', uin: '10001', body: Buffer.alloc(0),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onPacket).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('drops packets whose uin is non-real ("0" / too short / non-numeric)', async () => {
|
||||||
|
const onPacket = vi.fn();
|
||||||
|
const ctx = makeSession({ pipeLive: true, onPacket });
|
||||||
|
await ctx.session.load();
|
||||||
|
ctx.session.onPipeUp();
|
||||||
|
await flush();
|
||||||
|
ctx.currentClient().fireLogin('10001');
|
||||||
|
|
||||||
|
// Packet uin '0' falls back to session uin '10001' — that one passes.
|
||||||
|
// Force a bad uin via the packet AND null out session uin by simulating
|
||||||
|
// a logout? Simpler: send a packet with uin '42' (numeric but <5 chars).
|
||||||
|
ctx.currentClient().firePacket({
|
||||||
|
seq: 1, error: 0, cmd: 'foo', uin: '42', body: Buffer.alloc(0),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onPacket).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is a no-op when no onPacket sink is provided', async () => {
|
||||||
|
const ctx = makeSession({ pipeLive: true }); // no onPacket
|
||||||
|
await ctx.session.load();
|
||||||
|
ctx.session.onPipeUp();
|
||||||
|
await flush();
|
||||||
|
ctx.currentClient().fireLogin('10001');
|
||||||
|
|
||||||
|
// Should not throw / crash.
|
||||||
|
ctx.currentClient().firePacket({
|
||||||
|
seq: 1, error: 0, cmd: 'foo', uin: '10001', body: Buffer.from([7]),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user