@rangojs/router 0.0.0-experimental.d7eeaa75 → 0.0.0-experimental.dc2bd2b4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/README.md +120 -25
  2. package/dist/bin/rango.js +147 -57
  3. package/dist/testing/vitest.js +48 -0
  4. package/dist/vite/index.js +2151 -846
  5. package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  6. package/package.json +57 -11
  7. package/skills/breadcrumbs/SKILL.md +3 -1
  8. package/skills/bundle-analysis/SKILL.md +159 -0
  9. package/skills/cache-guide/SKILL.md +220 -30
  10. package/skills/caching/SKILL.md +116 -8
  11. package/skills/composability/SKILL.md +27 -2
  12. package/skills/document-cache/SKILL.md +78 -55
  13. package/skills/handler-use/SKILL.md +364 -0
  14. package/skills/hooks/SKILL.md +229 -20
  15. package/skills/host-router/SKILL.md +45 -20
  16. package/skills/i18n/SKILL.md +276 -0
  17. package/skills/intercept/SKILL.md +46 -4
  18. package/skills/layout/SKILL.md +28 -7
  19. package/skills/links/SKILL.md +247 -17
  20. package/skills/loader/SKILL.md +219 -9
  21. package/skills/middleware/SKILL.md +47 -12
  22. package/skills/migrate-nextjs/SKILL.md +562 -0
  23. package/skills/migrate-react-router/SKILL.md +769 -0
  24. package/skills/mime-routes/SKILL.md +27 -0
  25. package/skills/observability/SKILL.md +137 -0
  26. package/skills/parallel/SKILL.md +71 -6
  27. package/skills/prerender/SKILL.md +14 -33
  28. package/skills/rango/SKILL.md +242 -22
  29. package/skills/react-compiler/SKILL.md +168 -0
  30. package/skills/response-routes/SKILL.md +66 -9
  31. package/skills/route/SKILL.md +57 -4
  32. package/skills/router-setup/SKILL.md +3 -3
  33. package/skills/server-actions/SKILL.md +751 -0
  34. package/skills/streams-and-websockets/SKILL.md +283 -0
  35. package/skills/testing/SKILL.md +647 -0
  36. package/skills/typesafety/SKILL.md +319 -27
  37. package/skills/use-cache/SKILL.md +34 -5
  38. package/skills/view-transitions/SKILL.md +294 -0
  39. package/src/__augment-tests__/augment.ts +81 -0
  40. package/src/__augment-tests__/augmented.check.ts +117 -0
  41. package/src/browser/action-coordinator.ts +53 -36
  42. package/src/browser/app-shell.ts +52 -0
  43. package/src/browser/event-controller.ts +86 -70
  44. package/src/browser/history-state.ts +21 -0
  45. package/src/browser/index.ts +3 -3
  46. package/src/browser/navigation-bridge.ts +84 -11
  47. package/src/browser/navigation-client.ts +76 -28
  48. package/src/browser/navigation-store.ts +32 -9
  49. package/src/browser/navigation-transaction.ts +10 -28
  50. package/src/browser/partial-update.ts +64 -26
  51. package/src/browser/prefetch/cache.ts +129 -21
  52. package/src/browser/prefetch/fetch.ts +148 -16
  53. package/src/browser/prefetch/queue.ts +36 -5
  54. package/src/browser/rango-state.ts +53 -13
  55. package/src/browser/react/Link.tsx +30 -2
  56. package/src/browser/react/NavigationProvider.tsx +72 -31
  57. package/src/browser/react/filter-segment-order.ts +51 -7
  58. package/src/browser/react/index.ts +3 -0
  59. package/src/browser/react/location-state-shared.ts +175 -4
  60. package/src/browser/react/location-state.ts +39 -13
  61. package/src/browser/react/use-handle.ts +17 -9
  62. package/src/browser/react/use-navigation.ts +22 -2
  63. package/src/browser/react/use-params.ts +20 -8
  64. package/src/browser/react/use-reverse.ts +106 -0
  65. package/src/browser/react/use-router.ts +22 -2
  66. package/src/browser/react/use-segments.ts +11 -8
  67. package/src/browser/response-adapter.ts +25 -0
  68. package/src/browser/rsc-router.tsx +64 -22
  69. package/src/browser/scroll-restoration.ts +22 -14
  70. package/src/browser/segment-reconciler.ts +36 -14
  71. package/src/browser/segment-structure-assert.ts +2 -2
  72. package/src/browser/server-action-bridge.ts +23 -30
  73. package/src/browser/types.ts +21 -0
  74. package/src/build/collect-fallback-refs.ts +107 -0
  75. package/src/build/generate-manifest.ts +60 -35
  76. package/src/build/generate-route-types.ts +2 -0
  77. package/src/build/index.ts +2 -0
  78. package/src/build/route-trie.ts +52 -25
  79. package/src/build/route-types/codegen.ts +4 -4
  80. package/src/build/route-types/include-resolution.ts +1 -1
  81. package/src/build/route-types/per-module-writer.ts +7 -4
  82. package/src/build/route-types/router-processing.ts +55 -14
  83. package/src/build/route-types/scan-filter.ts +1 -1
  84. package/src/build/route-types/source-scan.ts +118 -0
  85. package/src/build/runtime-discovery.ts +9 -20
  86. package/src/cache/cache-scope.ts +28 -42
  87. package/src/cache/cf/cf-cache-store.ts +54 -13
  88. package/src/client.rsc.tsx +3 -0
  89. package/src/client.tsx +92 -182
  90. package/src/context-var.ts +5 -5
  91. package/src/decode-loader-results.ts +36 -0
  92. package/src/errors.ts +30 -1
  93. package/src/handle.ts +26 -13
  94. package/src/host/index.ts +2 -2
  95. package/src/host/router.ts +129 -57
  96. package/src/host/types.ts +31 -2
  97. package/src/host/utils.ts +1 -1
  98. package/src/href-client.ts +140 -20
  99. package/src/index.rsc.ts +9 -4
  100. package/src/index.ts +53 -15
  101. package/src/loader-store.ts +500 -0
  102. package/src/loader.rsc.ts +2 -5
  103. package/src/loader.ts +3 -10
  104. package/src/missing-id-error.ts +68 -0
  105. package/src/outlet-context.ts +1 -1
  106. package/src/prerender.ts +4 -4
  107. package/src/response-utils.ts +37 -0
  108. package/src/reverse.ts +65 -36
  109. package/src/route-content-wrapper.tsx +6 -28
  110. package/src/route-definition/dsl-helpers.ts +384 -257
  111. package/src/route-definition/helper-factories.ts +29 -139
  112. package/src/route-definition/helpers-types.ts +100 -28
  113. package/src/route-definition/resolve-handler-use.ts +6 -0
  114. package/src/route-definition/use-item-types.ts +32 -0
  115. package/src/route-types.ts +26 -41
  116. package/src/router/basename.ts +14 -0
  117. package/src/router/content-negotiation.ts +15 -2
  118. package/src/router/error-handling.ts +1 -1
  119. package/src/router/handler-context.ts +21 -38
  120. package/src/router/intercept-resolution.ts +4 -18
  121. package/src/router/lazy-includes.ts +8 -8
  122. package/src/router/loader-resolution.ts +19 -2
  123. package/src/router/manifest.ts +22 -13
  124. package/src/router/match-api.ts +4 -3
  125. package/src/router/match-handlers.ts +63 -20
  126. package/src/router/match-middleware/cache-lookup.ts +44 -91
  127. package/src/router/match-middleware/cache-store.ts +3 -2
  128. package/src/router/match-result.ts +53 -32
  129. package/src/router/metrics.ts +1 -1
  130. package/src/router/middleware-types.ts +15 -26
  131. package/src/router/middleware.ts +99 -84
  132. package/src/router/pattern-matching.ts +101 -17
  133. package/src/router/prerender-match.ts +1 -1
  134. package/src/router/preview-match.ts +3 -1
  135. package/src/router/request-classification.ts +4 -28
  136. package/src/router/revalidation.ts +58 -2
  137. package/src/router/router-interfaces.ts +45 -28
  138. package/src/router/router-options.ts +40 -1
  139. package/src/router/router-registry.ts +2 -5
  140. package/src/router/segment-resolution/fresh.ts +27 -6
  141. package/src/router/segment-resolution/revalidation.ts +147 -106
  142. package/src/router/segment-resolution/view-transition-default.ts +36 -0
  143. package/src/router/substitute-pattern-params.ts +56 -0
  144. package/src/router/telemetry.ts +99 -0
  145. package/src/router/trie-matching.ts +18 -13
  146. package/src/router/types.ts +8 -0
  147. package/src/router/url-params.ts +49 -0
  148. package/src/router.ts +38 -23
  149. package/src/rsc/handler-context.ts +2 -2
  150. package/src/rsc/handler.ts +28 -69
  151. package/src/rsc/helpers.ts +91 -43
  152. package/src/rsc/index.ts +1 -1
  153. package/src/rsc/origin-guard.ts +28 -10
  154. package/src/rsc/progressive-enhancement.ts +4 -0
  155. package/src/rsc/response-route-handler.ts +46 -53
  156. package/src/rsc/rsc-rendering.ts +35 -51
  157. package/src/rsc/runtime-warnings.ts +9 -10
  158. package/src/rsc/server-action.ts +17 -37
  159. package/src/rsc/ssr-setup.ts +16 -0
  160. package/src/rsc/types.ts +8 -2
  161. package/src/search-params.ts +4 -4
  162. package/src/segment-content-promise.ts +67 -0
  163. package/src/segment-loader-promise.ts +122 -0
  164. package/src/segment-system.tsx +132 -116
  165. package/src/serialize.ts +243 -0
  166. package/src/server/context.ts +143 -53
  167. package/src/server/cookie-store.ts +28 -4
  168. package/src/server/request-context.ts +20 -42
  169. package/src/ssr/index.tsx +5 -1
  170. package/src/static-handler.ts +1 -1
  171. package/src/testing/cache-status.ts +166 -0
  172. package/src/testing/collect-handle.ts +63 -0
  173. package/src/testing/dispatch.ts +440 -0
  174. package/src/testing/dom.entry.ts +22 -0
  175. package/src/testing/e2e/fixture.ts +154 -0
  176. package/src/testing/e2e/index.ts +149 -0
  177. package/src/testing/e2e/matchers.ts +51 -0
  178. package/src/testing/e2e/page-helpers.ts +272 -0
  179. package/src/testing/e2e/parity.ts +306 -0
  180. package/src/testing/e2e/server.ts +183 -0
  181. package/src/testing/flight-matchers.ts +104 -0
  182. package/src/testing/flight-runtime.d.ts +21 -0
  183. package/src/testing/flight.entry.ts +22 -0
  184. package/src/testing/flight.ts +182 -0
  185. package/src/testing/generated-routes.ts +223 -0
  186. package/src/testing/index.ts +105 -0
  187. package/src/testing/internal/context.ts +193 -0
  188. package/src/testing/render-route.tsx +536 -0
  189. package/src/testing/run-loader.ts +296 -0
  190. package/src/testing/run-middleware.ts +170 -0
  191. package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
  192. package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
  193. package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
  194. package/src/testing/vitest-stubs/version.ts +5 -0
  195. package/src/testing/vitest.ts +183 -0
  196. package/src/types/global-namespace.ts +39 -26
  197. package/src/types/handler-context.ts +68 -50
  198. package/src/types/index.ts +1 -0
  199. package/src/types/loader-types.ts +5 -6
  200. package/src/types/request-scope.ts +126 -0
  201. package/src/types/route-entry.ts +11 -0
  202. package/src/types/segments.ts +35 -2
  203. package/src/urls/include-helper.ts +34 -67
  204. package/src/urls/index.ts +0 -3
  205. package/src/urls/path-helper-types.ts +41 -7
  206. package/src/urls/path-helper.ts +17 -52
  207. package/src/urls/pattern-types.ts +36 -19
  208. package/src/urls/response-types.ts +22 -29
  209. package/src/urls/type-extraction.ts +26 -116
  210. package/src/urls/urls-function.ts +1 -5
  211. package/src/use-loader.tsx +413 -42
  212. package/src/vite/debug.ts +185 -0
  213. package/src/vite/discovery/bundle-postprocess.ts +6 -6
  214. package/src/vite/discovery/discover-routers.ts +101 -51
  215. package/src/vite/discovery/discovery-errors.ts +194 -0
  216. package/src/vite/discovery/gate-state.ts +171 -0
  217. package/src/vite/discovery/prerender-collection.ts +67 -26
  218. package/src/vite/discovery/route-types-writer.ts +40 -84
  219. package/src/vite/discovery/self-gen-tracking.ts +27 -1
  220. package/src/vite/discovery/state.ts +33 -0
  221. package/src/vite/discovery/virtual-module-codegen.ts +13 -23
  222. package/src/vite/index.ts +2 -0
  223. package/src/vite/plugin-types.ts +67 -0
  224. package/src/vite/plugins/cjs-to-esm.ts +8 -7
  225. package/src/vite/plugins/client-ref-dedup.ts +16 -0
  226. package/src/vite/plugins/client-ref-hashing.ts +28 -5
  227. package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
  228. package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
  229. package/src/vite/plugins/cloudflare-protocol-stub.ts +214 -0
  230. package/src/vite/plugins/expose-action-id.ts +54 -30
  231. package/src/vite/plugins/expose-id-utils.ts +12 -8
  232. package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
  233. package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
  234. package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
  235. package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
  236. package/src/vite/plugins/expose-internal-ids.ts +496 -486
  237. package/src/vite/plugins/performance-tracks.ts +29 -25
  238. package/src/vite/plugins/use-cache-transform.ts +65 -50
  239. package/src/vite/plugins/version-injector.ts +39 -23
  240. package/src/vite/plugins/version-plugin.ts +59 -2
  241. package/src/vite/plugins/virtual-entries.ts +2 -2
  242. package/src/vite/rango.ts +116 -29
  243. package/src/vite/router-discovery.ts +750 -100
  244. package/src/vite/utils/ast-handler-extract.ts +15 -15
  245. package/src/vite/utils/banner.ts +1 -1
  246. package/src/vite/utils/bundle-analysis.ts +4 -2
  247. package/src/vite/utils/client-chunks.ts +190 -0
  248. package/src/vite/utils/forward-user-plugins.ts +193 -0
  249. package/src/vite/utils/manifest-utils.ts +21 -5
  250. package/src/vite/utils/package-resolution.ts +41 -1
  251. package/src/vite/utils/prerender-utils.ts +21 -6
  252. package/src/vite/utils/shared-utils.ts +107 -26
  253. package/src/browser/action-response-classifier.ts +0 -99
@@ -9,6 +9,7 @@
9
9
 
10
10
  import type { CookieOptions } from "../router/middleware-types.js";
11
11
  import { getRequestContext } from "./request-context.js";
12
+ import { isInsideCacheScope } from "./context.js";
12
13
  import { INSIDE_CACHE_EXEC } from "../cache/taint.js";
13
14
 
14
15
  /**
@@ -84,10 +85,23 @@ export interface ReadonlyHeaders {
84
85
  type HeadersIterator<T> = IterableIterator<T>;
85
86
 
86
87
  /**
87
- * Throw if called inside a "use cache" function.
88
- * Reading request-scoped data (cookies, headers) inside a cached function
89
- * produces results that vary per request but the cache key does not include
90
- * those values, leading to one user's data being served to another.
88
+ * Throw if called inside a cache boundary — either a "use cache" function
89
+ * (`INSIDE_CACHE_EXEC` stamped on ctx by the cache runtime) or a `cache()`
90
+ * DSL boundary (`isInsideCacheScope()` the render-store flag set while
91
+ * resolving a `type: "cache"` route entry).
92
+ *
93
+ * Reading request-scoped data (cookies, headers) inside a cached scope
94
+ * produces per-request values that are NOT reflected in the cache key, so
95
+ * they would be frozen into the shared cache entry and served to the wrong
96
+ * users. This is the same hazard for both scopes: a `cache()` boundary caches
97
+ * everything except loaders (it is the document-level "PPR shell"), so a read
98
+ * here is baked into the shell exactly like a `"use cache"` return value is
99
+ * baked into its cache entry.
100
+ *
101
+ * `isInsideCacheScope()` returns false inside loaders (loaders always run
102
+ * fresh on every request, even on a cache hit), so reading cookies()/headers()
103
+ * from a loader is allowed — loaders are the dynamic "holes" of a cached
104
+ * document.
91
105
  */
92
106
  function assertNotInsideCacheContext(ctx: unknown, fnName: string): void {
93
107
  if (
@@ -106,6 +120,16 @@ function assertNotInsideCacheContext(ctx: unknown, fnName: string): void {
106
120
  ` const data = await getCachedData(locale); // locale is now in the cache key`,
107
121
  );
108
122
  }
123
+ if (isInsideCacheScope()) {
124
+ throw new Error(
125
+ `${fnName}() cannot be called inside a cache() boundary. ` +
126
+ `A cache() scope caches everything except loaders, so request-scoped ` +
127
+ `data (cookies, headers) read here would be frozen into the shared ` +
128
+ `cached shell and served to other users. Read it inside a loader ` +
129
+ `instead — loaders always run fresh on every request, even on a cache hit:\n\n` +
130
+ ` loader("user", () => getUser(cookies().get("session")?.value));`,
131
+ );
132
+ }
109
133
  }
110
134
 
111
135
  const HEADERS_MUTATION_METHODS = new Set(["set", "append", "delete"]);
@@ -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:
@@ -349,6 +322,15 @@ export interface RequestContext<
349
322
  * to avoid a second resolveRoute call. Cleared on HMR invalidation.
350
323
  */
351
324
  _classifiedRoute?: import("../router/route-snapshot.js").RouteSnapshot;
325
+
326
+ /**
327
+ * @internal Coarse route-level cache signal for the X-Rango-Cache debug
328
+ * header. Populated by match/matchPartial only when the debug cache signal
329
+ * gate is enabled (debugCacheSignal option or RANGO_TEST_SIGNALS=1). Read by
330
+ * the response-finalization path (createResponseWithMergedHeaders). Undefined
331
+ * when the gate is off, so no header is emitted.
332
+ */
333
+ _cacheSignal?: import("../router/telemetry.js").CacheSegmentSignal[];
352
334
  }
353
335
 
354
336
  /**
@@ -389,6 +371,7 @@ export type PublicRequestContext<
389
371
  | "_setStatus"
390
372
  | "_variables"
391
373
  | "_classifiedRoute"
374
+ | "_cacheSignal"
392
375
  | "res"
393
376
  >;
394
377
 
@@ -498,13 +481,7 @@ export function requireRequestContext<
498
481
  return getRequestContext<TEnv>();
499
482
  }
500
483
 
501
- /**
502
- * Cloudflare Workers ExecutionContext (subset we need)
503
- */
504
- export interface ExecutionContext {
505
- waitUntil(promise: Promise<any>): void;
506
- passThroughOnException(): void;
507
- }
484
+ export type { ExecutionContext };
508
485
 
509
486
  /**
510
487
  * Options for creating a request context
@@ -768,16 +745,14 @@ export function createRequestContext<TEnv>(
768
745
 
769
746
  waitUntil(fn: () => Promise<void>): void {
770
747
  if (executionContext?.waitUntil) {
771
- // Cloudflare Workers: use native waitUntil
772
748
  executionContext.waitUntil(fn());
773
749
  } else {
774
- // Node.js / dev: fire-and-forget with error logging
775
- fn().catch((err) =>
776
- console.error("[waitUntil] Background task failed:", err),
777
- );
750
+ fireAndForgetWaitUntil(fn);
778
751
  }
779
752
  },
780
753
 
754
+ executionContext,
755
+
781
756
  _onResponseCallbacks: [],
782
757
 
783
758
  onResponse(callback: (response: Response) => Response): void {
@@ -1043,7 +1018,10 @@ export function createUseFunction<TEnv>(
1043
1018
  search: (ctx as any).search ?? {},
1044
1019
  pathname: ctx.pathname,
1045
1020
  url: ctx.url,
1021
+ originalUrl: ctx.originalUrl,
1046
1022
  env: ctx.env as any,
1023
+ waitUntil: ctx.waitUntil.bind(ctx),
1024
+ executionContext: ctx.executionContext,
1047
1025
  get: ctx.get as any,
1048
1026
  use: (<TDep, TDepParams = any>(
1049
1027
  dep: LoaderDefinition<TDep, TDepParams>,
package/src/ssr/index.tsx CHANGED
@@ -162,9 +162,13 @@ function createSsrEventController(opts: {
162
162
  }): EventController {
163
163
  const location = new URL(opts.pathname, "http://localhost");
164
164
  let params = opts.params ?? {};
165
+ const rawMatched = opts.matched ?? [];
165
166
  const handleState = {
166
167
  data: opts.handleData ?? {},
167
- segmentOrder: filterSegmentOrder(opts.matched ?? []),
168
+ segmentOrder: filterSegmentOrder(rawMatched),
169
+ routeSegmentIds: rawMatched.filter(
170
+ (id) => !id.includes(".@") && !/D\d+\./.test(id),
171
+ ),
168
172
  };
169
173
  const state: DerivedNavigationState = {
170
174
  state: "idle",
@@ -96,7 +96,7 @@ export function Static<TParams extends Record<string, any>>(
96
96
 
97
97
  if (!id) {
98
98
  throw new Error(
99
- "[rsc-router] Static: missing $$id. " +
99
+ "[rango] Static: missing $$id. " +
100
100
  "Ensure the exposeInternalIds Vite plugin is configured.",
101
101
  );
102
102
  }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Cache-status testing primitives for @rangojs/router consumers.
3
+ *
4
+ * Two complementary paths, both DEVELOPMENT/TEST ONLY:
5
+ *
6
+ * 1. Header path — `parseCacheHeader` / `assertCacheStatus` read the
7
+ * `X-Rango-Cache` response header. The header is emitted only when the
8
+ * router's debug cache signal gate is on (the `debugCacheSignal` option or
9
+ * `RANGO_TEST_SIGNALS=1`). With the gate off there is no header and these
10
+ * helpers throw a clear "header missing" error.
11
+ *
12
+ * 2. Telemetry path — `createCacheSink` returns a `{ sink, events }` pair the
13
+ * consumer wires via `createRouter({ telemetry: sink })`. This has ZERO
14
+ * production surface: no header, just structured `cache.decision` events
15
+ * (which carry the same coarse `segments` cache signal).
16
+ *
17
+ * v1 cache status is COARSE (route-level): the router reports a single entry
18
+ * keyed by the route key (the route NAME), not per individual segment.
19
+ *
20
+ * Import path: from a Vitest unit/integration test use `@rangojs/router/testing`;
21
+ * from a Playwright e2e use `@rangojs/router/testing/e2e` (the barrel pulls a
22
+ * build-only virtual that does not resolve in a plain Playwright runner).
23
+ */
24
+
25
+ import type {
26
+ CacheDecisionEvent,
27
+ CacheSegmentStatus,
28
+ TelemetryEvent,
29
+ TelemetrySink,
30
+ } from "../router/telemetry.js";
31
+
32
+ const CACHE_HEADER = "X-Rango-Cache";
33
+
34
+ /** Expected cache status passed to assertCacheStatus. */
35
+ export type ExpectedCacheStatus = CacheSegmentStatus;
36
+
37
+ /** A target carrying response headers (a Response or a `{ headers }` object). */
38
+ export type CacheStatusTarget = Response | { headers: Headers };
39
+
40
+ /**
41
+ * Parse an `X-Rango-Cache` header value into a `{ routeKey: status }` map.
42
+ *
43
+ * Header format: `<routeKey>=<status>, <routeKey2>=<status2>`. The key is the
44
+ * route NAME (ctx.routeKey, e.g. `product.detail`), NOT the URL pattern —
45
+ * see assertCacheStatus. Whitespace around entries and the `=` is tolerated.
46
+ * Entries without a status are ignored.
47
+ *
48
+ * @example
49
+ * parseCacheHeader("product.detail=hit, shop.layout=stale")
50
+ * // => { "product.detail": "hit", "shop.layout": "stale" }
51
+ */
52
+ export function parseCacheHeader(
53
+ headerValue: string | null | undefined,
54
+ ): Record<string, string> {
55
+ const result: Record<string, string> = {};
56
+ if (!headerValue) return result;
57
+ for (const rawEntry of headerValue.split(",")) {
58
+ const entry = rawEntry.trim();
59
+ if (entry.length === 0) continue;
60
+ const eq = entry.indexOf("=");
61
+ if (eq === -1) continue;
62
+ const id = entry.slice(0, eq).trim();
63
+ const status = entry.slice(eq + 1).trim();
64
+ if (id.length === 0 || status.length === 0) continue;
65
+ result[id] = status;
66
+ }
67
+ return result;
68
+ }
69
+
70
+ function getHeaders(target: CacheStatusTarget): Headers {
71
+ return target.headers;
72
+ }
73
+
74
+ /**
75
+ * Assert that the `X-Rango-Cache` header reports `expected` status for the
76
+ * given route. Throws a descriptive error when the header is missing (gate
77
+ * off), the route is absent, or the status differs.
78
+ *
79
+ * `routeKey` is the route NAME (e.g. `product.detail`), the same id the header
80
+ * carries — NOT the URL pattern (`/products/:id`). The signal is built from
81
+ * ctx.routeKey (telemetry.ts), so a pattern-shaped key never matches.
82
+ *
83
+ * The header is produced by the RSC render pipeline, so get the Response from
84
+ * the router's real fetch path (`router.fetch(...)`), with the debug cache
85
+ * signal gate enabled (`debugCacheSignal: true` or `RANGO_TEST_SIGNALS=1`).
86
+ * NOTE: `dispatch()` is the non-RSC primitive and never emits this header.
87
+ *
88
+ * @example
89
+ * // debugCacheSignal must be enabled on the router under test.
90
+ * const res = await router.fetch(new Request("https://app/products/42"));
91
+ * assertCacheStatus(res, "product.detail", "hit");
92
+ */
93
+ export function assertCacheStatus(
94
+ target: CacheStatusTarget,
95
+ segment: string,
96
+ expected: ExpectedCacheStatus,
97
+ ): void {
98
+ const headerValue = getHeaders(target).get(CACHE_HEADER);
99
+ if (headerValue === null) {
100
+ throw new Error(
101
+ `assertCacheStatus: response has no ${CACHE_HEADER} header. ` +
102
+ `Enable the debug cache signal via createRouter({ debugCacheSignal: true }) ` +
103
+ `or RANGO_TEST_SIGNALS=1.`,
104
+ );
105
+ }
106
+ const map = parseCacheHeader(headerValue);
107
+ const actual = map[segment];
108
+ if (actual === undefined) {
109
+ const known = Object.keys(map);
110
+ throw new Error(
111
+ `assertCacheStatus: segment "${segment}" not found in ${CACHE_HEADER} ` +
112
+ `("${headerValue}"). Known segments: ${
113
+ known.length > 0 ? known.join(", ") : "(none)"
114
+ }.`,
115
+ );
116
+ }
117
+ if (actual !== expected) {
118
+ throw new Error(
119
+ `assertCacheStatus: segment "${segment}" expected "${expected}" but got "${actual}".`,
120
+ );
121
+ }
122
+ }
123
+
124
+ /**
125
+ * A telemetry sink paired with the array it records events into.
126
+ */
127
+ export interface CacheSink {
128
+ /** Wire into `createRouter({ telemetry: sink })`. */
129
+ sink: TelemetrySink;
130
+ /** All telemetry events captured so far, in emit order. */
131
+ events: TelemetryEvent[];
132
+ }
133
+
134
+ /**
135
+ * Create a capturing telemetry sink for asserting on `cache.decision` events.
136
+ *
137
+ * This is the ZERO-production-surface path: no response header is emitted, the
138
+ * consumer just inspects the captured events.
139
+ *
140
+ * @example
141
+ * const { sink, events } = createCacheSink();
142
+ * const router = createRouter({ telemetry: sink, ... });
143
+ * // ...send a request through the router's RSC fetch path...
144
+ * const decisions = filterCacheDecisions(events);
145
+ * expect(decisions[0].segments?.[0].cacheStatus).toBe("hit");
146
+ */
147
+ export function createCacheSink(): CacheSink {
148
+ const events: TelemetryEvent[] = [];
149
+ const sink: TelemetrySink = {
150
+ emit(event: TelemetryEvent): void {
151
+ events.push(event);
152
+ },
153
+ };
154
+ return { sink, events };
155
+ }
156
+
157
+ /**
158
+ * Filter captured telemetry events down to `cache.decision` events.
159
+ */
160
+ export function filterCacheDecisions(
161
+ events: readonly TelemetryEvent[],
162
+ ): CacheDecisionEvent[] {
163
+ return events.filter(
164
+ (e): e is CacheDecisionEvent => e.type === "cache.decision",
165
+ );
166
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * collectHandle — unit-test a handle's `collect`/accumulator function directly.
3
+ *
4
+ * A handle's collect function (the `createHandle(collect)` argument that maps the
5
+ * per-segment pushed values into the accumulated result) is otherwise not
6
+ * directly reachable: createHandle keeps it in a private registry keyed by the
7
+ * handle's `$$id` and returns only `{ __brand, $$id }`. This primitive runs that
8
+ * REAL registered collect on per-segment values you provide and returns the
9
+ * accumulated result — so the mapper/accumulator is unit-testable without a full
10
+ * route match.
11
+ *
12
+ * It relies on createHandle registering the collect even in a bare test (it
13
+ * assigns a runtime fallback id when the Vite plugin did not inject one). If a
14
+ * handle's module was never imported (so createHandle never ran), the collect is
15
+ * unregistered and this falls back to a flat array with a warning.
16
+ */
17
+
18
+ import { getCollectFn, type Handle } from "../handle.js";
19
+
20
+ /**
21
+ * Run a handle's collect function on per-segment pushed values.
22
+ *
23
+ * @param handle - The handle whose collect to run.
24
+ * @param segments - Per-segment pushed values: each entry is the array of values
25
+ * one route segment pushed for this handle, in parent -> child order. Empty
26
+ * per-segment arrays are dropped before the collect runs, matching production
27
+ * collectHandleData (a segment that pushed nothing is not passed through).
28
+ * @returns The accumulated value the handle's collect produces.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * // Default flatten
33
+ * collectHandle(Breadcrumbs, [[{ label: "Home", href: "/" }], [{ label: "P", href: "/p" }]]);
34
+ * // -> [{ label: "Home", href: "/" }, { label: "P", href: "/p" }]
35
+ *
36
+ * // Custom "last wins"
37
+ * const PageTitle = createHandle<string, string>((s) => s.flat().at(-1) ?? "");
38
+ * collectHandle(PageTitle, [["Home"], ["Product"]]); // -> "Product"
39
+ * ```
40
+ */
41
+ export function collectHandle<TData, TAccumulated>(
42
+ handle: Handle<TData, TAccumulated>,
43
+ segments: ReadonlyArray<ReadonlyArray<TData>>,
44
+ ): TAccumulated {
45
+ const collectFn = getCollectFn(handle.$$id) as
46
+ | ((segments: TData[][]) => TAccumulated)
47
+ | undefined;
48
+
49
+ if (!collectFn) {
50
+ console.warn(
51
+ `[rango] collectHandle: handle "${handle.$$id}" has no registered collect ` +
52
+ `function. Import the handle's module so createHandle() runs. Falling ` +
53
+ `back to a flat array.`,
54
+ );
55
+ return segments.flat() as unknown as TAccumulated;
56
+ }
57
+
58
+ // Match production collectHandleData (handle.ts): segments that pushed
59
+ // nothing (empty arrays) are dropped before the collect runs, so a collect
60
+ // that inspects segment count or indices sees the same input as at runtime.
61
+ const nonEmpty = segments.filter((seg) => seg.length > 0) as TData[][];
62
+ return collectFn(nonEmpty);
63
+ }