@krate/runtime 0.5.0-rc.14 → 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.
Files changed (70) hide show
  1. package/NOTICE +10 -0
  2. package/dist/action-context.js +5 -1
  3. package/dist/cache-scope.d.ts +10 -2
  4. package/dist/cache-scope.js +26 -4
  5. package/dist/dev/api.d.ts +93 -0
  6. package/dist/dev/api.js +2 -0
  7. package/dist/dev/app.d.ts +165 -0
  8. package/dist/dev/app.js +1204 -0
  9. package/dist/dev/commands.d.ts +49 -0
  10. package/dist/dev/commands.js +114 -0
  11. package/dist/dev/highlight.d.ts +4 -0
  12. package/dist/dev/highlight.js +126 -0
  13. package/dist/dev/icons.d.ts +4 -0
  14. package/dist/dev/icons.js +57 -0
  15. package/dist/dev/inspect.d.ts +55 -0
  16. package/dist/dev/inspect.js +256 -0
  17. package/dist/dev/inspector.d.ts +39 -0
  18. package/dist/dev/inspector.js +348 -0
  19. package/dist/dev/issues.d.ts +83 -0
  20. package/dist/dev/issues.js +166 -0
  21. package/dist/dev/modal.d.ts +16 -0
  22. package/dist/dev/modal.js +836 -0
  23. package/dist/dev/native.d.ts +53 -0
  24. package/dist/dev/native.js +95 -0
  25. package/dist/dev/palette.d.ts +9 -0
  26. package/dist/dev/palette.js +250 -0
  27. package/dist/dev/panel.d.ts +31 -0
  28. package/dist/dev/panel.js +848 -0
  29. package/dist/dev/requests.d.ts +36 -0
  30. package/dist/dev/requests.js +150 -0
  31. package/dist/dev/settings.d.ts +32 -0
  32. package/dist/dev/settings.js +78 -0
  33. package/dist/dev/sourcemap.d.ts +22 -0
  34. package/dist/dev/sourcemap.js +148 -0
  35. package/dist/dev/stack.d.ts +38 -0
  36. package/dist/dev/stack.js +115 -0
  37. package/dist/dev/styles.d.ts +1 -0
  38. package/dist/dev/styles.js +424 -0
  39. package/dist/dev/util.d.ts +63 -0
  40. package/dist/dev/util.js +190 -0
  41. package/dist/dev-overlay.d.ts +9 -83
  42. package/dist/dev-overlay.js +17 -1038
  43. package/dist/island-swap.js +53 -9
  44. package/dist/krate-core.js +1 -1
  45. package/dist/krate-dev.js +441 -871
  46. package/dist/krate-f-list.js +1 -1
  47. package/dist/krate-f-navigation.js +1 -1
  48. package/dist/krate-hydrate-lite.js +5 -5
  49. package/dist/krate-hydrate.js +5 -5
  50. package/dist/krate-island-swap.js +1 -1
  51. package/dist/krate-runtime.js +5 -5
  52. package/dist/list.js +23 -1
  53. package/dist/memsize.d.ts +14 -0
  54. package/dist/memsize.js +71 -0
  55. package/dist/metadata.js +23 -12
  56. package/dist/request-context.d.ts +10 -0
  57. package/dist/request-context.js +24 -0
  58. package/dist/segmented-lru.d.ts +14 -1
  59. package/dist/segmented-lru.js +90 -21
  60. package/dist/server-handler.d.ts +3 -0
  61. package/dist/server-handler.js +4 -2
  62. package/dist/server-renderer.js +10 -3
  63. package/dist/server.d.ts +3 -1
  64. package/dist/server.js +55 -29
  65. package/dist/shared-isr.js +2 -1
  66. package/dist/signal.d.ts +8 -0
  67. package/dist/signal.js +4 -0
  68. package/dist/ssr-core.d.ts +13 -2
  69. package/dist/ssr-core.js +63 -11
  70. package/package.json +3 -2
@@ -0,0 +1,49 @@
1
+ export interface RouteInfo {
2
+ pattern: string;
3
+ source?: string;
4
+ mode?: string;
5
+ revalidate?: number;
6
+ kind: 'page' | 'api';
7
+ methods?: string[];
8
+ paths?: string[];
9
+ }
10
+ export interface PaletteItem {
11
+ id: string;
12
+ group: 'Routes' | 'Actions';
13
+ label: string;
14
+ mono?: boolean;
15
+ /** Indented under its route (a concrete path of a dynamic route). */
16
+ sub?: boolean;
17
+ chip?: string;
18
+ chipClass?: string;
19
+ right?: string;
20
+ icon?: string;
21
+ keys?: string[];
22
+ /** URL to navigate to, for route items. */
23
+ href?: string;
24
+ /** Project file Cmd/Ctrl+Enter opens. */
25
+ source?: string;
26
+ action?: string;
27
+ }
28
+ export interface Match {
29
+ score: number;
30
+ ranges: [number, number][];
31
+ }
32
+ /**
33
+ * Matches a query against text: a substring scores best (earlier and at a
34
+ * segment start is better), then a subsequence. Null when it does not match.
35
+ */
36
+ export declare function fuzzyMatch(query: string, text: string): Match | null;
37
+ /** Text with the matched ranges wrapped in <mark>, escaped. */
38
+ export declare function markRanges(text: string, ranges: [number, number][]): string;
39
+ /** Command menu items for the routes /__krate/dev/routes lists. */
40
+ export declare function routeItems(routes: RouteInfo[], query: string): PaletteItem[];
41
+ /**
42
+ * Filters and orders items for the query. Routes come before actions; within a
43
+ * group the best match comes first. An empty query keeps the given order, and
44
+ * concrete paths only show when they match.
45
+ */
46
+ export declare function filterItems(items: PaletteItem[], query: string, limit?: number): {
47
+ item: PaletteItem;
48
+ match: Match;
49
+ }[];
@@ -0,0 +1,114 @@
1
+ // Command menu items and their filtering.
2
+ import { esc } from './util.js';
3
+ /**
4
+ * Matches a query against text: a substring scores best (earlier and at a
5
+ * segment start is better), then a subsequence. Null when it does not match.
6
+ */
7
+ export function fuzzyMatch(query, text) {
8
+ const q = query.trim().toLowerCase();
9
+ if (!q)
10
+ return { score: 0, ranges: [] };
11
+ const t = text.toLowerCase();
12
+ const at = t.indexOf(q);
13
+ if (at !== -1) {
14
+ const boundary = at === 0 || /[/\s\-_.[]/.test(t[at - 1]);
15
+ return { score: 1000 - at + (boundary ? 200 : 0) - t.length / 100, ranges: [[at, at + q.length]] };
16
+ }
17
+ const ranges = [];
18
+ let ti = 0;
19
+ for (const ch of q) {
20
+ if (ch === ' ')
21
+ continue;
22
+ const idx = t.indexOf(ch, ti);
23
+ if (idx === -1)
24
+ return null;
25
+ const last = ranges[ranges.length - 1];
26
+ if (last && last[1] === idx)
27
+ last[1] = idx + 1;
28
+ else
29
+ ranges.push([idx, idx + 1]);
30
+ ti = idx + 1;
31
+ }
32
+ return { score: 100 - ranges.length * 10 - t.length / 100, ranges };
33
+ }
34
+ /** Text with the matched ranges wrapped in <mark>, escaped. */
35
+ export function markRanges(text, ranges) {
36
+ let out = '';
37
+ let pos = 0;
38
+ for (const [a, b] of ranges) {
39
+ out += esc(text.slice(pos, a)) + '<mark>' + esc(text.slice(a, b)) + '</mark>';
40
+ pos = b;
41
+ }
42
+ return out + esc(text.slice(pos));
43
+ }
44
+ function isDynamic(pattern) {
45
+ return pattern.includes('[');
46
+ }
47
+ /** Command menu items for the routes /__krate/dev/routes lists. */
48
+ export function routeItems(routes, query) {
49
+ const items = [];
50
+ const q = query.trim();
51
+ for (const r of routes) {
52
+ const dyn = isDynamic(r.pattern);
53
+ const paths = r.paths || [];
54
+ const api = r.kind === 'api';
55
+ let right = r.source || '';
56
+ if (api)
57
+ right = r.methods && r.methods.length ? r.methods.join(' ') : r.source || '';
58
+ else if (dyn)
59
+ right = paths.length + (paths.length === 1 ? ' path' : ' paths');
60
+ else if (r.revalidate)
61
+ right = 'revalidate ' + r.revalidate + 's';
62
+ const mode = api ? 'API' : (r.mode || 'ssg').toUpperCase();
63
+ items.push({
64
+ id: 'route:' + r.kind + ':' + r.pattern,
65
+ group: 'Routes',
66
+ label: r.pattern,
67
+ mono: true,
68
+ chip: mode,
69
+ chipClass: api ? 'api' : (r.mode || 'ssg').toLowerCase(),
70
+ right,
71
+ href: dyn ? paths[0] : r.pattern,
72
+ source: r.source,
73
+ });
74
+ if (dyn && q) {
75
+ for (const p of paths) {
76
+ if (p === r.pattern)
77
+ continue;
78
+ items.push({
79
+ id: 'path:' + p,
80
+ group: 'Routes',
81
+ label: p,
82
+ mono: true,
83
+ sub: true,
84
+ right: r.pattern,
85
+ href: p,
86
+ source: r.source,
87
+ });
88
+ }
89
+ }
90
+ }
91
+ return items;
92
+ }
93
+ /**
94
+ * Filters and orders items for the query. Routes come before actions; within a
95
+ * group the best match comes first. An empty query keeps the given order, and
96
+ * concrete paths only show when they match.
97
+ */
98
+ export function filterItems(items, query, limit = 60) {
99
+ const q = query.trim();
100
+ const out = [];
101
+ items.forEach((item, i) => {
102
+ const m = fuzzyMatch(q, item.label);
103
+ if (!m)
104
+ return;
105
+ if (item.sub && m.ranges.length !== 1)
106
+ return;
107
+ out.push({ item, match: m, i });
108
+ });
109
+ const order = (g) => (g === 'Routes' ? 0 : 1);
110
+ out.sort((a, b) => order(a.item.group) - order(b.item.group) || (q ? b.match.score - a.match.score : 0) || a.i - b.i);
111
+ const routes = out.filter((o) => o.item.group === 'Routes').slice(0, limit);
112
+ const actions = out.filter((o) => o.item.group === 'Actions');
113
+ return [...routes, ...actions].map(({ item, match }) => ({ item, match }));
114
+ }
@@ -0,0 +1,4 @@
1
+ /** The [start, end) range of the word at a 1-based column, for the squiggle. */
2
+ export declare function wordAt(src: string, col: number): [number, number];
3
+ /** One highlighted line as HTML; `sq` underlines [start, end) with a squiggle. */
4
+ export declare function highlightLine(src: string, lang: string, sq?: [number, number]): string;
@@ -0,0 +1,126 @@
1
+ // A small single-line highlighter for code frames. It only has to look right
2
+ // for a few lines of TSX, TS, JS or CSS around an error; anything it does not
3
+ // know stays plain.
4
+ import { esc } from './util.js';
5
+ const KEYWORDS = new Set(('import export from default return const let var function if else for while do switch case break continue new ' +
6
+ 'class extends await async typeof instanceof in of try catch finally throw yield void delete this null undefined ' +
7
+ 'true false as type interface enum package func go defer chan map struct range select').split(' '));
8
+ const SCRIPT = new Set(['tsx', 'ts', 'jsx', 'js', 'go', 'mdx']);
9
+ function tokenize(src, lang) {
10
+ const toks = [];
11
+ const jsx = lang === 'tsx' || lang === 'jsx' || lang === 'mdx' || lang === 'html' || lang === 'svg';
12
+ const css = lang === 'css';
13
+ const script = SCRIPT.has(lang);
14
+ let i = 0;
15
+ let prevWord = '';
16
+ let inTag = false;
17
+ const push = (text, cls) => {
18
+ if (text)
19
+ toks.push([text, cls]);
20
+ };
21
+ while (i < src.length) {
22
+ const rest = src.slice(i);
23
+ let m;
24
+ if ((script || css) && (m = /^(\/\/.*|\/\*.*?(?:\*\/|$))/.exec(rest)) && !(css && rest.startsWith('//'))) {
25
+ push(m[0], 'k5');
26
+ }
27
+ else if ((m = /^("(?:\\.|[^"\\])*"?|'(?:\\.|[^'\\])*'?|`(?:\\.|[^`\\])*`?)/.exec(rest))) {
28
+ push(m[0], 'k3');
29
+ }
30
+ else if (css && (m = /^#[0-9a-fA-F]{3,8}\b/.exec(rest))) {
31
+ push(m[0], 'k1');
32
+ }
33
+ else if ((m = /^\d[\d_]*(?:\.\d+)?(?:[a-z%]+)?/.exec(rest)) && !/[\w$]/.test(src[i - 1] || '')) {
34
+ push(m[0], 'k1');
35
+ }
36
+ else if (jsx && (m = /^<\/?(?=[A-Za-z])/.exec(rest))) {
37
+ push(m[0], 'k5');
38
+ const name = /^[A-Za-z][\w.:-]*/.exec(src.slice(i + m[0].length));
39
+ if (name) {
40
+ push(name[0], 'k1');
41
+ i += name[0].length;
42
+ }
43
+ inTag = true;
44
+ }
45
+ else if (jsx && (m = /^\/?>/.exec(rest)) && src[i - 1] !== '=' && (inTag || /^\s*\/?>/.test(src.slice(0, i + m[0].length)))) {
46
+ push(m[0], 'k5');
47
+ inTag = false;
48
+ }
49
+ else if (jsx && (m = /^[A-Za-z_$][\w$]*(?:[-:][\w$]+)+(?=\s*=(?![=>]))/.exec(rest))) {
50
+ push(m[0], 'k2');
51
+ }
52
+ else if (css && (m = /^[\w-]+(?=\s*:)/.exec(rest))) {
53
+ push(m[0], 'k2');
54
+ }
55
+ else if ((m = /^[A-Za-z_$][\w$]*/.exec(rest))) {
56
+ const w = m[0];
57
+ const after = src.slice(i + w.length);
58
+ const before = src[i - 1] || '';
59
+ let cls = '';
60
+ if (css)
61
+ cls = '';
62
+ else if (KEYWORDS.has(w) && before !== '.')
63
+ cls = 'k4';
64
+ else if (/^\s*\(/.test(after))
65
+ cls = 'k2';
66
+ else if (/^\s*=(?![=>])/.test(after))
67
+ cls = prevWord === 'const' || prevWord === 'let' || prevWord === 'var' ? 'k6' : 'k2';
68
+ else if (prevWord === 'const' || prevWord === 'let' || prevWord === 'var' || prevWord === 'function')
69
+ cls = 'k6';
70
+ else if (before === '{' || /^\s*[}]/.test(after))
71
+ cls = 'k6';
72
+ push(w, cls);
73
+ prevWord = w;
74
+ i += w.length;
75
+ continue;
76
+ }
77
+ else {
78
+ push(src[i], '');
79
+ i++;
80
+ continue;
81
+ }
82
+ i += m[0].length;
83
+ }
84
+ return toks;
85
+ }
86
+ /** The [start, end) range of the word at a 1-based column, for the squiggle. */
87
+ export function wordAt(src, col) {
88
+ let s = Math.max(0, Math.min(src.length, col - 1));
89
+ if (s >= src.length)
90
+ return [Math.max(0, src.length - 1), src.length];
91
+ const isWord = (c) => /[\w$-]/.test(c);
92
+ if (!isWord(src[s]))
93
+ return [s, s + 1];
94
+ let e = s;
95
+ while (s > 0 && isWord(src[s - 1]))
96
+ s--;
97
+ while (e < src.length && isWord(src[e]))
98
+ e++;
99
+ return [s, e];
100
+ }
101
+ /** One highlighted line as HTML; `sq` underlines [start, end) with a squiggle. */
102
+ export function highlightLine(src, lang, sq) {
103
+ let out = '';
104
+ let pos = 0;
105
+ for (const [text, cls] of tokenize(src, lang)) {
106
+ const end = pos + text.length;
107
+ if (sq && sq[0] < end && sq[1] > pos) {
108
+ const a = Math.max(sq[0], pos) - pos;
109
+ const b = Math.min(sq[1], end) - pos;
110
+ const k = cls ? cls + ' ' : '';
111
+ if (a > 0)
112
+ out += span(text.slice(0, a), cls);
113
+ out += '<span class="' + k + 'sq">' + esc(text.slice(a, b)) + '</span>';
114
+ if (b < text.length)
115
+ out += span(text.slice(b), cls);
116
+ }
117
+ else {
118
+ out += span(text, cls);
119
+ }
120
+ pos = end;
121
+ }
122
+ return out;
123
+ }
124
+ function span(text, cls) {
125
+ return cls ? '<span class="' + cls + '">' + esc(text) + '</span>' : esc(text);
126
+ }
@@ -0,0 +1,4 @@
1
+ /** An inline icon; extra classes go on the svg element. */
2
+ export declare function icon(name: string, cls?: string): string;
3
+ /** The Krate mark. */
4
+ export declare function mark(cls?: string): string;
@@ -0,0 +1,57 @@
1
+ // Lucide icons (ISC license, https://lucide.dev), 24x24 geometry only; the
2
+ // svg element supplies stroke and fill.
3
+ const ICONS = {
4
+ 'x': '<path d="M18 6L6 18M6 6l12 12"/>',
5
+ 'chevron-left': '<path d="m15 18l-6-6l6-6"/>',
6
+ 'chevron-right': '<path d="m9 18l6-6l-6-6"/>',
7
+ 'chevron-down': '<path d="m6 9l6 6l6-6"/>',
8
+ 'check': '<path d="M20 6L9 17l-5-5"/>',
9
+ 'circle-x': '<circle cx="12" cy="12" r="10"/><path d="m15 9l-6 6m0-6l6 6"/>',
10
+ 'circle-exclamation-point': '<circle cx="12" cy="12" r="10"/><path d="M12 8v4m0 4h.01"/>',
11
+ 'triangle-exclamation-point': '<path d="m21.73 18l-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3M12 9v4m0 4h.01"/>',
12
+ 'palette': '<path d="M12 22a1 1 0 0 1 0-20a10 9 0 0 1 10 9a5 5 0 0 1-5 5h-2.25a1.75 1.75 0 0 0-1.4 2.8l.3.4a1.75 1.75 0 0 1-1.4 2.8z"/><circle cx="13.5" cy="6.5" r=".5" fill="currentColor"/><circle cx="17.5" cy="10.5" r=".5" fill="currentColor"/><circle cx="6.5" cy="12.5" r=".5" fill="currentColor"/><circle cx="8.5" cy="7.5" r=".5" fill="currentColor"/>',
13
+ 'zap': '<path d="M15.914 4a1.5 1.5 0 0 0-2.474-1.561l-9 9A1.5 1.5 0 0 0 5.5 14h4.002a.5.5 0 0 1 .471.666L8.086 20a1.5 1.5 0 0 0 2.475 1.56l9-9A1.5 1.5 0 0 0 18.5 10h-3.997a.5.5 0 0 1-.472-.667z"/>',
14
+ 'rotate-cw': '<path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/>',
15
+ 'refresh-cw': '<path d="M3 12a9 9 0 0 1 9-9a9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5m5 4a9 9 0 0 1-9 9a9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M8 16H3v5"/>',
16
+ 'square-arrow-up-right-exit': '<path d="M21 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h6m10 0l-9 9m3-9h6v6"/>',
17
+ 'copy': '<rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>',
18
+ 'lightbulb': '<path d="M15 14c.2-1 .7-1.7 1.5-2.5c1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5c.7.7 1.3 1.5 1.5 2.5m0 4h6m-5 4h4"/>',
19
+ 'wand-sparkles': '<path d="m21.64 3.64l-1.28-1.28a1.21 1.21 0 0 0-1.72 0L2.36 18.64a1.21 1.21 0 0 0 0 1.72l1.28 1.28a1.2 1.2 0 0 0 1.72 0L21.64 5.36a1.2 1.2 0 0 0 0-1.72M14 7l3 3M5 6v4m14 4v4M10 2v2M7 8H3m18 8h-4M11 3H9"/>',
20
+ 'mouse-pointer-2': '<path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/>',
21
+ 'search': '<path d="m21 21l-4.34-4.34"/><circle cx="11" cy="11" r="8"/>',
22
+ 'trash': '<path d="M10 11v6m4-6v6m5-11v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6M3 6h18M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>',
23
+ 'monitor': '<rect width="20" height="14" x="2" y="3" rx="2"/><path d="M8 21h8m-4-4v4"/>',
24
+ 'server': '<rect width="20" height="8" x="2" y="2" rx="2" ry="2"/><rect width="20" height="8" x="2" y="14" rx="2" ry="2"/><path d="M6 6h.01M6 18h.01"/>',
25
+ 'memory-stick': '<path d="M12 12v-2m0 8v-2m4-4v-2m0 8v-2M2 11h1.5M20 18v-2m.5-5H22M4 18v-2m4-4v-2m0 8v-2"/><rect width="20" height="10" x="2" y="6" rx="2"/>',
26
+ 'command': '<path d="M15 6v12a3 3 0 1 0 3-3H6a3 3 0 1 0 3 3V6a3 3 0 1 0-3 3h12a3 3 0 1 0-3-3"/>',
27
+ 'option': '<path d="M14 3h7M3 3h5.28a1 1 0 0 1 .948.684l5.544 16.632a1 1 0 0 0 .949.684H21"/>',
28
+ 'arrow-big-up': '<path d="M9 19a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1v-6a1 1 0 0 1 1-1h3.293a.707.707 0 0 0 .5-1.207l-7.086-7.086a1 1 0 0 0-1.414 0l-7.086 7.086a.707.707 0 0 0 .5 1.207H8a1 1 0 0 1 1 1z"/>',
29
+ 'corner-down-left': '<path d="M20 4v7a4 4 0 0 1-4 4H4"/><path d="m9 10l-5 5l5 5"/>',
30
+ 'file-code': '<path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z"/><path d="M14 2v5a1 1 0 0 0 1 1h5m-10 4.5L8 15l2 2.5m4-5l2 2.5l-2 2.5"/>',
31
+ 'link': '<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/>',
32
+ 'eye-off': '<path d="M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575a1 1 0 0 1 0 .696a10.8 10.8 0 0 1-1.444 2.49m-6.41-.679a3 3 0 0 1-4.242-4.242"/><path d="M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151a1 1 0 0 1 0-.696a10.75 10.75 0 0 1 4.446-5.143M2 2l20 20"/>',
33
+ 'info': '<circle cx="12" cy="12" r="10"/><path d="M12 16v-4m0-4h.01"/>',
34
+ 'activity': '<path d="M22 12h-2.48a2 2 0 0 0-1.93 1.46l-2.35 8.36a.25.25 0 0 1-.48 0L9.24 2.18a.25.25 0 0 0-.48 0l-2.35 8.36A2 2 0 0 1 4.49 12H2"/>',
35
+ 'plug-zap': '<path d="M6.3 20.3a2.4 2.4 0 0 0 3.4 0L12 18l-6-6l-2.3 2.3a2.4 2.4 0 0 0 0 3.4ZM2 22l3-3m2.5-5.5L10 11m.5 5.5L13 14m5-11l-4 4h6l-4 4"/>',
36
+ 'arrow-up': '<path d="m5 12l7-7l7 7m-7 7V5"/>',
37
+ 'arrow-down': '<path d="M12 5v14m7-7l-7 7l-7-7"/>',
38
+ 'scroll-text': '<path d="M15 12h-5m5-4h-5m9 9V5a2 2 0 0 0-2-2H4"/><path d="M8 21h12a2 2 0 0 0 2-2v-1a1 1 0 0 0-1-1H11a1 1 0 0 0-1 1v1a2 2 0 1 1-4 0V5a2 2 0 1 0-4 0v2a1 1 0 0 0 1 1h3"/>',
39
+ 'layers': '<path d="M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z"/><path d="M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12"/><path d="M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17"/>',
40
+ 'route': '<path d="M9 5h8.5a1 1 0 0 1 0 7h-11a1 1 0 0 0 0 7H15"/><circle cx="18" cy="19" r="3"/><circle cx="6" cy="5" r="3"/>',
41
+ 'settings': '<path d="M9.671 4.136a2.34 2.34 0 0 1 4.659 0a2.34 2.34 0 0 0 3.319 1.915a2.34 2.34 0 0 1 2.33 4.033a2.34 2.34 0 0 0 0 3.831a2.34 2.34 0 0 1-2.33 4.033a2.34 2.34 0 0 0-3.319 1.915a2.34 2.34 0 0 1-4.659 0a2.34 2.34 0 0 0-3.32-1.915a2.34 2.34 0 0 1-2.33-4.033a2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915"/><circle cx="12" cy="12" r="3"/>',
42
+ 'terminal': '<path d="M12 19h8M4 17l6-6l-6-6"/>',
43
+ };
44
+ /** An inline icon; extra classes go on the svg element. */
45
+ export function icon(name, cls = '') {
46
+ return ('<svg class="i' + (cls ? ' ' + cls : '') + '" viewBox="0 0 24 24" aria-hidden="true">' + (ICONS[name] || '') + '</svg>');
47
+ }
48
+ /** The Krate mark. */
49
+ export function mark(cls = 'mark') {
50
+ return ('<svg class="' +
51
+ cls +
52
+ '" viewBox="0 0 300 300" aria-hidden="true"><g transform="matrix(0.302678 0 0 0.302678 -3.911763 -0.279627)" fill="currentColor">' +
53
+ '<path fill-rule="evenodd" d="M219,67h579c77,0,140,63,140,140v579c0,77-63,140-140,140h-579c-77,0-140-63-140-140v-579c0-77,63-140,140-140Zm-5,85c-28,0-50,22-50,50v589c0,28,22,50,50,50h589c28,0,50-22,50-50v-589c0-28-22-50-50-50h-589Z"/>' +
54
+ '<path d="M222,182h121c16,0,29,13,29,29v154c0,9-4,16-11,23L237,511c-17,16-44,4-44-21v-279c0-16,13-29,29-29Z"/>' +
55
+ '<path d="M220,812c-15,0-27-13-27-28v-173c0-9,4-15,10-21L607,190c6-6,13-8,22-8h168c22,0,33,27,16,47L240,804c-6,6-12,8-20,8Z"/>' +
56
+ '<path d="M616,486c6,0,14,3,19,9L815,677c5,5,8,12,8,20v85c0,17-13,30-30,30h-81c-8,0-14-3-20-9L505,619c-11-11-10-24,1-35l90-91c5-5,12-7,20-7Z"/></g></svg>');
57
+ }
@@ -0,0 +1,55 @@
1
+ export type Tier = 'static' | 'server' | 'client' | 'runtime' | 'island';
2
+ export declare const TIERS: Tier[];
3
+ export interface CompRow {
4
+ n: number;
5
+ key?: string;
6
+ name: string;
7
+ tier: Tier;
8
+ file?: string;
9
+ line?: number;
10
+ parent: number;
11
+ island?: string;
12
+ region?: string;
13
+ boundary?: number;
14
+ }
15
+ export declare const TABLE_ID = "__krate_dev_components";
16
+ /** The component table of the page, or null when the build wrote none. */
17
+ export declare function readTable(doc?: Document): CompRow[] | null;
18
+ export declare function tierCounts(rows: CompRow[]): Record<Tier, number>;
19
+ /** The chain of instance names from the outermost to row, for the component path. */
20
+ export declare function componentPath(rows: CompRow[], row: CompRow): string[];
21
+ export interface SignalValue {
22
+ name: string;
23
+ value: unknown;
24
+ }
25
+ /** Current values of the signals a client instance registered. */
26
+ export declare function signalsOf(key: string | undefined, g?: any): SignalValue[];
27
+ /** Hydration time of an instance in ms, when the page recorded it. */
28
+ export declare function hydrationOf(row: CompRow, g?: any): number | undefined;
29
+ /** When an instance started hydrating, in ms after navigation start. */
30
+ export declare function hydrationStartOf(row: CompRow, g?: any): number | undefined;
31
+ /** A signal value as short text for the tooltip. */
32
+ export declare function formatValue(v: unknown): string;
33
+ /** Instance numbers of an element's data-kd attribute, outermost first. */
34
+ export declare function kdNumbers(el: Element): number[];
35
+ /** Maps instance numbers to the elements they mark. */
36
+ export declare function indexElements(doc?: Document): Map<number, Element[]>;
37
+ export interface CommentRange {
38
+ start: Comment;
39
+ end: Comment;
40
+ /** The JSON a krate-island comment carries, when it parses. */
41
+ data?: any;
42
+ }
43
+ /** The comment-delimited ranges of rows without an element of their own. */
44
+ export declare function commentRanges(rows: CompRow[], doc?: Document): Map<number, CommentRange>;
45
+ export interface Box {
46
+ left: number;
47
+ top: number;
48
+ width: number;
49
+ height: number;
50
+ }
51
+ /** The box of an element; a display:contents wrapper takes its children's. */
52
+ export declare function elementBox(el: Element): Box | null;
53
+ /** The box of the nodes between two comments. */
54
+ export declare function rangeBox(range: CommentRange): Box | null;
55
+ export declare function boxContains(b: Box, x: number, y: number): boolean;
@@ -0,0 +1,256 @@
1
+ // Component inspector data: the dev build's component table, the nodes each
2
+ // instance rendered, their rects, and the live signal and hydration globals.
3
+ export const TIERS = ['static', 'server', 'client', 'runtime', 'island'];
4
+ export const TABLE_ID = '__krate_dev_components';
5
+ /** The component table of the page, or null when the build wrote none. */
6
+ export function readTable(doc = document) {
7
+ const el = doc.getElementById(TABLE_ID);
8
+ if (!el)
9
+ return null;
10
+ try {
11
+ const rows = JSON.parse(el.textContent || '[]');
12
+ if (!Array.isArray(rows))
13
+ return null;
14
+ return rows.filter((r) => r && typeof r.name === 'string' && typeof r.n === 'number');
15
+ }
16
+ catch {
17
+ return null;
18
+ }
19
+ }
20
+ export function tierCounts(rows) {
21
+ const c = { static: 0, server: 0, client: 0, runtime: 0, island: 0 };
22
+ for (const r of rows)
23
+ if (r.tier in c)
24
+ c[r.tier]++;
25
+ return c;
26
+ }
27
+ /** The chain of instance names from the outermost to row, for the component path. */
28
+ export function componentPath(rows, row) {
29
+ const out = [];
30
+ const seen = new Set();
31
+ let cur = row;
32
+ while (cur && !seen.has(cur.n)) {
33
+ seen.add(cur.n);
34
+ out.unshift(cur.name);
35
+ cur = cur.parent >= 0 ? rows[cur.parent] : undefined;
36
+ }
37
+ return out;
38
+ }
39
+ /** Current values of the signals a client instance registered. */
40
+ export function signalsOf(key, g = globalThis) {
41
+ if (!key)
42
+ return [];
43
+ const list = g.__krate_dev_signals;
44
+ if (!Array.isArray(list))
45
+ return [];
46
+ const out = [];
47
+ for (const s of list) {
48
+ if (!s || s.c !== key)
49
+ continue;
50
+ let value;
51
+ try {
52
+ value = typeof s.get === 'function' ? s.get() : undefined;
53
+ }
54
+ catch {
55
+ value = undefined;
56
+ }
57
+ out.push({ name: String(s.name || '?'), value });
58
+ }
59
+ return out;
60
+ }
61
+ /** Hydration time of an instance in ms, when the page recorded it. */
62
+ export function hydrationOf(row, g = globalThis) {
63
+ if (row.key && g.__krate_dev_hydration && typeof g.__krate_dev_hydration[row.key] === 'number') {
64
+ return g.__krate_dev_hydration[row.key];
65
+ }
66
+ const isl = row.island && g.__krate_island_hydration ? g.__krate_island_hydration[row.island] : undefined;
67
+ if (isl && typeof isl.ms === 'number')
68
+ return isl.ms;
69
+ return undefined;
70
+ }
71
+ /** When an instance started hydrating, in ms after navigation start. */
72
+ export function hydrationStartOf(row, g = globalThis) {
73
+ const st = g.__krate_dev_hydration_start;
74
+ if (row.key && st && typeof st[row.key] === 'number')
75
+ return st[row.key];
76
+ const isl = row.island && g.__krate_island_hydration ? g.__krate_island_hydration[row.island] : undefined;
77
+ if (isl && typeof isl.start === 'number')
78
+ return isl.start;
79
+ return undefined;
80
+ }
81
+ /** A signal value as short text for the tooltip. */
82
+ export function formatValue(v) {
83
+ if (typeof v === 'string')
84
+ return JSON.stringify(v.length > 40 ? v.slice(0, 40) + '...' : v);
85
+ if (typeof v === 'function')
86
+ return 'fn';
87
+ if (v === undefined)
88
+ return 'undefined';
89
+ try {
90
+ const s = JSON.stringify(v);
91
+ if (s === undefined)
92
+ return String(v);
93
+ return s.length > 60 ? s.slice(0, 60) + '...' : s;
94
+ }
95
+ catch {
96
+ return String(v);
97
+ }
98
+ }
99
+ /** Instance numbers of an element's data-kd attribute, outermost first. */
100
+ export function kdNumbers(el) {
101
+ const v = el.getAttribute('data-kd');
102
+ if (!v)
103
+ return [];
104
+ return v
105
+ .split(/\s+/)
106
+ .map(Number)
107
+ .filter((n) => Number.isInteger(n) && n >= 0);
108
+ }
109
+ /** Maps instance numbers to the elements they mark. */
110
+ export function indexElements(doc = document) {
111
+ const map = new Map();
112
+ doc.querySelectorAll('[data-kd]').forEach((el) => {
113
+ for (const n of kdNumbers(el)) {
114
+ const list = map.get(n);
115
+ if (list)
116
+ list.push(el);
117
+ else
118
+ map.set(n, [el]);
119
+ }
120
+ });
121
+ return map;
122
+ }
123
+ function comments(root) {
124
+ const out = [];
125
+ const doc = root.ownerDocument || root;
126
+ const w = doc.createTreeWalker(root, 128 /* NodeFilter.SHOW_COMMENT */);
127
+ let n = w.nextNode();
128
+ while (n) {
129
+ out.push(n);
130
+ n = w.nextNode();
131
+ }
132
+ return out;
133
+ }
134
+ /** The sibling comment that closes `open`, skipping nested pairs. */
135
+ function closer(open, isOpen, isClose) {
136
+ let depth = 0;
137
+ for (let n = open.nextSibling; n; n = n.nextSibling) {
138
+ if (n.nodeType !== 8)
139
+ continue;
140
+ const d = n.data;
141
+ if (isOpen(d))
142
+ depth++;
143
+ else if (isClose(d)) {
144
+ if (depth === 0)
145
+ return n;
146
+ depth--;
147
+ }
148
+ }
149
+ return null;
150
+ }
151
+ /** The comment-delimited ranges of rows without an element of their own. */
152
+ export function commentRanges(rows, doc = document) {
153
+ const out = new Map();
154
+ const wantBoundary = rows.some((r) => typeof r.boundary === 'number');
155
+ const wantRegion = rows.some((r) => r.region);
156
+ if (!wantBoundary && !wantRegion)
157
+ return out;
158
+ const all = doc.body ? comments(doc.body) : [];
159
+ const islands = all.filter((c) => c.data.indexOf('krate-island:') === 0);
160
+ for (const r of rows) {
161
+ if (typeof r.boundary === 'number') {
162
+ const open = islands[r.boundary];
163
+ if (!open)
164
+ continue;
165
+ const end = closer(open, (d) => d.indexOf('krate-island:') === 0, (d) => d === '/krate-island');
166
+ if (!end)
167
+ continue;
168
+ let data;
169
+ try {
170
+ data = JSON.parse(open.data.slice('krate-island:'.length));
171
+ }
172
+ catch {
173
+ data = undefined;
174
+ }
175
+ out.set(r.n, { start: open, end, data });
176
+ }
177
+ else if (r.region) {
178
+ for (const prefix of ['region:', 'suspense:']) {
179
+ const open = all.find((c) => c.data === prefix + r.region);
180
+ if (!open)
181
+ continue;
182
+ const end = closer(open, () => false, (d) => d === '/' + prefix + r.region);
183
+ if (end) {
184
+ out.set(r.n, { start: open, end });
185
+ break;
186
+ }
187
+ }
188
+ }
189
+ }
190
+ return out;
191
+ }
192
+ function union(rects) {
193
+ const live = rects.filter((r) => r.width > 0 || r.height > 0);
194
+ if (!live.length)
195
+ return null;
196
+ let l = Infinity;
197
+ let t = Infinity;
198
+ let r = -Infinity;
199
+ let b = -Infinity;
200
+ for (const x of live) {
201
+ l = Math.min(l, x.left);
202
+ t = Math.min(t, x.top);
203
+ r = Math.max(r, x.left + x.width);
204
+ b = Math.max(b, x.top + x.height);
205
+ }
206
+ return { left: l, top: t, width: r - l, height: b - t };
207
+ }
208
+ /** The box of an element; a display:contents wrapper takes its children's. */
209
+ export function elementBox(el) {
210
+ const r = el.getBoundingClientRect();
211
+ if (r.width > 0 || r.height > 0)
212
+ return { left: r.left, top: r.top, width: r.width, height: r.height };
213
+ let display = '';
214
+ try {
215
+ display = getComputedStyle(el).display;
216
+ }
217
+ catch {
218
+ display = '';
219
+ }
220
+ if (display !== 'contents' && el.children.length === 0)
221
+ return null;
222
+ const kids = [];
223
+ for (const c of Array.from(el.children)) {
224
+ const b = elementBox(c);
225
+ if (b)
226
+ kids.push(b);
227
+ }
228
+ return union(kids);
229
+ }
230
+ /** The box of the nodes between two comments. */
231
+ export function rangeBox(range) {
232
+ try {
233
+ const doc = range.start.ownerDocument;
234
+ const rg = doc.createRange();
235
+ rg.setStartAfter(range.start);
236
+ rg.setEndBefore(range.end);
237
+ const r = rg.getBoundingClientRect();
238
+ if (r && (r.width > 0 || r.height > 0))
239
+ return { left: r.left, top: r.top, width: r.width, height: r.height };
240
+ }
241
+ catch {
242
+ /* fall back to the elements in between */
243
+ }
244
+ const kids = [];
245
+ for (let n = range.start.nextSibling; n && n !== range.end; n = n.nextSibling) {
246
+ if (n.nodeType === 1) {
247
+ const b = elementBox(n);
248
+ if (b)
249
+ kids.push(b);
250
+ }
251
+ }
252
+ return union(kids);
253
+ }
254
+ export function boxContains(b, x, y) {
255
+ return x >= b.left && x <= b.left + b.width && y >= b.top && y <= b.top + b.height;
256
+ }