@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/list.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type KrateAdoptable } from './dom.js';
2
- type KeyFn = ((item: unknown, index: number) => unknown) | null;
3
- type RenderFn = (item: unknown, index: unknown) => unknown;
2
+ export type KeyFn = ((item: unknown, index: number) => unknown) | null;
3
+ export type RenderFn = (item: unknown, index: unknown) => unknown;
4
4
  /**
5
5
  * Indices (into `seq`) of a longest strictly increasing subsequence of the
6
6
  * non-negative entries. O(n log n).
@@ -16,5 +16,15 @@ export declare function krateBindList(start: Comment, getItems: () => unknown, k
16
16
  * mounts as a fragment with its own markers, or adopts a server-rendered list
17
17
  * at the hydration cursor.
18
18
  */
19
- export declare function krateList(getItems: () => unknown, keyFn: KeyFn, render: RenderFn, mode: number): KrateAdoptable;
20
- export {};
19
+ /**
20
+ * The value krateList returns. Besides mounting itself as DOM it carries its
21
+ * arguments, so a custom renderer (a renderer component's children) can
22
+ * reconcile the list itself.
23
+ */
24
+ export interface KrateListValue extends KrateAdoptable {
25
+ items: () => unknown;
26
+ key: KeyFn;
27
+ render: RenderFn;
28
+ mode: number;
29
+ }
30
+ export declare function krateList(getItems: () => unknown, keyFn: KeyFn, render: RenderFn, mode: number): KrateListValue;
package/dist/list.js CHANGED
@@ -436,14 +436,13 @@ export function krateBindList(start, getItems, keyFn, render, mode) {
436
436
  }
437
437
  run(newState(start, end, keyFn, render, mode), getItems, true);
438
438
  }
439
- /**
440
- * A list nested in rendered JSX (a `.map()` inside a row or component). It
441
- * mounts as a fragment with its own markers, or adopts a server-rendered list
442
- * at the hydration cursor.
443
- */
444
439
  export function krateList(getItems, keyFn, render, mode) {
445
440
  return {
446
441
  __ka: 1,
442
+ items: getItems,
443
+ key: keyFn,
444
+ render,
445
+ mode,
447
446
  mount() {
448
447
  const frag = document.createDocumentFragment();
449
448
  const start = document.createComment('');
package/dist/metadata.js CHANGED
@@ -131,10 +131,10 @@ function fillParams(files, params) {
131
131
  function mergeFiles(acc, files, own) {
132
132
  const out = { ...acc, metadata: { ...acc.metadata } };
133
133
  if (files.openGraph && files.openGraph.images !== undefined && !(own && own.openGraph && own.openGraph.images !== undefined)) {
134
- out.metadata.openGraph = { ...(out.metadata.openGraph || {}), images: files.openGraph.images };
134
+ out.metadata.openGraph = { ...out.metadata.openGraph, images: files.openGraph.images };
135
135
  }
136
136
  if (files.twitter && files.twitter.images !== undefined && !(own && own.twitter && own.twitter.images !== undefined)) {
137
- out.metadata.twitter = { card: 'summary_large_image', ...(out.metadata.twitter || {}), images: files.twitter.images };
137
+ out.metadata.twitter = { card: 'summary_large_image', ...out.metadata.twitter, images: files.twitter.images };
138
138
  }
139
139
  if (files.icons !== undefined && !(own && own.icons !== undefined))
140
140
  out.metadata.icons = files.icons;
@@ -170,6 +170,9 @@ function resolveBaseJoin(value, base) {
170
170
  // metadataBase's path is a prefix: "/docs" + "/a" = "/docs/a".
171
171
  const prefix = b.pathname.replace(/\/$/, '');
172
172
  const path = s.startsWith('/') ? s : '/' + s;
173
+ // A convention image URL already carries the site base path.
174
+ if (prefix && (path === prefix || path.startsWith(prefix + '/')))
175
+ return b.origin + path;
173
176
  return b.origin + prefix + path;
174
177
  }
175
178
  catch {
@@ -0,0 +1,13 @@
1
+ import type { AppRouterInstance } from './navigation.js';
2
+ type ParamValue = string | string[];
3
+ type UseSyncExternalStore = <T>(subscribe: (cb: () => void) => () => void, getSnapshot: () => T, getServerSnapshot?: () => T) => T;
4
+ export interface ValueNavigationHooks {
5
+ usePathname(): string;
6
+ useSearchParams(): URLSearchParams;
7
+ useParams<T extends Record<string, ParamValue> = Record<string, ParamValue>>(): T;
8
+ useRouter(): AppRouterInstance;
9
+ useSelectedLayoutSegments(parallelRouteKey?: string, depth?: number): string[];
10
+ useSelectedLayoutSegment(parallelRouteKey?: string, depth?: number): string | null;
11
+ }
12
+ export declare function createValueNavigationHooks(useSyncExternalStore: UseSyncExternalStore): ValueNavigationHooks;
13
+ export {};
@@ -0,0 +1,86 @@
1
+ // Router hooks for React-model components: modules that run on Preact compat
2
+ // or on real React (a "use client" module under a server component, an island
3
+ // that uses an npm React library). They read hook results as plain values, so
4
+ // each hook returns a value and the component re-renders when the URL changes.
5
+ // Krate-compiled components use the getter-returning hooks of navigation.ts.
6
+ //
7
+ // The compiler's island builds replace the router hooks of `@krate/runtime`
8
+ // with createValueNavigationHooks(useSyncExternalStore) of their React.
9
+ import { getRequestContext } from './request-context.js';
10
+ import * as serverNav from './navigation-server.js';
11
+ import { krateRouter } from './router.js';
12
+ import { krateRouteStore, krateMatchParams, krateLayoutSegments } from './navigation.js';
13
+ const routerInstance = {
14
+ push: (href, options) => void krateRouter.push(href, options),
15
+ replace: (href, options) => void krateRouter.replace(href, options),
16
+ back: () => krateRouter.back(),
17
+ forward: () => krateRouter.forward(),
18
+ refresh: () => void krateRouter.refresh(),
19
+ prefetch: (href) => void krateRouter.prefetch(href),
20
+ };
21
+ export function createValueNavigationHooks(useSyncExternalStore) {
22
+ const store = krateRouteStore();
23
+ const route = () => useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
24
+ // Values derived from the snapshot are cached so they keep their identity
25
+ // between renders of the same URL, as React's hooks do.
26
+ let paramsKey = '';
27
+ let paramsVal = new URLSearchParams();
28
+ let routeKey = '';
29
+ let routeVal = {};
30
+ let segKey = '';
31
+ let segVal = [];
32
+ const segments = (depth) => {
33
+ const s = route();
34
+ const key = s.pathname + '\0' + (depth ?? '');
35
+ if (key !== segKey) {
36
+ segKey = key;
37
+ segVal = krateLayoutSegments(s.pathname, depth);
38
+ }
39
+ return segVal;
40
+ };
41
+ return {
42
+ usePathname() {
43
+ if (getRequestContext())
44
+ return serverNav.usePathname();
45
+ return route().pathname;
46
+ },
47
+ useSearchParams() {
48
+ if (getRequestContext())
49
+ return serverNav.useSearchParams();
50
+ const s = route();
51
+ if (s.search !== paramsKey) {
52
+ paramsKey = s.search;
53
+ paramsVal = new URLSearchParams(s.search);
54
+ }
55
+ return paramsVal;
56
+ },
57
+ useParams() {
58
+ if (getRequestContext())
59
+ return serverNav.useParams();
60
+ const s = route();
61
+ const key = s.route + '\0' + s.pathname;
62
+ if (key !== routeKey) {
63
+ routeKey = key;
64
+ routeVal = krateMatchParams(s.route, s.pathname);
65
+ }
66
+ return routeVal;
67
+ },
68
+ useRouter() {
69
+ if (getRequestContext())
70
+ return serverNav.useRouter();
71
+ return routerInstance;
72
+ },
73
+ useSelectedLayoutSegments(parallelRouteKey, depth) {
74
+ if (getRequestContext())
75
+ return serverNav.useSelectedLayoutSegments(parallelRouteKey, depth);
76
+ return segments(depth);
77
+ },
78
+ useSelectedLayoutSegment(parallelRouteKey, depth) {
79
+ if (getRequestContext())
80
+ return serverNav.useSelectedLayoutSegment(parallelRouteKey, depth);
81
+ const at = typeof depth === 'number' ? depth : 0;
82
+ const parts = krateLayoutSegments(route().pathname);
83
+ return parts.length > at ? parts[at] : null;
84
+ },
85
+ };
86
+ }
@@ -1,3 +1,22 @@
1
+ interface RouteSnapshot {
2
+ pathname: string;
3
+ search: string;
4
+ /** The route pattern of the current document ("/blog/[slug]"), "" for a static route. */
5
+ route: string;
6
+ }
7
+ /**
8
+ * An external store of the route for React-model components (Preact compat and
9
+ * real React islands), which read hook results as plain values and re-render
10
+ * when the URL changes. The snapshot object only changes with the route.
11
+ */
12
+ export declare function krateRouteStore(): {
13
+ subscribe: (cb: () => void) => () => void;
14
+ getSnapshot: () => RouteSnapshot;
15
+ };
16
+ /** The params a route pattern matches in a pathname (see useParams). */
17
+ export declare function krateMatchParams(route: string, pathname: string): Record<string, string | string[]>;
18
+ /** The path segments below depth (see useSelectedLayoutSegments). */
19
+ export declare function krateLayoutSegments(pathname: string, depth?: number): string[];
1
20
  /** Test hook: forgets the shared snapshot so the next hook call reads the URL again. */
2
21
  export declare function krateResetNavigation(): void;
3
22
  /** The current URL path without the base path and the query ("/blog/hello"). */
@@ -58,6 +58,41 @@ function routeState() {
58
58
  }
59
59
  return read;
60
60
  }
61
+ /**
62
+ * An external store of the route for React-model components (Preact compat and
63
+ * real React islands), which read hook results as plain values and re-render
64
+ * when the URL changes. The snapshot object only changes with the route.
65
+ */
66
+ export function krateRouteStore() {
67
+ let last = null;
68
+ const getSnapshot = () => {
69
+ const next = snapshot();
70
+ if (last && last.pathname === next.pathname && last.search === next.search && last.route === next.route)
71
+ return last;
72
+ last = next;
73
+ return next;
74
+ };
75
+ const subscribe = (cb) => {
76
+ const off = subscribeNavigation(cb);
77
+ // The router swaps the document before it emits the final status.
78
+ if (typeof window !== 'undefined')
79
+ window.addEventListener('krate:navigate-end', cb);
80
+ return () => {
81
+ off();
82
+ if (typeof window !== 'undefined')
83
+ window.removeEventListener('krate:navigate-end', cb);
84
+ };
85
+ };
86
+ return { subscribe, getSnapshot };
87
+ }
88
+ /** The params a route pattern matches in a pathname (see useParams). */
89
+ export function krateMatchParams(route, pathname) {
90
+ return matchRouteParams(route, pathname);
91
+ }
92
+ /** The path segments below depth (see useSelectedLayoutSegments). */
93
+ export function krateLayoutSegments(pathname, depth) {
94
+ return pathname.split('/').filter(Boolean).map(safeDecode).slice(typeof depth === 'number' ? depth : 0);
95
+ }
61
96
  /** Test hook: forgets the shared snapshot so the next hook call reads the URL again. */
62
97
  export function krateResetNavigation() {
63
98
  state = null;
@@ -71,7 +71,7 @@ function acceptsEncoding(header, coding) {
71
71
  let token = part;
72
72
  let q = 1;
73
73
  const semi = part.indexOf(";");
74
- if (semi >= 0) {
74
+ if (semi !== -1) {
75
75
  token = part.slice(0, semi).trim();
76
76
  for (const param of part.slice(semi + 1).split(";")) {
77
77
  const t = param.trim();
@@ -198,7 +198,7 @@ export function createNodeStaticLayer(distDir) {
198
198
  return null;
199
199
  }
200
200
  const ext = path.extname(target).toLowerCase();
201
- let contentType = MIME[ext] || "application/octet-stream";
201
+ const contentType = MIME[ext] || "application/octet-stream";
202
202
  const headers = new Headers();
203
203
  const ae = request.headers.get("accept-encoding");
204
204
  let encoding = "";
@@ -235,7 +235,7 @@ export function createNodeStaticLayer(distDir) {
235
235
  let start = 0;
236
236
  let end = size - 1;
237
237
  const range = request.headers.get("range");
238
- if (range && /^bytes=/.test(range) && !range.includes(",")) {
238
+ if (range && range.startsWith('bytes=') && !range.includes(",")) {
239
239
  const m = /^bytes=(\d*)-(\d*)$/.exec(range.trim());
240
240
  if (m && (m[1] !== "" || m[2] !== "")) {
241
241
  if (m[1] === "") {
package/dist/og.js CHANGED
@@ -35,7 +35,7 @@ export function serializeElement(node, path = 'root') {
35
35
  const el = node;
36
36
  if (typeof el.__raw === 'string')
37
37
  throw new TypeError(`ImageResponse: ${path} is raw HTML, which is not supported`);
38
- const props = { ...(el.props || {}) };
38
+ const props = { ...el.props };
39
39
  const children = el.children || [];
40
40
  if (typeof el.type === 'function') {
41
41
  const out = el.type({ ...props, children: children.length === 1 ? children[0] : children });
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Platform access for server code, exported from `@krate/runtime/server`.
3
+ *
4
+ * The portable server handler runs every request inside an AsyncLocalStorage
5
+ * scope published on `globalThis[Symbol.for('krate.platform.store')]` (the
6
+ * same pattern as the request store), so pages, API routes, server actions
7
+ * and middleware all see the same `{ env, ctx, cf }` of the request in flight.
8
+ * On the cloudflare target `env` holds the Worker bindings (D1, KV, R2,
9
+ * Durable Objects, secrets); it is never copied into `process.env`.
10
+ */
11
+ /** The deploy target the handler runs on, or "none" outside a handler (dev, tests). */
12
+ export type PlatformTarget = 'cloudflare' | 'vercel' | 'netlify' | 'node' | 'none';
13
+ /** Keeps the instance alive for work that outlives the response. */
14
+ export interface PlatformExecutionContext {
15
+ waitUntil(work: Promise<unknown>): void;
16
+ /** Cloudflare only: let the request fall through to the origin when the Worker throws. */
17
+ passThroughOnException?(): void;
18
+ }
19
+ /**
20
+ * Augment this interface with your bindings to type `env`:
21
+ *
22
+ * declare module '@krate/runtime/server' {
23
+ * interface KrateCloudflareEnv { DB: D1Database; CACHE: KVNamespace }
24
+ * }
25
+ */
26
+ export interface KrateCloudflareEnv {
27
+ }
28
+ export interface PlatformContext<Env = Record<string, unknown>> {
29
+ target: PlatformTarget;
30
+ /** The Worker `env` on cloudflare; an empty object elsewhere. */
31
+ env: Env;
32
+ ctx: PlatformExecutionContext;
33
+ /** Cloudflare's `request.cf` properties (colo, country, ...), when present. */
34
+ cf: Record<string, unknown> | undefined;
35
+ }
36
+ export interface CloudflareContext<Env = KrateCloudflareEnv & Record<string, any>> extends PlatformContext<Env> {
37
+ target: 'cloudflare';
38
+ }
39
+ /**
40
+ * The platform of the request being served. Outside a request handler (`krate
41
+ * dev`, tests) it returns an empty environment and a `waitUntil` that just
42
+ * detaches the promise, so code that only uses it opportunistically keeps working.
43
+ */
44
+ export declare function getPlatform<Env = Record<string, unknown>>(): PlatformContext<Env>;
45
+ /**
46
+ * The Cloudflare `{ env, ctx, cf }` of the request being served. Throws
47
+ * outside a Worker request, so a missing binding is an error at the call
48
+ * site rather than an `undefined` later.
49
+ */
50
+ export declare function getCloudflareContext<Env = KrateCloudflareEnv & Record<string, any>>(): CloudflareContext<Env>;
51
+ /** Runs work after the response is sent, through the platform's waitUntil when there is one. */
52
+ export declare function platformWaitUntil(work: Promise<unknown>): boolean;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Platform access for server code, exported from `@krate/runtime/server`.
3
+ *
4
+ * The portable server handler runs every request inside an AsyncLocalStorage
5
+ * scope published on `globalThis[Symbol.for('krate.platform.store')]` (the
6
+ * same pattern as the request store), so pages, API routes, server actions
7
+ * and middleware all see the same `{ env, ctx, cf }` of the request in flight.
8
+ * On the cloudflare target `env` holds the Worker bindings (D1, KV, R2,
9
+ * Durable Objects, secrets); it is never copied into `process.env`.
10
+ */
11
+ const PLATFORM_STORE = Symbol.for('krate.platform.store');
12
+ function current() {
13
+ const store = globalThis[PLATFORM_STORE];
14
+ return store && typeof store.getStore === 'function' ? store.getStore() : undefined;
15
+ }
16
+ /**
17
+ * The platform of the request being served. Outside a request handler (`krate
18
+ * dev`, tests) it returns an empty environment and a `waitUntil` that just
19
+ * detaches the promise, so code that only uses it opportunistically keeps working.
20
+ */
21
+ export function getPlatform() {
22
+ const c = current();
23
+ if (c)
24
+ return c;
25
+ return {
26
+ target: 'none',
27
+ env: {},
28
+ ctx: {
29
+ waitUntil(work) {
30
+ Promise.resolve(work).catch((err) => console.error(`[krate] waitUntil task failed: ${(err && err.stack) || err}`));
31
+ },
32
+ },
33
+ cf: undefined,
34
+ };
35
+ }
36
+ /**
37
+ * The Cloudflare `{ env, ctx, cf }` of the request being served. Throws
38
+ * outside a Worker request, so a missing binding is an error at the call
39
+ * site rather than an `undefined` later.
40
+ */
41
+ export function getCloudflareContext() {
42
+ const c = current();
43
+ if (!c || c.target !== 'cloudflare') {
44
+ throw new Error('getCloudflareContext() works only while a request is served by the cloudflare target (krate deploy --target cloudflare); ' +
45
+ (c ? `this request is served by "${c.target}"` : 'there is no request in flight here (krate dev and krate serve run on Node and have no Worker bindings)') +
46
+ '. Use getPlatform() for code that also has to run elsewhere.');
47
+ }
48
+ return c;
49
+ }
50
+ /** Runs work after the response is sent, through the platform's waitUntil when there is one. */
51
+ export function platformWaitUntil(work) {
52
+ const c = current();
53
+ if (!c || c.target === 'none' || !c.ctx || typeof c.ctx.waitUntil !== 'function')
54
+ return false;
55
+ c.ctx.waitUntil(work);
56
+ return true;
57
+ }
@@ -0,0 +1,39 @@
1
+ type AnyFn = (...args: any[]) => any;
2
+ export interface FormStatus {
3
+ pending: boolean;
4
+ data: FormData | null;
5
+ method: string | null;
6
+ action: unknown;
7
+ }
8
+ export interface ReactCompatExtras {
9
+ useOptimistic: AnyFn;
10
+ useActionState: AnyFn;
11
+ useFormState: AnyFn;
12
+ useFormStatus: () => FormStatus;
13
+ requestFormReset: (form: HTMLFormElement) => void;
14
+ use: AnyFn;
15
+ cache: AnyFn;
16
+ useEffectEvent: AnyFn;
17
+ startTransition: AnyFn;
18
+ useTransition: AnyFn;
19
+ }
20
+ /**
21
+ * Builds the additions once per Preact instance: the `react` and `react-dom`
22
+ * modules of a bundle (and of every island chunk) share one action scope and
23
+ * one form-status context.
24
+ */
25
+ export declare function createReactCompatExtras(c: any, options: any): ReactCompatExtras;
26
+ /**
27
+ * React 18 flushes the passive effects of a render caused by a discrete event
28
+ * (a click, a key press, typing) synchronously at the end of its commit, and
29
+ * renders the updates those effects make in the same task. Preact runs them
30
+ * after the next frame, so a quick second event could render a component with
31
+ * a new prop and a state update from its previous effects at once: react-colorful
32
+ * then took its own color prop for user input and called onChange with a
33
+ * stale colour. While a discrete event's task runs, this flushes Preact's
34
+ * effect queue after every commit (Preact's render loop picks up the updates
35
+ * they make); otherwise effects keep running after paint. The compiler's
36
+ * client builds call it from their `react` module, once per Preact instance.
37
+ */
38
+ export declare function installDiscreteEffects(options: any): void;
39
+ export {};