test(webui): extract + test debug SSE backpressure framing
Adversarial Wave A3 flagged the hand-rolled SSE drop/marker logic as the
untested weak point. Extract createFramePusher (drop under desiredSize<=0,
coalesced {kind:'dropped'} marker before the next delivered frame, null = no
backpressure) and unit-test it; the stream handler now uses it. Non-blocking
hardening — no behavior change.
This commit is contained in:
3 files changed
+88
-8
No files matched your search
@@ -0,0 +1,28 @@
|
||||
// SSE frame pusher for the debug stream (Wave A3). Drops frames under
|
||||
// backpressure (consumer slow → desiredSize <= 0) instead of buffering
|
||||
// unbounded into memory, and coalesces the drop count into a {kind:'dropped'}
|
||||
// marker emitted just before the next delivered frame. Extracted from the route
|
||||
// handler so this (easy-to-get-wrong) logic is unit-tested.
|
||||
|
||||
export interface FramePusherOptions {
|
||||
/** The stream controller's current desiredSize (null = no backpressure info). */
|
||||
desiredSize: () => number | null;
|
||||
/** Deliver an encoded SSE chunk (the caller maps this to the controller). */
|
||||
enqueue: (chunk: Uint8Array) => void;
|
||||
encode?: (s: string) => Uint8Array;
|
||||
}
|
||||
|
||||
/** Returns a `push(payload)` that frames + delivers with backpressure dropping. */
|
||||
export function createFramePusher(opts: FramePusherOptions): (payload: unknown) => void {
|
||||
const encode = opts.encode ?? ((s: string) => new TextEncoder().encode(s));
|
||||
let dropped = 0;
|
||||
return (payload: unknown): void => {
|
||||
const d = opts.desiredSize();
|
||||
if (d !== null && d <= 0) { dropped += 1; return; }
|
||||
if (dropped > 0) {
|
||||
opts.enqueue(encode(`data: ${JSON.stringify({ kind: 'dropped', count: dropped })}\n\n`));
|
||||
dropped = 0;
|
||||
}
|
||||
opts.enqueue(encode(`data: ${JSON.stringify(payload)}\n\n`));
|
||||
};
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import { resolveTlsContext, validateTlsPair } from './tls';
|
||||
import { coerceSettingsPatch } from './system-settings';
|
||||
import { buildBackup, planRestore, validateBackup } from './backup';
|
||||
import { collectActionDocs, collectCategories } from '@snowluma/onebot/action-docs';
|
||||
import { createFramePusher } from './debug-stream';
|
||||
import { describeTrustProxy, makeClientIpResolver, parseTrustProxy } from './client-ip';
|
||||
import { findAvailablePort } from './port';
|
||||
import {
|
||||
@@ -617,7 +618,6 @@ export async function initWebUI(
|
||||
start(controller) {
|
||||
const encoder = new TextEncoder();
|
||||
let closed = false;
|
||||
let dropped = 0;
|
||||
const offs: Array<() => void> = [];
|
||||
let heartbeat: NodeJS.Timeout | undefined;
|
||||
const teardown = () => {
|
||||
@@ -631,13 +631,13 @@ export async function initWebUI(
|
||||
if (closed) return;
|
||||
try { controller.enqueue(chunk); } catch { teardown(); }
|
||||
};
|
||||
const send = (payload: unknown) => {
|
||||
if (closed) return;
|
||||
// Drop under backpressure rather than buffer unbounded into memory.
|
||||
if (controller.desiredSize !== null && controller.desiredSize <= 0) { dropped += 1; return; }
|
||||
if (dropped > 0) { raw(encoder.encode(`data: ${JSON.stringify({ kind: 'dropped', count: dropped })}\n\n`)); dropped = 0; }
|
||||
raw(encoder.encode(`data: ${JSON.stringify(payload)}\n\n`));
|
||||
};
|
||||
// Drop-under-backpressure framing (unit-tested in debug-stream.ts).
|
||||
const pushFrame = createFramePusher({
|
||||
desiredSize: () => controller.desiredSize,
|
||||
enqueue: raw,
|
||||
encode: (s) => encoder.encode(s),
|
||||
});
|
||||
const send = (payload: unknown) => { if (!closed) pushFrame(payload); };
|
||||
send({ kind: 'ready' });
|
||||
for (const inst of oneBotManager.getInstances()) {
|
||||
const uin = inst.uin;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createFramePusher } from '../src/webui/debug-stream';
|
||||
|
||||
function harness(sizes: number[]) {
|
||||
// desiredSize() returns the next scripted value each call.
|
||||
let i = 0;
|
||||
const frames: string[] = [];
|
||||
const push = createFramePusher({
|
||||
desiredSize: () => (i < sizes.length ? sizes[i++] : sizes[sizes.length - 1] ?? 1),
|
||||
enqueue: (chunk) => { frames.push(Buffer.from(chunk).toString()); },
|
||||
encode: (s) => Buffer.from(s),
|
||||
});
|
||||
return { push, frames };
|
||||
}
|
||||
|
||||
describe('createFramePusher', () => {
|
||||
it('enqueues an SSE data frame when there is headroom', () => {
|
||||
const { push, frames } = harness([1]);
|
||||
push({ kind: 'event', x: 1 });
|
||||
expect(frames).toEqual([`data: ${JSON.stringify({ kind: 'event', x: 1 })}\n\n`]);
|
||||
});
|
||||
|
||||
it('drops under backpressure (desiredSize <= 0) without enqueuing', () => {
|
||||
const { push, frames } = harness([0, -3]);
|
||||
push({ a: 1 });
|
||||
push({ a: 2 });
|
||||
expect(frames).toEqual([]);
|
||||
});
|
||||
|
||||
it('emits a coalesced dropped marker before the next delivered frame', () => {
|
||||
const { push, frames } = harness([0, 0, 5]); // drop, drop, then headroom
|
||||
push({ a: 1 });
|
||||
push({ a: 2 });
|
||||
push({ a: 3 });
|
||||
expect(frames).toEqual([
|
||||
`data: ${JSON.stringify({ kind: 'dropped', count: 2 })}\n\n`,
|
||||
`data: ${JSON.stringify({ a: 3 })}\n\n`,
|
||||
]);
|
||||
});
|
||||
|
||||
it('treats null desiredSize as always-deliver (no backpressure signal)', () => {
|
||||
let i = 0;
|
||||
const frames: string[] = [];
|
||||
const push = createFramePusher({
|
||||
desiredSize: () => (i++ === 0 ? null : 1),
|
||||
enqueue: (chunk) => { frames.push(Buffer.from(chunk).toString()); },
|
||||
encode: (s) => Buffer.from(s),
|
||||
});
|
||||
push({ a: 1 });
|
||||
expect(frames).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user