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>
53 lines
2.2 KiB
TypeScript
53 lines
2.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { matchPattern, matchRoute, allowedMethods } from '../src/http/router';
|
|
import type { RouteDef } from '../src/http/types';
|
|
|
|
const route = (method: string, pattern: string): RouteDef => ({
|
|
method,
|
|
pattern,
|
|
transport: 'buffer',
|
|
handler: async () => ({ status: 200, headers: {}, body: '' }),
|
|
});
|
|
|
|
describe('matchPattern', () => {
|
|
it('matches static and :param segments, decoding params', () => {
|
|
expect(matchPattern('/users/:id', '/users/a%20b')).toEqual({ id: 'a b' });
|
|
expect(matchPattern('/users/:id', '/users')).toBeUndefined();
|
|
expect(matchPattern('/users', '/users/1')).toBeUndefined();
|
|
});
|
|
|
|
it(':name* captures the rest of the path, slashes included', () => {
|
|
expect(matchPattern('/files/:path*', '/files/img/2026/logo.png')).toEqual({ path: 'img/2026/logo.png' });
|
|
expect(matchPattern('/files/:path*', '/files/a%2Fb')).toEqual({ path: 'a/b' });
|
|
});
|
|
|
|
it(':name* matches zero remaining segments as an empty string', () => {
|
|
expect(matchPattern('/files/:path*', '/files')).toEqual({ path: '' });
|
|
});
|
|
});
|
|
|
|
describe('matchRoute precedence', () => {
|
|
it('static beats :param beats catch-all regardless of registration order', () => {
|
|
const routes = [route('GET', '/a/:rest*'), route('GET', '/a/:id'), route('GET', '/a/b')];
|
|
expect(matchRoute(routes, 'GET', '/a/b')?.def.pattern).toBe('/a/b');
|
|
expect(matchRoute(routes, 'GET', '/a/x')?.def.pattern).toBe('/a/:id');
|
|
expect(matchRoute(routes, 'GET', '/a/x/y')?.def.pattern).toBe('/a/:rest*');
|
|
});
|
|
|
|
it('returns undefined when no pattern matches the path', () => {
|
|
expect(matchRoute([route('GET', '/a')], 'GET', '/b')).toBeUndefined();
|
|
});
|
|
|
|
it('does not match a route registered under a different method', () => {
|
|
expect(matchRoute([route('POST', '/a')], 'GET', '/a')).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('allowedMethods (405 support)', () => {
|
|
it('lists distinct methods whose pattern matches the path', () => {
|
|
const routes = [route('GET', '/a'), route('POST', '/a'), route('GET', '/b')];
|
|
expect(allowedMethods(routes, '/a').sort()).toEqual(['GET', 'POST']);
|
|
expect(allowedMethods(routes, '/nope')).toEqual([]);
|
|
});
|
|
});
|