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

Update SSE respones to have 2 newlines per SSE spec #1663

Merged
merged 1 commit into from
Jan 23, 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
12 changes: 6 additions & 6 deletions spec/common/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ describe("onCallHandler", () => {

const resp = await runHandler(fn, mockReq as any);
const data = [`data: {"message":"hello"}`, `data: {"result":"world"}`];
expect(resp.body).to.equal([...data, ""].join("\n"));
expect(resp.body).to.equal([...data, ""].join("\n\n"));
});

it("returns error in SSE format", async () => {
Expand All @@ -800,7 +800,7 @@ describe("onCallHandler", () => {

const resp = await runHandler(fn, mockReq as any);
const data = [`data: {"error":{"message":"INTERNAL","status":"INTERNAL"}}`];
expect(resp.body).to.equal([...data, ""].join("\n"));
expect(resp.body).to.equal([...data, ""].join("\n\n"));
});

it("always returns error for v1 callables", async () => {
Expand Down Expand Up @@ -851,7 +851,7 @@ describe("onCallHandler", () => {

const resp = await runHandler(fn, mockReq);

expect(resp.body).to.equal(`data: {"message":"initial message"}\n`);
expect(resp.body).to.equal(`data: {"message":"initial message"}\n\n`);
});

describe("Heartbeats", () => {
Expand Down Expand Up @@ -890,7 +890,7 @@ describe("onCallHandler", () => {
await clock.tickAsync(11_000);
const resp = await handlerPromise;
const data = [": ping", ": ping", `data: {"result":"done"}`];
expect(resp.body).to.equal([...data, ""].join("\n"));
expect(resp.body).to.equal([...data, ""].join("\n\n"));
});

it("doesn't send heartbeat messages if user writes data", async () => {
Expand Down Expand Up @@ -919,7 +919,7 @@ describe("onCallHandler", () => {
await clock.tickAsync(10_000);
const resp = await handlerPromise;
const data = [`data: {"message":"hello"}`, `data: {"result":"done"}`];
expect(resp.body).to.equal([...data, ""].join("\n"));
expect(resp.body).to.equal([...data, ""].join("\n\n"));
});

it("respects null heartbeatSeconds option", async () => {
Expand All @@ -945,7 +945,7 @@ describe("onCallHandler", () => {
const handlerPromise = runHandler(fn, mockReq as any);
await clock.tickAsync(31_000);
const resp = await handlerPromise;
expect(resp.body).to.equal('data: {"result":"done"}\n');
expect(resp.body).to.equal('data: {"result":"done"}\n\n');
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ export function onCallHandler<Req = any, Res = any, Stream = unknown>(
}

function encodeSSE(data: unknown): string {
return `data: ${JSON.stringify(data)}\n`;
return `data: ${JSON.stringify(data)}\n\n`;
}

/** @internal */
Expand Down Expand Up @@ -766,7 +766,7 @@ function wrapOnCallHandler<Req = any, Res = any, Stream = unknown>(
if (!abortController.signal.aborted) {
heartbeatInterval = setTimeout(() => {
if (!abortController.signal.aborted) {
res.write(": ping\n");
res.write(": ping\n\n");
scheduleHeartbeat();
}
}, heartbeatSeconds * 1000);
Expand Down
Loading