refactor(core): split HookManager into PipeWatcher + HookSession

HookManager was a 515-line god class with four tangled responsibilities
(state machine, watcher loop, per-PID serialization, lifecycle plumbing)
and no tests — every code path needed a real QQ.exe to exercise.

Split into three modules:

- PipeWatcher: the only module that polls the OS for QQ.exe processes
  and live SnowLuma named pipes. Emits diff events (process-discovered,
  process-gone, pipe-up, pipe-down, tick). Native list functions are
  constructor deps so tests can drive ticks with in-memory fakes.

- HookSession: per-PID lifecycle. Owns its own state machine, its own
  promise chain (serializes load/unload/refresh/onPipeUp/onPipeDown so
  rapid user clicks don't race), and its own QqHookClient. Emits
  high-level events (login, disconnected, status-changed, disposed)
  that the manager forwards instead of reaching into BridgeManager
  directly. Injector and pipe-client factory are deps for testability.

- HookManager: shrinks to a thin orchestrator (227 lines, was 515).
  Routes user commands and watcher events to sessions, forwards session
  events to BridgeManager, retries stuck-in-connecting sessions on every
  watcher tick (preserves the original tickWatcher reconcile pass).
  Public API (listProcesses/loadProcess/unloadProcess/refreshProcess)
  unchanged for WebUI callers; only the constructor moves to a deps
  object: new HookManager({ ntqq, bridgeManager }).

Behavior preserved including all the edge cases the original code
handles: re-using a SnowLuma DLL that survived a process restart
(fast-path via pipeWatcher.isPipeLive), stale close events from a
replaced client, DLL-unload-failed fallback to "watcher will
reconnect", QQ.exe killed with a live pipe (pipe-down emitted before
process-gone so disconnect runs before dispose).

Adds 27 unit tests across two new vitest suites (hook-session.test.ts
and pipe-watcher.test.ts) covering load fast-path, login emission,
duplicate-login suppression, connect retry on failure, pipe up/down
cycles, unload while logged in, unload verification failure, user
command serialization, and process-gone handling — none of which
previously had test coverage.
This commit is contained in:
DrSmoothl committed 2026-05-12 14:43:30 +08:00
1 parent 5ce8aa60de
commit 4fc52578a5
7 files changed
+1338 -468

No files matched your search

+180 -467
View File
@@ -1,514 +1,227 @@
import type { BridgeManager } from '../bridge/manager';
import type { NtqqHandler } from '../protocol/ntqq-handler';
import { createLogger } from '../utils/logger';
import { HookPacketClient } from './hook-packet-client';
import { injectHookProcess, listHookProcesses, unloadHookProcess, type HookInjectResult, type HookProcessBaseInfo } from './injector';
import { QqHookClient, type QqHookLoginState, type QqHookPacket } from './qq-hook-client';
import { createLogger, type Logger } from '../utils/logger';
import {
injectHookProcess,
listHookProcesses,
unloadHookProcess,
type HookProcessBaseInfo,
} from './injector';
import { HookSession, type HookSessionDeps } from './hook-session';
import { PipeWatcher } from './pipe-watcher';
import { QqHookClient } from './qq-hook-client';
import type { HookProcessInfo } from './types';
const log = createLogger('Hook');
const DEFAULT_WATCHER_INTERVAL_MS = 1500;
const MIN_WATCHER_INTERVAL_MS = 250;
export type { HookProcessInfo, HookProcessStatus } from './types';
export type { HookProcessBaseInfo } from './injector';
export type HookProcessStatus =
| 'available'
| 'loading'
| 'connecting'
| 'loaded'
| 'online'
| 'error'
| 'disconnected';
export interface HookProcessInfo extends HookProcessBaseInfo {
injected: boolean;
connected: boolean;
loggedIn: boolean;
uin: string;
status: HookProcessStatus;
error: string;
method: string;
}
interface HookProcessState extends HookProcessInfo {
client: QqHookClient | null;
sender: HookPacketClient | null;
injectResult: HookInjectResult | null;
bound: boolean;
}
export interface HookManagerOptions {
/** Background pipe-watcher polling interval. Lower = faster reconnect but
* more frequent named-pipe directory listings. Defaults to 1500 ms. */
export type HookManagerDeps = {
ntqq: NtqqHandler;
bridgeManager: BridgeManager;
/** Native injector entrypoints. Defaults to the real native addon. */
injector?: HookSessionDeps['injector'];
/** Hook pipe-client factory. Defaults to `new QqHookClient(pid)`. */
makeClient?: HookSessionDeps['makeClient'];
/** Pre-built watcher. Defaults to a PipeWatcher wrapping the native listings. */
pipeWatcher?: PipeWatcher;
/** Polling interval for the default watcher. Ignored if `pipeWatcher` is provided. */
watcherIntervalMs?: number;
}
/** Native process lister used by `listProcesses()`. Defaults to the native addon. */
listProcesses?: () => HookProcessBaseInfo[];
log?: Logger;
};
/**
* Manages SnowLuma hook injection and bridge connectivity for QQ.exe processes.
* HookManager — thin orchestrator over a per-PID HookSession map and a
* singleton PipeWatcher.
*
* Lifecycle: load -> inject DLL only (returns fast) -> background watcher
* detects the named pipe coming up and connects -> on QQ login the bridge is
* notified. The watcher also handles reconnect after a pipe drop and detects
* hooks that survived a previous SnowLuma restart.
* Responsibilities:
* - Route user commands (load/unload/refresh) to the matching session.
* - Route watcher diff events to the matching session.
* - Forward session events ('login' / 'disconnected') to BridgeManager.
* - Retry stuck-in-connecting sessions on every watcher tick (so a
* failed connect eventually recovers without a manual refresh).
*
* Concurrency: every per-PID mutation (load / unload / refresh / connect /
* close-handler / watcher reconcile) goes through a per-PID promise chain so
* users can spam buttons without races.
* The native injector, native pipe-client, and native process/pipe
* listings are all swappable dependencies so tests can run without a
* real QQ.exe or a native addon.
*/
export class HookManager {
private readonly states = new Map<number, HookProcessState>();
private readonly opsByPid = new Map<number, Promise<unknown>>();
private readonly watcherIntervalMs: number;
private watcherTimer: NodeJS.Timeout | null = null;
private watcherTicking = false;
private readonly ntqq: NtqqHandler;
private readonly bridgeManager: BridgeManager;
private readonly injector: HookSessionDeps['injector'];
private readonly makeClient: HookSessionDeps['makeClient'];
private readonly pipeWatcher: PipeWatcher;
private readonly ownsPipeWatcher: boolean;
private readonly listProcessesNative: () => HookProcessBaseInfo[];
private readonly log: Logger;
private readonly sessions = new Map<number, HookSession>();
private readonly startPromise: Promise<void>;
private disposed = false;
constructor(
private readonly ntqq: NtqqHandler,
private readonly bridgeManager: BridgeManager,
options: HookManagerOptions = {},
) {
this.watcherIntervalMs = Math.max(
MIN_WATCHER_INTERVAL_MS,
options.watcherIntervalMs ?? DEFAULT_WATCHER_INTERVAL_MS,
);
// Boot the watcher on the next tick so callers can attach listeners first.
this.scheduleWatcher(0);
constructor(deps: HookManagerDeps) {
this.ntqq = deps.ntqq;
this.bridgeManager = deps.bridgeManager;
this.log = deps.log ?? createLogger('Hook');
this.injector = deps.injector ?? {
inject: injectHookProcess,
unload: (pid, handle) => {
if (!handle) return;
unloadHookProcess(pid, handle);
},
};
this.makeClient = deps.makeClient ?? ((pid: number) => new QqHookClient(pid));
this.listProcessesNative = deps.listProcesses ?? listHookProcesses;
if (deps.pipeWatcher) {
this.pipeWatcher = deps.pipeWatcher;
this.ownsPipeWatcher = false;
} else {
this.pipeWatcher = new PipeWatcher({
listProcesses: this.listProcessesNative,
listLivePipes: () => QqHookClient.listLivePipes(),
intervalMs: deps.watcherIntervalMs,
log: this.log,
});
this.ownsPipeWatcher = true;
}
this.bindWatcher();
this.startPromise = this.pipeWatcher.start();
}
// ─────────────── public API (unchanged from prior HookManager) ───────────────
async listProcesses(): Promise<HookProcessInfo[]> {
const processes = listHookProcesses();
for (const proc of processes) {
const state = this.ensureState(proc.pid);
state.name = proc.name || state.name;
state.path = proc.path || state.path;
await this.startPromise;
let processes: HookProcessBaseInfo[];
try {
processes = this.listProcessesNative();
} catch (error) {
this.log.warn('listProcesses failed: %s', errMsg(error));
processes = [];
}
const result: HookProcessInfo[] = [];
for (const proc of processes) {
result.push(this.toPublicInfo(this.ensureState(proc.pid)));
const session = this.ensureSession(proc.pid);
session.attachProcessInfo(proc);
result.push(session.toInfo());
}
return result.sort((a, b) => a.pid - b.pid);
}
loadProcess(pid: number): Promise<HookProcessInfo> {
if (!Number.isInteger(pid) || pid <= 0) throw new Error('invalid pid');
return this.serialize(pid, () => this.loadProcessInternal(pid));
async loadProcess(pid: number): Promise<HookProcessInfo> {
this.assertValidPid(pid);
await this.startPromise;
const session = this.ensureSession(pid);
const info = await session.load();
// Pull the next tick forward so a freshly-injected pipe gets noticed
// within the next event-loop turn instead of after the full interval.
this.pipeWatcher.wake();
return info;
}
unloadProcess(pid: number): Promise<HookProcessInfo> {
if (!Number.isInteger(pid) || pid <= 0) throw new Error('invalid pid');
return this.serialize(pid, () => this.unloadProcessInternal(pid));
async unloadProcess(pid: number): Promise<HookProcessInfo> {
this.assertValidPid(pid);
await this.startPromise;
const session = this.ensureSession(pid);
return session.unload();
}
/**
* Re-probe the named pipe for `pid`, reconnect if it became available,
* or tear down a stale connection if it disappeared. Safe to call at any
* time; serialised against load/unload/watcher actions on the same PID.
*/
refreshProcess(pid: number): Promise<HookProcessInfo> {
if (!Number.isInteger(pid) || pid <= 0) throw new Error('invalid pid');
return this.serialize(pid, () => this.refreshProcessInternal(pid));
async refreshProcess(pid: number): Promise<HookProcessInfo> {
this.assertValidPid(pid);
await this.startPromise;
const session = this.ensureSession(pid);
return session.refresh();
}
dispose(): void {
if (this.disposed) return;
this.disposed = true;
if (this.watcherTimer) {
clearTimeout(this.watcherTimer);
this.watcherTimer = null;
for (const session of this.sessions.values()) {
session.dispose();
}
for (const state of this.states.values()) {
this.tearDownClient(state);
state.status = state.injected ? 'disconnected' : 'available';
this.sessions.clear();
if (this.ownsPipeWatcher) {
this.pipeWatcher.dispose();
}
}
// ───────────────────────── per-PID serialization ─────────────────────────
// ─────────────── wiring ───────────────
/**
* Serialise an operation against a per-PID promise chain. Guarantees that
* load / unload / refresh / watcher-driven connects for the same PID never
* interleave even if the user mashes buttons.
*/
private async serialize<T>(pid: number, op: () => Promise<T>): Promise<T> {
const previous = this.opsByPid.get(pid) ?? Promise.resolve();
let release!: () => void;
const completion = new Promise<void>(resolve => { release = resolve; });
const chained = previous.then(() => completion);
this.opsByPid.set(pid, chained);
try {
await previous.catch(() => undefined);
return await op();
} finally {
release();
// Drop the chain entry once nothing else is waiting on it.
if (this.opsByPid.get(pid) === chained) this.opsByPid.delete(pid);
}
}
// ──────────────────────────── load / unload ──────────────────────────────
private async loadProcessInternal(pid: number): Promise<HookProcessInfo> {
const state = this.ensureState(pid);
state.error = '';
state.status = 'loading';
try {
if (!state.injected) {
// Fast path: a previous SnowLuma run may have left the hook DLL
// resident in QQ.exe. If the control pipe is already listed we skip
// re-injection and let the watcher reconnect instead.
const livePipes = await QqHookClient.listLivePipes();
if (livePipes.has(pid)) {
state.injected = true;
state.method = state.method || 'reconnect';
log.info('PID=%d already has SnowLuma pipe; will reconnect via watcher', pid);
} else {
state.injectResult = injectHookProcess(pid);
state.injected = true;
state.method = state.injectResult.method;
}
}
state.status = state.connected
? (state.loggedIn ? 'online' : 'loaded')
: 'connecting';
} catch (error) {
state.status = 'error';
state.error = error instanceof Error ? error.message : String(error);
log.error('load failed: PID=%d err=%s', pid, state.error);
}
// Wake the watcher so it can pick up the freshly-injected pipe asap
// instead of waiting up to one full interval.
this.scheduleWatcher(0);
return this.toPublicInfo(state);
}
private async unloadProcessInternal(pid: number): Promise<HookProcessInfo> {
const state = this.ensureState(pid);
state.error = '';
try {
const wasLoggedIn = state.loggedIn;
this.tearDownClient(state);
if (wasLoggedIn) this.bridgeManager.onPidDisconnected(pid);
const handle = state.injectResult?.handle;
if (state.injected && handle) {
unloadHookProcess(pid, handle);
log.info('SnowLuma unloaded from PID=%d via unloadModuleManual', pid);
}
// Clear injection state first
state.injected = false;
state.injectResult = null;
state.method = '';
state.uin = '0';
// Verify unload succeeded by checking if pipe still exists
const livePipes = await QqHookClient.listLivePipes();
if (livePipes.has(pid)) {
// Pipe still exists - unload failed, but let watcher handle reconnection
state.error = 'DLL卸载失败:命名管道仍然存在,watcher将自动重连';
state.status = 'connecting'; // Let watcher pick it up and reconnect
log.warn('unload verification failed: PID=%d pipe still exists after unloadModuleManual, watcher will reconnect', pid);
} else {
// Successfully unloaded
state.status = 'available';
state.error = '';
}
} catch (error) {
state.status = 'error';
state.error = error instanceof Error ? error.message : String(error);
log.error('unload failed: PID=%d err=%s', pid, state.error);
}
return this.toPublicInfo(state);
}
private async refreshProcessInternal(pid: number): Promise<HookProcessInfo> {
const state = this.ensureState(pid);
state.error = '';
try {
const livePipes = await QqHookClient.listLivePipes();
const pipeUp = livePipes.has(pid);
if (pipeUp) {
// A live pipe means a SnowLuma DLL is resident — even if we did not
// inject it ourselves (e.g. left over from a prior run).
if (!state.injected) {
state.injected = true;
state.method = state.method || 'reconnect';
}
if (state.client?.isClosed) this.tearDownClient(state);
if (!state.connected) {
await this.attemptConnect(state);
} else {
state.status = state.loggedIn ? 'online' : 'loaded';
}
} else {
// Pipe is gone; if we still hold a client, drop it and surface the
// appropriate disconnected/available state to the UI.
const wasLoggedIn = state.loggedIn;
this.tearDownClient(state);
if (wasLoggedIn) this.bridgeManager.onPidDisconnected(pid);
state.status = state.injected ? 'disconnected' : 'available';
}
} catch (error) {
state.error = error instanceof Error ? error.message : String(error);
state.status = state.injected ? 'disconnected' : 'error';
log.warn('refresh failed: PID=%d err=%s', pid, state.error);
}
return this.toPublicInfo(state);
}
private ensureState(pid: number): HookProcessState {
let state = this.states.get(pid);
if (!state) {
state = {
pid,
name: 'QQ.exe',
path: '',
injected: false,
connected: false,
loggedIn: false,
uin: '0',
status: 'available',
error: '',
method: '',
client: null,
sender: null,
injectResult: null,
bound: false,
};
this.states.set(pid, state);
}
return state;
}
// ───────────────────────────── pipe watcher ──────────────────────────────
private scheduleWatcher(delayMs: number): void {
if (this.disposed) return;
if (this.watcherTimer) {
clearTimeout(this.watcherTimer);
this.watcherTimer = null;
}
this.watcherTimer = setTimeout(() => {
this.watcherTimer = null;
void this.tickWatcher();
}, Math.max(0, delayMs));
}
private async tickWatcher(): Promise<void> {
if (this.disposed) return;
if (this.watcherTicking) {
this.scheduleWatcher(this.watcherIntervalMs);
return;
}
this.watcherTicking = true;
try {
const livePipes = await QqHookClient.listLivePipes();
const processes = listHookProcesses();
const runningSet = new Set(processes.map(p => p.pid));
// Refresh metadata + ensure a state entry exists for each known PID.
for (const proc of processes) {
const state = this.ensureState(proc.pid);
state.name = proc.name || state.name;
state.path = proc.path || state.path;
}
// Auto-detect hooks that survived a SnowLuma restart or failed unload.
for (const pid of livePipes) {
if (!runningSet.has(pid)) continue;
const state = this.ensureState(pid);
if (!state.injected) {
state.injected = true;
state.method = state.method || 'reconnect';
if (state.status === 'available') state.status = 'connecting';
log.info('Detected pre-existing SnowLuma pipe in PID=%d', pid);
}
}
const tasks: Promise<unknown>[] = [];
for (const state of [...this.states.values()]) {
const alivePipe = livePipes.has(state.pid);
const aliveProc = runningSet.has(state.pid);
if (!aliveProc) {
const wasLoggedIn = state.loggedIn;
this.tearDownClient(state);
if (wasLoggedIn) this.bridgeManager.onPidDisconnected(state.pid);
this.states.delete(state.pid);
continue;
}
// Pipe vanished but we still hold a client → schedule a teardown.
if (state.connected && !alivePipe) {
tasks.push(this.serialize(state.pid, async () => this.reconcileLostPipe(state.pid))
.catch(err => log.warn('watcher teardown failed: PID=%d err=%s', state.pid, this.errMsg(err))));
continue;
}
// Injected & pipe up but not connected → schedule a connect.
if (state.injected && !state.connected && alivePipe) {
tasks.push(this.serialize(state.pid, async () => this.reconcileConnect(state.pid))
.catch(err => log.warn('watcher connect failed: PID=%d err=%s', state.pid, this.errMsg(err))));
continue;
}
// Injected but pipe still pending → keep status visible to the UI.
if (state.injected && !state.connected && !alivePipe) {
if (state.status !== 'error' && state.status !== 'disconnected' && state.status !== 'loading') {
state.status = 'connecting';
}
}
}
await Promise.all(tasks);
} finally {
this.watcherTicking = false;
this.scheduleWatcher(this.watcherIntervalMs);
}
}
/** Re-check pipe and connect under the per-PID lock. */
private async reconcileConnect(pid: number): Promise<void> {
const state = this.states.get(pid);
if (!state || !state.injected || state.connected) return;
if (state.client?.isClosed) this.tearDownClient(state);
const live = await QqHookClient.listLivePipes();
if (!live.has(pid)) return;
await this.attemptConnect(state);
}
/** Tear down a client whose pipe disappeared. */
private async reconcileLostPipe(pid: number): Promise<void> {
const state = this.states.get(pid);
if (!state || !state.connected) return;
const live = await QqHookClient.listLivePipes();
if (live.has(pid)) return;
const wasLoggedIn = state.loggedIn;
this.tearDownClient(state);
state.status = state.injected ? (wasLoggedIn ? 'disconnected' : 'connecting') : 'available';
if (wasLoggedIn) this.bridgeManager.onPidDisconnected(pid);
}
// ───────────────────────────── client plumbing ───────────────────────────
private async attemptConnect(state: HookProcessState): Promise<void> {
if (state.connected) return;
if (state.client?.isClosed) this.tearDownClient(state);
if (!state.client) {
state.client = new QqHookClient(state.pid);
state.sender = new HookPacketClient(state.client);
state.bound = false;
}
if (!state.bound) {
this.bindClient(state, state.client);
state.bound = true;
}
const client = state.client;
try {
await client.connectAll({ recv: true });
state.connected = true;
state.status = client.isLoggedIn ? 'online' : 'loaded';
state.error = '';
const loginState = client.getLoginState();
if (loginState.loggedIn) this.handleLoginState(state, loginState);
log.info('pipe connected: PID=%d', state.pid);
} catch (error) {
state.error = this.errMsg(error);
// Drop the client so the next attempt builds a fresh socket pair.
this.tearDownClient(state);
state.status = state.injected ? 'connecting' : 'available';
}
}
private bindClient(state: HookProcessState, client: QqHookClient): void {
client.on('packet', packet => this.handlePacket(state, packet));
client.on('loginState', loginState => this.handleLoginState(state, loginState));
client.on('error', error => {
const msg = this.errMsg(error);
state.error = msg;
log.warn('pipe error: PID=%d err=%s', state.pid, msg);
});
client.on('close', () => {
private bindWatcher(): void {
this.pipeWatcher.on('process-discovered', (info: HookProcessBaseInfo) => {
if (this.disposed) return;
// Only the client that is currently registered should drive a reconcile;
// listeners may fire for an already-replaced client.
if (state.client !== client) return;
void this.serialize(state.pid, async () => {
if (state.client !== client) return;
const wasLoggedIn = state.loggedIn;
this.tearDownClient(state);
if (!state.injected) {
state.status = 'available';
} else if (wasLoggedIn) {
state.status = 'disconnected';
this.bridgeManager.onPidDisconnected(state.pid);
} else {
state.status = 'connecting';
const session = this.ensureSession(info.pid);
session.attachProcessInfo(info);
});
this.pipeWatcher.on('process-gone', (pid: number) => {
if (this.disposed) return;
const session = this.sessions.get(pid);
if (session) session.notifyProcessGone();
});
this.pipeWatcher.on('pipe-up', (pid: number) => {
if (this.disposed) return;
const session = this.sessions.get(pid);
if (session) session.onPipeUp();
});
this.pipeWatcher.on('pipe-down', (pid: number) => {
if (this.disposed) return;
const session = this.sessions.get(pid);
if (session) session.onPipeDown();
});
// After every tick, retry sessions that are stuck in 'connecting'
// while the pipe is up. Mirrors the original tickWatcher's per-tick
// reconcileConnect pass; onPipeUp is idempotent so calling it for an
// already-connected session is harmless.
this.pipeWatcher.on('tick', () => {
if (this.disposed) return;
for (const session of this.sessions.values()) {
if (session.status === 'connecting' && this.pipeWatcher.isPipeLive(session.pid)) {
session.onPipeUp();
}
}).catch(err => log.warn('close reconcile failed: PID=%d err=%s', state.pid, this.errMsg(err)));
}
});
}
/** Detach + close the current client and reset its associated fields. */
private tearDownClient(state: HookProcessState): void {
const client = state.client;
if (client) {
client.removeAllListeners();
try { client.close(); } catch { /* ignore */ }
}
state.client = null;
state.sender = null;
state.bound = false;
state.connected = false;
state.loggedIn = false;
private ensureSession(pid: number): HookSession {
let session = this.sessions.get(pid);
if (session) return session;
session = new HookSession(pid, {
injector: this.injector,
makeClient: this.makeClient,
pipeWatcher: this.pipeWatcher,
ntqq: this.ntqq,
log: this.log,
});
session.attachProcessInfo({ name: defaultProcessName() });
session.on('login', (uin: string, sender) => {
this.bridgeManager.onHookLogin(pid, uin, sender);
});
session.on('disconnected', (wasLoggedIn: boolean) => {
if (wasLoggedIn) this.bridgeManager.onPidDisconnected(pid);
});
session.on('disposed', () => {
this.sessions.delete(pid);
});
this.sessions.set(pid, session);
return session;
}
private handleLoginState(state: HookProcessState, loginState: QqHookLoginState): void {
const wasLoggedIn = state.loggedIn;
const previousUin = state.uin;
state.uin = loginState.uin || loginState.uinNumber.toString();
state.loggedIn = loginState.loggedIn && isRealUin(state.uin);
state.status = state.loggedIn ? 'online' : (state.connected ? 'loaded' : state.status);
if (!state.loggedIn || !state.sender) return;
if (wasLoggedIn && previousUin === state.uin) return;
this.bridgeManager.onHookLogin(state.pid, state.uin, state.sender);
log.success('login detected: PID=%d UIN=%s', state.pid, state.uin);
}
private handlePacket(state: HookProcessState, packet: QqHookPacket): void {
if (!state.loggedIn) return;
const uin = packet.uin || state.uin;
if (!isRealUin(uin)) return;
this.ntqq.onHookPacket(state.pid, { ...packet, uin });
}
private toPublicInfo(state: HookProcessState): HookProcessInfo {
return {
pid: state.pid,
name: state.name,
path: state.path,
injected: state.injected,
connected: state.connected,
loggedIn: state.loggedIn,
uin: state.uin,
status: state.status,
error: state.error,
method: state.method,
};
}
private errMsg(value: unknown): string {
return value instanceof Error ? value.message : String(value);
private assertValidPid(pid: number): void {
if (!Number.isInteger(pid) || pid <= 0) throw new Error('invalid pid');
}
}
function isRealUin(uin: string): boolean {
if (!uin || uin === '0') return false;
return /^\d+$/.test(uin) && uin.length >= 5;
function defaultProcessName(): string {
return process.platform === 'win32' ? 'QQ.exe' : 'qq';
}
function errMsg(value: unknown): string {
return value instanceof Error ? value.message : String(value);
}