@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.10

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 (214) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +470 -77
  22. package/dist/dom.js +18 -3
  23. package/dist/effects.js +2 -2
  24. package/dist/flight-props.d.ts +7 -0
  25. package/dist/flight-props.js +105 -0
  26. package/dist/handler-html.js +4 -4
  27. package/dist/handler-routing.d.ts +2 -23
  28. package/dist/handler-routing.js +9 -190
  29. package/dist/handler-segments.d.ts +6 -0
  30. package/dist/handler-segments.js +18 -4
  31. package/dist/head-tags.d.ts +9 -0
  32. package/dist/head-tags.js +93 -6
  33. package/dist/highlight-bind.js +1 -1
  34. package/dist/highlight.d.ts +3 -2
  35. package/dist/highlight.js +17 -12
  36. package/dist/hints.d.ts +25 -2
  37. package/dist/hints.js +147 -74
  38. package/dist/index.d.ts +2 -0
  39. package/dist/index.js +3 -0
  40. package/dist/island-swap.d.ts +47 -0
  41. package/dist/island-swap.js +635 -0
  42. package/dist/jsx-dev-runtime.d.ts +1 -1
  43. package/dist/jsx-types.d.ts +99 -3
  44. package/dist/krate-core.js +1 -1
  45. package/dist/krate-dev.js +385 -66
  46. package/dist/krate-f-actions.js +1 -1
  47. package/dist/krate-f-classes.js +1 -1
  48. package/dist/krate-f-highlight.js +1 -1
  49. package/dist/krate-f-list.js +1 -1
  50. package/dist/krate-f-navigation.js +1 -1
  51. package/dist/krate-f-router-lite.js +1 -1
  52. package/dist/krate-f-router.js +1 -1
  53. package/dist/krate-features.json +454 -0
  54. package/dist/krate-hydrate-lite.js +5 -5
  55. package/dist/krate-hydrate.js +5 -5
  56. package/dist/krate-island-swap.js +1 -0
  57. package/dist/krate-runtime.js +5 -5
  58. package/dist/list.d.ts +14 -4
  59. package/dist/list.js +4 -5
  60. package/dist/metadata.js +5 -2
  61. package/dist/navigation-react.d.ts +13 -0
  62. package/dist/navigation-react.js +86 -0
  63. package/dist/navigation.d.ts +19 -0
  64. package/dist/navigation.js +35 -0
  65. package/dist/node-server.js +3 -3
  66. package/dist/og.js +1 -1
  67. package/dist/platform.d.ts +52 -0
  68. package/dist/platform.js +57 -0
  69. package/dist/react-compat.d.ts +39 -0
  70. package/dist/react-compat.js +359 -0
  71. package/dist/react-server.d.ts +63 -0
  72. package/dist/react-server.js +125 -0
  73. package/dist/region-wire.d.ts +10 -0
  74. package/dist/region-wire.js +154 -0
  75. package/dist/request-context.js +1 -1
  76. package/dist/request-extras.d.ts +4 -0
  77. package/dist/request-extras.js +51 -0
  78. package/dist/resp.js +1 -1
  79. package/dist/route-source.d.ts +39 -0
  80. package/dist/route-source.js +532 -0
  81. package/dist/router-lite.js +3 -9
  82. package/dist/router.d.ts +1 -1
  83. package/dist/router.js +628 -98
  84. package/dist/server-handler.d.ts +69 -1
  85. package/dist/server-handler.js +515 -96
  86. package/dist/server-renderer.js +16 -7
  87. package/dist/server.d.ts +73 -16
  88. package/dist/server.js +619 -102
  89. package/dist/sha256.js +9 -9
  90. package/dist/signal.js +0 -6
  91. package/dist/site-config.d.ts +15 -0
  92. package/dist/site-config.js +28 -0
  93. package/dist/slot.d.ts +12 -0
  94. package/dist/slot.js +23 -0
  95. package/dist/ssr-core.d.ts +2 -0
  96. package/dist/ssr-core.js +12 -4
  97. package/dist/testing.d.ts +62 -0
  98. package/dist/testing.js +90 -0
  99. package/dist/use-id.js +1 -1
  100. package/package.json +28 -3
  101. package/dist/action-context.test.d.ts +0 -1
  102. package/dist/action-context.test.js +0 -72
  103. package/dist/actions.test.d.ts +0 -1
  104. package/dist/actions.test.js +0 -477
  105. package/dist/cache-adapter.test.d.ts +0 -1
  106. package/dist/cache-adapter.test.js +0 -244
  107. package/dist/cache.test.d.ts +0 -1
  108. package/dist/cache.test.js +0 -233
  109. package/dist/class-component.test.d.ts +0 -1
  110. package/dist/class-component.test.js +0 -462
  111. package/dist/class-helpers.test.d.ts +0 -1
  112. package/dist/class-helpers.test.js +0 -169
  113. package/dist/context.test.d.ts +0 -1
  114. package/dist/context.test.js +0 -57
  115. package/dist/derived.test.d.ts +0 -1
  116. package/dist/derived.test.js +0 -140
  117. package/dist/dev-overlay.test.d.ts +0 -1
  118. package/dist/dev-overlay.test.js +0 -165
  119. package/dist/discrete.test.d.ts +0 -1
  120. package/dist/discrete.test.js +0 -100
  121. package/dist/dom.test.d.ts +0 -1
  122. package/dist/dom.test.js +0 -39
  123. package/dist/effect-errors.test.d.ts +0 -1
  124. package/dist/effect-errors.test.js +0 -199
  125. package/dist/effects.test.d.ts +0 -1
  126. package/dist/effects.test.js +0 -485
  127. package/dist/events.test.d.ts +0 -1
  128. package/dist/events.test.js +0 -447
  129. package/dist/handler-html.test.d.ts +0 -1
  130. package/dist/handler-html.test.js +0 -256
  131. package/dist/handler-routing.test.d.ts +0 -1
  132. package/dist/handler-routing.test.js +0 -99
  133. package/dist/handler-segments.test.d.ts +0 -1
  134. package/dist/handler-segments.test.js +0 -217
  135. package/dist/head-tags.test.d.ts +0 -1
  136. package/dist/head-tags.test.js +0 -131
  137. package/dist/highlight-bind.test.d.ts +0 -1
  138. package/dist/highlight-bind.test.js +0 -124
  139. package/dist/highlight.test.d.ts +0 -1
  140. package/dist/highlight.test.js +0 -56
  141. package/dist/lanes.test.d.ts +0 -1
  142. package/dist/lanes.test.js +0 -59
  143. package/dist/list.test.d.ts +0 -1
  144. package/dist/list.test.js +0 -435
  145. package/dist/metadata.test.d.ts +0 -1
  146. package/dist/metadata.test.js +0 -167
  147. package/dist/navigation.test.d.ts +0 -1
  148. package/dist/navigation.test.js +0 -130
  149. package/dist/node-server.test.d.ts +0 -1
  150. package/dist/node-server.test.js +0 -492
  151. package/dist/optimistic-lanes.test.d.ts +0 -1
  152. package/dist/optimistic-lanes.test.js +0 -45
  153. package/dist/patterns.test.d.ts +0 -1
  154. package/dist/patterns.test.js +0 -125
  155. package/dist/portal.test.d.ts +0 -1
  156. package/dist/portal.test.js +0 -99
  157. package/dist/react-effects.test.d.ts +0 -1
  158. package/dist/react-effects.test.js +0 -133
  159. package/dist/react-suspense.test.d.ts +0 -1
  160. package/dist/react-suspense.test.js +0 -194
  161. package/dist/react.test.d.ts +0 -1
  162. package/dist/react.test.js +0 -60
  163. package/dist/react19.test.d.ts +0 -1
  164. package/dist/react19.test.js +0 -176
  165. package/dist/reactivity-core.test.d.ts +0 -1
  166. package/dist/reactivity-core.test.js +0 -152
  167. package/dist/reconcile.test.d.ts +0 -1
  168. package/dist/reconcile.test.js +0 -110
  169. package/dist/refs.test.d.ts +0 -1
  170. package/dist/refs.test.js +0 -257
  171. package/dist/router-layout.test.d.ts +0 -1
  172. package/dist/router-layout.test.js +0 -249
  173. package/dist/router-nav.test.d.ts +0 -1
  174. package/dist/router-nav.test.js +0 -559
  175. package/dist/router-slots.test.d.ts +0 -1
  176. package/dist/router-slots.test.js +0 -144
  177. package/dist/router-speculation.test.d.ts +0 -1
  178. package/dist/router-speculation.test.js +0 -25
  179. package/dist/router-styles.test.d.ts +0 -1
  180. package/dist/router-styles.test.js +0 -98
  181. package/dist/router.test.d.ts +0 -1
  182. package/dist/router.test.js +0 -36
  183. package/dist/server-async.test.d.ts +0 -1
  184. package/dist/server-async.test.js +0 -216
  185. package/dist/server-handler.test.d.ts +0 -1
  186. package/dist/server-handler.test.js +0 -535
  187. package/dist/server-highlight.test.d.ts +0 -1
  188. package/dist/server-highlight.test.js +0 -32
  189. package/dist/server-renderer.test.d.ts +0 -1
  190. package/dist/server-renderer.test.js +0 -266
  191. package/dist/server.test.d.ts +0 -1
  192. package/dist/server.test.js +0 -88
  193. package/dist/sha256.test.d.ts +0 -1
  194. package/dist/sha256.test.js +0 -13
  195. package/dist/signal-lanes.test.d.ts +0 -1
  196. package/dist/signal-lanes.test.js +0 -32
  197. package/dist/signal.test.d.ts +0 -1
  198. package/dist/signal.test.js +0 -68
  199. package/dist/slots.test.d.ts +0 -1
  200. package/dist/slots.test.js +0 -187
  201. package/dist/snapshots.test.d.ts +0 -1
  202. package/dist/snapshots.test.js +0 -208
  203. package/dist/ssr-core-behavior.test.d.ts +0 -1
  204. package/dist/ssr-core-behavior.test.js +0 -620
  205. package/dist/ssr-core.test.d.ts +0 -1
  206. package/dist/ssr-core.test.js +0 -324
  207. package/dist/static-memory.test.d.ts +0 -1
  208. package/dist/static-memory.test.js +0 -26
  209. package/dist/storage.test.d.ts +0 -1
  210. package/dist/storage.test.js +0 -46
  211. package/dist/suspense-actions.test.d.ts +0 -1
  212. package/dist/suspense-actions.test.js +0 -72
  213. package/dist/suspense.test.d.ts +0 -1
  214. package/dist/suspense.test.js +0 -556
package/dist/sha256.js CHANGED
@@ -1,14 +1,14 @@
1
1
  // Portable synchronous SHA-256 (no node:crypto, no Web Crypto) so the render
2
2
  // core can compute the same error digest on Node, Workers and edge hosts.
3
3
  const K = new Uint32Array([
4
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
5
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
6
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
7
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
8
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
9
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
10
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
11
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
4
+ 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
5
+ 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
6
+ 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
7
+ 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
8
+ 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
9
+ 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
10
+ 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
11
+ 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
12
12
  ]);
13
13
  export function sha256Hex(input) {
14
14
  const msg = new TextEncoder().encode(input);
@@ -21,7 +21,7 @@ export function sha256Hex(input) {
21
21
  view.setUint32(total - 8, Math.floor(bitLen / 0x100000000), false);
22
22
  view.setUint32(total - 4, bitLen >>> 0, false);
23
23
  const h = new Uint32Array([
24
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
24
+ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
25
25
  ]);
26
26
  const w = new Uint32Array(64);
27
27
  const rotr = (x, n) => (x >>> n) | (x << (32 - n));
package/dist/signal.js CHANGED
@@ -80,7 +80,6 @@ export function krateTagOwner(owner, scope) {
80
80
  // subscriber re-runs into a single flush so N writes in a loop (or one write
81
81
  // fanning out) produce one pass instead of N synchronous effect runs.
82
82
  const pending = [];
83
- let flushScheduled = false;
84
83
  function newEffectState(fn) {
85
84
  const scope = krateCurrentScope();
86
85
  const effect = { fn, cleanups: [], disposed: false, sources: new Set(), running: false, rerun: false, queued: false, owner: currentOwner, boundaryOwner: ownerHooks ? ownerHooks.current() : null, scope };
@@ -524,7 +523,6 @@ export function disposeAll() {
524
523
  }
525
524
  pending.length = 0;
526
525
  afterQueue.length = 0;
527
- flushScheduled = false;
528
526
  flushMode = 0;
529
527
  flushToken++;
530
528
  syncLane = false;
@@ -861,7 +859,6 @@ function scheduleFlush(reactWrite = false) {
861
859
  const micro = !later || discrete || committing > 0 || renderDepth > 0 || typeof setTimeout !== 'function';
862
860
  if (flushMode === 1 || (!micro && flushMode === 2))
863
861
  return;
864
- flushScheduled = true;
865
862
  const token = ++flushToken;
866
863
  const run = () => {
867
864
  if (token === flushToken)
@@ -887,7 +884,6 @@ function flushEffects() {
887
884
  }
888
885
  }
889
886
  function flushEffectsInner() {
890
- flushScheduled = false;
891
887
  flushMode = 0;
892
888
  flushToken++;
893
889
  // One effect throwing must not strand the effects queued behind it: their
@@ -951,7 +947,6 @@ function flushEffectsInner() {
951
947
  if (pending.length === 0) {
952
948
  // The cascade is done: a flush the passes scheduled has nothing to do.
953
949
  flushMode = 0;
954
- flushScheduled = false;
955
950
  flushToken++;
956
951
  }
957
952
  if (pending.length === 0) {
@@ -1009,7 +1004,6 @@ function abortUpdateLoop() {
1009
1004
  const looping = pending.splice(0);
1010
1005
  for (const effect of looping)
1011
1006
  effect.queued = false;
1012
- flushScheduled = false;
1013
1007
  flushMode = 0;
1014
1008
  flushToken++;
1015
1009
  const culprits = looping.length > 0 ? looping : lastPassEffects;
@@ -0,0 +1,15 @@
1
+ /** Site config injected by the compiler. */
2
+ export interface KrateRuntimeConfig {
3
+ basePath?: string;
4
+ viewTransitions?: boolean | 'auto';
5
+ /** Milliseconds a prefetched document stays fresh (default 30000). */
6
+ prefetchTTL?: number;
7
+ /** Default link prefetch: "viewport" also prefetches links as they scroll into view. */
8
+ prefetch?: 'intent' | 'viewport';
9
+ /** Set by `krate dev`: enables router dev warnings. */
10
+ dev?: boolean;
11
+ }
12
+ /** Id of the JSON data block the compiler writes. */
13
+ export declare const CFG_BLOCK_ID = "__krate_cfg__";
14
+ /** The site config, read once from the head's JSON block when the global is not set. */
15
+ export declare function siteConfig(): KrateRuntimeConfig;
@@ -0,0 +1,28 @@
1
+ // The site runtime config (`__KRATE_CFG__`). The compiler writes it into the
2
+ // page head as a JSON data block rather than an inline script, so a strict
3
+ // `script-src 'self'` policy needs no allowance for it. Server bundles set the
4
+ // global directly. The block is only present when something differs from the
5
+ // defaults (a base path, View Transitions off, dev mode).
6
+ /** Id of the JSON data block the compiler writes. */
7
+ export const CFG_BLOCK_ID = '__krate_cfg__';
8
+ /** The site config, read once from the head's JSON block when the global is not set. */
9
+ export function siteConfig() {
10
+ const g = globalThis;
11
+ if (g.__KRATE_CFG__)
12
+ return g.__KRATE_CFG__;
13
+ const doc = g.document;
14
+ const el = doc && typeof doc.getElementById === 'function' ? doc.getElementById(CFG_BLOCK_ID) : null;
15
+ if (el) {
16
+ try {
17
+ const parsed = JSON.parse(el.textContent || '{}');
18
+ if (parsed && typeof parsed === 'object') {
19
+ g.__KRATE_CFG__ = parsed;
20
+ return parsed;
21
+ }
22
+ }
23
+ catch {
24
+ // a malformed block falls back to the defaults
25
+ }
26
+ }
27
+ return {};
28
+ }
package/dist/slot.d.ts CHANGED
@@ -5,4 +5,16 @@ export declare const Root: typeof KrateSlot;
5
5
  export declare function Slottable(props: {
6
6
  children?: unknown;
7
7
  }): unknown;
8
+ type SlotProps = Record<string, unknown> & {
9
+ children?: unknown;
10
+ };
11
+ /**
12
+ * Radix's `createSlot(ownerName)` (react-slot >= 1.2): a Slot that merges its
13
+ * props and `ref` onto its single child. `ownerName` only labels the component.
14
+ */
15
+ export declare function createSlot(ownerName: string): (props: SlotProps) => Node | null;
16
+ /** Radix's `createSlottable(ownerName)`: a `Slottable` marker for one owner. */
17
+ export declare function createSlottable(ownerName: string): (props: {
18
+ children?: unknown;
19
+ }) => unknown;
8
20
  export default Slot;
package/dist/slot.js CHANGED
@@ -8,4 +8,27 @@ export const Root = KrateSlot;
8
8
  export function Slottable(props) {
9
9
  return props?.children;
10
10
  }
11
+ /**
12
+ * Radix's `createSlot(ownerName)` (react-slot >= 1.2): a Slot that merges its
13
+ * props and `ref` onto its single child. `ownerName` only labels the component.
14
+ */
15
+ export function createSlot(ownerName) {
16
+ const OwnerSlot = (props) => {
17
+ const node = KrateSlot(props);
18
+ const ref = props?.ref;
19
+ if (node && typeof ref === 'function')
20
+ ref(node);
21
+ else if (node && ref && typeof ref === 'object')
22
+ ref.current = node;
23
+ return node;
24
+ };
25
+ OwnerSlot.displayName = ownerName + '.Slot';
26
+ return OwnerSlot;
27
+ }
28
+ /** Radix's `createSlottable(ownerName)`: a `Slottable` marker for one owner. */
29
+ export function createSlottable(ownerName) {
30
+ const OwnerSlottable = (props) => props?.children;
31
+ OwnerSlottable.displayName = ownerName + '.Slottable';
32
+ return OwnerSlottable;
33
+ }
11
34
  export default Slot;
@@ -63,6 +63,8 @@ export interface RenderResponse {
63
63
  digest?: string;
64
64
  stack?: string;
65
65
  noStore?: boolean;
66
+ /** An error boundary caught a render error; html is its fallback and the answer is a 500. */
67
+ errorCaught?: boolean;
66
68
  }
67
69
  export declare function variantKey(req: {
68
70
  route: string;
package/dist/ssr-core.js CHANGED
@@ -30,7 +30,7 @@ export function newQueryTracker() {
30
30
  return { keys: new Set(), all: false };
31
31
  }
32
32
  export function trackedQuery(query, t) {
33
- const target = { ...(query || {}) };
33
+ const target = { ...query };
34
34
  const note = (prop) => {
35
35
  if (typeof prop === "string" && prop !== "then" && prop !== "toJSON" && prop !== "constructor")
36
36
  t.keys.add(prop);
@@ -369,8 +369,8 @@ export function createSSRCore(opts) {
369
369
  method: req.method || "GET",
370
370
  headers: h,
371
371
  cookies: parseCookieHeader(h.get("cookie")),
372
- params: { ...(req.params || {}) },
373
- searchParams: tracker ? trackedQuery(req.query, tracker) : { ...(req.query || {}) },
372
+ params: { ...req.params },
373
+ searchParams: tracker ? trackedQuery(req.query, tracker) : { ...req.query },
374
374
  };
375
375
  }
376
376
  function withRequest(req, fn, tracker) {
@@ -449,6 +449,12 @@ export function createSSRCore(opts) {
449
449
  status: 200,
450
450
  noStore: scope.noStore || invalidated,
451
451
  };
452
+ if (scope.errorCaught) {
453
+ // An error boundary rendered its fallback: the page is an error
454
+ // response, so it is never cached (scope.noStore keeps it out of ISR).
455
+ response.errorCaught = true;
456
+ response.noStore = true;
457
+ }
452
458
  // An uncacheable read (no-store) keeps the render out of the ISR cache, and
453
459
  // a revalidation that landed mid-render means the HTML may be stale.
454
460
  if (page.mode === "isr" && !scope.noStore && !invalidated) {
@@ -585,7 +591,7 @@ export function createSSRCore(opts) {
585
591
  // defines globalThis.__krate_render(propsJSON)) with the build-time baked props,
586
592
  // so nothing is re-derived at request time.
587
593
  function regionProps(region, req) {
588
- const props = { ...(region.props || {}) };
594
+ const props = { ...region.props };
589
595
  if (req.params && Object.keys(req.params).length > 0)
590
596
  props.params = req.params;
591
597
  if (req.query && Object.keys(req.query).length > 0)
@@ -865,6 +871,8 @@ export function createSSRCore(opts) {
865
871
  frame.stack = result.stack;
866
872
  if (result.noStore)
867
873
  frame.noStore = true;
874
+ if (result.errorCaught)
875
+ frame.errorCaught = true;
868
876
  if (page.serverComponents && frame.status >= 500)
869
877
  frame.fatal = true;
870
878
  // The page's <Head> children (title, meta, link, script) render in place;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Test helpers, exported from `@krate/runtime/testing`. They call an API route
3
+ * module or a server action in-process, without a running server. Node only
4
+ * (it uses AsyncLocalStorage); import it from your test files, never from app
5
+ * code.
6
+ */
7
+ import type { KrateRequest } from "./after.js";
8
+ export type RouteMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
9
+ export type RouteHandler = (request: KrateRequest, context: {
10
+ params: Record<string, string>;
11
+ }) => Response | Promise<Response>;
12
+ /** An imported `src/api/*.ts` module: named method exports and/or a default Web handler. */
13
+ export type RouteModule = Partial<Record<RouteMethod, RouteHandler>> & {
14
+ default?: unknown;
15
+ };
16
+ export interface CallRouteOptions {
17
+ /** Dynamic segments of the route (`[id]` -> `{ id: "7" }`). */
18
+ params?: Record<string, string>;
19
+ headers?: HeadersInit;
20
+ /** A JSON body; sets `content-type: application/json`. */
21
+ json?: unknown;
22
+ /** A raw body (string, FormData, ...). Ignored when `json` is set. */
23
+ body?: BodyInit | null;
24
+ /** The site base path, reported by request.nextUrl.basePath. */
25
+ basePath?: string;
26
+ }
27
+ /**
28
+ * Calls a route module the way the server does: the named export for `method`
29
+ * (HEAD falls back to GET without a body, OPTIONS answers `Allow`), else a
30
+ * default Web-style handler, else a 405 with `Allow`. `target` is a path
31
+ * (`"/api/users/7?x=1"`), an absolute URL, or a ready `Request`.
32
+ */
33
+ export declare function callRoute(mod: RouteModule, method: RouteMethod, target: string | Request, options?: CallRouteOptions): Promise<Response>;
34
+ export interface ActionCallOptions {
35
+ /** Request headers the action sees through `headers()`. */
36
+ headers?: HeadersInit;
37
+ /** Cookies the request carried; the action reads them with `cookies()`. */
38
+ cookies?: Record<string, string>;
39
+ /** The page URL path the action was submitted from. */
40
+ url?: string;
41
+ /** The action id; only visible through `getActionContext()`. */
42
+ actionId?: string;
43
+ }
44
+ export interface SetCookieCall {
45
+ name: string;
46
+ /** Empty string for a delete. */
47
+ value: string;
48
+ options?: Record<string, unknown>;
49
+ deleted: boolean;
50
+ }
51
+ export interface ActionCallResult<T> {
52
+ /** What the action returned. A thrown error (including `redirect()`) rejects instead. */
53
+ result: T;
54
+ /** Every `cookies().set()` / `delete()` call, in order. */
55
+ setCookies: SetCookieCall[];
56
+ }
57
+ /**
58
+ * Runs a server action function with the request context a real call has, so
59
+ * `cookies()`, `headers()` and `getActionContext()` from `@krate/runtime/server`
60
+ * work. The action is the plain exported function you imported from the module.
61
+ */
62
+ export declare function runAction<T>(action: () => T | Promise<T>, options?: ActionCallOptions): Promise<ActionCallResult<T>>;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Test helpers, exported from `@krate/runtime/testing`. They call an API route
3
+ * module or a server action in-process, without a running server. Node only
4
+ * (it uses AsyncLocalStorage); import it from your test files, never from app
5
+ * code.
6
+ */
7
+ import { AsyncLocalStorage } from "node:async_hooks";
8
+ import { attachCookies } from "./request-extras.js";
9
+ const METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
10
+ /**
11
+ * Calls a route module the way the server does: the named export for `method`
12
+ * (HEAD falls back to GET without a body, OPTIONS answers `Allow`), else a
13
+ * default Web-style handler, else a 405 with `Allow`. `target` is a path
14
+ * (`"/api/users/7?x=1"`), an absolute URL, or a ready `Request`.
15
+ */
16
+ export async function callRoute(mod, method, target, options = {}) {
17
+ // The server hands routes a Request with request.cookies and request.nextUrl.
18
+ const request = attachCookies(toRequest(method, target, options), options.basePath ?? "");
19
+ const context = { params: options.params ?? {} };
20
+ const named = (m) => (typeof mod[m] === "function" ? mod[m] : undefined);
21
+ const fallback = typeof mod.default === "function" ? mod.default : undefined;
22
+ if (fallback && fallback.length >= 2) {
23
+ throw new TypeError("callRoute: a default export with a (req, res) signature is the legacy Node style and is not supported; export named GET/POST handlers instead");
24
+ }
25
+ const direct = named(method);
26
+ if (direct)
27
+ return direct(request, context);
28
+ if (method === "HEAD" && named("GET")) {
29
+ const res = await named("GET")(request, context);
30
+ return new Response(null, { status: res.status, statusText: res.statusText, headers: res.headers });
31
+ }
32
+ if (fallback && !METHODS.some((m) => named(m)))
33
+ return (await fallback(request, context));
34
+ const allow = METHODS.filter((m) => named(m) || (m === "HEAD" && named("GET")));
35
+ if (!allow.includes("OPTIONS"))
36
+ allow.push("OPTIONS");
37
+ if (method === "OPTIONS")
38
+ return new Response(null, { status: 204, headers: { Allow: allow.join(", ") } });
39
+ return new Response("Method Not Allowed", { status: 405, headers: { Allow: allow.join(", ") } });
40
+ }
41
+ function toRequest(method, target, options) {
42
+ if (typeof target !== "string")
43
+ return target;
44
+ const url = /^https?:\/\//i.test(target) ? target : "http://localhost" + (target.startsWith("/") ? target : "/" + target);
45
+ const headers = new Headers(options.headers);
46
+ let body = options.body;
47
+ if (options.json !== undefined) {
48
+ body = JSON.stringify(options.json);
49
+ if (!headers.has("content-type"))
50
+ headers.set("content-type", "application/json");
51
+ }
52
+ const hasBody = method !== "GET" && method !== "HEAD" && body !== undefined && body !== null;
53
+ return new Request(url, { method, headers, body: hasBody ? body : undefined });
54
+ }
55
+ const ACTION_STORE = Symbol.for("krate.action.store");
56
+ const als = new AsyncLocalStorage();
57
+ /**
58
+ * Runs a server action function with the request context a real call has, so
59
+ * `cookies()`, `headers()` and `getActionContext()` from `@krate/runtime/server`
60
+ * work. The action is the plain exported function you imported from the module.
61
+ */
62
+ export async function runAction(action, options = {}) {
63
+ const jar = new Map(Object.entries(options.cookies ?? {}));
64
+ const setCookies = [];
65
+ const cookies = {
66
+ get: (name) => jar.get(name),
67
+ getAll: () => Array.from(jar, ([name, value]) => ({ name, value })),
68
+ has: (name) => jar.has(name),
69
+ set: (name, value, opts) => {
70
+ setCookies.push({ name, value: String(value), options: opts, deleted: false });
71
+ jar.set(name, String(value));
72
+ },
73
+ delete: (name, opts) => {
74
+ setCookies.push({ name, value: "", options: opts, deleted: true });
75
+ jar.delete(name);
76
+ },
77
+ };
78
+ const context = {
79
+ actionId: options.actionId ?? "test-action",
80
+ url: options.url ?? "/",
81
+ method: "POST",
82
+ headers: new Headers(options.headers),
83
+ cookies,
84
+ };
85
+ // Published once and left in place: concurrent runAction calls share it, and
86
+ // each call's context lives in its own async scope.
87
+ globalThis[ACTION_STORE] = als;
88
+ const result = await als.run(context, async () => action());
89
+ return { result, setCookies };
90
+ }
package/dist/use-id.js CHANGED
@@ -11,7 +11,7 @@ const encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
11
11
  */
12
12
  function idSafe(s) {
13
13
  return s.replace(/[^A-Za-z0-9_.:$-]/gu, (c) => {
14
- const bytes = encoder ? Array.from(encoder.encode(c)) : [c.charCodeAt(0) & 0xff];
14
+ const bytes = encoder ? Array.from(encoder.encode(c)) : [c.charCodeAt(0) & 0xFF];
15
15
  return bytes.map((b) => '%' + b.toString(16).toUpperCase().padStart(2, '0')).join('');
16
16
  });
17
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krate/runtime",
3
- "version": "0.5.0-rc.1",
3
+ "version": "0.5.0-rc.10",
4
4
  "description": "Client-side runtime for Krate - signals, hydration, SPA router, resources, and context.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -38,6 +38,18 @@
38
38
  "types": "./dist/highlight.d.ts",
39
39
  "default": "./dist/highlight.js"
40
40
  },
41
+ "./hints": {
42
+ "types": "./dist/hints.d.ts",
43
+ "default": "./dist/hints.js"
44
+ },
45
+ "./react-compat": {
46
+ "types": "./dist/react-compat.d.ts",
47
+ "default": "./dist/react-compat.js"
48
+ },
49
+ "./navigation-react": {
50
+ "types": "./dist/navigation-react.d.ts",
51
+ "default": "./dist/navigation-react.js"
52
+ },
41
53
  "./cache": {
42
54
  "types": "./dist/cache.d.ts",
43
55
  "default": "./dist/cache.js"
@@ -62,6 +74,10 @@
62
74
  "types": "./dist/react.d.ts",
63
75
  "default": "./dist/react.js"
64
76
  },
77
+ "./react-server": {
78
+ "types": "./dist/react-server.d.ts",
79
+ "default": "./dist/react-server.js"
80
+ },
65
81
  "./react/jsx-runtime": {
66
82
  "types": "./dist/react-jsx-runtime.d.ts",
67
83
  "default": "./dist/react-jsx-runtime.js"
@@ -78,6 +94,10 @@
78
94
  "types": "./dist/node-server.d.ts",
79
95
  "default": "./dist/node-server.js"
80
96
  },
97
+ "./cloudflare-workers": {
98
+ "types": "./dist/cloudflare-workers.d.ts",
99
+ "default": "./dist/cloudflare-workers.js"
100
+ },
81
101
  "./assets-static": {
82
102
  "types": "./dist/assets-static.d.ts",
83
103
  "default": "./dist/assets-static.js"
@@ -93,10 +113,15 @@
93
113
  "./og": {
94
114
  "types": "./dist/og.d.ts",
95
115
  "default": "./dist/og.js"
116
+ },
117
+ "./testing": {
118
+ "types": "./dist/testing.d.ts",
119
+ "default": "./dist/testing.js"
96
120
  }
97
121
  },
98
122
  "files": [
99
- "dist"
123
+ "dist",
124
+ "!dist/**/*.test.*"
100
125
  ],
101
126
  "devDependencies": {
102
127
  "@types/node": "^26.2.0",
@@ -109,6 +134,6 @@
109
134
  "build": "tsc && node bundle.cjs",
110
135
  "dev": "tsc --watch",
111
136
  "test": "vitest run",
112
- "typecheck": "tsc --noEmit"
137
+ "typecheck": "node ../../node_modules/typescript7/bin/tsc --noEmit"
113
138
  }
114
139
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,72 +0,0 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
- import { afterEach, describe, expect, it } from 'vitest';
3
- import { cookies, getActionContext, headers, redirect } from './action-context.js';
4
- import { installRenderStore, withCacheScope } from './cache-scope.js';
5
- import { jsx, renderToString } from './server.js';
6
- const KEY = Symbol.for('krate.action.store');
7
- const REQUEST_KEY = Symbol.for('krate.request.store');
8
- afterEach(() => {
9
- delete globalThis[KEY];
10
- delete globalThis[REQUEST_KEY];
11
- });
12
- describe('server action context', () => {
13
- it('throws outside an action with an actionable message', () => {
14
- expect(getActionContext()).toBeUndefined();
15
- expect(() => headers()).toThrow(/only be called while a server action runs/);
16
- });
17
- it('reads headers and cookies from the sidecar store', () => {
18
- const jar = new Map([['sid', 'abc']]);
19
- const ctx = {
20
- actionId: 'a1',
21
- url: '/__krate/action/a1',
22
- method: 'POST',
23
- headers: new Headers({ 'x-test': 'yes' }),
24
- cookies: {
25
- get: (n) => jar.get(n),
26
- getAll: () => [],
27
- has: (n) => jar.has(n),
28
- set: () => { },
29
- delete: () => { },
30
- },
31
- };
32
- globalThis[KEY] = { getStore: () => ctx };
33
- expect(headers().get('x-test')).toBe('yes');
34
- expect(cookies().get('sid')).toEqual({ name: 'sid', value: 'abc' });
35
- expect(cookies().get('sid')?.value).toBe('abc');
36
- expect(cookies().get('missing')).toBeUndefined();
37
- expect(getActionContext()?.cookies.get('sid')).toEqual({ name: 'sid', value: 'abc' });
38
- expect(getActionContext()?.actionId).toBe('a1');
39
- });
40
- it('marks a render uncacheable when headers or cookies are read', async () => {
41
- const ctx = {
42
- url: '/p',
43
- method: 'GET',
44
- headers: new Headers({ 'x-test': 'yes' }),
45
- cookies: new Map([['sid', 'abc']]),
46
- params: {},
47
- searchParams: {},
48
- };
49
- globalThis[REQUEST_KEY] = { getStore: () => ctx };
50
- installRenderStore(new AsyncLocalStorage());
51
- const { scope } = await withCacheScope('/p', () => {
52
- headers();
53
- cookies();
54
- return 'ok';
55
- });
56
- expect(scope.noStore).toBe(true);
57
- });
58
- it('redirect() throws the marker the sidecar turns into a navigation', () => {
59
- let caught;
60
- try {
61
- redirect('/done');
62
- }
63
- catch (e) {
64
- caught = e;
65
- }
66
- expect(caught.__krateRedirect).toBe('/done');
67
- });
68
- it('does not render client function actions as attributes', () => {
69
- const html = renderToString(jsx('form', { action: async () => { }, children: 'x' }));
70
- expect(html).toBe('<form>x</form>');
71
- });
72
- });
@@ -1 +0,0 @@
1
- export {};