This commit is contained in:
DrSmoothl committed 2026-10-12 15:43:34 +08:00
1 parent effeb66554
commit 7c0519f75f
977 files changed
+198426 -158324

No files matched your search

+25 -16
View File
@@ -5,24 +5,33 @@
// 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;
/** 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`));
};
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`));
};
}