feat: beta-hardening — routing, degradation, mesh gate, uploads, @header

Restructure first: split the two oversized files by cohesion (http.ts 449
→ http/{router,request,stream,ws,server,types}; app.ts 556 → app/{types,
introspect,index}), rename cryptic locals, JSDoc the public API, and
extract helpers so no function exceeds the complexity gate (createApp
187 → <15, listen 113 → 6). Public import paths are unchanged (barrels).

On that structure, five features that close the biggest gaps vs
Express/Fastify/Nest for a comparable beta:

- routing: `:name*` catch-all, specificity precedence (static ▸ param ▸
  catch-all, not registration order), and 405 + Allow when a path exists
  under a different method (was 404)
- visible degradation: optional providers that fail boot are summarized on
  listen() and queryable via app.degraded(), instead of a silent warn
- mesh is alpha — gated behind `experimental: true`; createApp throws if
  `mesh` is configured without it
- multipart moves to the optional `busboy` peer dependency (clear 501 when
  absent), keeping the core at one runtime dependency
- `@header('name')` as a singular alias of `@headers`

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-07 12:00:15 -05:00
1 parent 9d79972f62
commit d6c5cee0ee
42 files changed
+2619 -1170

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import http from 'http';
import { HttpError } from '../signals';
/** Reads the full request body, enforcing the max-bytes ceiling (throws 413 if exceeded). */
export async function readBody(req: http.IncomingMessage, maxBodyBytes: number): Promise<Buffer | undefined> {
const chunks: Buffer[] = [];
let total = 0;
for await (const chunk of req) {
total += (chunk as Buffer).length;
if (total > maxBodyBytes) throw new HttpError(413, 'Payload Too Large');
chunks.push(chunk as Buffer);
}
return chunks.length === 0 ? undefined : Buffer.concat(chunks);
}
/** Returns the first comma-separated token of a header value (handles array-valued headers). */
function firstToken(value: string | string[] | undefined): string | undefined {
const first = Array.isArray(value) ? value[0] : value;
return first?.split(',')[0].trim();
}
/** Determines whether the request arrived over HTTPS, honoring `x-forwarded-proto` when proxy is trusted. */
export function deriveSecure(req: http.IncomingMessage, trustProxy: boolean): boolean {
if (trustProxy) {
const proto = firstToken(req.headers['x-forwarded-proto']);
if (proto) return proto === 'https';
}
return (req.socket as any).encrypted === true;
}
/** Resolves the client IP, honoring `x-forwarded-for` when proxy is trusted. */
export function deriveIp(req: http.IncomingMessage, trustProxy: boolean): string {
if (trustProxy) {
const forwarded = firstToken(req.headers['x-forwarded-for']);
if (forwarded) return forwarded;
}
return req.socket.remoteAddress ?? '';
}