@rangojs/router 0.0.0-experimental.84 → 0.0.0-experimental.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -20
- package/dist/vite/index.js +19 -9
- package/package.json +14 -15
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/hooks/SKILL.md +4 -2
- package/skills/links/SKILL.md +88 -16
- package/skills/loader/SKILL.md +35 -2
- package/skills/typesafety/SKILL.md +3 -1
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/navigation-bridge.ts +51 -2
- package/src/browser/navigation-store.ts +25 -1
- package/src/browser/partial-update.ts +20 -1
- package/src/browser/prefetch/cache.ts +16 -0
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/NavigationProvider.tsx +44 -9
- package/src/browser/react/use-router.ts +8 -1
- package/src/browser/rsc-router.tsx +34 -6
- package/src/browser/types.ts +13 -0
- package/src/cache/cf/cf-cache-store.ts +5 -7
- package/src/index.rsc.ts +3 -0
- package/src/index.ts +3 -0
- package/src/outlet-context.ts +1 -1
- package/src/reverse.ts +3 -2
- package/src/router/handler-context.ts +20 -3
- package/src/router/lazy-includes.ts +1 -1
- package/src/router/loader-resolution.ts +3 -0
- package/src/router/match-api.ts +3 -3
- package/src/router/middleware-types.ts +2 -22
- package/src/router/middleware.ts +18 -3
- package/src/router/pattern-matching.ts +60 -9
- package/src/router/trie-matching.ts +10 -4
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +1 -2
- package/src/rsc/handler.ts +2 -1
- package/src/rsc/response-route-handler.ts +3 -0
- package/src/server/request-context.ts +10 -42
- package/src/types/handler-context.ts +2 -34
- package/src/types/loader-types.ts +2 -6
- package/src/types/request-scope.ts +126 -0
- package/src/urls/response-types.ts +2 -10
- package/src/vite/rango.ts +23 -7
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/package-resolution.ts +1 -1
package/src/router/match-api.ts
CHANGED
|
@@ -22,10 +22,10 @@ import { collectRouteMiddleware } from "./middleware.js";
|
|
|
22
22
|
import { traverseBack } from "./pattern-matching.js";
|
|
23
23
|
import { DefaultErrorFallback } from "../default-error-boundary.js";
|
|
24
24
|
import {
|
|
25
|
-
EntryData,
|
|
26
|
-
LoaderEntry,
|
|
25
|
+
type EntryData,
|
|
26
|
+
type LoaderEntry,
|
|
27
27
|
getContext,
|
|
28
|
-
InterceptSelectorContext,
|
|
28
|
+
type InterceptSelectorContext,
|
|
29
29
|
} from "../server/context";
|
|
30
30
|
import type { ErrorBoundaryHandler, ErrorInfo, MatchResult } from "../types";
|
|
31
31
|
import type { ReactNode } from "react";
|
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
import type { ScopedReverseFunction } from "../reverse.js";
|
|
15
15
|
import type { Theme } from "../theme/types.js";
|
|
16
16
|
import type { LocationStateEntry } from "../browser/react/location-state-shared.js";
|
|
17
|
+
import type { RequestScope } from "../types/request-scope.js";
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Get variable function type
|
|
@@ -57,28 +58,7 @@ export interface CookieOptions {
|
|
|
57
58
|
export interface MiddlewareContext<
|
|
58
59
|
TEnv = any,
|
|
59
60
|
TParams = Record<string, string>,
|
|
60
|
-
> {
|
|
61
|
-
/** Original request */
|
|
62
|
-
request: Request;
|
|
63
|
-
|
|
64
|
-
/** Parsed URL (with internal `_rsc*` params stripped) */
|
|
65
|
-
url: URL;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* The original request URL with all parameters intact, including
|
|
69
|
-
* internal `_rsc*` transport params.
|
|
70
|
-
*/
|
|
71
|
-
originalUrl: URL;
|
|
72
|
-
|
|
73
|
-
/** URL pathname */
|
|
74
|
-
pathname: string;
|
|
75
|
-
|
|
76
|
-
/** URL search params */
|
|
77
|
-
searchParams: URLSearchParams;
|
|
78
|
-
|
|
79
|
-
/** Platform bindings (Cloudflare, etc.) */
|
|
80
|
-
env: TEnv;
|
|
81
|
-
|
|
61
|
+
> extends RequestScope<TEnv> {
|
|
82
62
|
/** URL params extracted from route/middleware pattern */
|
|
83
63
|
params: TParams;
|
|
84
64
|
|
package/src/router/middleware.ts
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { contextGet, contextSet } from "../context-var.js";
|
|
13
|
+
import { safeDecodeURIComponent } from "./url-params.js";
|
|
14
|
+
import { fireAndForgetWaitUntil } from "../types/request-scope.js";
|
|
13
15
|
import type {
|
|
14
16
|
CollectedMiddleware,
|
|
15
17
|
MiddlewareCollectableEntry,
|
|
@@ -113,7 +115,12 @@ function escapeRegex(str: string): string {
|
|
|
113
115
|
}
|
|
114
116
|
|
|
115
117
|
/**
|
|
116
|
-
* Extract params from a pathname using a pattern's regex and param names
|
|
118
|
+
* Extract params from a pathname using a pattern's regex and param names.
|
|
119
|
+
*
|
|
120
|
+
* Values are URL-decoded so apps see the raw string (e.g. "ivo@example.com")
|
|
121
|
+
* instead of the percent-encoded form ("ivo%40example.com"). This matches the
|
|
122
|
+
* contract assumed by ctx.reverse (which re-encodes) and aligns with
|
|
123
|
+
* Express/React Router/Fastify/Koa.
|
|
117
124
|
*/
|
|
118
125
|
export function extractParams(
|
|
119
126
|
pathname: string,
|
|
@@ -125,7 +132,7 @@ export function extractParams(
|
|
|
125
132
|
|
|
126
133
|
const params: Record<string, string> = {};
|
|
127
134
|
for (let i = 0; i < paramNames.length; i++) {
|
|
128
|
-
params[paramNames[i]] = match[i + 1] || "";
|
|
135
|
+
params[paramNames[i]] = safeDecodeURIComponent(match[i + 1] || "");
|
|
129
136
|
}
|
|
130
137
|
return params;
|
|
131
138
|
}
|
|
@@ -180,14 +187,22 @@ export function createMiddlewareContext<TEnv>(
|
|
|
180
187
|
return responseHolder.response;
|
|
181
188
|
};
|
|
182
189
|
|
|
190
|
+
// Capture reqCtx once: the request-scoped platform fields
|
|
191
|
+
// (originalUrl, executionContext, waitUntil) are immutable per request,
|
|
192
|
+
// so snapshotting beats re-reading ALS on every access. The lazy getters
|
|
193
|
+
// below (routeName, theme, setTheme) stay lazy because those can change
|
|
194
|
+
// during `await next()`.
|
|
195
|
+
const reqCtx = _getRequestContext();
|
|
183
196
|
return {
|
|
184
197
|
request,
|
|
185
198
|
url,
|
|
186
|
-
originalUrl: new URL(request.url),
|
|
199
|
+
originalUrl: reqCtx?.originalUrl ?? new URL(request.url),
|
|
187
200
|
pathname: url.pathname,
|
|
188
201
|
searchParams: url.searchParams,
|
|
189
202
|
env: env as MiddlewareContext<TEnv>["env"],
|
|
190
203
|
params,
|
|
204
|
+
executionContext: reqCtx?.executionContext,
|
|
205
|
+
waitUntil: reqCtx ? reqCtx.waitUntil.bind(reqCtx) : fireAndForgetWaitUntil,
|
|
191
206
|
// Getter: re-derives from request context on each access so that global
|
|
192
207
|
// middleware sees the matched route name after await next().
|
|
193
208
|
get routeName(): MiddlewareContext<TEnv>["routeName"] {
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import type { RouteEntry, TrailingSlashMode } from "../types";
|
|
8
8
|
import type { EntryData } from "../server/context";
|
|
9
9
|
import { debugLog, isRouterDebugEnabled } from "./logging.js";
|
|
10
|
+
import { safeDecodeURIComponent } from "./url-params.js";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Parsed segment info
|
|
@@ -82,6 +83,13 @@ export interface CompiledPattern {
|
|
|
82
83
|
paramNames: string[];
|
|
83
84
|
optionalParams: Set<string>;
|
|
84
85
|
hasTrailingSlash: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Param-name → allowed values for constrained params (e.g. `:lang(en|gb)`).
|
|
88
|
+
* Validated against the **decoded** param value after regex extraction so
|
|
89
|
+
* a URL like `/en%20GB` still matches `:lang(en GB)` — matching the trie
|
|
90
|
+
* path's behavior (trie-matching.ts:validateAndBuild).
|
|
91
|
+
*/
|
|
92
|
+
constraints?: Record<string, string[]>;
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
// Module-level cache for compiled patterns. Route patterns are a finite set
|
|
@@ -142,6 +150,7 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
142
150
|
const segments = parsePattern(normalizedPattern);
|
|
143
151
|
const paramNames: string[] = [];
|
|
144
152
|
const optionalParams = new Set<string>();
|
|
153
|
+
let constraints: Record<string, string[]> | undefined;
|
|
145
154
|
|
|
146
155
|
let regexPattern = "";
|
|
147
156
|
|
|
@@ -152,11 +161,14 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
152
161
|
} else if (segment.type === "param") {
|
|
153
162
|
paramNames.push(segment.value);
|
|
154
163
|
const suffixPattern = segment.suffix ? escapeRegex(segment.suffix) : "";
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
164
|
+
// Constrained params capture anything here; the allowed values are
|
|
165
|
+
// checked post-decode in findMatch so URL-encoded constraint values
|
|
166
|
+
// (e.g. `:lang(en GB)` via `/en%20GB`) still match.
|
|
167
|
+
const valuePattern = segment.suffix ? "([^/]+?)" : "([^/]+)";
|
|
168
|
+
|
|
169
|
+
if (segment.constraint) {
|
|
170
|
+
(constraints ??= {})[segment.value] = segment.constraint;
|
|
171
|
+
}
|
|
160
172
|
|
|
161
173
|
if (segment.optional) {
|
|
162
174
|
optionalParams.add(segment.value);
|
|
@@ -186,9 +198,33 @@ export function compilePattern(pattern: string): CompiledPattern {
|
|
|
186
198
|
paramNames,
|
|
187
199
|
optionalParams,
|
|
188
200
|
hasTrailingSlash,
|
|
201
|
+
...(constraints ? { constraints } : {}),
|
|
189
202
|
};
|
|
190
203
|
}
|
|
191
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Validate decoded params against a compiled pattern's constraints.
|
|
207
|
+
* Returns false if any constrained param has a non-empty value not in the
|
|
208
|
+
* allowed list (empty-string = absent optional, which is allowed).
|
|
209
|
+
*/
|
|
210
|
+
function satisfiesConstraints(
|
|
211
|
+
params: Record<string, string>,
|
|
212
|
+
constraints: Record<string, string[]> | undefined,
|
|
213
|
+
): boolean {
|
|
214
|
+
if (!constraints) return true;
|
|
215
|
+
for (const name in constraints) {
|
|
216
|
+
const value = params[name];
|
|
217
|
+
if (
|
|
218
|
+
value !== undefined &&
|
|
219
|
+
value !== "" &&
|
|
220
|
+
!constraints[name].includes(value)
|
|
221
|
+
) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
|
|
192
228
|
/**
|
|
193
229
|
* Escape special regex characters in a string
|
|
194
230
|
*/
|
|
@@ -392,8 +428,13 @@ export function findMatch<TEnv>(
|
|
|
392
428
|
fullPattern = entry.prefix + pattern;
|
|
393
429
|
}
|
|
394
430
|
|
|
395
|
-
const {
|
|
396
|
-
|
|
431
|
+
const {
|
|
432
|
+
regex,
|
|
433
|
+
paramNames,
|
|
434
|
+
optionalParams,
|
|
435
|
+
hasTrailingSlash,
|
|
436
|
+
constraints,
|
|
437
|
+
} = getCompiledPattern(fullPattern);
|
|
397
438
|
|
|
398
439
|
// Get trailing slash mode for this route (per-route config or pattern-based)
|
|
399
440
|
const trailingSlashMode: TrailingSlashMode | undefined =
|
|
@@ -412,9 +453,15 @@ export function findMatch<TEnv>(
|
|
|
412
453
|
if (match) {
|
|
413
454
|
const params: Record<string, string> = {};
|
|
414
455
|
paramNames.forEach((name, index) => {
|
|
415
|
-
params[name] = match[index + 1] ?? "";
|
|
456
|
+
params[name] = safeDecodeURIComponent(match[index + 1] ?? "");
|
|
416
457
|
});
|
|
417
458
|
|
|
459
|
+
// Validate constraints against decoded values; a failure falls
|
|
460
|
+
// through to the next route so other patterns can still match.
|
|
461
|
+
if (!satisfiesConstraints(params, constraints)) {
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
|
|
418
465
|
if (effectiveDebug) {
|
|
419
466
|
debugLog("findMatch", "matched route", {
|
|
420
467
|
routeKey,
|
|
@@ -467,9 +514,13 @@ export function findMatch<TEnv>(
|
|
|
467
514
|
if (altMatch) {
|
|
468
515
|
const params: Record<string, string> = {};
|
|
469
516
|
paramNames.forEach((name, index) => {
|
|
470
|
-
params[name] = altMatch[index + 1] ?? "";
|
|
517
|
+
params[name] = safeDecodeURIComponent(altMatch[index + 1] ?? "");
|
|
471
518
|
});
|
|
472
519
|
|
|
520
|
+
if (!satisfiesConstraints(params, constraints)) {
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
|
|
473
524
|
// Determine redirect behavior based on mode
|
|
474
525
|
if (trailingSlashMode === "ignore") {
|
|
475
526
|
// Match without redirect
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { TrieNode, TrieLeaf } from "../build/route-trie.js";
|
|
9
|
+
import { safeDecodeURIComponent } from "./url-params.js";
|
|
9
10
|
|
|
10
11
|
export interface TrieMatchResult {
|
|
11
12
|
/** Route name */
|
|
@@ -173,20 +174,25 @@ function validateAndBuild(
|
|
|
173
174
|
originalPathname: string,
|
|
174
175
|
pathnameHasTrailingSlash: boolean,
|
|
175
176
|
): TrieMatchResult | null {
|
|
176
|
-
// Build named params by zipping leaf.pa with positional paramValues
|
|
177
|
+
// Build named params by zipping leaf.pa with positional paramValues.
|
|
178
|
+
// Params are URL-decoded at this boundary so ctx.params holds the values
|
|
179
|
+
// apps expect (matching Express/React Router) and round-trip cleanly
|
|
180
|
+
// through ctx.reverse.
|
|
177
181
|
const params: Record<string, string> = {};
|
|
178
182
|
if (leaf.pa) {
|
|
179
183
|
for (let i = 0; i < leaf.pa.length && i < paramValues.length; i++) {
|
|
180
|
-
params[leaf.pa[i]] = paramValues[i];
|
|
184
|
+
params[leaf.pa[i]] = safeDecodeURIComponent(paramValues[i]);
|
|
181
185
|
}
|
|
182
186
|
}
|
|
183
187
|
|
|
184
188
|
// Add wildcard param (wildcard leaves have pn from TrieNode.w type)
|
|
185
189
|
if (wildcardValue !== undefined && "pn" in leaf) {
|
|
186
|
-
params[(leaf as TrieLeaf & { pn: string }).pn] =
|
|
190
|
+
params[(leaf as TrieLeaf & { pn: string }).pn] =
|
|
191
|
+
safeDecodeURIComponent(wildcardValue);
|
|
187
192
|
}
|
|
188
193
|
|
|
189
|
-
// Validate constraints
|
|
194
|
+
// Validate constraints against decoded values so constraint lists can be
|
|
195
|
+
// written in decoded form (e.g. ["en-GB", "en US"]).
|
|
190
196
|
if (leaf.cv) {
|
|
191
197
|
for (const paramName in leaf.cv) {
|
|
192
198
|
const allowed = leaf.cv[paramName]!;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL param encode/decode at the route boundary.
|
|
3
|
+
*
|
|
4
|
+
* Extraction (decode): regex/trie matchers keep param values URL-encoded;
|
|
5
|
+
* `safeDecodeURIComponent` turns them back into raw strings so `ctx.params`
|
|
6
|
+
* matches the contract apps expect (Express/React Router/Fastify/Koa) and
|
|
7
|
+
* round-trips through reverse stay stable. Malformed %-encoding is
|
|
8
|
+
* preserved as-is so a broken URL doesn't crash matching.
|
|
9
|
+
*
|
|
10
|
+
* Reversal (encode): `encodePathSegment` escapes only what RFC 3986
|
|
11
|
+
* requires for a path segment — `/`, `?`, `#`, space, control chars,
|
|
12
|
+
* non-ASCII — and leaves pchar sub-delims (`@ : $ & + , ; =` and friends)
|
|
13
|
+
* readable. `encodeURIComponent` over-encodes for path segments, which
|
|
14
|
+
* makes generated URLs harder for humans to read in the address bar
|
|
15
|
+
* (e.g. mailbox IDs like `ivo@example.com` would become
|
|
16
|
+
* `ivo%40example.com` even though `@` is path-legal).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export function safeDecodeURIComponent(raw: string): string {
|
|
20
|
+
if (raw === "" || raw.indexOf("%") === -1) return raw;
|
|
21
|
+
try {
|
|
22
|
+
return decodeURIComponent(raw);
|
|
23
|
+
} catch {
|
|
24
|
+
return raw;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// encodeURIComponent over-encodes for path segments. After running it,
|
|
29
|
+
// un-encode the pchar sub-delims + (`:` / `@`) so the resulting URL
|
|
30
|
+
// keeps human-readable characters that are legal in a path segment.
|
|
31
|
+
// Everything dangerous — `/ ? # %` and space/control/non-ASCII — stays
|
|
32
|
+
// encoded.
|
|
33
|
+
const PATH_SAFE_ESCAPES: Record<string, string> = {
|
|
34
|
+
"%3A": ":",
|
|
35
|
+
"%40": "@",
|
|
36
|
+
"%24": "$",
|
|
37
|
+
"%26": "&",
|
|
38
|
+
"%2B": "+",
|
|
39
|
+
"%2C": ",",
|
|
40
|
+
"%3B": ";",
|
|
41
|
+
"%3D": "=",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export function encodePathSegment(value: string): string {
|
|
45
|
+
return encodeURIComponent(value).replace(
|
|
46
|
+
/%(?:3A|40|24|26|2B|2C|3B|3D)/gi,
|
|
47
|
+
(match) => PATH_SAFE_ESCAPES[match.toUpperCase()] ?? match,
|
|
48
|
+
);
|
|
49
|
+
}
|
package/src/router.ts
CHANGED
|
@@ -22,8 +22,7 @@ import type { UrlPatterns } from "./urls.js";
|
|
|
22
22
|
import type { UrlBuilder } from "./urls/pattern-types.js";
|
|
23
23
|
import { urls } from "./urls.js";
|
|
24
24
|
import {
|
|
25
|
-
EntryData,
|
|
26
|
-
InterceptSelectorContext,
|
|
25
|
+
type EntryData,
|
|
27
26
|
getContext,
|
|
28
27
|
RSCRouterContext,
|
|
29
28
|
type MetricsStore,
|
package/src/rsc/handler.ts
CHANGED
|
@@ -57,6 +57,7 @@ import {
|
|
|
57
57
|
getRouterTrie,
|
|
58
58
|
} from "../route-map-builder.js";
|
|
59
59
|
import type { HandlerContext } from "./handler-context.js";
|
|
60
|
+
import type { SegmentCacheStore } from "../cache/types.js";
|
|
60
61
|
import { buildRouterTrieFromUrlpatterns } from "./manifest-init.js";
|
|
61
62
|
import { handleProgressiveEnhancement } from "./progressive-enhancement.js";
|
|
62
63
|
import {
|
|
@@ -353,7 +354,7 @@ export function createRSCHandler<
|
|
|
353
354
|
// Resolve cache store configuration
|
|
354
355
|
// Priority: options.cache (handler override) > router.cache (router default)
|
|
355
356
|
// Store is enabled only if: config provided, enabled, and no ?__no_cache query param
|
|
356
|
-
let cacheStore
|
|
357
|
+
let cacheStore: SegmentCacheStore | undefined;
|
|
357
358
|
const cacheOption = options.cache ?? router.cache;
|
|
358
359
|
if (cacheOption && !url.searchParams.has("__no_cache")) {
|
|
359
360
|
const cacheConfig =
|
|
@@ -80,10 +80,13 @@ export async function handleResponseRoute<TEnv>(
|
|
|
80
80
|
env,
|
|
81
81
|
searchParams: cleanUrl.searchParams,
|
|
82
82
|
url: cleanUrl,
|
|
83
|
+
originalUrl: reqCtx.originalUrl,
|
|
83
84
|
pathname: url.pathname,
|
|
84
85
|
reverse: createReverseFunction(handlerCtx.getRequiredRouteMap()),
|
|
85
86
|
get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
|
|
86
87
|
header: (name: string, value: string) => reqCtx.header(name, value),
|
|
88
|
+
waitUntil: reqCtx.waitUntil.bind(reqCtx),
|
|
89
|
+
executionContext: reqCtx.executionContext,
|
|
87
90
|
_responseType: preview.responseType,
|
|
88
91
|
};
|
|
89
92
|
// Brand with taint symbol so "use cache" detects it as request-scoped
|
|
@@ -37,6 +37,8 @@ import { track, type MetricsStore } from "./context.js";
|
|
|
37
37
|
import { getFetchableLoader } from "./fetchable-loader-store.js";
|
|
38
38
|
import type { SegmentCacheStore } from "../cache/types.js";
|
|
39
39
|
import type { Theme, ResolvedThemeConfig } from "../theme/types.js";
|
|
40
|
+
import type { ExecutionContext, RequestScope } from "../types/request-scope.js";
|
|
41
|
+
import { fireAndForgetWaitUntil } from "../types/request-scope.js";
|
|
40
42
|
import { THEME_COOKIE } from "../theme/constants.js";
|
|
41
43
|
import type { LocationStateEntry } from "../browser/react/location-state-shared.js";
|
|
42
44
|
import { NOCACHE_SYMBOL, assertNotInsideCacheExec } from "../cache/taint.js";
|
|
@@ -58,22 +60,7 @@ import { isAutoGeneratedRouteName } from "../route-name.js";
|
|
|
58
60
|
export interface RequestContext<
|
|
59
61
|
TEnv = DefaultEnv,
|
|
60
62
|
TParams = Record<string, string>,
|
|
61
|
-
> {
|
|
62
|
-
/** Platform bindings (Cloudflare env, etc.) */
|
|
63
|
-
env: TEnv;
|
|
64
|
-
/** Original HTTP request */
|
|
65
|
-
request: Request;
|
|
66
|
-
/** Parsed URL (with internal `_rsc*` params stripped) */
|
|
67
|
-
url: URL;
|
|
68
|
-
/**
|
|
69
|
-
* The original request URL with all parameters intact, including
|
|
70
|
-
* internal `_rsc*` transport params.
|
|
71
|
-
*/
|
|
72
|
-
originalUrl: URL;
|
|
73
|
-
/** URL pathname */
|
|
74
|
-
pathname: string;
|
|
75
|
-
/** URL search params (with internal `_rsc*` params stripped, same as `url.searchParams`) */
|
|
76
|
-
searchParams: URLSearchParams;
|
|
63
|
+
> extends RequestScope<TEnv> {
|
|
77
64
|
/** @internal Shared variable backing store for ctx.get()/ctx.set(). */
|
|
78
65
|
_variables: Record<string, any>;
|
|
79
66
|
/** Get a variable set by middleware */
|
|
@@ -159,20 +146,6 @@ export interface RequestContext<
|
|
|
159
146
|
import("../cache/profile-registry.js").CacheProfile
|
|
160
147
|
>;
|
|
161
148
|
|
|
162
|
-
/**
|
|
163
|
-
* Schedule work to run after the response is sent.
|
|
164
|
-
* On Cloudflare Workers, uses ctx.waitUntil().
|
|
165
|
-
* On Node.js, runs as fire-and-forget.
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* ```typescript
|
|
169
|
-
* ctx.waitUntil(async () => {
|
|
170
|
-
* await cacheStore.set(key, data, ttl);
|
|
171
|
-
* });
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
waitUntil(fn: () => Promise<void>): void;
|
|
175
|
-
|
|
176
149
|
/**
|
|
177
150
|
* Register a callback to run when the response is created.
|
|
178
151
|
* Callbacks are sync and receive the response. They can:
|
|
@@ -498,13 +471,7 @@ export function requireRequestContext<
|
|
|
498
471
|
return getRequestContext<TEnv>();
|
|
499
472
|
}
|
|
500
473
|
|
|
501
|
-
|
|
502
|
-
* Cloudflare Workers ExecutionContext (subset we need)
|
|
503
|
-
*/
|
|
504
|
-
export interface ExecutionContext {
|
|
505
|
-
waitUntil(promise: Promise<any>): void;
|
|
506
|
-
passThroughOnException(): void;
|
|
507
|
-
}
|
|
474
|
+
export type { ExecutionContext };
|
|
508
475
|
|
|
509
476
|
/**
|
|
510
477
|
* Options for creating a request context
|
|
@@ -768,16 +735,14 @@ export function createRequestContext<TEnv>(
|
|
|
768
735
|
|
|
769
736
|
waitUntil(fn: () => Promise<void>): void {
|
|
770
737
|
if (executionContext?.waitUntil) {
|
|
771
|
-
// Cloudflare Workers: use native waitUntil
|
|
772
738
|
executionContext.waitUntil(fn());
|
|
773
739
|
} else {
|
|
774
|
-
|
|
775
|
-
fn().catch((err) =>
|
|
776
|
-
console.error("[waitUntil] Background task failed:", err),
|
|
777
|
-
);
|
|
740
|
+
fireAndForgetWaitUntil(fn);
|
|
778
741
|
}
|
|
779
742
|
},
|
|
780
743
|
|
|
744
|
+
executionContext,
|
|
745
|
+
|
|
781
746
|
_onResponseCallbacks: [],
|
|
782
747
|
|
|
783
748
|
onResponse(callback: (response: Response) => Response): void {
|
|
@@ -1043,7 +1008,10 @@ export function createUseFunction<TEnv>(
|
|
|
1043
1008
|
search: (ctx as any).search ?? {},
|
|
1044
1009
|
pathname: ctx.pathname,
|
|
1045
1010
|
url: ctx.url,
|
|
1011
|
+
originalUrl: ctx.originalUrl,
|
|
1046
1012
|
env: ctx.env as any,
|
|
1013
|
+
waitUntil: ctx.waitUntil.bind(ctx),
|
|
1014
|
+
executionContext: ctx.executionContext,
|
|
1047
1015
|
get: ctx.get as any,
|
|
1048
1016
|
use: (<TDep, TDepParams = any>(
|
|
1049
1017
|
dep: LoaderDefinition<TDep, TDepParams>,
|
|
@@ -20,6 +20,7 @@ import type {
|
|
|
20
20
|
} from "./route-config.js";
|
|
21
21
|
import type { LoaderDefinition } from "./loader-types.js";
|
|
22
22
|
import type { UseItems, HandlerUseItem } from "../route-types.js";
|
|
23
|
+
import type { RequestScope } from "./request-scope.js";
|
|
23
24
|
|
|
24
25
|
// Re-export MiddlewareFn for internal/advanced use
|
|
25
26
|
export type { MiddlewareFn } from "../router/middleware.js";
|
|
@@ -195,7 +196,7 @@ export type HandlerContext<
|
|
|
195
196
|
TEnv = DefaultEnv,
|
|
196
197
|
TSearch extends SearchSchema = {},
|
|
197
198
|
TRouteMap = never,
|
|
198
|
-
> = {
|
|
199
|
+
> = RequestScope<TEnv> & {
|
|
199
200
|
/**
|
|
200
201
|
* Route parameters extracted from the URL pattern.
|
|
201
202
|
* Type-safe when using Handler<"/path/:param"> or Handler<{ param: string }>.
|
|
@@ -215,44 +216,11 @@ export type HandlerContext<
|
|
|
215
216
|
* changing build semantics (e.g., skip expensive operations in dev).
|
|
216
217
|
*/
|
|
217
218
|
dev: boolean;
|
|
218
|
-
/**
|
|
219
|
-
* The original incoming Request object (transport URL intact).
|
|
220
|
-
* Use `ctx.url` / `ctx.searchParams` for application logic — those have
|
|
221
|
-
* internal `_rsc*` params stripped. `ctx.request` preserves the raw URL
|
|
222
|
-
* for cases where you need original headers, method, or body.
|
|
223
|
-
*/
|
|
224
|
-
request: Request;
|
|
225
|
-
/**
|
|
226
|
-
* Query parameters from the URL (system params like `_rsc*` are filtered).
|
|
227
|
-
* Always a standard URLSearchParams instance.
|
|
228
|
-
*/
|
|
229
|
-
searchParams: URLSearchParams;
|
|
230
219
|
/**
|
|
231
220
|
* Typed search parameters parsed from URL query string via the route's
|
|
232
221
|
* search schema. Empty object when no schema is defined.
|
|
233
222
|
*/
|
|
234
223
|
search: {} extends TSearch ? {} : ResolveSearchSchema<TSearch>;
|
|
235
|
-
/**
|
|
236
|
-
* The pathname portion of the request URL.
|
|
237
|
-
*/
|
|
238
|
-
pathname: string;
|
|
239
|
-
/**
|
|
240
|
-
* The full URL object (with internal `_rsc*` params stripped).
|
|
241
|
-
* Use this for application logic — routing, link generation, display.
|
|
242
|
-
*/
|
|
243
|
-
url: URL;
|
|
244
|
-
/**
|
|
245
|
-
* The original request URL with all parameters intact, including
|
|
246
|
-
* internal `_rsc*` transport params. Use `ctx.url` for application
|
|
247
|
-
* logic — this is only needed for advanced cases like debugging
|
|
248
|
-
* or custom cache keying.
|
|
249
|
-
*/
|
|
250
|
-
originalUrl: URL;
|
|
251
|
-
/**
|
|
252
|
-
* Platform bindings (DB, KV, secrets, etc.).
|
|
253
|
-
* Access resources like `ctx.env.DB`, `ctx.env.KV`.
|
|
254
|
-
*/
|
|
255
|
-
env: TEnv;
|
|
256
224
|
/**
|
|
257
225
|
* Type-safe getter for middleware variables.
|
|
258
226
|
* Preferred way to read middleware-injected variables.
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
DefaultReverseRouteMap,
|
|
9
9
|
DefaultVars,
|
|
10
10
|
} from "./global-namespace.js";
|
|
11
|
+
import type { RequestScope } from "./request-scope.js";
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Context passed to loader functions during execution
|
|
@@ -39,7 +40,7 @@ export type LoaderContext<
|
|
|
39
40
|
TEnv = DefaultEnv,
|
|
40
41
|
TBody = unknown,
|
|
41
42
|
TSearch extends SearchSchema = {},
|
|
42
|
-
> = {
|
|
43
|
+
> = RequestScope<TEnv> & {
|
|
43
44
|
params: TParams;
|
|
44
45
|
/**
|
|
45
46
|
* Route params extracted from the URL pattern match (server-side only).
|
|
@@ -48,12 +49,7 @@ export type LoaderContext<
|
|
|
48
49
|
* resource scoping.
|
|
49
50
|
*/
|
|
50
51
|
routeParams: Record<string, string>;
|
|
51
|
-
request: Request;
|
|
52
|
-
searchParams: URLSearchParams;
|
|
53
52
|
search: {} extends TSearch ? {} : ResolveSearchSchema<TSearch>;
|
|
54
|
-
pathname: string;
|
|
55
|
-
url: URL;
|
|
56
|
-
env: TEnv;
|
|
57
53
|
get: {
|
|
58
54
|
<T>(contextVar: ContextVar<T>): T | undefined;
|
|
59
55
|
} & (<K extends keyof DefaultVars>(key: K) => DefaultVars[K]);
|