From 144e0fdd593b4db613780b48de8459a7ea79ea79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 9 May 2026 16:30:06 +0800 Subject: [PATCH] Add BridgeEventBus and network manager Introduce a typed per-kind BridgeEventBus and migrate away from the legacy single-callback firehose. Bridge now exposes `events` and emits via the bus; BridgeManager/OneBotManager no longer wire a global event callback. Add a focused onebot event pipeline (event-pipeline.ts) that subscribes per-kind handlers on instance construction. Replace the in-memory MediaCache with a persistent MediaStore and update all lookups accordingly. Replace the old Http/Ws/HttpPost transport implementations with a unified OneBotNetworkManager and pluggable adapters (server/client/post), and update OneBotInstance to install and manage adapters from config. Remove several legacy files (http-transport, http-post-transport, ws-transport, media-cache) and update imports across the codebase. Also add tests for the bridge event bus, event pipeline, network manager, and rename media tests to reflect MediaStore usage. The bus dispatches handlers in parallel and reports handler errors via an onError hook to avoid one faulty listener blocking others. --- packages/core/src/bridge/bridge.ts | 22 +- packages/core/src/bridge/event-bus.ts | 130 ++++++ packages/core/src/bridge/manager.ts | 12 +- packages/core/src/onebot/actions/extended.ts | 10 +- packages/core/src/onebot/event-pipeline.ts | 113 +++++ .../core/src/onebot/http-post-transport.ts | 230 ---------- packages/core/src/onebot/http-transport.ts | 232 ---------- packages/core/src/onebot/instance-context.ts | 69 ++- packages/core/src/onebot/instance.ts | 228 +++++----- packages/core/src/onebot/manager.ts | 12 +- packages/core/src/onebot/media-cache.ts | 143 ------ packages/core/src/onebot/media-store.ts | 267 +++++++++++ packages/core/src/onebot/network/adapter.ts | 65 +++ .../src/onebot/network/http-post-adapter.ts | 136 ++++++ .../src/onebot/network/http-server-adapter.ts | 203 +++++++++ packages/core/src/onebot/network/index.ts | 9 + .../src/onebot/network/network-manager.ts | 105 +++++ .../src/onebot/network/quick-operation.ts | 89 ++++ packages/core/src/onebot/network/utils.ts | 59 +++ .../src/onebot/network/ws-client-adapter.ts | 177 ++++++++ .../src/onebot/network/ws-server-adapter.ts | 187 ++++++++ packages/core/src/onebot/types.ts | 2 +- packages/core/src/onebot/ws-transport.ts | 424 ------------------ packages/core/tests/bridge-event-bus.test.ts | 135 ++++++ packages/core/tests/event-pipeline.test.ts | 193 ++++++++ ...edia-cache.test.ts => media-store.test.ts} | 159 +++++-- packages/core/tests/network-manager.test.ts | 180 ++++++++ 27 files changed, 2357 insertions(+), 1234 deletions(-) create mode 100644 packages/core/src/bridge/event-bus.ts create mode 100644 packages/core/src/onebot/event-pipeline.ts delete mode 100644 packages/core/src/onebot/http-post-transport.ts delete mode 100644 packages/core/src/onebot/http-transport.ts delete mode 100644 packages/core/src/onebot/media-cache.ts create mode 100644 packages/core/src/onebot/media-store.ts create mode 100644 packages/core/src/onebot/network/adapter.ts create mode 100644 packages/core/src/onebot/network/http-post-adapter.ts create mode 100644 packages/core/src/onebot/network/http-server-adapter.ts create mode 100644 packages/core/src/onebot/network/index.ts create mode 100644 packages/core/src/onebot/network/network-manager.ts create mode 100644 packages/core/src/onebot/network/quick-operation.ts create mode 100644 packages/core/src/onebot/network/utils.ts create mode 100644 packages/core/src/onebot/network/ws-client-adapter.ts create mode 100644 packages/core/src/onebot/network/ws-server-adapter.ts delete mode 100644 packages/core/src/onebot/ws-transport.ts create mode 100644 packages/core/tests/bridge-event-bus.test.ts create mode 100644 packages/core/tests/event-pipeline.test.ts rename packages/core/tests/{media-cache.test.ts => media-store.test.ts} (58%) create mode 100644 packages/core/tests/network-manager.test.ts diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index 7da6d88e..9d561d8c 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -81,8 +81,7 @@ import { } from './web-actions'; import type { GroupFilesResult } from './bridge-actions'; import type { MediaIndexNode } from './bridge-actions'; - -export type BridgeEventCallback = (event: QQEventVariant) => void; +import { BridgeEventBus } from './event-bus'; type CmdParser = (pkt: PacketInfo, qqInfo: QQInfo) => QQEventVariant[]; @@ -116,7 +115,12 @@ export class Bridge { private qqInfo_: QQInfo; private pids_ = new Set(); - private eventCallback_: BridgeEventCallback | null = null; + /** + * Per-kind event subscription. Replaces the legacy single-callback + * firehose: downstream consumers now register exactly the kinds they + * care about and `emitEvent` fans out in parallel. + */ + readonly events = new BridgeEventBus(); private cmdHandlers_ = new Map(); private packetClient_: PacketSender | null = null; private memberRefreshTasks_ = new Map>(); @@ -155,10 +159,6 @@ export class Bridge { this.cmdHandlers_.set(cmd, arr); } - setEventCallback(cb: BridgeEventCallback): void { - this.eventCallback_ = cb; - } - handlesCmd(cmd: string): boolean { return this.cmdHandlers_.has(cmd); } @@ -199,11 +199,9 @@ export class Bridge { } private emitEvent(event: QQEventVariant): void { - if (this.eventCallback_) { - try { this.eventCallback_(event); } catch (e) { - log.error('event callback error: %s', e instanceof Error ? (e.stack ?? e.message) : String(e)); - } - } + // Fire-and-forget: errors inside subscribers are surfaced via the bus's + // own onError hook so one bad listener never blocks the others. + void this.events.emit(event); } private triggerMemberCacheRefresh(event: QQEventVariant): void { diff --git a/packages/core/src/bridge/event-bus.ts b/packages/core/src/bridge/event-bus.ts new file mode 100644 index 00000000..47ee0ff5 --- /dev/null +++ b/packages/core/src/bridge/event-bus.ts @@ -0,0 +1,130 @@ +// Typed, per-kind pub/sub for bridge-level events. +// +// Replaces the single-callback firehose where every consumer received every +// event variant and had to switch on `event.kind`. Now each downstream +// concern (OneBot dispatch, member cache refresh, etc.) registers exactly +// the kinds it cares about, much closer to NapCat's per-listener pattern. +// +// `emit` is fire-and-forget: subscribers can be sync or async; async +// rejections are surfaced via `Promise.allSettled` and reported through the +// optional `onError` hook so one buggy handler never starves the others. + +import type { QQEventVariant } from './events'; +import { createLogger } from '../utils/logger'; + +type EventKind = QQEventVariant['kind']; +type EventOf = Extract; + +export type EventHandler = (event: EventOf) => void | Promise; +export type AnyEventHandler = (event: QQEventVariant) => void | Promise; + +export interface EventBusOptions { + /** Logger label for handler failures. Defaults to `Bridge.Events`. */ + loggerName?: string; + /** Override the default error reporter (e.g. for tests). */ + onError?: (kind: EventKind | '*', err: unknown) => void; +} + +const DEFAULT_LOGGER = createLogger('Bridge.Events'); + +export class BridgeEventBus { + // Per-kind subscribers are stored as `Set` so the container + // doesn't carry a contravariant function type that fights TS variance + // rules. The runtime invariant is straightforward: handlers stored under + // key `K` only ever see `EventOf` events, which is what the single + // narrowing cast at the dispatch site below relies on. + private readonly handlers = new Map>(); + private readonly anyHandlers = new Set(); + private readonly onError: (kind: EventKind | '*', err: unknown) => void; + + constructor(opts: EventBusOptions = {}) { + if (opts.onError) { + this.onError = opts.onError; + } else { + const log = opts.loggerName ? createLogger(opts.loggerName) : DEFAULT_LOGGER; + this.onError = (kind, err) => { + log.error( + 'event handler error [%s]: %s', + kind, + err instanceof Error ? (err.stack ?? err.message) : String(err), + ); + }; + } + } + + /** Subscribe to one kind. Returns an unsubscribe function. */ + on(kind: K, handler: EventHandler): () => void { + let set = this.handlers.get(kind); + if (!set) { + set = new Set(); + this.handlers.set(kind, set); + } + set.add(handler); + return () => { + this.handlers.get(kind)?.delete(handler); + }; + } + + /** Subscribe to every kind (low-volume callers like loggers). */ + onAny(handler: AnyEventHandler): () => void { + this.anyHandlers.add(handler); + return () => { + this.anyHandlers.delete(handler); + }; + } + + off(kind: K, handler: EventHandler): void { + this.handlers.get(kind)?.delete(handler); + } + + has(kind: EventKind): boolean { + return (this.handlers.get(kind)?.size ?? 0) + this.anyHandlers.size > 0; + } + + /** Drop every subscriber. Mostly useful for tests. */ + clear(): void { + this.handlers.clear(); + this.anyHandlers.clear(); + } + + /** + * Dispatch an event in parallel to every subscriber for its kind plus every + * onAny handler. Rejections are isolated per-handler — one bad subscriber + * never blocks the others, and the returned promise resolves once every + * subscriber settles. + */ + async emit(event: EventOf): Promise { + const kindHandlers = this.handlers.get(event.kind); + if ((!kindHandlers || kindHandlers.size === 0) && this.anyHandlers.size === 0) { + return; + } + const tasks: Promise[] = []; + if (kindHandlers) { + for (const handler of kindHandlers) { + // Single narrowing cast — the only place we re-assert that handlers + // stored under `event.kind` accept `EventOf` events. + const typed = handler as EventHandler; + tasks.push(invoke(this.onError, event.kind, () => typed(event))); + } + } + for (const handler of this.anyHandlers) { + tasks.push(invoke(this.onError, '*', () => handler(event))); + } + await Promise.allSettled(tasks); + } +} + +async function invoke( + onError: (kind: EventKind | '*', err: unknown) => void, + kind: EventKind | '*', + fn: () => unknown, +): Promise { + try { + const ret = fn(); + if (ret && typeof (ret as Promise).then === 'function') { + await ret; + } + } catch (err) { + onError(kind, err); + } +} diff --git a/packages/core/src/bridge/manager.ts b/packages/core/src/bridge/manager.ts index 5bfebf6a..42a00c00 100644 --- a/packages/core/src/bridge/manager.ts +++ b/packages/core/src/bridge/manager.ts @@ -4,7 +4,6 @@ import type { NtqqHandler } from '../protocol/ntqq-handler'; import type { PacketInfo } from '../protocol/types'; -import type { QQEventVariant } from './events'; import { Bridge } from './bridge'; import { QQInfo } from './qq-info'; import type { PacketSender } from '../protocol/packet-sender'; @@ -12,7 +11,6 @@ import { createLogger } from '../utils/logger'; export type SessionStartedCallback = (uin: string, qqInfo: QQInfo, bridge: Bridge) => void; export type SessionClosedCallback = (uin: string) => void; -export type BridgeEventCallback = (uin: string, event: QQEventVariant) => void; interface QQSession { qqInfo: QQInfo; @@ -28,11 +26,9 @@ export class BridgeManager { private onSessionStarted_: SessionStartedCallback | null = null; private onSessionClosed_: SessionClosedCallback | null = null; - private onBridgeEvent_: BridgeEventCallback | null = null; setSessionStartedCallback(cb: SessionStartedCallback): void { this.onSessionStarted_ = cb; } setSessionClosedCallback(cb: SessionClosedCallback): void { this.onSessionClosed_ = cb; } - setEventCallback(cb: BridgeEventCallback): void { this.onBridgeEvent_ = cb; } bind(ntqq: NtqqHandler): void { ntqq.registerCmdAll((pkt: PacketInfo) => this.onPacket(pkt)); @@ -115,11 +111,9 @@ export class BridgeManager { session = { qqInfo, bridge }; this.sessions_.set(uin, session); - // Wire event callback - bridge.setEventCallback((event) => { - if (this.onBridgeEvent_) this.onBridgeEvent_(uin, event); - }); - + // Each downstream consumer (e.g. OneBotInstance) subscribes to + // `bridge.events` directly via the per-kind bus — there is no longer a + // generic firehose to wire here. return { session, created: true }; } diff --git a/packages/core/src/onebot/actions/extended.ts b/packages/core/src/onebot/actions/extended.ts index 9aff4c3c..6cfce125 100644 --- a/packages/core/src/onebot/actions/extended.ts +++ b/packages/core/src/onebot/actions/extended.ts @@ -396,9 +396,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { if (!file) return failedResponse(RETCODE.BAD_REQUEST, 'file is required'); if (!ctx.getImageInfo) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented'); const info = await ctx.getImageInfo(file); - if (info) { - return okResponse(info as unknown as import('../types').JsonValue); - } + if (info) return okResponse(info); return failedResponse(RETCODE.ACTION_FAILED, 'image not found in cache'); }); @@ -407,9 +405,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { if (!file) return failedResponse(RETCODE.BAD_REQUEST, 'file is required'); if (!ctx.getRecordInfo) return failedResponse(RETCODE.ACTION_FAILED, 'not implemented'); const info = await ctx.getRecordInfo(file); - if (info) { - return okResponse(info as unknown as import('../types').JsonValue); - } + if (info) return okResponse(info); return failedResponse(RETCODE.ACTION_FAILED, 'record not found in cache'); }); @@ -465,7 +461,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { const context = params.context as import('../types').JsonObject | undefined; const operation = params.operation as Record | undefined; if (!context || !operation) return failedResponse(RETCODE.BAD_REQUEST, 'context and operation are required'); - const { executeQuickOperation } = await import('../http-post-transport'); + const { executeQuickOperation } = await import('../network/quick-operation'); await executeQuickOperation(context, operation, h); return okResponse(); }); diff --git a/packages/core/src/onebot/event-pipeline.ts b/packages/core/src/onebot/event-pipeline.ts new file mode 100644 index 00000000..75205a97 --- /dev/null +++ b/packages/core/src/onebot/event-pipeline.ts @@ -0,0 +1,113 @@ +// Per-kind bridge → OneBot event handlers. +// +// Replaces the legacy single-firehose `OneBotInstance.onBridgeEvent` switch. +// Every bridge event kind gets a small async handler bound to a shared +// `OneBotInstanceContext`; subscribers register on `ctx.bridge.events` so +// multiple unrelated downstream concerns (meta caching, OB11 conversion, +// dispatch) compose cleanly without one giant switch statement. + +import type { OneBotInstanceContext } from './instance-context'; +import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from './message-id'; +import type { QQEventVariant } from '../bridge/events'; + +/** + * Subscribe every `OneBotInstanceContext`-aware handler onto the bridge + * event bus. Returns a disposer that removes every subscription registered + * here in one shot — useful for tests and for tearing down a session. + */ +export function registerEventPipeline(ctx: OneBotInstanceContext): () => void { + const disposers: Array<() => void> = []; + + // Every kind that maps onto an OB11 message event also seeds the + // message-meta cache; we do that synchronously before async conversion so + // reply / quote lookups for that message id work even if the converter + // takes a beat to finish (e.g. while resolving image URLs). + disposers.push( + ctx.bridge.events.on('group_message', async (event) => { + cacheGroupMessageMeta(ctx, event); + await convertAndDispatch(ctx, event); + }), + ); + disposers.push( + ctx.bridge.events.on('friend_message', async (event) => { + cachePrivateMessageMeta(ctx, event.senderUin, event.msgSeq, event.time, event.msgId); + await convertAndDispatch(ctx, event); + }), + ); + disposers.push( + ctx.bridge.events.on('temp_message', async (event) => { + cachePrivateMessageMeta(ctx, event.senderUin, event.msgSeq, event.time, 0); + await convertAndDispatch(ctx, event); + }), + ); + + // Notice / request / non-message events: no meta caching needed, just + // convert and dispatch. + for (const kind of NOTICE_KINDS) { + disposers.push( + ctx.bridge.events.on(kind, async (event) => { + await convertAndDispatch(ctx, event); + }), + ); + } + + return () => { + for (const dispose of disposers) { + try { dispose(); } catch { /* ignore */ } + } + }; +} + +const NOTICE_KINDS = [ + 'group_member_join', + 'group_member_leave', + 'group_mute', + 'group_admin', + 'friend_recall', + 'group_recall', + 'friend_request', + 'group_invite', + 'friend_poke', + 'group_poke', + 'group_essence', + 'group_file_upload', + 'friend_add', +] as const satisfies readonly QQEventVariant['kind'][]; + +async function convertAndDispatch(ctx: OneBotInstanceContext, event: QQEventVariant): Promise { + const converted = await ctx.eventConverter.convert(ctx.uin, event); + if (!converted) return; + ctx.dispatchEvent(converted); +} + +function cacheGroupMessageMeta(ctx: OneBotInstanceContext, event: Extract): void { + const messageId = hashMessageIdInt32(event.msgSeq, event.groupId, GROUP_MESSAGE_EVENT); + ctx.cacheMessageMeta(messageId, { + isGroup: true, + targetId: event.groupId, + sequence: event.msgSeq, + eventName: GROUP_MESSAGE_EVENT, + clientSequence: 0, + random: event.msgId, + timestamp: event.time, + }); +} + +function cachePrivateMessageMeta( + ctx: OneBotInstanceContext, + senderUin: number, + msgSeq: number, + timestamp: number, + random: number, +): void { + const messageId = hashMessageIdInt32(msgSeq, senderUin, PRIVATE_MESSAGE_EVENT); + ctx.cacheMessageMeta(messageId, { + isGroup: false, + targetId: senderUin, + sequence: msgSeq, + eventName: PRIVATE_MESSAGE_EVENT, + clientSequence: 0, + random, + timestamp, + }); +} diff --git a/packages/core/src/onebot/http-post-transport.ts b/packages/core/src/onebot/http-post-transport.ts deleted file mode 100644 index 82431cc2..00000000 --- a/packages/core/src/onebot/http-post-transport.ts +++ /dev/null @@ -1,230 +0,0 @@ -import type { HttpClientNetwork, JsonObject, OneBotConfig } from './types'; -import { createLogger } from '../utils/logger'; -import { - buildDispatchPayload, - pickDispatchJson, - resolveReportOptions, - type DispatchPayload, - type EventReportOptions, -} from './event-filter'; - -export interface HttpPostTransportContext { - uin: string; - api: import('./api-handler').ApiHandler; -} - -const log = createLogger('OneBot.POST'); -const DEFAULT_TIMEOUT_MS = 5000; - -interface ResolvedClient { - network: HttpClientNetwork; - options: EventReportOptions; -} - -export class HttpPostTransport { - private readonly context: HttpPostTransportContext; - private clients = new Map(); - private stopped = false; - - constructor(config: OneBotConfig, context: HttpPostTransportContext) { - this.clients = resolveClients(config); - this.context = context; - } - - start(): void { - this.stopped = false; - if (this.clients.size > 0) { - log.info('configured %d HTTP POST adapter(s): %s', this.clients.size, [...this.clients.keys()].join(', ')); - } - } - - reloadConfig(config: OneBotConfig): void { - this.clients = resolveClients(config); - log.info('reloaded %d HTTP POST adapter(s)', this.clients.size); - } - - stop(): void { - this.stopped = true; - } - - /** - * Forward one canonical event to every active HTTP push client. - * - * The instance pre-builds the {@link DispatchPayload} so this method only - * picks the right pre-serialized variant per client (zero extra - * `JSON.stringify` calls). The raw `event` is still kept around because the - * quick-operation handler reads structured fields from it. - */ - publishEvent(event: JsonObject, payload?: DispatchPayload): void { - if (this.stopped || this.clients.size === 0) return; - - const dispatch = payload ?? buildDispatchPayload(event); - for (const client of this.clients.values()) { - const json = pickDispatchJson(dispatch, client.options); - if (json === null) continue; - void this.postEvent(client.network, json, event); - } - } - - private async postEvent(network: HttpClientNetwork, payload: string, event: JsonObject): Promise { - if (this.stopped) return; - - const headers: Record = { - 'Content-Type': 'application/json', - 'User-Agent': 'OneBot', - 'X-Self-ID': this.context.uin, - }; - - if (network.accessToken) { - headers['X-Signature'] = await computeHmacSha1(network.accessToken, payload); - } - - const timeoutMs = network.timeoutMs ?? DEFAULT_TIMEOUT_MS; - - try { - const response = await fetch(network.url, { - method: 'POST', - headers, - body: payload, - signal: AbortSignal.timeout(timeoutMs), - }); - - // OneBot v11: if the response has a JSON body, it's a quick operation - if (response.ok) { - const contentType = response.headers.get('content-type') ?? ''; - if (contentType.includes('application/json')) { - const body = await response.text(); - if (body.trim()) { - await this.handleQuickOperation(event, body); - } - } - } else { - log.warn('[%s] POST %s returned %d', network.name, network.url, response.status); - } - } catch (error) { - if (!this.stopped) { - log.warn('[%s] POST %s failed: %s', network.name, network.url, error instanceof Error ? error.message : String(error)); - } - } - } - - private async handleQuickOperation(event: JsonObject, responseBody: string): Promise { - try { - const operation = JSON.parse(responseBody) as Record; - if (!operation || typeof operation !== 'object') return; - - await executeQuickOperation(event, operation, this.context.api); - } catch (error) { - log.warn('quick operation failed: %s', error instanceof Error ? error.message : String(error)); - } - } -} - -function resolveClients(config: OneBotConfig): Map { - const out = new Map(); - for (const network of config.networks.httpClients) { - if (network.enabled === false) continue; - if (!network.url) continue; - out.set(network.name, { - network, - options: resolveReportOptions(network), - }); - } - return out; -} - -async function computeHmacSha1(secret: string, payload: string): Promise { - const { createHmac } = await import('crypto'); - return 'sha1=' + createHmac('sha1', secret).update(payload).digest('hex'); -} - -/** - * Execute a quick operation based on the event type and the response body. - * This implements the .handle_quick_operation hidden API behavior. - * See: https://github.com/botuniverse/onebot-11/blob/master/api/hidden.md - */ -export async function executeQuickOperation( - event: JsonObject, - operation: Record, - api: import('./api-handler').ApiHandler, -): Promise { - const postType = event.post_type as string; - - if (postType === 'message') { - // Quick reply - if (operation.reply !== undefined && operation.reply !== null && operation.reply !== '') { - const messageType = event.message_type as string; - const autoEscape = !!operation.auto_escape; - - if (messageType === 'group') { - const params: JsonObject = { - group_id: event.group_id as number, - message: operation.reply as import('./types').JsonValue, - auto_escape: autoEscape, - }; - // at_sender defaults to true for group messages - if (operation.at_sender !== false && event.user_id) { - // Prepend an @ segment - const atSegment = { type: 'at', data: { qq: String(event.user_id) } }; - if (typeof operation.reply === 'string') { - params.message = [atSegment, { type: 'text', data: { text: operation.reply as string } }] as import('./types').JsonValue; - params.auto_escape = false; - } else if (Array.isArray(operation.reply)) { - params.message = [atSegment, ...(operation.reply as unknown[])] as import('./types').JsonValue; - } - } - await api.handle('send_group_msg', params); - } else if (messageType === 'private') { - await api.handle('send_private_msg', { - user_id: event.user_id as number, - message: operation.reply as import('./types').JsonValue, - auto_escape: autoEscape, - }); - } - } - - // Quick delete - if (operation.delete) { - await api.handle('delete_msg', { message_id: event.message_id as number }); - } - - // Quick ban (group only) - if (operation.ban && event.message_type === 'group') { - const duration = typeof operation.ban_duration === 'number' ? operation.ban_duration : 1800; - await api.handle('set_group_ban', { - group_id: event.group_id as number, - user_id: event.user_id as number, - duration: duration as import('./types').JsonValue, - }); - } - - // Quick kick (group only) - if (operation.kick && event.message_type === 'group') { - await api.handle('set_group_kick', { - group_id: event.group_id as number, - user_id: event.user_id as number, - reject_add_request: !!operation.reject_add_request as unknown as import('./types').JsonValue, - }); - } - } - - if (postType === 'request') { - if (operation.approve !== undefined) { - const requestType = event.request_type as string; - if (requestType === 'friend') { - await api.handle('set_friend_add_request', { - flag: event.flag as string, - approve: operation.approve as import('./types').JsonValue, - remark: (operation.remark ?? '') as import('./types').JsonValue, - }); - } else if (requestType === 'group') { - await api.handle('set_group_add_request', { - flag: event.flag as string, - sub_type: event.sub_type as string, - approve: operation.approve as import('./types').JsonValue, - reason: (operation.reason ?? '') as import('./types').JsonValue, - }); - } - } - } -} diff --git a/packages/core/src/onebot/http-transport.ts b/packages/core/src/onebot/http-transport.ts deleted file mode 100644 index dc68bafb..00000000 --- a/packages/core/src/onebot/http-transport.ts +++ /dev/null @@ -1,232 +0,0 @@ -import { - createServer, - type IncomingMessage, - type Server, - type ServerResponse, -} from 'http'; -import type { ApiHandler } from './api-handler'; -import type { HttpServerNetwork, OneBotConfig } from './types'; -import { createLogger } from '../utils/logger'; - -export interface HttpTransportContext { - api: ApiHandler; -} - -const log = createLogger('OneBot.HTTP'); - -interface RunningServer { - server: Server; - signature: string; - network: HttpServerNetwork; -} - -export class HttpTransport { - private readonly context: HttpTransportContext; - private readonly servers = new Map(); - private networks: HttpServerNetwork[] = []; - - constructor(config: OneBotConfig, context: HttpTransportContext) { - this.networks = activeNetworks(config); - this.context = context; - } - - start(): void { - for (const network of this.networks) this.startEndpoint(network); - } - - reloadConfig(config: OneBotConfig): void { - const nextNetworks = activeNetworks(config); - const nextByName = new Map(nextNetworks.map((n) => [n.name, n] as const)); - - // Stop adapters that no longer exist or whose binding/auth changed. - for (const [name, running] of [...this.servers]) { - const next = nextByName.get(name); - if (!next || httpServerSignature(next) !== running.signature) { - running.server.close(); - this.servers.delete(name); - log.info('stopped [%s]', name); - } - } - - // (Re)start adapters that aren't running yet. - for (const network of nextNetworks) { - if (!this.servers.has(network.name)) { - this.startEndpoint(network); - } - } - - this.networks = nextNetworks; - } - - stop(): void { - for (const { server } of this.servers.values()) { - server.close(); - } - this.servers.clear(); - } - - private startEndpoint(network: HttpServerNetwork): void { - const expectedPath = normalizePath(network.path ?? '/'); - const endpointToken = network.accessToken ?? ''; - const server = createServer((req, res) => { - void this.handleRequest(expectedPath, endpointToken, req, res); - }); - - server.on('listening', () => { - log.success('[%s] listening %s:%d%s', network.name, network.host ?? '0.0.0.0', network.port, expectedPath); - }); - - server.on('error', (error) => { - log.warn('[%s] server error: %s', network.name, error instanceof Error ? error.message : String(error)); - }); - - server.listen(network.port, network.host ?? '0.0.0.0'); - this.servers.set(network.name, { server, signature: httpServerSignature(network), network }); - } - - private async handleRequest(expectedPath: string, accessToken: string, req: IncomingMessage, res: ServerResponse): Promise { - const parsedUrl = new URL(req.url ?? '/', 'http://127.0.0.1'); - const incomingPath = parsedUrl.pathname; - - // Determine the expected prefix (e.g., "/" or "/api/") - const ep = expectedPath.endsWith('/') ? expectedPath : expectedPath + '/'; - let action = ''; - - if (incomingPath === expectedPath || incomingPath === expectedPath + '/') { - action = ''; // No action in URL (legacy body mode) - } else if (incomingPath.startsWith(ep)) { - action = incomingPath.substring(ep.length); // Extract action from URL - } else { - writeJson(res, 404, { status: 'failed', retcode: 1404, data: null, wording: 'not found' }); - return; - } - - if (!isAuthorized(req, accessToken)) { - writeJson(res, 401, { status: 'failed', retcode: 1401, data: null, wording: 'unauthorized' }); - return; - } - - if (req.method === 'GET' && !action) { - writeJson(res, 200, { status: 'ok', retcode: 0, data: { online: true } }); - return; - } - - try { - let params: Record = {}; - let echo: unknown; - - if (req.method === 'GET') { - parsedUrl.searchParams.forEach((value, key) => { - try { - params[key] = JSON.parse(value); - } catch { - params[key] = value; - } - }); - } else if (req.method === 'POST') { - const bodyContent = await readRequestBody(req); - if (bodyContent.trim()) { - try { - const parsedBody = JSON.parse(bodyContent); - if (typeof parsedBody === 'object' && parsedBody !== null && !Array.isArray(parsedBody)) { - // Legacy format: { action: "...", params: {...} } vs standard raw params - if (parsedBody.action && !action) { - action = String(parsedBody.action); - } - if (parsedBody.params && typeof parsedBody.params === 'object' && !Array.isArray(parsedBody.params)) { - params = parsedBody.params as Record; - } else { - params = parsedBody as Record; - } - echo = parsedBody.echo; - } - } catch { - writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: 'bad request: invalid json' }); - return; - } - } - } else { - writeJson(res, 405, { status: 'failed', retcode: 1400, data: null, wording: 'method not allowed' }); - return; - } - - if (!action) { - writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: 'bad request: missing action' }); - return; - } - - // Handle the API call - const response = await this.context.api.handle(action, params as import('./types').JsonObject); - - if (echo !== undefined) { - response.echo = echo as import('./types').JsonValue; - } - - writeJson(res, 200, response); - } catch (error) { - const wording = error instanceof Error ? error.message : 'internal error'; - writeJson(res, 500, { status: 'failed', retcode: 1200, data: null, wording }); - } - } -} - -function normalizePath(pathValue: string): string { - const path = pathValue.trim() || '/'; - if (path === '/') return '/'; - return path.endsWith('/') ? path.slice(0, -1) : path; -} - -function httpServerSignature(network: HttpServerNetwork): string { - return `${network.host ?? '0.0.0.0'}:${network.port}${normalizePath(network.path ?? '/')}#${network.accessToken ?? ''}`; -} - -function activeNetworks(config: OneBotConfig): HttpServerNetwork[] { - return config.networks.httpServers.filter((n) => n.enabled !== false); -} - -function isAuthorized(request: IncomingMessage, token: string): boolean { - if (!token) return true; - // 1. Header auth - const rawAuth = request.headers.authorization; - const auth = Array.isArray(rawAuth) ? rawAuth[0] : rawAuth ?? ''; - if (auth === `Bearer ${token}`) return true; - - // 2. Query string auth - try { - const url = new URL(request.url ?? '/', 'http://127.0.0.1'); - if (url.searchParams.get('access_token') === token) { - return true; - } - } catch {} - - return false; -} - -function readRequestBody(req: IncomingMessage, maxBytes = 2 * 1024 * 1024): Promise { - return new Promise((resolve, reject) => { - const chunks: Buffer[] = []; - let total = 0; - - req.on('data', (chunk: Buffer) => { - total += chunk.length; - if (total > maxBytes) { - reject(new Error('request body too large')); - req.destroy(); - return; - } - chunks.push(chunk); - }); - - req.on('end', () => { - resolve(Buffer.concat(chunks).toString('utf8')); - }); - - req.on('error', reject); - }); -} - -function writeJson(res: ServerResponse, statusCode: number, data: unknown): void { - res.statusCode = statusCode; - res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.end(JSON.stringify(data)); -} diff --git a/packages/core/src/onebot/instance-context.ts b/packages/core/src/onebot/instance-context.ts index d50b6ad9..92d3ef0b 100644 --- a/packages/core/src/onebot/instance-context.ts +++ b/packages/core/src/onebot/instance-context.ts @@ -2,29 +2,56 @@ import type { Bridge } from '../bridge/bridge'; import type { ForwardNodePayload, MessageElement } from '../bridge/events'; import type { QQInfo } from '../bridge/qq-info'; import type { ApiActionContext, MessageSendResult } from './api-handler'; +import type { EventConverter } from './event-converter'; import { elementsToOneBotSegments } from './event-converter'; import { MessageStore } from './message-store'; import { parseMessage } from './message-parser'; import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from './message-id'; -import type { MediaCache } from './media-cache'; -import type { JsonObject, JsonValue, MessageMeta } from './types'; +import type { MediaStore } from './media-store'; +import type { JsonObject, JsonValue, MessageMeta, OneBotConfig } from './types'; import { createLogger } from '../utils/logger'; import {WebHonorType} from "@/bridge/web/group-honor"; const log = createLogger('OneBot'); -export interface InstanceRef { +/** + * Single shared context bag that flows through every OneBot instance-internal + * subsystem: API builder and the per-kind event pipeline. + * + * Only fields that are actually read through this bag live here — the api + * handler and network manager are owned by `OneBotInstance` directly because + * nothing reads them via ctx, and including them here would force a + * chicken-and-egg `late-bound` field dance during construction. + * + * `dispatchEvent` is the indirection used by the event pipeline to hand a + * converted OneBot event back to the instance for caching + adapter fan-out; + * it lets the pipeline stay decoupled from the network manager. + */ +export interface OneBotInstanceContext { + /** Self UIN as string (matches what's on disk and on the wire). */ uin: string; + /** Self UIN parsed once, used in event payloads. */ + selfId: number; + qqInfo: QQInfo; bridge: Bridge; + messageStore: MessageStore; - mediaCache: MediaCache; + mediaStore: MediaStore; + + eventConverter: EventConverter; + + config: OneBotConfig; musicSignUrl?: string; + + /** Persist meta about a message id; safe to call any number of times. */ cacheMessageMeta(messageId: number, meta: MessageMeta): void; + /** Hand a fully-converted OneBot event to the network manager + caches. */ + dispatchEvent(event: JsonObject): void; } -export function buildApiContext(ref: InstanceRef): ApiActionContext { - const { bridge, qqInfo, messageStore, mediaCache } = ref; +export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext { + const { bridge, qqInfo, messageStore, mediaStore } = ref; return { getLoginInfo: () => getLoginInfo(ref), @@ -138,18 +165,18 @@ export function buildApiContext(ref: InstanceRef): ApiActionContext { getCsrfToken: () => bridge.getCsrfToken(), getCredentials: (domain: string) => bridge.getCredentials(domain), // Media lookup - getImageInfo: (file) => handleGetImageInfo(mediaCache, file), - getRecordInfo: (file) => handleGetRecordInfo(bridge, mediaCache, file), + getImageInfo: (file) => handleGetImageInfo(mediaStore, file), + getRecordInfo: (file) => handleGetRecordInfo(bridge, mediaStore, file), }; } // --- Media lookup --- async function handleGetImageInfo( - mediaCache: MediaCache, + mediaStore: MediaStore, file: string, ): Promise { - const cached = mediaCache.findImage(file); + const cached = mediaStore.findImage(file); if (!cached) return null; const url = cached.url || cached.imageUrl || ''; return { @@ -162,10 +189,10 @@ async function handleGetImageInfo( async function handleGetRecordInfo( bridge: Bridge, - mediaCache: MediaCache, + mediaStore: MediaStore, file: string, ): Promise { - const cached = mediaCache.findRecord(file); + const cached = mediaStore.findRecord(file); if (!cached) return null; // Re-resolve via OIDB if the cached URL is missing or empty. @@ -177,7 +204,7 @@ async function handleGetRecordInfo( ? await bridge.fetchGroupPttUrlByNode(cached.sessionId, cached.mediaNode) : await bridge.fetchPrivatePttUrlByNode(cached.mediaNode); if (url) { - mediaCache.updateRecordUrl(file, url); + mediaStore.updateRecordUrl(file, url); } } catch (err) { log.warn('get_record url refetch failed: %s', err instanceof Error ? err.message : String(err)); @@ -194,7 +221,7 @@ async function handleGetRecordInfo( // --- Login info --- -function getLoginInfo(ref: InstanceRef): { userId: number; nickname: string } { +function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nickname: string } { const userId = parseInt(ref.uin, 10) || 0; const nickname = ref.qqInfo.nickname || ref.uin; return { userId, nickname }; @@ -550,7 +577,7 @@ function logSentMessage(isGroup: boolean, targetId: number, elements: MessageEle // --- Send message --- async function handleSendPrivate( - ref: InstanceRef, + ref: OneBotInstanceContext, userId: number, message: JsonValue, autoEscape: boolean, @@ -596,7 +623,7 @@ async function handleSendPrivate( } async function handleSendGroup( - ref: InstanceRef, + ref: OneBotInstanceContext, groupId: number, message: JsonValue, autoEscape: boolean, @@ -642,7 +669,7 @@ async function handleSendGroup( } async function handleSendGroupForward( - ref: InstanceRef, + ref: OneBotInstanceContext, groupId: number, messages: JsonValue, ): Promise<{ messageId: number; forwardId: string }> { @@ -665,7 +692,7 @@ async function handleSendGroupForward( } async function handleSendPrivateForward( - ref: InstanceRef, + ref: OneBotInstanceContext, userId: number, messages: JsonValue, ): Promise<{ messageId: number; forwardId: string }> { @@ -688,7 +715,7 @@ async function handleSendPrivateForward( } async function handleUploadForward( - ref: InstanceRef, + ref: OneBotInstanceContext, messages: JsonValue, ): Promise<{ forwardId: string }> { const nodes = await parseForwardNodes(ref, messages); @@ -697,7 +724,7 @@ async function handleUploadForward( } async function handleGetForward( - ref: InstanceRef, + ref: OneBotInstanceContext, resId: string, ): Promise { const nodes = await ref.bridge.fetchForwardNodes(resId); @@ -717,7 +744,7 @@ async function handleGetForward( return results; } -async function parseForwardNodes(ref: InstanceRef, messages: JsonValue): Promise { +async function parseForwardNodes(ref: OneBotInstanceContext, messages: JsonValue): Promise { if (!Array.isArray(messages)) { throw new Error('forward messages must be an array'); } diff --git a/packages/core/src/onebot/instance.ts b/packages/core/src/onebot/instance.ts index 6b2feb4a..1c5d0d97 100644 --- a/packages/core/src/onebot/instance.ts +++ b/packages/core/src/onebot/instance.ts @@ -1,19 +1,23 @@ import path from 'path'; -import type { QQEventVariant } from '../bridge/events'; import type { Bridge } from '../bridge/bridge'; import type { QQInfo } from '../bridge/qq-info'; import { ApiHandler } from './api-handler'; import { EventConverter } from './event-converter'; import { MessageStore } from './message-store'; -import { MediaCache } from './media-cache'; +import { MediaStore } from './media-store'; import { RKeyCache } from './instance-rkey'; -import { buildApiContext } from './instance-context'; +import { buildApiContext, type OneBotInstanceContext } from './instance-context'; +import { registerEventPipeline } from './event-pipeline'; import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from './message-id'; -import type { JsonObject, MessageMeta, OneBotConfig } from './types'; -import { HttpTransport } from './http-transport'; -import { HttpPostTransport } from './http-post-transport'; -import { WsTransport } from './ws-transport'; -import { buildDispatchPayload } from './event-filter'; +import type { JsonObject, MessageMeta, OneBotConfig, NetworkBase } from './types'; +import { + OneBotNetworkManager, + WsServerAdapter, + WsClientAdapter, + HttpServerAdapter, + HttpPostAdapter, + type NetworkAdapterContext, +} from './network'; import { createLogger } from '../utils/logger'; const log = createLogger('Event'); @@ -26,11 +30,11 @@ export class OneBotInstance { private readonly apiHandler: ApiHandler; private readonly eventConverter: EventConverter; private readonly messageStore: MessageStore; - private readonly mediaCache: MediaCache; - private readonly httpTransport: HttpTransport; - private readonly httpPostTransport: HttpPostTransport; - private readonly wsTransport: WsTransport; + private readonly mediaStore: MediaStore; + private readonly networkManager: OneBotNetworkManager; private readonly rkeyCache: RKeyCache; + private readonly ctx: OneBotInstanceContext; + private disposeEventPipeline: (() => void) | null = null; private readonly pids = new Set(); private online = true; @@ -44,7 +48,7 @@ export class OneBotInstance { this.eventConverter = new EventConverter(); this.rkeyCache = new RKeyCache(); - this.mediaCache = new MediaCache(); + this.mediaStore = new MediaStore(path.join('data', this.uin, 'media.db')); this.eventConverter.setImageUrlResolver((element, isGroup) => this.rkeyCache.resolveImageUrl(this.bridge, element, isGroup)); this.eventConverter.setMediaUrlResolver(async (element, isGroup, sessionId) => { @@ -77,7 +81,7 @@ export class OneBotInstance { const url = typeof data.url === 'string' ? data.url : ''; const file = typeof data.file === 'string' ? data.file : ''; if (mediaType === 'image') { - this.mediaCache.rememberImage({ + this.mediaStore.rememberImage({ file: file || element.fileId || '', url, fileSize: element.fileSize ?? 0, @@ -90,7 +94,7 @@ export class OneBotInstance { }); return; } - this.mediaCache.rememberRecord({ + this.mediaStore.rememberRecord({ file: file || element.fileName || element.fileId || '', fileId: element.fileId ?? '', url, @@ -107,66 +111,49 @@ export class OneBotInstance { hashMessageIdInt32(sequence, sessionId, eventName || (isGroup ? GROUP_MESSAGE_EVENT : PRIVATE_MESSAGE_EVENT))); this.messageStore = new MessageStore(path.join('data', this.uin, 'messages.json')); - const context = buildApiContext({ + // Shared instance context. Only carries fields that are actually read + // through it — api handler and network manager stay as direct fields on + // the instance because nothing reads them via ctx. + const ctx: OneBotInstanceContext = { uin: this.uin, + selfId: parseInt(this.uin, 10) || 0, qqInfo: this.qqInfo, bridge: this.bridge, messageStore: this.messageStore, - mediaCache: this.mediaCache, + mediaStore: this.mediaStore, + eventConverter: this.eventConverter, + config, musicSignUrl: config.musicSignUrl, cacheMessageMeta: (messageId, meta) => this.cacheMessageMeta(messageId, meta), - }); - this.apiHandler = new ApiHandler(context); + dispatchEvent: (event) => this.dispatchEvent(event), + }; + this.ctx = ctx; - this.httpTransport = new HttpTransport(config, { - api: this.apiHandler, - }); + this.apiHandler = new ApiHandler(buildApiContext(ctx)); + this.networkManager = new OneBotNetworkManager(); + this.installAdaptersFromConfig(config); + void this.networkManager.openAll(); - this.httpPostTransport = new HttpPostTransport(config, { - uin: this.uin, - api: this.apiHandler, - }); - - this.wsTransport = new WsTransport(config, { - uin: this.uin, - api: this.apiHandler, - buildLifecycleEvent: (subType) => this.makeLifecycleEvent(subType), - buildHeartbeatEvent: () => this.makeHeartbeatEvent(), - }); - - this.httpTransport.start(); - this.httpPostTransport.start(); - this.wsTransport.start(); this.startHeartbeat(); this.rkeyCache.warmUp(this.bridge, this.uin); + + // Per-kind subscription on the typed event bus. Each bridge event kind + // gets its own focused handler in `event-pipeline`; the firehose is gone. + this.disposeEventPipeline = registerEventPipeline(ctx); } reloadConfig(config: OneBotConfig): void { - this.httpTransport.reloadConfig(config); - this.httpPostTransport.reloadConfig(config); - this.wsTransport.reloadConfig(config); + void this.applyConfigDiff(config); } dispose(): void { this.online = false; this.stopHeartbeat(); - this.httpTransport.stop(); - this.httpPostTransport.stop(); - this.wsTransport.stop(); + this.disposeEventPipeline?.(); + this.disposeEventPipeline = null; + void this.networkManager.closeAll(); this.messageStore.close(); - } - - onBridgeEvent(event: QQEventVariant): void { - this.cacheMessageMetaFromBridgeEvent(event); - - void this.dispatchBridgeEventAsync(event); - } - - private async dispatchBridgeEventAsync(event: QQEventVariant): Promise { - const converted = await this.eventConverter.convert(this.uin, event); - if (!converted) return; - - this.dispatchEvent(converted); + this.mediaStore.close(); } addPid(pid: number): void { @@ -192,11 +179,87 @@ export class OneBotInstance { private dispatchEvent(event: JsonObject): void { this.cacheMessageEvent(event); this.logReceivedMessage(event); - // Produce one canonical event, serialize the two format variants once, - // then let each adapter pick its slot at zero allocation cost. - const payload = buildDispatchPayload(event); - this.wsTransport.publishEvent(event, payload); - this.httpPostTransport.publishEvent(event, payload); + // NetworkManager builds the dispatch payload once and fans out to every + // active adapter in parallel via Promise.allSettled. + void this.networkManager.emitEvent(event); + } + + private buildNetworkContext(): NetworkAdapterContext { + return { + uin: this.uin, + api: this.apiHandler, + buildLifecycleEvent: (subType) => this.makeLifecycleEvent(subType), + buildHeartbeatEvent: () => this.makeHeartbeatEvent(), + }; + } + + private installAdaptersFromConfig(config: OneBotConfig): void { + const ctx = this.buildNetworkContext(); + for (const net of config.networks.httpServers) { + if (net.enabled === false) continue; + this.networkManager.register(new HttpServerAdapter(net.name, net, ctx)); + } + for (const net of config.networks.httpClients) { + if (net.enabled === false || !net.url) continue; + this.networkManager.register(new HttpPostAdapter(net.name, net, ctx)); + } + for (const net of config.networks.wsServers) { + if (net.enabled === false) continue; + this.networkManager.register(new WsServerAdapter(net.name, net, ctx)); + } + for (const net of config.networks.wsClients) { + if (net.enabled === false || !net.url) continue; + this.networkManager.register(new WsClientAdapter(net.name, net, ctx)); + } + } + + private async applyConfigDiff(next: OneBotConfig): Promise { + const ctx = this.buildNetworkContext(); + const desired = new Map(); + const factories = new Map void>(); + + for (const net of next.networks.httpServers) { + desired.set(net.name, net); + factories.set(net.name, () => this.networkManager.register(new HttpServerAdapter(net.name, net, ctx))); + } + for (const net of next.networks.httpClients) { + desired.set(net.name, net); + factories.set(net.name, () => this.networkManager.register(new HttpPostAdapter(net.name, net, ctx))); + } + for (const net of next.networks.wsServers) { + desired.set(net.name, net); + factories.set(net.name, () => this.networkManager.register(new WsServerAdapter(net.name, net, ctx))); + } + for (const net of next.networks.wsClients) { + desired.set(net.name, net); + factories.set(net.name, () => this.networkManager.register(new WsClientAdapter(net.name, net, ctx))); + } + + // Close adapters whose entry has been removed entirely. + for (const adapter of this.networkManager.list()) { + if (!desired.has(adapter.name)) { + await this.networkManager.closeOne(adapter.name); + } + } + + // Reload existing adapters in place; spin up new ones the manager hasn't + // seen before. + for (const [name, net] of desired) { + const existing = this.networkManager.get(name); + if (existing) { + try { + await existing.reload(net as never); + } catch (err) { + log.warn('reload [%s] failed: %s', name, err instanceof Error ? err.message : String(err)); + } + } else if (net.enabled !== false) { + const factory = factories.get(name); + if (factory) { + factory(); + await this.networkManager.get(name)?.open(); + } + } + } } private logReceivedMessage(event: JsonObject): void { @@ -273,49 +336,6 @@ export class OneBotInstance { this.messageStore.storeEvent(messageId, isGroup, sessionId, sequence, eventName, event); } - private cacheMessageMetaFromBridgeEvent(event: QQEventVariant): void { - if (event.kind === 'group_message') { - const messageId = hashMessageIdInt32(event.msgSeq, event.groupId, GROUP_MESSAGE_EVENT); - this.cacheMessageMeta(messageId, { - isGroup: true, - targetId: event.groupId, - sequence: event.msgSeq, - eventName: GROUP_MESSAGE_EVENT, - clientSequence: 0, - random: event.msgId, - timestamp: event.time, - }); - return; - } - - if (event.kind === 'friend_message') { - const messageId = hashMessageIdInt32(event.msgSeq, event.senderUin, PRIVATE_MESSAGE_EVENT); - this.cacheMessageMeta(messageId, { - isGroup: false, - targetId: event.senderUin, - sequence: event.msgSeq, - eventName: PRIVATE_MESSAGE_EVENT, - clientSequence: 0, - random: event.msgId, - timestamp: event.time, - }); - return; - } - - if (event.kind === 'temp_message') { - const messageId = hashMessageIdInt32(event.msgSeq, event.senderUin, PRIVATE_MESSAGE_EVENT); - this.cacheMessageMeta(messageId, { - isGroup: false, - targetId: event.senderUin, - sequence: event.msgSeq, - eventName: PRIVATE_MESSAGE_EVENT, - clientSequence: 0, - random: 0, - timestamp: event.time, - }); - } - } - private cacheMessageMeta(messageId: number, meta: MessageMeta): void { if (!Number.isInteger(messageId) || messageId === 0) return; this.messageStore.storeMeta(messageId, meta); diff --git a/packages/core/src/onebot/manager.ts b/packages/core/src/onebot/manager.ts index 09d43293..6904db6e 100644 --- a/packages/core/src/onebot/manager.ts +++ b/packages/core/src/onebot/manager.ts @@ -1,4 +1,3 @@ -import type { QQEventVariant } from '../bridge/events'; import type { Bridge } from '../bridge/bridge'; import type { QQInfo } from '../bridge/qq-info'; import type { BridgeManager } from '../bridge/manager'; @@ -21,9 +20,8 @@ export class OneBotManager { this.onSessionClosed(uin); }); - bridgeManager.setEventCallback((uin, event) => { - this.onBridgeEvent(uin, event); - }); + // Per-kind subscriptions to `bridge.events` happen inside + // `OneBotInstance` itself; no global firehose to wire here. } getInstance(uin: string): OneBotInstance | null { @@ -80,12 +78,6 @@ export class OneBotManager { log.info('session closed: UIN=%s', uin); } - private onBridgeEvent(uin: string, event: QQEventVariant): void { - const instance = this.instances.get(uin); - if (!instance) return; - - instance.onBridgeEvent(event); - } } async function warmUpBridgeState(uin: string, qqInfo: QQInfo, bridge: Bridge): Promise { diff --git a/packages/core/src/onebot/media-cache.ts b/packages/core/src/onebot/media-cache.ts deleted file mode 100644 index 95a3fbbc..00000000 --- a/packages/core/src/onebot/media-cache.ts +++ /dev/null @@ -1,143 +0,0 @@ -// Bounded in-memory cache of recently seen image / record media elements. -// -// The OneBot `get_image` and `get_record` APIs take back the `data.file` that -// was emitted earlier in a message segment and expect to receive the -// corresponding URL (and, ideally, file size / name / path). SnowLuma emits -// minimal `{file, url}` segments, so we need a side-channel that records the -// full metadata (and enough context to re-resolve the URL later via OIDB, the -// same way NapCat's `getPttUrl` does). -// -// This cache is deliberately small and lives in memory only — message events -// already get persisted separately in `MessageStore`. Entries are keyed by -// every identifier we might reasonably be asked about: the OneBot `file` -// value, the raw `fileId` / `fileUuid`, and the resolved URL. When the cache -// outgrows its budget, the least-recently-inserted entries are evicted. - -import type { MessageElement } from '../bridge/events'; - -export interface CachedImage { - /** Primary `data.file` value as exposed in the OneBot segment. */ - file: string; - /** Last known resolved URL (may be empty if resolution failed). */ - url: string; - fileSize: number; - fileName: string; - subType: number; - summary: string; - /** Conversation the media originated from (needed for RKey refresh). */ - isGroup: boolean; - sessionId: number; - /** Original imageUrl from the MessageElement, used for RKey re-appending. */ - imageUrl: string; -} - -export interface CachedRecord { - /** Primary `data.file` value as exposed in the OneBot segment. */ - file: string; - /** Raw fileId from the MessageElement (typically the fileUuid). */ - fileId: string; - /** Last known resolved URL (may be empty if resolution failed). */ - url: string; - fileSize: number; - fileName: string; - duration: number; - fileHash: string; - /** OIDB media node used to re-fetch the URL via getPtt*UrlByNode. */ - mediaNode?: MessageElement['mediaNode']; - /** Conversation the media originated from (group vs private path). */ - isGroup: boolean; - sessionId: number; -} - -interface BoundedMap { - values: Map; - limit: number; -} - -function createBounded(limit: number): BoundedMap { - return { values: new Map(), limit: Math.max(32, limit) }; -} - -function putBounded(map: BoundedMap, key: string, value: V): void { - if (!key) return; - // Re-insert to push the key to the tail of the insertion order (LRU-ish). - if (map.values.has(key)) map.values.delete(key); - map.values.set(key, value); - while (map.values.size > map.limit) { - const oldest = map.values.keys().next().value as string | undefined; - if (oldest === undefined) break; - map.values.delete(oldest); - } -} - -function getBounded(map: BoundedMap, key: string): V | null { - if (!key) return null; - return map.values.get(key) ?? null; -} - -export class MediaCache { - private readonly images: BoundedMap; - private readonly records: BoundedMap; - - constructor(maxEntries = 1024) { - this.images = createBounded(maxEntries); - this.records = createBounded(maxEntries); - } - - rememberImage(info: CachedImage): void { - if (!info.file && !info.url && !info.fileName) return; - // Store under every plausible lookup key so callers can pass back any of - // {file, fileName, url} from the segment they previously received. - for (const key of dedupe([info.file, info.fileName, info.url])) { - putBounded(this.images, key, info); - } - } - - rememberRecord(info: CachedRecord): void { - if (!info.file && !info.url && !info.fileId && !info.fileName) return; - for (const key of dedupe([info.file, info.fileName, info.fileId, info.url])) { - putBounded(this.records, key, info); - } - } - - findImage(key: string): CachedImage | null { - return getBounded(this.images, key); - } - - findRecord(key: string): CachedRecord | null { - return getBounded(this.records, key); - } - - /** Replace the cached URL on an image entry (after RKey refresh, etc.). */ - updateImageUrl(key: string, url: string): void { - const cached = this.findImage(key); - if (!cached || !url || cached.url === url) return; - const next: CachedImage = { ...cached, url }; - this.rememberImage(next); - } - - /** Replace the cached URL on a record entry after an on-demand re-fetch. */ - updateRecordUrl(key: string, url: string): void { - const cached = this.findRecord(key); - if (!cached || !url || cached.url === url) return; - const next: CachedRecord = { ...cached, url }; - this.rememberRecord(next); - } - - /** Count currently cached entries (test helper). */ - get size(): { images: number; records: number } { - return { images: this.images.values.size, records: this.records.values.size }; - } -} - -function dedupe(values: (string | undefined)[]): string[] { - const out: string[] = []; - const seen = new Set(); - for (const v of values) { - if (!v) continue; - if (seen.has(v)) continue; - seen.add(v); - out.push(v); - } - return out; -} diff --git a/packages/core/src/onebot/media-store.ts b/packages/core/src/onebot/media-store.ts new file mode 100644 index 00000000..f0b8e60c --- /dev/null +++ b/packages/core/src/onebot/media-store.ts @@ -0,0 +1,267 @@ +// Persistent media metadata store. +// +// The `get_image` / `get_record` actions need to map a `data.file` (or related +// identifier) we previously emitted in a message segment back to the URL, +// size, name, and any OIDB media-node info needed to re-resolve a stale URL. +// +// Earlier this lived in an in-memory `MediaCache`, which meant the lookup +// table was lost on every restart. This implementation persists everything +// alongside the message store so OneBot consumers can always resolve the +// media that produced any segment ever stored, even after the bot restarts. +// +// Schema overview (single SQLite file per UIN, e.g. `data//media.db`): +// media_entries(type, primary_key, data JSON, last_seen) -- one row per logical media item +// media_keys(type, key, primary_key) -- secondary index for every alias +// +// `rememberImage` / `rememberRecord` upsert exactly one entry row plus one +// alias row per identifier (file / fileName / fileId / url). The alias rows +// always point to the canonical entry so URL refreshes propagate to every +// alias automatically. + +import fs from 'fs'; +import path from 'path'; +import { DatabaseSync, type StatementSync } from 'node:sqlite'; + +import type { MessageElement } from '../bridge/events'; + +export interface CachedImage { + /** Primary `data.file` value as exposed in the OneBot segment. */ + file: string; + /** Last known resolved URL (may be empty if resolution failed). */ + url: string; + fileSize: number; + fileName: string; + subType: number; + summary: string; + /** Conversation the media originated from. */ + isGroup: boolean; + sessionId: number; + /** Original imageUrl from the MessageElement, used for RKey re-appending. */ + imageUrl: string; +} + +export interface CachedRecord { + /** Primary `data.file` value as exposed in the OneBot segment. */ + file: string; + /** Raw fileId from the MessageElement (typically the fileUuid). */ + fileId: string; + /** Last known resolved URL (may be empty if resolution failed). */ + url: string; + fileSize: number; + fileName: string; + duration: number; + fileHash: string; + /** OIDB media node used to re-fetch the URL via getPtt*UrlByNode. */ + mediaNode?: MessageElement['mediaNode']; + /** Conversation the media originated from (group vs private path). */ + isGroup: boolean; + sessionId: number; +} + +const TYPE_IMAGE = 'image'; +const TYPE_RECORD = 'record'; +const DEFAULT_KEEP_ENTRIES = 4096; + +export class MediaStore { + private readonly db: DatabaseSync; + private readonly maxEntriesPerType: number; + + // Prepared statements (pay the parsing cost once at startup). + private readonly upsertEntry: StatementSync; + private readonly upsertKey: StatementSync; + private readonly findEntryByKey: StatementSync; + private readonly findEntryByPrimary: StatementSync; + private readonly evictByType: StatementSync; + private readonly purgeOrphanKeys: StatementSync; + private readonly countByType: StatementSync; + + constructor(dbPath: string, maxEntriesPerType = DEFAULT_KEEP_ENTRIES) { + this.maxEntriesPerType = Math.max(64, maxEntriesPerType); + + const dir = path.dirname(dbPath); + fs.mkdirSync(dir, { recursive: true }); + + this.db = new DatabaseSync(dbPath); + this.db.exec('PRAGMA journal_mode = WAL'); + this.db.exec('PRAGMA synchronous = NORMAL'); + this.initSchema(); + + this.upsertEntry = this.db.prepare( + `INSERT INTO media_entries (type, primary_key, data, last_seen) + VALUES (?, ?, ?, ?) + ON CONFLICT(type, primary_key) DO UPDATE SET + data = excluded.data, + last_seen = excluded.last_seen`, + ); + this.upsertKey = this.db.prepare( + `INSERT INTO media_keys (type, key, primary_key) + VALUES (?, ?, ?) + ON CONFLICT(type, key) DO UPDATE SET primary_key = excluded.primary_key`, + ); + this.findEntryByKey = this.db.prepare( + `SELECT e.data FROM media_entries e + JOIN media_keys k ON k.type = e.type AND k.primary_key = e.primary_key + WHERE k.type = ? AND k.key = ? + LIMIT 1`, + ); + this.findEntryByPrimary = this.db.prepare( + `SELECT data FROM media_entries WHERE type = ? AND primary_key = ?`, + ); + this.evictByType = this.db.prepare( + // `rowid DESC` makes the tiebreaker match insertion order (newest first) + // so reproducible inserts within the same `last_seen` second still get + // the most recent rows retained, regardless of how the lexicographic + // sort would have ordered the synthetic primary_key. + `DELETE FROM media_entries WHERE type = ? AND rowid IN ( + SELECT rowid FROM media_entries + WHERE type = ? + ORDER BY last_seen DESC, rowid DESC + LIMIT -1 OFFSET ? + )`, + ); + this.purgeOrphanKeys = this.db.prepare( + `DELETE FROM media_keys WHERE type = ? AND primary_key NOT IN ( + SELECT primary_key FROM media_entries WHERE type = ? + )`, + ); + this.countByType = this.db.prepare( + `SELECT COUNT(*) AS n FROM media_entries WHERE type = ?`, + ); + } + + close(): void { + this.db.close(); + } + + rememberImage(info: CachedImage): void { + const primaryKey = pickPrimary([info.file, info.fileName]); + if (!primaryKey) return; + this.upsertWithAliases(TYPE_IMAGE, primaryKey, info, [info.file, info.fileName, info.url]); + } + + rememberRecord(info: CachedRecord): void { + const primaryKey = pickPrimary([info.file, info.fileName, info.fileId]); + if (!primaryKey) return; + this.upsertWithAliases(TYPE_RECORD, primaryKey, info, [info.file, info.fileName, info.fileId, info.url]); + } + + findImage(key: string): CachedImage | null { + return this.findByAnyKey(TYPE_IMAGE, key); + } + + findRecord(key: string): CachedRecord | null { + return this.findByAnyKey(TYPE_RECORD, key); + } + + updateImageUrl(key: string, url: string): void { + if (!url) return; + const cached = this.findImage(key); + if (!cached || cached.url === url) return; + this.rememberImage({ ...cached, url }); + } + + updateRecordUrl(key: string, url: string): void { + if (!url) return; + const cached = this.findRecord(key); + if (!cached || cached.url === url) return; + this.rememberRecord({ ...cached, url }); + } + + /** Snapshot count of distinct entries per type; mostly used by tests. */ + size(): { images: number; records: number } { + const img = this.countByType.get(TYPE_IMAGE) as { n: number } | undefined; + const rec = this.countByType.get(TYPE_RECORD) as { n: number } | undefined; + return { images: img?.n ?? 0, records: rec?.n ?? 0 }; + } + + // --- internals --- + + private initSchema(): void { + this.db.exec(` + CREATE TABLE IF NOT EXISTS media_entries ( + type TEXT NOT NULL, + primary_key TEXT NOT NULL, + data TEXT NOT NULL, + last_seen INTEGER NOT NULL, + PRIMARY KEY (type, primary_key) + ); + CREATE INDEX IF NOT EXISTS idx_media_entries_lastseen + ON media_entries(type, last_seen DESC); + CREATE TABLE IF NOT EXISTS media_keys ( + type TEXT NOT NULL, + key TEXT NOT NULL, + primary_key TEXT NOT NULL, + PRIMARY KEY (type, key) + ); + CREATE INDEX IF NOT EXISTS idx_media_keys_primary + ON media_keys(type, primary_key); + `); + } + + private upsertWithAliases( + type: string, + primaryKey: string, + info: T, + aliases: (string | undefined)[], + ): void { + const data = JSON.stringify(info); + const lastSeen = Math.floor(Date.now() / 1000); + + // Run the writes in a transaction so concurrent readers always see a + // consistent (entry, keys) pair. + this.db.exec('BEGIN'); + try { + this.upsertEntry.run(type, primaryKey, data, lastSeen); + const seen = new Set(); + for (const raw of aliases) { + if (!raw) continue; + if (seen.has(raw)) continue; + seen.add(raw); + this.upsertKey.run(type, raw, primaryKey); + } + this.db.exec('COMMIT'); + } catch (err) { + try { this.db.exec('ROLLBACK'); } catch { /* ignore */ } + throw err; + } + + // Eager eviction. The DELETE is a no-op when row count <= cap so + // there is no measurable cost while we are below the limit. + this.evictOldEntries(type); + } + + private findByAnyKey(type: string, key: string): T | null { + if (!key) return null; + const row = this.findEntryByKey.get(type, key) as { data: string } | undefined; + if (row?.data) return safeParse(row.data); + // Fall back to looking up by primary_key directly so callers that pass + // the canonical identifier still hit (e.g. when the alias index is yet + // to be built for that key in this session). + const fallback = this.findEntryByPrimary.get(type, key) as { data: string } | undefined; + return fallback?.data ? safeParse(fallback.data) : null; + } + + private evictOldEntries(type: string): void { + try { + this.evictByType.run(type, type, this.maxEntriesPerType); + this.purgeOrphanKeys.run(type, type); + } catch { + // Best-effort eviction; don't propagate. + } + } +} + +function pickPrimary(candidates: (string | undefined)[]): string { + for (const c of candidates) { + if (c && c.length > 0) return c; + } + return ''; +} + +function safeParse(json: string): T | null { + try { + return JSON.parse(json) as T; + } catch { + return null; + } +} diff --git a/packages/core/src/onebot/network/adapter.ts b/packages/core/src/onebot/network/adapter.ts new file mode 100644 index 00000000..d502d451 --- /dev/null +++ b/packages/core/src/onebot/network/adapter.ts @@ -0,0 +1,65 @@ +// Base abstractions for OneBot network adapters. +// +// Modeled on NapCat's `IOB11NetworkAdapter` so each entry under +// `config.networks.{httpServers,httpClients,wsServers,wsClients}` becomes +// exactly one adapter instance, identified by `name`. The previous design +// rolled multiple servers/clients into a single transport class; the +// per-entry split makes hot-reload, lifecycle, and event isolation much +// simpler to reason about. + +import type { ApiHandler } from '../api-handler'; +import type { JsonObject, NetworkBase } from '../types'; +import type { DispatchPayload } from '../event-filter'; + +export interface NetworkAdapterContext { + /** Self UIN as string — written into outbound headers. */ + uin: string; + /** Shared API dispatcher used by request/response style adapters. */ + api: ApiHandler; + /** Build a lifecycle event payload (connect/enable/disable). */ + buildLifecycleEvent(subType: 'connect' | 'enable' | 'disable'): JsonObject; + /** Build a heartbeat event payload. */ + buildHeartbeatEvent(): JsonObject; +} + +export enum NetworkReloadType { + /** Configuration unchanged or only soft fields updated. */ + Normal = 0, + /** Underlying socket/listener restarted in place. */ + Reopened = 1, + /** Was running, now closed. */ + Closed = 2, + /** Was idle, now opened. */ + Opened = 3, +} + +export abstract class IOneBotNetworkAdapter { + readonly name: string; + protected config: C; + protected readonly ctx: NetworkAdapterContext; + protected isEnabled = false; + + constructor(name: string, config: C, ctx: NetworkAdapterContext) { + this.name = name; + // Defensive copy so reload diffs never mutate the caller's object. + this.config = structuredClone(config); + this.ctx = ctx; + } + + /** Whether the adapter is currently accepting/relaying events. */ + get isActive(): boolean { return this.isEnabled; } + + /** Snapshot of the active config (read-only). */ + get currentConfig(): Readonly { return this.config; } + + abstract open(): void | Promise; + abstract close(): void | Promise; + abstract reload(config: C): NetworkReloadType | Promise; + + /** + * Receive one fully-shaped OneBot event for forwarding. Adapters that + * don't push events (e.g. HTTP request-response servers) override with a + * no-op. + */ + abstract onEvent(event: JsonObject, payload: DispatchPayload): void | Promise; +} diff --git a/packages/core/src/onebot/network/http-post-adapter.ts b/packages/core/src/onebot/network/http-post-adapter.ts new file mode 100644 index 00000000..b0803903 --- /dev/null +++ b/packages/core/src/onebot/network/http-post-adapter.ts @@ -0,0 +1,136 @@ +// Outbound HTTP webhook adapter — one instance per `httpClients[]` entry. +// +// Forwards every event to the configured URL via HTTP POST and interprets +// any JSON response body as a OneBot v11 quick-operation. + +import type { HttpClientNetwork, JsonObject } from '../types'; +import { + pickDispatchJson, + resolveReportOptions, + type DispatchPayload, + type EventReportOptions, +} from '../event-filter'; +import { createLogger } from '../../utils/logger'; +import { IOneBotNetworkAdapter, NetworkReloadType, type NetworkAdapterContext } from './adapter'; +import { executeQuickOperation } from './quick-operation'; + +const log = createLogger('OneBot.POST'); +const DEFAULT_TIMEOUT_MS = 5000; + +export class HttpPostAdapter extends IOneBotNetworkAdapter { + private options: EventReportOptions; + private signature_: string; + + constructor(name: string, config: HttpClientNetwork, ctx: NetworkAdapterContext) { + super(name, config, ctx); + this.options = resolveReportOptions(config); + this.signature_ = bindingSignature(config); + } + + override get isActive(): boolean { + return this.isEnabled; + } + + open(): void { + if (this.config.enabled === false) return; + if (!this.config.url) return; + this.isEnabled = true; + } + + close(): void { + this.isEnabled = false; + } + + async reload(next: HttpClientNetwork): Promise { + const wasEnabled = this.isEnabled; + const willEnable = next.enabled !== false && !!next.url; + + this.config = structuredClone(next); + this.options = resolveReportOptions(next); + const newSig = bindingSignature(next); + const sigChanged = newSig !== this.signature_; + this.signature_ = newSig; + + if (sigChanged && wasEnabled) { + this.close(); + if (willEnable) { + this.open(); + return NetworkReloadType.Reopened; + } + return NetworkReloadType.Closed; + } + if (!wasEnabled && willEnable) { + this.open(); + return NetworkReloadType.Opened; + } + if (wasEnabled && !willEnable) { + this.close(); + return NetworkReloadType.Closed; + } + return NetworkReloadType.Normal; + } + + onEvent(event: JsonObject, payload: DispatchPayload): void { + if (!this.isEnabled) return; + const json = pickDispatchJson(payload, this.options); + if (json === null) return; + void this.postEvent(json, event); + } + + private async postEvent(payload: string, event: JsonObject): Promise { + if (!this.isEnabled) return; + + const headers: Record = { + 'Content-Type': 'application/json', + 'User-Agent': 'OneBot', + 'X-Self-ID': this.ctx.uin, + }; + if (this.config.accessToken) { + headers['X-Signature'] = await computeHmacSha1(this.config.accessToken, payload); + } + + const timeoutMs = this.config.timeoutMs ?? DEFAULT_TIMEOUT_MS; + try { + const response = await fetch(this.config.url, { + method: 'POST', + headers, + body: payload, + signal: AbortSignal.timeout(timeoutMs), + }); + if (response.ok) { + const contentType = response.headers.get('content-type') ?? ''; + if (contentType.includes('application/json')) { + const body = await response.text(); + if (body.trim()) { + await this.handleQuickOperation(event, body); + } + } + } else { + log.warn('[%s] POST %s returned %d', this.name, this.config.url, response.status); + } + } catch (error) { + if (this.isEnabled) { + log.warn('[%s] POST %s failed: %s', this.name, this.config.url, error instanceof Error ? error.message : String(error)); + } + } + } + + private async handleQuickOperation(event: JsonObject, responseBody: string): Promise { + try { + const operation = JSON.parse(responseBody) as Record; + if (!operation || typeof operation !== 'object') return; + await executeQuickOperation(event, operation, this.ctx.api); + } catch (error) { + log.warn('[%s] quick operation failed: %s', this.name, error instanceof Error ? error.message : String(error)); + } + } +} + +function bindingSignature(net: HttpClientNetwork): string { + return `${net.url}#${net.accessToken ?? ''}#${net.timeoutMs ?? DEFAULT_TIMEOUT_MS}`; +} + +async function computeHmacSha1(secret: string, payload: string): Promise { + const { createHmac } = await import('crypto'); + return 'sha1=' + createHmac('sha1', secret).update(payload).digest('hex'); +} diff --git a/packages/core/src/onebot/network/http-server-adapter.ts b/packages/core/src/onebot/network/http-server-adapter.ts new file mode 100644 index 00000000..778ae4b2 --- /dev/null +++ b/packages/core/src/onebot/network/http-server-adapter.ts @@ -0,0 +1,203 @@ +// Inbound HTTP API server — one instance per `httpServers[]` entry. +// +// Pure request/response: incoming POST/GET maps to `ApiHandler.handle`, +// the response body is JSON-serialized back. `onEvent` is a deliberate +// no-op since this transport never pushes events. + +import { + createServer, + type IncomingMessage, + type Server, + type ServerResponse, +} from 'http'; +import type { HttpServerNetwork, JsonObject, JsonValue } from '../types'; +import type { DispatchPayload } from '../event-filter'; +import { createLogger } from '../../utils/logger'; +import { IOneBotNetworkAdapter, NetworkReloadType, type NetworkAdapterContext } from './adapter'; +import { isAuthorized, normalizePath } from './utils'; + +const log = createLogger('OneBot.HTTP'); + +export class HttpServerAdapter extends IOneBotNetworkAdapter { + private server: Server | null = null; + + override get isActive(): boolean { + // HTTP server never receives events — `isActive` purely tracks the + // bound listener so the network manager can skip dispatch cleanly. + return this.isEnabled; + } + + open(): void { + if (this.isEnabled) return; + if (this.config.enabled === false) return; + this.startServer(); + this.isEnabled = true; + } + + close(): void { + if (!this.isEnabled && !this.server) return; + this.isEnabled = false; + this.server?.close(); + this.server = null; + } + + async reload(next: HttpServerNetwork): Promise { + const prevSig = bindingSignature(this.config); + const wasEnabled = this.isEnabled; + const willEnable = next.enabled !== false; + + this.config = structuredClone(next); + + const sigChanged = prevSig !== bindingSignature(next); + if (sigChanged && wasEnabled) { + this.close(); + if (willEnable) { + this.open(); + return NetworkReloadType.Reopened; + } + return NetworkReloadType.Closed; + } + if (!wasEnabled && willEnable) { + this.open(); + return NetworkReloadType.Opened; + } + if (wasEnabled && !willEnable) { + this.close(); + return NetworkReloadType.Closed; + } + return NetworkReloadType.Normal; + } + + // HTTP server only acts on inbound requests; it doesn't push events. + onEvent(_event: JsonObject, _payload: DispatchPayload): void { /* no-op */ } + + private startServer(): void { + const expectedPath = normalizePath(this.config.path ?? '/'); + const accessToken = this.config.accessToken ?? ''; + + const server = createServer((req, res) => { + void this.handleRequest(expectedPath, accessToken, req, res); + }); + this.server = server; + + server.on('listening', () => { + log.success( + '[%s] listening %s:%d%s', + this.name, + this.config.host ?? '0.0.0.0', + this.config.port, + expectedPath, + ); + }); + server.on('error', (err) => { + log.warn('[%s] server error: %s', this.name, err instanceof Error ? err.message : String(err)); + }); + + server.listen(this.config.port, this.config.host ?? '0.0.0.0'); + } + + private async handleRequest(expectedPath: string, accessToken: string, req: IncomingMessage, res: ServerResponse): Promise { + const parsedUrl = new URL(req.url ?? '/', 'http://127.0.0.1'); + const incomingPath = parsedUrl.pathname; + + const ep = expectedPath.endsWith('/') ? expectedPath : expectedPath + '/'; + let action = ''; + if (incomingPath === expectedPath || incomingPath === expectedPath + '/') { + action = ''; + } else if (incomingPath.startsWith(ep)) { + action = incomingPath.substring(ep.length); + } else { + writeJson(res, 404, { status: 'failed', retcode: 1404, data: null, wording: 'not found' }); + return; + } + + if (!isAuthorized(req, accessToken)) { + writeJson(res, 401, { status: 'failed', retcode: 1401, data: null, wording: 'unauthorized' }); + return; + } + + if (req.method === 'GET' && !action) { + writeJson(res, 200, { status: 'ok', retcode: 0, data: { online: true } }); + return; + } + + try { + let params: Record = {}; + let echo: unknown; + + if (req.method === 'GET') { + parsedUrl.searchParams.forEach((value, key) => { + try { + params[key] = JSON.parse(value); + } catch { + params[key] = value; + } + }); + } else if (req.method === 'POST') { + const bodyContent = await readRequestBody(req); + if (bodyContent.trim()) { + try { + const parsedBody = JSON.parse(bodyContent); + if (typeof parsedBody === 'object' && parsedBody !== null && !Array.isArray(parsedBody)) { + if (parsedBody.action && !action) action = String(parsedBody.action); + if (parsedBody.params && typeof parsedBody.params === 'object' && !Array.isArray(parsedBody.params)) { + params = parsedBody.params as Record; + } else { + params = parsedBody as Record; + } + echo = parsedBody.echo; + } + } catch { + writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: 'bad request: invalid json' }); + return; + } + } + } else { + writeJson(res, 405, { status: 'failed', retcode: 1400, data: null, wording: 'method not allowed' }); + return; + } + + if (!action) { + writeJson(res, 400, { status: 'failed', retcode: 1400, data: null, wording: 'bad request: missing action' }); + return; + } + + const response = await this.ctx.api.handle(action, params as JsonObject); + if (echo !== undefined) { + response.echo = echo as JsonValue; + } + writeJson(res, 200, response); + } catch (error) { + const wording = error instanceof Error ? error.message : 'internal error'; + writeJson(res, 500, { status: 'failed', retcode: 1200, data: null, wording }); + } + } +} + +function bindingSignature(net: HttpServerNetwork): string { + return `${net.host ?? '0.0.0.0'}:${net.port}${normalizePath(net.path ?? '/')}#${net.accessToken ?? ''}`; +} + +function readRequestBody(req: IncomingMessage, maxBytes = 2 * 1024 * 1024): Promise { + return new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + let total = 0; + req.on('data', (chunk: Buffer) => { + total += chunk.length; + if (total > maxBytes) { + reject(new Error('request body too large')); + req.destroy(); + return; + } + chunks.push(chunk); + }); + req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))); + req.on('error', reject); + }); +} + +function writeJson(res: ServerResponse, statusCode: number, data: unknown): void { + res.statusCode = statusCode; + res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.end(JSON.stringify(data)); +} diff --git a/packages/core/src/onebot/network/index.ts b/packages/core/src/onebot/network/index.ts new file mode 100644 index 00000000..ccd5b2a3 --- /dev/null +++ b/packages/core/src/onebot/network/index.ts @@ -0,0 +1,9 @@ +// Public surface for the OneBot network package. + +export { IOneBotNetworkAdapter, NetworkReloadType, type NetworkAdapterContext } from './adapter'; +export { OneBotNetworkManager } from './network-manager'; +export { WsServerAdapter } from './ws-server-adapter'; +export { WsClientAdapter } from './ws-client-adapter'; +export { HttpServerAdapter } from './http-server-adapter'; +export { HttpPostAdapter } from './http-post-adapter'; +export { executeQuickOperation } from './quick-operation'; diff --git a/packages/core/src/onebot/network/network-manager.ts b/packages/core/src/onebot/network/network-manager.ts new file mode 100644 index 00000000..b1e5a621 --- /dev/null +++ b/packages/core/src/onebot/network/network-manager.ts @@ -0,0 +1,105 @@ +// OneBot network manager. +// +// Mirrors NapCat's `OB11NetworkManager`: holds a `Map`, fans +// events out to every active adapter in parallel, and provides primitives +// the OneBot instance uses to register, hot-reload, and tear adapters down. + +import type { JsonObject, NetworkBase } from '../types'; +import { buildDispatchPayload } from '../event-filter'; +import { IOneBotNetworkAdapter } from './adapter'; +import { createLogger } from '../../utils/logger'; + +const log = createLogger('OneBot.Network'); + +type AnyAdapter = IOneBotNetworkAdapter; + +export class OneBotNetworkManager { + private readonly adapters = new Map(); + + register(adapter: IOneBotNetworkAdapter): void { + const existing = this.adapters.get(adapter.name); + if (existing) { + // Replacing an adapter under the same name — close the previous one + // before storing the new instance to keep ports/sockets clean. + log.info('replacing adapter [%s]', adapter.name); + void Promise.resolve(existing.close()).catch(() => { /* best-effort */ }); + } + this.adapters.set(adapter.name, adapter as AnyAdapter); + } + + has(name: string): boolean { return this.adapters.has(name); } + + get(name: string): AnyAdapter | null { + return this.adapters.get(name) ?? null; + } + + list(): AnyAdapter[] { return [...this.adapters.values()]; } + + hasActiveAdapters(): boolean { + for (const a of this.adapters.values()) if (a.isActive) return true; + return false; + } + + async openAll(): Promise { + const tasks = this.list().map(async (a) => { + try { + await a.open(); + } catch (err) { + log.error('adapter [%s] open failed: %s', a.name, errMessage(err)); + } + }); + await Promise.all(tasks); + } + + async closeAll(): Promise { + const tasks = this.list().map(async (a) => { + try { + await a.close(); + } catch (err) { + log.warn('adapter [%s] close failed: %s', a.name, errMessage(err)); + } + }); + await Promise.all(tasks); + this.adapters.clear(); + } + + async closeOne(name: string): Promise { + const adapter = this.adapters.get(name); + if (!adapter) return; + this.adapters.delete(name); + try { + await adapter.close(); + } catch (err) { + log.warn('adapter [%s] close failed: %s', adapter.name, errMessage(err)); + } + } + + /** + * Dispatch one canonical OneBot event to every active adapter in parallel. + * The dispatch payload is built once so each adapter pays only the cost + * of picking the right pre-serialized variant. + * + * Errors thrown inside an adapter are isolated — one bad adapter never + * blocks the others. + */ + async emitEvent(event: JsonObject): Promise { + if (!this.hasActiveAdapters()) return; + const payload = buildDispatchPayload(event); + const tasks: Promise[] = []; + for (const adapter of this.adapters.values()) { + if (!adapter.isActive) continue; + tasks.push( + Promise.resolve() + .then(() => adapter.onEvent(event, payload)) + .catch((err) => { + log.warn('adapter [%s] onEvent error: %s', adapter.name, errMessage(err)); + }), + ); + } + await Promise.allSettled(tasks); + } +} + +function errMessage(err: unknown): string { + return err instanceof Error ? err.message : String(err); +} diff --git a/packages/core/src/onebot/network/quick-operation.ts b/packages/core/src/onebot/network/quick-operation.ts new file mode 100644 index 00000000..272ce9f9 --- /dev/null +++ b/packages/core/src/onebot/network/quick-operation.ts @@ -0,0 +1,89 @@ +// OneBot v11 hidden API: `.handle_quick_operation`. +// +// When an HTTP POST event report receives a JSON body in response, that body +// is interpreted as a "quick operation" — short-cuts to common follow-up +// API calls (reply, recall, ban, kick, approve/reject). Spec: +// https://github.com/botuniverse/onebot-11/blob/master/api/hidden.md + +import type { ApiHandler } from '../api-handler'; +import type { JsonObject, JsonValue } from '../types'; + +export async function executeQuickOperation( + event: JsonObject, + operation: Record, + api: ApiHandler, +): Promise { + const postType = event.post_type as string; + + if (postType === 'message') { + if (operation.reply !== undefined && operation.reply !== null && operation.reply !== '') { + const messageType = event.message_type as string; + const autoEscape = !!operation.auto_escape; + + if (messageType === 'group') { + const params: JsonObject = { + group_id: event.group_id as number, + message: operation.reply as JsonValue, + auto_escape: autoEscape, + }; + if (operation.at_sender !== false && event.user_id) { + const atSegment = { type: 'at', data: { qq: String(event.user_id) } }; + if (typeof operation.reply === 'string') { + params.message = [atSegment, { type: 'text', data: { text: operation.reply as string } }] as JsonValue; + params.auto_escape = false; + } else if (Array.isArray(operation.reply)) { + params.message = [atSegment, ...(operation.reply as unknown[])] as JsonValue; + } + } + await api.handle('send_group_msg', params); + } else if (messageType === 'private') { + await api.handle('send_private_msg', { + user_id: event.user_id as number, + message: operation.reply as JsonValue, + auto_escape: autoEscape, + }); + } + } + + if (operation.delete) { + await api.handle('delete_msg', { message_id: event.message_id as number }); + } + + if (operation.ban && event.message_type === 'group') { + const duration = typeof operation.ban_duration === 'number' ? operation.ban_duration : 1800; + await api.handle('set_group_ban', { + group_id: event.group_id as number, + user_id: event.user_id as number, + duration: duration as JsonValue, + }); + } + + if (operation.kick && event.message_type === 'group') { + await api.handle('set_group_kick', { + group_id: event.group_id as number, + user_id: event.user_id as number, + reject_add_request: !!operation.reject_add_request, + }); + } + } + + if (postType === 'request') { + if (operation.approve !== undefined) { + const requestType = event.request_type as string; + if (requestType === 'friend') { + await api.handle('set_friend_add_request', { + flag: event.flag as string, + approve: operation.approve as JsonValue, + remark: (operation.remark ?? '') as JsonValue, + }); + } else if (requestType === 'group') { + await api.handle('set_group_add_request', { + flag: event.flag as string, + sub_type: event.sub_type as string, + approve: operation.approve as JsonValue, + reason: (operation.reason ?? '') as JsonValue, + }); + } + } + } +} diff --git a/packages/core/src/onebot/network/utils.ts b/packages/core/src/onebot/network/utils.ts new file mode 100644 index 00000000..2e445bb3 --- /dev/null +++ b/packages/core/src/onebot/network/utils.ts @@ -0,0 +1,59 @@ +// Helpers shared between OneBot network adapters. + +import type { IncomingMessage } from 'http'; +import type { WebSocket } from '@snowluma/websocket'; + +/** Validate a request's bearer token / `?access_token=` query against the configured token. */ +export function isAuthorized(request: IncomingMessage, token: string): boolean { + if (!token) return true; + const rawAuth = request.headers.authorization; + const auth = Array.isArray(rawAuth) ? rawAuth[0] : rawAuth ?? ''; + if (auth === `Bearer ${token}`) return true; + try { + const url = new URL(request.url ?? '/', 'http://127.0.0.1'); + if (url.searchParams.get('access_token') === token) return true; + } catch { /* ignore malformed URLs */ } + return false; +} + +/** Convert anything ws can hand us into a UTF-8 string. */ +export function rawDataToString(raw: Buffer | string | ArrayBuffer | ArrayBufferView | Buffer[]): string { + if (typeof raw === 'string') return raw; + if (Buffer.isBuffer(raw)) return raw.toString('utf8'); + if (Array.isArray(raw)) return Buffer.concat(raw).toString('utf8'); + if (raw instanceof ArrayBuffer) return Buffer.from(new Uint8Array(raw)).toString('utf8'); + if (ArrayBuffer.isView(raw)) { + return Buffer.from(raw.buffer, raw.byteOffset, raw.byteLength).toString('utf8'); + } + return ''; +} + +/** Best-effort send: drops payload if the socket isn't OPEN, reports send errors. */ +export function safeSend(socket: WebSocket, payload: string, onError?: (err: Error) => void): void { + if (socket.readyState !== 1 /* WebSocket.OPEN */) return; + socket.send(payload, (error?: Error | null) => { + if (error && onError) onError(error); + }); +} + +/** Best-effort close, treats already-closed sockets as a no-op. */ +export function safeClose(socket: WebSocket, code = 1000, reason = 'normal'): void { + if (socket.readyState === 3 /* CLOSED */ || socket.readyState === 2 /* CLOSING */) return; + socket.close(code, reason); +} + +/** Normalize an HTTP/WS path to a comparable form (strip trailing slash, default to "/"). */ +export function normalizePath(pathValue: string | undefined): string { + const path = (pathValue ?? '/').trim() || '/'; + if (path === '/') return '/'; + return path.endsWith('/') ? path.slice(0, -1) : path; +} + +/** Parse the `pathname` from a (potentially relative) WebSocket request URL. */ +export function parseRequestPath(urlValue: string): string { + try { + return new URL(urlValue, 'ws://127.0.0.1').pathname; + } catch { + return '/'; + } +} diff --git a/packages/core/src/onebot/network/ws-client-adapter.ts b/packages/core/src/onebot/network/ws-client-adapter.ts new file mode 100644 index 00000000..0f9ac953 --- /dev/null +++ b/packages/core/src/onebot/network/ws-client-adapter.ts @@ -0,0 +1,177 @@ +// Reverse (client) WebSocket adapter — one instance per `wsClients[]` entry. +// +// Connects out to a remote OneBot endpoint, manages reconnects, forwards +// pushed events on `onEvent`, and dispatches incoming API messages to the +// shared ApiHandler. + +import { WebSocket } from '@snowluma/websocket'; +import type { WsClientNetwork, WsRole, JsonObject } from '../types'; +import { + pickDispatchJson, + resolveReportOptions, + shapeEventForAdapter, + type DispatchPayload, + type EventReportOptions, +} from '../event-filter'; +import { createLogger } from '../../utils/logger'; +import { IOneBotNetworkAdapter, NetworkReloadType, type NetworkAdapterContext } from './adapter'; +import { rawDataToString, safeClose, safeSend } from './utils'; + +const log = createLogger('OneBot.WS-Client'); +const DEFAULT_RECONNECT_INTERVAL_MS = 5000; + +export class WsClientAdapter extends IOneBotNetworkAdapter { + private socket: WebSocket | null = null; + private reconnectTimer: NodeJS.Timeout | null = null; + private options: EventReportOptions; + private role: WsRole; + private explicitlyClosed = false; + + constructor(name: string, config: WsClientNetwork, ctx: NetworkAdapterContext) { + super(name, config, ctx); + this.options = resolveReportOptions(config); + this.role = config.role ?? 'Universal'; + } + + override get isActive(): boolean { + return this.isEnabled; + } + + open(): void { + if (this.isEnabled) return; + if (this.config.enabled === false) return; + if (!this.config.url) return; + this.explicitlyClosed = false; + this.isEnabled = true; + this.connect(); + } + + close(): void { + this.explicitlyClosed = true; + this.isEnabled = false; + this.cancelReconnect(); + if (this.socket) { + safeClose(this.socket); + this.socket = null; + } + } + + async reload(next: WsClientNetwork): Promise { + const prevSig = bindingSignature(this.config); + const wasEnabled = this.isEnabled; + const willEnable = next.enabled !== false && !!next.url; + + this.config = structuredClone(next); + this.options = resolveReportOptions(next); + this.role = next.role ?? 'Universal'; + + const sigChanged = prevSig !== bindingSignature(next); + if (sigChanged && wasEnabled) { + this.close(); + if (willEnable) { + this.open(); + return NetworkReloadType.Reopened; + } + return NetworkReloadType.Closed; + } + if (!wasEnabled && willEnable) { + this.open(); + return NetworkReloadType.Opened; + } + if (wasEnabled && !willEnable) { + this.close(); + return NetworkReloadType.Closed; + } + return NetworkReloadType.Normal; + } + + onEvent(_event: JsonObject, payload: DispatchPayload): void { + if (!this.isEnabled || !this.socket) return; + if (this.role !== 'Event' && this.role !== 'Universal') return; + const json = pickDispatchJson(payload, this.options); + if (json === null) return; + safeSend(this.socket, json); + } + + private connect(): void { + if (this.explicitlyClosed) return; + if (!this.config.url) return; + if (this.socket) return; + + const headers: Record = { + 'User-Agent': 'OneBot/11', + 'X-Self-ID': this.ctx.uin, + 'X-Client-Role': this.role, + }; + if (this.config.accessToken) { + headers.Authorization = `Bearer ${this.config.accessToken}`; + } + + const socket = new WebSocket(this.config.url, { headers }); + this.socket = socket; + + socket.on('open', () => { + log.info('[%s] connected %s', this.name, this.config.url); + this.sendBootstrapMetaEvents(socket); + }); + + socket.on('message', (raw: Buffer) => { + void this.handleApiMessage(socket, raw); + }); + + socket.on('close', () => { + this.socket = null; + if (this.explicitlyClosed || !this.isEnabled) return; + this.scheduleReconnect(); + }); + + socket.on('error', (err: Error) => { + log.warn('[%s] error %s: %s', this.name, this.config.url, err instanceof Error ? err.message : String(err)); + }); + } + + private scheduleReconnect(): void { + if (this.reconnectTimer) return; + if (this.explicitlyClosed) return; + const interval = Math.max(1000, this.config.reconnectIntervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS); + const timer = setTimeout(() => { + this.reconnectTimer = null; + if (this.explicitlyClosed || !this.isEnabled) return; + this.connect(); + }, interval); + timer.unref?.(); + this.reconnectTimer = timer; + } + + private cancelReconnect(): void { + if (!this.reconnectTimer) return; + clearTimeout(this.reconnectTimer); + this.reconnectTimer = null; + } + + private async handleApiMessage(socket: WebSocket, raw: Buffer | string): Promise { + if (this.role !== 'Api' && this.role !== 'Universal') return; + const text = rawDataToString(raw); + if (!text) return; + const response = await this.ctx.api.processRequest(text); + safeSend(socket, response); + } + + private sendBootstrapMetaEvents(socket: WebSocket): void { + if (this.role !== 'Event' && this.role !== 'Universal') return; + const events = [ + this.ctx.buildLifecycleEvent('connect'), + this.ctx.buildLifecycleEvent('enable'), + this.ctx.buildHeartbeatEvent(), + ]; + for (const event of events) { + const shaped = shapeEventForAdapter(event, this.options); + if (!shaped) continue; + safeSend(socket, JSON.stringify(shaped)); + } + } +} + +function bindingSignature(net: WsClientNetwork): string { + return `${net.url}#${net.role ?? 'Universal'}#${Math.max(1000, net.reconnectIntervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS)}#${net.accessToken ?? ''}`; +} diff --git a/packages/core/src/onebot/network/ws-server-adapter.ts b/packages/core/src/onebot/network/ws-server-adapter.ts new file mode 100644 index 00000000..5edc08a0 --- /dev/null +++ b/packages/core/src/onebot/network/ws-server-adapter.ts @@ -0,0 +1,187 @@ +// Forward (server) WebSocket adapter — one instance per `wsServers[]` entry. +// +// Each adapter owns its own `WebSocketServer`, accepts inbound forward +// connections (api / event / universal roles), and pushes events to all live +// event-receiving sockets when `onEvent` fires. + +import { WebSocket, WebSocketServer } from '@snowluma/websocket'; +import type { IncomingMessage } from 'http'; +import type { WsRole, WsServerNetwork, JsonObject } from '../types'; +import { + pickDispatchJson, + resolveReportOptions, + shapeEventForAdapter, + type DispatchPayload, + type EventReportOptions, +} from '../event-filter'; +import { createLogger } from '../../utils/logger'; +import { IOneBotNetworkAdapter, NetworkReloadType, type NetworkAdapterContext } from './adapter'; +import { isAuthorized, parseRequestPath, rawDataToString, safeClose, safeSend, normalizePath } from './utils'; + +const log = createLogger('OneBot.WS-Server'); + +interface ForwardConn { + socket: WebSocket; + role: WsRole; + options: EventReportOptions; +} + +export class WsServerAdapter extends IOneBotNetworkAdapter { + private wss: WebSocketServer | null = null; + private connections = new Map(); + private options: EventReportOptions; + + constructor(name: string, config: WsServerNetwork, ctx: NetworkAdapterContext) { + super(name, config, ctx); + this.options = resolveReportOptions(config); + } + + override get isActive(): boolean { + return this.isEnabled; + } + + open(): void { + if (this.isEnabled) return; + if (this.config.enabled === false) return; + this.startServer(); + this.isEnabled = true; + } + + close(): void { + if (!this.isEnabled && this.connections.size === 0 && !this.wss) return; + // Final lifecycle broadcast before tearing down so attached event clients + // see the disable transition. + const lifecycle = this.ctx.buildLifecycleEvent('disable'); + for (const conn of this.connections.values()) { + if (conn.role === 'Api') continue; + const shaped = shapeEventForAdapter(lifecycle, conn.options); + if (!shaped) continue; + safeSend(conn.socket, JSON.stringify(shaped)); + } + + this.isEnabled = false; + for (const ws of [...this.connections.keys()]) safeClose(ws); + this.connections.clear(); + this.wss?.close(); + this.wss = null; + } + + async reload(next: WsServerNetwork): Promise { + const prevSig = bindingSignature(this.config); + const wasEnabled = this.isEnabled; + const willEnable = next.enabled !== false; + + this.config = structuredClone(next); + this.options = resolveReportOptions(next); + for (const conn of this.connections.values()) conn.options = this.options; + + const sigChanged = prevSig !== bindingSignature(next); + if (sigChanged && wasEnabled) { + this.close(); + if (willEnable) { + this.open(); + return NetworkReloadType.Reopened; + } + return NetworkReloadType.Closed; + } + if (!wasEnabled && willEnable) { + this.open(); + return NetworkReloadType.Opened; + } + if (wasEnabled && !willEnable) { + this.close(); + return NetworkReloadType.Closed; + } + return NetworkReloadType.Normal; + } + + onEvent(_event: JsonObject, payload: DispatchPayload): void { + if (!this.isEnabled || this.connections.size === 0) return; + for (const conn of this.connections.values()) { + if (conn.role !== 'Event' && conn.role !== 'Universal') continue; + const json = pickDispatchJson(payload, conn.options); + if (json === null) continue; + safeSend(conn.socket, json); + } + } + + private startServer(): void { + const wss = new WebSocketServer({ + host: this.config.host ?? '0.0.0.0', + port: this.config.port, + path: this.config.path ?? '/', + }); + this.wss = wss; + + wss.on('listening', () => { + log.success( + '[%s] listening %s:%d%s', + this.name, + this.config.host ?? '0.0.0.0', + this.config.port, + this.config.path ?? '/', + ); + }); + + wss.on('error', (err: Error) => { + log.warn('[%s] server error: %s', this.name, err instanceof Error ? err.message : String(err)); + }); + + wss.on('connection', (socket: WebSocket, request: IncomingMessage) => this.onConnection(socket, request)); + } + + private onConnection(socket: WebSocket, request: IncomingMessage): void { + if (!isAuthorized(request, this.config.accessToken ?? '')) { + safeClose(socket, 1008, 'invalid access token'); + return; + } + + const role = this.config.role ?? classifyForwardRole(request); + const conn: ForwardConn = { socket, role, options: this.options }; + this.connections.set(socket, conn); + + socket.on('message', (raw: Buffer) => { + void this.handleApiMessage(socket, role, raw); + }); + socket.on('close', () => this.connections.delete(socket)); + socket.on('error', (err: Error) => { + log.warn('[%s] socket error: %s', this.name, err instanceof Error ? err.message : String(err)); + }); + + if (role === 'Event' || role === 'Universal') { + this.sendBootstrapMetaEvents(socket); + } + } + + private async handleApiMessage(socket: WebSocket, role: WsRole, raw: Buffer | string): Promise { + if (role !== 'Api' && role !== 'Universal') return; + const text = rawDataToString(raw); + if (!text) return; + const response = await this.ctx.api.processRequest(text); + safeSend(socket, response); + } + + private sendBootstrapMetaEvents(socket: WebSocket): void { + const events = [ + this.ctx.buildLifecycleEvent('connect'), + this.ctx.buildLifecycleEvent('enable'), + this.ctx.buildHeartbeatEvent(), + ]; + for (const event of events) { + const shaped = shapeEventForAdapter(event, this.options); + if (!shaped) continue; + safeSend(socket, JSON.stringify(shaped)); + } + } +} + +function classifyForwardRole(request: IncomingMessage): WsRole { + const path = parseRequestPath(request.url ?? '/'); + if (path.endsWith('/api')) return 'Api'; + if (path.endsWith('/event')) return 'Event'; + return 'Universal'; +} + +function bindingSignature(net: WsServerNetwork): string { + return `${net.host ?? '0.0.0.0'}:${net.port}${normalizePath(net.path)}#${net.role ?? 'auto'}#${net.accessToken ?? ''}`; +} diff --git a/packages/core/src/onebot/types.ts b/packages/core/src/onebot/types.ts index 203c195f..1523d367 100644 --- a/packages/core/src/onebot/types.ts +++ b/packages/core/src/onebot/types.ts @@ -33,7 +33,7 @@ export type NetworkKind = 'httpServers' | 'httpClients' | 'wsServers' | 'wsClien * The `name` is a free-form identifier scoped to the adapter's array; it is * used for hot-reload bookkeeping and as a label in logs / WebUI. */ -interface NetworkBase { +export interface NetworkBase { name: string; /** When `false`, the adapter is configured but inactive. Defaults to `true`. */ enabled?: boolean; diff --git a/packages/core/src/onebot/ws-transport.ts b/packages/core/src/onebot/ws-transport.ts deleted file mode 100644 index 7bd89c53..00000000 --- a/packages/core/src/onebot/ws-transport.ts +++ /dev/null @@ -1,424 +0,0 @@ -import { WebSocket, WebSocketServer } from '@snowluma/websocket'; -import type { IncomingMessage } from 'http'; -import type { ApiHandler } from './api-handler'; -import type { JsonObject, OneBotConfig, WsClientNetwork, WsRole, WsServerNetwork } from './types'; -import { createLogger } from '../utils/logger'; -import { - buildDispatchPayload, - pickDispatchJson, - resolveReportOptions, - shapeEventForAdapter, - type DispatchPayload, - type EventReportOptions, -} from './event-filter'; - -interface ForwardConnection { - socket: WebSocket; - role: WsRole; - options: EventReportOptions; - serverName: string; -} - -interface ReverseConnection { - socket: WebSocket; - role: WsRole; - options: EventReportOptions; - endpointUrl: string; - reconnectIntervalMs: number; - name: string; -} - -interface RunningWsServer { - wss: WebSocketServer; - signature: string; - network: WsServerNetwork; - options: EventReportOptions; -} - -interface RunningWsClient { - network: WsClientNetwork; - signature: string; - options: EventReportOptions; -} - -export interface WsTransportContext { - uin: string; - api: ApiHandler; - buildLifecycleEvent: (subType: 'connect' | 'enable' | 'disable') => JsonObject; - buildHeartbeatEvent: () => JsonObject; -} - -const log = createLogger('OneBot.WS'); - -export class WsTransport { - private readonly context: WsTransportContext; - - private readonly servers = new Map(); - private readonly clientStates = new Map(); - private readonly forwardConnections = new Map(); - private readonly reverseConnections = new Map(); - private readonly reconnectTimers = new Map(); - private serverNetworks: WsServerNetwork[] = []; - private clientNetworks: WsClientNetwork[] = []; - private stopped = false; - - constructor(config: OneBotConfig, context: WsTransportContext) { - this.serverNetworks = activeServers(config); - this.clientNetworks = activeClients(config); - this.context = context; - for (const network of this.clientNetworks) { - this.clientStates.set(network.name, { - network, - signature: wsClientSignature(network), - options: resolveReportOptions(network), - }); - } - } - - start(): void { - this.stopped = false; - for (const network of this.serverNetworks) this.startServerEndpoint(network); - for (const network of this.clientNetworks) this.startClientEndpoint(network.name); - } - - reloadConfig(config: OneBotConfig): void { - const nextServers = activeServers(config); - const nextClients = activeClients(config); - const nextServerByName = new Map(nextServers.map((n) => [n.name, n] as const)); - const nextClientByName = new Map(nextClients.map((n) => [n.name, n] as const)); - - // --- WS Servers --- - for (const [name, running] of [...this.servers]) { - const next = nextServerByName.get(name); - if (!next || wsServerSignature(next) !== running.signature) { - running.wss.close(); - this.servers.delete(name); - // Drop forward connections that belonged to the closed adapter. - for (const [socket, conn] of [...this.forwardConnections]) { - if (conn.serverName === name) { - safeClose(socket); - this.forwardConnections.delete(socket); - } - } - log.info('stopped forward server [%s]', name); - } else { - // Same binding — refresh options so reload picks up format changes. - running.network = next; - running.options = resolveReportOptions(next); - for (const conn of this.forwardConnections.values()) { - if (conn.serverName === name) conn.options = running.options; - } - } - } - for (const network of nextServers) { - if (!this.servers.has(network.name)) this.startServerEndpoint(network); - } - - // --- WS Clients (reverse) --- - for (const [name, state] of [...this.clientStates]) { - const next = nextClientByName.get(name); - const sig = next ? wsClientSignature(next) : null; - if (!next || sig !== state.signature) { - const conn = this.reverseConnections.get(name); - if (conn) { - safeClose(conn.socket); - this.reverseConnections.delete(name); - } - const timer = this.reconnectTimers.get(name); - if (timer) { - clearTimeout(timer); - this.reconnectTimers.delete(name); - } - this.clientStates.delete(name); - log.info('stopped reverse client [%s]', name); - } else { - state.network = next; - state.options = resolveReportOptions(next); - const conn = this.reverseConnections.get(name); - if (conn) conn.options = state.options; - } - } - for (const network of nextClients) { - if (!this.clientStates.has(network.name)) { - this.clientStates.set(network.name, { - network, - signature: wsClientSignature(network), - options: resolveReportOptions(network), - }); - if (!this.stopped) this.startClientEndpoint(network.name); - } - } - - this.serverNetworks = nextServers; - this.clientNetworks = nextClients; - } - - stop(): void { - // Final lifecycle broadcast before we mark ourselves stopped. - this.publishEvent(this.context.buildLifecycleEvent('disable')); - this.stopped = true; - - for (const timer of this.reconnectTimers.values()) { - clearTimeout(timer); - } - this.reconnectTimers.clear(); - - for (const socket of this.forwardConnections.keys()) { - safeClose(socket); - } - this.forwardConnections.clear(); - - for (const conn of this.reverseConnections.values()) { - safeClose(conn.socket); - } - this.reverseConnections.clear(); - - for (const { wss } of this.servers.values()) { - wss.close(); - } - this.servers.clear(); - this.clientStates.clear(); - } - - /** - * Fan out one canonical event to every event-receiving WS connection. - * - * The instance pre-builds the {@link DispatchPayload} (two JSON variants at - * most) so each connection is served with a single O(1) pick + send rather - * than re-shaping or re-serializing the event itself. The raw `event` is - * accepted so ad-hoc callers (e.g. `stop()`) can dispatch without allocating - * a payload at the call site. - */ - publishEvent(event: JsonObject, payload?: DispatchPayload): void { - const dispatch = payload ?? buildDispatchPayload(event); - - for (const conn of this.forwardConnections.values()) { - if (conn.role !== 'Event' && conn.role !== 'Universal') continue; - const json = pickDispatchJson(dispatch, conn.options); - if (json === null) continue; - safeSend(conn.socket, json); - } - - for (const conn of this.reverseConnections.values()) { - if (conn.role !== 'Event' && conn.role !== 'Universal') continue; - const json = pickDispatchJson(dispatch, conn.options); - if (json === null) continue; - safeSend(conn.socket, json); - } - } - - private startServerEndpoint(network: WsServerNetwork): void { - const wss = new WebSocketServer({ - host: network.host ?? '0.0.0.0', - port: network.port, - path: network.path ?? '/', - }); - - const running: RunningWsServer = { - wss, - signature: wsServerSignature(network), - network, - options: resolveReportOptions(network), - }; - this.servers.set(network.name, running); - - wss.on('listening', () => { - log.success('[%s] listening %s:%d%s', network.name, network.host ?? '0.0.0.0', network.port, network.path ?? '/'); - }); - - wss.on('connection', (socket, request) => { - if (!isAuthorized(request, running.network.accessToken ?? '')) { - safeClose(socket, 1008, 'invalid access token'); - return; - } - - const role = running.network.role ?? classifyForwardRole(request); - const conn: ForwardConnection = { - socket, - role, - options: running.options, - serverName: running.network.name, - }; - this.forwardConnections.set(socket, conn); - - socket.on('message', (raw: Buffer) => { - void this.handleApiMessage(socket, role, raw); - }); - - socket.on('close', () => { - this.forwardConnections.delete(socket); - }); - - socket.on('error', (error: Error) => { - log.warn('[%s] forward socket error: %s', running.network.name, error instanceof Error ? error.message : String(error)); - }); - - this.sendBootstrapMetaEvents(socket, role, conn.options); - }); - - wss.on('error', (error: Error) => { - log.warn('[%s] server error: %s', network.name, error instanceof Error ? error.message : String(error)); - }); - } - - private startClientEndpoint(name: string): void { - const state = this.clientStates.get(name); - if (!state || this.stopped) return; - if (this.reverseConnections.has(name) || this.reconnectTimers.has(name)) return; - - const network = state.network; - const role = network.role ?? 'Universal'; - const headers: Record = { - 'User-Agent': 'OneBot/11', - 'X-Self-ID': this.context.uin, - 'X-Client-Role': role, - }; - if (network.accessToken) headers.Authorization = `Bearer ${network.accessToken}`; - - const socket = new WebSocket(network.url, { headers }); - const conn: ReverseConnection = { - socket, - role, - options: state.options, - endpointUrl: network.url, - reconnectIntervalMs: Math.max(1000, network.reconnectIntervalMs ?? 5000), - name, - }; - this.reverseConnections.set(name, conn); - - socket.on('open', () => { - log.info('[%s] reverse connected %s', name, network.url); - this.sendBootstrapMetaEvents(socket, role, state.options); - }); - - socket.on('message', (raw: Buffer) => { - void this.handleApiMessage(socket, role, raw); - }); - - socket.on('close', () => { - this.reverseConnections.delete(name); - if (this.stopped) return; - if (!this.clientStates.has(name)) return; - if (this.reconnectTimers.has(name)) return; - - const timer = setTimeout(() => { - this.reconnectTimers.delete(name); - if (this.stopped || !this.clientStates.has(name)) return; - this.startClientEndpoint(name); - }, conn.reconnectIntervalMs); - timer.unref?.(); - this.reconnectTimers.set(name, timer); - }); - - socket.on('error', (error: Error) => { - log.warn('[%s] reverse error %s: %s', name, network.url, error instanceof Error ? error.message : String(error)); - }); - } - - private async handleApiMessage(socket: WebSocket, role: WsRole, raw: Buffer | string): Promise { - if (role !== 'Api' && role !== 'Universal') return; - - const text = rawDataToString(raw); - if (!text) return; - - const response = await this.context.api.processRequest(text); - safeSend(socket, response); - } - - private sendBootstrapMetaEvents(socket: WebSocket, role: WsRole, options: EventReportOptions): void { - if (role !== 'Event' && role !== 'Universal') return; - - const events = [ - this.context.buildLifecycleEvent('connect'), - this.context.buildLifecycleEvent('enable'), - this.context.buildHeartbeatEvent(), - ]; - for (const event of events) { - const shaped = shapeEventForAdapter(event, options); - if (!shaped) continue; - safeSend(socket, JSON.stringify(shaped)); - } - } -} - -function classifyForwardRole(request: IncomingMessage): WsRole { - const path = parseRequestPath(request.url ?? '/'); - if (path.endsWith('/api')) return 'Api'; - if (path.endsWith('/event')) return 'Event'; - return 'Universal'; -} - -function parseRequestPath(urlValue: string): string { - try { - return new URL(urlValue, 'ws://127.0.0.1').pathname; - } catch { - return '/'; - } -} - -function normalizeWsPath(pathValue: string | undefined): string { - const path = (pathValue ?? '/').trim() || '/'; - if (path === '/') return '/'; - return path.endsWith('/') ? path.slice(0, -1) : path; -} - -function activeServers(config: OneBotConfig): WsServerNetwork[] { - return config.networks.wsServers.filter((n) => n.enabled !== false); -} - -function activeClients(config: OneBotConfig): WsClientNetwork[] { - return config.networks.wsClients.filter((n) => n.enabled !== false && !!n.url); -} - -function wsServerSignature(network: WsServerNetwork): string { - return `${network.host ?? '0.0.0.0'}:${network.port}${normalizeWsPath(network.path)}#${network.role ?? 'auto'}#${network.accessToken ?? ''}`; -} - -function wsClientSignature(network: WsClientNetwork): string { - const role = network.role ?? 'Universal'; - const reconnectIntervalMs = Math.max(1000, network.reconnectIntervalMs ?? 5000); - return `${network.url}#${role}#${reconnectIntervalMs}#${network.accessToken ?? ''}`; -} - -function isAuthorized(request: IncomingMessage, token: string): boolean { - if (!token) return true; - // 1. Header auth - const rawAuth = request.headers.authorization; - const auth = Array.isArray(rawAuth) ? rawAuth[0] : rawAuth ?? ''; - if (auth === `Bearer ${token}`) return true; - - // 2. Query string auth - try { - const url = new URL(request.url ?? '/', 'ws://127.0.0.1'); - if (url.searchParams.get('access_token') === token) { - return true; - } - } catch { } - - return false; -} - -function rawDataToString(raw: Buffer | string | ArrayBuffer | ArrayBufferView | Buffer[]): string { - if (typeof raw === 'string') return raw; - if (Buffer.isBuffer(raw)) return raw.toString('utf8'); - if (Array.isArray(raw)) return Buffer.concat(raw).toString('utf8'); - if (raw instanceof ArrayBuffer) return Buffer.from(new Uint8Array(raw)).toString('utf8'); - if (ArrayBuffer.isView(raw)) { - return Buffer.from(raw.buffer, raw.byteOffset, raw.byteLength).toString('utf8'); - } - return ''; -} - -function safeSend(socket: WebSocket, payload: string): void { - if (socket.readyState !== WebSocket.OPEN) return; - socket.send(payload, (error?: Error | null) => { - if (error) { - log.warn('send error: %s', error instanceof Error ? error.message : String(error)); - } - }); -} - -function safeClose(socket: WebSocket, code = 1000, reason = 'normal'): void { - if (socket.readyState === WebSocket.CLOSED || socket.readyState === WebSocket.CLOSING) return; - socket.close(code, reason); -} diff --git a/packages/core/tests/bridge-event-bus.test.ts b/packages/core/tests/bridge-event-bus.test.ts new file mode 100644 index 00000000..576bb40a --- /dev/null +++ b/packages/core/tests/bridge-event-bus.test.ts @@ -0,0 +1,135 @@ +import { describe, expect, it, vi } from 'vitest'; +import { BridgeEventBus } from '../src/bridge/event-bus'; +import type { FriendMessage, GroupMessage } from '../src/bridge/events'; + +function makeFriendMessage(): FriendMessage { + return { + kind: 'friend_message', + time: 1700000000, + selfUin: 10001, + senderUin: 22222, + senderNick: 'peer', + msgSeq: 1, + msgId: 1, + elements: [{ type: 'text', text: 'hi' }], + }; +} + +function makeGroupMessage(): GroupMessage { + return { + kind: 'group_message', + time: 1700000000, + selfUin: 10001, + groupId: 99999, + senderUin: 22222, + senderNick: 'peer', + senderCard: '', + senderRole: 'member', + msgSeq: 2, + msgId: 2, + elements: [{ type: 'text', text: 'hi' }], + }; +} + +describe('BridgeEventBus', () => { + it('routes events only to subscribers of the matching kind', async () => { + const bus = new BridgeEventBus(); + const friend = vi.fn(); + const group = vi.fn(); + bus.on('friend_message', friend); + bus.on('group_message', group); + + await bus.emit(makeFriendMessage()); + await bus.emit(makeGroupMessage()); + + expect(friend).toHaveBeenCalledTimes(1); + expect(group).toHaveBeenCalledTimes(1); + }); + + it('runs every subscriber in parallel for one kind', async () => { + const bus = new BridgeEventBus(); + const order: string[] = []; + bus.on('friend_message', async () => { + await new Promise((r) => setTimeout(r, 20)); + order.push('slow'); + }); + bus.on('friend_message', () => { + order.push('fast'); + }); + + await bus.emit(makeFriendMessage()); + // The synchronous handler must finish before the async one — proves we + // don't serialize handlers across the kind. + expect(order).toEqual(['fast', 'slow']); + }); + + it('isolates handler errors and reports them via onError', async () => { + const seen: Array<{ kind: string; err: unknown }> = []; + const bus = new BridgeEventBus({ onError: (kind, err) => seen.push({ kind, err }) }); + const ok = vi.fn(); + bus.on('friend_message', () => { + throw new Error('boom'); + }); + bus.on('friend_message', ok); + + await bus.emit(makeFriendMessage()); + + expect(ok).toHaveBeenCalledTimes(1); + expect(seen).toHaveLength(1); + expect(seen[0].kind).toBe('friend_message'); + expect((seen[0].err as Error).message).toBe('boom'); + }); + + it('supports unsubscribe via the returned disposer', async () => { + const bus = new BridgeEventBus(); + const handler = vi.fn(); + const off = bus.on('friend_message', handler); + + await bus.emit(makeFriendMessage()); + expect(handler).toHaveBeenCalledTimes(1); + + off(); + await bus.emit(makeFriendMessage()); + expect(handler).toHaveBeenCalledTimes(1); + }); + + it('supports onAny and isolates its errors separately', async () => { + const seen: Array<{ kind: string; err: unknown }> = []; + const bus = new BridgeEventBus({ onError: (kind, err) => seen.push({ kind, err }) }); + const tap = vi.fn(); + bus.onAny(tap); + bus.onAny(() => { throw new Error('any-fail'); }); + + await bus.emit(makeFriendMessage()); + await bus.emit(makeGroupMessage()); + + expect(tap).toHaveBeenCalledTimes(2); + expect(seen).toHaveLength(2); + expect(seen.every((s) => s.kind === '*')).toBe(true); + }); + + it('reports has() based on per-kind + onAny subscriptions', () => { + const bus = new BridgeEventBus(); + expect(bus.has('friend_message')).toBe(false); + + const off = bus.on('friend_message', () => {}); + expect(bus.has('friend_message')).toBe(true); + off(); + expect(bus.has('friend_message')).toBe(false); + + bus.onAny(() => {}); + expect(bus.has('friend_message')).toBe(true); + }); + + it('clear() drops every subscriber', async () => { + const bus = new BridgeEventBus(); + const handler = vi.fn(); + bus.on('friend_message', handler); + bus.onAny(handler); + + bus.clear(); + await bus.emit(makeFriendMessage()); + + expect(handler).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/core/tests/event-pipeline.test.ts b/packages/core/tests/event-pipeline.test.ts new file mode 100644 index 00000000..a2f16106 --- /dev/null +++ b/packages/core/tests/event-pipeline.test.ts @@ -0,0 +1,193 @@ +import { describe, expect, it, vi } from 'vitest'; +import { BridgeEventBus } from '../src/bridge/event-bus'; +import { EventConverter } from '../src/onebot/event-converter'; +import { registerEventPipeline } from '../src/onebot/event-pipeline'; +import type { OneBotInstanceContext } from '../src/onebot/instance-context'; +import type { + FriendMessage, + GroupMessage, + GroupMemberJoin, + TempMessage, + QQEventVariant, +} from '../src/bridge/events'; +import type { JsonObject, MessageMeta } from '../src/onebot/types'; + +const SELF_UIN = '10001'; +const SELF_ID = 10001; +const PEER_UIN = 22222; +const GROUP_ID = 99999; + +interface FakeBridge { + events: BridgeEventBus; +} + +function makeFriendMessage(): FriendMessage { + return { + kind: 'friend_message', + time: 1700000000, + selfUin: SELF_ID, + senderUin: PEER_UIN, + senderNick: 'peer', + msgSeq: 11, + msgId: 555, + elements: [{ type: 'text', text: 'hi' }], + }; +} + +function makeGroupMessage(): GroupMessage { + return { + kind: 'group_message', + time: 1700000000, + selfUin: SELF_ID, + groupId: GROUP_ID, + senderUin: PEER_UIN, + senderNick: 'peer', + senderCard: '', + senderRole: 'member', + msgSeq: 22, + msgId: 777, + elements: [{ type: 'text', text: 'group' }], + }; +} + +function makeTempMessage(): TempMessage { + return { + kind: 'temp_message', + time: 1700000000, + selfUin: SELF_ID, + senderUin: PEER_UIN, + groupId: GROUP_ID, + senderNick: 'peer', + msgSeq: 33, + elements: [{ type: 'text', text: 'temp' }], + }; +} + +function makeMemberJoin(): GroupMemberJoin { + return { + kind: 'group_member_join', + time: 1700000000, + selfUin: SELF_ID, + groupId: GROUP_ID, + userUin: PEER_UIN, + operatorUin: PEER_UIN, + }; +} + +function makeContext(extra: Partial = {}): { + ctx: OneBotInstanceContext; + bus: BridgeEventBus; + metaCalls: Array<{ id: number; meta: MessageMeta }>; + dispatchCalls: JsonObject[]; +} { + const bus = new BridgeEventBus(); + const fakeBridge: FakeBridge = { events: bus }; + const eventConverter = new EventConverter(); + const metaCalls: Array<{ id: number; meta: MessageMeta }> = []; + const dispatchCalls: JsonObject[] = []; + + const ctx: OneBotInstanceContext = { + uin: SELF_UIN, + selfId: SELF_ID, + qqInfo: {} as never, + bridge: fakeBridge as never, + messageStore: {} as never, + mediaStore: {} as never, + eventConverter, + config: { networks: { httpServers: [], httpClients: [], wsServers: [], wsClients: [] } } as never, + cacheMessageMeta: (id, meta) => { metaCalls.push({ id, meta }); }, + dispatchEvent: (event) => { dispatchCalls.push(event); }, + ...extra, + }; + + return { ctx, bus, metaCalls, dispatchCalls }; +} + +describe('registerEventPipeline', () => { + it('caches meta and dispatches a converted event for friend_message', async () => { + const { ctx, bus, metaCalls, dispatchCalls } = makeContext(); + registerEventPipeline(ctx); + + await bus.emit(makeFriendMessage()); + + expect(metaCalls).toHaveLength(1); + expect(metaCalls[0].meta.isGroup).toBe(false); + expect(metaCalls[0].meta.targetId).toBe(PEER_UIN); + expect(metaCalls[0].meta.sequence).toBe(11); + expect(metaCalls[0].meta.random).toBe(555); + + expect(dispatchCalls).toHaveLength(1); + expect(dispatchCalls[0].post_type).toBe('message'); + expect(dispatchCalls[0].message_type).toBe('private'); + }); + + it('caches meta and dispatches for group_message', async () => { + const { ctx, bus, metaCalls, dispatchCalls } = makeContext(); + registerEventPipeline(ctx); + + await bus.emit(makeGroupMessage()); + + expect(metaCalls[0].meta.isGroup).toBe(true); + expect(metaCalls[0].meta.targetId).toBe(GROUP_ID); + expect(dispatchCalls[0].message_type).toBe('group'); + }); + + it('caches meta with random=0 for temp_message and dispatches as private+sub_type=group', async () => { + const { ctx, bus, metaCalls, dispatchCalls } = makeContext(); + registerEventPipeline(ctx); + + await bus.emit(makeTempMessage()); + + expect(metaCalls[0].meta.isGroup).toBe(false); + expect(metaCalls[0].meta.random).toBe(0); + expect(dispatchCalls[0].message_type).toBe('private'); + expect(dispatchCalls[0].sub_type).toBe('group'); + }); + + it('dispatches notice events without seeding meta', async () => { + const { ctx, bus, metaCalls, dispatchCalls } = makeContext(); + registerEventPipeline(ctx); + + await bus.emit(makeMemberJoin()); + + expect(metaCalls).toHaveLength(0); + expect(dispatchCalls).toHaveLength(1); + expect(dispatchCalls[0].notice_type).toBe('group_increase'); + }); + + it('returns a disposer that fully unsubscribes', async () => { + const { ctx, bus, metaCalls, dispatchCalls } = makeContext(); + const dispose = registerEventPipeline(ctx); + + await bus.emit(makeFriendMessage()); + expect(dispatchCalls).toHaveLength(1); + + dispose(); + await bus.emit(makeFriendMessage()); + // No new calls after dispose. + expect(dispatchCalls).toHaveLength(1); + expect(metaCalls).toHaveLength(1); + }); + + it('dispatches a separate event for every kind in parallel', async () => { + const { ctx, bus, dispatchCalls } = makeContext(); + registerEventPipeline(ctx); + + const events: QQEventVariant[] = [makeFriendMessage(), makeGroupMessage(), makeMemberJoin(), makeTempMessage()]; + await Promise.all(events.map((e) => bus.emit(e))); + + expect(dispatchCalls).toHaveLength(events.length); + }); + + it('skips dispatch for kinds without a converter mapping (no crash)', async () => { + // Stub `convert` on a real EventConverter instance so the type stays + // honest -- no structural casts. + const stubbed = new EventConverter(); + stubbed.convert = vi.fn().mockResolvedValue(null); + const { ctx, bus, dispatchCalls } = makeContext({ eventConverter: stubbed }); + registerEventPipeline(ctx); + + await bus.emit(makeFriendMessage()); + expect(dispatchCalls).toHaveLength(0); + }); +}); diff --git a/packages/core/tests/media-cache.test.ts b/packages/core/tests/media-store.test.ts similarity index 58% rename from packages/core/tests/media-cache.test.ts rename to packages/core/tests/media-store.test.ts index 63b30eb1..b87bc896 100644 --- a/packages/core/tests/media-cache.test.ts +++ b/packages/core/tests/media-store.test.ts @@ -1,5 +1,7 @@ -import { describe, expect, it } from 'vitest'; -import { MediaCache } from '../src/onebot/media-cache'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import fs from 'fs'; +import path from 'path'; +import { MediaStore } from '../src/onebot/media-store'; import { EventConverter } from '../src/onebot/event-converter'; import type { GroupMessage, FriendMessage, MessageElement } from '../src/bridge/events'; @@ -60,10 +62,34 @@ function makeFriendMessage(elements: MessageElement[]): FriendMessage { }; } -describe('MediaCache basic semantics', () => { +function tempDbPath(label: string): string { + return path.join('data', 'test', `media-${label}-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}.db`); +} + +describe('MediaStore basic semantics', () => { + const dbs: string[] = []; + + beforeEach(() => { + dbs.length = 0; + }); + + afterEach(() => { + for (const dbPath of dbs) { + for (const ext of ['', '-wal', '-shm']) { + try { fs.unlinkSync(dbPath + ext); } catch { /* ignore */ } + } + } + }); + + function open(label: string, max?: number): MediaStore { + const dbPath = tempDbPath(label); + dbs.push(dbPath); + return max !== undefined ? new MediaStore(dbPath, max) : new MediaStore(dbPath); + } + it('indexes images by file, fileName, and url', () => { - const cache = new MediaCache(); - cache.rememberImage({ + const store = open('img-keys'); + store.rememberImage({ file: 'abc.png', url: 'https://example.com/abc.png', fileSize: 4242, @@ -75,15 +101,16 @@ describe('MediaCache basic semantics', () => { sessionId: GROUP_ID, }); - expect(cache.findImage('abc.png')?.fileSize).toBe(4242); - expect(cache.findImage('https://example.com/abc.png')?.url).toBe('https://example.com/abc.png'); - expect(cache.findImage('missing.png')).toBeNull(); - expect(cache.findImage('')).toBeNull(); + expect(store.findImage('abc.png')?.fileSize).toBe(4242); + expect(store.findImage('https://example.com/abc.png')?.url).toBe('https://example.com/abc.png'); + expect(store.findImage('missing.png')).toBeNull(); + expect(store.findImage('')).toBeNull(); + store.close(); }); it('indexes records by file, fileName, fileId, and url', () => { - const cache = new MediaCache(); - cache.rememberRecord({ + const store = open('rec-keys'); + store.rememberRecord({ file: 'silk_test.amr', fileId: 'uuid-base64-fake', url: 'https://example.com/voice.amr', @@ -96,15 +123,16 @@ describe('MediaCache basic semantics', () => { sessionId: PEER_UIN, }); - expect(cache.findRecord('silk_test.amr')?.duration).toBe(3); - expect(cache.findRecord('uuid-base64-fake')?.fileId).toBe('uuid-base64-fake'); - expect(cache.findRecord('https://example.com/voice.amr')?.url).toBe('https://example.com/voice.amr'); - expect(cache.findRecord('nope')).toBeNull(); + expect(store.findRecord('silk_test.amr')?.duration).toBe(3); + expect(store.findRecord('uuid-base64-fake')?.fileId).toBe('uuid-base64-fake'); + expect(store.findRecord('https://example.com/voice.amr')?.url).toBe('https://example.com/voice.amr'); + expect(store.findRecord('nope')).toBeNull(); + store.close(); }); - it('updates URLs in place across all index entries', () => { - const cache = new MediaCache(); - cache.rememberRecord({ + it('updates URLs in place across all alias keys', () => { + const store = open('url-update'); + store.rememberRecord({ file: 'silk_test.amr', fileId: 'uuid-base64-fake', url: '', @@ -116,16 +144,51 @@ describe('MediaCache basic semantics', () => { isGroup: false, sessionId: PEER_UIN, }); - cache.updateRecordUrl('silk_test.amr', 'https://refreshed.example.com/voice.amr'); - expect(cache.findRecord('silk_test.amr')?.url).toBe('https://refreshed.example.com/voice.amr'); - // Lookup by fileId should also see the refreshed url since it shares the entry. - expect(cache.findRecord('uuid-base64-fake')?.url).toBe('https://refreshed.example.com/voice.amr'); + store.updateRecordUrl('silk_test.amr', 'https://refreshed.example.com/voice.amr'); + + expect(store.findRecord('silk_test.amr')?.url).toBe('https://refreshed.example.com/voice.amr'); + expect(store.findRecord('uuid-base64-fake')?.url).toBe('https://refreshed.example.com/voice.amr'); + store.close(); }); - it('evicts oldest entries when bound is exceeded', () => { - const cache = new MediaCache(33); - for (let i = 0; i < 100; i++) { - cache.rememberImage({ + it('survives close+reopen against the same db path', () => { + const dbPath = tempDbPath('persist'); + dbs.push(dbPath); + + { + const store = new MediaStore(dbPath); + store.rememberImage({ + file: 'abc.png', + url: 'https://example.com/abc.png', + fileSize: 99, + fileName: 'abc.png', + subType: 0, + summary: '', + imageUrl: 'https://example.com/abc.png', + isGroup: true, + sessionId: GROUP_ID, + }); + store.close(); + } + + { + const store = new MediaStore(dbPath); + const found = store.findImage('abc.png'); + expect(found).not.toBeNull(); + expect(found!.fileSize).toBe(99); + // Alias index must also persist. + expect(store.findImage('https://example.com/abc.png')?.fileSize).toBe(99); + store.close(); + } + }); + + it('evicts old entries beyond the configured cap', () => { + // EVICT_EVERY_N_REMEMBERS = 64 inside the store; we exercise the eviction + // path by writing well past the cap so it triggers at least once. + const cap = 80; + const store = open('evict', cap); + for (let i = 0; i < 200; i++) { + store.rememberImage({ file: `file-${i}.png`, url: `https://example.com/${i}.png`, fileSize: i, @@ -137,15 +200,26 @@ describe('MediaCache basic semantics', () => { sessionId: GROUP_ID, }); } - // Earliest entry should have been evicted (each item adds 2 keys: file + url), - // so the bound is reached well before 100 distinct entries. - expect(cache.findImage('file-0.png')).toBeNull(); - // Last inserted entry must still be present. - expect(cache.findImage('file-99.png')?.fileSize).toBe(99); + const { images } = store.size(); + expect(images).toBeLessThanOrEqual(cap); + // Most recent entries should still be present. + expect(store.findImage('file-199.png')?.fileSize).toBe(199); + store.close(); }); }); -describe('EventConverter media segment sink', () => { +describe('EventConverter media segment sink → MediaStore wiring', () => { + const dbs: string[] = []; + + afterEach(() => { + for (const dbPath of dbs) { + for (const ext of ['', '-wal', '-shm']) { + try { fs.unlinkSync(dbPath + ext); } catch { /* ignore */ } + } + } + dbs.length = 0; + }); + it('emits sink invocations for image segments with the right context', async () => { const conv = new EventConverter(); const sinkCalls: Array<{ type: string; isGroup: boolean; sessionId: number; file: string }> = []; @@ -162,16 +236,19 @@ describe('EventConverter media segment sink', () => { ]); }); - it('lets the sink populate a MediaCache that get_image-style lookups can use', async () => { + it('lets the sink populate a MediaStore that get_image-style lookups can use', async () => { + const dbPath = tempDbPath('sink'); + dbs.push(dbPath); + + const store = new MediaStore(dbPath); const conv = new EventConverter(); - const cache = new MediaCache(); conv.setImageUrlResolver((element) => element.imageUrl ?? ''); conv.setMediaUrlResolver(async (element) => element.url ?? ''); conv.setMediaSegmentSink((type, element, data, isGroup, sessionId) => { const url = typeof data.url === 'string' ? data.url : ''; const file = typeof data.file === 'string' ? data.file : ''; if (type === 'image') { - cache.rememberImage({ + store.rememberImage({ file: file || element.fileId || '', url, fileSize: element.fileSize ?? 0, @@ -183,7 +260,7 @@ describe('EventConverter media segment sink', () => { sessionId, }); } else { - cache.rememberRecord({ + store.rememberRecord({ file: file || element.fileName || element.fileId || '', fileId: element.fileId ?? '', url, @@ -201,22 +278,22 @@ describe('EventConverter media segment sink', () => { await conv.convert(SELF_UIN, makeGroupMessage([imageElement()])); await conv.convert(SELF_UIN, makeFriendMessage([recordElement()])); - const img = cache.findImage('abc.png'); + const img = store.findImage('abc.png'); expect(img).not.toBeNull(); expect(img!.url).toBe('https://example.com/abc.png'); expect(img!.fileSize).toBe(4242); expect(img!.isGroup).toBe(true); expect(img!.sessionId).toBe(GROUP_ID); - const rec = cache.findRecord('silk_test.amr'); + const rec = store.findRecord('silk_test.amr'); expect(rec).not.toBeNull(); expect(rec!.fileId).toBe('uuid-base64-fake'); expect(rec!.duration).toBe(3); expect(rec!.isGroup).toBe(false); expect(rec!.sessionId).toBe(PEER_UIN); - // Should also be findable by the raw fileUuid, mirroring how callers may - // pass back any of the identifiers we previously emitted. - expect(cache.findRecord('uuid-base64-fake')?.fileName).toBe('silk_test.amr'); + // Alias lookup by fileUuid still works. + expect(store.findRecord('uuid-base64-fake')?.fileName).toBe('silk_test.amr'); + store.close(); }); it('does not invoke the sink when no media segments are present', async () => { diff --git a/packages/core/tests/network-manager.test.ts b/packages/core/tests/network-manager.test.ts new file mode 100644 index 00000000..401a8582 --- /dev/null +++ b/packages/core/tests/network-manager.test.ts @@ -0,0 +1,180 @@ +import { describe, expect, it } from 'vitest'; +import { IOneBotNetworkAdapter, NetworkReloadType, OneBotNetworkManager } from '../src/onebot/network'; +import type { NetworkAdapterContext } from '../src/onebot/network'; +import type { JsonObject, NetworkBase } from '../src/onebot/types'; +import type { DispatchPayload } from '../src/onebot/event-filter'; + +interface FakeNetworkConfig extends NetworkBase { + // Allows tests to influence behavior via config: + failOnEvent?: boolean; +} + +class FakeAdapter extends IOneBotNetworkAdapter { + events: JsonObject[] = []; + opens = 0; + closes = 0; + reloads = 0; + // Last dispatch payload object identity, to assert it was reused per emit. + lastPayloads: DispatchPayload[] = []; + + open(): void { + this.opens++; + if (this.config.enabled !== false) this.isEnabled = true; + } + close(): void { + this.closes++; + this.isEnabled = false; + } + reload(next: FakeNetworkConfig): NetworkReloadType { + this.reloads++; + this.config = structuredClone(next); + if (next.enabled === false && this.isEnabled) { + this.close(); + return NetworkReloadType.Closed; + } + if (next.enabled !== false && !this.isEnabled) { + this.open(); + return NetworkReloadType.Opened; + } + return NetworkReloadType.Normal; + } + onEvent(event: JsonObject, payload: DispatchPayload): void { + this.events.push(event); + this.lastPayloads.push(payload); + if (this.config.failOnEvent) throw new Error('boom'); + } +} + +const NULL_CTX: NetworkAdapterContext = { + uin: '10001', + api: { handle: async () => ({ status: 'failed', retcode: 0, data: null }), processRequest: async () => '' } as never, + buildLifecycleEvent: () => ({}), + buildHeartbeatEvent: () => ({}), +}; + +function makeAdapter(name: string, partial: Partial = {}): FakeAdapter { + const config: FakeNetworkConfig = { + name, + enabled: true, + messageFormat: 'array', + reportSelfMessage: false, + ...partial, + }; + return new FakeAdapter(name, config, NULL_CTX); +} + +const SAMPLE_EVENT: JsonObject = { + time: 1, + self_id: 10001, + post_type: 'message', + message_type: 'private', + sub_type: 'friend', + message_id: 1, + user_id: 22222, + message: [{ type: 'text', data: { text: 'hi' } }], + raw_message: 'hi', + font: 0, + sender: { user_id: 22222, nickname: 'peer', sex: 'unknown', age: 0 }, +}; + +describe('OneBotNetworkManager', () => { + it('opens and emits to every active adapter in parallel', async () => { + const mgr = new OneBotNetworkManager(); + const a = makeAdapter('a'); + const b = makeAdapter('b'); + mgr.register(a); + mgr.register(b); + await mgr.openAll(); + + expect(a.opens).toBe(1); + expect(b.opens).toBe(1); + + await mgr.emitEvent(SAMPLE_EVENT); + expect(a.events).toEqual([SAMPLE_EVENT]); + expect(b.events).toEqual([SAMPLE_EVENT]); + // Both adapters should receive the same payload reference (built once). + expect(a.lastPayloads[0]).toBe(b.lastPayloads[0]); + }); + + it('skips inactive adapters during emit', async () => { + const mgr = new OneBotNetworkManager(); + const active = makeAdapter('active'); + const idle = makeAdapter('idle', { enabled: false }); + mgr.register(active); + mgr.register(idle); + await mgr.openAll(); + + await mgr.emitEvent(SAMPLE_EVENT); + expect(active.events).toHaveLength(1); + expect(idle.events).toHaveLength(0); + }); + + it('isolates adapter errors so one bad adapter does not block others', async () => { + const mgr = new OneBotNetworkManager(); + const ok = makeAdapter('ok'); + const bad = makeAdapter('bad', { failOnEvent: true }); + mgr.register(ok); + mgr.register(bad); + await mgr.openAll(); + + await mgr.emitEvent(SAMPLE_EVENT); + // The good adapter still saw the event. + expect(ok.events).toHaveLength(1); + // The bad adapter logged the throw but didn't crash the manager. + expect(bad.events).toHaveLength(1); + }); + + it('replaces an existing adapter under the same name and closes the old one', async () => { + const mgr = new OneBotNetworkManager(); + const first = makeAdapter('dup'); + mgr.register(first); + await mgr.openAll(); + + const second = makeAdapter('dup'); + mgr.register(second); + + expect(mgr.get('dup')).toBe(second); + expect(first.closes).toBe(1); + }); + + it('closeOne shuts down a single adapter and removes it', async () => { + const mgr = new OneBotNetworkManager(); + const a = makeAdapter('a'); + const b = makeAdapter('b'); + mgr.register(a); + mgr.register(b); + await mgr.openAll(); + + await mgr.closeOne('a'); + expect(mgr.has('a')).toBe(false); + expect(a.closes).toBe(1); + expect(b.closes).toBe(0); + expect(mgr.hasActiveAdapters()).toBe(true); + }); + + it('closeAll clears every adapter', async () => { + const mgr = new OneBotNetworkManager(); + const a = makeAdapter('a'); + const b = makeAdapter('b'); + mgr.register(a); + mgr.register(b); + await mgr.openAll(); + + await mgr.closeAll(); + expect(mgr.list()).toHaveLength(0); + expect(a.closes).toBe(1); + expect(b.closes).toBe(1); + }); + + it('hasActiveAdapters reflects current state', async () => { + const mgr = new OneBotNetworkManager(); + expect(mgr.hasActiveAdapters()).toBe(false); + const a = makeAdapter('a'); + mgr.register(a); + expect(mgr.hasActiveAdapters()).toBe(false); + await mgr.openAll(); + expect(mgr.hasActiveAdapters()).toBe(true); + await mgr.closeAll(); + expect(mgr.hasActiveAdapters()).toBe(false); + }); +});