Skip to content

Commit

Permalink
Remove support for streaming responses for v1 callables. (#1645)
Browse files Browse the repository at this point in the history
* Remove support for streaming responses for v1 callables.

* Complete implementations, add test.

* Fix formatting.
  • Loading branch information
taeold authored Nov 21, 2024
1 parent 648b922 commit 363c275
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions spec/common/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,31 @@ describe("onCallHandler", () => {
const data = [`data: {"error":{"message":"INTERNAL","status":"INTERNAL"}}`];
expect(resp.body).to.equal([...data, ""].join("\n"));
});

it("always returns error for v1 callables", async () => {
const mockReq = mockRequest(
{ message: "hello streaming" },
"application/json",
{},
{ accept: "text/event-stream" }
) as any;
const fn = https.onCallHandler(
{
cors: { origin: true, methods: "POST" },
},
() => {
return "hello world";
},
"gcfv1"
);
const resp = await runHandler(fn, mockReq);
expect(JSON.parse(resp.body)).to.deep.equal({
error: {
status: "INVALID_ARGUMENT",
message: "Unsupported Accept header 'text/event-stream'",
},
});
});
});
});

Expand Down
8 changes: 7 additions & 1 deletion src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,12 @@ function wrapOnCallHandler<Req = any, Res = any>(
}

const acceptsStreaming = req.header("accept") === "text/event-stream";

if (acceptsStreaming && version === "gcfv1") {
// streaming responses are not supported in v1 callable
throw new HttpsError("invalid-argument", "Unsupported Accept header 'text/event-stream'");
}

const data: Req = decode(req.body.data);
let result: Res;
if (version === "gcfv1") {
Expand Down Expand Up @@ -832,7 +838,7 @@ function wrapOnCallHandler<Req = any, Res = any>(

const { status } = httpErr.httpErrorCode;
const body = { error: httpErr.toJSON() };
if (req.header("accept") === "text/event-stream") {
if (version === "gcfv2" && req.header("accept") === "text/event-stream") {
res.send(encodeSSE(body));
} else {
res.status(status).send(body);
Expand Down

0 comments on commit 363c275

Please sign in to comment.