Files
SnowLuma/packages/core/src/index.ts
T
DrSmoothl 4fc52578a5 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.
2026-05-12 14:43:30 +08:00

48 lines
1.4 KiB
TypeScript

import { NtqqHandler } from './protocol/ntqq-handler';
import { BridgeManager } from './bridge/manager';
import { OneBotManager } from './onebot/manager';
import { loadRuntimeConfig } from './common/runtime';
import { createLogger } from './utils/logger';
import { HookManager } from './hook/hook-manager';
const runtimeConfig = loadRuntimeConfig();
const log = createLogger('App');
async function main() {
log.info('SnowLuma starting');
const ntqq = new NtqqHandler();
const bridgeManager = new BridgeManager();
const oneBotManager = new OneBotManager();
const hookManager = new HookManager({ ntqq, bridgeManager });
// Bind bridge manager to NTQQ handler (receives all parsed packets)
bridgeManager.bind(ntqq);
oneBotManager.bind(bridgeManager);
if (
(typeof __BUILD_WEBUI__ !== 'undefined' && __BUILD_WEBUI__) ||
process.env.SNOWLUMA_DEV_WEBUI === '1'
) {
try {
const { initWebUI } = await import('./webui/server');
await initWebUI(runtimeConfig.webuiPort || 5099, oneBotManager, hookManager);
} catch (err) {
log.error('Failed to start WebUI: ', err);
}
}
// Graceful shutdown
process.on('SIGINT', () => {
log.warn('Shutting down...');
oneBotManager.dispose();
hookManager.dispose();
process.exit(0);
});
}
main().catch((error) => {
log.error(error instanceof Error ? (error.stack ?? error.message) : String(error));
process.exit(1);
});