Skip to content

Commit

Permalink
Update SSE respones to have 2 newlines per SSE spec. (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
taeold authored Jan 23, 2025
1 parent c3d35c8 commit 01c1198
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
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

0 comments on commit 01c1198

Please sign in to comment.