@krate/runtime 0.5.0-rc.15 → 0.5.0-rc.16
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/NOTICE +10 -0
- package/dist/cache-scope.d.ts +10 -2
- package/dist/cache-scope.js +26 -4
- package/dist/island-swap.js +46 -8
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +3 -3
- package/dist/krate-island-swap.js +1 -1
- package/dist/krate-runtime.js +4 -4
- package/dist/memsize.d.ts +14 -0
- package/dist/memsize.js +71 -0
- package/dist/segmented-lru.d.ts +14 -1
- package/dist/segmented-lru.js +90 -21
- package/dist/server-handler.d.ts +1 -0
- package/dist/server-handler.js +3 -1
- package/dist/server-renderer.js +4 -1
- package/dist/server.d.ts +3 -1
- package/dist/server.js +53 -27
- package/dist/shared-isr.js +2 -1
- package/dist/ssr-core.d.ts +10 -2
- package/dist/ssr-core.js +19 -6
- package/package.json +3 -2
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
@krate/runtime
|
|
2
|
+
Copyright 2026 The Krate Authors
|
|
3
|
+
|
|
4
|
+
This package is part of Krate (https://github.com/kratejs/krate,
|
|
5
|
+
https://krate.js.org), licensed under the Apache License, Version 2.0
|
|
6
|
+
(https://www.apache.org/licenses/LICENSE-2.0).
|
|
7
|
+
|
|
8
|
+
Under section 4(d) of the License, any redistribution of this package or of
|
|
9
|
+
a Derivative Work, including a fork, must retain this NOTICE file, or the
|
|
10
|
+
attribution notices it contains.
|
package/dist/cache-scope.d.ts
CHANGED
|
@@ -51,13 +51,21 @@ export interface RevalidateScope {
|
|
|
51
51
|
/** Tasks registered with after(); the host runs them once the response is sent. */
|
|
52
52
|
after?: Array<() => unknown>;
|
|
53
53
|
}
|
|
54
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* In-process data cache with tag index and FIFO bound, by entry count and,
|
|
56
|
+
* with maxBytes, by the approximate size of the values.
|
|
57
|
+
*/
|
|
55
58
|
export declare class DataCache {
|
|
56
59
|
private max;
|
|
60
|
+
private maxBytes;
|
|
57
61
|
private map;
|
|
58
62
|
private byTag;
|
|
59
|
-
|
|
63
|
+
private sizes;
|
|
64
|
+
private used;
|
|
65
|
+
constructor(max?: number, maxBytes?: number);
|
|
60
66
|
get size(): number;
|
|
67
|
+
/** The approximate memory the cached values take (0 without maxBytes). */
|
|
68
|
+
get bytes(): number;
|
|
61
69
|
get(key: string): unknown | undefined;
|
|
62
70
|
set(key: string, value: unknown, ttlSeconds: number, tags: string[]): void;
|
|
63
71
|
delete(key: string): void;
|
package/dist/cache-scope.js
CHANGED
|
@@ -19,18 +19,29 @@
|
|
|
19
19
|
* there the data cache is backed by the Go host (`_go_cache_get/_go_cache_set`).
|
|
20
20
|
*/
|
|
21
21
|
import { HEAD_CLOSE, HEAD_OPEN } from './head-tags.js';
|
|
22
|
+
import { approxBytes, cacheByteBudgets } from './memsize.js';
|
|
22
23
|
const STATE_KEY = Symbol.for('krate.cache.runtime');
|
|
23
24
|
const REVALIDATE_STORE_KEY = Symbol.for('krate.revalidate.store');
|
|
24
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* In-process data cache with tag index and FIFO bound, by entry count and,
|
|
27
|
+
* with maxBytes, by the approximate size of the values.
|
|
28
|
+
*/
|
|
25
29
|
export class DataCache {
|
|
26
|
-
constructor(max = 1000) {
|
|
30
|
+
constructor(max = 1000, maxBytes = 0) {
|
|
27
31
|
this.max = max;
|
|
32
|
+
this.maxBytes = maxBytes;
|
|
28
33
|
this.map = new Map();
|
|
29
34
|
this.byTag = new Map();
|
|
35
|
+
this.sizes = new Map();
|
|
36
|
+
this.used = 0;
|
|
30
37
|
}
|
|
31
38
|
get size() {
|
|
32
39
|
return this.map.size;
|
|
33
40
|
}
|
|
41
|
+
/** The approximate memory the cached values take (0 without maxBytes). */
|
|
42
|
+
get bytes() {
|
|
43
|
+
return this.used;
|
|
44
|
+
}
|
|
34
45
|
get(key) {
|
|
35
46
|
const e = this.map.get(key);
|
|
36
47
|
if (!e)
|
|
@@ -46,7 +57,10 @@ export class DataCache {
|
|
|
46
57
|
return;
|
|
47
58
|
if (this.map.has(key))
|
|
48
59
|
this.delete(key);
|
|
49
|
-
|
|
60
|
+
const n = this.maxBytes > 0 ? approxBytes(value) + key.length : 0;
|
|
61
|
+
if (this.maxBytes > 0 && n > this.maxBytes)
|
|
62
|
+
return;
|
|
63
|
+
while (this.map.size >= this.max || (this.maxBytes > 0 && this.map.size > 0 && this.used + n > this.maxBytes)) {
|
|
50
64
|
const oldest = this.map.keys().next().value;
|
|
51
65
|
if (oldest === undefined)
|
|
52
66
|
break;
|
|
@@ -54,6 +68,10 @@ export class DataCache {
|
|
|
54
68
|
}
|
|
55
69
|
const expires = ttlSeconds === Infinity ? Infinity : Date.now() + ttlSeconds * 1000;
|
|
56
70
|
this.map.set(key, { value, expires, tags: [...tags] });
|
|
71
|
+
if (n > 0) {
|
|
72
|
+
this.sizes.set(key, n);
|
|
73
|
+
this.used += n;
|
|
74
|
+
}
|
|
57
75
|
for (const t of tags) {
|
|
58
76
|
let keys = this.byTag.get(t);
|
|
59
77
|
if (!keys)
|
|
@@ -66,6 +84,8 @@ export class DataCache {
|
|
|
66
84
|
if (!e)
|
|
67
85
|
return;
|
|
68
86
|
this.map.delete(key);
|
|
87
|
+
this.used -= this.sizes.get(key) || 0;
|
|
88
|
+
this.sizes.delete(key);
|
|
69
89
|
for (const t of e.tags) {
|
|
70
90
|
const keys = this.byTag.get(t);
|
|
71
91
|
if (keys) {
|
|
@@ -93,6 +113,8 @@ export class DataCache {
|
|
|
93
113
|
clear() {
|
|
94
114
|
this.map.clear();
|
|
95
115
|
this.byTag.clear();
|
|
116
|
+
this.sizes.clear();
|
|
117
|
+
this.used = 0;
|
|
96
118
|
}
|
|
97
119
|
}
|
|
98
120
|
/** The shared cache runtime, created on first use. */
|
|
@@ -102,7 +124,7 @@ export function cacheRuntime() {
|
|
|
102
124
|
if (!rt) {
|
|
103
125
|
const max = envNumber('KRATE_DATA_CACHE_MAX') || 1000;
|
|
104
126
|
const created = {
|
|
105
|
-
dataCache: new DataCache(max),
|
|
127
|
+
dataCache: new DataCache(max, cacheByteBudgets(envNumber('KRATE_SSR_CACHE_BYTES')).data),
|
|
106
128
|
epoch: 0,
|
|
107
129
|
invalidate(payload) {
|
|
108
130
|
created.epoch++;
|
package/dist/island-swap.js
CHANGED
|
@@ -18,9 +18,13 @@
|
|
|
18
18
|
// Preact island, the server children (slots) it adopted from the document.
|
|
19
19
|
//
|
|
20
20
|
// Keying: islands are matched per container (the swapped region, or one
|
|
21
|
-
// server-children slot of a kept island), outermost islands only,
|
|
22
|
-
//
|
|
23
|
-
//
|
|
21
|
+
// server-children slot of a kept island), outermost islands only, by module
|
|
22
|
+
// and export. An island rendered with a React key (the marker's k, a host's
|
|
23
|
+
// data-key) pairs with the live island of the same component and key wherever
|
|
24
|
+
// it is, so a list that lost or reordered rows keeps each row's state, as
|
|
25
|
+
// React reconciles a keyed list; a live keyed island whose key is gone is
|
|
26
|
+
// unmounted. Unkeyed islands pair in document order: the n-th unkeyed island
|
|
27
|
+
// of a component in the live region pairs with the n-th one in the new markup. A navigation to another
|
|
24
28
|
// pathname only keeps an island whose server props are unchanged too (a
|
|
25
29
|
// layout's island); one whose props differ belongs to the other page and is
|
|
26
30
|
// mounted anew, as React remounts a page's components.
|
|
@@ -72,6 +76,10 @@ function propsText(el) {
|
|
|
72
76
|
const s = el.previousElementSibling;
|
|
73
77
|
return s && s.localName === 'script' && s.hasAttribute('data-krate-props') ? s.textContent || '' : '';
|
|
74
78
|
}
|
|
79
|
+
/** The pairing key of an island: its component, and its React key when it has one. */
|
|
80
|
+
function islandKey(component, k) {
|
|
81
|
+
return k === null ? component : component + '#' + JSON.stringify(k);
|
|
82
|
+
}
|
|
75
83
|
/** The outermost islands in list, in document order. */
|
|
76
84
|
function scan(list, chain = [], out = []) {
|
|
77
85
|
for (let i = 0; i < list.length; i++) {
|
|
@@ -87,13 +95,16 @@ function scan(list, chain = [], out = []) {
|
|
|
87
95
|
}
|
|
88
96
|
if (!end || !p)
|
|
89
97
|
continue;
|
|
90
|
-
|
|
98
|
+
const keyed = typeof p.k === 'string';
|
|
99
|
+
out.push({ key: islandKey(p.m + '#' + p.e, keyed ? p.k : null), keyed, start: n, end, json: n.data.slice(OPEN.length), chain, rec: n.__krateIsland });
|
|
91
100
|
while (i < list.length - 1 && list[i] !== end)
|
|
92
101
|
i++;
|
|
93
102
|
}
|
|
94
103
|
else if (isHost(n)) {
|
|
104
|
+
const k = n.getAttribute('data-key');
|
|
95
105
|
out.push({
|
|
96
|
-
key: n.getAttribute('data-module') + '#' + n.getAttribute('data-export'),
|
|
106
|
+
key: islandKey(n.getAttribute('data-module') + '#' + n.getAttribute('data-export'), k),
|
|
107
|
+
keyed: k !== null,
|
|
97
108
|
el: n,
|
|
98
109
|
json: propsText(n),
|
|
99
110
|
chain,
|
|
@@ -341,10 +352,25 @@ function pairIslands(liveList, newList, allow) {
|
|
|
341
352
|
const live = scan(liveList);
|
|
342
353
|
const next = scan(newList);
|
|
343
354
|
const pairs = new Map();
|
|
355
|
+
// A keyed island pairs by key wherever it is (the first live island with a
|
|
356
|
+
// key, when two share it); unkeyed ones pair in order.
|
|
357
|
+
const byKey = new Map();
|
|
358
|
+
for (const l of live)
|
|
359
|
+
if (l.keyed && !byKey.has(l.key))
|
|
360
|
+
byKey.set(l.key, l);
|
|
344
361
|
let from = 0;
|
|
345
362
|
for (const n of next) {
|
|
363
|
+
if (n.keyed) {
|
|
364
|
+
const l = byKey.get(n.key);
|
|
365
|
+
if (!l)
|
|
366
|
+
continue;
|
|
367
|
+
byKey.delete(n.key);
|
|
368
|
+
if (mounted(l.rec) && (!allow || allow(l, n)))
|
|
369
|
+
pairs.set(n, l);
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
346
372
|
for (let j = from; j < live.length; j++) {
|
|
347
|
-
if (live[j].key !== n.key)
|
|
373
|
+
if (live[j].keyed || live[j].key !== n.key)
|
|
348
374
|
continue;
|
|
349
375
|
from = j + 1;
|
|
350
376
|
if (mounted(live[j].rec) && (!allow || allow(live[j], n)))
|
|
@@ -417,9 +443,21 @@ function resolve(p, list) {
|
|
|
417
443
|
}
|
|
418
444
|
return out;
|
|
419
445
|
}
|
|
420
|
-
/**
|
|
446
|
+
/**
|
|
447
|
+
* Whether every island on both sides is paired (nothing to mount or unmount)
|
|
448
|
+
* and in the same order (a keyed list that was reordered moves its islands).
|
|
449
|
+
*/
|
|
421
450
|
function allPaired(p) {
|
|
422
|
-
|
|
451
|
+
if (p.gone.length !== 0 || p.pairs.size !== p.next.length)
|
|
452
|
+
return false;
|
|
453
|
+
let last = -1;
|
|
454
|
+
for (const n of p.next) {
|
|
455
|
+
const at = p.live.indexOf(p.pairs.get(n));
|
|
456
|
+
if (at < last)
|
|
457
|
+
return false;
|
|
458
|
+
last = at;
|
|
459
|
+
}
|
|
460
|
+
return true;
|
|
423
461
|
}
|
|
424
462
|
/**
|
|
425
463
|
* Replaces the live children of parent between start and end (exclusive;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var __krate_exports=(()=>{var m=Object.defineProperty;var
|
|
1
|
+
var __krate_exports=(()=>{var m=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var q=(e,t)=>{for(var n in t)m(e,n,{get:t[n],enumerable:!0})},I=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of U(t))!F.call(e,o)&&o!==n&&m(e,o,{get:()=>t[o],enumerable:!(r=H(t,o))||r.enumerable});return e};var M=e=>I(m({},"__esModule",{value:!0}),e);var se={};q(se,{forbidden:()=>C,krateLayoutSegments:()=>X,krateMatchParams:()=>V,krateResetNavigation:()=>W,krateRouteStore:()=>K,notFound:()=>v,permanentRedirect:()=>oe,redirect:()=>re,unauthorized:()=>E,useParams:()=>Z,usePathname:()=>$,useRouter:()=>ee,useSearchParams:()=>Y,useSelectedLayoutSegment:()=>ne,useSelectedLayoutSegments:()=>te});var T=window.createSignal;var c=window.krateDerived;var G=Symbol.for("krate.request.store");function i(){let e=globalThis[G];return e&&typeof e.getStore=="function"?e.getStore():void 0}function v(){let e=new Error("NEXT_NOT_FOUND");throw e.__krateNotFound=!0,e.digest="NEXT_NOT_FOUND",e}function _(e){let t=new Error(e===403?"NEXT_FORBIDDEN":"NEXT_UNAUTHORIZED");throw t.__krateHTTPStatus=e,t.digest="NEXT_HTTP_ERROR_FALLBACK;"+e,t}function C(){return _(403)}function E(){return _(401)}var fe=64*1024*1024;function S(){let e=i();if(!e)return{path:"/",route:"",params:{}};let t=e.url.split("?")[0]||"/",n=e.params||{},r=[];for(let o of t.split("/").filter(Boolean)){let s=/^\[(\[)?(\.\.\.)?([^\]]+)\]?\]$/.exec(o);if(!s){r.push(o);continue}let u=n[s[3]];u!==void 0&&u!==""&&r.push(...u.split("/").filter(Boolean))}return{path:"/"+r.join("/"),route:t,params:n}}function w(e){try{return decodeURIComponent(e)}catch{return e}}function A(){return S().path}function O(){let e=i(),t=new URLSearchParams;for(let[n,r]of Object.entries(e&&e.searchParams||{}))t.append(n,String(r));return t}function N(){let{route:e,params:t}=S(),n={};for(let r of e.split("/").filter(Boolean)){let o=/^\[(\[)?(\.\.\.)?([^\]]+)\]?\]$/.exec(r);if(!o)continue;let s=t[o[3]];s===void 0||s===""||(n[o[3]]=o[2]?s.split("/").filter(Boolean).map(w):w(s))}return n}function f(e){return()=>{throw new Error(`useRouter().${e}() can only be called in the browser; use redirect() while rendering`)}}function D(){return{push:f("push"),replace:f("replace"),back:f("back"),forward:f("forward"),refresh:f("refresh"),prefetch:()=>{}}}function x(e,t){return S().path.split("/").filter(Boolean).map(w).slice(typeof t=="number"?t:0)}function P(e,t){let n=x(e,t);return n.length>0?n[0]:null}var L=window.getNavigationState,k=window.subscribeNavigation,a=window.krateRouter;function j(){let e=globalThis.__KRATE_ROUTE__;if(typeof e=="string")return e;if(typeof document>"u")return"";let t=document.getElementById("__krate_page");if(!t)return"";try{let n=JSON.parse(t.textContent||"{}");return typeof n.r=="string"?n.r:""}catch{return""}}function z(e){return e.length>1&&e.endsWith("/")?e.replace(/\/+$/,"")||"/":e}function y(){if(typeof location>"u")return{pathname:"/",search:"",route:""};let{pathname:e,search:t}=L();return{pathname:z(e),search:t,route:j()}}var p=null;function g(){if(p)return p;let[e,t]=T(y());if(p=e,typeof window<"u"){let n=()=>{let r=y(),o=e();(r.pathname!==o.pathname||r.search!==o.search||r.route!==o.route)&&t(r)};k(n),window.addEventListener("krate:navigate-end",n)}return e}function K(){let e=null;return{subscribe:r=>{let o=k(r);return typeof window<"u"&&window.addEventListener("krate:navigate-end",r),()=>{o(),typeof window<"u"&&window.removeEventListener("krate:navigate-end",r)}},getSnapshot:()=>{let r=y();return e&&e.pathname===r.pathname&&e.search===r.search&&e.route===r.route?e:(e=r,r)}}}function V(e,t){return B(e,t)}function X(e,t){return e.split("/").filter(Boolean).map(d).slice(typeof t=="number"?t:0)}function W(){p=null}function l(e,t){let n=e;return n.toString=t,n.toJSON=()=>e(),e}function $(){if(i())return A();let e=g(),t=c(()=>e().pathname);return l(t,()=>t())}function Y(){if(i())return O();let e=g(),t=c(()=>new URLSearchParams(e().search));return l(t,()=>e().search)}function d(e){try{return decodeURIComponent(e)}catch{return e}}function B(e,t){let n={};if(!e)return n;let r=e.split("/").filter(Boolean),o=t.split("/").filter(Boolean);for(let s=0;s<r.length;s++){let u=r[s],h=/^\[(\[)?(\.\.\.)?([^\]]+)\]?\]$/.exec(u);if(!h)continue;let R=h[3];if(h[2]){let b=o.slice(s).map(d);return b.length>0&&(n[R]=b),n}s<o.length&&(n[R]=d(o[s]))}return n}function Z(){if(i())return N();let e=g(),t=c(()=>{let n=e();return B(n.route,n.pathname)});return l(t,()=>JSON.stringify(t()))}var Q={push:(e,t)=>{a.push(e,t)},replace:(e,t)=>{a.replace(e,t)},back:()=>a.back(),forward:()=>a.forward(),refresh:()=>{a.refresh()},prefetch:e=>{a.prefetch(e)}};function ee(){return i()?D():Q}function te(e,t){if(i())return x(e,t);let n=g(),r=typeof t=="number"?t:0,o=c(()=>n().pathname.split("/").filter(Boolean).map(d).slice(r));return l(o,()=>o().join("/"))}function ne(e,t){if(i())return P(e,t);let n=g(),r=typeof t=="number"?t:0,o=c(()=>{let s=n().pathname.split("/").filter(Boolean).map(d);return s.length>r?s[r]:null});return l(o,()=>String(o()))}function re(e){typeof location<"u"&&a.push(e);let t=new Error("Server action redirect to "+e);throw t.__krateRedirect=e,t}function oe(e){typeof location<"u"&&a.push(e);let t=new Error("Server action redirect to "+e);throw t.__krateRedirect=e,t.__krateRedirectStatus=308,t}return M(se);})();
|
|
2
2
|
for (const k in __krate_exports) { window[k] = __krate_exports[k]; }
|