From 03def0324b5eb742bc0ececa044420689d24274b Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 24 Oct 2026 22:22:04 -0500 Subject: [PATCH] fix(params): match @header names without case (GitHub #37) Node's IncomingHttpHeaders and the fetch adapter's headersToRecord both store request header names lower-cased, but resolveOne looked a @header/@headers key up exactly as the decorator wrote it. Any name with a capital letter, such as @header('Content-Type'), therefore reached the handler as undefined. resolveOne now lower-cases the key for the headers source before the lookup. A picked bag (@headers([...])) keeps the names as written, so a handler reads back the key it asked for; @headers() with no argument still returns the whole lower-cased bag. test/params.test.ts covers the single and picked forms on resolveArgs; test/header-case.test.ts sends a request over app.fetch and the Node server. All three fail without the fix. Signed-off-by: Diego Resendez --- CHANGELOG.md | 7 +++++++ src/params.ts | 7 +++++-- test/header-case.test.ts | 36 ++++++++++++++++++++++++++++++++++++ test/params.test.ts | 10 ++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 test/header-case.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bbc92d..73a40d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -327,6 +327,13 @@ npm treats versions as semver and semver forbids leading zeros. handing the handler partial data. The Node server and `app.fetch` parse multipart through the same path, so the fix applies to both. +- **`@header('Content-Type')` finds the header** (GitHub #37). Every adapter stores request + header names lower-cased, and `@header`/`@headers` looked the name up as written, so any name + with a capital letter reached the handler as `undefined`. The name is now matched without case. + `@headers(['X-Trace'])` keeps the names as written in the bag it hands over, so the handler reads + `X-Trace` back; `@headers()` with no argument still returns the lower-cased bag. Node, Deno, Bun + and the edge share this path. + - **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or `EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise never settled, and the error surfaced as an unhandled one. It now rejects with that error. The diff --git a/src/params.ts b/src/params.ts index ef30185..3bfa2e7 100644 --- a/src/params.ts +++ b/src/params.ts @@ -147,14 +147,17 @@ function resolveOne(spec: ArgSpec, context: Context): unknown { const bag = spec.source === 'cookies' ? cookiesOf(context) : context[spec.source]; if (typeof bag !== 'object' || bag === null) return bag; // can't pick from a non-object; return as-is const values = bag as Record; + // Every adapter stores header names lower-cased, so `@header('Content-Type')` looks up + // `content-type` (GitHub #37). A picked bag keeps the names as the handler wrote them. + const at = spec.source === 'headers' ? (key: string) => values[key.toLowerCase()] : (key: string) => values[key]; if (spec.keys) { const picked: Record = {}; - for (const key of spec.keys) picked[key] = values[key]; + for (const key of spec.keys) picked[key] = at(key); return picked; } - if (spec.key !== undefined) return values[spec.key]; + if (spec.key !== undefined) return at(spec.key); return values; } diff --git a/test/header-case.test.ts b/test/header-case.test.ts new file mode 100644 index 0000000..f5fbe44 --- /dev/null +++ b/test/header-case.test.ts @@ -0,0 +1,36 @@ +// GitHub #37: `@header('Content-Type')` reached the handler as undefined, because every adapter +// stores header names lower-cased and the lookup used the name as written. +import 'reflect-metadata'; +import { afterEach, describe, expect, it } from 'vitest'; +import type { AddressInfo } from 'node:net'; +import type { Server } from 'node:http'; +import { createApp, Get, Module, Route, header } from '../src/index'; + +@Route('/') +class Ctl { + @Get('/h') h(@header('Content-Type') ct: unknown, @header('X-Trace') trace: unknown) { + return { ct: ct ?? null, trace: trace ?? null }; + } +} + +@Module({ mountpoint: '/', controllers: [Ctl] }) +class M {} + +const sent = { 'content-type': 'text/plain', 'x-trace': 'abc' }; +const expected = { ct: 'text/plain', trace: 'abc' }; + +describe('@header matches the name without case (GitHub #37)', () => { + let server: Server | undefined; + afterEach(() => new Promise((resolve) => (server ? server.close(() => resolve()) : resolve()))); + + it('on app.fetch', async () => { + const app = createApp({ modules: [M] }); + expect(await (await app.fetch(new Request('http://x/h', { headers: sent }))).json()).toEqual(expected); + }); + + it('on the Node server', async () => { + server = await createApp({ modules: [M] }).listen(0); + const r = await fetch(`http://127.0.0.1:${(server.address() as AddressInfo).port}/h`, { headers: sent }); + expect(await r.json()).toEqual(expected); + }); +}); diff --git a/test/params.test.ts b/test/params.test.ts index 7dbd34f..98b8563 100644 --- a/test/params.test.ts +++ b/test/params.test.ts @@ -110,6 +110,16 @@ describe('@headers / @header', () => { expect(args[1]).toEqual({ authorization: 'Bearer t0ken', 'x-trace': 'abc' }); }); + // GitHub #37: adapters store header names lower-cased; the decorator name is matched without case. + it('@header and @headers([...]) match the name without case, keeping the written names', async () => { + class Ctl { + handle(@header('Authorization') _auth: string, @headers(['X-Trace', 'x-count']) _some: unknown) {} + } + const args = await resolveArgs(getArgs(Ctl, 'handle'), { headers: reqHeaders }); + expect(args[0]).toBe('Bearer t0ken'); + expect(args[1]).toEqual({ 'X-Trace': 'abc', 'x-count': '5' }); + }); + it('@header(name, schema) validates the picked header', async () => { class Ctl { handle(@header('x-count', numFromStr) _count: number) {}