@tanstack/router-core 1.136.3 → 1.136.5
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/dist/cjs/Matches.cjs.map +1 -1
- package/dist/cjs/Matches.d.cts +2 -0
- package/dist/cjs/index.cjs +0 -5
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -4
- package/dist/cjs/lru-cache.cjs +5 -0
- package/dist/cjs/lru-cache.cjs.map +1 -1
- package/dist/cjs/lru-cache.d.cts +1 -0
- package/dist/cjs/new-process-route-tree.cjs +655 -0
- package/dist/cjs/new-process-route-tree.cjs.map +1 -0
- package/dist/cjs/new-process-route-tree.d.cts +177 -0
- package/dist/cjs/path.cjs +133 -434
- package/dist/cjs/path.cjs.map +1 -1
- package/dist/cjs/path.d.cts +3 -39
- package/dist/cjs/router.cjs +47 -98
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +7 -11
- package/dist/cjs/ssr/constants.cjs.map +1 -1
- package/dist/cjs/ssr/constants.d.cts +1 -0
- package/dist/cjs/ssr/ssr-client.cjs +2 -0
- package/dist/cjs/ssr/ssr-client.cjs.map +1 -1
- package/dist/cjs/ssr/ssr-client.d.cts +4 -1
- package/dist/cjs/ssr/ssr-server.cjs +64 -12
- package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
- package/dist/cjs/ssr/tsrScript.cjs +1 -1
- package/dist/cjs/ssr/tsrScript.cjs.map +1 -1
- package/dist/esm/Matches.d.ts +2 -0
- package/dist/esm/Matches.js.map +1 -1
- package/dist/esm/index.d.ts +1 -4
- package/dist/esm/index.js +1 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lru-cache.d.ts +1 -0
- package/dist/esm/lru-cache.js +5 -0
- package/dist/esm/lru-cache.js.map +1 -1
- package/dist/esm/new-process-route-tree.d.ts +177 -0
- package/dist/esm/new-process-route-tree.js +655 -0
- package/dist/esm/new-process-route-tree.js.map +1 -0
- package/dist/esm/path.d.ts +3 -39
- package/dist/esm/path.js +133 -434
- package/dist/esm/path.js.map +1 -1
- package/dist/esm/router.d.ts +7 -11
- package/dist/esm/router.js +48 -99
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/ssr/constants.d.ts +1 -0
- package/dist/esm/ssr/constants.js.map +1 -1
- package/dist/esm/ssr/ssr-client.d.ts +4 -1
- package/dist/esm/ssr/ssr-client.js +2 -0
- package/dist/esm/ssr/ssr-client.js.map +1 -1
- package/dist/esm/ssr/ssr-server.js +64 -12
- package/dist/esm/ssr/ssr-server.js.map +1 -1
- package/dist/esm/ssr/tsrScript.js +1 -1
- package/dist/esm/ssr/tsrScript.js.map +1 -1
- package/package.json +1 -1
- package/src/Matches.ts +2 -0
- package/src/index.ts +0 -6
- package/src/lru-cache.ts +6 -0
- package/src/new-process-route-tree.ts +1036 -0
- package/src/path.ts +168 -639
- package/src/router.ts +58 -126
- package/src/ssr/constants.ts +1 -0
- package/src/ssr/ssr-client.ts +10 -1
- package/src/ssr/ssr-server.ts +69 -12
- package/src/ssr/tsrScript.ts +4 -0
- package/dist/cjs/process-route-tree.cjs +0 -144
- package/dist/cjs/process-route-tree.cjs.map +0 -1
- package/dist/cjs/process-route-tree.d.cts +0 -18
- package/dist/esm/process-route-tree.d.ts +0 -18
- package/dist/esm/process-route-tree.js +0 -144
- package/dist/esm/process-route-tree.js.map +0 -1
- package/src/process-route-tree.ts +0 -241
|
@@ -0,0 +1,1036 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant'
|
|
2
|
+
import { createLRUCache } from './lru-cache'
|
|
3
|
+
import { last } from './utils'
|
|
4
|
+
import type { LRUCache } from './lru-cache'
|
|
5
|
+
|
|
6
|
+
export const SEGMENT_TYPE_PATHNAME = 0
|
|
7
|
+
export const SEGMENT_TYPE_PARAM = 1
|
|
8
|
+
export const SEGMENT_TYPE_WILDCARD = 2
|
|
9
|
+
export const SEGMENT_TYPE_OPTIONAL_PARAM = 3
|
|
10
|
+
|
|
11
|
+
export type SegmentKind =
|
|
12
|
+
| typeof SEGMENT_TYPE_PATHNAME
|
|
13
|
+
| typeof SEGMENT_TYPE_PARAM
|
|
14
|
+
| typeof SEGMENT_TYPE_WILDCARD
|
|
15
|
+
| typeof SEGMENT_TYPE_OPTIONAL_PARAM
|
|
16
|
+
|
|
17
|
+
const PARAM_W_CURLY_BRACES_RE =
|
|
18
|
+
/^([^{]*)\{\$([a-zA-Z_$][a-zA-Z0-9_$]*)\}([^}]*)$/ // prefix{$paramName}suffix
|
|
19
|
+
const OPTIONAL_PARAM_W_CURLY_BRACES_RE =
|
|
20
|
+
/^([^{]*)\{-\$([a-zA-Z_$][a-zA-Z0-9_$]*)\}([^}]*)$/ // prefix{-$paramName}suffix
|
|
21
|
+
const WILDCARD_W_CURLY_BRACES_RE = /^([^{]*)\{\$\}([^}]*)$/ // prefix{$}suffix
|
|
22
|
+
|
|
23
|
+
type ParsedSegment = Uint16Array & {
|
|
24
|
+
/** segment type (0 = pathname, 1 = param, 2 = wildcard, 3 = optional param) */
|
|
25
|
+
0: SegmentKind
|
|
26
|
+
/** index of the end of the prefix */
|
|
27
|
+
1: number
|
|
28
|
+
/** index of the start of the value */
|
|
29
|
+
2: number
|
|
30
|
+
/** index of the end of the value */
|
|
31
|
+
3: number
|
|
32
|
+
/** index of the start of the suffix */
|
|
33
|
+
4: number
|
|
34
|
+
/** index of the end of the segment */
|
|
35
|
+
5: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Populates the `output` array with the parsed representation of the given `segment` string.
|
|
40
|
+
*
|
|
41
|
+
* Usage:
|
|
42
|
+
* ```ts
|
|
43
|
+
* let output
|
|
44
|
+
* let cursor = 0
|
|
45
|
+
* while (cursor < path.length) {
|
|
46
|
+
* output = parseSegment(path, cursor, output)
|
|
47
|
+
* const end = output[5]
|
|
48
|
+
* cursor = end + 1
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* `output` is stored outside to avoid allocations during repeated calls. It doesn't need to be typed
|
|
52
|
+
* or initialized, it will be done automatically.
|
|
53
|
+
*/
|
|
54
|
+
export function parseSegment(
|
|
55
|
+
/** The full path string containing the segment. */
|
|
56
|
+
path: string,
|
|
57
|
+
/** The starting index of the segment within the path. */
|
|
58
|
+
start: number,
|
|
59
|
+
/** A Uint16Array (length: 6) to populate with the parsed segment data. */
|
|
60
|
+
output: Uint16Array = new Uint16Array(6),
|
|
61
|
+
): ParsedSegment {
|
|
62
|
+
const next = path.indexOf('/', start)
|
|
63
|
+
const end = next === -1 ? path.length : next
|
|
64
|
+
const part = path.substring(start, end)
|
|
65
|
+
|
|
66
|
+
if (!part || !part.includes('$')) {
|
|
67
|
+
// early escape for static pathname
|
|
68
|
+
output[0] = SEGMENT_TYPE_PATHNAME
|
|
69
|
+
output[1] = start
|
|
70
|
+
output[2] = start
|
|
71
|
+
output[3] = end
|
|
72
|
+
output[4] = end
|
|
73
|
+
output[5] = end
|
|
74
|
+
return output as ParsedSegment
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// $ (wildcard)
|
|
78
|
+
if (part === '$') {
|
|
79
|
+
const total = path.length
|
|
80
|
+
output[0] = SEGMENT_TYPE_WILDCARD
|
|
81
|
+
output[1] = start
|
|
82
|
+
output[2] = start
|
|
83
|
+
output[3] = total
|
|
84
|
+
output[4] = total
|
|
85
|
+
output[5] = total
|
|
86
|
+
return output as ParsedSegment
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// $paramName
|
|
90
|
+
if (part.charCodeAt(0) === 36) {
|
|
91
|
+
output[0] = SEGMENT_TYPE_PARAM
|
|
92
|
+
output[1] = start
|
|
93
|
+
output[2] = start + 1 // skip '$'
|
|
94
|
+
output[3] = end
|
|
95
|
+
output[4] = end
|
|
96
|
+
output[5] = end
|
|
97
|
+
return output as ParsedSegment
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const wildcardBracesMatch = part.match(WILDCARD_W_CURLY_BRACES_RE)
|
|
101
|
+
if (wildcardBracesMatch) {
|
|
102
|
+
const prefix = wildcardBracesMatch[1]!
|
|
103
|
+
const pLength = prefix.length
|
|
104
|
+
output[0] = SEGMENT_TYPE_WILDCARD
|
|
105
|
+
output[1] = start + pLength
|
|
106
|
+
output[2] = start + pLength + 1 // skip '{'
|
|
107
|
+
output[3] = start + pLength + 2 // '$'
|
|
108
|
+
output[4] = start + pLength + 3 // skip '}'
|
|
109
|
+
output[5] = path.length
|
|
110
|
+
return output as ParsedSegment
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const optionalParamBracesMatch = part.match(OPTIONAL_PARAM_W_CURLY_BRACES_RE)
|
|
114
|
+
if (optionalParamBracesMatch) {
|
|
115
|
+
const prefix = optionalParamBracesMatch[1]!
|
|
116
|
+
const paramName = optionalParamBracesMatch[2]!
|
|
117
|
+
const suffix = optionalParamBracesMatch[3]!
|
|
118
|
+
const pLength = prefix.length
|
|
119
|
+
output[0] = SEGMENT_TYPE_OPTIONAL_PARAM
|
|
120
|
+
output[1] = start + pLength
|
|
121
|
+
output[2] = start + pLength + 3 // skip '{-$'
|
|
122
|
+
output[3] = start + pLength + 3 + paramName.length
|
|
123
|
+
output[4] = end - suffix.length
|
|
124
|
+
output[5] = end
|
|
125
|
+
return output as ParsedSegment
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const paramBracesMatch = part.match(PARAM_W_CURLY_BRACES_RE)
|
|
129
|
+
if (paramBracesMatch) {
|
|
130
|
+
const prefix = paramBracesMatch[1]!
|
|
131
|
+
const paramName = paramBracesMatch[2]!
|
|
132
|
+
const suffix = paramBracesMatch[3]!
|
|
133
|
+
const pLength = prefix.length
|
|
134
|
+
output[0] = SEGMENT_TYPE_PARAM
|
|
135
|
+
output[1] = start + pLength
|
|
136
|
+
output[2] = start + pLength + 2 // skip '{$'
|
|
137
|
+
output[3] = start + pLength + 2 + paramName.length
|
|
138
|
+
output[4] = end - suffix.length
|
|
139
|
+
output[5] = end
|
|
140
|
+
return output as ParsedSegment
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// fallback to static pathname (should never happen)
|
|
144
|
+
output[0] = SEGMENT_TYPE_PATHNAME
|
|
145
|
+
output[1] = start
|
|
146
|
+
output[2] = start
|
|
147
|
+
output[3] = end
|
|
148
|
+
output[4] = end
|
|
149
|
+
output[5] = end
|
|
150
|
+
return output as ParsedSegment
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Recursively parses the segments of the given route tree and populates a segment trie.
|
|
155
|
+
*
|
|
156
|
+
* @param data A reusable Uint16Array for parsing segments. (non important, we're just avoiding allocations)
|
|
157
|
+
* @param route The current route to parse.
|
|
158
|
+
* @param start The starting index for parsing within the route's full path.
|
|
159
|
+
* @param node The current segment node in the trie to populate.
|
|
160
|
+
* @param onRoute Callback invoked for each route processed.
|
|
161
|
+
*/
|
|
162
|
+
function parseSegments<TRouteLike extends RouteLike>(
|
|
163
|
+
defaultCaseSensitive: boolean,
|
|
164
|
+
data: Uint16Array,
|
|
165
|
+
route: TRouteLike,
|
|
166
|
+
start: number,
|
|
167
|
+
node: AnySegmentNode<TRouteLike>,
|
|
168
|
+
depth: number,
|
|
169
|
+
onRoute?: (route: TRouteLike) => void,
|
|
170
|
+
) {
|
|
171
|
+
onRoute?.(route)
|
|
172
|
+
let cursor = start
|
|
173
|
+
{
|
|
174
|
+
const path = route.fullPath ?? route.from
|
|
175
|
+
const length = path.length
|
|
176
|
+
const caseSensitive = route.options?.caseSensitive ?? defaultCaseSensitive
|
|
177
|
+
while (cursor < length) {
|
|
178
|
+
const segment = parseSegment(path, cursor, data)
|
|
179
|
+
let nextNode: AnySegmentNode<TRouteLike>
|
|
180
|
+
const start = cursor
|
|
181
|
+
const end = segment[5]
|
|
182
|
+
cursor = end + 1
|
|
183
|
+
depth++
|
|
184
|
+
const kind = segment[0]
|
|
185
|
+
switch (kind) {
|
|
186
|
+
case SEGMENT_TYPE_PATHNAME: {
|
|
187
|
+
const value = path.substring(segment[2], segment[3])
|
|
188
|
+
if (caseSensitive) {
|
|
189
|
+
const existingNode = node.static?.get(value)
|
|
190
|
+
if (existingNode) {
|
|
191
|
+
nextNode = existingNode
|
|
192
|
+
} else {
|
|
193
|
+
node.static ??= new Map()
|
|
194
|
+
const next = createStaticNode<TRouteLike>(
|
|
195
|
+
route.fullPath ?? route.from,
|
|
196
|
+
)
|
|
197
|
+
next.parent = node
|
|
198
|
+
next.depth = depth
|
|
199
|
+
nextNode = next
|
|
200
|
+
node.static.set(value, next)
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
const name = value.toLowerCase()
|
|
204
|
+
const existingNode = node.staticInsensitive?.get(name)
|
|
205
|
+
if (existingNode) {
|
|
206
|
+
nextNode = existingNode
|
|
207
|
+
} else {
|
|
208
|
+
node.staticInsensitive ??= new Map()
|
|
209
|
+
const next = createStaticNode<TRouteLike>(
|
|
210
|
+
route.fullPath ?? route.from,
|
|
211
|
+
)
|
|
212
|
+
next.parent = node
|
|
213
|
+
next.depth = depth
|
|
214
|
+
nextNode = next
|
|
215
|
+
node.staticInsensitive.set(name, next)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
break
|
|
219
|
+
}
|
|
220
|
+
case SEGMENT_TYPE_PARAM: {
|
|
221
|
+
const prefix_raw = path.substring(start, segment[1])
|
|
222
|
+
const suffix_raw = path.substring(segment[4], end)
|
|
223
|
+
const actuallyCaseSensitive =
|
|
224
|
+
caseSensitive && !!(prefix_raw || suffix_raw)
|
|
225
|
+
const prefix = !prefix_raw
|
|
226
|
+
? undefined
|
|
227
|
+
: actuallyCaseSensitive
|
|
228
|
+
? prefix_raw
|
|
229
|
+
: prefix_raw.toLowerCase()
|
|
230
|
+
const suffix = !suffix_raw
|
|
231
|
+
? undefined
|
|
232
|
+
: actuallyCaseSensitive
|
|
233
|
+
? suffix_raw
|
|
234
|
+
: suffix_raw.toLowerCase()
|
|
235
|
+
const existingNode = node.dynamic?.find(
|
|
236
|
+
(s) =>
|
|
237
|
+
s.caseSensitive === actuallyCaseSensitive &&
|
|
238
|
+
s.prefix === prefix &&
|
|
239
|
+
s.suffix === suffix,
|
|
240
|
+
)
|
|
241
|
+
if (existingNode) {
|
|
242
|
+
nextNode = existingNode
|
|
243
|
+
} else {
|
|
244
|
+
const next = createDynamicNode<TRouteLike>(
|
|
245
|
+
SEGMENT_TYPE_PARAM,
|
|
246
|
+
route.fullPath ?? route.from,
|
|
247
|
+
actuallyCaseSensitive,
|
|
248
|
+
prefix,
|
|
249
|
+
suffix,
|
|
250
|
+
)
|
|
251
|
+
nextNode = next
|
|
252
|
+
next.depth = depth
|
|
253
|
+
next.parent = node
|
|
254
|
+
node.dynamic ??= []
|
|
255
|
+
node.dynamic.push(next)
|
|
256
|
+
}
|
|
257
|
+
break
|
|
258
|
+
}
|
|
259
|
+
case SEGMENT_TYPE_OPTIONAL_PARAM: {
|
|
260
|
+
const prefix_raw = path.substring(start, segment[1])
|
|
261
|
+
const suffix_raw = path.substring(segment[4], end)
|
|
262
|
+
const actuallyCaseSensitive =
|
|
263
|
+
caseSensitive && !!(prefix_raw || suffix_raw)
|
|
264
|
+
const prefix = !prefix_raw
|
|
265
|
+
? undefined
|
|
266
|
+
: actuallyCaseSensitive
|
|
267
|
+
? prefix_raw
|
|
268
|
+
: prefix_raw.toLowerCase()
|
|
269
|
+
const suffix = !suffix_raw
|
|
270
|
+
? undefined
|
|
271
|
+
: actuallyCaseSensitive
|
|
272
|
+
? suffix_raw
|
|
273
|
+
: suffix_raw.toLowerCase()
|
|
274
|
+
const existingNode = node.optional?.find(
|
|
275
|
+
(s) =>
|
|
276
|
+
s.caseSensitive === actuallyCaseSensitive &&
|
|
277
|
+
s.prefix === prefix &&
|
|
278
|
+
s.suffix === suffix,
|
|
279
|
+
)
|
|
280
|
+
if (existingNode) {
|
|
281
|
+
nextNode = existingNode
|
|
282
|
+
} else {
|
|
283
|
+
const next = createDynamicNode<TRouteLike>(
|
|
284
|
+
SEGMENT_TYPE_OPTIONAL_PARAM,
|
|
285
|
+
route.fullPath ?? route.from,
|
|
286
|
+
actuallyCaseSensitive,
|
|
287
|
+
prefix,
|
|
288
|
+
suffix,
|
|
289
|
+
)
|
|
290
|
+
nextNode = next
|
|
291
|
+
next.parent = node
|
|
292
|
+
next.depth = depth
|
|
293
|
+
node.optional ??= []
|
|
294
|
+
node.optional.push(next)
|
|
295
|
+
}
|
|
296
|
+
break
|
|
297
|
+
}
|
|
298
|
+
case SEGMENT_TYPE_WILDCARD: {
|
|
299
|
+
const prefix_raw = path.substring(start, segment[1])
|
|
300
|
+
const suffix_raw = path.substring(segment[4], end)
|
|
301
|
+
const actuallyCaseSensitive =
|
|
302
|
+
caseSensitive && !!(prefix_raw || suffix_raw)
|
|
303
|
+
const prefix = !prefix_raw
|
|
304
|
+
? undefined
|
|
305
|
+
: actuallyCaseSensitive
|
|
306
|
+
? prefix_raw
|
|
307
|
+
: prefix_raw.toLowerCase()
|
|
308
|
+
const suffix = !suffix_raw
|
|
309
|
+
? undefined
|
|
310
|
+
: actuallyCaseSensitive
|
|
311
|
+
? suffix_raw
|
|
312
|
+
: suffix_raw.toLowerCase()
|
|
313
|
+
const next = createDynamicNode<TRouteLike>(
|
|
314
|
+
SEGMENT_TYPE_WILDCARD,
|
|
315
|
+
route.fullPath ?? route.from,
|
|
316
|
+
actuallyCaseSensitive,
|
|
317
|
+
prefix,
|
|
318
|
+
suffix,
|
|
319
|
+
)
|
|
320
|
+
nextNode = next
|
|
321
|
+
next.parent = node
|
|
322
|
+
next.depth = depth
|
|
323
|
+
node.wildcard ??= []
|
|
324
|
+
node.wildcard.push(next)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
node = nextNode
|
|
328
|
+
}
|
|
329
|
+
if ((route.path || !route.children) && !route.isRoot) {
|
|
330
|
+
const isIndex = path.endsWith('/')
|
|
331
|
+
// we cannot fuzzy match an index route,
|
|
332
|
+
// but if there is *also* a layout route at this path, save it as notFound
|
|
333
|
+
// we can use it when fuzzy matching to display the NotFound component in the layout route
|
|
334
|
+
if (!isIndex) node.notFound = route
|
|
335
|
+
if (!node.route || (!node.isIndex && isIndex)) node.route = route
|
|
336
|
+
node.isIndex ||= isIndex
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (route.children)
|
|
340
|
+
for (const child of route.children) {
|
|
341
|
+
parseSegments(
|
|
342
|
+
defaultCaseSensitive,
|
|
343
|
+
data,
|
|
344
|
+
child as TRouteLike,
|
|
345
|
+
cursor,
|
|
346
|
+
node,
|
|
347
|
+
depth,
|
|
348
|
+
onRoute,
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function sortDynamic(
|
|
354
|
+
a: { prefix?: string; suffix?: string; caseSensitive: boolean },
|
|
355
|
+
b: { prefix?: string; suffix?: string; caseSensitive: boolean },
|
|
356
|
+
) {
|
|
357
|
+
if (a.prefix && b.prefix && a.prefix !== b.prefix) {
|
|
358
|
+
if (a.prefix.startsWith(b.prefix)) return -1
|
|
359
|
+
if (b.prefix.startsWith(a.prefix)) return 1
|
|
360
|
+
}
|
|
361
|
+
if (a.suffix && b.suffix && a.suffix !== b.suffix) {
|
|
362
|
+
if (a.suffix.endsWith(b.suffix)) return -1
|
|
363
|
+
if (b.suffix.endsWith(a.suffix)) return 1
|
|
364
|
+
}
|
|
365
|
+
if (a.prefix && !b.prefix) return -1
|
|
366
|
+
if (!a.prefix && b.prefix) return 1
|
|
367
|
+
if (a.suffix && !b.suffix) return -1
|
|
368
|
+
if (!a.suffix && b.suffix) return 1
|
|
369
|
+
if (a.caseSensitive && !b.caseSensitive) return -1
|
|
370
|
+
if (!a.caseSensitive && b.caseSensitive) return 1
|
|
371
|
+
|
|
372
|
+
// we don't need a tiebreaker here
|
|
373
|
+
// at this point the 2 nodes cannot conflict during matching
|
|
374
|
+
return 0
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function sortTreeNodes(node: SegmentNode<RouteLike>) {
|
|
378
|
+
if (node.static) {
|
|
379
|
+
for (const child of node.static.values()) {
|
|
380
|
+
sortTreeNodes(child)
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (node.staticInsensitive) {
|
|
384
|
+
for (const child of node.staticInsensitive.values()) {
|
|
385
|
+
sortTreeNodes(child)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (node.dynamic?.length) {
|
|
389
|
+
node.dynamic.sort(sortDynamic)
|
|
390
|
+
for (const child of node.dynamic) {
|
|
391
|
+
sortTreeNodes(child)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (node.optional?.length) {
|
|
395
|
+
node.optional.sort(sortDynamic)
|
|
396
|
+
for (const child of node.optional) {
|
|
397
|
+
sortTreeNodes(child)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (node.wildcard?.length) {
|
|
401
|
+
node.wildcard.sort(sortDynamic)
|
|
402
|
+
for (const child of node.wildcard) {
|
|
403
|
+
sortTreeNodes(child)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function createStaticNode<T extends RouteLike>(
|
|
409
|
+
fullPath: string,
|
|
410
|
+
): StaticSegmentNode<T> {
|
|
411
|
+
return {
|
|
412
|
+
kind: SEGMENT_TYPE_PATHNAME,
|
|
413
|
+
depth: 0,
|
|
414
|
+
static: null,
|
|
415
|
+
staticInsensitive: null,
|
|
416
|
+
dynamic: null,
|
|
417
|
+
optional: null,
|
|
418
|
+
wildcard: null,
|
|
419
|
+
route: null,
|
|
420
|
+
fullPath,
|
|
421
|
+
parent: null,
|
|
422
|
+
isIndex: false,
|
|
423
|
+
notFound: null,
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Keys must be declared in the same order as in `SegmentNode` type,
|
|
429
|
+
* to ensure they are represented as the same object class in the engine.
|
|
430
|
+
*/
|
|
431
|
+
function createDynamicNode<T extends RouteLike>(
|
|
432
|
+
kind:
|
|
433
|
+
| typeof SEGMENT_TYPE_PARAM
|
|
434
|
+
| typeof SEGMENT_TYPE_WILDCARD
|
|
435
|
+
| typeof SEGMENT_TYPE_OPTIONAL_PARAM,
|
|
436
|
+
fullPath: string,
|
|
437
|
+
caseSensitive: boolean,
|
|
438
|
+
prefix?: string,
|
|
439
|
+
suffix?: string,
|
|
440
|
+
): DynamicSegmentNode<T> {
|
|
441
|
+
return {
|
|
442
|
+
kind,
|
|
443
|
+
depth: 0,
|
|
444
|
+
static: null,
|
|
445
|
+
staticInsensitive: null,
|
|
446
|
+
dynamic: null,
|
|
447
|
+
optional: null,
|
|
448
|
+
wildcard: null,
|
|
449
|
+
route: null,
|
|
450
|
+
fullPath,
|
|
451
|
+
parent: null,
|
|
452
|
+
isIndex: false,
|
|
453
|
+
notFound: null,
|
|
454
|
+
caseSensitive,
|
|
455
|
+
prefix,
|
|
456
|
+
suffix,
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
type StaticSegmentNode<T extends RouteLike> = SegmentNode<T> & {
|
|
461
|
+
kind: typeof SEGMENT_TYPE_PATHNAME
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
type DynamicSegmentNode<T extends RouteLike> = SegmentNode<T> & {
|
|
465
|
+
kind:
|
|
466
|
+
| typeof SEGMENT_TYPE_PARAM
|
|
467
|
+
| typeof SEGMENT_TYPE_WILDCARD
|
|
468
|
+
| typeof SEGMENT_TYPE_OPTIONAL_PARAM
|
|
469
|
+
prefix?: string
|
|
470
|
+
suffix?: string
|
|
471
|
+
caseSensitive: boolean
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
type AnySegmentNode<T extends RouteLike> =
|
|
475
|
+
| StaticSegmentNode<T>
|
|
476
|
+
| DynamicSegmentNode<T>
|
|
477
|
+
|
|
478
|
+
type SegmentNode<T extends RouteLike> = {
|
|
479
|
+
kind: SegmentKind
|
|
480
|
+
|
|
481
|
+
/** Static segments (highest priority) */
|
|
482
|
+
static: Map<string, StaticSegmentNode<T>> | null
|
|
483
|
+
|
|
484
|
+
/** Case insensitive static segments (second highest priority) */
|
|
485
|
+
staticInsensitive: Map<string, StaticSegmentNode<T>> | null
|
|
486
|
+
|
|
487
|
+
/** Dynamic segments ($param) */
|
|
488
|
+
dynamic: Array<DynamicSegmentNode<T>> | null
|
|
489
|
+
|
|
490
|
+
/** Optional dynamic segments ({-$param}) */
|
|
491
|
+
optional: Array<DynamicSegmentNode<T>> | null
|
|
492
|
+
|
|
493
|
+
/** Wildcard segments ($ - lowest priority) */
|
|
494
|
+
wildcard: Array<DynamicSegmentNode<T>> | null
|
|
495
|
+
|
|
496
|
+
/** Terminal route (if this path can end here) */
|
|
497
|
+
route: T | null
|
|
498
|
+
|
|
499
|
+
/** The full path for this segment node (will only be valid on leaf nodes) */
|
|
500
|
+
fullPath: string
|
|
501
|
+
|
|
502
|
+
parent: AnySegmentNode<T> | null
|
|
503
|
+
|
|
504
|
+
depth: number
|
|
505
|
+
|
|
506
|
+
/** is it an index route (trailing / path), only valid for nodes with a `route` */
|
|
507
|
+
isIndex: boolean
|
|
508
|
+
|
|
509
|
+
/** Same as `route`, but only present if both an "index route" and a "layout route" exist at this path */
|
|
510
|
+
notFound: T | null
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
type RouteLike = {
|
|
514
|
+
path?: string // relative path from the parent,
|
|
515
|
+
children?: Array<RouteLike> // child routes,
|
|
516
|
+
parentRoute?: RouteLike // parent route,
|
|
517
|
+
isRoot?: boolean
|
|
518
|
+
options?: {
|
|
519
|
+
caseSensitive?: boolean
|
|
520
|
+
}
|
|
521
|
+
} &
|
|
522
|
+
// router tree
|
|
523
|
+
(| { fullPath: string; from?: never } // full path from the root
|
|
524
|
+
// flat route masks list
|
|
525
|
+
| { fullPath?: never; from: string } // full path from the root
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
export type ProcessedTree<
|
|
529
|
+
TTree extends Extract<RouteLike, { fullPath: string }>,
|
|
530
|
+
TFlat extends Extract<RouteLike, { from: string }>,
|
|
531
|
+
TSingle extends Extract<RouteLike, { from: string }>,
|
|
532
|
+
> = {
|
|
533
|
+
/** a representation of the `routeTree` as a segment tree */
|
|
534
|
+
segmentTree: AnySegmentNode<TTree>
|
|
535
|
+
/** a mini route tree generated from the flat `routeMasks` list */
|
|
536
|
+
masksTree: AnySegmentNode<TFlat> | null
|
|
537
|
+
/** @deprecated keep until v2 so that `router.matchRoute` can keep not caring about the actual route tree */
|
|
538
|
+
singleCache: Map<any, AnySegmentNode<TSingle>>
|
|
539
|
+
/** a cache of route matches from the `segmentTree` */
|
|
540
|
+
matchCache: LRUCache<string, ReturnType<typeof findMatch<TTree>>>
|
|
541
|
+
/** a cache of route matches from the `masksTree` */
|
|
542
|
+
flatCache: LRUCache<string, ReturnType<typeof findMatch<TFlat>>> | null
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export function processRouteMasks<
|
|
546
|
+
TRouteLike extends Extract<RouteLike, { from: string }>,
|
|
547
|
+
>(
|
|
548
|
+
routeList: Array<TRouteLike>,
|
|
549
|
+
processedTree: ProcessedTree<any, TRouteLike, any>,
|
|
550
|
+
) {
|
|
551
|
+
const segmentTree = createStaticNode<TRouteLike>('/')
|
|
552
|
+
const data = new Uint16Array(6)
|
|
553
|
+
for (const route of routeList) {
|
|
554
|
+
parseSegments(false, data, route, 1, segmentTree, 0)
|
|
555
|
+
}
|
|
556
|
+
sortTreeNodes(segmentTree)
|
|
557
|
+
processedTree.masksTree = segmentTree
|
|
558
|
+
processedTree.flatCache = createLRUCache<
|
|
559
|
+
string,
|
|
560
|
+
ReturnType<typeof findMatch<TRouteLike>>
|
|
561
|
+
>(1000)
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Take an arbitrary list of routes, create a tree from them (if it hasn't been created already), and match a path against it.
|
|
566
|
+
*/
|
|
567
|
+
export function findFlatMatch<T extends Extract<RouteLike, { from: string }>>(
|
|
568
|
+
/** The path to match. */
|
|
569
|
+
path: string,
|
|
570
|
+
/** The `processedTree` returned by the initial `processRouteTree` call. */
|
|
571
|
+
processedTree: ProcessedTree<any, T, any>,
|
|
572
|
+
) {
|
|
573
|
+
path ||= '/'
|
|
574
|
+
const cached = processedTree.flatCache!.get(path)
|
|
575
|
+
if (cached) return cached
|
|
576
|
+
const result = findMatch(path, processedTree.masksTree!)
|
|
577
|
+
processedTree.flatCache!.set(path, result)
|
|
578
|
+
return result
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* @deprecated keep until v2 so that `router.matchRoute` can keep not caring about the actual route tree
|
|
583
|
+
*/
|
|
584
|
+
export function findSingleMatch(
|
|
585
|
+
from: string,
|
|
586
|
+
caseSensitive: boolean,
|
|
587
|
+
fuzzy: boolean,
|
|
588
|
+
path: string,
|
|
589
|
+
processedTree: ProcessedTree<any, any, { from: string }>,
|
|
590
|
+
) {
|
|
591
|
+
from ||= '/'
|
|
592
|
+
path ||= '/'
|
|
593
|
+
const key = caseSensitive ? `case\0${from}` : from
|
|
594
|
+
let tree = processedTree.singleCache.get(key)
|
|
595
|
+
if (!tree) {
|
|
596
|
+
// single flat routes (router.matchRoute) are not eagerly processed,
|
|
597
|
+
// if we haven't seen this route before, process it now
|
|
598
|
+
tree = createStaticNode<{ from: string }>('/')
|
|
599
|
+
const data = new Uint16Array(6)
|
|
600
|
+
parseSegments(caseSensitive, data, { from }, 1, tree, 0)
|
|
601
|
+
processedTree.singleCache.set(key, tree)
|
|
602
|
+
}
|
|
603
|
+
return findMatch(path, tree, fuzzy)
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export function findRouteMatch<
|
|
607
|
+
T extends Extract<RouteLike, { fullPath: string }>,
|
|
608
|
+
>(
|
|
609
|
+
/** The path to match against the route tree. */
|
|
610
|
+
path: string,
|
|
611
|
+
/** The `processedTree` returned by the initial `processRouteTree` call. */
|
|
612
|
+
processedTree: ProcessedTree<T, any, any>,
|
|
613
|
+
/** If `true`, allows fuzzy matching (partial matches), i.e. which node in the tree would have been an exact match if the `path` had been shorter? */
|
|
614
|
+
fuzzy = false,
|
|
615
|
+
) {
|
|
616
|
+
const key = fuzzy ? `fuzzy\0${path}` : path
|
|
617
|
+
const cached = processedTree.matchCache.get(key)
|
|
618
|
+
if (cached) return cached
|
|
619
|
+
path ||= '/'
|
|
620
|
+
const result = findMatch(path, processedTree.segmentTree, fuzzy)
|
|
621
|
+
processedTree.matchCache.set(key, result)
|
|
622
|
+
return result
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** Trim trailing slashes (except preserving root '/'). */
|
|
626
|
+
export function trimPathRight(path: string) {
|
|
627
|
+
return path === '/' ? path : path.replace(/\/{1,}$/, '')
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Processes a route tree into a segment trie for efficient path matching.
|
|
632
|
+
* Also builds lookup maps for routes by ID and by trimmed full path.
|
|
633
|
+
*/
|
|
634
|
+
export function processRouteTree<
|
|
635
|
+
TRouteLike extends Extract<RouteLike, { fullPath: string }> & { id: string },
|
|
636
|
+
>(
|
|
637
|
+
/** The root of the route tree to process. */
|
|
638
|
+
routeTree: TRouteLike,
|
|
639
|
+
/** Whether matching should be case sensitive by default (overridden by individual route options). */
|
|
640
|
+
caseSensitive: boolean = false,
|
|
641
|
+
/** Optional callback invoked for each route during processing. */
|
|
642
|
+
initRoute?: (route: TRouteLike, index: number) => void,
|
|
643
|
+
): {
|
|
644
|
+
/** Should be considered a black box, needs to be provided to all matching functions in this module. */
|
|
645
|
+
processedTree: ProcessedTree<TRouteLike, any, any>
|
|
646
|
+
/** A lookup map of routes by their unique IDs. */
|
|
647
|
+
routesById: Record<string, TRouteLike>
|
|
648
|
+
/** A lookup map of routes by their trimmed full paths. */
|
|
649
|
+
routesByPath: Record<string, TRouteLike>
|
|
650
|
+
} {
|
|
651
|
+
const segmentTree = createStaticNode<TRouteLike>(routeTree.fullPath)
|
|
652
|
+
const data = new Uint16Array(6)
|
|
653
|
+
const routesById = {} as Record<string, TRouteLike>
|
|
654
|
+
const routesByPath = {} as Record<string, TRouteLike>
|
|
655
|
+
let index = 0
|
|
656
|
+
parseSegments(caseSensitive, data, routeTree, 1, segmentTree, 0, (route) => {
|
|
657
|
+
initRoute?.(route, index)
|
|
658
|
+
|
|
659
|
+
invariant(
|
|
660
|
+
!(route.id in routesById),
|
|
661
|
+
`Duplicate routes found with id: ${String(route.id)}`,
|
|
662
|
+
)
|
|
663
|
+
|
|
664
|
+
routesById[route.id] = route
|
|
665
|
+
|
|
666
|
+
if (index !== 0 && route.path) {
|
|
667
|
+
const trimmedFullPath = trimPathRight(route.fullPath)
|
|
668
|
+
if (!routesByPath[trimmedFullPath] || route.fullPath.endsWith('/')) {
|
|
669
|
+
routesByPath[trimmedFullPath] = route
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
index++
|
|
674
|
+
})
|
|
675
|
+
sortTreeNodes(segmentTree)
|
|
676
|
+
const processedTree: ProcessedTree<TRouteLike, any, any> = {
|
|
677
|
+
segmentTree,
|
|
678
|
+
singleCache: new Map(),
|
|
679
|
+
matchCache: createLRUCache<
|
|
680
|
+
string,
|
|
681
|
+
ReturnType<typeof findMatch<TRouteLike>>
|
|
682
|
+
>(1000),
|
|
683
|
+
flatCache: null,
|
|
684
|
+
masksTree: null,
|
|
685
|
+
}
|
|
686
|
+
return {
|
|
687
|
+
processedTree,
|
|
688
|
+
routesById,
|
|
689
|
+
routesByPath,
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function findMatch<T extends RouteLike>(
|
|
694
|
+
path: string,
|
|
695
|
+
segmentTree: AnySegmentNode<T>,
|
|
696
|
+
fuzzy = false,
|
|
697
|
+
): { route: T; params: Record<string, string> } | null {
|
|
698
|
+
const parts = path.split('/')
|
|
699
|
+
const leaf = getNodeMatch(path, parts, segmentTree, fuzzy)
|
|
700
|
+
if (!leaf) return null
|
|
701
|
+
const params = extractParams(path, parts, leaf)
|
|
702
|
+
const isFuzzyMatch = '**' in leaf
|
|
703
|
+
if (isFuzzyMatch) params['**'] = leaf['**']
|
|
704
|
+
const route = isFuzzyMatch
|
|
705
|
+
? (leaf.node.notFound ?? leaf.node.route!)
|
|
706
|
+
: leaf.node.route!
|
|
707
|
+
return {
|
|
708
|
+
route,
|
|
709
|
+
params,
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function extractParams<T extends RouteLike>(
|
|
714
|
+
path: string,
|
|
715
|
+
parts: Array<string>,
|
|
716
|
+
leaf: { node: AnySegmentNode<T>; skipped: number },
|
|
717
|
+
) {
|
|
718
|
+
const list = buildBranch(leaf.node)
|
|
719
|
+
let nodeParts: Array<string> | null = null
|
|
720
|
+
const params: Record<string, string> = {}
|
|
721
|
+
for (
|
|
722
|
+
let partIndex = 0, nodeIndex = 0, pathIndex = 0;
|
|
723
|
+
nodeIndex < list.length;
|
|
724
|
+
partIndex++, nodeIndex++, pathIndex++
|
|
725
|
+
) {
|
|
726
|
+
const node = list[nodeIndex]!
|
|
727
|
+
const part = parts[partIndex]
|
|
728
|
+
const currentPathIndex = pathIndex
|
|
729
|
+
if (part) pathIndex += part.length
|
|
730
|
+
if (node.kind === SEGMENT_TYPE_PARAM) {
|
|
731
|
+
nodeParts ??= leaf.node.fullPath.split('/')
|
|
732
|
+
const nodePart = nodeParts[nodeIndex]!
|
|
733
|
+
const preLength = node.prefix?.length ?? 0
|
|
734
|
+
// we can't rely on the presence of prefix/suffix to know whether it's curly-braced or not, because `/{$param}/` is valid, but has no prefix/suffix
|
|
735
|
+
const isCurlyBraced = nodePart.charCodeAt(preLength) === 123 // '{'
|
|
736
|
+
// param name is extracted at match-time so that tree nodes that are identical except for param name can share the same node
|
|
737
|
+
if (isCurlyBraced) {
|
|
738
|
+
const sufLength = node.suffix?.length ?? 0
|
|
739
|
+
const name = nodePart.substring(
|
|
740
|
+
preLength + 2,
|
|
741
|
+
nodePart.length - sufLength - 1,
|
|
742
|
+
)
|
|
743
|
+
const value = part!.substring(preLength, part!.length - sufLength)
|
|
744
|
+
params[name] = decodeURIComponent(value)
|
|
745
|
+
} else {
|
|
746
|
+
const name = nodePart.substring(1)
|
|
747
|
+
params[name] = decodeURIComponent(part!)
|
|
748
|
+
}
|
|
749
|
+
} else if (node.kind === SEGMENT_TYPE_OPTIONAL_PARAM) {
|
|
750
|
+
if (leaf.skipped & (1 << nodeIndex)) {
|
|
751
|
+
partIndex-- // stay on the same part
|
|
752
|
+
continue
|
|
753
|
+
}
|
|
754
|
+
nodeParts ??= leaf.node.fullPath.split('/')
|
|
755
|
+
const nodePart = nodeParts[nodeIndex]!
|
|
756
|
+
const preLength = node.prefix?.length ?? 0
|
|
757
|
+
const sufLength = node.suffix?.length ?? 0
|
|
758
|
+
const name = nodePart.substring(
|
|
759
|
+
preLength + 3,
|
|
760
|
+
nodePart.length - sufLength - 1,
|
|
761
|
+
)
|
|
762
|
+
const value =
|
|
763
|
+
node.suffix || node.prefix
|
|
764
|
+
? part!.substring(preLength, part!.length - sufLength)
|
|
765
|
+
: part
|
|
766
|
+
if (value) params[name] = decodeURIComponent(value)
|
|
767
|
+
} else if (node.kind === SEGMENT_TYPE_WILDCARD) {
|
|
768
|
+
const n = node
|
|
769
|
+
const value = path.substring(
|
|
770
|
+
currentPathIndex + (n.prefix?.length ?? 0),
|
|
771
|
+
path.length - (n.suffix?.length ?? 0),
|
|
772
|
+
)
|
|
773
|
+
const splat = decodeURIComponent(value)
|
|
774
|
+
// TODO: Deprecate *
|
|
775
|
+
params['*'] = splat
|
|
776
|
+
params._splat = splat
|
|
777
|
+
break
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return params
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
function buildBranch<T extends RouteLike>(node: AnySegmentNode<T>) {
|
|
784
|
+
const list: Array<AnySegmentNode<T>> = Array(node.depth + 1)
|
|
785
|
+
do {
|
|
786
|
+
list[node.depth] = node
|
|
787
|
+
node = node.parent!
|
|
788
|
+
} while (node)
|
|
789
|
+
return list
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
type MatchStackFrame<T extends RouteLike> = {
|
|
793
|
+
node: AnySegmentNode<T>
|
|
794
|
+
/** index of the segment of path */
|
|
795
|
+
index: number
|
|
796
|
+
/** how many nodes between `node` and the root of the segment tree */
|
|
797
|
+
depth: number
|
|
798
|
+
/**
|
|
799
|
+
* Bitmask of skipped optional segments.
|
|
800
|
+
*
|
|
801
|
+
* This is a very performant way of storing an "array of booleans", but it means beyond 32 segments we can't track skipped optionals.
|
|
802
|
+
* If we really really need to support more than 32 segments we can switch to using a `BigInt` here. It's about 2x slower in worst case scenarios.
|
|
803
|
+
*/
|
|
804
|
+
skipped: number
|
|
805
|
+
statics: number
|
|
806
|
+
dynamics: number
|
|
807
|
+
optionals: number
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
function getNodeMatch<T extends RouteLike>(
|
|
811
|
+
path: string,
|
|
812
|
+
parts: Array<string>,
|
|
813
|
+
segmentTree: AnySegmentNode<T>,
|
|
814
|
+
fuzzy: boolean,
|
|
815
|
+
) {
|
|
816
|
+
const trailingSlash = !last(parts)
|
|
817
|
+
const pathIsIndex = trailingSlash && path !== '/'
|
|
818
|
+
const partsLength = parts.length - (trailingSlash ? 1 : 0)
|
|
819
|
+
|
|
820
|
+
type Frame = MatchStackFrame<T>
|
|
821
|
+
|
|
822
|
+
// use a stack to explore all possible paths (params cause branching)
|
|
823
|
+
// iterate "backwards" (low priority first) so that we can push() each candidate, and pop() the highest priority candidate first
|
|
824
|
+
// - pros: it is depth-first, so we find full matches faster
|
|
825
|
+
// - cons: we cannot short-circuit, because highest priority matches are at the end of the loop (for loop with i--) (but we have no good short-circuiting anyway)
|
|
826
|
+
// other possible approaches:
|
|
827
|
+
// - shift instead of pop (measure performance difference), this allows iterating "forwards" (effectively breadth-first)
|
|
828
|
+
// - never remove from the stack, keep a cursor instead. Then we can push "forwards" and avoid reversing the order of candidates (effectively breadth-first)
|
|
829
|
+
const stack: Array<Frame> = [
|
|
830
|
+
{
|
|
831
|
+
node: segmentTree,
|
|
832
|
+
index: 1,
|
|
833
|
+
skipped: 0,
|
|
834
|
+
depth: 1,
|
|
835
|
+
statics: 1,
|
|
836
|
+
dynamics: 0,
|
|
837
|
+
optionals: 0,
|
|
838
|
+
},
|
|
839
|
+
]
|
|
840
|
+
|
|
841
|
+
let wildcardMatch: Frame | null = null
|
|
842
|
+
let bestFuzzy: Frame | null = null
|
|
843
|
+
let bestMatch: Frame | null = null
|
|
844
|
+
|
|
845
|
+
while (stack.length) {
|
|
846
|
+
const frame = stack.pop()!
|
|
847
|
+
// eslint-disable-next-line prefer-const
|
|
848
|
+
let { node, index, skipped, depth, statics, dynamics, optionals } = frame
|
|
849
|
+
|
|
850
|
+
// In fuzzy mode, track the best partial match we've found so far
|
|
851
|
+
if (fuzzy && node.notFound && isFrameMoreSpecific(bestFuzzy, frame)) {
|
|
852
|
+
bestFuzzy = frame
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const isBeyondPath = index === partsLength
|
|
856
|
+
if (isBeyondPath) {
|
|
857
|
+
if (node.route && (!pathIsIndex || node.isIndex)) {
|
|
858
|
+
if (isFrameMoreSpecific(bestMatch, frame)) {
|
|
859
|
+
bestMatch = frame
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// perfect match, no need to continue
|
|
863
|
+
if (statics === partsLength) return bestMatch
|
|
864
|
+
}
|
|
865
|
+
// beyond the length of the path parts, only skipped optional segments or wildcard segments can match
|
|
866
|
+
if (!node.optional && !node.wildcard) continue
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
const part = isBeyondPath ? undefined : parts[index]!
|
|
870
|
+
let lowerPart: string
|
|
871
|
+
|
|
872
|
+
// 5. Try wildcard match
|
|
873
|
+
if (node.wildcard && isFrameMoreSpecific(wildcardMatch, frame)) {
|
|
874
|
+
for (const segment of node.wildcard) {
|
|
875
|
+
const { prefix, suffix } = segment
|
|
876
|
+
if (prefix) {
|
|
877
|
+
if (isBeyondPath) continue
|
|
878
|
+
const casePart = segment.caseSensitive
|
|
879
|
+
? part
|
|
880
|
+
: (lowerPart ??= part!.toLowerCase())
|
|
881
|
+
if (!casePart!.startsWith(prefix)) continue
|
|
882
|
+
}
|
|
883
|
+
if (suffix) {
|
|
884
|
+
if (isBeyondPath) continue
|
|
885
|
+
const end = parts.slice(index).join('/').slice(-suffix.length)
|
|
886
|
+
const casePart = segment.caseSensitive ? end : end.toLowerCase()
|
|
887
|
+
if (casePart !== suffix) continue
|
|
888
|
+
}
|
|
889
|
+
// the first wildcard match is the highest priority one
|
|
890
|
+
wildcardMatch = {
|
|
891
|
+
node: segment,
|
|
892
|
+
index,
|
|
893
|
+
skipped,
|
|
894
|
+
depth,
|
|
895
|
+
statics,
|
|
896
|
+
dynamics,
|
|
897
|
+
optionals,
|
|
898
|
+
}
|
|
899
|
+
break
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// 4. Try optional match
|
|
904
|
+
if (node.optional) {
|
|
905
|
+
const nextSkipped = skipped | (1 << depth)
|
|
906
|
+
const nextDepth = depth + 1
|
|
907
|
+
for (let i = node.optional.length - 1; i >= 0; i--) {
|
|
908
|
+
const segment = node.optional[i]!
|
|
909
|
+
// when skipping, node and depth advance by 1, but index doesn't
|
|
910
|
+
stack.push({
|
|
911
|
+
node: segment,
|
|
912
|
+
index,
|
|
913
|
+
skipped: nextSkipped,
|
|
914
|
+
depth: nextDepth,
|
|
915
|
+
statics,
|
|
916
|
+
dynamics,
|
|
917
|
+
optionals,
|
|
918
|
+
}) // enqueue skipping the optional
|
|
919
|
+
}
|
|
920
|
+
if (!isBeyondPath) {
|
|
921
|
+
for (let i = node.optional.length - 1; i >= 0; i--) {
|
|
922
|
+
const segment = node.optional[i]!
|
|
923
|
+
const { prefix, suffix } = segment
|
|
924
|
+
if (prefix || suffix) {
|
|
925
|
+
const casePart = segment.caseSensitive
|
|
926
|
+
? part!
|
|
927
|
+
: (lowerPart ??= part!.toLowerCase())
|
|
928
|
+
if (prefix && !casePart.startsWith(prefix)) continue
|
|
929
|
+
if (suffix && !casePart.endsWith(suffix)) continue
|
|
930
|
+
}
|
|
931
|
+
stack.push({
|
|
932
|
+
node: segment,
|
|
933
|
+
index: index + 1,
|
|
934
|
+
skipped,
|
|
935
|
+
depth: nextDepth,
|
|
936
|
+
statics,
|
|
937
|
+
dynamics,
|
|
938
|
+
optionals: optionals + 1,
|
|
939
|
+
})
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
// 3. Try dynamic match
|
|
945
|
+
if (!isBeyondPath && node.dynamic && part) {
|
|
946
|
+
for (let i = node.dynamic.length - 1; i >= 0; i--) {
|
|
947
|
+
const segment = node.dynamic[i]!
|
|
948
|
+
const { prefix, suffix } = segment
|
|
949
|
+
if (prefix || suffix) {
|
|
950
|
+
const casePart = segment.caseSensitive
|
|
951
|
+
? part
|
|
952
|
+
: (lowerPart ??= part.toLowerCase())
|
|
953
|
+
if (prefix && !casePart.startsWith(prefix)) continue
|
|
954
|
+
if (suffix && !casePart.endsWith(suffix)) continue
|
|
955
|
+
}
|
|
956
|
+
stack.push({
|
|
957
|
+
node: segment,
|
|
958
|
+
index: index + 1,
|
|
959
|
+
skipped,
|
|
960
|
+
depth: depth + 1,
|
|
961
|
+
statics,
|
|
962
|
+
dynamics: dynamics + 1,
|
|
963
|
+
optionals,
|
|
964
|
+
})
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
// 2. Try case insensitive static match
|
|
969
|
+
if (!isBeyondPath && node.staticInsensitive) {
|
|
970
|
+
const match = node.staticInsensitive.get(
|
|
971
|
+
(lowerPart ??= part!.toLowerCase()),
|
|
972
|
+
)
|
|
973
|
+
if (match) {
|
|
974
|
+
stack.push({
|
|
975
|
+
node: match,
|
|
976
|
+
index: index + 1,
|
|
977
|
+
skipped,
|
|
978
|
+
depth: depth + 1,
|
|
979
|
+
statics: statics + 1,
|
|
980
|
+
dynamics,
|
|
981
|
+
optionals,
|
|
982
|
+
})
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
// 1. Try static match
|
|
987
|
+
if (!isBeyondPath && node.static) {
|
|
988
|
+
const match = node.static.get(part!)
|
|
989
|
+
if (match) {
|
|
990
|
+
stack.push({
|
|
991
|
+
node: match,
|
|
992
|
+
index: index + 1,
|
|
993
|
+
skipped,
|
|
994
|
+
depth: depth + 1,
|
|
995
|
+
statics: statics + 1,
|
|
996
|
+
dynamics,
|
|
997
|
+
optionals,
|
|
998
|
+
})
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
if (bestMatch) return bestMatch
|
|
1004
|
+
|
|
1005
|
+
if (wildcardMatch) return wildcardMatch
|
|
1006
|
+
|
|
1007
|
+
if (fuzzy && bestFuzzy) {
|
|
1008
|
+
let sliceIndex = bestFuzzy.index
|
|
1009
|
+
for (let i = 0; i < bestFuzzy.index; i++) {
|
|
1010
|
+
sliceIndex += parts[i]!.length
|
|
1011
|
+
}
|
|
1012
|
+
const splat = sliceIndex === path.length ? '/' : path.slice(sliceIndex)
|
|
1013
|
+
return {
|
|
1014
|
+
node: bestFuzzy.node,
|
|
1015
|
+
skipped: bestFuzzy.skipped,
|
|
1016
|
+
'**': decodeURIComponent(splat),
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
return null
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function isFrameMoreSpecific(
|
|
1024
|
+
// the stack frame previously saved as "best match"
|
|
1025
|
+
prev: MatchStackFrame<any> | null,
|
|
1026
|
+
// the candidate stack frame
|
|
1027
|
+
next: MatchStackFrame<any>,
|
|
1028
|
+
): boolean {
|
|
1029
|
+
if (!prev) return true
|
|
1030
|
+
return (
|
|
1031
|
+
next.statics > prev.statics ||
|
|
1032
|
+
(next.statics === prev.statics &&
|
|
1033
|
+
(next.dynamics > prev.dynamics ||
|
|
1034
|
+
(next.dynamics === prev.dynamics && next.optionals > prev.optionals)))
|
|
1035
|
+
)
|
|
1036
|
+
}
|