diff --git a/.talismanrc b/.talismanrc index 94226f3..d45f9fc 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: f5d6ea83004c5b7187512e86e4e9635511792dd292e861db18e16982d2d27539 + checksum: f846e00049c0c27e378702832fdbeee8f65647b75f285ea5fc2f8ded51757c1f 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: 1f3d5d2fe7d9d820e405bb016a3cfd2119a0126e14632432361add738540bf51 + checksum: cecfa3d38671f42c1dd47c908836d9b618b95f33a0eafb7538486488327b3ed1 - filename: src/functions/function.errors.ts checksum: a24194bef73c8db8c28b8ac104955f2f22abdc8c8d0608fa36a39032055bda57 - filename: src/transport/proxy.test.ts diff --git a/AGENTS.md b/AGENTS.md index 96f0e8f..8a88865 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -537,6 +537,13 @@ The approved differences from V1 are these, and only these: The project `.env` is loaded before the functions are bundled. Azure and GCP still run the vendored V1 server, which leaves text and octet-stream bodies unparsed; locally a handler that reads the raw request stream for those two types gets an empty stream. + `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. - **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 728ff58..0bd7711 100644 --- a/src/functions/cloud-functions.test.ts +++ b/src/functions/cloud-functions.test.ts @@ -556,6 +556,46 @@ describe('CloudFunctions serve routing', () => { expect((await sendRequest(port, '/cached')).cacheControl).toBe('max-age=60'); }); + it('sends a number given to res.send as JSON with the current status, as deployed functions do', async () => { + writeFunctionFile('count.js', 'export default function count(request, response) { response.send(42); }'); + writeFunctionFile('zero.js', 'export default function zero(request, response) { response.send(0); }'); + writeFunctionFile('teapot.js', 'export default function teapot(request, response) { response.status(201).send(418); }'); + writeFunctionFile('nan.js', 'export default function nan(request, response) { response.send(Number.NaN); }'); + writeFunctionFile( + 'typed.js', + 'export default function typed(request, response) { response.setHeader("content-type", "text/plain"); response.send(7); }', + ); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + const count = await sendRequest(port, '/count'); + const zero = await sendRequest(port, '/zero'); + const teapot = await sendRequest(port, '/teapot'); + const nan = await sendRequest(port, '/nan'); + const typed = await sendRequest(port, '/typed'); + + expect(count).toMatchObject({ status: 200, body: '42', contentType: 'application/json; charset=utf-8' }); + expect(zero).toMatchObject({ status: 200, body: '0', contentType: 'application/json; charset=utf-8' }); + expect(teapot).toMatchObject({ status: 201, body: '418', contentType: 'application/json; charset=utf-8' }); + expect(nan).toMatchObject({ status: 200, body: 'null', contentType: 'application/json; charset=utf-8' }); + expect(typed).toMatchObject({ status: 200, body: '7', contentType: 'text/plain; charset=utf-8' }); + expect(loggedErrors).toEqual([]); + }); + + it('ignores a second argument to res.send, as deployed functions do', async () => { + writeFunctionFile('missing.js', 'export default function missing(request, response) { response.send(404, "Not here"); }'); + writeFunctionFile('gone.js', 'export default function gone(request, response) { response.send("gone", 410); }'); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + const missing = await sendRequest(port, '/missing'); + const gone = await sendRequest(port, '/gone'); + + expect(missing).toMatchObject({ status: 200, body: '404', contentType: 'application/json; charset=utf-8' }); + expect(gone).toMatchObject({ status: 200, body: 'gone', contentType: 'text/html; 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 c8f413a..1145144 100755 --- a/src/functions/cloud-functions.ts +++ b/src/functions/cloud-functions.ts @@ -173,6 +173,13 @@ function redirectLikeDeployedFunctions(_request: Request, response: Response, ne next(); } +function sendLikeDeployedFunctions(_request: Request, response: Response, next: NextFunction): void { + const send = response.send.bind(response); + response.send = ((body?: unknown): Response => + typeof body === 'number' ? response.json(body) : send(body)) as Response['send']; + next(); +} + export function answerRequestError( error: Error & { status?: number; type?: string }, _request: Request, @@ -192,6 +199,7 @@ function applyRequestMiddleware(app: Express): void { app.use(defaultToNoStore); app.use(attachCookies); app.use(redirectLikeDeployedFunctions); + app.use(sendLikeDeployedFunctions); app.use(express.json({ limit: DEPLOYED_PAYLOAD_LIMIT })); app.use(express.urlencoded({ extended: true, limit: DEPLOYED_PAYLOAD_LIMIT })); app.use(express.text({ limit: DEPLOYED_PAYLOAD_LIMIT }));