refactor(onebot): lift bootstrap-meta framing into the network adapter base

Phase 1. The connect/enable/heartbeat bootstrap triplet was built +
shaped + stringified + null-dropped identically in WsServerAdapter and
WsClientAdapter. Lift that into IOneBotNetworkAdapter:

- metaFrame(event, options): string|null  — shape via shapeEventForAdapter
  then JSON.stringify, or null when the shaper drops it
- bootstrapMetaFrames(options): string[]  — the 3 bootstrap events,
  framed and null-filtered

Both ws adapters' sendBootstrapMetaEvents become one-liners over
bootstrapMetaFrames (ws-client keeps its role guard at the call site);
ws-server's close() disable broadcast reuses metaFrame. Base now owns
the shape+serialize policy, adapters own only the transport — same
shape as ADR-0009's reload template method.

Behavior-preserving (adversarially verified byte-for-byte); the two ws
adapters drop their now-unused shapeEventForAdapter import. Safety net
(network-bootstrap-meta, 9 tests) green.
This commit is contained in:
DrSmoothl committed 2026-06-24 23:40:31 +08:00
1 parent e11601bfec
commit 3a4af38842
3 files changed
+38 -26

No files matched your search

+34 -1
View File
@@ -1,6 +1,6 @@
import { createLogger, type Logger } from '@snowluma/common/logger';
import type { ApiHandler } from '../api-handler';
import type { DispatchPayload } from '../event-filter';
import { shapeEventForAdapter, type DispatchPayload, type EventReportOptions } from '../event-filter';
import type { JsonObject, NetworkBase } from '../types';
export interface NetworkAdapterContext {
@@ -58,6 +58,39 @@ export abstract class IOneBotNetworkAdapter<C extends NetworkBase> {
/** Report live connection health for the WebUI dashboard. */
abstract describeStatus(): AdapterStatus;
// ── meta-event framing: shared by the connection-oriented adapters ─────
//
// Lifecycle (connect/enable/disable) and heartbeat meta events don't flow
// through `OneBotNetworkManager.emitEvent` (no pre-serialized DispatchPayload),
// so each connection-oriented adapter shapes them per its report options and
// serializes on the spot. The shape+serialize policy lives here once; the
// adapters only own the transport (which socket(s) to `safeSend` to).
/** Shape one meta event for `options` and serialize it to a wire frame, or
* `null` when the shaper drops it (e.g. a self-message under
* `reportSelfMessage:false`). */
protected metaFrame(event: JsonObject, options: EventReportOptions): string | null {
const shaped = shapeEventForAdapter(event, options);
return shaped === null ? null : JSON.stringify(shaped);
}
/** The bootstrap meta frames an event client receives on (re)connect:
* `connect`, `enable`, then a `heartbeat`, shaped for `options`. Frames the
* shaper drops are omitted. */
protected bootstrapMetaFrames(options: EventReportOptions): string[] {
const events = [
this.ctx.buildLifecycleEvent('connect'),
this.ctx.buildLifecycleEvent('enable'),
this.ctx.buildHeartbeatEvent(),
];
const frames: string[] = [];
for (const event of events) {
const frame = this.metaFrame(event, options);
if (frame !== null) frames.push(frame);
}
return frames;
}
// ── reload: shared hot-reload state machine (template method) ──────────
//
// All four adapters reconcile a live config swap identically: decide
@@ -3,7 +3,6 @@ import { createLogger } from '@snowluma/common/logger';
import {
pickDispatchJson,
resolveReportOptions,
shapeEventForAdapter,
type DispatchPayload,
type EventReportOptions,
} from '../event-filter';
@@ -153,16 +152,7 @@ export class WsClientAdapter extends IOneBotNetworkAdapter<WsClientNetwork> {
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));
}
for (const frame of this.bootstrapMetaFrames(this.options)) safeSend(socket, frame);
}
}
@@ -4,7 +4,6 @@ import { createLogger } from '@snowluma/common/logger';
import {
pickDispatchJson,
resolveReportOptions,
shapeEventForAdapter,
type DispatchPayload,
type EventReportOptions,
} from '../event-filter';
@@ -45,9 +44,8 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
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));
const frame = this.metaFrame(lifecycle, conn.options);
if (frame) safeSend(conn.socket, frame);
}
this.isEnabled = false;
@@ -142,16 +140,7 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
}
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));
}
for (const frame of this.bootstrapMetaFrames(this.options)) safeSend(socket, frame);
}
}