burger-api 0.9.7 → 1.0.0-beta

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.
Files changed (175) hide show
  1. package/README.md +84 -241
  2. package/dist/src/adapter/bun/index.d.ts +14 -0
  3. package/dist/src/adapter/bun/index.js +73 -0
  4. package/dist/src/adapter/bun/types.d.ts +15 -0
  5. package/dist/src/adapter/bun/types.js +1 -0
  6. package/dist/src/adapter/index.d.ts +1 -0
  7. package/dist/src/adapter/index.js +1 -0
  8. package/dist/src/adapter/types.d.ts +49 -0
  9. package/dist/src/adapter/types.js +1 -0
  10. package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
  11. package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
  12. package/dist/src/adapter/web-standard/index.d.ts +8 -0
  13. package/dist/src/adapter/web-standard/index.js +7 -0
  14. package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
  15. package/dist/src/analysis/route-access-analyzer.js +287 -0
  16. package/dist/src/chain/chain.d.ts +16 -0
  17. package/dist/src/chain/chain.js +17 -0
  18. package/dist/src/chain/flattener.d.ts +3 -0
  19. package/dist/src/chain/flattener.js +78 -0
  20. package/dist/src/chain/node.d.ts +25 -0
  21. package/dist/src/chain/node.js +1 -0
  22. package/dist/src/compiler/conventions.d.ts +67 -0
  23. package/dist/src/compiler/conventions.js +129 -0
  24. package/dist/src/compiler/index.d.ts +13 -0
  25. package/dist/src/compiler/index.js +11 -0
  26. package/dist/src/compiler/module-loader.d.ts +75 -0
  27. package/dist/src/compiler/module-loader.js +276 -0
  28. package/dist/src/compiler/route-module.d.ts +97 -0
  29. package/dist/src/compiler/route-module.js +1 -0
  30. package/dist/src/compiler/route-tree.d.ts +19 -0
  31. package/dist/src/compiler/route-tree.js +28 -0
  32. package/dist/src/compiler/scanner.d.ts +42 -0
  33. package/dist/src/compiler/scanner.js +201 -0
  34. package/dist/src/context/context-set.d.ts +26 -0
  35. package/dist/src/context/context-set.js +45 -0
  36. package/dist/src/context/context.d.ts +357 -0
  37. package/dist/src/context/context.js +401 -0
  38. package/dist/src/context/query-parser.d.ts +23 -0
  39. package/dist/src/context/query-parser.js +132 -0
  40. package/dist/src/context/route-access.d.ts +7 -0
  41. package/dist/src/context/route-access.js +18 -0
  42. package/dist/src/context/types.d.ts +62 -0
  43. package/dist/src/context/types.js +5 -0
  44. package/dist/src/core/asset-mime.d.ts +9 -0
  45. package/dist/src/core/asset-mime.js +35 -0
  46. package/dist/src/core/assets.d.ts +30 -0
  47. package/dist/src/core/assets.js +67 -0
  48. package/dist/src/core/docs-providers.d.ts +13 -0
  49. package/dist/src/core/docs-providers.js +89 -0
  50. package/dist/src/core/embedded-assets.d.ts +18 -0
  51. package/dist/src/core/embedded-assets.js +13 -0
  52. package/dist/src/core/openapi.d.ts +9 -10
  53. package/dist/src/core/openapi.js +333 -127
  54. package/dist/src/core/page-router.d.ts +13 -17
  55. package/dist/src/core/page-router.js +86 -55
  56. package/dist/src/core/server.d.ts +20 -11
  57. package/dist/src/core/server.js +41 -45
  58. package/dist/src/errors/forbidden.d.ts +14 -0
  59. package/dist/src/errors/forbidden.js +16 -0
  60. package/dist/src/errors/http-error.d.ts +47 -0
  61. package/dist/src/errors/http-error.js +146 -0
  62. package/dist/src/errors/method-not-allowed.d.ts +13 -0
  63. package/dist/src/errors/method-not-allowed.js +15 -0
  64. package/dist/src/errors/not-found.d.ts +14 -0
  65. package/dist/src/errors/not-found.js +16 -0
  66. package/dist/src/errors/unauthorized.d.ts +14 -0
  67. package/dist/src/errors/unauthorized.js +16 -0
  68. package/dist/src/index.d.ts +263 -50
  69. package/dist/src/index.js +919 -332
  70. package/dist/src/lifecycle/executor.d.ts +64 -0
  71. package/dist/src/lifecycle/executor.js +201 -0
  72. package/dist/src/lifecycle/hook-runner.d.ts +11 -0
  73. package/dist/src/lifecycle/hook-runner.js +156 -0
  74. package/dist/src/lifecycle/jit.d.ts +50 -0
  75. package/dist/src/lifecycle/jit.js +258 -0
  76. package/dist/src/lifecycle/transform.d.ts +26 -0
  77. package/dist/src/lifecycle/transform.js +99 -0
  78. package/dist/src/lifecycle/types.d.ts +135 -0
  79. package/dist/src/lifecycle/types.js +1 -0
  80. package/dist/src/plugin/composer.d.ts +3 -0
  81. package/dist/src/plugin/composer.js +28 -0
  82. package/dist/src/plugin/registry.d.ts +25 -0
  83. package/dist/src/plugin/registry.js +107 -0
  84. package/dist/src/plugin/types.d.ts +20 -0
  85. package/dist/src/plugin/types.js +1 -0
  86. package/dist/src/router/allow-cache.d.ts +20 -0
  87. package/dist/src/router/allow-cache.js +26 -0
  88. package/dist/src/router/compiler.d.ts +39 -0
  89. package/dist/src/router/compiler.js +817 -0
  90. package/dist/src/router/define.d.ts +67 -0
  91. package/dist/src/router/define.js +34 -0
  92. package/dist/src/router/index.d.ts +6 -0
  93. package/dist/src/router/index.js +5 -0
  94. package/dist/src/router/param-extract.d.ts +33 -0
  95. package/dist/src/router/param-extract.js +87 -0
  96. package/dist/src/router/regex-matcher.d.ts +42 -0
  97. package/dist/src/router/regex-matcher.js +216 -0
  98. package/dist/src/router/router.d.ts +201 -0
  99. package/dist/src/router/router.js +600 -0
  100. package/dist/src/router/static-map.d.ts +39 -0
  101. package/dist/src/router/static-map.js +48 -0
  102. package/dist/src/router/trie.d.ts +69 -0
  103. package/dist/src/router/trie.js +572 -0
  104. package/dist/src/router/types.d.ts +104 -0
  105. package/dist/src/router/types.js +1 -0
  106. package/dist/src/runtime/capabilities.d.ts +29 -0
  107. package/dist/src/runtime/capabilities.js +48 -0
  108. package/dist/src/types/index.d.ts +260 -109
  109. package/dist/src/types/index.js +1 -1
  110. package/dist/src/types/inference.d.ts +78 -0
  111. package/dist/src/types/inference.js +20 -0
  112. package/dist/src/types/openapi-config.d.ts +164 -0
  113. package/dist/src/types/openapi-config.js +8 -0
  114. package/dist/src/utils/env.d.ts +13 -0
  115. package/dist/src/utils/env.js +24 -0
  116. package/dist/src/utils/fs.d.ts +29 -0
  117. package/dist/src/utils/fs.js +75 -0
  118. package/dist/src/utils/index.d.ts +1 -1
  119. package/dist/src/utils/index.js +2 -2
  120. package/dist/src/utils/pathConversion.js +13 -7
  121. package/dist/src/utils/response.d.ts +42 -6
  122. package/dist/src/utils/response.js +172 -7
  123. package/dist/src/utils/routing.d.ts +32 -13
  124. package/dist/src/utils/routing.js +41 -18
  125. package/dist/src/utils/thenable.d.ts +8 -0
  126. package/dist/src/utils/thenable.js +12 -0
  127. package/dist/src/utils/timing-safe.d.ts +9 -0
  128. package/dist/src/utils/timing-safe.js +20 -0
  129. package/dist/src/utils/wildcard.d.ts +2 -9
  130. package/dist/src/utils/wildcard.js +20 -58
  131. package/dist/src/validation/adapter.d.ts +54 -0
  132. package/dist/src/validation/adapter.js +75 -0
  133. package/dist/src/validation/adapters/standard.d.ts +11 -0
  134. package/dist/src/validation/adapters/standard.js +110 -0
  135. package/dist/src/validation/adapters/zod.d.ts +9 -0
  136. package/dist/src/validation/adapters/zod.js +128 -0
  137. package/dist/src/validation/cache.d.ts +26 -0
  138. package/dist/src/validation/cache.js +35 -0
  139. package/dist/src/validation/coerce.d.ts +28 -0
  140. package/dist/src/validation/coerce.js +176 -0
  141. package/dist/src/validation/compiler.d.ts +27 -0
  142. package/dist/src/validation/compiler.js +191 -0
  143. package/dist/src/validation/error.d.ts +69 -0
  144. package/dist/src/validation/error.js +137 -0
  145. package/dist/src/validation/index.d.ts +16 -0
  146. package/dist/src/validation/index.js +12 -0
  147. package/dist/src/validation/response.d.ts +25 -0
  148. package/dist/src/validation/response.js +79 -0
  149. package/dist/src/validation/types.d.ts +148 -0
  150. package/dist/src/validation/types.js +6 -0
  151. package/dist/src/validation/validator.d.ts +39 -0
  152. package/dist/src/validation/validator.js +441 -0
  153. package/dist/src/ws/adapter.d.ts +219 -0
  154. package/dist/src/ws/adapter.js +623 -0
  155. package/dist/src/ws/compiler.d.ts +37 -0
  156. package/dist/src/ws/compiler.js +156 -0
  157. package/dist/src/ws/index.d.ts +11 -0
  158. package/dist/src/ws/index.js +8 -0
  159. package/dist/src/ws/platform.d.ts +81 -0
  160. package/dist/src/ws/platform.js +113 -0
  161. package/dist/src/ws/router.d.ts +48 -0
  162. package/dist/src/ws/router.js +174 -0
  163. package/dist/src/ws/scanner.d.ts +97 -0
  164. package/dist/src/ws/scanner.js +193 -0
  165. package/dist/src/ws/types.d.ts +380 -0
  166. package/dist/src/ws/types.js +213 -0
  167. package/package.json +72 -60
  168. package/dist/src/core/api-router.d.ts +0 -65
  169. package/dist/src/core/api-router.js +0 -264
  170. package/dist/src/core/swagger-ui.d.ts +0 -1
  171. package/dist/src/core/swagger-ui.js +0 -33
  172. package/dist/src/middleware/validator.d.ts +0 -14
  173. package/dist/src/middleware/validator.js +0 -133
  174. package/dist/src/utils/error.d.ts +0 -17
  175. package/dist/src/utils/error.js +0 -63
@@ -0,0 +1,69 @@
1
+ import type { CompiledHandler } from './types.js';
2
+ /**
3
+ * Result of a successful trie match.
4
+ */
5
+ export interface TrieMatch {
6
+ handler: CompiledHandler;
7
+ methods: Set<string>;
8
+ params: Record<string, string>;
9
+ wildcardParams?: string[];
10
+ isWildcard: boolean;
11
+ /** The route-definition path (for `RouteMeta.pattern`). */
12
+ pattern: string;
13
+ }
14
+ /**
15
+ * Optimized dynamic-route matcher.
16
+ *
17
+ * Per node the priority is literal continuation > `:param` > `*`, with
18
+ * backtracking when a literal prefix dead-ends. A `*` route also matches
19
+ * its own base path (`/files/*` matches `/files`, `wildcardParams: []`).
20
+ * A trailing slash is preserved: `/users/` binds `:id === ""` (the Router
21
+ * rejects empty params — the loose-slash retry then runs). Ambiguous param
22
+ * folders at one level and non-terminal wildcards throw at insert time.
23
+ */
24
+ export declare class Trie {
25
+ private root;
26
+ /**
27
+ * Segment matcher retaining the exact decoding semantics for
28
+ * percent-encoded or non-absolute pathnames. Every route is inserted
29
+ * into both structures; the hot path never touches this one when the
30
+ * pathname is absolute and percent-free.
31
+ */
32
+ private legacy;
33
+ /**
34
+ * Inserts a compiled route into the trie.
35
+ * @throws on ambiguous param folders (two different param names at the same level)
36
+ * or a wildcard segment that is not the last segment (unmatchable by design).
37
+ */
38
+ insert(path: string, handler: CompiledHandler, methods: Set<string>, isWildcard: boolean): void;
39
+ /**
40
+ * Matches a pathname against the trie.
41
+ * @param pathname A pathname. A single trailing slash is preserved so that
42
+ * `:param` routes can capture an empty value (e.g. `/users/` → `:id`
43
+ * with `id === ""`), matching Bun's native behavior.
44
+ * @returns the match result, or `null` if no route matches.
45
+ */
46
+ match(pathname: string): TrieMatch | null;
47
+ /**
48
+ * Returns the allowed methods for a pathname, or `null` if it does not match.
49
+ */
50
+ allowedMethods(pathname: string): Set<string> | null;
51
+ /**
52
+ * Emits every registered pattern in strict match-priority order: a
53
+ * pre-order DFS following the matcher's per-node priority (static
54
+ * continuations, then `:param`, then `*`). The first emitted pattern that
55
+ * can match a path is exactly the one the trie would select — the
56
+ * authoritative ordering source for the RegExp matcher.
57
+ */
58
+ orderedPatterns(): string[];
59
+ private visitRadix;
60
+ private insertRadix;
61
+ /**
62
+ * Descends `part` into the radix subtree, splitting nodes where the
63
+ * existing literal run and `part` diverge. Returns the node whose
64
+ * `part` ends where `part` ends.
65
+ */
66
+ private insertPart;
67
+ private matchRadix;
68
+ private matchNode;
69
+ }
@@ -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
+ }