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:
42 files changed
+2619
-1170
No files matched your search
+52
-27
@@ -1,21 +1,40 @@
|
||||
function levenshtein(a: string, b: string): number {
|
||||
const m = a.length, n = b.length;
|
||||
const d: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
||||
for (let i = 0; i <= m; i++) d[i][0] = i;
|
||||
for (let j = 0; j <= n; j++) d[0][j] = j;
|
||||
for (let i = 1; i <= m; i++) for (let j = 1; j <= n; j++) {
|
||||
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
||||
}
|
||||
return d[m][n];
|
||||
const aLen = a.length,
|
||||
bLen = b.length;
|
||||
const matrix: number[][] = Array.from({ length: aLen + 1 }, () => Array(bLen + 1).fill(0));
|
||||
for (let i = 0; i <= aLen; i++) matrix[i][0] = i;
|
||||
for (let j = 0; j <= bLen; j++) matrix[0][j] = j;
|
||||
|
||||
for (let i = 1; i <= aLen; i++)
|
||||
for (let j = 1; j <= bLen; j++) {
|
||||
matrix[i][j] = Math.min(
|
||||
matrix[i - 1][j] + 1,
|
||||
matrix[i][j - 1] + 1,
|
||||
matrix[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
|
||||
);
|
||||
}
|
||||
|
||||
return matrix[aLen][bLen];
|
||||
}
|
||||
|
||||
/** Returns the candidate within edit distance 2 of `token`, for "did you mean?" hints. */
|
||||
export function nearest(token: string, candidates: Iterable<string>): string | undefined {
|
||||
let best: string | undefined;
|
||||
let bestD = 3; // only suggest edit distance <= 2
|
||||
for (const c of candidates) { const dist = levenshtein(token, c); if (dist < bestD) { bestD = dist; best = c; } }
|
||||
let bestDistance = 3; // only suggest edit distance <= 2
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const distance = levenshtein(token, candidate);
|
||||
|
||||
if (distance < bestDistance) {
|
||||
bestDistance = distance;
|
||||
best = candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
/** A node in the dependency graph: what it provides, what it needs, and where it came from. */
|
||||
export interface GraphNode {
|
||||
name: string;
|
||||
needs: string[];
|
||||
@@ -23,15 +42,16 @@ export interface GraphNode {
|
||||
origin: string;
|
||||
}
|
||||
|
||||
/** Orders nodes so every dependency precedes its dependents; throws on missing keys or cycles. */
|
||||
export function topoSort(nodes: GraphNode[], seedKeys: string[]): GraphNode[] {
|
||||
const producedBy = new Map<string, GraphNode>();
|
||||
for (const n of nodes) for (const key of n.provides) producedBy.set(key, n);
|
||||
for (const node of nodes) for (const key of node.provides) producedBy.set(key, node);
|
||||
|
||||
// validate dependencies exist
|
||||
for (const n of nodes) {
|
||||
for (const key of n.needs) {
|
||||
for (const node of nodes) {
|
||||
for (const key of node.needs) {
|
||||
if (!seedKeys.includes(key) && !producedBy.has(key)) {
|
||||
throw new Error(`missing dependency: ${key} needed by ${n.name}`);
|
||||
throw new Error(`missing dependency: ${key} needed by ${node.name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,28 +59,32 @@ export function topoSort(nodes: GraphNode[], seedKeys: string[]): GraphNode[] {
|
||||
const ordered: GraphNode[] = [];
|
||||
const state = new Map<GraphNode, 'visiting' | 'done'>();
|
||||
|
||||
const visit = (n: GraphNode, trail: string[]): void => {
|
||||
const s = state.get(n);
|
||||
if (s === 'done') return;
|
||||
if (s === 'visiting') throw new Error(`cycle detected: ${[...trail, n.name].join(' -> ')}`);
|
||||
state.set(n, 'visiting');
|
||||
for (const key of n.needs) {
|
||||
const dep = producedBy.get(key);
|
||||
if (dep && dep !== n) visit(dep, [...trail, n.name]);
|
||||
const visit = (node: GraphNode, trail: string[]): void => {
|
||||
const visitState = state.get(node);
|
||||
if (visitState === 'done') return;
|
||||
if (visitState === 'visiting') throw new Error(`cycle detected: ${[...trail, node.name].join(' -> ')}`);
|
||||
state.set(node, 'visiting');
|
||||
|
||||
for (const key of node.needs) {
|
||||
const dependency = producedBy.get(key);
|
||||
if (dependency && dependency !== node) visit(dependency, [...trail, node.name]);
|
||||
}
|
||||
state.set(n, 'done');
|
||||
ordered.push(n);
|
||||
|
||||
state.set(node, 'done');
|
||||
ordered.push(node);
|
||||
};
|
||||
|
||||
for (const n of nodes) visit(n, []);
|
||||
for (const node of nodes) visit(node, []);
|
||||
return ordered;
|
||||
}
|
||||
|
||||
/** Filters `ordered` down to the transitive closure of producers reachable from `needs`. */
|
||||
export function subgraphFor(needs: string[], ordered: GraphNode[]): GraphNode[] {
|
||||
const producedBy = new Map<string, GraphNode>();
|
||||
for (const n of ordered) for (const k of n.provides) producedBy.set(k, n);
|
||||
for (const node of ordered) for (const key of node.provides) producedBy.set(key, node);
|
||||
const inClosure = new Set<GraphNode>();
|
||||
const work = [...needs];
|
||||
|
||||
while (work.length) {
|
||||
const token = work.pop() as string;
|
||||
const node = producedBy.get(token);
|
||||
@@ -68,5 +92,6 @@ export function subgraphFor(needs: string[], ordered: GraphNode[]): GraphNode[]
|
||||
inClosure.add(node);
|
||||
work.push(...node.needs);
|
||||
}
|
||||
return ordered.filter((n) => inClosure.has(n));
|
||||
|
||||
return ordered.filter((node) => inClosure.has(node));
|
||||
}
|
||||
Reference in new issue
Block a user