burger-api 0.9.7 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -241
- package/dist/src/adapter/bun/index.d.ts +14 -0
- package/dist/src/adapter/bun/index.js +73 -0
- package/dist/src/adapter/bun/types.d.ts +15 -0
- package/dist/src/adapter/bun/types.js +1 -0
- package/dist/src/adapter/index.d.ts +1 -0
- package/dist/src/adapter/index.js +1 -0
- package/dist/src/adapter/types.d.ts +49 -0
- package/dist/src/adapter/types.js +1 -0
- package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
- package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
- package/dist/src/adapter/web-standard/index.d.ts +8 -0
- package/dist/src/adapter/web-standard/index.js +7 -0
- package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
- package/dist/src/analysis/route-access-analyzer.js +287 -0
- package/dist/src/chain/chain.d.ts +16 -0
- package/dist/src/chain/chain.js +17 -0
- package/dist/src/chain/flattener.d.ts +3 -0
- package/dist/src/chain/flattener.js +78 -0
- package/dist/src/chain/node.d.ts +25 -0
- package/dist/src/chain/node.js +1 -0
- package/dist/src/compiler/conventions.d.ts +67 -0
- package/dist/src/compiler/conventions.js +129 -0
- package/dist/src/compiler/index.d.ts +13 -0
- package/dist/src/compiler/index.js +11 -0
- package/dist/src/compiler/module-loader.d.ts +75 -0
- package/dist/src/compiler/module-loader.js +276 -0
- package/dist/src/compiler/route-module.d.ts +97 -0
- package/dist/src/compiler/route-module.js +1 -0
- package/dist/src/compiler/route-tree.d.ts +19 -0
- package/dist/src/compiler/route-tree.js +28 -0
- package/dist/src/compiler/scanner.d.ts +42 -0
- package/dist/src/compiler/scanner.js +240 -0
- package/dist/src/context/context-set.d.ts +26 -0
- package/dist/src/context/context-set.js +45 -0
- package/dist/src/context/context.d.ts +357 -0
- package/dist/src/context/context.js +401 -0
- package/dist/src/context/query-parser.d.ts +23 -0
- package/dist/src/context/query-parser.js +132 -0
- package/dist/src/context/route-access.d.ts +7 -0
- package/dist/src/context/route-access.js +18 -0
- package/dist/src/context/types.d.ts +62 -0
- package/dist/src/context/types.js +5 -0
- package/dist/src/core/asset-mime.d.ts +9 -0
- package/dist/src/core/asset-mime.js +35 -0
- package/dist/src/core/assets.d.ts +30 -0
- package/dist/src/core/assets.js +67 -0
- package/dist/src/core/docs-providers.d.ts +13 -0
- package/dist/src/core/docs-providers.js +89 -0
- package/dist/src/core/embedded-assets.d.ts +18 -0
- package/dist/src/core/embedded-assets.js +13 -0
- package/dist/src/core/openapi.d.ts +9 -10
- package/dist/src/core/openapi.js +333 -127
- package/dist/src/core/page-router.d.ts +13 -17
- package/dist/src/core/page-router.js +86 -55
- package/dist/src/core/server.d.ts +20 -11
- package/dist/src/core/server.js +41 -45
- package/dist/src/errors/forbidden.d.ts +14 -0
- package/dist/src/errors/forbidden.js +16 -0
- package/dist/src/errors/http-error.d.ts +49 -0
- package/dist/src/errors/http-error.js +155 -0
- package/dist/src/errors/method-not-allowed.d.ts +13 -0
- package/dist/src/errors/method-not-allowed.js +15 -0
- package/dist/src/errors/not-found.d.ts +14 -0
- package/dist/src/errors/not-found.js +16 -0
- package/dist/src/errors/unauthorized.d.ts +14 -0
- package/dist/src/errors/unauthorized.js +16 -0
- package/dist/src/index.d.ts +275 -50
- package/dist/src/index.js +974 -333
- package/dist/src/lifecycle/executor.d.ts +64 -0
- package/dist/src/lifecycle/executor.js +201 -0
- package/dist/src/lifecycle/hook-runner.d.ts +11 -0
- package/dist/src/lifecycle/hook-runner.js +156 -0
- package/dist/src/lifecycle/jit.d.ts +50 -0
- package/dist/src/lifecycle/jit.js +258 -0
- package/dist/src/lifecycle/transform.d.ts +26 -0
- package/dist/src/lifecycle/transform.js +99 -0
- package/dist/src/lifecycle/types.d.ts +135 -0
- package/dist/src/lifecycle/types.js +1 -0
- package/dist/src/plugin/composer.d.ts +3 -0
- package/dist/src/plugin/composer.js +28 -0
- package/dist/src/plugin/registry.d.ts +25 -0
- package/dist/src/plugin/registry.js +107 -0
- package/dist/src/plugin/types.d.ts +20 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/router/allow-cache.d.ts +20 -0
- package/dist/src/router/allow-cache.js +26 -0
- package/dist/src/router/compiler.d.ts +39 -0
- package/dist/src/router/compiler.js +867 -0
- package/dist/src/router/define.d.ts +67 -0
- package/dist/src/router/define.js +34 -0
- package/dist/src/router/index.d.ts +6 -0
- package/dist/src/router/index.js +5 -0
- package/dist/src/router/param-extract.d.ts +33 -0
- package/dist/src/router/param-extract.js +87 -0
- package/dist/src/router/regex-matcher.d.ts +42 -0
- package/dist/src/router/regex-matcher.js +216 -0
- package/dist/src/router/router.d.ts +214 -0
- package/dist/src/router/router.js +670 -0
- package/dist/src/router/static-map.d.ts +39 -0
- package/dist/src/router/static-map.js +48 -0
- package/dist/src/router/trie.d.ts +69 -0
- package/dist/src/router/trie.js +572 -0
- package/dist/src/router/types.d.ts +104 -0
- package/dist/src/router/types.js +1 -0
- package/dist/src/runtime/capabilities.d.ts +29 -0
- package/dist/src/runtime/capabilities.js +48 -0
- package/dist/src/types/index.d.ts +260 -109
- package/dist/src/types/index.js +1 -1
- package/dist/src/types/inference.d.ts +78 -0
- package/dist/src/types/inference.js +20 -0
- package/dist/src/types/openapi-config.d.ts +164 -0
- package/dist/src/types/openapi-config.js +8 -0
- package/dist/src/utils/env.d.ts +13 -0
- package/dist/src/utils/env.js +24 -0
- package/dist/src/utils/fs.d.ts +29 -0
- package/dist/src/utils/fs.js +75 -0
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +2 -2
- package/dist/src/utils/pathConversion.js +13 -7
- package/dist/src/utils/response.d.ts +42 -6
- package/dist/src/utils/response.js +189 -7
- package/dist/src/utils/routing.d.ts +32 -13
- package/dist/src/utils/routing.js +41 -18
- package/dist/src/utils/thenable.d.ts +8 -0
- package/dist/src/utils/thenable.js +12 -0
- package/dist/src/utils/timing-safe.d.ts +9 -0
- package/dist/src/utils/timing-safe.js +20 -0
- package/dist/src/utils/wildcard.d.ts +6 -7
- package/dist/src/utils/wildcard.js +38 -55
- package/dist/src/validation/adapter.d.ts +54 -0
- package/dist/src/validation/adapter.js +75 -0
- package/dist/src/validation/adapters/standard.d.ts +11 -0
- package/dist/src/validation/adapters/standard.js +110 -0
- package/dist/src/validation/adapters/zod.d.ts +9 -0
- package/dist/src/validation/adapters/zod.js +128 -0
- package/dist/src/validation/cache.d.ts +26 -0
- package/dist/src/validation/cache.js +35 -0
- package/dist/src/validation/coerce.d.ts +28 -0
- package/dist/src/validation/coerce.js +176 -0
- package/dist/src/validation/compiler.d.ts +27 -0
- package/dist/src/validation/compiler.js +191 -0
- package/dist/src/validation/error.d.ts +69 -0
- package/dist/src/validation/error.js +137 -0
- package/dist/src/validation/index.d.ts +16 -0
- package/dist/src/validation/index.js +12 -0
- package/dist/src/validation/response.d.ts +25 -0
- package/dist/src/validation/response.js +85 -0
- package/dist/src/validation/types.d.ts +148 -0
- package/dist/src/validation/types.js +6 -0
- package/dist/src/validation/validator.d.ts +39 -0
- package/dist/src/validation/validator.js +441 -0
- package/dist/src/ws/adapter.d.ts +230 -0
- package/dist/src/ws/adapter.js +660 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +178 -0
- package/dist/src/ws/index.d.ts +11 -0
- package/dist/src/ws/index.js +8 -0
- package/dist/src/ws/platform.d.ts +82 -0
- package/dist/src/ws/platform.js +122 -0
- package/dist/src/ws/router.d.ts +47 -0
- package/dist/src/ws/router.js +183 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +253 -0
- package/dist/src/ws/types.d.ts +381 -0
- package/dist/src/ws/types.js +213 -0
- package/package.json +72 -60
- package/dist/src/core/api-router.d.ts +0 -65
- package/dist/src/core/api-router.js +0 -264
- package/dist/src/core/swagger-ui.d.ts +0 -1
- package/dist/src/core/swagger-ui.js +0 -33
- package/dist/src/middleware/validator.d.ts +0 -14
- package/dist/src/middleware/validator.js +0 -133
- package/dist/src/utils/error.d.ts +0 -17
- package/dist/src/utils/error.js +0 -63
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
import { ROUTE_CONSTANTS } from '../utils/routing.js';
|
|
2
|
+
function createRadixNode(part) {
|
|
3
|
+
return { part, store: null, wildcard: null, inert: null, param: null };
|
|
4
|
+
}
|
|
5
|
+
function cloneRadixNode(node, part) {
|
|
6
|
+
return {
|
|
7
|
+
part,
|
|
8
|
+
store: node.store,
|
|
9
|
+
wildcard: node.wildcard,
|
|
10
|
+
inert: node.inert,
|
|
11
|
+
param: node.param,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** Turns a split-out node into a branching parent of `children`. */
|
|
15
|
+
function resetRadixNode(node, part, children) {
|
|
16
|
+
node.part = part;
|
|
17
|
+
node.store = null;
|
|
18
|
+
node.wildcard = null;
|
|
19
|
+
node.param = null;
|
|
20
|
+
const inert = new Map();
|
|
21
|
+
for (let i = 0; i < children.length; i++) {
|
|
22
|
+
const child = children[i];
|
|
23
|
+
inert.set(child.part.charCodeAt(0), child);
|
|
24
|
+
}
|
|
25
|
+
node.inert = inert;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Per-match capture scratch. Matching is synchronous and never re-enters
|
|
29
|
+
* (no user code runs while a match is in progress), so one module-level
|
|
30
|
+
* buffer replaces a per-request array.
|
|
31
|
+
*/
|
|
32
|
+
const captured = [];
|
|
33
|
+
let capturedCount = 0;
|
|
34
|
+
let wildcardCapture = null;
|
|
35
|
+
/** Literal runs shorter than this compare with a char-code loop, not a slice. */
|
|
36
|
+
const CHAR_COMPARE_LIMIT = 15;
|
|
37
|
+
/**
|
|
38
|
+
* Optimized dynamic-route matcher.
|
|
39
|
+
*
|
|
40
|
+
* Per node the priority is literal continuation > `:param` > `*`, with
|
|
41
|
+
* backtracking when a literal prefix dead-ends. A `*` route also matches
|
|
42
|
+
* its own base path (`/files/*` matches `/files`, `wildcardParams: []`).
|
|
43
|
+
* A trailing slash is preserved: `/users/` binds `:id === ""` (the Router
|
|
44
|
+
* rejects empty params — the loose-slash retry then runs). Ambiguous param
|
|
45
|
+
* folders at one level and non-terminal wildcards throw at insert time.
|
|
46
|
+
*/
|
|
47
|
+
export class Trie {
|
|
48
|
+
root = createRadixNode('/');
|
|
49
|
+
/**
|
|
50
|
+
* Segment matcher retaining the exact decoding semantics for
|
|
51
|
+
* percent-encoded or non-absolute pathnames. Every route is inserted
|
|
52
|
+
* into both structures; the hot path never touches this one when the
|
|
53
|
+
* pathname is absolute and percent-free.
|
|
54
|
+
*/
|
|
55
|
+
legacy = new LegacyTrie();
|
|
56
|
+
/**
|
|
57
|
+
* Inserts a compiled route into the trie.
|
|
58
|
+
* @throws on ambiguous param folders (two different param names at the same level)
|
|
59
|
+
* or a wildcard segment that is not the last segment (unmatchable by design).
|
|
60
|
+
*/
|
|
61
|
+
insert(path, handler, methods, isWildcard) {
|
|
62
|
+
this.insertRadix(path, handler, methods, isWildcard);
|
|
63
|
+
this.legacy.insert(path, handler, methods, isWildcard);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Matches a pathname against the trie.
|
|
67
|
+
* @param pathname A pathname. A single trailing slash is preserved so that
|
|
68
|
+
* `:param` routes can capture an empty value (e.g. `/users/` → `:id`
|
|
69
|
+
* with `id === ""`), matching Bun's native behavior.
|
|
70
|
+
* @returns the match result, or `null` if no route matches.
|
|
71
|
+
*/
|
|
72
|
+
match(pathname) {
|
|
73
|
+
if (pathname.charCodeAt(0) === 47 /* '/' */ &&
|
|
74
|
+
pathname.indexOf('%') === -1 &&
|
|
75
|
+
pathname.indexOf('//') === -1) {
|
|
76
|
+
return this.matchRadix(pathname);
|
|
77
|
+
}
|
|
78
|
+
return this.legacy.match(pathname);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the allowed methods for a pathname, or `null` if it does not match.
|
|
82
|
+
*/
|
|
83
|
+
allowedMethods(pathname) {
|
|
84
|
+
const m = this.match(pathname);
|
|
85
|
+
return m ? m.methods : null;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Emits every registered pattern in strict match-priority order: a
|
|
89
|
+
* pre-order DFS following the matcher's per-node priority (static
|
|
90
|
+
* continuations, then `:param`, then `*`). The first emitted pattern that
|
|
91
|
+
* can match a path is exactly the one the trie would select — the
|
|
92
|
+
* authoritative ordering source for the RegExp matcher.
|
|
93
|
+
*/
|
|
94
|
+
orderedPatterns() {
|
|
95
|
+
const out = [];
|
|
96
|
+
this.visitRadix(this.root, out);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
visitRadix(node, out) {
|
|
100
|
+
if (node.store)
|
|
101
|
+
out.push(node.store.pattern);
|
|
102
|
+
if (node.inert) {
|
|
103
|
+
for (const child of node.inert.values()) {
|
|
104
|
+
this.visitRadix(child, out);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (node.param) {
|
|
108
|
+
if (node.param.store)
|
|
109
|
+
out.push(node.param.store.pattern);
|
|
110
|
+
if (node.param.child)
|
|
111
|
+
this.visitRadix(node.param.child, out);
|
|
112
|
+
if (node.param.wildcard)
|
|
113
|
+
out.push(node.param.wildcard.pattern);
|
|
114
|
+
}
|
|
115
|
+
if (node.wildcard)
|
|
116
|
+
out.push(node.wildcard.pattern);
|
|
117
|
+
}
|
|
118
|
+
// --- build ---------------------------------------------------------
|
|
119
|
+
insertRadix(path, handler, methods, isWildcard) {
|
|
120
|
+
const segments = splitPattern(path);
|
|
121
|
+
// Locate a terminal wildcard segment (always last — enforced below).
|
|
122
|
+
let wildcardIndex = -1;
|
|
123
|
+
for (let i = 0; i < segments.length; i++) {
|
|
124
|
+
if (segments[i].startsWith(ROUTE_CONSTANTS.WILDCARD_SEGMENT_PREFIX)) {
|
|
125
|
+
if (i < segments.length - 1) {
|
|
126
|
+
throw new Error(`Wildcard segment "*" must be the last segment in route path "${path}" — ` +
|
|
127
|
+
`segments after a wildcard can never be matched.`);
|
|
128
|
+
}
|
|
129
|
+
wildcardIndex = i;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const names = [];
|
|
134
|
+
let node = this.root;
|
|
135
|
+
// Param edge awaiting its continuation / terminal (set by a `:param`
|
|
136
|
+
// token, consumed by the next literal run or by the terminal itself).
|
|
137
|
+
let pending = null;
|
|
138
|
+
const flushLiteral = (trailingSlash) => {
|
|
139
|
+
if (literal.length === 0 && !trailingSlash)
|
|
140
|
+
return;
|
|
141
|
+
const text = literal.length === 0
|
|
142
|
+
? '/'
|
|
143
|
+
: '/' + literal.join('/') + (trailingSlash ? '/' : '');
|
|
144
|
+
if (pending !== null) {
|
|
145
|
+
const child = pending.child;
|
|
146
|
+
if (child === null) {
|
|
147
|
+
pending.child = createRadixNode(text);
|
|
148
|
+
node = pending.child;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// `insertPart` mutates the existing subtree in place; the
|
|
152
|
+
// subtree root stays `pending.child`.
|
|
153
|
+
node = this.insertPart(child, text);
|
|
154
|
+
}
|
|
155
|
+
pending = null;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
node = this.insertPart(node, text);
|
|
159
|
+
}
|
|
160
|
+
literal.length = 0;
|
|
161
|
+
};
|
|
162
|
+
const literal = [];
|
|
163
|
+
for (let i = 0; i < segments.length; i++) {
|
|
164
|
+
if (i === wildcardIndex)
|
|
165
|
+
break;
|
|
166
|
+
const segment = segments[i];
|
|
167
|
+
if (segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX)) {
|
|
168
|
+
// A param terminates the current literal run with '/'.
|
|
169
|
+
flushLiteral(true);
|
|
170
|
+
if (pending !== null) {
|
|
171
|
+
// Consecutive params: materialize the in-between node.
|
|
172
|
+
pending.child ??= createRadixNode('');
|
|
173
|
+
node = pending.child;
|
|
174
|
+
pending = null;
|
|
175
|
+
}
|
|
176
|
+
const name = segment.slice(1);
|
|
177
|
+
if (node.param !== null) {
|
|
178
|
+
if (node.param.name !== name) {
|
|
179
|
+
throw new Error(`Ambiguous dynamic route folders at "${path}": ` +
|
|
180
|
+
`":${node.param.name}" and ":${name}" cannot coexist at the same level.`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
node.param = {
|
|
185
|
+
name,
|
|
186
|
+
store: null,
|
|
187
|
+
child: null,
|
|
188
|
+
wildcard: null,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
names.push(name);
|
|
192
|
+
pending = node.param;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
literal.push(segment);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const leaf = (wildcard) => ({
|
|
199
|
+
handler,
|
|
200
|
+
methods,
|
|
201
|
+
pattern: path,
|
|
202
|
+
isWildcard: wildcard ? true : isWildcard,
|
|
203
|
+
names: names.slice(),
|
|
204
|
+
});
|
|
205
|
+
if (wildcardIndex !== -1) {
|
|
206
|
+
flushLiteral(false);
|
|
207
|
+
if (pending !== null) {
|
|
208
|
+
// `/:x/*`: the wildcard hangs off the param edge.
|
|
209
|
+
pending.wildcard = leaf(true);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
node.wildcard = leaf(true);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
flushLiteral(false);
|
|
216
|
+
if (pending !== null) {
|
|
217
|
+
pending.store = leaf(false);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
node.store = leaf(false);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Descends `part` into the radix subtree, splitting nodes where the
|
|
224
|
+
* existing literal run and `part` diverge. Returns the node whose
|
|
225
|
+
* `part` ends where `part` ends.
|
|
226
|
+
*/
|
|
227
|
+
insertPart(node, part) {
|
|
228
|
+
let current = node;
|
|
229
|
+
let offset = 0;
|
|
230
|
+
for (;;) {
|
|
231
|
+
if (offset === part.length) {
|
|
232
|
+
if (offset < current.part.length) {
|
|
233
|
+
// `part` is a strict prefix of the existing run.
|
|
234
|
+
const suffix = cloneRadixNode(current, current.part.slice(offset));
|
|
235
|
+
resetRadixNode(current, part, [suffix]);
|
|
236
|
+
}
|
|
237
|
+
return current;
|
|
238
|
+
}
|
|
239
|
+
if (offset === current.part.length) {
|
|
240
|
+
const code = part.charCodeAt(offset);
|
|
241
|
+
let inert = current.inert;
|
|
242
|
+
if (inert === null) {
|
|
243
|
+
inert = new Map();
|
|
244
|
+
current.inert = inert;
|
|
245
|
+
}
|
|
246
|
+
const child = inert.get(code);
|
|
247
|
+
if (child !== undefined) {
|
|
248
|
+
current = child;
|
|
249
|
+
part = part.slice(offset);
|
|
250
|
+
offset = 0;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const created = createRadixNode(part.slice(offset));
|
|
254
|
+
inert.set(code, created);
|
|
255
|
+
return created;
|
|
256
|
+
}
|
|
257
|
+
if (part.charCodeAt(offset) !== current.part.charCodeAt(offset)) {
|
|
258
|
+
const existing = cloneRadixNode(current, current.part.slice(offset));
|
|
259
|
+
const created = createRadixNode(part.slice(offset));
|
|
260
|
+
resetRadixNode(current, current.part.slice(0, offset), [
|
|
261
|
+
existing,
|
|
262
|
+
created,
|
|
263
|
+
]);
|
|
264
|
+
return created;
|
|
265
|
+
}
|
|
266
|
+
offset++;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// --- match ---------------------------------------------------------
|
|
270
|
+
matchRadix(pathname) {
|
|
271
|
+
capturedCount = 0;
|
|
272
|
+
wildcardCapture = null;
|
|
273
|
+
const leaf = this.matchNode(this.root, pathname, 0);
|
|
274
|
+
if (leaf === null)
|
|
275
|
+
return null;
|
|
276
|
+
const params = {};
|
|
277
|
+
const names = leaf.names;
|
|
278
|
+
for (let i = 0; i < names.length; i++) {
|
|
279
|
+
params[names[i]] = captured[i];
|
|
280
|
+
}
|
|
281
|
+
const wildcardParams = leaf.isWildcard && wildcardCapture !== null
|
|
282
|
+
? splitWildcardCapture(wildcardCapture)
|
|
283
|
+
: undefined;
|
|
284
|
+
return {
|
|
285
|
+
handler: leaf.handler,
|
|
286
|
+
methods: leaf.methods,
|
|
287
|
+
params,
|
|
288
|
+
wildcardParams,
|
|
289
|
+
isWildcard: leaf.isWildcard,
|
|
290
|
+
pattern: leaf.pattern,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
matchNode(node, url, start) {
|
|
294
|
+
const part = node.part;
|
|
295
|
+
const partLength = part.length;
|
|
296
|
+
if (partLength > 0) {
|
|
297
|
+
const end = start + partLength;
|
|
298
|
+
if (end > url.length)
|
|
299
|
+
return null;
|
|
300
|
+
if (partLength < CHAR_COMPARE_LIMIT) {
|
|
301
|
+
for (let i = 0, j = start; i < partLength; i++, j++) {
|
|
302
|
+
if (part.charCodeAt(i) !== url.charCodeAt(j))
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
else if (url.slice(start, end) !== part) {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const pos = start + partLength;
|
|
311
|
+
const param = node.param;
|
|
312
|
+
if (pos === url.length) {
|
|
313
|
+
if (node.store !== null)
|
|
314
|
+
return node.store;
|
|
315
|
+
if (node.wildcard !== null) {
|
|
316
|
+
wildcardCapture = '';
|
|
317
|
+
return node.wildcard;
|
|
318
|
+
}
|
|
319
|
+
if (param !== null && pos > 1) {
|
|
320
|
+
// Trailing-slash form: the empty final segment is the value.
|
|
321
|
+
// (`/` alone has no trailing empty segment — the segment
|
|
322
|
+
// matcher's zero-segment path.) No continuation can match at
|
|
323
|
+
// end-of-path; only the param's own terminal / wildcard.
|
|
324
|
+
const saved = capturedCount;
|
|
325
|
+
captured[capturedCount++] = '';
|
|
326
|
+
if (param.store !== null)
|
|
327
|
+
return param.store;
|
|
328
|
+
if (param.wildcard !== null) {
|
|
329
|
+
wildcardCapture = '';
|
|
330
|
+
return param.wildcard;
|
|
331
|
+
}
|
|
332
|
+
capturedCount = saved;
|
|
333
|
+
}
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
// Priority 1: literal continuation (backtrack if it dead-ends).
|
|
337
|
+
const inert = node.inert;
|
|
338
|
+
if (inert !== null) {
|
|
339
|
+
const child = inert.get(url.charCodeAt(pos));
|
|
340
|
+
if (child !== undefined) {
|
|
341
|
+
const leaf = this.matchNode(child, url, pos);
|
|
342
|
+
if (leaf !== null)
|
|
343
|
+
return leaf;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// Priority 2: `:param` — one segment up to the next `/`.
|
|
347
|
+
if (param !== null) {
|
|
348
|
+
const saved = capturedCount;
|
|
349
|
+
let slash = url.indexOf('/', pos);
|
|
350
|
+
if (slash === -1)
|
|
351
|
+
slash = url.length;
|
|
352
|
+
captured[capturedCount++] = url.slice(pos, slash);
|
|
353
|
+
if (slash === url.length) {
|
|
354
|
+
// Segment ends at end-of-path: only the param's own terminal.
|
|
355
|
+
if (param.store !== null)
|
|
356
|
+
return param.store;
|
|
357
|
+
}
|
|
358
|
+
else if (param.child !== null) {
|
|
359
|
+
const leaf = this.matchNode(param.child, url, slash);
|
|
360
|
+
if (leaf !== null)
|
|
361
|
+
return leaf;
|
|
362
|
+
}
|
|
363
|
+
if (param.wildcard !== null) {
|
|
364
|
+
wildcardCapture = url.slice(slash);
|
|
365
|
+
return param.wildcard;
|
|
366
|
+
}
|
|
367
|
+
capturedCount = saved;
|
|
368
|
+
}
|
|
369
|
+
// Priority 3: wildcard — capture the remainder. The capture starts at
|
|
370
|
+
// a segment boundary: the node's literal must end with `/` (root
|
|
371
|
+
// wildcard) or the next character must be `/`.
|
|
372
|
+
if (node.wildcard !== null) {
|
|
373
|
+
if (part.charCodeAt(partLength - 1) === 47 /* '/' */ ||
|
|
374
|
+
url.charCodeAt(pos) === 47 /* '/' */) {
|
|
375
|
+
wildcardCapture = url.slice(pos);
|
|
376
|
+
return node.wildcard;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Splits a wildcard capture (the text after the route's base path) into its
|
|
384
|
+
* segments. `''` is a base-path hit (no segments); a root wildcard (`/*`)
|
|
385
|
+
* captures from the first character. A trailing empty segment is preserved,
|
|
386
|
+
* exactly like `String.split('/')`.
|
|
387
|
+
*/
|
|
388
|
+
function splitWildcardCapture(raw) {
|
|
389
|
+
if (raw === '')
|
|
390
|
+
return [];
|
|
391
|
+
const out = [];
|
|
392
|
+
let start = raw.charCodeAt(0) === 47 /* '/' */ ? 1 : 0;
|
|
393
|
+
for (let i = start; i < raw.length; i++) {
|
|
394
|
+
if (raw.charCodeAt(i) === 47 /* '/' */) {
|
|
395
|
+
out.push(raw.slice(start, i));
|
|
396
|
+
start = i + 1;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
out.push(raw.slice(start));
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Build-time pattern split (used by both matchers): segments are NOT
|
|
404
|
+
* decoded — `:param` / `*` markers and static pattern text are stored
|
|
405
|
+
* verbatim.
|
|
406
|
+
*/
|
|
407
|
+
function splitPattern(path) {
|
|
408
|
+
const raw = path.split('/');
|
|
409
|
+
const segments = raw.slice(1); // drop the leading '' before the first '/'
|
|
410
|
+
if (path.endsWith('/') && path.length > 1) {
|
|
411
|
+
return segments;
|
|
412
|
+
}
|
|
413
|
+
if (segments.length > 0 && segments[segments.length - 1] === '') {
|
|
414
|
+
segments.pop();
|
|
415
|
+
}
|
|
416
|
+
return segments;
|
|
417
|
+
}
|
|
418
|
+
class LegacyTrie {
|
|
419
|
+
root = { children: new Map() };
|
|
420
|
+
/** {@link Trie.insert} — see the public class for the contract. */
|
|
421
|
+
insert(path, handler, methods, isWildcard) {
|
|
422
|
+
const segments = splitPattern(path);
|
|
423
|
+
let node = this.root;
|
|
424
|
+
for (let i = 0; i < segments.length; i++) {
|
|
425
|
+
const segment = segments[i];
|
|
426
|
+
if (segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX)) {
|
|
427
|
+
const name = segment.slice(1);
|
|
428
|
+
if (!node.paramChild) {
|
|
429
|
+
node.paramChild = { children: new Map() };
|
|
430
|
+
}
|
|
431
|
+
else if (node.paramChild.paramName !== undefined &&
|
|
432
|
+
node.paramChild.paramName !== name) {
|
|
433
|
+
throw new Error(`Ambiguous dynamic route folders at "${path}": ` +
|
|
434
|
+
`":${node.paramChild.paramName}" and ":${name}" cannot coexist at the same level.`);
|
|
435
|
+
}
|
|
436
|
+
node.paramChild.paramName = name;
|
|
437
|
+
node = node.paramChild;
|
|
438
|
+
}
|
|
439
|
+
else if (segment.startsWith(ROUTE_CONSTANTS.WILDCARD_SEGMENT_PREFIX)) {
|
|
440
|
+
if (i < segments.length - 1) {
|
|
441
|
+
throw new Error(`Wildcard segment "*" must be the last segment in route path "${path}" — ` +
|
|
442
|
+
`segments after a wildcard can never be matched.`);
|
|
443
|
+
}
|
|
444
|
+
if (!node.wildcardChild) {
|
|
445
|
+
node.wildcardChild = { children: new Map() };
|
|
446
|
+
}
|
|
447
|
+
node.wildcardChild.isWildcard = true;
|
|
448
|
+
node = node.wildcardChild;
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
if (!node.children.has(segment)) {
|
|
452
|
+
node.children.set(segment, { children: new Map() });
|
|
453
|
+
}
|
|
454
|
+
node = node.children.get(segment);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
node.handler = handler;
|
|
458
|
+
node.methods = methods;
|
|
459
|
+
node.isWildcard = isWildcard;
|
|
460
|
+
node.pattern = path;
|
|
461
|
+
}
|
|
462
|
+
/** {@link Trie.match} — see the public class for the contract. */
|
|
463
|
+
match(pathname) {
|
|
464
|
+
const segments = [];
|
|
465
|
+
splitPathInto(pathname, segments);
|
|
466
|
+
// ONE params object per match: `descend` writes into it while
|
|
467
|
+
// backtracking (restoring previous values on dead ends) and returns it
|
|
468
|
+
// by reference on success — no `{ ...params }` copy per candidate.
|
|
469
|
+
return this.descend(this.root, segments, 0, {});
|
|
470
|
+
}
|
|
471
|
+
descend(node, segments, i, params) {
|
|
472
|
+
// Consumed all segments: a complete route or a base-path wildcard.
|
|
473
|
+
if (i === segments.length) {
|
|
474
|
+
if (node.handler && node.methods) {
|
|
475
|
+
return {
|
|
476
|
+
handler: node.handler,
|
|
477
|
+
methods: node.methods,
|
|
478
|
+
params,
|
|
479
|
+
isWildcard: !!node.isWildcard,
|
|
480
|
+
pattern: node.pattern,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
if (node.wildcardChild?.handler) {
|
|
484
|
+
return {
|
|
485
|
+
handler: node.wildcardChild.handler,
|
|
486
|
+
methods: node.wildcardChild.methods,
|
|
487
|
+
params,
|
|
488
|
+
wildcardParams: [],
|
|
489
|
+
isWildcard: true,
|
|
490
|
+
pattern: node.wildcardChild.pattern,
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
const segment = segments[i];
|
|
496
|
+
// Priority 1: exact static segment (try first; backtrack if it dead-ends).
|
|
497
|
+
const child = node.children.get(segment);
|
|
498
|
+
if (child) {
|
|
499
|
+
const res = this.descend(child, segments, i + 1, params);
|
|
500
|
+
if (res)
|
|
501
|
+
return res;
|
|
502
|
+
}
|
|
503
|
+
// Priority 2: dynamic parameter.
|
|
504
|
+
if (node.paramChild) {
|
|
505
|
+
const pc = node.paramChild;
|
|
506
|
+
const name = pc.paramName;
|
|
507
|
+
const prev = params[name];
|
|
508
|
+
params[name] = segment;
|
|
509
|
+
const res = this.descend(pc, segments, i + 1, params);
|
|
510
|
+
if (res)
|
|
511
|
+
return res;
|
|
512
|
+
if (prev === undefined)
|
|
513
|
+
delete params[name];
|
|
514
|
+
else
|
|
515
|
+
params[name] = prev;
|
|
516
|
+
}
|
|
517
|
+
// Priority 3: wildcard — capture the rest (including the empty
|
|
518
|
+
// trailing segment produced by a path that ends with `/`).
|
|
519
|
+
if (node.wildcardChild?.handler) {
|
|
520
|
+
const count = segments.length - i;
|
|
521
|
+
const wildcardParams = new Array(count);
|
|
522
|
+
for (let j = 0; j < count; j++) {
|
|
523
|
+
wildcardParams[j] = segments[i + j];
|
|
524
|
+
}
|
|
525
|
+
return {
|
|
526
|
+
handler: node.wildcardChild.handler,
|
|
527
|
+
methods: node.wildcardChild.methods,
|
|
528
|
+
params,
|
|
529
|
+
wildcardParams,
|
|
530
|
+
isWildcard: true,
|
|
531
|
+
pattern: node.wildcardChild.pattern,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Splits a pathname into `out` without per-request `split()` / `map()` arrays.
|
|
539
|
+
* Preserves a single trailing empty segment (so `/users/` yields
|
|
540
|
+
* `["users", ""]` and a `:param` can capture `""`); the leading empty segment
|
|
541
|
+
* is dropped. Each segment is percent-decoded only when it carries `%`.
|
|
542
|
+
*/
|
|
543
|
+
function splitPathInto(pathname, out) {
|
|
544
|
+
let start = pathname.charCodeAt(0) === 47 /* '/' */ ? 1 : 0;
|
|
545
|
+
let count = 0;
|
|
546
|
+
for (let i = start; i < pathname.length; i++) {
|
|
547
|
+
if (pathname.charCodeAt(i) === 47 /* '/' */) {
|
|
548
|
+
out[count++] = safeDecode(pathname.slice(start, i));
|
|
549
|
+
start = i + 1;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (start < pathname.length || pathname.length > 1) {
|
|
553
|
+
// Keep the trailing '' so a `:param` can capture the empty value.
|
|
554
|
+
out[count++] = safeDecode(pathname.slice(start));
|
|
555
|
+
}
|
|
556
|
+
out.length = count;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Decodes a single path segment, falling back to the raw value if decoding fails
|
|
560
|
+
* (e.g. malformed percent-encoding). Segments without `%` are returned as-is
|
|
561
|
+
* (no `decodeURIComponent` call).
|
|
562
|
+
*/
|
|
563
|
+
function safeDecode(segment) {
|
|
564
|
+
if (segment === '' || segment.indexOf('%') === -1)
|
|
565
|
+
return segment;
|
|
566
|
+
try {
|
|
567
|
+
return decodeURIComponent(segment);
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
return segment;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { RouteDefinition } from '../types/index.js';
|
|
2
|
+
import type { CompiledRouteValidators, ValidatorConfig } from '../validation/types.js';
|
|
3
|
+
import type { ContextInit, RouteAccessInfo, RouteMeta } from '../context/types.js';
|
|
4
|
+
import type { HTTPMethod } from '../utils/routing.js';
|
|
5
|
+
/**
|
|
6
|
+
* A compiled route handler — the same shape for static (Bun-dispatched) and
|
|
7
|
+
* dynamic/wildcard (trie-dispatched) routes, so method dispatch, 405+Allow,
|
|
8
|
+
* auto-HEAD, and lifecycle behavior are identical on both paths.
|
|
9
|
+
*
|
|
10
|
+
* `prebuilt` is the pre-routing `BurgerContext` (created before routing so
|
|
11
|
+
* `onRequest` hooks can seed state); when provided, the handler binds it
|
|
12
|
+
* instead of allocating a second context. `env` / `executionCtx` are the
|
|
13
|
+
* platform bindings forwarded from the serving entry point.
|
|
14
|
+
*/
|
|
15
|
+
export type CompiledHandler = (request: Request, ctxInit?: ContextInit, prebuilt?: import('../context/context.js').BurgerContext, env?: import('../context/context.js').BurgerEnv, executionCtx?: import('../context/context.js').BurgerExecutionContext) => Response | Promise<Response>;
|
|
16
|
+
/**
|
|
17
|
+
* A route+method executor specialized at compile time. May return
|
|
18
|
+
* synchronously — a route with an empty hook plan does not force a Promise.
|
|
19
|
+
*
|
|
20
|
+
* `ctxInit` is supplied by the `fetch` fallback (trie / loose-slash); the
|
|
21
|
+
* native Bun path omits it and derives params from Bun's already-decoded
|
|
22
|
+
* `request.params`.
|
|
23
|
+
*/
|
|
24
|
+
export type RouteCore = (request: Request, ctxInit?: ContextInit, prebuilt?: import('../context/context.js').BurgerContext, env?: import('../context/context.js').BurgerEnv, executionCtx?: import('../context/context.js').BurgerExecutionContext) => Response | Promise<Response>;
|
|
25
|
+
/** Per-method executors for one route, keyed by HTTP method. */
|
|
26
|
+
export type NativeMethodCores = Partial<Record<HTTPMethod, RouteCore>>;
|
|
27
|
+
/**
|
|
28
|
+
* A Bun `routes` method object value: Bun invokes it with `(request, server)`.
|
|
29
|
+
*/
|
|
30
|
+
export type NativeMethodHandler = (request: Request, server?: unknown) => Response | Promise<Response>;
|
|
31
|
+
/**
|
|
32
|
+
* A Bun `routes` method object: one specialized handler per HTTP method,
|
|
33
|
+
* including the framework's derived `HEAD`, auto `OPTIONS`, and a 405 +
|
|
34
|
+
* Allow for every method the route does not declare. Keeping every key
|
|
35
|
+
* present stops Bun from falling through to a less specific route.
|
|
36
|
+
*/
|
|
37
|
+
export type NativeMethodHandlers = Partial<Record<HTTPMethod, NativeMethodHandler>>;
|
|
38
|
+
/**
|
|
39
|
+
* A route compiled into its dispatch structures.
|
|
40
|
+
*/
|
|
41
|
+
export interface CompiledRoute {
|
|
42
|
+
def: RouteDefinition;
|
|
43
|
+
handler: CompiledHandler;
|
|
44
|
+
methods: string[];
|
|
45
|
+
allow: string;
|
|
46
|
+
/** The matched-route identity (`path` + `pattern`), retained for introspection. */
|
|
47
|
+
route?: RouteMeta;
|
|
48
|
+
/** The optional RouteAccessAnalyzer hint (unused at runtime). */
|
|
49
|
+
meta?: RouteAccessInfo;
|
|
50
|
+
/** The precompiled validators for this route. Undefined when the
|
|
51
|
+
* route has no `schema`. Consumed by the validation orchestrator. */
|
|
52
|
+
validators?: CompiledRouteValidators;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The output of a single RouterCompiler.compile pass.
|
|
56
|
+
*/
|
|
57
|
+
export interface CompiledRouter {
|
|
58
|
+
staticMap: import('./static-map.js').StaticMap;
|
|
59
|
+
trie: import('./trie.js').Trie;
|
|
60
|
+
allowCache: import('./allow-cache.js').AllowCache;
|
|
61
|
+
/**
|
|
62
|
+
* Native dispatch table for `:param` / `*` routes, keyed by their Bun
|
|
63
|
+
* pattern (e.g. `/users/:id`). Consumed only by the Bun adapter, which
|
|
64
|
+
* registers them on `Bun.serve`'s `routes` map; non-Bun adapters ignore
|
|
65
|
+
* this and dispatch via the trie + `fetch` fallback.
|
|
66
|
+
*/
|
|
67
|
+
nativeRoutes: Map<string, CompiledHandler>;
|
|
68
|
+
/**
|
|
69
|
+
* Per-route per-method specialized executors, keyed by path. Used by
|
|
70
|
+
* `Router.staticRoutes()` / `Router.nativeRoutes()` to build Bun method
|
|
71
|
+
* objects; the `fetch` fallback reuses them through `CompiledHandler`s.
|
|
72
|
+
*/
|
|
73
|
+
methodCores: Map<string, NativeMethodCores>;
|
|
74
|
+
/**
|
|
75
|
+
* Retained compiled-route metadata (RouteAccessInfo + RouteMeta) keyed by
|
|
76
|
+
* path. Build-time only; never read on the request hot path.
|
|
77
|
+
*/
|
|
78
|
+
routes?: Map<string, CompiledRoute>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Configuration for the Router / RouterCompiler.
|
|
82
|
+
*/
|
|
83
|
+
export interface RouterConfig {
|
|
84
|
+
/** When true, the optional RouteAccessAnalyzer is skipped at compile time. */
|
|
85
|
+
debug?: boolean;
|
|
86
|
+
/** validation configuration (coercion / response / errors). */
|
|
87
|
+
validation?: ValidatorConfig;
|
|
88
|
+
/**
|
|
89
|
+
* Dynamic-route dispatch engine for the `fetch` fallback path.
|
|
90
|
+
* - `'auto'` (default) and `'trie'`: the radix trie.
|
|
91
|
+
* - `'regex'`: opt-in RegExp matcher (trie-ordered); falls back to the
|
|
92
|
+
* trie if its build bails out.
|
|
93
|
+
*
|
|
94
|
+
* Static routes are unaffected — they never reach this dispatch.
|
|
95
|
+
*/
|
|
96
|
+
engine?: 'auto' | 'regex' | 'trie';
|
|
97
|
+
/**
|
|
98
|
+
* JIT-compile each route's HookPlan into one async function
|
|
99
|
+
* (`lifecycle/jit.ts`). ON by default — capability-probed per process;
|
|
100
|
+
* runtimes without dynamic codegen keep the interpreter. Set `false` to
|
|
101
|
+
* force the interpreter everywhere.
|
|
102
|
+
*/
|
|
103
|
+
jit?: boolean;
|
|
104
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|