From 426d279ef3cb82306f1f08e0dfbac292afd66452 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 21 Nov 2024 13:01:46 -0800 Subject: [PATCH 1/3] Remove support for streaming responses for v1 callables. --- src/common/providers/https.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/common/providers/https.ts b/src/common/providers/https.ts index d10696837..3685bbdf1 100644 --- a/src/common/providers/https.ts +++ b/src/common/providers/https.ts @@ -782,6 +782,13 @@ function wrapOnCallHandler( } 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") { From dc7a4ca16d50979a96dbb9fcfc72f77bf62704a1 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 21 Nov 2024 13:30:12 -0800 Subject: [PATCH 2/3] Complete implementations, add test. --- spec/common/providers/https.spec.ts | 20 ++++++++++++++++++++ src/common/providers/https.ts | 7 +++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/spec/common/providers/https.spec.ts b/spec/common/providers/https.spec.ts index 050577564..f9442bc66 100644 --- a/spec/common/providers/https.spec.ts +++ b/spec/common/providers/https.spec.ts @@ -802,6 +802,26 @@ 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" }, + }, + (req, resp) => { + 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'" } }); + }); }); }); diff --git a/src/common/providers/https.ts b/src/common/providers/https.ts index 3685bbdf1..acef9271b 100644 --- a/src/common/providers/https.ts +++ b/src/common/providers/https.ts @@ -788,7 +788,6 @@ function wrapOnCallHandler( throw new HttpsError('invalid-argument', "Unsupported Accept header 'text/event-stream'") } - const data: Req = decode(req.body.data); let result: Res; if (version === "gcfv1") { @@ -839,11 +838,11 @@ function wrapOnCallHandler( 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); } } - }; -} + } +}; From ddc489761ba72e146076e9434ad54efb2082fff0 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 21 Nov 2024 13:54:34 -0800 Subject: [PATCH 3/3] Fix formatting. --- spec/common/providers/https.spec.ts | 9 +++++++-- src/common/providers/https.ts | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/spec/common/providers/https.spec.ts b/spec/common/providers/https.spec.ts index f9442bc66..55f698575 100644 --- a/spec/common/providers/https.spec.ts +++ b/spec/common/providers/https.spec.ts @@ -814,13 +814,18 @@ describe("onCallHandler", () => { { cors: { origin: true, methods: "POST" }, }, - (req, resp) => { + () => { 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'" } }); + expect(JSON.parse(resp.body)).to.deep.equal({ + error: { + status: "INVALID_ARGUMENT", + message: "Unsupported Accept header 'text/event-stream'", + }, + }); }); }); }); diff --git a/src/common/providers/https.ts b/src/common/providers/https.ts index acef9271b..d798a2579 100644 --- a/src/common/providers/https.ts +++ b/src/common/providers/https.ts @@ -785,7 +785,7 @@ function wrapOnCallHandler( if (acceptsStreaming && version === "gcfv1") { // streaming responses are not supported in v1 callable - throw new HttpsError('invalid-argument', "Unsupported Accept header 'text/event-stream'") + throw new HttpsError("invalid-argument", "Unsupported Accept header 'text/event-stream'"); } const data: Req = decode(req.body.data); @@ -844,5 +844,5 @@ function wrapOnCallHandler( res.status(status).send(body); } } - } -}; + }; +}