diff --git a/.talismanrc b/.talismanrc index d45f9fc..8e8cadf 100644 --- a/.talismanrc +++ b/.talismanrc @@ -240,7 +240,7 @@ fileignoreconfig: - "a project name passed on argv to the project resolver" - "expect\\(Object.keys\\(DEPENDENCIES\\)\\).toEqual\\(\\['project', 'env'\\]\\)" - filename: AGENTS.md - checksum: f846e00049c0c27e378702832fdbeee8f65647b75f285ea5fc2f8ded51757c1f + checksum: d9ece4c45007c65054469efcd50de93573c756ad9664a6a2d7658db021a5078c allowed_patterns: - "Do not add an exemption to a guard to make a new file pass" - "passes its own error wording to `client.request\\(req, MESSAGES\\)`" @@ -287,7 +287,7 @@ fileignoreconfig: - filename: test/integration/projects-list-command.test.ts checksum: 038f5cd182e1fd0105e6e10dde7f5068a0c15883222450f72ea3904890a56c0c - filename: src/functions/cloud-functions.test.ts - checksum: cecfa3d38671f42c1dd47c908836d9b618b95f33a0eafb7538486488327b3ed1 + checksum: ca4e03b4ae6d4e39fe5ad654f781b002deb37f2f30ebf58c27feeb162696c562 - filename: src/functions/function.errors.ts checksum: a24194bef73c8db8c28b8ac104955f2f22abdc8c8d0608fa36a39032055bda57 - filename: src/transport/proxy.test.ts diff --git a/AGENTS.md b/AGENTS.md index 8a88865..d40e523 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -540,10 +540,13 @@ The approved differences from V1 are these, and only these: `res.redirect(url)` answers a bare 307 with the location as given (CL-8970). `res.send` takes one argument, as on AWS and Vercel (CL-8970, chosen by the ticket owner): a number is sent as JSON with the current status (`res.send(42)` → `42`, - `application/json; charset=utf-8`; `NaN` → `null`), and a second argument is ignored, so Express's `res.send(status, body)` and `res.send(body, status)` forms - do not set the status. Azure and GCP (V1, Express 4) still answer `res.redirect(url)` with a - 302 and a body, treat `res.send(404)` as a 404 status and an invalid one such as - `res.send(42)` as a 500, and honour the two-argument forms. + `application/json; charset=utf-8`; `NaN` → `null`), and a second argument is ignored, so + Express's `res.send(status, body)` and `res.send(body, status)` forms do not set the status. + `res.json` takes one argument too and keeps the current status, so `res.json(404, body)` and + `res.json(body, 500)` answer 200 (CL-8970). Azure and GCP (V1, Express 4) still answer + `res.redirect(url)` with a 302 and a body, treat `res.send(404)` as a 404 status and an + invalid one such as `res.send(42)` as a 500, and honour the two-argument forms of `res.send` + and `res.json`. - **A function that reaches a file outside `functions/` is warned about, not refused**, naming the function file that crosses the boundary. - **Errors name the user's file.** A function that fails to bundle or load stops serve with diff --git a/src/functions/cloud-functions.test.ts b/src/functions/cloud-functions.test.ts index 0bd7711..93663cd 100644 --- a/src/functions/cloud-functions.test.ts +++ b/src/functions/cloud-functions.test.ts @@ -596,6 +596,32 @@ describe('CloudFunctions serve routing', () => { expect(loggedErrors).toEqual([]); }); + it('ignores a second argument to res.json and keeps the current status, as deployed functions do', async () => { + writeFunctionFile('missing.js', 'export default function missing(request, response) { response.json(404, { error: "missing" }); }'); + writeFunctionFile('failed.js', 'export default function failed(request, response) { response.json({ ok: false }, 500); }'); + writeFunctionFile( + 'created.js', + 'export default function created(request, response) { response.status(201).json({ id: "a1" }); }', + ); + writeFunctionFile( + 'plain.js', + 'export default function plain(request, response) { response.type("text/plain").json({ a: 1 }); }', + ); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + const missing = await sendRequest(port, '/missing'); + const failed = await sendRequest(port, '/failed'); + const created = await sendRequest(port, '/created'); + const plain = await sendRequest(port, '/plain'); + + expect(missing).toMatchObject({ status: 200, body: '404', contentType: 'application/json; charset=utf-8' }); + expect(failed).toMatchObject({ status: 200, body: '{"ok":false}', contentType: 'application/json; charset=utf-8' }); + expect(created).toMatchObject({ status: 201, body: '{"id":"a1"}', contentType: 'application/json; charset=utf-8' }); + expect(plain).toMatchObject({ status: 200, body: '{"a":1}', contentType: 'text/plain; charset=utf-8' }); + expect(loggedErrors).toEqual([]); + }); + it('answers res.redirect(url) with a bare 307 and the location exactly as given, as deployed functions do', async () => { writeFunctionFile('go.js', 'export default function go(request, response) { response.redirect("/target?from=a b"); }'); const port = await freePort(); diff --git a/src/functions/cloud-functions.ts b/src/functions/cloud-functions.ts index 1145144..35a7363 100755 --- a/src/functions/cloud-functions.ts +++ b/src/functions/cloud-functions.ts @@ -175,6 +175,8 @@ function redirectLikeDeployedFunctions(_request: Request, response: Response, ne function sendLikeDeployedFunctions(_request: Request, response: Response, next: NextFunction): void { const send = response.send.bind(response); + const json = response.json.bind(response); + response.json = ((body?: unknown): Response => json(body)) as Response['json']; response.send = ((body?: unknown): Response => typeof body === 'number' ? response.json(body) : send(body)) as Response['send']; next();