@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.
- package/dist/action-context.js +1 -1
- package/dist/actions.d.ts +7 -0
- package/dist/actions.js +34 -10
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/boundary.js +1 -1
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/cache-adapter.js +1 -1
- package/dist/cache-scope.d.ts +8 -0
- package/dist/cache-scope.js +14 -1
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +470 -77
- package/dist/dom.js +18 -3
- package/dist/effects.js +2 -2
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/handler-html.js +4 -4
- package/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +9 -190
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +18 -4
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +93 -6
- package/dist/highlight-bind.js +1 -1
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +17 -12
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +147 -74
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/island-swap.d.ts +47 -0
- package/dist/island-swap.js +635 -0
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-types.d.ts +99 -3
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +385 -66
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +1 -1
- package/dist/krate-f-highlight.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +454 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/list.d.ts +14 -4
- package/dist/list.js +4 -5
- package/dist/metadata.js +5 -2
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/node-server.js +3 -3
- package/dist/og.js +1 -1
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/react-compat.d.ts +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/region-wire.d.ts +10 -0
- package/dist/region-wire.js +154 -0
- package/dist/request-context.js +1 -1
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/resp.js +1 -1
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.d.ts +1 -1
- package/dist/router.js +628 -98
- package/dist/server-handler.d.ts +69 -1
- package/dist/server-handler.js +515 -96
- package/dist/server-renderer.js +16 -7
- package/dist/server.d.ts +73 -16
- package/dist/server.js +619 -102
- package/dist/sha256.js +9 -9
- package/dist/signal.js +0 -6
- package/dist/site-config.d.ts +15 -0
- package/dist/site-config.js +28 -0
- package/dist/slot.d.ts +12 -0
- package/dist/slot.js +23 -0
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +12 -4
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +90 -0
- package/dist/use-id.js +1 -1
- package/package.json +28 -3
- package/dist/action-context.test.d.ts +0 -1
- package/dist/action-context.test.js +0 -72
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -477
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -244
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -233
- package/dist/class-component.test.d.ts +0 -1
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.d.ts +0 -1
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -57
- package/dist/derived.test.d.ts +0 -1
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.d.ts +0 -1
- package/dist/dev-overlay.test.js +0 -165
- package/dist/discrete.test.d.ts +0 -1
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.d.ts +0 -1
- package/dist/dom.test.js +0 -39
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -199
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -447
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -256
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -217
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -131
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -435
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -492
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -125
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -133
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -559
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -535
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -266
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -88
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -187
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -620
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- 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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
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;
|
package/dist/ssr-core.d.ts
CHANGED
|
@@ -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 = { ...
|
|
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: { ...
|
|
373
|
-
searchParams: tracker ? trackedQuery(req.query, tracker) : { ...
|
|
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 = { ...
|
|
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>>;
|
package/dist/testing.js
ADDED
|
@@ -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) &
|
|
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.
|
|
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
|
-
});
|
package/dist/actions.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|