Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom queuing strategy for event streams #442

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/server/event-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ describe(eventStream.name, () => {
expect(done).toBe(true);
});

test("accepts custom queuing strategy", () => {
expect(() =>
eventStream(
new AbortController().signal,
(_, abort) => {
return () => abort();
},
undefined,
new CountQueuingStrategy({ highWaterMark: 2 }),
),
).not.toThrow();
});

describe("Headers Overrides", () => {
test("overrrides Content-Type header", () => {
// biome-ignore lint/suspicious/noEmptyBlockStatements: Test
Expand Down
56 changes: 30 additions & 26 deletions src/server/event-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,45 @@ export function eventStream(
signal: AbortSignal,
init: InitFunction,
options: ResponseInit = {},
strategy?: QueuingStrategy,
) {
let stream = new ReadableStream({
start(controller) {
let encoder = new TextEncoder();
let closed = false;
let stream = new ReadableStream(
{
start(controller) {
let encoder = new TextEncoder();
let closed = false;

function send({ event = "message", data }: SendFunctionArgs) {
if (closed) return; // If already closed, not enqueue anything
controller.enqueue(encoder.encode(`event: ${event}\n`));

if (closed) return; // If already closed, not enqueue anything
function send({ event = "message", data }: SendFunctionArgs) {
if (closed) return; // If already closed, not enqueue anything
controller.enqueue(encoder.encode(`event: ${event}\n`));

data.split("\n").forEach((line, index, array) => {
if (closed) return; // If already closed, not enqueue anything
let value = `data: ${line}\n`;
if (index === array.length - 1) value += "\n";
controller.enqueue(encoder.encode(value));
});
}

let cleanup = init(send, close);
data.split("\n").forEach((line, index, array) => {
if (closed) return; // If already closed, not enqueue anything
let value = `data: ${line}\n`;
if (index === array.length - 1) value += "\n";
controller.enqueue(encoder.encode(value));
});
}

let cleanup = init(send, close);

function close() {
if (closed) return;
cleanup();
closed = true;
signal.removeEventListener("abort", close);
controller.close();
}
function close() {
if (closed) return;
cleanup();
closed = true;
signal.removeEventListener("abort", close);
controller.close();
}

signal.addEventListener("abort", close);
signal.addEventListener("abort", close);

if (signal.aborted) return close();
if (signal.aborted) return close();
},
},
});
strategy,
);

let headers = new Headers(options.headers);

Expand Down