@rangojs/router 0.0.0-experimental.77 → 0.0.0-experimental.77ed8945
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 +120 -25
- package/dist/bin/rango.js +147 -57
- package/dist/vite/index.js +2103 -861
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +13 -8
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +220 -30
- package/skills/caching/SKILL.md +116 -8
- package/skills/composability/SKILL.md +27 -2
- package/skills/css/SKILL.md +76 -0
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +3 -1
- package/skills/hooks/SKILL.md +229 -20
- package/skills/host-router/SKILL.md +66 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +26 -4
- package/skills/layout/SKILL.md +6 -7
- package/skills/links/SKILL.md +247 -17
- package/skills/loader/SKILL.md +219 -9
- package/skills/middleware/SKILL.md +47 -12
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +12 -6
- package/skills/prerender/SKILL.md +14 -33
- package/skills/rango/SKILL.md +238 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +122 -47
- package/skills/route/SKILL.md +33 -4
- package/skills/router-setup/SKILL.md +3 -3
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/tailwind/SKILL.md +27 -3
- package/skills/typesafety/SKILL.md +319 -27
- package/skills/use-cache/SKILL.md +34 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +39 -0
- package/src/browser/event-controller.ts +86 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/navigation-bridge.ts +29 -9
- package/src/browser/navigation-client.ts +99 -77
- package/src/browser/navigation-store.ts +7 -8
- package/src/browser/navigation-transaction.ts +10 -28
- package/src/browser/partial-update.ts +60 -40
- package/src/browser/prefetch/cache.ts +196 -49
- package/src/browser/prefetch/fetch.ts +203 -59
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +37 -13
- package/src/browser/react/Link.tsx +18 -13
- package/src/browser/react/NavigationProvider.tsx +75 -31
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +17 -9
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +23 -2
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +52 -1
- package/src/browser/rsc-router.tsx +71 -22
- package/src/browser/scroll-restoration.ts +22 -14
- package/src/browser/segment-reconciler.ts +10 -14
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +44 -30
- package/src/browser/types.ts +12 -2
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +60 -35
- package/src/build/generate-route-types.ts +2 -0
- package/src/build/index.ts +8 -1
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +45 -1
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +1 -1
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +55 -14
- package/src/build/route-types/scan-filter.ts +1 -1
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-runtime.ts +17 -5
- package/src/cache/cache-scope.ts +51 -49
- package/src/cache/cf/cf-cache-store.ts +502 -32
- package/src/cache/cf/index.ts +3 -0
- package/src/cache/handle-snapshot.ts +103 -0
- package/src/cache/index.ts +3 -0
- package/src/cache/memory-segment-store.ts +3 -2
- package/src/cache/types.ts +10 -6
- package/src/client.rsc.tsx +3 -0
- package/src/client.tsx +96 -205
- package/src/context-var.ts +5 -5
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -4
- package/src/handle.ts +4 -6
- package/src/host/index.ts +2 -2
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -21
- package/src/index.rsc.ts +10 -6
- package/src/index.ts +17 -8
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +2 -5
- package/src/loader.ts +3 -10
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender/store.ts +9 -7
- package/src/prerender.ts +4 -4
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -39
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +253 -265
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +43 -15
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-types.ts +26 -41
- package/src/router/content-negotiation.ts +15 -2
- package/src/router/error-handling.ts +1 -1
- package/src/router/find-match.ts +54 -6
- package/src/router/handler-context.ts +21 -41
- package/src/router/intercept-resolution.ts +4 -18
- package/src/router/lazy-includes.ts +41 -22
- package/src/router/loader-resolution.ts +82 -36
- package/src/router/manifest.ts +41 -19
- package/src/router/match-api.ts +4 -3
- package/src/router/match-handlers.ts +1 -0
- package/src/router/match-middleware/cache-lookup.ts +57 -95
- package/src/router/match-middleware/cache-store.ts +3 -2
- package/src/router/match-result.ts +53 -32
- package/src/router/metrics.ts +1 -1
- package/src/router/middleware-types.ts +15 -26
- package/src/router/middleware.ts +99 -84
- package/src/router/pattern-matching.ts +116 -19
- package/src/router/prerender-match.ts +40 -15
- package/src/router/preview-match.ts +3 -1
- package/src/router/request-classification.ts +40 -37
- package/src/router/revalidation.ts +58 -2
- package/src/router/router-interfaces.ts +51 -35
- package/src/router/router-options.ts +25 -1
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +27 -6
- package/src/router/segment-resolution/revalidation.ts +147 -106
- package/src/router/segment-resolution/static-store.ts +19 -5
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/trie-matching.ts +40 -16
- package/src/router/types.ts +8 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +37 -25
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +58 -77
- package/src/rsc/helpers.ts +72 -43
- package/src/rsc/index.ts +1 -1
- package/src/rsc/manifest-init.ts +28 -41
- package/src/rsc/origin-guard.ts +30 -10
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +76 -61
- package/src/rsc/rsc-rendering.ts +45 -51
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +33 -39
- package/src/rsc/ssr-setup.ts +16 -0
- package/src/rsc/types.ts +8 -2
- package/src/search-params.ts +4 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +132 -116
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +175 -53
- package/src/server/cookie-store.ts +28 -4
- package/src/server/request-context.ts +57 -51
- package/src/ssr/index.tsx +5 -1
- package/src/static-handler.ts +1 -1
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +68 -50
- package/src/types/index.ts +1 -0
- package/src/types/loader-types.ts +11 -9
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +35 -2
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +1 -5
- package/src/urls/path-helper-types.ts +17 -3
- package/src/urls/path-helper.ts +17 -52
- package/src/urls/pattern-types.ts +36 -19
- package/src/urls/response-types.ts +22 -29
- package/src/urls/type-extraction.ts +58 -139
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +413 -42
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +6 -6
- package/src/vite/discovery/discover-routers.ts +106 -75
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +72 -31
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +33 -0
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +2 -0
- package/src/vite/plugin-types.ts +67 -0
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
- package/src/vite/plugins/expose-action-id.ts +54 -30
- package/src/vite/plugins/expose-id-utils.ts +12 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +496 -486
- package/src/vite/plugins/performance-tracks.ts +29 -25
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +59 -2
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +116 -29
- package/src/vite/router-discovery.ts +753 -104
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +8 -59
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +5 -4
- package/src/vite/utils/shared-utils.ts +107 -26
- package/src/browser/action-response-classifier.ts +0 -99
package/src/rsc/helpers.ts
CHANGED
|
@@ -8,9 +8,50 @@ import {
|
|
|
8
8
|
_getRequestContext,
|
|
9
9
|
getLocationState,
|
|
10
10
|
} from "../server/request-context.js";
|
|
11
|
+
import type { RequestContext } from "../server/request-context.js";
|
|
11
12
|
import { resolveLocationStateEntries } from "../browser/react/location-state-shared.js";
|
|
13
|
+
import { isRedirectResponse } from "../response-utils.js";
|
|
12
14
|
import type { MiddlewareEntry, MiddlewareFn } from "../router/middleware.js";
|
|
13
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Copy stub headers from the request context onto a target Headers instance:
|
|
18
|
+
* append Set-Cookie entries, set everything else only if absent. Header
|
|
19
|
+
* mutation failures are swallowed so the same logic works against Response
|
|
20
|
+
* headers that may be immutable (e.g. Cloudflare protocol-switch responses).
|
|
21
|
+
*/
|
|
22
|
+
function applyStubHeaders(target: Headers, stub: Headers): void {
|
|
23
|
+
stub.forEach((value, name) => {
|
|
24
|
+
try {
|
|
25
|
+
if (name.toLowerCase() === "set-cookie") {
|
|
26
|
+
target.append(name, value);
|
|
27
|
+
} else if (!target.has(name)) {
|
|
28
|
+
target.set(name, value);
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
// Headers immutable — skip.
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Drain ctx._onResponseCallbacks onto a response. Swapping the array before
|
|
38
|
+
* iteration prevents re-entrant registrations from double-firing and matches
|
|
39
|
+
* the contract that each callback runs at most once per request.
|
|
40
|
+
*/
|
|
41
|
+
function drainOnResponseCallbacks(
|
|
42
|
+
ctx: RequestContext,
|
|
43
|
+
response: Response,
|
|
44
|
+
): Response {
|
|
45
|
+
const callbacks = ctx._onResponseCallbacks;
|
|
46
|
+
if (callbacks.length === 0) return response;
|
|
47
|
+
ctx._onResponseCallbacks = [];
|
|
48
|
+
let result = response;
|
|
49
|
+
for (const callback of callbacks) {
|
|
50
|
+
result = callback(result) ?? result;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
14
55
|
/**
|
|
15
56
|
* Check if a request body has content to decode
|
|
16
57
|
*/
|
|
@@ -39,40 +80,23 @@ export function createResponseWithMergedHeaders(
|
|
|
39
80
|
return new Response(body, init);
|
|
40
81
|
}
|
|
41
82
|
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
// merge points (e.g. executeMiddleware) do not duplicate them.
|
|
83
|
+
// Delete Set-Cookie from the stub after consuming so downstream merge
|
|
84
|
+
// points (e.g. executeMiddleware) don't duplicate them.
|
|
45
85
|
const mergedHeaders = new Headers(init.headers);
|
|
46
|
-
ctx.res.headers
|
|
47
|
-
if (name.toLowerCase() === "set-cookie") {
|
|
48
|
-
mergedHeaders.append(name, value);
|
|
49
|
-
} else if (!mergedHeaders.has(name)) {
|
|
50
|
-
// Only set if not already present in init.headers
|
|
51
|
-
mergedHeaders.set(name, value);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
86
|
+
applyStubHeaders(mergedHeaders, ctx.res.headers);
|
|
54
87
|
ctx.res.headers.delete("set-cookie");
|
|
55
88
|
|
|
56
|
-
//
|
|
57
|
-
//
|
|
89
|
+
// ctx.res.status overrides init.status when explicitly set (e.g. 404 for
|
|
90
|
+
// notFound, 500 for error). Default ctx.res.status is 200.
|
|
58
91
|
const status = ctx.res.status !== 200 ? ctx.res.status : init.status;
|
|
59
92
|
|
|
60
|
-
|
|
93
|
+
const response = new Response(body, {
|
|
61
94
|
...init,
|
|
62
95
|
status,
|
|
63
96
|
headers: mergedHeaders,
|
|
64
97
|
});
|
|
65
98
|
|
|
66
|
-
|
|
67
|
-
// Drain the array so that downstream callers (e.g. finalizeResponse)
|
|
68
|
-
// do not re-execute the same callbacks on this response.
|
|
69
|
-
const callbacks = ctx._onResponseCallbacks;
|
|
70
|
-
ctx._onResponseCallbacks = [];
|
|
71
|
-
for (const callback of callbacks) {
|
|
72
|
-
response = callback(response) ?? response;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return response;
|
|
99
|
+
return drainOnResponseCallbacks(ctx, response);
|
|
76
100
|
}
|
|
77
101
|
|
|
78
102
|
/**
|
|
@@ -122,10 +146,10 @@ export function interceptRedirectForPartial(
|
|
|
122
146
|
locationState?: Record<string, unknown>,
|
|
123
147
|
) => Response,
|
|
124
148
|
): Response | null {
|
|
125
|
-
|
|
126
|
-
if (!(response.status >= 300 && response.status < 400 && redirectUrl)) {
|
|
149
|
+
if (!isRedirectResponse(response)) {
|
|
127
150
|
return null;
|
|
128
151
|
}
|
|
152
|
+
const redirectUrl = response.headers.get("Location")!;
|
|
129
153
|
const locationState = getLocationState();
|
|
130
154
|
let intercepted: Response;
|
|
131
155
|
if (locationState) {
|
|
@@ -175,24 +199,29 @@ export function buildRouteMiddlewareEntries<TEnv>(
|
|
|
175
199
|
}
|
|
176
200
|
|
|
177
201
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
202
|
+
* Merge stub headers from the request context onto an existing Response in
|
|
203
|
+
* place, then drain onResponse callbacks. Used when a Response cannot flow
|
|
204
|
+
* through `new Response()` — status 101 is outside the constructor's
|
|
205
|
+
* 200-599 range, and the Cloudflare-specific `webSocket` property would be
|
|
206
|
+
* lost on reconstruction.
|
|
183
207
|
*/
|
|
184
|
-
export function
|
|
208
|
+
export function mergeStubHeadersAndFinalize(response: Response): Response {
|
|
185
209
|
const ctx = _getRequestContext();
|
|
186
|
-
if (!ctx
|
|
187
|
-
return response;
|
|
188
|
-
}
|
|
210
|
+
if (!ctx) return response;
|
|
189
211
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
212
|
+
applyStubHeaders(response.headers, ctx.res.headers);
|
|
213
|
+
ctx.res.headers.delete("set-cookie");
|
|
214
|
+
|
|
215
|
+
return drainOnResponseCallbacks(ctx, response);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Run onResponse callbacks on an existing Response. Used by code paths that
|
|
220
|
+
* bypass createResponseWithMergedHeaders (e.g. middleware short-circuits)
|
|
221
|
+
* but still need ctx.onResponse() callbacks to fire.
|
|
222
|
+
*/
|
|
223
|
+
export function finalizeResponse(response: Response): Response {
|
|
224
|
+
const ctx = _getRequestContext();
|
|
225
|
+
if (!ctx) return response;
|
|
226
|
+
return drainOnResponseCallbacks(ctx, response);
|
|
198
227
|
}
|
package/src/rsc/index.ts
CHANGED
package/src/rsc/manifest-init.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
setRouteTrie,
|
|
14
14
|
setRouterManifest,
|
|
15
15
|
setRouterTrie,
|
|
16
|
+
setRouterPrecomputedEntries,
|
|
16
17
|
} from "../route-map-builder.js";
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -36,47 +37,13 @@ export async function buildRouterTrieFromUrlpatterns(
|
|
|
36
37
|
undefined,
|
|
37
38
|
router.basename ? { urlPrefix: router.basename } : undefined,
|
|
38
39
|
);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const routeToStaticPrefix: Record<string, string> = {};
|
|
47
|
-
for (const name of Object.keys(generated.routeManifest)) {
|
|
48
|
-
routeToStaticPrefix[name] = "";
|
|
49
|
-
}
|
|
50
|
-
// Override with prefix from include() entries so the trie
|
|
51
|
-
// returns the correct sp for lazy entry lookup in findMatch.
|
|
52
|
-
// Walk recursively to include routes in nested includes.
|
|
53
|
-
if (generated.prefixTree) {
|
|
54
|
-
const visitPrefixNode = (node: any): void => {
|
|
55
|
-
const sp = node.staticPrefix || "";
|
|
56
|
-
for (const route of node.routes || []) {
|
|
57
|
-
routeToStaticPrefix[route] = sp;
|
|
58
|
-
}
|
|
59
|
-
for (const child of Object.values(node.children || {})) {
|
|
60
|
-
visitPrefixNode(child);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
for (const node of Object.values(generated.prefixTree)) {
|
|
64
|
-
visitPrefixNode(node);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const trie = buildRouteTrie(
|
|
68
|
-
generated.routeManifest,
|
|
69
|
-
generated._routeAncestry,
|
|
70
|
-
routeToStaticPrefix,
|
|
71
|
-
generated.routeTrailingSlash,
|
|
72
|
-
generated.prerenderRoutes
|
|
73
|
-
? new Set(generated.prerenderRoutes)
|
|
74
|
-
: undefined,
|
|
75
|
-
generated.passthroughRoutes
|
|
76
|
-
? new Set(generated.passthroughRoutes)
|
|
77
|
-
: undefined,
|
|
78
|
-
generated.responseTypeRoutes,
|
|
79
|
-
);
|
|
40
|
+
// Build the trie through the SAME shared helper the production discovery uses
|
|
41
|
+
// (discover-routers.ts), so the dev runtime-rebuilt trie and the prod
|
|
42
|
+
// serialized trie cannot drift. buildPerRouterTrie returns null when there
|
|
43
|
+
// are no routes.
|
|
44
|
+
const { buildPerRouterTrie } = await import("../build/route-trie.js");
|
|
45
|
+
const trie = buildPerRouterTrie(generated);
|
|
46
|
+
if (trie) {
|
|
80
47
|
setRouterTrie(router.id, trie);
|
|
81
48
|
// Set global trie only if not already set by another router
|
|
82
49
|
if (!getRouteTrie()) {
|
|
@@ -84,6 +51,26 @@ export async function buildRouterTrieFromUrlpatterns(
|
|
|
84
51
|
}
|
|
85
52
|
}
|
|
86
53
|
setRouterManifest(router.id, generated.routeManifest);
|
|
54
|
+
|
|
55
|
+
// Match the production discovery path: precompute leaf-include entries so the
|
|
56
|
+
// match-time shortcut in evaluateLazyEntry applies in dev/Cloudflare too.
|
|
57
|
+
// Without this, dev re-runs each matched leaf include's handler at match time
|
|
58
|
+
// (evaluateLazyEntry) AND again at render time (loadManifest); with it, the
|
|
59
|
+
// match-time run is skipped and the handler runs once per first request.
|
|
60
|
+
// Identical route ownership to the handler path (the shortcut is guarded by
|
|
61
|
+
// the same prefixIsShared and #506 checks production uses).
|
|
62
|
+
const { flattenLeafEntries } = await import("../build/prefix-tree-utils.js");
|
|
63
|
+
const precomputed: Array<{
|
|
64
|
+
staticPrefix: string;
|
|
65
|
+
routes: Record<string, string>;
|
|
66
|
+
}> = [];
|
|
67
|
+
flattenLeafEntries(
|
|
68
|
+
generated.prefixTree,
|
|
69
|
+
generated.routeManifest,
|
|
70
|
+
precomputed,
|
|
71
|
+
);
|
|
72
|
+
setRouterPrecomputedEntries(router.id, precomputed);
|
|
73
|
+
|
|
87
74
|
// Merge into global manifest (needed for reverse/href across routers)
|
|
88
75
|
const existing = hasCachedManifest() ? getGlobalRouteMap() : {};
|
|
89
76
|
setCachedManifest({ ...existing, ...generated.routeManifest });
|
package/src/rsc/origin-guard.ts
CHANGED
|
@@ -9,11 +9,31 @@
|
|
|
9
9
|
* navigations, bookmarks, and non-browser clients don't send Origin.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import type { RequestPlan } from "../router/request-classification.js";
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* Request phase that triggered the origin check.
|
|
14
16
|
*/
|
|
15
17
|
export type OriginCheckPhase = "action" | "loader" | "pe-form";
|
|
16
18
|
|
|
19
|
+
// Exhaustive over RequestPlan modes so a new mode must be classified here (the
|
|
20
|
+
// security gate) instead of silently falling through to no origin check.
|
|
21
|
+
export const ORIGIN_CHECK_PHASE_BY_MODE: Record<
|
|
22
|
+
RequestPlan["mode"],
|
|
23
|
+
OriginCheckPhase | null
|
|
24
|
+
> = {
|
|
25
|
+
action: "action",
|
|
26
|
+
loader: "loader",
|
|
27
|
+
"pe-render": "pe-form",
|
|
28
|
+
"full-render": null,
|
|
29
|
+
"partial-render": null,
|
|
30
|
+
response: null,
|
|
31
|
+
redirect: null,
|
|
32
|
+
"version-mismatch": null,
|
|
33
|
+
// Terminal: handled before the origin guard (emits X-RSC-Reload, no execution).
|
|
34
|
+
"app-switch": null,
|
|
35
|
+
};
|
|
36
|
+
|
|
17
37
|
/**
|
|
18
38
|
* Context passed to the originCheck callback.
|
|
19
39
|
*/
|
|
@@ -116,14 +136,15 @@ export async function checkRequestOrigin<TEnv = any>(
|
|
|
116
136
|
// Disabled by explicit opt-out
|
|
117
137
|
if (config === false) return null;
|
|
118
138
|
|
|
119
|
-
// Default
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
139
|
+
// Default (true/undefined) becomes a callback returning boolean, so the
|
|
140
|
+
// Response|true|reject resolution below is written once.
|
|
141
|
+
const check: (
|
|
142
|
+
ctx: OriginCheckContext<TEnv>,
|
|
143
|
+
) => boolean | Response | Promise<boolean | Response> =
|
|
144
|
+
config === true || config === undefined
|
|
145
|
+
? () => defaultOriginCheck(request, url)
|
|
146
|
+
: config;
|
|
125
147
|
|
|
126
|
-
// Custom function — build context and call
|
|
127
148
|
const ctx: OriginCheckContext<TEnv> = {
|
|
128
149
|
request,
|
|
129
150
|
url,
|
|
@@ -133,9 +154,8 @@ export async function checkRequestOrigin<TEnv = any>(
|
|
|
133
154
|
defaultCheck: () => defaultOriginCheck(request, url),
|
|
134
155
|
};
|
|
135
156
|
|
|
136
|
-
const result = await
|
|
157
|
+
const result = await check(ctx);
|
|
137
158
|
|
|
138
159
|
if (result instanceof Response) return result;
|
|
139
|
-
|
|
140
|
-
return createForbiddenResponse(request);
|
|
160
|
+
return result === true ? null : createForbiddenResponse(request);
|
|
141
161
|
}
|
|
@@ -248,6 +248,8 @@ export async function handleProgressiveEnhancement<TEnv>(
|
|
|
248
248
|
segments: match.segments,
|
|
249
249
|
matched: match.matched,
|
|
250
250
|
diff: match.diff,
|
|
251
|
+
resolvedIds: match.resolvedIds,
|
|
252
|
+
params: match.params,
|
|
251
253
|
isPartial: false,
|
|
252
254
|
rootLayout: ctx.router.rootLayout,
|
|
253
255
|
handles: handleStore.stream(),
|
|
@@ -353,6 +355,8 @@ async function renderPeErrorBoundary<TEnv>(
|
|
|
353
355
|
segments: errorResult.segments,
|
|
354
356
|
matched: errorResult.matched,
|
|
355
357
|
diff: errorResult.diff,
|
|
358
|
+
resolvedIds: errorResult.resolvedIds,
|
|
359
|
+
params: errorResult.params,
|
|
356
360
|
isPartial: false,
|
|
357
361
|
isError: true,
|
|
358
362
|
rootLayout: ctx.router.rootLayout,
|
|
@@ -1,37 +1,104 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Problem Details (RFC 9457) Builder
|
|
3
3
|
*
|
|
4
|
-
* Builds a
|
|
5
|
-
*
|
|
4
|
+
* Builds a problem+json error body from a caught error, controlling what
|
|
5
|
+
* information is exposed based on error type and environment.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { RouterError } from "../errors.js";
|
|
9
|
-
import type {
|
|
9
|
+
import type { ProblemDetails } from "../urls.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* HTTP reason phrases for the problem `title` member. Inlined because the
|
|
13
|
+
* router targets edge/worker runtimes without node's `http.STATUS_CODES`;
|
|
14
|
+
* covers the full standard 4xx/5xx range, with a generic fallback for any
|
|
15
|
+
* non-standard status a handler might set.
|
|
16
|
+
*/
|
|
17
|
+
const STATUS_PHRASES: Record<number, string> = {
|
|
18
|
+
400: "Bad Request",
|
|
19
|
+
401: "Unauthorized",
|
|
20
|
+
402: "Payment Required",
|
|
21
|
+
403: "Forbidden",
|
|
22
|
+
404: "Not Found",
|
|
23
|
+
405: "Method Not Allowed",
|
|
24
|
+
406: "Not Acceptable",
|
|
25
|
+
407: "Proxy Authentication Required",
|
|
26
|
+
408: "Request Timeout",
|
|
27
|
+
409: "Conflict",
|
|
28
|
+
410: "Gone",
|
|
29
|
+
411: "Length Required",
|
|
30
|
+
412: "Precondition Failed",
|
|
31
|
+
413: "Payload Too Large",
|
|
32
|
+
414: "URI Too Long",
|
|
33
|
+
415: "Unsupported Media Type",
|
|
34
|
+
416: "Range Not Satisfiable",
|
|
35
|
+
417: "Expectation Failed",
|
|
36
|
+
418: "I'm a Teapot",
|
|
37
|
+
421: "Misdirected Request",
|
|
38
|
+
422: "Unprocessable Entity",
|
|
39
|
+
423: "Locked",
|
|
40
|
+
424: "Failed Dependency",
|
|
41
|
+
425: "Too Early",
|
|
42
|
+
426: "Upgrade Required",
|
|
43
|
+
428: "Precondition Required",
|
|
44
|
+
429: "Too Many Requests",
|
|
45
|
+
431: "Request Header Fields Too Large",
|
|
46
|
+
451: "Unavailable For Legal Reasons",
|
|
47
|
+
500: "Internal Server Error",
|
|
48
|
+
501: "Not Implemented",
|
|
49
|
+
502: "Bad Gateway",
|
|
50
|
+
503: "Service Unavailable",
|
|
51
|
+
504: "Gateway Timeout",
|
|
52
|
+
505: "HTTP Version Not Supported",
|
|
53
|
+
506: "Variant Also Negotiates",
|
|
54
|
+
507: "Insufficient Storage",
|
|
55
|
+
508: "Loop Detected",
|
|
56
|
+
510: "Not Extended",
|
|
57
|
+
511: "Network Authentication Required",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function statusPhrase(status: number): string {
|
|
61
|
+
return STATUS_PHRASES[status] ?? "Error";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Build an RFC 9457 problem+json body from a caught error.
|
|
66
|
+
* RouterError messages/codes are always exposed (developer-crafted).
|
|
14
67
|
* Standard Error messages are hidden in production.
|
|
68
|
+
*
|
|
69
|
+
* The `type` member is omitted in this phase: per RFC 9457 an absent `type` is
|
|
70
|
+
* treated as `"about:blank"` (no semantics beyond the HTTP status), so emitting
|
|
71
|
+
* it adds nothing. Per-route problem-type URIs arrive with the declared-errors
|
|
72
|
+
* map later. `code` is always present so consumers can branch on it
|
|
73
|
+
* (`"INTERNAL"` for non-RouterError failures).
|
|
15
74
|
*/
|
|
16
|
-
export function
|
|
75
|
+
export function createProblemDetails(
|
|
17
76
|
error: unknown,
|
|
77
|
+
status: number,
|
|
18
78
|
isDev: boolean,
|
|
19
|
-
):
|
|
79
|
+
): ProblemDetails {
|
|
20
80
|
if (error instanceof RouterError) {
|
|
21
81
|
return {
|
|
22
|
-
|
|
82
|
+
title: statusPhrase(status),
|
|
83
|
+
status,
|
|
84
|
+
detail: error.message,
|
|
23
85
|
code: error.code,
|
|
24
|
-
...(error.type ? { type: error.type } : {}),
|
|
25
86
|
...(isDev && error.stack ? { stack: error.stack } : {}),
|
|
26
87
|
};
|
|
27
88
|
}
|
|
28
89
|
if (error instanceof Error) {
|
|
29
90
|
return {
|
|
30
|
-
|
|
91
|
+
title: statusPhrase(status),
|
|
92
|
+
status,
|
|
93
|
+
detail: isDev ? error.message : "Internal Server Error",
|
|
94
|
+
code: "INTERNAL",
|
|
31
95
|
...(isDev && error.stack ? { stack: error.stack } : {}),
|
|
32
96
|
};
|
|
33
97
|
}
|
|
34
98
|
return {
|
|
35
|
-
|
|
99
|
+
title: statusPhrase(status),
|
|
100
|
+
status,
|
|
101
|
+
detail: isDev ? String(error) : "Internal Server Error",
|
|
102
|
+
code: "INTERNAL",
|
|
36
103
|
};
|
|
37
104
|
}
|
|
@@ -11,6 +11,7 @@ import { requireRequestContext } from "../server/request-context.js";
|
|
|
11
11
|
import { contextGet } from "../context-var.js";
|
|
12
12
|
import { NOCACHE_SYMBOL } from "../cache/taint.js";
|
|
13
13
|
import { traverseBack } from "../router/pattern-matching.js";
|
|
14
|
+
import { RESPONSE_TYPE_MIME } from "../router/content-negotiation.js";
|
|
14
15
|
import { createCacheScope } from "../cache/cache-scope.js";
|
|
15
16
|
import { executeMiddleware } from "../router/middleware.js";
|
|
16
17
|
import {
|
|
@@ -20,13 +21,15 @@ import {
|
|
|
20
21
|
import type { MiddlewareFn } from "../router/middleware.js";
|
|
21
22
|
import type { EntryData } from "../server/context.js";
|
|
22
23
|
import type { HandlerContext } from "./handler-context.js";
|
|
23
|
-
import {
|
|
24
|
+
import { createProblemDetails } from "./response-error.js";
|
|
24
25
|
import {
|
|
25
26
|
createResponseWithMergedHeaders,
|
|
26
27
|
finalizeResponse,
|
|
27
28
|
isCacheableStatus,
|
|
28
29
|
buildRouteMiddlewareEntries,
|
|
30
|
+
mergeStubHeadersAndFinalize,
|
|
29
31
|
} from "./helpers.js";
|
|
32
|
+
import { isWebSocketUpgradeResponse } from "../response-utils.js";
|
|
30
33
|
|
|
31
34
|
export interface ResponseRouteMatch {
|
|
32
35
|
responseType: string;
|
|
@@ -78,10 +81,13 @@ export async function handleResponseRoute<TEnv>(
|
|
|
78
81
|
env,
|
|
79
82
|
searchParams: cleanUrl.searchParams,
|
|
80
83
|
url: cleanUrl,
|
|
84
|
+
originalUrl: reqCtx.originalUrl,
|
|
81
85
|
pathname: url.pathname,
|
|
82
86
|
reverse: createReverseFunction(handlerCtx.getRequiredRouteMap()),
|
|
83
87
|
get: ((keyOrVar: any) => contextGet(variables, keyOrVar)) as any,
|
|
84
88
|
header: (name: string, value: string) => reqCtx.header(name, value),
|
|
89
|
+
waitUntil: reqCtx.waitUntil.bind(reqCtx),
|
|
90
|
+
executionContext: reqCtx.executionContext,
|
|
85
91
|
_responseType: preview.responseType,
|
|
86
92
|
};
|
|
87
93
|
// Brand with taint symbol so "use cache" detects it as request-scoped
|
|
@@ -96,6 +102,12 @@ export async function handleResponseRoute<TEnv>(
|
|
|
96
102
|
// so that stub headers (cookies, custom headers set via ctx.header()) are included.
|
|
97
103
|
// Use Headers (not Record<string, string>) to preserve duplicate entries like Set-Cookie.
|
|
98
104
|
const rewrapResponse = (result: Response) => {
|
|
105
|
+
// 204/205/304 are NOT short-circuited — they're valid for the Response
|
|
106
|
+
// constructor and must honor ctx.setStatus() overrides. Only upgrade
|
|
107
|
+
// responses (status 101 / `webSocket` property) bypass reconstruction.
|
|
108
|
+
if (isWebSocketUpgradeResponse(result)) {
|
|
109
|
+
return mergeStubHeadersAndFinalize(result);
|
|
110
|
+
}
|
|
99
111
|
const headers = new Headers();
|
|
100
112
|
result.headers.forEach((value, key) => {
|
|
101
113
|
if (key.toLowerCase() === "set-cookie") {
|
|
@@ -110,37 +122,6 @@ export async function handleResponseRoute<TEnv>(
|
|
|
110
122
|
});
|
|
111
123
|
};
|
|
112
124
|
|
|
113
|
-
// JSON response routes: wrap in { data } / { error } envelope
|
|
114
|
-
if (preview.responseType === "json") {
|
|
115
|
-
try {
|
|
116
|
-
const result = await (preview.handler as Function)(responseHandlerCtx);
|
|
117
|
-
if (result instanceof Response) {
|
|
118
|
-
return rewrapResponse(result);
|
|
119
|
-
}
|
|
120
|
-
return createResponseWithMergedHeaders(
|
|
121
|
-
JSON.stringify({ data: result }),
|
|
122
|
-
{
|
|
123
|
-
status: 200,
|
|
124
|
-
headers: { "content-type": "application/json;charset=utf-8" },
|
|
125
|
-
},
|
|
126
|
-
);
|
|
127
|
-
} catch (error) {
|
|
128
|
-
handlerCtx.callOnError(error, "handler", errorCtx);
|
|
129
|
-
const isDev = process.env.NODE_ENV !== "production";
|
|
130
|
-
const status = error instanceof RouterError ? error.status : 500;
|
|
131
|
-
return createResponseWithMergedHeaders(
|
|
132
|
-
JSON.stringify({
|
|
133
|
-
error: createResponseErrorPayload(error, isDev),
|
|
134
|
-
}),
|
|
135
|
-
{
|
|
136
|
-
status,
|
|
137
|
-
headers: { "content-type": "application/json;charset=utf-8" },
|
|
138
|
-
},
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Non-JSON response routes: catch errors and return plain Response
|
|
144
125
|
try {
|
|
145
126
|
const result = await (preview.handler as Function)(responseHandlerCtx);
|
|
146
127
|
|
|
@@ -148,38 +129,70 @@ export async function handleResponseRoute<TEnv>(
|
|
|
148
129
|
return rewrapResponse(result);
|
|
149
130
|
}
|
|
150
131
|
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
132
|
+
// Handled before the MIME lookup (json is also a RESPONSE_TYPE_MIME key).
|
|
133
|
+
if (preview.responseType === "json") {
|
|
134
|
+
// Runtime guard: the json() return type rejects nested Promises at
|
|
135
|
+
// compile time, but an `as`-cast or untyped (JS) handler can still slip
|
|
136
|
+
// one through. JSON.stringify would silently emit {} for it (the
|
|
137
|
+
// forgotten-await footgun — the RSC pipeline awaits nested promises, this
|
|
138
|
+
// path does not). Throw a clear error instead of shipping empty data.
|
|
139
|
+
const body = JSON.stringify(result, (_key, value) => {
|
|
140
|
+
if (
|
|
141
|
+
value != null &&
|
|
142
|
+
typeof (value as { then?: unknown }).then === "function"
|
|
143
|
+
) {
|
|
144
|
+
throw new RouterError(
|
|
145
|
+
"RESPONSE_NOT_SERIALIZABLE",
|
|
146
|
+
"A json() response route returned a Promise (likely a forgotten " +
|
|
147
|
+
"await). Await async values before returning so they serialize, " +
|
|
148
|
+
"instead of emitting an empty {}.",
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return value;
|
|
152
|
+
});
|
|
153
|
+
return createResponseWithMergedHeaders(body, {
|
|
154
|
+
status: 200,
|
|
155
|
+
headers: { "content-type": "application/json;charset=utf-8" },
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Object.hasOwn (not truthiness) so prototype names like "toString" are not
|
|
160
|
+
// matched; image/stream/any are absent and fall through to the throw.
|
|
161
|
+
if (Object.hasOwn(RESPONSE_TYPE_MIME, preview.responseType)) {
|
|
162
|
+
return createResponseWithMergedHeaders(String(result), {
|
|
163
|
+
status: 200,
|
|
164
|
+
headers: {
|
|
165
|
+
"content-type": `${RESPONSE_TYPE_MIME[preview.responseType]};charset=utf-8`,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
178
168
|
}
|
|
169
|
+
|
|
170
|
+
throw new Error(
|
|
171
|
+
`Response route handler for "${preview.responseType}" must return a Response object, got ${typeof result}`,
|
|
172
|
+
);
|
|
179
173
|
} catch (error) {
|
|
180
174
|
handlerCtx.callOnError(error, "handler", errorCtx);
|
|
181
175
|
const isDev = process.env.NODE_ENV !== "production";
|
|
182
|
-
const
|
|
176
|
+
const derivedStatus = error instanceof RouterError ? error.status : 500;
|
|
177
|
+
// Resolve the effective status the same way createResponseWithMergedHeaders
|
|
178
|
+
// will (ctx.res.status override) so the problem body's status/title match
|
|
179
|
+
// the actual HTTP status — e.g. when a handler called ctx.setStatus()
|
|
180
|
+
// before throwing.
|
|
181
|
+
const status =
|
|
182
|
+
reqCtx.res.status !== 200 ? reqCtx.res.status : derivedStatus;
|
|
183
|
+
|
|
184
|
+
if (preview.responseType === "json") {
|
|
185
|
+
return createResponseWithMergedHeaders(
|
|
186
|
+
JSON.stringify(createProblemDetails(error, status, isDev)),
|
|
187
|
+
{
|
|
188
|
+
status,
|
|
189
|
+
headers: {
|
|
190
|
+
"content-type": "application/problem+json;charset=utf-8",
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
183
196
|
const message =
|
|
184
197
|
error instanceof RouterError
|
|
185
198
|
? error.message
|
|
@@ -196,7 +209,9 @@ export async function handleResponseRoute<TEnv>(
|
|
|
196
209
|
// Wrap callHandler to append Vary: Accept on content-negotiated responses
|
|
197
210
|
const callHandlerWithVary = async () => {
|
|
198
211
|
const response = await callHandler();
|
|
199
|
-
if (preview.negotiated) {
|
|
212
|
+
if (preview.negotiated && !isWebSocketUpgradeResponse(response)) {
|
|
213
|
+
// Skip Vary on upgrade responses: headers are semantically immutable
|
|
214
|
+
// on some runtimes, and Vary is meaningless for a 101 response.
|
|
200
215
|
response.headers.append("Vary", "Accept");
|
|
201
216
|
}
|
|
202
217
|
return response;
|