@krate/runtime 0.5.0-rc.14 → 0.5.0-rc.15

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 (61) hide show
  1. package/dist/action-context.js +5 -1
  2. package/dist/dev/api.d.ts +93 -0
  3. package/dist/dev/api.js +2 -0
  4. package/dist/dev/app.d.ts +165 -0
  5. package/dist/dev/app.js +1204 -0
  6. package/dist/dev/commands.d.ts +49 -0
  7. package/dist/dev/commands.js +114 -0
  8. package/dist/dev/highlight.d.ts +4 -0
  9. package/dist/dev/highlight.js +126 -0
  10. package/dist/dev/icons.d.ts +4 -0
  11. package/dist/dev/icons.js +57 -0
  12. package/dist/dev/inspect.d.ts +55 -0
  13. package/dist/dev/inspect.js +256 -0
  14. package/dist/dev/inspector.d.ts +39 -0
  15. package/dist/dev/inspector.js +348 -0
  16. package/dist/dev/issues.d.ts +83 -0
  17. package/dist/dev/issues.js +166 -0
  18. package/dist/dev/modal.d.ts +16 -0
  19. package/dist/dev/modal.js +836 -0
  20. package/dist/dev/native.d.ts +53 -0
  21. package/dist/dev/native.js +95 -0
  22. package/dist/dev/palette.d.ts +9 -0
  23. package/dist/dev/palette.js +250 -0
  24. package/dist/dev/panel.d.ts +31 -0
  25. package/dist/dev/panel.js +848 -0
  26. package/dist/dev/requests.d.ts +36 -0
  27. package/dist/dev/requests.js +150 -0
  28. package/dist/dev/settings.d.ts +32 -0
  29. package/dist/dev/settings.js +78 -0
  30. package/dist/dev/sourcemap.d.ts +22 -0
  31. package/dist/dev/sourcemap.js +148 -0
  32. package/dist/dev/stack.d.ts +38 -0
  33. package/dist/dev/stack.js +115 -0
  34. package/dist/dev/styles.d.ts +1 -0
  35. package/dist/dev/styles.js +424 -0
  36. package/dist/dev/util.d.ts +63 -0
  37. package/dist/dev/util.js +190 -0
  38. package/dist/dev-overlay.d.ts +9 -83
  39. package/dist/dev-overlay.js +17 -1038
  40. package/dist/island-swap.js +7 -1
  41. package/dist/krate-core.js +1 -1
  42. package/dist/krate-dev.js +441 -871
  43. package/dist/krate-f-list.js +1 -1
  44. package/dist/krate-f-navigation.js +1 -1
  45. package/dist/krate-hydrate-lite.js +5 -5
  46. package/dist/krate-hydrate.js +5 -5
  47. package/dist/krate-island-swap.js +1 -1
  48. package/dist/krate-runtime.js +5 -5
  49. package/dist/list.js +23 -1
  50. package/dist/metadata.js +23 -12
  51. package/dist/request-context.d.ts +10 -0
  52. package/dist/request-context.js +24 -0
  53. package/dist/server-handler.d.ts +2 -0
  54. package/dist/server-handler.js +1 -1
  55. package/dist/server-renderer.js +6 -2
  56. package/dist/server.js +2 -2
  57. package/dist/signal.d.ts +8 -0
  58. package/dist/signal.js +4 -0
  59. package/dist/ssr-core.d.ts +3 -0
  60. package/dist/ssr-core.js +44 -5
  61. package/package.json +1 -1
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Splits a buffer of Krate frames (u32 length, JSON, u16 blob count, blobs)
3
+ * into its complete JSON values. `rest` is the incomplete tail to prepend to
4
+ * the next chunk. Exported (pure) for testing.
5
+ */
6
+ export declare function parseFrames(buf: Uint8Array): {
7
+ values: unknown[];
8
+ rest: Uint8Array;
9
+ };
10
+ /** The host-side stack a KrateError carries in dev (`data.stack`), or "". */
11
+ export declare function hostStackOf(reason: unknown): string;
12
+ export interface HostEvent {
13
+ seq: number;
14
+ kind: string;
15
+ fn: string;
16
+ id?: number;
17
+ time?: string;
18
+ durNs?: number;
19
+ code?: string;
20
+ message?: string;
21
+ }
22
+ /** One line of the Host panel. Exported (pure) for testing. */
23
+ export declare function hostEventLine(ev: HostEvent): string;
24
+ /** The memstat report the shell serves at /__krate/dev/memory (bytes). */
25
+ export interface MemoryReport {
26
+ os?: string;
27
+ source?: string;
28
+ procs?: {
29
+ pid: number;
30
+ name?: string;
31
+ role: string;
32
+ rss?: number;
33
+ pss: number;
34
+ privateDirty: number;
35
+ }[];
36
+ total?: {
37
+ pss: number;
38
+ privateDirty: number;
39
+ rss?: number;
40
+ };
41
+ notes?: string[];
42
+ }
43
+ /** Format a byte count for the Memory panel. Exported (pure) for testing. */
44
+ export declare function formatBytes(n: number): string;
45
+ /**
46
+ * The JS heap of this page, or null where the engine does not expose it.
47
+ * Chromium has performance.memory; WebKit (the engine of the Linux and macOS
48
+ * webviews) has no heap size API, so the panel shows the web process row
49
+ * (PSS and private dirty, which include the JS heap) instead.
50
+ */
51
+ export declare function jsHeapBytes(): number | null;
52
+ /** The rows of the Memory panel as [label, value] pairs. Exported (pure) for testing. */
53
+ export declare function memoryRows(rep: MemoryReport | null, domNodes: number, heap: number | null): [string, string][];
@@ -0,0 +1,95 @@
1
+ // Krate Native helpers: the frame stream, the Host feed and the Memory tab.
2
+ /**
3
+ * Splits a buffer of Krate frames (u32 length, JSON, u16 blob count, blobs)
4
+ * into its complete JSON values. `rest` is the incomplete tail to prepend to
5
+ * the next chunk. Exported (pure) for testing.
6
+ */
7
+ export function parseFrames(buf) {
8
+ const values = [];
9
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
10
+ const td = new TextDecoder();
11
+ let pos = 0;
12
+ while (buf.length - pos >= 4) {
13
+ const n = view.getUint32(pos);
14
+ let end = pos + 4 + n;
15
+ if (buf.length < end + 2)
16
+ break;
17
+ const json = td.decode(buf.subarray(pos + 4, end));
18
+ const blobs = view.getUint16(end);
19
+ end += 2;
20
+ let complete = true;
21
+ for (let i = 0; i < blobs; i++) {
22
+ if (buf.length < end + 4) {
23
+ complete = false;
24
+ break;
25
+ }
26
+ end += 4 + view.getUint32(end);
27
+ if (buf.length < end) {
28
+ complete = false;
29
+ break;
30
+ }
31
+ }
32
+ if (!complete)
33
+ break;
34
+ try {
35
+ values.push(JSON.parse(json));
36
+ }
37
+ catch {
38
+ /* skip a malformed frame */
39
+ }
40
+ pos = end;
41
+ }
42
+ return { values, rest: buf.subarray(pos) };
43
+ }
44
+ /** The host-side stack a KrateError carries in dev (`data.stack`), or "". */
45
+ export function hostStackOf(reason) {
46
+ const data = reason?.data;
47
+ const stack = data && typeof data === 'object' ? data.stack : undefined;
48
+ return typeof stack === 'string' ? stack : '';
49
+ }
50
+ /** One line of the Host panel. Exported (pure) for testing. */
51
+ export function hostEventLine(ev) {
52
+ const t = ev.time ? new Date(ev.time).toLocaleTimeString() : '';
53
+ const dur = ev.durNs ? (ev.durNs / 1e6 >= 10 ? Math.round(ev.durNs / 1e6) : Math.round(ev.durNs / 1e5) / 10) + 'ms' : '';
54
+ const err = ev.code ? ' ' + ev.code + (ev.message ? ': ' + ev.message : '') : '';
55
+ return [t, ev.kind, ev.fn, dur].filter(Boolean).join(' ') + err;
56
+ }
57
+ /** Format a byte count for the Memory panel. Exported (pure) for testing. */
58
+ export function formatBytes(n) {
59
+ if (!Number.isFinite(n) || n < 0)
60
+ return 'n/a';
61
+ if (n >= 1 << 30)
62
+ return (n / (1 << 30)).toFixed(2) + ' GiB';
63
+ if (n >= 1 << 20)
64
+ return (n / (1 << 20)).toFixed(1) + ' MiB';
65
+ if (n >= 1 << 10)
66
+ return Math.round(n / (1 << 10)) + ' KiB';
67
+ return Math.round(n) + ' B';
68
+ }
69
+ /**
70
+ * The JS heap of this page, or null where the engine does not expose it.
71
+ * Chromium has performance.memory; WebKit (the engine of the Linux and macOS
72
+ * webviews) has no heap size API, so the panel shows the web process row
73
+ * (PSS and private dirty, which include the JS heap) instead.
74
+ */
75
+ export function jsHeapBytes() {
76
+ const m = (typeof performance !== 'undefined' ? performance.memory : undefined);
77
+ return m && typeof m.usedJSHeapSize === 'number' ? m.usedJSHeapSize : null;
78
+ }
79
+ /** The rows of the Memory panel as [label, value] pairs. Exported (pure) for testing. */
80
+ export function memoryRows(rep, domNodes, heap) {
81
+ const rows = [];
82
+ if (rep && rep.procs) {
83
+ for (const p of rep.procs) {
84
+ rows.push([p.role + ' (' + p.pid + ')', 'PSS ' + formatBytes(p.pss) + ' / private dirty ' + formatBytes(p.privateDirty)]);
85
+ }
86
+ if (rep.total)
87
+ rows.push(['total', 'PSS ' + formatBytes(rep.total.pss) + ' / private dirty ' + formatBytes(rep.total.privateDirty)]);
88
+ }
89
+ else {
90
+ rows.push(['processes', 'unavailable']);
91
+ }
92
+ rows.push(['DOM nodes', String(domNodes)]);
93
+ rows.push(['JS heap', heap === null ? 'unavailable in this engine (see the web process row)' : formatBytes(heap)]);
94
+ return rows;
95
+ }
@@ -0,0 +1,9 @@
1
+ import type { KrateDev } from './app.js';
2
+ import { type PaletteItem } from './commands.js';
3
+ /** The dev actions the menu offers on this page. */
4
+ export declare function actionItems(app: KrateDev): PaletteItem[];
5
+ export declare function openPalette(app: KrateDev): void;
6
+ export declare function closePalette(app: KrateDev): void;
7
+ /** Keys while the menu is open; true when handled. */
8
+ export declare function paletteKey(app: KrateDev, e: KeyboardEvent): boolean;
9
+ export declare function paletteClick(app: KrateDev, act: string, t: HTMLElement, e: MouseEvent): boolean;
@@ -0,0 +1,250 @@
1
+ // The command menu (Alt+K): routes and dev actions.
2
+ import { filterItems, markRanges, routeItems } from './commands.js';
3
+ import { icon } from './icons.js';
4
+ import { readTable } from './inspect.js';
5
+ import { kbd } from './settings.js';
6
+ import { esc, isMac } from './util.js';
7
+ const state = new WeakMap();
8
+ /** The dev actions the menu offers on this page. */
9
+ export function actionItems(app) {
10
+ const items = [];
11
+ const add = (action, label, ic, keys) => items.push({ id: 'action:' + action, group: 'Actions', label, icon: ic, keys, action });
12
+ const page = app.pageRoute();
13
+ if (readTable())
14
+ add('inspect', 'Toggle component inspector', 'mouse-pointer-2', ['Alt', 'I']);
15
+ if (app.visibleIssues().length)
16
+ add('issues', 'Show issues', 'triangle-exclamation-point', ['Alt', 'E']);
17
+ if (app.cfg.api) {
18
+ add('rebuild-page', 'Rebuild this page', 'rotate-cw');
19
+ add('rebuild-all', 'Rebuild everything', 'refresh-cw');
20
+ add('restart-server', 'Restart dev server', 'monitor');
21
+ add('clear-cache', 'Clear .krate cache and rebuild', 'trash');
22
+ }
23
+ if (page.source)
24
+ add('open-source', 'Open page source', 'file-code');
25
+ if (app.cfg.toolbar)
26
+ add('settings', 'Open settings', 'settings');
27
+ add('hide', 'Hide the overlay', 'eye-off', ['Alt', '.']);
28
+ return items;
29
+ }
30
+ export function openPalette(app) {
31
+ if (app.pal && !app.pal.hidden) {
32
+ closePalette(app);
33
+ return;
34
+ }
35
+ if (!app.pal) {
36
+ const wrap = document.createElement('div');
37
+ wrap.className = 'pal-wrap';
38
+ wrap.innerHTML =
39
+ '<div class="pal" role="dialog" aria-modal="true" aria-label="Command menu"><div class="pal-in">' +
40
+ icon('search') +
41
+ '<input type="text" role="combobox" aria-expanded="true" aria-controls="kd-pal-list" aria-autocomplete="list" placeholder="Go to a route or run an action" autocomplete="off" spellcheck="false">' +
42
+ '<span class="kbd">Esc</span></div><div class="pal-list" id="kd-pal-list" role="listbox"></div><div class="pal-f"></div></div>';
43
+ app.root.appendChild(wrap);
44
+ app.pal = wrap;
45
+ const input = wrap.querySelector('input');
46
+ input.addEventListener('input', () => {
47
+ const st = state.get(app);
48
+ st.query = input.value;
49
+ st.sel = 0;
50
+ renderList(app);
51
+ });
52
+ wrap.addEventListener('mousemove', (e) => {
53
+ const el = e.target.closest('.pi');
54
+ if (!el)
55
+ return;
56
+ const n = Number(el.dataset.n);
57
+ const st = state.get(app);
58
+ if (n !== st.sel) {
59
+ st.sel = n;
60
+ mark(app);
61
+ }
62
+ });
63
+ }
64
+ const mac = isMac();
65
+ app.pal.querySelector('.pal-f').innerHTML =
66
+ '<span><span class="kbd">' +
67
+ icon('arrow-up') +
68
+ '</span><span class="kbd">' +
69
+ icon('arrow-down') +
70
+ '</span>move</span><span>' +
71
+ kbd(['Enter'], mac) +
72
+ 'open</span><span>' +
73
+ kbd(['Mod', 'Enter'], mac) +
74
+ 'open source file</span><span class="grow"></span><span>' +
75
+ kbd(['Alt', 'K'], mac) +
76
+ 'toggle</span>';
77
+ state.set(app, { query: '', sel: 0, shown: [] });
78
+ const input = app.pal.querySelector('input');
79
+ input.value = '';
80
+ app.pal.hidden = false;
81
+ renderList(app);
82
+ try {
83
+ input.focus({ preventScroll: true });
84
+ }
85
+ catch {
86
+ /* focus is a nicety */
87
+ }
88
+ if (app.routes === undefined && app.cfg.api) {
89
+ void app.loadRoutes().then(() => {
90
+ if (app.pal && !app.pal.hidden)
91
+ renderList(app);
92
+ });
93
+ }
94
+ }
95
+ export function closePalette(app) {
96
+ if (app.pal)
97
+ app.pal.hidden = true;
98
+ }
99
+ function renderList(app) {
100
+ const st = state.get(app);
101
+ const box = app.pal?.querySelector('.pal-list');
102
+ if (!st || !box)
103
+ return;
104
+ const items = [...routeItems(app.routes || [], st.query), ...actionItems(app)];
105
+ const res = filterItems(items, st.query);
106
+ st.shown = res.map((r) => r.item);
107
+ if (st.sel >= st.shown.length)
108
+ st.sel = Math.max(0, st.shown.length - 1);
109
+ if (!res.length) {
110
+ box.innerHTML = '<div class="pal-empty">' + (app.routes === undefined && app.cfg.api ? 'Loading routes' : 'Nothing matches') + '</div>';
111
+ return;
112
+ }
113
+ let html = '';
114
+ let group = '';
115
+ res.forEach(({ item, match }, n) => {
116
+ if (item.group !== group) {
117
+ if (group)
118
+ html += '</div>';
119
+ html += '<div class="pal-g" role="group" aria-label="' + item.group + '"><div class="lbl">' + item.group + '</div>';
120
+ group = item.group;
121
+ }
122
+ html +=
123
+ '<button class="pi' +
124
+ (item.sub ? ' sub' : '') +
125
+ '" type="button" role="option" id="kd-pi-' +
126
+ n +
127
+ '" data-act="pal-run" data-n="' +
128
+ n +
129
+ '" aria-selected="' +
130
+ (n === st.sel) +
131
+ '">' +
132
+ (item.chip ? '<span class="mode ' + esc(item.chipClass || '') + '">' + esc(item.chip) + '</span>' : '') +
133
+ (item.icon ? icon(item.icon) : '') +
134
+ '<span class="name' +
135
+ (item.mono ? ' mono' : '') +
136
+ '">' +
137
+ markRanges(item.label, match.ranges) +
138
+ '</span><span class="r">' +
139
+ (item.right ? esc(item.right) : '') +
140
+ (item.keys ? kbd(item.keys) : '') +
141
+ (item.href ? kbd(['Enter']).replace('class="kbd"', 'class="kbd ent"') : '') +
142
+ '</span></button>';
143
+ });
144
+ html += '</div>';
145
+ box.innerHTML = html;
146
+ mark(app);
147
+ }
148
+ function mark(app) {
149
+ const st = state.get(app);
150
+ if (!st || !app.pal)
151
+ return;
152
+ app.pal.querySelectorAll('.pi').forEach((el) => {
153
+ const on = Number(el.dataset.n) === st.sel;
154
+ el.setAttribute('aria-selected', String(on));
155
+ if (on)
156
+ el.scrollIntoView?.({ block: 'nearest' });
157
+ });
158
+ app.pal.querySelector('input')?.setAttribute('aria-activedescendant', 'kd-pi-' + st.sel);
159
+ }
160
+ function run(app, item, source) {
161
+ if (!item)
162
+ return;
163
+ if (source) {
164
+ const file = item.source || (item.action === 'open-source' ? app.pageRoute().source : undefined);
165
+ if (file) {
166
+ closePalette(app);
167
+ void app.openInEditor(file);
168
+ }
169
+ return;
170
+ }
171
+ closePalette(app);
172
+ if (item.group === 'Routes') {
173
+ if (!item.href) {
174
+ app.toast('No prebuilt path for this route', item.label, 'x');
175
+ return;
176
+ }
177
+ const base = app.cfg.basePath || '';
178
+ const url = base + item.href;
179
+ if (item.chip === 'API')
180
+ window.open(url, '_blank', 'noopener');
181
+ else
182
+ location.href = url;
183
+ return;
184
+ }
185
+ switch (item.action) {
186
+ case 'inspect':
187
+ app.inspector.toggle();
188
+ break;
189
+ case 'issues':
190
+ app.openModal();
191
+ break;
192
+ case 'rebuild-page':
193
+ void app.runAction('rebuild-page', { route: location.pathname });
194
+ break;
195
+ case 'rebuild-all':
196
+ case 'restart-server':
197
+ case 'clear-cache':
198
+ void app.runAction(item.action);
199
+ break;
200
+ case 'open-source':
201
+ void app.openInEditor(app.pageRoute().source);
202
+ break;
203
+ case 'settings':
204
+ app.togglePanel('settings', true);
205
+ break;
206
+ case 'hide':
207
+ app.toggleHidden();
208
+ break;
209
+ }
210
+ }
211
+ /** Keys while the menu is open; true when handled. */
212
+ export function paletteKey(app, e) {
213
+ const st = state.get(app);
214
+ if (!st)
215
+ return false;
216
+ if (e.altKey && e.code === 'KeyK') {
217
+ e.preventDefault();
218
+ closePalette(app);
219
+ return true;
220
+ }
221
+ switch (e.key) {
222
+ case 'Escape':
223
+ e.preventDefault();
224
+ e.stopPropagation();
225
+ closePalette(app);
226
+ return true;
227
+ case 'ArrowDown':
228
+ e.preventDefault();
229
+ st.sel = Math.min(st.shown.length - 1, st.sel + 1);
230
+ mark(app);
231
+ return true;
232
+ case 'ArrowUp':
233
+ e.preventDefault();
234
+ st.sel = Math.max(0, st.sel - 1);
235
+ mark(app);
236
+ return true;
237
+ case 'Enter':
238
+ e.preventDefault();
239
+ run(app, st.shown[st.sel], e.metaKey || e.ctrlKey);
240
+ return true;
241
+ }
242
+ return false;
243
+ }
244
+ export function paletteClick(app, act, t, e) {
245
+ if (act !== 'pal-run')
246
+ return false;
247
+ const st = state.get(app);
248
+ run(app, st?.shown[Number(t.dataset.n)], e.metaKey || e.ctrlKey);
249
+ return true;
250
+ }
@@ -0,0 +1,31 @@
1
+ import type { KrateDev } from './app.js';
2
+ import type { BuildRecord, LogLine, RouteData } from './api.js';
3
+ import { type CompRow } from './inspect.js';
4
+ export type Tab = 'route' | 'inspect' | 'activity' | 'logs' | 'settings' | 'host' | 'memory';
5
+ export declare function visibleTabs(app: KrateDev): Tab[];
6
+ /** Renders the panel head, tabs and body; `headOnly` refreshes just the tab badges. */
7
+ export declare function renderPanel(app: KrateDev, headOnly?: boolean): void;
8
+ export declare function renderPanelBody(app: KrateDev): void;
9
+ /** The islands of the page: top-level client instances, external islands and runtime regions. */
10
+ export declare function pageIslands(rows: CompRow[]): CompRow[];
11
+ export interface HeadCheck {
12
+ name: string;
13
+ ok: boolean;
14
+ missing: string;
15
+ }
16
+ export declare function headChecks(doc?: Document): HeadCheck[];
17
+ /**
18
+ * checks.budget against the page measured the same way the checks measure
19
+ * it: raw bytes, not gzip. Dev builds are not minified, so production is
20
+ * smaller than what this shows.
21
+ */
22
+ export declare function budgetHTML(r: RouteData | null | undefined): string;
23
+ export declare function buildHeadline(b: BuildRecord): string;
24
+ export declare function buildDetail(b: BuildRecord, later: BuildRecord[], route: string): string;
25
+ export declare function mergedLogs(app: KrateDev): LogLine[];
26
+ export declare function filterLogs(lines: LogLine[], filter: KrateDev['logFilter'], query: string, errorsOnly: boolean): LogLine[];
27
+ export declare function refreshLogs(app: KrateDev): void;
28
+ export declare function stopPanelTimers(app: KrateDev): void;
29
+ /** Polls what the open tab shows: logs every second, host calls every second, memory every 2 seconds. */
30
+ export declare function startPanelTimers(app: KrateDev): void;
31
+ export declare function panelClick(app: KrateDev, act: string, t: HTMLElement, _e: MouseEvent): boolean;