fix(transformers)!: answer 204 when a handler returns undefined (GitHub #38)
JsonTransformer passed the handler's value to JSON.stringify, which returns undefined for undefined, while TransformerFn promises a string body. The Node server wrote that as an empty 200 with content-type application/json; the fetch adapter's toBodyInit read `body.buffer` on it and threw, so Deno, Bun and the edge answered 500 for a handler with no `return`. JsonTransformer now returns status 204 and an empty body (no headers) for undefined. outcomeToResponse already sends a null body for 204 and the Node server ends with the empty string, so all four runtimes give the same answer. null is a JSON value and still renders as 200 `null`. Routes with their own @Transformer are unchanged. BREAKING CHANGE: a handler with no return value now answers 204 instead of an empty 200 on Node (and instead of 500 elsewhere). Return a value to keep a 200. test/undefined-return.test.ts covers app.fetch and the Node server (both fail without the fix) and pins null to 200. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+62
-6
No files matched your search
@@ -126,6 +126,15 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
migrate, remove the token from the node's `needs`, or move the work that produces it into a
|
||||
separate provider or step the node needs.
|
||||
|
||||
- **A handler that returns `undefined` answers `204 No Content`** (GitHub #38). The default JSON
|
||||
transformer passed the value to `JSON.stringify`, which returns `undefined` rather than a string:
|
||||
the Node server sent an empty `200` with `content-type: application/json`, and the fetch adapter
|
||||
threw a `TypeError`, so Deno, Bun and the edge answered `500` for a handler that did nothing
|
||||
wrong. Every runtime now sends `204` with no body and no `content-type`. A handler that returns
|
||||
`null` still gets `200` with `null`. A client that checked for `200` after a handler with no
|
||||
`return` sees `204` instead; return a value to keep a `200`. A route with its own `@Transformer`
|
||||
is not affected.
|
||||
|
||||
### Added
|
||||
|
||||
- **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__`
|
||||
|
||||
Reference in new issue
Block a user