From ccaacb9a029bccc285f3fab6fae148fc0ba0e4bd Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 26 Jun 2026 01:51:02 -0500 Subject: [PATCH] fix: stream errors -> 500, guard non-object body picks, test @query e2e --- src/http.ts | 27 ++++++++++++++++++--------- src/params.ts | 10 ++++++---- test/app.test.ts | 19 ++++++++++++++++++- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/http.ts b/src/http.ts index 3eccf1c..b0dbff0 100644 --- a/src/http.ts +++ b/src/http.ts @@ -37,15 +37,12 @@ export function parseQuery(url: string): Record { return out; } -async function readBody(req: http.IncomingMessage): Promise { +async function readBody(req: http.IncomingMessage): Promise { const chunks: Buffer[] = []; for await (const chunk of req) chunks.push(chunk as Buffer); if (chunks.length === 0) return undefined; const raw = Buffer.concat(chunks).toString('utf8'); - if (!raw) return undefined; - const ct = String(req.headers['content-type'] ?? ''); - if (ct.includes('application/json')) return JSON.parse(raw); // throws → caller maps to 400 - return raw; + return raw === '' ? undefined : raw; } export function createHttpServer(routes: RouteDef[]): http.Server { @@ -58,14 +55,26 @@ export function createHttpServer(routes: RouteDef[]): http.Server { res.end(JSON.stringify({ error: 'Not Found' })); return; } - let body: unknown; + let raw: string | undefined; try { - body = await readBody(req); + raw = await readBody(req); } catch { - res.writeHead(400, { 'content-type': 'application/json' }); - res.end(JSON.stringify({ error: 'Invalid JSON body' })); + // stream/network failure — not the client's JSON fault + res.writeHead(500, { 'content-type': 'application/json' }); + res.end(JSON.stringify({ error: 'Internal Server Error' })); return; } + let body: unknown = raw; + const ct = String(req.headers['content-type'] ?? ''); + if (raw !== undefined && ct.includes('application/json')) { + try { + body = JSON.parse(raw); + } catch { + res.writeHead(400, { 'content-type': 'application/json' }); + res.end(JSON.stringify({ error: 'Invalid JSON body' })); + return; + } + } let result: ResponseShape; try { result = await matched.handler({ diff --git a/src/params.ts b/src/params.ts index bdfb9ca..41c5e96 100644 --- a/src/params.ts +++ b/src/params.ts @@ -65,12 +65,14 @@ export function resolveArgs(specs: ArgSpec[], context: Context): unknown[] { function resolveOne(s: ArgSpec, context: Context): unknown { if (s.source === 'ctx') return context; if (s.source === 'needs') return context[s.key as string]; - const bag = (context[s.source] ?? {}) as Record; + const bag = context[s.source]; + if (typeof bag !== 'object' || bag === null) return bag; // can't pick from a non-object; return as-is + const record = bag as Record; if (s.keys) { const out: Record = {}; - for (const k of s.keys) out[k] = bag[k]; + for (const k of s.keys) out[k] = record[k]; return out; } - if (s.key !== undefined) return bag[s.key]; - return bag; + if (s.key !== undefined) return record[s.key]; + return record; } diff --git a/test/app.test.ts b/test/app.test.ts index 0195fd9..5db5423 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -4,7 +4,7 @@ import { Provider, Step, Route, Get, Module, Transformer } from '../src/metadata import { JsonTransformer } from '../src/transformers'; import { Unauthorized } from '../src/signals'; import { createApp } from '../src/app'; -import { needs, param } from '../src/params'; +import { needs, param, query } from '../src/params'; @Provider({ provides: 'db' }) class Db { provide() { return { db: { find: (t: string) => (t === 'good' ? { id: 'u1' } : null) } }; } } @@ -78,6 +78,23 @@ test('optional provider failure warns but does not abort boot', async () => { warn.mockRestore(); }); +test('handler receives @query value through a live request', async () => { + @Route('/search') + class SearchCtl { + @Get('/') + run(@query('q') q: string) { return { q }; } + } + @Module({ mountpoint: '/s', providers: [], steps: [], controllers: [SearchCtl] }) + class SearchModule {} + + const app = createApp({ modules: [SearchModule] }); + const server = await app.listen(0); + const port = (server.address() as any).port; + const res = await fetch(`http://127.0.0.1:${port}/s/search/?q=hello`); + expect(await res.json()).toEqual({ q: 'hello' }); + server.close(); +}); + test('boot fails when a handler needs something nothing provides', () => { @Route('/things') class ThingCtl {