@rangojs/router 0.0.0-experimental.0f44aca1
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/AGENTS.md +5 -0
- package/README.md +899 -0
- package/dist/bin/rango.js +1601 -0
- package/dist/vite/index.js +5214 -0
- package/package.json +176 -0
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +262 -0
- package/skills/caching/SKILL.md +220 -0
- package/skills/composability/SKILL.md +172 -0
- package/skills/debug-manifest/SKILL.md +112 -0
- package/skills/document-cache/SKILL.md +182 -0
- package/skills/fonts/SKILL.md +167 -0
- package/skills/hooks/SKILL.md +704 -0
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +313 -0
- package/skills/layout/SKILL.md +310 -0
- package/skills/links/SKILL.md +239 -0
- package/skills/loader/SKILL.md +596 -0
- package/skills/middleware/SKILL.md +339 -0
- package/skills/mime-routes/SKILL.md +128 -0
- package/skills/parallel/SKILL.md +305 -0
- package/skills/prerender/SKILL.md +643 -0
- package/skills/rango/SKILL.md +118 -0
- package/skills/response-routes/SKILL.md +411 -0
- package/skills/route/SKILL.md +385 -0
- package/skills/router-setup/SKILL.md +439 -0
- package/skills/tailwind/SKILL.md +129 -0
- package/skills/theme/SKILL.md +79 -0
- package/skills/typesafety/SKILL.md +623 -0
- package/skills/use-cache/SKILL.md +324 -0
- package/src/__internal.ts +273 -0
- package/src/bin/rango.ts +321 -0
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/action-response-classifier.ts +99 -0
- package/src/browser/event-controller.ts +899 -0
- package/src/browser/history-state.ts +80 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/link-interceptor.ts +141 -0
- package/src/browser/logging.ts +55 -0
- package/src/browser/merge-segment-loaders.ts +134 -0
- package/src/browser/navigation-bridge.ts +645 -0
- package/src/browser/navigation-client.ts +215 -0
- package/src/browser/navigation-store.ts +806 -0
- package/src/browser/navigation-transaction.ts +295 -0
- package/src/browser/network-error-handler.ts +61 -0
- package/src/browser/partial-update.ts +550 -0
- package/src/browser/prefetch/cache.ts +146 -0
- package/src/browser/prefetch/fetch.ts +135 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +42 -0
- package/src/browser/prefetch/queue.ts +88 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +360 -0
- package/src/browser/react/NavigationProvider.tsx +386 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +59 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +162 -0
- package/src/browser/react/location-state.ts +107 -0
- package/src/browser/react/mount-context.ts +37 -0
- package/src/browser/react/nonce-context.ts +23 -0
- package/src/browser/react/shallow-equal.ts +27 -0
- package/src/browser/react/use-action.ts +218 -0
- package/src/browser/react/use-client-cache.ts +58 -0
- package/src/browser/react/use-handle.ts +162 -0
- package/src/browser/react/use-href.tsx +40 -0
- package/src/browser/react/use-link-status.ts +135 -0
- package/src/browser/react/use-mount.ts +31 -0
- package/src/browser/react/use-navigation.ts +99 -0
- package/src/browser/react/use-params.ts +65 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-router.ts +63 -0
- package/src/browser/react/use-search-params.ts +56 -0
- package/src/browser/react/use-segments.ts +171 -0
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +431 -0
- package/src/browser/scroll-restoration.ts +400 -0
- package/src/browser/segment-reconciler.ts +216 -0
- package/src/browser/segment-structure-assert.ts +83 -0
- package/src/browser/server-action-bridge.ts +667 -0
- package/src/browser/shallow.ts +40 -0
- package/src/browser/types.ts +538 -0
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +438 -0
- package/src/build/generate-route-types.ts +36 -0
- package/src/build/index.ts +35 -0
- package/src/build/route-trie.ts +265 -0
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +98 -0
- package/src/build/route-types/codegen.ts +102 -0
- package/src/build/route-types/include-resolution.ts +411 -0
- package/src/build/route-types/param-extraction.ts +48 -0
- package/src/build/route-types/per-module-writer.ts +128 -0
- package/src/build/route-types/router-processing.ts +469 -0
- package/src/build/route-types/scan-filter.ts +78 -0
- package/src/build/runtime-discovery.ts +231 -0
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-key-utils.ts +44 -0
- package/src/cache/cache-policy.ts +125 -0
- package/src/cache/cache-runtime.ts +338 -0
- package/src/cache/cache-scope.ts +382 -0
- package/src/cache/cf/cf-cache-store.ts +540 -0
- package/src/cache/cf/index.ts +25 -0
- package/src/cache/document-cache.ts +369 -0
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +41 -0
- package/src/cache/index.ts +43 -0
- package/src/cache/memory-segment-store.ts +328 -0
- package/src/cache/profile-registry.ts +73 -0
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +256 -0
- package/src/cache/taint.ts +98 -0
- package/src/cache/types.ts +342 -0
- package/src/client.rsc.tsx +85 -0
- package/src/client.tsx +601 -0
- package/src/component-utils.ts +76 -0
- package/src/components/DefaultDocument.tsx +27 -0
- package/src/context-var.ts +86 -0
- package/src/debug.ts +243 -0
- package/src/default-error-boundary.tsx +88 -0
- package/src/deps/browser.ts +8 -0
- package/src/deps/html-stream-client.ts +2 -0
- package/src/deps/html-stream-server.ts +2 -0
- package/src/deps/rsc.ts +10 -0
- package/src/deps/ssr.ts +2 -0
- package/src/errors.ts +365 -0
- package/src/handle.ts +135 -0
- package/src/handles/MetaTags.tsx +246 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +7 -0
- package/src/handles/meta.ts +264 -0
- package/src/host/cookie-handler.ts +165 -0
- package/src/host/errors.ts +97 -0
- package/src/host/index.ts +53 -0
- package/src/host/pattern-matcher.ts +214 -0
- package/src/host/router.ts +352 -0
- package/src/host/testing.ts +79 -0
- package/src/host/types.ts +146 -0
- package/src/host/utils.ts +25 -0
- package/src/href-client.ts +222 -0
- package/src/index.rsc.ts +233 -0
- package/src/index.ts +277 -0
- package/src/internal-debug.ts +11 -0
- package/src/loader.rsc.ts +89 -0
- package/src/loader.ts +64 -0
- package/src/network-error-thrower.tsx +23 -0
- package/src/outlet-context.ts +15 -0
- package/src/outlet-provider.tsx +45 -0
- package/src/prerender/param-hash.ts +37 -0
- package/src/prerender/store.ts +185 -0
- package/src/prerender.ts +463 -0
- package/src/reverse.ts +330 -0
- package/src/root-error-boundary.tsx +289 -0
- package/src/route-content-wrapper.tsx +196 -0
- package/src/route-definition/dsl-helpers.ts +934 -0
- package/src/route-definition/helper-factories.ts +200 -0
- package/src/route-definition/helpers-types.ts +430 -0
- package/src/route-definition/index.ts +52 -0
- package/src/route-definition/redirect.ts +93 -0
- package/src/route-definition.ts +1 -0
- package/src/route-map-builder.ts +275 -0
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +259 -0
- package/src/router/content-negotiation.ts +116 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/find-match.ts +158 -0
- package/src/router/handler-context.ts +451 -0
- package/src/router/intercept-resolution.ts +395 -0
- package/src/router/lazy-includes.ts +234 -0
- package/src/router/loader-resolution.ts +420 -0
- package/src/router/logging.ts +248 -0
- package/src/router/manifest.ts +267 -0
- package/src/router/match-api.ts +620 -0
- package/src/router/match-context.ts +266 -0
- package/src/router/match-handlers.ts +440 -0
- package/src/router/match-middleware/background-revalidation.ts +223 -0
- package/src/router/match-middleware/cache-lookup.ts +634 -0
- package/src/router/match-middleware/cache-store.ts +295 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +306 -0
- package/src/router/match-middleware/segment-resolution.ts +192 -0
- package/src/router/match-pipelines.ts +179 -0
- package/src/router/match-result.ts +219 -0
- package/src/router/metrics.ts +282 -0
- package/src/router/middleware-cookies.ts +55 -0
- package/src/router/middleware-types.ts +222 -0
- package/src/router/middleware.ts +748 -0
- package/src/router/pattern-matching.ts +563 -0
- package/src/router/prerender-match.ts +402 -0
- package/src/router/preview-match.ts +170 -0
- package/src/router/revalidation.ts +289 -0
- package/src/router/router-context.ts +316 -0
- package/src/router/router-interfaces.ts +452 -0
- package/src/router/router-options.ts +592 -0
- package/src/router/router-registry.ts +24 -0
- package/src/router/segment-resolution/fresh.ts +570 -0
- package/src/router/segment-resolution/helpers.ts +263 -0
- package/src/router/segment-resolution/loader-cache.ts +198 -0
- package/src/router/segment-resolution/revalidation.ts +1239 -0
- package/src/router/segment-resolution/static-store.ts +67 -0
- package/src/router/segment-resolution.ts +21 -0
- package/src/router/segment-wrappers.ts +289 -0
- package/src/router/telemetry-otel.ts +299 -0
- package/src/router/telemetry.ts +300 -0
- package/src/router/timeout.ts +148 -0
- package/src/router/trie-matching.ts +239 -0
- package/src/router/types.ts +170 -0
- package/src/router.ts +1002 -0
- package/src/rsc/handler-context.ts +45 -0
- package/src/rsc/handler.ts +1089 -0
- package/src/rsc/helpers.ts +198 -0
- package/src/rsc/index.ts +36 -0
- package/src/rsc/loader-fetch.ts +209 -0
- package/src/rsc/manifest-init.ts +86 -0
- package/src/rsc/nonce.ts +32 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +379 -0
- package/src/rsc/response-error.ts +37 -0
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +235 -0
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +348 -0
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +263 -0
- package/src/search-params.ts +230 -0
- package/src/segment-system.tsx +454 -0
- package/src/server/context.ts +591 -0
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +37 -0
- package/src/server/handle-store.ts +308 -0
- package/src/server/loader-registry.ts +133 -0
- package/src/server/request-context.ts +914 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +51 -0
- package/src/ssr/index.tsx +365 -0
- package/src/static-handler.ts +114 -0
- package/src/theme/ThemeProvider.tsx +297 -0
- package/src/theme/ThemeScript.tsx +61 -0
- package/src/theme/constants.ts +62 -0
- package/src/theme/index.ts +48 -0
- package/src/theme/theme-context.ts +44 -0
- package/src/theme/theme-script.ts +155 -0
- package/src/theme/types.ts +182 -0
- package/src/theme/use-theme.ts +44 -0
- package/src/types/boundaries.ts +158 -0
- package/src/types/cache-types.ts +198 -0
- package/src/types/error-types.ts +192 -0
- package/src/types/global-namespace.ts +100 -0
- package/src/types/handler-context.ts +687 -0
- package/src/types/index.ts +88 -0
- package/src/types/loader-types.ts +183 -0
- package/src/types/route-config.ts +170 -0
- package/src/types/route-entry.ts +102 -0
- package/src/types/segments.ts +148 -0
- package/src/types.ts +1 -0
- package/src/urls/include-helper.ts +197 -0
- package/src/urls/index.ts +53 -0
- package/src/urls/path-helper-types.ts +339 -0
- package/src/urls/path-helper.ts +329 -0
- package/src/urls/pattern-types.ts +95 -0
- package/src/urls/response-types.ts +106 -0
- package/src/urls/type-extraction.ts +372 -0
- package/src/urls/urls-function.ts +98 -0
- package/src/urls.ts +1 -0
- package/src/use-loader.tsx +354 -0
- package/src/vite/discovery/bundle-postprocess.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +344 -0
- package/src/vite/discovery/prerender-collection.ts +385 -0
- package/src/vite/discovery/route-types-writer.ts +258 -0
- package/src/vite/discovery/self-gen-tracking.ts +47 -0
- package/src/vite/discovery/state.ts +110 -0
- package/src/vite/discovery/virtual-module-codegen.ts +203 -0
- package/src/vite/index.ts +16 -0
- package/src/vite/plugin-types.ts +131 -0
- package/src/vite/plugins/cjs-to-esm.ts +93 -0
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +105 -0
- package/src/vite/plugins/expose-action-id.ts +365 -0
- package/src/vite/plugins/expose-id-utils.ts +287 -0
- package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +179 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +569 -0
- package/src/vite/plugins/refresh-cmd.ts +65 -0
- package/src/vite/plugins/use-cache-transform.ts +323 -0
- package/src/vite/plugins/version-injector.ts +83 -0
- package/src/vite/plugins/version-plugin.ts +254 -0
- package/src/vite/plugins/version.d.ts +12 -0
- package/src/vite/plugins/virtual-entries.ts +123 -0
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +510 -0
- package/src/vite/router-discovery.ts +785 -0
- package/src/vite/utils/ast-handler-extract.ts +517 -0
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +137 -0
- package/src/vite/utils/manifest-utils.ts +70 -0
- package/src/vite/utils/package-resolution.ts +121 -0
- package/src/vite/utils/prerender-utils.ts +189 -0
- package/src/vite/utils/shared-utils.ts +169 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Router Telemetry Sink
|
|
3
|
+
*
|
|
4
|
+
* Internal event model for structured lifecycle events.
|
|
5
|
+
* The sink is optional and zero-cost when not configured.
|
|
6
|
+
*
|
|
7
|
+
* Emit points:
|
|
8
|
+
* - request.start / request.end (match-handlers.ts)
|
|
9
|
+
* - request.error (match-handlers.ts catch blocks)
|
|
10
|
+
* - request.origin-rejected (rsc/handler.ts origin guard)
|
|
11
|
+
* - loader.start / loader.end / loader.error (loader-resolution.ts)
|
|
12
|
+
* - handler.error (trackHandler catch, segment-resolution/helpers.ts)
|
|
13
|
+
* - cache.decision (cache-lookup middleware)
|
|
14
|
+
* - revalidation.decision (revalidation evaluation)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Event types
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
interface BaseEvent {
|
|
22
|
+
/** Monotonic timestamp from performance.now() */
|
|
23
|
+
timestamp: number;
|
|
24
|
+
/** Request ID (from header or generated) */
|
|
25
|
+
requestId?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RequestStartEvent extends BaseEvent {
|
|
29
|
+
type: "request.start";
|
|
30
|
+
method: string;
|
|
31
|
+
pathname: string;
|
|
32
|
+
/** "match" for full document requests, "matchPartial" for navigation */
|
|
33
|
+
transaction: "match" | "matchPartial";
|
|
34
|
+
isPartial: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface RequestEndEvent extends BaseEvent {
|
|
38
|
+
type: "request.end";
|
|
39
|
+
method: string;
|
|
40
|
+
pathname: string;
|
|
41
|
+
transaction: "match" | "matchPartial";
|
|
42
|
+
durationMs: number;
|
|
43
|
+
segmentCount: number;
|
|
44
|
+
cacheHit: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RequestErrorEvent extends BaseEvent {
|
|
48
|
+
type: "request.error";
|
|
49
|
+
method: string;
|
|
50
|
+
pathname: string;
|
|
51
|
+
transaction: "match" | "matchPartial";
|
|
52
|
+
error: Error;
|
|
53
|
+
phase: string;
|
|
54
|
+
durationMs: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface LoaderStartEvent extends BaseEvent {
|
|
58
|
+
type: "loader.start";
|
|
59
|
+
segmentId: string;
|
|
60
|
+
loaderName: string;
|
|
61
|
+
pathname: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface LoaderEndEvent extends BaseEvent {
|
|
65
|
+
type: "loader.end";
|
|
66
|
+
segmentId: string;
|
|
67
|
+
loaderName: string;
|
|
68
|
+
pathname: string;
|
|
69
|
+
durationMs: number;
|
|
70
|
+
ok: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface LoaderErrorEvent extends BaseEvent {
|
|
74
|
+
type: "loader.error";
|
|
75
|
+
segmentId: string;
|
|
76
|
+
loaderName: string;
|
|
77
|
+
pathname: string;
|
|
78
|
+
error: Error;
|
|
79
|
+
handledByBoundary: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface HandlerErrorEvent extends BaseEvent {
|
|
83
|
+
type: "handler.error";
|
|
84
|
+
segmentId?: string;
|
|
85
|
+
segmentType?: string;
|
|
86
|
+
error: Error;
|
|
87
|
+
handledByBoundary: boolean;
|
|
88
|
+
pathname?: string;
|
|
89
|
+
routeKey?: string;
|
|
90
|
+
params?: Record<string, string>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface CacheDecisionEvent extends BaseEvent {
|
|
94
|
+
type: "cache.decision";
|
|
95
|
+
pathname: string;
|
|
96
|
+
routeKey: string;
|
|
97
|
+
hit: boolean;
|
|
98
|
+
/** Whether stale-while-revalidate was triggered */
|
|
99
|
+
shouldRevalidate: boolean;
|
|
100
|
+
source?: "runtime" | "prerender";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface RevalidationDecisionEvent extends BaseEvent {
|
|
104
|
+
type: "revalidation.decision";
|
|
105
|
+
segmentId: string;
|
|
106
|
+
pathname: string;
|
|
107
|
+
routeKey: string;
|
|
108
|
+
shouldRevalidate: boolean;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface RequestTimeoutEvent extends BaseEvent {
|
|
112
|
+
type: "request.timeout";
|
|
113
|
+
phase: import("./timeout.js").TimeoutPhase;
|
|
114
|
+
pathname: string;
|
|
115
|
+
routeKey?: string;
|
|
116
|
+
actionId?: string;
|
|
117
|
+
durationMs: number;
|
|
118
|
+
customHandler: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface OriginCheckRejectedEvent extends BaseEvent {
|
|
122
|
+
type: "request.origin-rejected";
|
|
123
|
+
method: string;
|
|
124
|
+
pathname: string;
|
|
125
|
+
phase: import("../rsc/origin-guard.js").OriginCheckPhase;
|
|
126
|
+
origin: string | null;
|
|
127
|
+
host: string | null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export type TelemetryEvent =
|
|
131
|
+
| RequestStartEvent
|
|
132
|
+
| RequestEndEvent
|
|
133
|
+
| RequestErrorEvent
|
|
134
|
+
| LoaderStartEvent
|
|
135
|
+
| LoaderEndEvent
|
|
136
|
+
| LoaderErrorEvent
|
|
137
|
+
| HandlerErrorEvent
|
|
138
|
+
| CacheDecisionEvent
|
|
139
|
+
| RevalidationDecisionEvent
|
|
140
|
+
| RequestTimeoutEvent
|
|
141
|
+
| OriginCheckRejectedEvent;
|
|
142
|
+
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
// Sink interface
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Telemetry sink receives structured lifecycle events from the router.
|
|
149
|
+
* Implement this interface to integrate with any observability backend.
|
|
150
|
+
*
|
|
151
|
+
* All methods are fire-and-forget — exceptions are caught and logged.
|
|
152
|
+
*/
|
|
153
|
+
export interface TelemetrySink {
|
|
154
|
+
emit(event: TelemetryEvent): void;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
// No-op singleton (zero-cost disabled state)
|
|
159
|
+
// ---------------------------------------------------------------------------
|
|
160
|
+
|
|
161
|
+
const noopSink: TelemetrySink = {
|
|
162
|
+
emit() {},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns the configured sink, or the no-op singleton.
|
|
167
|
+
* Call sites use this so they don't need null checks.
|
|
168
|
+
*/
|
|
169
|
+
export function resolveSink(sink: TelemetrySink | undefined): TelemetrySink {
|
|
170
|
+
return sink ?? noopSink;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Safe emit — catches any error thrown by the sink to prevent
|
|
175
|
+
* telemetry failures from affecting request handling.
|
|
176
|
+
*/
|
|
177
|
+
export function safeEmit(sink: TelemetrySink, event: TelemetryEvent): void {
|
|
178
|
+
try {
|
|
179
|
+
sink.emit(event);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
// Telemetry must never break request handling
|
|
182
|
+
if (process.env.NODE_ENV !== "production") {
|
|
183
|
+
console.error("[Router.telemetry] Sink error:", e);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Request ID extraction (for span correlation)
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
// Per-request memoization so the same Request object always maps to the
|
|
193
|
+
// same ID. WeakMap allows GC when the Request is no longer referenced.
|
|
194
|
+
const requestIds = new WeakMap<Request, string>();
|
|
195
|
+
let telemetryRequestCounter = 0;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Get or create a request ID for telemetry correlation.
|
|
199
|
+
* Checks standard headers first (x-rsc-router-request-id, x-request-id,
|
|
200
|
+
* cf-ray), then generates an internal ID when none is present.
|
|
201
|
+
* Generated IDs use format "t-{base36}" to distinguish from header values.
|
|
202
|
+
*/
|
|
203
|
+
export function getRequestId(request: Request): string {
|
|
204
|
+
const existing = requestIds.get(request);
|
|
205
|
+
if (existing) return existing;
|
|
206
|
+
|
|
207
|
+
const candidate =
|
|
208
|
+
request.headers.get("x-rsc-router-request-id") ??
|
|
209
|
+
request.headers.get("x-request-id") ??
|
|
210
|
+
request.headers.get("cf-ray");
|
|
211
|
+
|
|
212
|
+
let id: string;
|
|
213
|
+
if (candidate) {
|
|
214
|
+
const trimmed = candidate.trim();
|
|
215
|
+
id =
|
|
216
|
+
trimmed.length > 0
|
|
217
|
+
? trimmed
|
|
218
|
+
: `t-${(++telemetryRequestCounter).toString(36)}`;
|
|
219
|
+
} else {
|
|
220
|
+
id = `t-${(++telemetryRequestCounter).toString(36)}`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
requestIds.set(request, id);
|
|
224
|
+
return id;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// ---------------------------------------------------------------------------
|
|
228
|
+
// Console sink (built-in, replaces ad-hoc console.log debug traces)
|
|
229
|
+
// ---------------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Built-in console sink that logs events in a structured format.
|
|
233
|
+
* Designed as the default sink for development / debugging.
|
|
234
|
+
*/
|
|
235
|
+
export function createConsoleSink(): TelemetrySink {
|
|
236
|
+
return {
|
|
237
|
+
emit(event: TelemetryEvent): void {
|
|
238
|
+
switch (event.type) {
|
|
239
|
+
case "request.start":
|
|
240
|
+
console.log(
|
|
241
|
+
`[telemetry] ${event.type} ${event.method} ${event.pathname} (${event.transaction})`,
|
|
242
|
+
);
|
|
243
|
+
break;
|
|
244
|
+
case "request.end":
|
|
245
|
+
console.log(
|
|
246
|
+
`[telemetry] ${event.type} ${event.method} ${event.pathname} ${event.durationMs.toFixed(1)}ms segments=${event.segmentCount} cache=${event.cacheHit}`,
|
|
247
|
+
);
|
|
248
|
+
break;
|
|
249
|
+
case "request.error":
|
|
250
|
+
console.log(
|
|
251
|
+
`[telemetry] ${event.type} ${event.method} ${event.pathname} phase=${event.phase} ${event.durationMs.toFixed(1)}ms`,
|
|
252
|
+
event.error.message,
|
|
253
|
+
);
|
|
254
|
+
break;
|
|
255
|
+
case "loader.start":
|
|
256
|
+
console.log(
|
|
257
|
+
`[telemetry] ${event.type} ${event.loaderName} (${event.segmentId})`,
|
|
258
|
+
);
|
|
259
|
+
break;
|
|
260
|
+
case "loader.end":
|
|
261
|
+
console.log(
|
|
262
|
+
`[telemetry] ${event.type} ${event.loaderName} ${event.durationMs.toFixed(1)}ms ok=${event.ok}`,
|
|
263
|
+
);
|
|
264
|
+
break;
|
|
265
|
+
case "loader.error":
|
|
266
|
+
console.log(
|
|
267
|
+
`[telemetry] ${event.type} ${event.loaderName} boundary=${event.handledByBoundary}`,
|
|
268
|
+
event.error.message,
|
|
269
|
+
);
|
|
270
|
+
break;
|
|
271
|
+
case "handler.error":
|
|
272
|
+
console.log(
|
|
273
|
+
`[telemetry] ${event.type} segment=${event.segmentId ?? "unknown"} boundary=${event.handledByBoundary}${event.pathname ? ` ${event.pathname}` : ""}`,
|
|
274
|
+
event.error.message,
|
|
275
|
+
);
|
|
276
|
+
break;
|
|
277
|
+
case "cache.decision":
|
|
278
|
+
console.log(
|
|
279
|
+
`[telemetry] ${event.type} ${event.pathname} hit=${event.hit} swr=${event.shouldRevalidate}${event.source ? ` source=${event.source}` : ""}`,
|
|
280
|
+
);
|
|
281
|
+
break;
|
|
282
|
+
case "revalidation.decision":
|
|
283
|
+
console.log(
|
|
284
|
+
`[telemetry] ${event.type} ${event.segmentId} revalidate=${event.shouldRevalidate}`,
|
|
285
|
+
);
|
|
286
|
+
break;
|
|
287
|
+
case "request.timeout":
|
|
288
|
+
console.log(
|
|
289
|
+
`[telemetry] ${event.type} phase=${event.phase} ${event.pathname} ${event.durationMs.toFixed(1)}ms custom=${event.customHandler}`,
|
|
290
|
+
);
|
|
291
|
+
break;
|
|
292
|
+
case "request.origin-rejected":
|
|
293
|
+
console.log(
|
|
294
|
+
`[telemetry] ${event.type} ${event.method} ${event.pathname} phase=${event.phase} origin=${event.origin ?? "none"} host=${event.host ?? "none"}`,
|
|
295
|
+
);
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Router Timeout
|
|
3
|
+
*
|
|
4
|
+
* Types, resolution logic, and helpers for request-level timeouts.
|
|
5
|
+
* Timeouts wrap action execution and render-start phases with
|
|
6
|
+
* a Promise.race mechanism, returning 504 on expiry.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Public types
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
export interface RouterTimeouts {
|
|
14
|
+
/** Timeout for server action execution (ms). */
|
|
15
|
+
actionMs?: number;
|
|
16
|
+
/** Timeout for initial render/response production (ms). */
|
|
17
|
+
renderStartMs?: number;
|
|
18
|
+
/** Timeout for idle streaming after render starts (ms). Reserved for PR 2. */
|
|
19
|
+
streamIdleMs?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type TimeoutPhase = "action" | "render-start" | "stream-idle";
|
|
23
|
+
|
|
24
|
+
export interface TimeoutContext<TEnv = any> {
|
|
25
|
+
phase: TimeoutPhase;
|
|
26
|
+
request: Request;
|
|
27
|
+
url: URL;
|
|
28
|
+
env: TEnv;
|
|
29
|
+
routeKey?: string;
|
|
30
|
+
actionId?: string;
|
|
31
|
+
durationMs: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type OnTimeoutCallback<TEnv = any> = (
|
|
35
|
+
ctx: TimeoutContext<TEnv>,
|
|
36
|
+
) => Response | Promise<Response>;
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
// Internal resolved form
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
export interface ResolvedTimeouts {
|
|
43
|
+
actionMs: number | undefined;
|
|
44
|
+
renderStartMs: number | undefined;
|
|
45
|
+
streamIdleMs: number | undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Merge the `timeout` shorthand with the structured `timeouts` object.
|
|
50
|
+
*
|
|
51
|
+
* - `timeout` applies to `actionMs` and `renderStartMs` (NOT `streamIdleMs`).
|
|
52
|
+
* - Explicit `timeouts.*` values override the shorthand.
|
|
53
|
+
* - Returns `undefined` for any phase that has no configured value.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveTimeouts(
|
|
56
|
+
timeout?: number,
|
|
57
|
+
timeouts?: RouterTimeouts,
|
|
58
|
+
): ResolvedTimeouts {
|
|
59
|
+
return {
|
|
60
|
+
actionMs: timeouts?.actionMs ?? timeout ?? undefined,
|
|
61
|
+
renderStartMs: timeouts?.renderStartMs ?? timeout ?? undefined,
|
|
62
|
+
streamIdleMs: timeouts?.streamIdleMs ?? undefined,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// Error class
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
export class RouterTimeoutError extends Error {
|
|
71
|
+
override name = "RouterTimeoutError" as const;
|
|
72
|
+
phase: TimeoutPhase;
|
|
73
|
+
durationMs: number;
|
|
74
|
+
|
|
75
|
+
constructor(phase: TimeoutPhase, durationMs: number) {
|
|
76
|
+
super(
|
|
77
|
+
`Request timed out during ${phase} after ${Math.round(durationMs)}ms`,
|
|
78
|
+
);
|
|
79
|
+
this.phase = phase;
|
|
80
|
+
this.durationMs = durationMs;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// Race helper
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
type TimeoutResult<T> =
|
|
89
|
+
| { result: T; timedOut: false }
|
|
90
|
+
| { timedOut: true; durationMs: number };
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Race an operation against a deadline.
|
|
94
|
+
*
|
|
95
|
+
* Returns a discriminated union so callers handle the timeout case
|
|
96
|
+
* without try/catch. Non-timeout errors from the operation re-throw.
|
|
97
|
+
*
|
|
98
|
+
* When `timeoutMs` is `undefined` or `<= 0`, the operation runs
|
|
99
|
+
* without any deadline (pass-through).
|
|
100
|
+
*/
|
|
101
|
+
export async function withTimeout<T>(
|
|
102
|
+
operation: Promise<T>,
|
|
103
|
+
timeoutMs: number | undefined,
|
|
104
|
+
phase: TimeoutPhase,
|
|
105
|
+
): Promise<TimeoutResult<T>> {
|
|
106
|
+
if (timeoutMs == null || timeoutMs <= 0) {
|
|
107
|
+
return { result: await operation, timedOut: false };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const start = performance.now();
|
|
111
|
+
let timer: ReturnType<typeof setTimeout>;
|
|
112
|
+
|
|
113
|
+
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
114
|
+
timer = setTimeout(() => {
|
|
115
|
+
reject(new RouterTimeoutError(phase, performance.now() - start));
|
|
116
|
+
}, timeoutMs);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
const result = await Promise.race([operation, timeoutPromise]);
|
|
121
|
+
clearTimeout(timer!);
|
|
122
|
+
return { result, timedOut: false };
|
|
123
|
+
} catch (error) {
|
|
124
|
+
clearTimeout(timer!);
|
|
125
|
+
if (error instanceof RouterTimeoutError) {
|
|
126
|
+
return { timedOut: true, durationMs: error.durationMs };
|
|
127
|
+
}
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
// Default response
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Create the default 504 response for a timed-out request.
|
|
138
|
+
* Includes `X-Rango-Timeout-Phase` header for observability.
|
|
139
|
+
*/
|
|
140
|
+
export function createDefaultTimeoutResponse(phase: TimeoutPhase): Response {
|
|
141
|
+
return new Response("Request timed out", {
|
|
142
|
+
status: 504,
|
|
143
|
+
headers: {
|
|
144
|
+
"Content-Type": "text/plain;charset=utf-8",
|
|
145
|
+
"X-Rango-Timeout-Phase": phase,
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime Trie Matching
|
|
3
|
+
*
|
|
4
|
+
* Walks the pre-built trie by path segments in O(path_length) time.
|
|
5
|
+
* Falls back to null when no match is found (caller uses regex fallback).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { TrieNode, TrieLeaf } from "../build/route-trie.js";
|
|
9
|
+
|
|
10
|
+
export interface TrieMatchResult {
|
|
11
|
+
/** Route name */
|
|
12
|
+
routeKey: string;
|
|
13
|
+
/** Static prefix of the matched entry */
|
|
14
|
+
sp: string;
|
|
15
|
+
/** Matched route params */
|
|
16
|
+
params: Record<string, string>;
|
|
17
|
+
/** Optional param names (absent params have empty string value) */
|
|
18
|
+
optionalParams?: string[];
|
|
19
|
+
/** Ancestry shortCodes for layout pruning */
|
|
20
|
+
ancestry: string[];
|
|
21
|
+
/** Redirect target if trailing slash requires it */
|
|
22
|
+
redirectTo?: string;
|
|
23
|
+
/** Route has pre-rendered data available */
|
|
24
|
+
pr?: true;
|
|
25
|
+
/** Passthrough: handler kept for live fallback on unknown params */
|
|
26
|
+
pt?: true;
|
|
27
|
+
/** Response type for non-RSC routes (json, text, image, any) */
|
|
28
|
+
responseType?: string;
|
|
29
|
+
/** Negotiate variants: response-type routes sharing this path */
|
|
30
|
+
negotiateVariants?: Array<{ routeKey: string; responseType: string }>;
|
|
31
|
+
/** RSC-first: RSC route was defined before response-type variants */
|
|
32
|
+
rscFirst?: true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Try to match a pathname against the trie.
|
|
37
|
+
* Returns null if no match found (caller should fall back to regex).
|
|
38
|
+
*/
|
|
39
|
+
export function tryTrieMatch(
|
|
40
|
+
trie: TrieNode | null,
|
|
41
|
+
pathname: string,
|
|
42
|
+
): TrieMatchResult | null {
|
|
43
|
+
if (!trie) return null;
|
|
44
|
+
|
|
45
|
+
// Split pathname into segments, filtering empty strings from leading/trailing slashes
|
|
46
|
+
const pathnameHasTrailingSlash =
|
|
47
|
+
pathname.length > 1 && pathname.endsWith("/");
|
|
48
|
+
const normalizedPath = pathnameHasTrailingSlash
|
|
49
|
+
? pathname.slice(0, -1)
|
|
50
|
+
: pathname;
|
|
51
|
+
|
|
52
|
+
// Handle root path
|
|
53
|
+
if (normalizedPath === "" || normalizedPath === "/") {
|
|
54
|
+
if (trie.r) {
|
|
55
|
+
return validateAndBuild(
|
|
56
|
+
trie.r,
|
|
57
|
+
[],
|
|
58
|
+
undefined,
|
|
59
|
+
pathname,
|
|
60
|
+
pathnameHasTrailingSlash,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Remove leading slash and split
|
|
67
|
+
const segments = normalizedPath.slice(1).split("/");
|
|
68
|
+
|
|
69
|
+
// Try exact match with normalized path (no trailing slash)
|
|
70
|
+
const result = walkTrie(trie, segments, 0, []);
|
|
71
|
+
if (result) {
|
|
72
|
+
return validateAndBuild(
|
|
73
|
+
result.leaf,
|
|
74
|
+
result.paramValues,
|
|
75
|
+
result.wildcardValue,
|
|
76
|
+
pathname,
|
|
77
|
+
pathnameHasTrailingSlash,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface WalkResult {
|
|
85
|
+
leaf: TrieLeaf;
|
|
86
|
+
paramValues: string[];
|
|
87
|
+
wildcardValue?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Walk the trie by segments with priority: static > param > wildcard.
|
|
92
|
+
* Uses backtracking to try all possible matches.
|
|
93
|
+
*/
|
|
94
|
+
function walkTrie(
|
|
95
|
+
node: TrieNode,
|
|
96
|
+
segments: string[],
|
|
97
|
+
index: number,
|
|
98
|
+
paramValues: string[],
|
|
99
|
+
): WalkResult | null {
|
|
100
|
+
// All segments consumed: check for terminal
|
|
101
|
+
if (index === segments.length) {
|
|
102
|
+
if (node.r) {
|
|
103
|
+
return { leaf: node.r, paramValues: [...paramValues] };
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const segment = segments[index];
|
|
109
|
+
const staticChild = node.s?.[segment];
|
|
110
|
+
|
|
111
|
+
// Priority 1: Static match
|
|
112
|
+
if (staticChild) {
|
|
113
|
+
const result = walkTrie(staticChild, segments, index + 1, paramValues);
|
|
114
|
+
if (result) return result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Priority 2: Suffix-param match (e.g., :productId.html)
|
|
118
|
+
if (node.xp) {
|
|
119
|
+
for (const suffix in node.xp) {
|
|
120
|
+
if (segment.endsWith(suffix) && segment.length > suffix.length) {
|
|
121
|
+
const paramValue = segment.slice(0, -suffix.length);
|
|
122
|
+
paramValues.push(paramValue);
|
|
123
|
+
const result = walkTrie(
|
|
124
|
+
node.xp[suffix].c,
|
|
125
|
+
segments,
|
|
126
|
+
index + 1,
|
|
127
|
+
paramValues,
|
|
128
|
+
);
|
|
129
|
+
paramValues.pop();
|
|
130
|
+
if (result) return result;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Priority 3: Param match
|
|
136
|
+
if (node.p) {
|
|
137
|
+
paramValues.push(segment);
|
|
138
|
+
const result = walkTrie(node.p.c, segments, index + 1, paramValues);
|
|
139
|
+
paramValues.pop();
|
|
140
|
+
if (result) return result;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Priority 4: Wildcard match (consumes rest)
|
|
144
|
+
if (node.w) {
|
|
145
|
+
const rest = joinRemainingSegments(segments, index);
|
|
146
|
+
return {
|
|
147
|
+
leaf: node.w,
|
|
148
|
+
paramValues: [...paramValues],
|
|
149
|
+
wildcardValue: rest,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function joinRemainingSegments(segments: string[], start: number): string {
|
|
157
|
+
if (start >= segments.length) return "";
|
|
158
|
+
|
|
159
|
+
let rest = segments[start]!;
|
|
160
|
+
for (let i = start + 1; i < segments.length; i++) {
|
|
161
|
+
rest += "/" + segments[i]!;
|
|
162
|
+
}
|
|
163
|
+
return rest;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Post-match: validate constraints and handle trailing slash logic.
|
|
168
|
+
*/
|
|
169
|
+
function validateAndBuild(
|
|
170
|
+
leaf: TrieLeaf,
|
|
171
|
+
paramValues: string[],
|
|
172
|
+
wildcardValue: string | undefined,
|
|
173
|
+
originalPathname: string,
|
|
174
|
+
pathnameHasTrailingSlash: boolean,
|
|
175
|
+
): TrieMatchResult | null {
|
|
176
|
+
// Build named params by zipping leaf.pa with positional paramValues
|
|
177
|
+
const params: Record<string, string> = {};
|
|
178
|
+
if (leaf.pa) {
|
|
179
|
+
for (let i = 0; i < leaf.pa.length && i < paramValues.length; i++) {
|
|
180
|
+
params[leaf.pa[i]] = paramValues[i];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Add wildcard param (wildcard leaves have pn from TrieNode.w type)
|
|
185
|
+
if (wildcardValue !== undefined && "pn" in leaf) {
|
|
186
|
+
params[(leaf as TrieLeaf & { pn: string }).pn] = wildcardValue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Validate constraints
|
|
190
|
+
if (leaf.cv) {
|
|
191
|
+
for (const paramName in leaf.cv) {
|
|
192
|
+
const allowed = leaf.cv[paramName]!;
|
|
193
|
+
const value = params[paramName];
|
|
194
|
+
if (value !== undefined && value !== "" && !allowed.includes(value)) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Fill in empty strings for optional params that weren't matched
|
|
201
|
+
if (leaf.op) {
|
|
202
|
+
for (const name of leaf.op) {
|
|
203
|
+
if (!(name in params)) {
|
|
204
|
+
params[name] = "";
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Trailing slash handling
|
|
210
|
+
const tsMode = leaf.ts as "never" | "always" | "ignore" | undefined;
|
|
211
|
+
let redirectTo: string | undefined;
|
|
212
|
+
|
|
213
|
+
if (
|
|
214
|
+
tsMode === "always" &&
|
|
215
|
+
!pathnameHasTrailingSlash &&
|
|
216
|
+
originalPathname !== "/"
|
|
217
|
+
) {
|
|
218
|
+
redirectTo = originalPathname + "/";
|
|
219
|
+
} else if (tsMode === "never" && pathnameHasTrailingSlash) {
|
|
220
|
+
redirectTo = originalPathname.slice(0, -1);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const result: TrieMatchResult = {
|
|
224
|
+
routeKey: leaf.n,
|
|
225
|
+
sp: leaf.sp,
|
|
226
|
+
params,
|
|
227
|
+
ancestry: leaf.a,
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
if (leaf.op) result.optionalParams = leaf.op;
|
|
231
|
+
if (redirectTo) result.redirectTo = redirectTo;
|
|
232
|
+
if (leaf.pr) result.pr = true;
|
|
233
|
+
if (leaf.pt) result.pt = true;
|
|
234
|
+
if (leaf.rt) result.responseType = leaf.rt;
|
|
235
|
+
if (leaf.nv) result.negotiateVariants = leaf.nv;
|
|
236
|
+
if (leaf.rf) result.rscFirst = true;
|
|
237
|
+
|
|
238
|
+
return result;
|
|
239
|
+
}
|