@krate/runtime 0.5.0-alpha.3 → 0.5.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/action-context.d.ts +14 -3
- package/dist/action-context.js +70 -4
- package/dist/action-context.test.js +26 -1
- package/dist/actions.d.ts +12 -1
- package/dist/actions.js +101 -5
- package/dist/actions.test.js +87 -1
- package/dist/cache-scope.d.ts +118 -0
- package/dist/cache-scope.js +314 -0
- package/dist/cache.d.ts +55 -0
- package/dist/cache.js +155 -0
- package/dist/cache.test.d.ts +1 -0
- package/dist/cache.test.js +233 -0
- package/dist/class-helpers.d.ts +4 -4
- package/dist/class-helpers.js +357 -32
- package/dist/class-helpers.test.d.ts +1 -0
- package/dist/class-helpers.test.js +169 -0
- package/dist/context.d.ts +16 -2
- package/dist/context.js +44 -3
- package/dist/dev-overlay.js +75 -2
- package/dist/dev-overlay.test.js +22 -0
- package/dist/dom.d.ts +7 -0
- package/dist/dom.js +69 -3
- package/dist/effects.js +74 -3
- package/dist/error-overlay.js +4 -4
- package/dist/hook-args.d.ts +7 -0
- package/dist/hook-args.js +10 -0
- package/dist/htmlspec-attributes.d.ts +1 -0
- package/dist/htmlspec-attributes.js +159 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/krate-core.js +2 -0
- package/dist/krate-dev.js +65 -2
- package/dist/krate-f-actions.js +2 -0
- package/dist/krate-f-classes.js +8 -0
- package/dist/krate-f-helpers.js +2 -0
- package/dist/krate-f-list.js +2 -0
- package/dist/krate-f-router-lite.js +2 -0
- package/dist/krate-f-router.js +2 -0
- package/dist/krate-features.json +239 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-runtime.js +5 -5
- package/dist/patterns.test.d.ts +1 -0
- package/dist/patterns.test.js +98 -0
- package/dist/portal.d.ts +5 -1
- package/dist/portal.js +15 -4
- package/dist/reactivity-core.test.d.ts +1 -0
- package/dist/reactivity-core.test.js +152 -0
- package/dist/reconcile.js +4 -2
- package/dist/request-context.d.ts +62 -0
- package/dist/request-context.js +107 -0
- package/dist/router-layout.test.d.ts +1 -0
- package/dist/router-layout.test.js +179 -0
- package/dist/router-lite.d.ts +25 -0
- package/dist/router-lite.js +73 -0
- package/dist/router-nav.test.d.ts +1 -0
- package/dist/router-nav.test.js +437 -0
- package/dist/router-slots.test.d.ts +1 -0
- package/dist/router-slots.test.js +144 -0
- package/dist/router-speculation.test.d.ts +1 -0
- package/dist/router-speculation.test.js +25 -0
- package/dist/router-styles.test.d.ts +1 -0
- package/dist/router-styles.test.js +98 -0
- package/dist/router.d.ts +66 -2
- package/dist/router.js +1617 -648
- package/dist/server-async.test.d.ts +1 -0
- package/dist/server-async.test.js +194 -0
- package/dist/server-renderer.js +556 -132
- package/dist/server.d.ts +24 -0
- package/dist/server.js +247 -11
- package/dist/server.test.js +20 -0
- package/dist/signal.d.ts +41 -4
- package/dist/signal.js +254 -24
- package/dist/slots.d.ts +5 -1
- package/dist/slots.js +40 -3
- package/package.json +6 -2
package/dist/server.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export { getActionContext, headers, cookies, redirect } from './action-context.js';
|
|
2
2
|
export type { ActionContext, ActionCookies, ActionCookieOptions } from './action-context.js';
|
|
3
|
+
export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
4
|
+
export type { RevalidatePathType, UnstableCacheOptions } from './cache.js';
|
|
5
|
+
export { getRequestContext, notFound } from './request-context.js';
|
|
6
|
+
export type { RequestContext, RequestCookies } from './request-context.js';
|
|
3
7
|
export declare function createSignal<T>(initial: T): [() => T, (v: T | ((prev: T) => T)) => void];
|
|
4
8
|
export declare function createEffect(_fn: () => void): () => void;
|
|
5
9
|
export declare function createMemo<T>(fn: () => T): () => T;
|
|
6
10
|
export declare function krateDerived<T>(fn: () => T, _triggers?: () => unknown): () => T;
|
|
7
11
|
export declare function krateHook<T>(value: T | (() => T)): () => T;
|
|
12
|
+
export declare function krateParam<T>(value: T | (() => T)): T;
|
|
8
13
|
export declare function onCleanup(_fn: () => void): void;
|
|
9
14
|
export declare function onMount(_fn: () => void): void;
|
|
10
15
|
interface RawHTML {
|
|
@@ -23,6 +28,16 @@ export declare function Fragment(_props: {
|
|
|
23
28
|
export declare function jsx(type: string | Function, props: Record<string, any>, _key?: string): JSXElement;
|
|
24
29
|
export declare const jsxs: typeof jsx;
|
|
25
30
|
export declare function renderToString(node: JSXNode): string;
|
|
31
|
+
/**
|
|
32
|
+
* renderToStringAsync renders a tree that may contain async (server)
|
|
33
|
+
* components. Every Promise-returning component is awaited, at any depth, and
|
|
34
|
+
* sibling subtrees resolve in parallel. Errors thrown by a component (or a
|
|
35
|
+
* rejected Promise) reject the returned Promise; nothing renders as "".
|
|
36
|
+
*
|
|
37
|
+
* <Suspense> renders its resolved children: the whole tree is awaited before
|
|
38
|
+
* any HTML is produced, so there is no fallback to show.
|
|
39
|
+
*/
|
|
40
|
+
export declare function renderToStringAsync(node: unknown): Promise<string>;
|
|
26
41
|
export declare function setStreamingResolved(v: boolean): void;
|
|
27
42
|
export declare function resetBoundaryCounter(): void;
|
|
28
43
|
interface SuspenseProps {
|
|
@@ -30,6 +45,15 @@ interface SuspenseProps {
|
|
|
30
45
|
children: JSXNode;
|
|
31
46
|
}
|
|
32
47
|
export declare function Suspense(props: SuspenseProps): JSXNode;
|
|
48
|
+
type ClientRender = (component: unknown, props: Record<string, unknown>, childrenHTML: string | null) => string;
|
|
49
|
+
/**
|
|
50
|
+
* clientBoundary wraps an export of a `"use client"` module for the server
|
|
51
|
+
* bundle of a page that renders server components. The server renders the
|
|
52
|
+
* component's initial HTML (render) into a <krate-island> marker carrying its
|
|
53
|
+
* serialized props; the per-module client chunk at src hydrates it in the
|
|
54
|
+
* browser. Children are rendered on the server and passed as HTML.
|
|
55
|
+
*/
|
|
56
|
+
export declare function clientBoundary(moduleId: string, exportName: string, src: string, impl: unknown, render: ClientRender): unknown;
|
|
33
57
|
export declare function Head(props: {
|
|
34
58
|
children?: JSXNode;
|
|
35
59
|
}): JSXNode;
|
package/dist/server.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// @krate/runtime/server - Server-side rendering runtime
|
|
2
2
|
// Provides the same API as the client runtime but renders to HTML strings.
|
|
3
|
+
import { ATTR_ALIASES } from './htmlspec-attributes.js';
|
|
3
4
|
export { getActionContext, headers, cookies, redirect } from './action-context.js';
|
|
5
|
+
export { revalidatePath, revalidateTag, unstable_cache, unstable_noStore } from './cache.js';
|
|
6
|
+
export { getRequestContext, notFound } from './request-context.js';
|
|
4
7
|
// Signal (server: returns initial value, no reactivity)
|
|
5
8
|
export function createSignal(initial) {
|
|
6
9
|
let value = initial;
|
|
@@ -38,6 +41,10 @@ export function krateHook(value) {
|
|
|
38
41
|
return value;
|
|
39
42
|
return () => value;
|
|
40
43
|
}
|
|
44
|
+
// Reads a custom hook parameter that a call site passed as a derived getter.
|
|
45
|
+
export function krateParam(value) {
|
|
46
|
+
return typeof value === "function" && derivedGetters.has(value) ? value() : value;
|
|
47
|
+
}
|
|
41
48
|
export function onCleanup(_fn) {
|
|
42
49
|
// No-op on server
|
|
43
50
|
}
|
|
@@ -104,9 +111,38 @@ function escapeHTML(str) {
|
|
|
104
111
|
.replace(/"/g, """)
|
|
105
112
|
.replace(/'/g, "'");
|
|
106
113
|
}
|
|
114
|
+
function isThenable(v) {
|
|
115
|
+
return v != null && (typeof v === "object" || typeof v === "function") && typeof v.then === "function";
|
|
116
|
+
}
|
|
117
|
+
function componentName(fn) {
|
|
118
|
+
return fn.displayName || fn.name || "anonymous";
|
|
119
|
+
}
|
|
120
|
+
function asyncInSyncRender(where) {
|
|
121
|
+
return new Error(`${where} returned a Promise during a synchronous renderToString. Async server components ` +
|
|
122
|
+
`are rendered per request by the SSR sidecar through renderToStringAsync; a synchronous ` +
|
|
123
|
+
`render (for example inside goComponent children) cannot await them.`);
|
|
124
|
+
}
|
|
125
|
+
function isClassComponent(fn) {
|
|
126
|
+
return typeof fn === "function" && fn.prototype && typeof fn.prototype.render === "function";
|
|
127
|
+
}
|
|
128
|
+
function classFallback(type, instance, error) {
|
|
129
|
+
if (typeof type.getDerivedStateFromError !== "function")
|
|
130
|
+
throw error;
|
|
131
|
+
instance.state = { ...(instance.state || {}), ...(type.getDerivedStateFromError(error) || {}) };
|
|
132
|
+
try {
|
|
133
|
+
if (typeof instance.componentDidCatch === "function")
|
|
134
|
+
instance.componentDidCatch(error, { componentStack: "" });
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// A throwing componentDidCatch must not hide the original render error.
|
|
138
|
+
}
|
|
139
|
+
return instance.render();
|
|
140
|
+
}
|
|
107
141
|
function renderNode(node) {
|
|
108
142
|
if (node == null || node === false)
|
|
109
143
|
return "";
|
|
144
|
+
if (isThenable(node))
|
|
145
|
+
throw asyncInSyncRender("A child value");
|
|
110
146
|
if (typeof node === "string")
|
|
111
147
|
return escapeHTML(node);
|
|
112
148
|
if (typeof node === "number")
|
|
@@ -124,40 +160,149 @@ function renderNode(node) {
|
|
|
124
160
|
function isServerAction(v) {
|
|
125
161
|
return typeof v === "function" && typeof v.actionUrl === "string";
|
|
126
162
|
}
|
|
163
|
+
const UNITLESS = new Set([
|
|
164
|
+
"animation-iteration-count", "aspect-ratio", "border-image-outset", "border-image-slice",
|
|
165
|
+
"border-image-width", "box-flex", "box-flex-group", "box-ordinal-group", "column-count",
|
|
166
|
+
"columns", "flex", "flex-grow", "flex-positive", "flex-shrink", "flex-negative", "flex-order",
|
|
167
|
+
"grid-area", "grid-row", "grid-row-end", "grid-row-span", "grid-row-start", "grid-column",
|
|
168
|
+
"grid-column-end", "grid-column-span", "grid-column-start", "font-weight", "line-clamp",
|
|
169
|
+
"line-height", "opacity", "order", "orphans", "scale", "tab-size", "widows", "z-index", "zoom",
|
|
170
|
+
"fill-opacity", "flood-opacity", "stop-opacity", "stroke-dasharray", "stroke-dashoffset",
|
|
171
|
+
"stroke-miterlimit", "stroke-opacity", "stroke-width",
|
|
172
|
+
]);
|
|
173
|
+
const URL_ATTRS = new Set(["href", "src", "action", "formaction", "xlink:href"]);
|
|
174
|
+
// eslint-disable-next-line no-control-regex
|
|
175
|
+
const JS_URL = /^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*:/i;
|
|
176
|
+
const BLOCKED_URL = "javascript:throw new Error('React has blocked a javascript: URL as a security precaution.')";
|
|
177
|
+
function cssName(key) {
|
|
178
|
+
if (key.startsWith("--"))
|
|
179
|
+
return key;
|
|
180
|
+
const name = key.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase());
|
|
181
|
+
return name.startsWith("ms-") ? "-" + name : name;
|
|
182
|
+
}
|
|
183
|
+
function styleString(value) {
|
|
184
|
+
const out = [];
|
|
185
|
+
for (const key of Object.keys(value)) {
|
|
186
|
+
const v = value[key];
|
|
187
|
+
if (v == null || typeof v === "boolean" || v === "")
|
|
188
|
+
continue;
|
|
189
|
+
const prop = cssName(key);
|
|
190
|
+
const bare = prop.replace(/^-(webkit|moz|ms|o)-/, "");
|
|
191
|
+
const text = typeof v === "number" && v !== 0 && !prop.startsWith("--") && !UNITLESS.has(bare)
|
|
192
|
+
? v + "px"
|
|
193
|
+
: String(v);
|
|
194
|
+
out.push(prop + ":" + text);
|
|
195
|
+
}
|
|
196
|
+
return out.join(";");
|
|
197
|
+
}
|
|
127
198
|
function setAttr(key, val) {
|
|
199
|
+
if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML")
|
|
200
|
+
return "";
|
|
201
|
+
const name = ATTR_ALIASES[key] || key;
|
|
128
202
|
if (val == null || val === false)
|
|
129
203
|
return "";
|
|
130
204
|
if (val === true)
|
|
131
|
-
return ` ${
|
|
132
|
-
if (
|
|
205
|
+
return ` ${name}`;
|
|
206
|
+
if (name === "action" && isServerAction(val)) {
|
|
133
207
|
return ` action="${escapeHTML(val.actionUrl)}" data-krate-action`;
|
|
134
208
|
}
|
|
135
|
-
if (
|
|
209
|
+
if (name === "formaction" && isServerAction(val)) {
|
|
136
210
|
return ` formaction="${escapeHTML(val.actionUrl)}" formmethod="post" data-krate-action`;
|
|
137
211
|
}
|
|
138
212
|
// Client function actions run only after hydration; they have no HTML form.
|
|
139
213
|
if (typeof val === "function")
|
|
140
214
|
return "";
|
|
141
|
-
if (key === "class")
|
|
142
|
-
return ` class="${escapeHTML(String(val))}"`;
|
|
143
215
|
if (key === "style" && typeof val === "object") {
|
|
144
|
-
const css =
|
|
145
|
-
|
|
146
|
-
.join(";");
|
|
147
|
-
return ` style="${escapeHTML(css)}"`;
|
|
216
|
+
const css = styleString(val);
|
|
217
|
+
return css ? ` style="${escapeHTML(css)}"` : "";
|
|
148
218
|
}
|
|
149
219
|
if (key.startsWith("on") || key.startsWith("__"))
|
|
150
220
|
return "";
|
|
151
|
-
|
|
221
|
+
let text = String(val);
|
|
222
|
+
if (URL_ATTRS.has(name.toLowerCase()) && JS_URL.test(text))
|
|
223
|
+
text = BLOCKED_URL;
|
|
224
|
+
return ` ${name}="${escapeHTML(text)}"`;
|
|
152
225
|
}
|
|
153
226
|
export function renderToString(node) {
|
|
154
227
|
return renderNode(node);
|
|
155
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* renderToStringAsync renders a tree that may contain async (server)
|
|
231
|
+
* components. Every Promise-returning component is awaited, at any depth, and
|
|
232
|
+
* sibling subtrees resolve in parallel. Errors thrown by a component (or a
|
|
233
|
+
* rejected Promise) reject the returned Promise; nothing renders as "".
|
|
234
|
+
*
|
|
235
|
+
* <Suspense> renders its resolved children: the whole tree is awaited before
|
|
236
|
+
* any HTML is produced, so there is no fallback to show.
|
|
237
|
+
*/
|
|
238
|
+
export async function renderToStringAsync(node) {
|
|
239
|
+
const resolved = await resolveTree(node);
|
|
240
|
+
return renderNode(resolved);
|
|
241
|
+
}
|
|
242
|
+
function isElement(v) {
|
|
243
|
+
return v != null && typeof v === "object" && "type" in v && "props" in v && Array.isArray(v.children);
|
|
244
|
+
}
|
|
245
|
+
function isJSXValue(v) {
|
|
246
|
+
return isElement(v) || isThenable(v) || (Array.isArray(v) && v.some(isJSXValue));
|
|
247
|
+
}
|
|
248
|
+
// resolveTree expands function components top-down into a tree of host
|
|
249
|
+
// elements, raw HTML and primitives, awaiting async components. Built-ins that
|
|
250
|
+
// render their children eagerly (marked __krateEagerChildren, e.g. Link) get
|
|
251
|
+
// their children and JSX-valued props resolved first, so an async component
|
|
252
|
+
// passed as a child still renders.
|
|
253
|
+
async function resolveTree(node) {
|
|
254
|
+
if (isThenable(node))
|
|
255
|
+
return resolveTree(await node);
|
|
256
|
+
if (node == null || typeof node !== "object")
|
|
257
|
+
return node;
|
|
258
|
+
if (Array.isArray(node))
|
|
259
|
+
return Promise.all(node.map(resolveTree));
|
|
260
|
+
if ("__raw" in node)
|
|
261
|
+
return node;
|
|
262
|
+
if (!isElement(node))
|
|
263
|
+
return node;
|
|
264
|
+
const { type, props, children } = node;
|
|
265
|
+
if (typeof type !== "function") {
|
|
266
|
+
return { type, props, children: (await Promise.all(children.map(resolveTree))) };
|
|
267
|
+
}
|
|
268
|
+
const fn = type;
|
|
269
|
+
if (fn.__krateSuspense)
|
|
270
|
+
return resolveTree(children);
|
|
271
|
+
let callProps = { ...props, children };
|
|
272
|
+
if (fn.__krateEagerChildren) {
|
|
273
|
+
const entries = await Promise.all(Object.entries(props).map(async ([k, v]) => [k, isJSXValue(v) ? await resolveTree(v) : v]));
|
|
274
|
+
callProps = { ...Object.fromEntries(entries), children: await resolveTree(children) };
|
|
275
|
+
}
|
|
276
|
+
if (isClassComponent(fn)) {
|
|
277
|
+
const instance = new fn(callProps);
|
|
278
|
+
try {
|
|
279
|
+
return await resolveTree(instance.render());
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
return resolveTree(classFallback(fn, instance, error));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
let out = fn(callProps);
|
|
286
|
+
if (isThenable(out))
|
|
287
|
+
out = await out;
|
|
288
|
+
return resolveTree(out);
|
|
289
|
+
}
|
|
156
290
|
function renderElement(el) {
|
|
157
291
|
const { type, props, children } = el;
|
|
158
292
|
// Component
|
|
159
293
|
if (typeof type === "function") {
|
|
294
|
+
if (isClassComponent(type)) {
|
|
295
|
+
const instance = new type({ ...props, children });
|
|
296
|
+
try {
|
|
297
|
+
return renderNode(instance.render());
|
|
298
|
+
}
|
|
299
|
+
catch (error) {
|
|
300
|
+
return renderNode(classFallback(type, instance, error));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
160
303
|
const result = type({ ...props, children });
|
|
304
|
+
if (isThenable(result))
|
|
305
|
+
throw asyncInSyncRender(`<${componentName(type)}>`);
|
|
161
306
|
return renderNode(result);
|
|
162
307
|
}
|
|
163
308
|
// HTML element
|
|
@@ -171,7 +316,10 @@ function renderElement(el) {
|
|
|
171
316
|
if (VOID_ELEMENTS.has(tag)) {
|
|
172
317
|
return `<${tag}${attrs}>`;
|
|
173
318
|
}
|
|
174
|
-
const
|
|
319
|
+
const dangerous = props.dangerouslySetInnerHTML;
|
|
320
|
+
const inner = dangerous && typeof dangerous === "object" && typeof dangerous.__html === "string"
|
|
321
|
+
? dangerous.__html
|
|
322
|
+
: children.map(renderNode).join("");
|
|
175
323
|
return `<${tag}${attrs}>${inner}</${tag}>`;
|
|
176
324
|
}
|
|
177
325
|
// Streaming SSR via Suspense
|
|
@@ -204,6 +352,93 @@ export function Suspense(props) {
|
|
|
204
352
|
return raw(`<span data-suspense="${id}">${fallback}</span>` +
|
|
205
353
|
`<template data-suspense="${id}">${inner}</template>`);
|
|
206
354
|
}
|
|
355
|
+
Suspense.__krateSuspense = true;
|
|
356
|
+
function describeValue(v) {
|
|
357
|
+
if (typeof v === "function")
|
|
358
|
+
return "a function";
|
|
359
|
+
if (typeof v === "symbol")
|
|
360
|
+
return "a symbol";
|
|
361
|
+
if (typeof v === "bigint")
|
|
362
|
+
return "a bigint";
|
|
363
|
+
if (v && typeof v === "object") {
|
|
364
|
+
const ctor = Object.getPrototypeOf(v)?.constructor?.name;
|
|
365
|
+
return ctor ? `an instance of ${ctor}` : "a non-plain object";
|
|
366
|
+
}
|
|
367
|
+
return typeof v;
|
|
368
|
+
}
|
|
369
|
+
// serializeClientProp converts a prop crossing from the server to a client
|
|
370
|
+
// component into JSON-safe data. Server actions become references the client
|
|
371
|
+
// chunk turns back into callable proxies; anything else that JSON cannot carry
|
|
372
|
+
// faithfully throws with the offending prop path.
|
|
373
|
+
function serializeClientProp(v, path, component) {
|
|
374
|
+
if (v === null || typeof v === "string" || typeof v === "boolean")
|
|
375
|
+
return v;
|
|
376
|
+
if (typeof v === "number") {
|
|
377
|
+
if (!Number.isFinite(v))
|
|
378
|
+
throw clientPropError(path, component, `the number ${v}`);
|
|
379
|
+
return v;
|
|
380
|
+
}
|
|
381
|
+
if (v === undefined)
|
|
382
|
+
return undefined;
|
|
383
|
+
if (isServerAction(v))
|
|
384
|
+
return { __krateAction: v.actionUrl };
|
|
385
|
+
if (Array.isArray(v))
|
|
386
|
+
return v.map((x, i) => serializeClientProp(x, `${path}[${i}]`, component));
|
|
387
|
+
if (typeof v === "object") {
|
|
388
|
+
if (isElement(v) || "__raw" in v) {
|
|
389
|
+
throw clientPropError(path, component, "JSX", "only `children` can carry server-rendered JSX into a client component");
|
|
390
|
+
}
|
|
391
|
+
const proto = Object.getPrototypeOf(v);
|
|
392
|
+
if (proto !== Object.prototype && proto !== null)
|
|
393
|
+
throw clientPropError(path, component, describeValue(v));
|
|
394
|
+
const out = {};
|
|
395
|
+
for (const [k, x] of Object.entries(v)) {
|
|
396
|
+
const sv = serializeClientProp(x, `${path}.${k}`, component);
|
|
397
|
+
if (sv !== undefined)
|
|
398
|
+
out[k] = sv;
|
|
399
|
+
}
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
throw clientPropError(path, component, describeValue(v));
|
|
403
|
+
}
|
|
404
|
+
function clientPropError(path, component, what, hint) {
|
|
405
|
+
return new Error(`Prop \`${path}\` passed to client component <${component}> is ${what}, which cannot be sent from a server ` +
|
|
406
|
+
`component to the client. ` +
|
|
407
|
+
(hint || "Pass JSON-serializable data (strings, numbers, booleans, null, plain objects, arrays) or a server action; " +
|
|
408
|
+
"move event handlers and class instances into the client component."));
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* clientBoundary wraps an export of a `"use client"` module for the server
|
|
412
|
+
* bundle of a page that renders server components. The server renders the
|
|
413
|
+
* component's initial HTML (render) into a <krate-island> marker carrying its
|
|
414
|
+
* serialized props; the per-module client chunk at src hydrates it in the
|
|
415
|
+
* browser. Children are rendered on the server and passed as HTML.
|
|
416
|
+
*/
|
|
417
|
+
export function clientBoundary(moduleId, exportName, src, impl, render) {
|
|
418
|
+
if (typeof impl !== "function")
|
|
419
|
+
return impl;
|
|
420
|
+
const name = impl.displayName || impl.name || exportName;
|
|
421
|
+
const Boundary = async (props) => {
|
|
422
|
+
const { children, ...rest } = props || {};
|
|
423
|
+
const data = {};
|
|
424
|
+
for (const [k, v] of Object.entries(rest)) {
|
|
425
|
+
if (k === "key")
|
|
426
|
+
continue;
|
|
427
|
+
const sv = serializeClientProp(v, k, name);
|
|
428
|
+
if (sv !== undefined)
|
|
429
|
+
data[k] = sv;
|
|
430
|
+
}
|
|
431
|
+
const hasChildren = children != null && !(Array.isArray(children) && children.length === 0);
|
|
432
|
+
const childrenHTML = hasChildren ? await renderToStringAsync(children) : null;
|
|
433
|
+
if (childrenHTML !== null)
|
|
434
|
+
data.__krateChildren = childrenHTML;
|
|
435
|
+
const html = render(impl, data, childrenHTML);
|
|
436
|
+
return raw(`<krate-island style="display:contents" data-module="${escapeHTML(moduleId)}" data-export="${escapeHTML(exportName)}" ` +
|
|
437
|
+
`data-src="${escapeHTML(src)}" data-props="${escapeHTML(JSON.stringify(data))}">${html}</krate-island>`);
|
|
438
|
+
};
|
|
439
|
+
Object.defineProperty(Boundary, "name", { value: name });
|
|
440
|
+
return Boundary;
|
|
441
|
+
}
|
|
207
442
|
// Built-in Components (server stubs)
|
|
208
443
|
// Head - renders children into the page head at build time.
|
|
209
444
|
// On the server, we just render children; the build pipeline extracts them.
|
|
@@ -255,6 +490,7 @@ export function Link(props) {
|
|
|
255
490
|
const inner = children != null ? renderNode(children) : "";
|
|
256
491
|
return raw(`<a ${attrs.join(" ")}>${inner}</a>`);
|
|
257
492
|
}
|
|
493
|
+
Link.__krateEagerChildren = true;
|
|
258
494
|
// Image - renders an <img> tag (server stub; Go compiler handles full responsive output at build time).
|
|
259
495
|
export function Image(props) {
|
|
260
496
|
const { src = "", alt = "", width, height, ...rest } = props;
|
package/dist/server.test.js
CHANGED
|
@@ -23,6 +23,26 @@ describe('renderToString', () => {
|
|
|
23
23
|
it('passes raw HTML through without escaping', () => {
|
|
24
24
|
expect(renderToString({ __raw: '<b>hi</b>' })).toBe('<b>hi</b>');
|
|
25
25
|
});
|
|
26
|
+
it('maps React attribute aliases like the compiler renderer', () => {
|
|
27
|
+
const html = renderToString(jsx('label', { className: 'x', htmlFor: 'name', children: 'Name' }));
|
|
28
|
+
expect(html).toBe('<label class="x" for="name">Name</label>');
|
|
29
|
+
});
|
|
30
|
+
it('serializes style objects with unitless exceptions', () => {
|
|
31
|
+
const html = renderToString(jsx('div', {
|
|
32
|
+
style: { backgroundColor: 'red', marginTop: 4, opacity: 0.5, zIndex: 2, '--raw': 1 },
|
|
33
|
+
}));
|
|
34
|
+
expect(html).toBe('<div style="background-color:red;margin-top:4px;opacity:0.5;z-index:2;--raw:1"></div>');
|
|
35
|
+
});
|
|
36
|
+
it('renders dangerouslySetInnerHTML instead of an attribute', () => {
|
|
37
|
+
const html = renderToString(jsx('div', { dangerouslySetInnerHTML: { __html: '<b>hi</b>' } }));
|
|
38
|
+
expect(html).toBe('<div><b>hi</b></div>');
|
|
39
|
+
});
|
|
40
|
+
it('blocks javascript: URLs and drops reserved props', () => {
|
|
41
|
+
const html = renderToString(jsx('a', { href: 'javascript:alert(1)', ref: {}, key: 'k', children: 'x' }));
|
|
42
|
+
expect(html).toContain('href="javascript:throw new Error(');
|
|
43
|
+
expect(html).not.toContain(' ref=');
|
|
44
|
+
expect(html).not.toContain(' key=');
|
|
45
|
+
});
|
|
26
46
|
});
|
|
27
47
|
describe('goComponent', () => {
|
|
28
48
|
it('inlines baked HTML for an annotated server-tier call site', () => {
|
package/dist/signal.d.ts
CHANGED
|
@@ -39,6 +39,25 @@ export interface OwnerHooks {
|
|
|
39
39
|
}
|
|
40
40
|
/** Installs (or with null removes) the owner integration hooks. */
|
|
41
41
|
export declare function setOwnerHooks(hooks: OwnerHooks | null): void;
|
|
42
|
+
/**
|
|
43
|
+
* Enters the route scope `id` for the hydration code that follows. Returns
|
|
44
|
+
* false when the SPA router kept that layout mounted: its bindings and
|
|
45
|
+
* effects are still live and must not be created twice.
|
|
46
|
+
*/
|
|
47
|
+
export declare function krateEnterScope(id: string): boolean;
|
|
48
|
+
/** Leaves the current route scope (see krateEnterScope). */
|
|
49
|
+
export declare function krateLeaveScope(): void;
|
|
50
|
+
/**
|
|
51
|
+
* The route scope new effects and owners are tagged with (internal): the
|
|
52
|
+
* running effect's scope, else the scope whose hydration code is running,
|
|
53
|
+
* else the current component owner's scope (code running later, e.g. an
|
|
54
|
+
* event handler of a layout component).
|
|
55
|
+
*/
|
|
56
|
+
export declare function krateCurrentScope(): string | null;
|
|
57
|
+
/** Sets the layout scopes the router kept mounted for the next hydration (internal). */
|
|
58
|
+
export declare function krateKeepScopes(ids: string[]): void;
|
|
59
|
+
/** Records the route scope of an owner key (internal; see effects.ts). */
|
|
60
|
+
export declare function krateTagOwner(owner: unknown, scope: string | null): void;
|
|
42
61
|
/**
|
|
43
62
|
* Disposes every effect created under `owner` and runs its top-level
|
|
44
63
|
* onCleanup callbacks (internal, used when a component unmounts).
|
|
@@ -87,8 +106,12 @@ export declare function krateSetOwner(owner: unknown): unknown;
|
|
|
87
106
|
export declare function krateOnBeforeFlush(fn: () => void): void;
|
|
88
107
|
/** Registers fn to run after every settled effect flush (internal). */
|
|
89
108
|
export declare function krateOnAfterFlush(fn: () => void): void;
|
|
90
|
-
/**
|
|
91
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Registers fn to run at the start of disposeAll (internal). For a partial
|
|
111
|
+
* teardown (krateDisposeExcept / krateDisposeScopes) `gone` reports which
|
|
112
|
+
* route scopes are torn down; it is null for a full teardown.
|
|
113
|
+
*/
|
|
114
|
+
export declare function krateOnDispose(fn: (gone: ((scope: string | null) => boolean) | null) => void): void;
|
|
92
115
|
/** Reports whether signal writes are waiting for the next flush. */
|
|
93
116
|
export declare function kratePendingFlush(): boolean;
|
|
94
117
|
/**
|
|
@@ -108,6 +131,19 @@ export declare function afterFlush(fn: () => void): void;
|
|
|
108
131
|
* from -0.
|
|
109
132
|
*/
|
|
110
133
|
export declare function sameValue(a: unknown, b: unknown): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Partial teardown for a soft navigation that keeps some layouts mounted:
|
|
136
|
+
* disposes every effect, cleanup and component owner whose route scope is
|
|
137
|
+
* not in `keep`. With an empty `keep` this is disposeAll().
|
|
138
|
+
*/
|
|
139
|
+
export declare function krateDisposeExcept(keep: string[]): void;
|
|
140
|
+
/**
|
|
141
|
+
* Disposes only the effects, cleanups and component owners of the given
|
|
142
|
+
* route scopes (a parallel-route slot whose content is replaced).
|
|
143
|
+
*/
|
|
144
|
+
export declare function krateDisposeScopes(ids: string[]): void;
|
|
145
|
+
/** The route scope encoded in a layout/slot id ("_lHASH_x" -> "_lHASH_"), else null. */
|
|
146
|
+
export declare function krateScopeOfId(id: string): string | null;
|
|
111
147
|
export declare function disposeAll(): void;
|
|
112
148
|
/** ARIA role presets and raw overrides accepted by every CSS primitive. */
|
|
113
149
|
export interface CSSARIAOptions {
|
|
@@ -115,9 +151,10 @@ export interface CSSARIAOptions {
|
|
|
115
151
|
* Accessibility semantics to synthesize. Defaults are native (radios,
|
|
116
152
|
* checkboxes) and ship no JS. Presets that need synthesized state
|
|
117
153
|
* (`tabs`, `listbox`, `accordion`, `disclosure`, `dialog`, `menu`,
|
|
118
|
-
* `popover`) inject a tiny ARIA
|
|
154
|
+
* `popover`, `nav`) inject a tiny keyboard and ARIA enhancer automatically:
|
|
155
|
+
* Enter/Space/Escape/arrow keys, focus move/trap/restore, aria-expanded.
|
|
119
156
|
*/
|
|
120
|
-
as?: 'radiogroup' | 'tabs' | 'tablist' | 'listbox' | 'menu' | 'menubar' | 'accordion' | 'disclosure' | 'dialog' | 'modal' | 'popover' | 'switch' | 'checkbox';
|
|
157
|
+
as?: 'radiogroup' | 'tabs' | 'tablist' | 'listbox' | 'menu' | 'menubar' | 'nav' | 'accordion' | 'disclosure' | 'dialog' | 'modal' | 'popover' | 'switch' | 'checkbox';
|
|
121
158
|
/** Accessible label for the scope container. */
|
|
122
159
|
label?: string;
|
|
123
160
|
/** Raw ARIA overrides (`{ role, 'aria-controls', panelRole }`); `false` opts out of all ARIA. */
|