@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,836 @@
1
+ // The error overlay: an issue rail, a pager and one issue in detail (build
2
+ // diagnostics, runtime errors, server crashes).
3
+ import { highlightLine, wordAt } from './highlight.js';
4
+ import { icon, mark } from './icons.js';
5
+ import { componentPath, readTable } from './inspect.js';
6
+ import { copyForAI, issueSummary } from './issues.js';
7
+ import { NEAR_BEFORE_MS, displayURL, rawFetch } from './requests.js';
8
+ import { parseStack, splitStack } from './stack.js';
9
+ import { basename, clock, copyText, dirname, esc, plural, richText } from './util.js';
10
+ const KIND_LABEL = { build: 'Build', server: 'Server', runtime: 'Runtime' };
11
+ function frameKey(i) {
12
+ return (i.file || '') + ':' + (i.line || 0);
13
+ }
14
+ function fixKey(i) {
15
+ return (i.file || '') + '|' + (i.code || '');
16
+ }
17
+ function isProjectFile(f) {
18
+ return !!f && !/^[a-z][a-z0-9+.-]*:\/\//i.test(f) && !/^(chunks|__krate)\//.test(f);
19
+ }
20
+ function canOpen(i) {
21
+ return isProjectFile(i.file) && !i.served;
22
+ }
23
+ const SOURCE_NAME = {
24
+ 'SSR renderer': 'SSR sidecar',
25
+ 'Node API sidecar': 'API sidecar',
26
+ 'Go API sidecar': 'Go API sidecar',
27
+ };
28
+ function title(i, all) {
29
+ if (i.kind === 'build') {
30
+ const errs = all.filter((x) => x.kind === 'build' && x.severity === 'error').length;
31
+ return errs ? ['Build failed', 'red'] : ['Build warnings', 'amber'];
32
+ }
33
+ if (i.kind === 'server') {
34
+ const src = i.server?.source || 'Server';
35
+ const name = SOURCE_NAME[src] || src;
36
+ const crashed = i.server?.exitCode !== undefined || /exited|crash/i.test(i.server?.detail || i.message);
37
+ return [name + (crashed ? ' crashed' : ' failed'), 'red'];
38
+ }
39
+ switch (i.origin) {
40
+ case 'console':
41
+ return ['Console error', 'amber'];
42
+ case 'resource':
43
+ return ['Failed to load a resource', 'red'];
44
+ case 'rejection':
45
+ return ['Unhandled promise rejection', 'red'];
46
+ }
47
+ return ['Unhandled runtime error', 'red'];
48
+ }
49
+ /** The component instance whose source holds the error location, for the path crumbs. */
50
+ function componentAt(app, i) {
51
+ if (!i.file)
52
+ return [];
53
+ const rows = readTable() || [];
54
+ const scope = app.frames.get(frameKey(i))?.scope;
55
+ const byKey = scope ? rows.find((r) => r.key === scope) : undefined;
56
+ if (byKey)
57
+ return componentPath(rows, byKey);
58
+ let best;
59
+ for (const r of rows) {
60
+ if (!r.file || !(i.file.endsWith(r.file) || r.file.endsWith(i.file)))
61
+ continue;
62
+ if (r.line && i.line && r.line > i.line)
63
+ continue;
64
+ if (!best || (r.line || 0) > (best.line || 0))
65
+ best = r;
66
+ }
67
+ return best ? componentPath(rows, best) : [];
68
+ }
69
+ function sub(app, i, list) {
70
+ const t = clock(i.time);
71
+ if (i.kind === 'build')
72
+ return issueSummary(list.filter((x) => x.kind === 'build')) + ' \u00B7 ' + t;
73
+ if (i.kind === 'server')
74
+ return serverSub(i, t);
75
+ const path = componentAt(app, i);
76
+ return 'in the browser' + (path.length ? ' while rendering ' + path[path.length - 1] : '') + ' \u00B7 ' + t;
77
+ }
78
+ const TIMES = ['', 'once', 'twice'];
79
+ /** "node 22.11 · exited with code 1 · 12:44:30 · restarted twice in the last minute". */
80
+ export function serverSub(i, t) {
81
+ const s = i.server;
82
+ const parts = [];
83
+ if (s?.runtime)
84
+ parts.push(s.runtime);
85
+ if (s?.exitCode !== undefined)
86
+ parts.push('exited with code ' + s.exitCode);
87
+ if (i.count > 1 && !s?.restarts)
88
+ parts.push('reported ' + plural(i.count, 'time'));
89
+ parts.push(t);
90
+ const n = s?.restarts || 0;
91
+ if (n > 0 && s?.restart)
92
+ parts.push('restarted ' + (TIMES[n] || n + ' times') + ' in the last minute');
93
+ return parts.join(' \u00B7 ');
94
+ }
95
+ function railHTML(app, list) {
96
+ let html = '';
97
+ let last = '';
98
+ list.forEach((i, n) => {
99
+ if (i.kind !== last) {
100
+ html += '<div class="lbl">' + KIND_LABEL[i.kind] + '</div>';
101
+ last = i.kind;
102
+ }
103
+ html +=
104
+ '<button class="issue ' +
105
+ (i.severity === 'error' ? 'e' : 'w') +
106
+ '" type="button" data-act="goto" data-n="' +
107
+ n +
108
+ '" aria-current="' +
109
+ (n === app.cur) +
110
+ '"><div class="top1"><span class="mono">' +
111
+ esc(i.label) +
112
+ '</span>' +
113
+ (i.severity === 'warning' && i.kind === 'build' ? '<span>warning</span>' : '') +
114
+ (i.count > 1 ? '<span class="cnt">x' + i.count + '</span>' : '') +
115
+ '</div><div class="msg">' +
116
+ richText(i.message.split('\n')[0]) +
117
+ '</div>' +
118
+ (i.file ? '<div class="file">' + esc(basename(i.file)) + (i.line ? ':' + i.line : '') + '</div>' : '') +
119
+ '</button>';
120
+ });
121
+ return html;
122
+ }
123
+ function pathHTML(i, shown) {
124
+ const f = shown || i.file || '';
125
+ return ('<span class="path">' +
126
+ esc(dirname(f)) +
127
+ '<b>' +
128
+ esc(basename(f)) +
129
+ '</b>' +
130
+ (i.line ? ':' + i.line + (i.col ? ':' + i.col : '') : '') +
131
+ '</span>');
132
+ }
133
+ /** The changed line of a fix: `from` replaced at its column, or its first occurrence. */
134
+ export function applyChange(line, c) {
135
+ const at = c.col - 1;
136
+ if (at >= 0 && line.slice(at, at + c.from.length) === c.from)
137
+ return line.slice(0, at) + c.to + line.slice(at + c.from.length);
138
+ const k = line.indexOf(c.from);
139
+ return k === -1 ? line : line.slice(0, k) + c.to + line.slice(k + c.from.length);
140
+ }
141
+ export function codeFrameHTML(frame, i, note = '') {
142
+ let lines = [];
143
+ let start = 1;
144
+ let lang = 'text';
145
+ let file = i.file || '';
146
+ if (frame && frame.lines.length) {
147
+ lines = frame.lines;
148
+ start = frame.start;
149
+ lang = frame.lang || 'text';
150
+ file = frame.file || file;
151
+ }
152
+ else if (i.source) {
153
+ lines = [i.source];
154
+ start = i.line || 1;
155
+ lang = /\.(tsx|ts|jsx|js|css|go|mdx)$/.exec(file)?.[1] || 'text';
156
+ }
157
+ else {
158
+ return '';
159
+ }
160
+ let html = '<div class="cf' +
161
+ (i.kind === 'build' && i.severity === 'warning' ? ' w' : '') +
162
+ '"><div class="cf-bar"><span>' +
163
+ esc(basename(file)) +
164
+ '</span>' +
165
+ (i.served ? '<span>built output, no source map</span>' : '') +
166
+ '<span class="grow"></span><span>' +
167
+ esc(lang.toUpperCase()) +
168
+ '</span></div>';
169
+ lines.forEach((text, k) => {
170
+ const n = start + k;
171
+ const hl = n === i.line;
172
+ const sq = hl && i.col ? wordAt(text, i.col) : undefined;
173
+ html +=
174
+ '<div class="cf-l' +
175
+ (hl ? ' hl' : '') +
176
+ '"><span class="n">' +
177
+ n +
178
+ '</span><span class="c">' +
179
+ highlightLine(text, lang, sq) +
180
+ '</span></div>';
181
+ if (hl && sq && note) {
182
+ html +=
183
+ '<div class="cf-l ann"><span class="n"></span><span class="c">' +
184
+ esc(' '.repeat(sq[0]) + '^'.repeat(Math.max(1, sq[1] - sq[0])) + (note ? ' ' + note : '')) +
185
+ '</span></div>';
186
+ }
187
+ });
188
+ return html + '</div>';
189
+ }
190
+ function msgHTML(html, raw) {
191
+ return '<h3 class="d-msg' + (raw.length > 140 || raw.includes('\n') ? ' long' : '') + '" id="kd-msg">' + html + '</h3>';
192
+ }
193
+ function fixFor(app, i) {
194
+ const list = app.fixes.get(fixKey(i));
195
+ if (!list || !list.length)
196
+ return null;
197
+ return list.find((c) => c.line === i.line) || list[0];
198
+ }
199
+ function buildDetail(app, i, list) {
200
+ const frame = app.frames.get(frameKey(i)) || null;
201
+ const fix = fixFor(app, i);
202
+ let html = '<div class="d-meta">';
203
+ const info = i.code ? app.codes.get(i.code) : undefined;
204
+ if (i.code) {
205
+ html += '<span class="code' + (i.severity === 'warning' ? ' w' : '') + '">' + esc(i.code) + '</span>';
206
+ if (info)
207
+ html += '<span class="chip">' + esc(info.title) + '</span>';
208
+ html += info
209
+ ? '<button class="whatbtn" type="button" data-act="code-pop" aria-expanded="' +
210
+ (app.codePop === i.code) +
211
+ '" aria-controls="kd-codepop">What this means</button>'
212
+ : '<a class="link" style="font-size:12.5px;text-decoration:none" href="' +
213
+ esc(app.docsURL(i.code)) +
214
+ '" target="_blank" rel="noopener">What this means \u2197</a>';
215
+ }
216
+ else {
217
+ html += '<span class="code' + (i.severity === 'warning' ? ' w' : '') + '">' + esc(i.label) + '</span>';
218
+ }
219
+ html += '</div>';
220
+ if (i.code && info && app.codePop === i.code) {
221
+ html +=
222
+ '<div class="codepop" id="kd-codepop" role="dialog" aria-label="' +
223
+ esc(i.code + ': ' + info.title) +
224
+ '"><button class="iconbtn x" type="button" data-act="code-pop" aria-label="Close">' +
225
+ icon('x') +
226
+ '</button><div class="t">' +
227
+ esc(i.code) +
228
+ ' \u00B7 ' +
229
+ esc(info.title) +
230
+ '</div><div>' +
231
+ richText(info.explanation) +
232
+ '</div><a href="' +
233
+ esc(app.docsURL(i.code)) +
234
+ '" target="_blank" rel="noopener">Read the docs \u2197</a></div>';
235
+ }
236
+ html += msgHTML(richText(i.message), i.message);
237
+ html += fileLine(app, i, frame?.file);
238
+ html += codeFrameHTML(frame, i, fix ? 'expected ' + fix.to : '');
239
+ if (i.hint)
240
+ html += '<div class="hint">' + icon('lightbulb') + '<div>' + richText(i.hint) + '</div></div>';
241
+ if (fix) {
242
+ const fixable = list.filter((x) => x.kind === 'build' && x.code).length;
243
+ const src = frame?.lines[fix.line - frame.start] ?? (fix.line === i.line ? i.source : undefined);
244
+ const w = String(fix.line).length;
245
+ const pad = (s) => s + ' '.repeat(Math.max(1, 8 - w));
246
+ const busy = app.busy.has('fix');
247
+ html +=
248
+ '<div class="fix"><div class="fix-h">' +
249
+ icon('wand-sparkles') +
250
+ '<span class="t">Safe fix available</span><span class="s">same rewrite as <span class="mono">krate fix</span></span><span class="grow"></span>' +
251
+ (fixable > 1 ? '<button class="b ghost" type="button" data-act="fix-all"' + (busy ? ' disabled' : '') + '>Fix all ' + fixable + '</button>' : '') +
252
+ '<button class="b pri" type="button" data-act="fix"' +
253
+ (busy ? ' disabled' : '') +
254
+ '>' +
255
+ (busy ? 'Applying' : 'Apply fix') +
256
+ ' <span class="kbd">F</span></button></div>' +
257
+ (src !== undefined
258
+ ? '<div class="diff"><div class="del">' +
259
+ esc('- ' + pad(String(fix.line)) + src) +
260
+ '</div><div class="add">' +
261
+ esc('+ ' + pad(String(fix.line)) + applyChange(src, fix)) +
262
+ '</div></div>'
263
+ : '<div class="diff"><div class="del">' +
264
+ esc('- ' + fix.from) +
265
+ '</div><div class="add">' +
266
+ esc('+ ' + fix.to) +
267
+ '</div></div>') +
268
+ '</div>';
269
+ }
270
+ return html;
271
+ }
272
+ function fileLine(app, i, shown) {
273
+ if (!i.file) {
274
+ return ('<div class="fileline"><span class="grow"></span><button class="b" type="button" data-act="copy-ai">' +
275
+ icon('copy') +
276
+ 'Copy for AI</button></div>');
277
+ }
278
+ return ('<div class="fileline">' +
279
+ pathHTML(i, shown) +
280
+ '<span class="grow"></span>' +
281
+ (canOpen(i)
282
+ ? '<button class="b" type="button" data-act="open-issue">' + icon('square-arrow-up-right-exit') + 'Open in editor <span class="kbd">O</span></button>'
283
+ : '') +
284
+ '<button class="b" type="button" data-act="copy-ai">' +
285
+ icon('copy') +
286
+ 'Copy for AI</button></div>');
287
+ }
288
+ function runtimeDetail(app, i) {
289
+ const frame = canOpen(i) || i.served ? app.frames.get(frameKey(i)) || null : null;
290
+ const frames = parseStack(i.stack);
291
+ const items = splitStack(frames);
292
+ const appCount = frames.filter((f) => f.app).length;
293
+ const path = componentAt(app, i);
294
+ let html = '<div class="d-meta"><span class="code' + (i.origin === 'console' ? ' w' : '') + '">' + esc(i.label) + '</span>';
295
+ if (path.length)
296
+ html += '<span class="chip">client island</span>';
297
+ if (i.origin === 'remote')
298
+ html += '<span class="chip">another tab</span>';
299
+ if (i.count > 1)
300
+ html += '<span class="chip">happened ' + i.count + ' times</span>';
301
+ html += '</div>' + msgHTML(esc(i.message), i.message);
302
+ if (path.length > 1) {
303
+ html +=
304
+ '<div class="crumbs">' +
305
+ path
306
+ .map((n, k) => '<span' + (k === path.length - 1 ? ' class="cur"' : '') + '>&lt;' + esc(n) + '&gt;</span>')
307
+ .join('<span class="sepc">\u203A</span>') +
308
+ '</div>';
309
+ }
310
+ html += fileLine(app, i, frame?.file);
311
+ html += codeFrameHTML(frame, i);
312
+ const console = app.localLogs.slice(-30);
313
+ const reqs = app.requests.near(i.time);
314
+ const tab = app.runtimeTab === 'requests' && !reqs.length ? 'stack' : app.runtimeTab;
315
+ html +=
316
+ '<div class="subtabs" role="tablist"><button type="button" role="tab" data-act="rt-tab" data-v="stack" aria-selected="' +
317
+ (tab === 'stack') +
318
+ '">Stack' +
319
+ (frames.length ? '<span>' + appCount + ' of ' + frames.length + '</span>' : '') +
320
+ '</button><button type="button" role="tab" data-act="rt-tab" data-v="console" aria-selected="' +
321
+ (tab === 'console') +
322
+ '">Console<span>' +
323
+ console.length +
324
+ '</span></button>' +
325
+ (reqs.length
326
+ ? '<button type="button" role="tab" data-act="rt-tab" data-v="requests" aria-selected="' +
327
+ (tab === 'requests') +
328
+ '">Requests<span>' +
329
+ reqs.length +
330
+ '</span></button>'
331
+ : '') +
332
+ '</div>';
333
+ if (tab === 'requests')
334
+ return html + requestsHTML(reqs, i.time);
335
+ if (tab === 'console') {
336
+ html += console.length
337
+ ? '<div class="term">' +
338
+ console
339
+ .map((l) => '<div class="tl"><span class="ts">' +
340
+ clock(l.t) +
341
+ '</span><span class="' +
342
+ (l.lvl === 'error' ? 'e' : l.lvl === 'warn' ? 'w' : 'm') +
343
+ '">' +
344
+ esc(l.text) +
345
+ '</span></div>')
346
+ .join('') +
347
+ '</div>'
348
+ : '<div class="empty">Nothing was logged in this tab.</div>';
349
+ return html;
350
+ }
351
+ if (!frames.length) {
352
+ html += i.stack ? '<div class="frames"><pre class="stackraw">' + esc(i.stack) + '</pre></div>' : '<div class="empty">No stack trace.</div>';
353
+ }
354
+ else {
355
+ html += '<div class="frames">';
356
+ items.forEach((it, k) => {
357
+ if (it.type === 'app') {
358
+ const f = it.frame;
359
+ html +=
360
+ (f.served
361
+ ? '<div class="fr app">'
362
+ : '<button class="fr app" type="button" data-act="open" data-file="' + esc(f.file) + '" data-line="' + f.line + '" data-col="' + f.col + '">') +
363
+ '<span class="fn">' +
364
+ esc(f.fn) +
365
+ '</span><span class="at"><b>' +
366
+ esc(f.file) +
367
+ '</b>:' +
368
+ f.line +
369
+ ':' +
370
+ f.col +
371
+ '</span>' +
372
+ (f.served ? '</div>' : '</button>');
373
+ return;
374
+ }
375
+ const key = i.id + '#' + k;
376
+ const open = app.unfold.has(key);
377
+ html +=
378
+ '<button class="fr fold" type="button" data-act="unfold" data-k="' +
379
+ esc(key) +
380
+ '"><span>' +
381
+ plural(it.frames.length, 'framework frame') +
382
+ frameworkOrigin(it.frames) +
383
+ '</span><span class="link">' +
384
+ (open ? 'Hide' : 'Show') +
385
+ '</span></button>';
386
+ if (open) {
387
+ for (const f of it.frames) {
388
+ html +=
389
+ '<div class="fr fw"><span class="fn">' +
390
+ esc(f.fn) +
391
+ '</span><span class="at">' +
392
+ esc(f.file) +
393
+ ':' +
394
+ f.line +
395
+ ':' +
396
+ f.col +
397
+ '</span></div>';
398
+ }
399
+ }
400
+ });
401
+ html += '</div>';
402
+ }
403
+ if (i.hostStack)
404
+ html += '<div class="lbl" style="margin-top:14px">Host stack</div><div class="frames"><pre class="stackraw">' + esc(i.hostStack) + '</pre></div>';
405
+ return html;
406
+ }
407
+ /** Failed requests around a runtime error, newest last. */
408
+ export function requestsHTML(reqs, at) {
409
+ let html = '<div class="reqs" role="list">';
410
+ for (const r of reqs) {
411
+ const delta = Math.round((r.t - at) / 100) / 10;
412
+ html +=
413
+ '<div class="req" role="listitem"><span class="st">' +
414
+ esc(r.status ? String(r.status) : 'failed') +
415
+ '</span><span class="m">' +
416
+ esc(r.method) +
417
+ '</span><span class="u" title="' +
418
+ esc(r.url) +
419
+ '">' +
420
+ esc(displayURL(r.url)) +
421
+ '</span><span class="ms">' +
422
+ esc(r.ms + ' ms') +
423
+ '</span><span class="ts" title="' +
424
+ esc(clock(r.t)) +
425
+ '">' +
426
+ esc((delta > 0 ? '+' : '') + delta + ' s') +
427
+ '</span>' +
428
+ (r.error ? '<span class="why">' + esc(r.error) + '</span>' : '') +
429
+ '</div>';
430
+ }
431
+ return (html +
432
+ '</div><div class="empty" style="text-align:left;padding:8px 2px 0">Failed fetch and XHR requests of this page from ' +
433
+ NEAR_BEFORE_MS / 1000 +
434
+ ' s before the error; times are relative to it.</div>');
435
+ }
436
+ function frameworkOrigin(frames) {
437
+ if (frames.every((f) => /node_modules/.test(f.file)))
438
+ return ' from node_modules';
439
+ if (frames.every((f) => /runtime|krate-|chunks\/runtime/.test(f.file)))
440
+ return ' from @krate/runtime';
441
+ return '';
442
+ }
443
+ function logLineClass(text) {
444
+ if (/\b(error|failed|panic|fatal|exception|uncaught|unhandled)\b|[A-Z]\w*Error\b/i.test(text))
445
+ return 'e';
446
+ if (/\bwarn(ing)?\b|deprecat/i.test(text))
447
+ return 'w';
448
+ return 'm';
449
+ }
450
+ function serverDetail(app, i) {
451
+ const s = i.server || { message: i.message };
452
+ const frame = isProjectFile(i.file) ? app.frames.get(frameKey(i)) || null : null;
453
+ let html = '<div class="d-meta"><span class="code">' + esc(i.label) + '</span>';
454
+ if (s.source && s.source !== i.label)
455
+ html += '<span class="chip">' + esc(s.source) + '</span>';
456
+ html += '</div>' + msgHTML(richText(i.message), i.message);
457
+ if (i.file)
458
+ html += fileLine(app, i, frame?.file) + codeFrameHTML(frame, i);
459
+ const tail = app.tails.get(i.id);
460
+ const log = tail && tail.length ? tail.map((l) => l.text) : s.log && s.log.length ? s.log : [];
461
+ const lastErr = log.reduce((a, l, k) => ((tail && tail[k] ? tail[k].lvl === 'error' : logLineClass(l) === 'e') ? k : a), -1);
462
+ const logFile = app.logFile();
463
+ html +=
464
+ '<div class="lbl" style="margin-top:18px">Last console lines<span class="grow"></span>' +
465
+ (logFile ? '<button class="link" type="button" style="font-size:12px" data-act="open" data-file="' + esc(logFile) + '">Open ' + esc(logFile) + '</button>' : '') +
466
+ '</div>';
467
+ if (log.length) {
468
+ html +=
469
+ '<div class="term">' +
470
+ log
471
+ .map((l, k) => {
472
+ const t = tail && tail[k];
473
+ return ('<div class="tl' +
474
+ (k === lastErr ? ' hl' : '') +
475
+ '">' +
476
+ (t ? '<span class="ts">' + clock(t.t) + '</span><span class="src s-' + esc(t.src) + '">' + esc(t.src) + '</span>' : '') +
477
+ '<span class="' +
478
+ (t && t.lvl === 'error' ? 'e' : t && t.lvl === 'warn' ? 'w' : logLineClass(l)) +
479
+ '">' +
480
+ esc(t ? l.replace(/^\s+/, '') : l) +
481
+ '</span></div>');
482
+ })
483
+ .join('') +
484
+ '</div>';
485
+ }
486
+ else if (s.stack) {
487
+ html += '<div class="term"><pre class="stackraw">' + esc(s.stack) + '</pre></div>';
488
+ }
489
+ else {
490
+ html += '<div class="empty">No console output was captured.</div>';
491
+ }
492
+ return html;
493
+ }
494
+ function footer(app, i) {
495
+ if (i.kind === 'build') {
496
+ const failed = app.visibleIssues().some((x) => x.kind === 'build' && x.severity === 'error');
497
+ if (!failed) {
498
+ return ('<span class="sp">' +
499
+ (app.busy.has('fixed') ? '<span class="spin"></span>Fix applied, rebuilding.' : 'The build passed with warnings. They clear when the code is fixed.') +
500
+ '</span><span class="grow"></span>' +
501
+ '<button class="b ghost" type="button" data-act="ignore">Ignore this warning for the session</button>');
502
+ }
503
+ // A built page on screen is the last good build; the error shell is not.
504
+ const lastGood = !!app.lastGoodAt && !!app.cfg.page;
505
+ return ('<span class="sp"><span class="spin"></span>' +
506
+ (app.busy.has('fixed') ? 'Fix applied, rebuilding.' : 'Watching for changes. This closes when the build passes.') +
507
+ '</span><span class="grow"></span>' +
508
+ (app.lastGoodAt ? '<span>Last good build ' + clock(app.lastGoodAt) + '</span>' : '') +
509
+ (lastGood ? '<button class="link" type="button" data-act="last-good">Show last good page</button>' : ''));
510
+ }
511
+ if (i.kind === 'server') {
512
+ const r = i.server?.restart;
513
+ const busy = app.busy.has('restart');
514
+ return ('<span class="sp">' +
515
+ (busy ? '<span class="spin"></span>Restarting.' : r ? 'Restart it here, or save a file.' : '<span class="spin"></span>Retries on the next request or save.') +
516
+ '</span><span class="grow"></span>' +
517
+ (i.server?.log?.length ? '<button class="b ghost" type="button" data-act="copy-log">Copy log</button>' : '') +
518
+ '<button class="b ghost" type="button" data-act="copy-ai">Copy for AI</button>' +
519
+ (r && app.cfg.api
520
+ ? '<button class="b pri" type="button" data-act="restart"' + (busy ? ' disabled' : '') + '>Restart sidecar</button>'
521
+ : ''));
522
+ }
523
+ return ('<span>Reported to the terminal as well.</span><span class="grow"></span>' +
524
+ '<button class="b ghost" type="button" data-act="ignore">Ignore this error for the session</button>' +
525
+ '<button class="b" type="button" data-act="reload">Reload page <span class="kbd">R</span></button>');
526
+ }
527
+ export function renderModal(app) {
528
+ const box = app.modal;
529
+ if (!box)
530
+ return;
531
+ const list = app.visibleIssues();
532
+ if (!list.length) {
533
+ box.hidden = true;
534
+ return;
535
+ }
536
+ if (app.cur >= list.length)
537
+ app.cur = list.length - 1;
538
+ const i = list[app.cur];
539
+ const [t, color] = title(i, list);
540
+ const detail = i.kind === 'build' ? buildDetail(app, i, list) : i.kind === 'server' ? serverDetail(app, i) : runtimeDetail(app, i);
541
+ const scroll = box.querySelector('.detail')?.scrollTop || 0;
542
+ const same = box.getAttribute('data-issue') === i.id;
543
+ box.setAttribute('data-issue', i.id);
544
+ box.innerHTML =
545
+ '<div class="modal" role="dialog" aria-modal="true" aria-labelledby="kd-title" aria-describedby="kd-msg" tabindex="-1"><div class="m-head' +
546
+ (color === 'amber' ? ' warn' : '') +
547
+ '">' +
548
+ mark() +
549
+ '<div><div class="m-title ' +
550
+ color +
551
+ '" id="kd-title">' +
552
+ esc(t) +
553
+ '</div><div class="m-sub">' +
554
+ esc(sub(app, i, list)) +
555
+ '</div></div><span class="grow"></span>' +
556
+ (list.length > 1
557
+ ? '<div class="pager"><button class="iconbtn" type="button" data-act="prev" aria-label="Previous issue"' +
558
+ (app.cur === 0 ? ' disabled' : '') +
559
+ '>' +
560
+ icon('chevron-left') +
561
+ '</button><span>' +
562
+ (app.cur + 1) +
563
+ ' of ' +
564
+ list.length +
565
+ '</span><button class="iconbtn" type="button" data-act="next" aria-label="Next issue"' +
566
+ (app.cur === list.length - 1 ? ' disabled' : '') +
567
+ '>' +
568
+ icon('chevron-right') +
569
+ '</button></div>'
570
+ : '') +
571
+ '<button class="b ghost" type="button" data-act="dismiss">Dismiss <span class="kbd">Esc</span></button></div>' +
572
+ '<div class="m-body' +
573
+ (list.length > 1 ? '' : ' solo') +
574
+ '">' +
575
+ (list.length > 1 ? '<nav class="rail" aria-label="Issues">' + railHTML(app, list) + '</nav>' : '') +
576
+ '<div class="detail">' +
577
+ detail +
578
+ '</div></div><div class="m-foot">' +
579
+ footer(app, i) +
580
+ '</div></div>';
581
+ if (same) {
582
+ const d = box.querySelector('.detail');
583
+ if (d)
584
+ d.scrollTop = scroll;
585
+ }
586
+ const term = box.querySelector('.term');
587
+ if (term)
588
+ term.scrollTop = term.scrollHeight;
589
+ }
590
+ /** A code frame from a script the page loaded, for stacks without a source map. */
591
+ async function servedFrame(file, line) {
592
+ try {
593
+ const res = await rawFetch('/' + file.replace(/^\/+/, ''), { cache: 'force-cache' });
594
+ if (!res.ok)
595
+ return null;
596
+ const all = (await res.text()).split('\n');
597
+ const start = Math.max(1, line - 3);
598
+ const lines = all.slice(start - 1, line + 3).map((l) => (l.length > 500 ? l.slice(0, 500) + '...' : l));
599
+ // The dev hydration code registers each client scope right after its
600
+ // body, so the next registration names the component that threw.
601
+ let scope;
602
+ for (const l of all.slice(line - 1, line + 300)) {
603
+ const m = /__krate_dev_reg\("([^"]+)"/.exec(l);
604
+ if (m) {
605
+ scope = m[1];
606
+ break;
607
+ }
608
+ }
609
+ return lines.length ? { file, lang: 'js', start, lines, scope } : null;
610
+ }
611
+ catch {
612
+ return null;
613
+ }
614
+ }
615
+ /** Loads the code frame and fix preview of an issue, then re-renders. */
616
+ export function prepareIssue(app, i) {
617
+ if (!i)
618
+ return;
619
+ const rerender = () => {
620
+ const cur = app.visibleIssues()[app.cur];
621
+ if (app.modalOpen && cur && cur.id === i.id)
622
+ renderModal(app);
623
+ };
624
+ const fk = frameKey(i);
625
+ if (i.served && i.file && i.line && !app.frames.has(fk) && !app.busy.has('src:' + fk)) {
626
+ app.busy.add('src:' + fk);
627
+ void servedFrame(i.file, i.line).then((f) => {
628
+ app.busy.delete('src:' + fk);
629
+ app.frames.set(fk, f);
630
+ rerender();
631
+ });
632
+ }
633
+ if (canOpen(i) && !app.frames.has(fk) && !app.busy.has('src:' + fk)) {
634
+ app.busy.add('src:' + fk);
635
+ void app.getJSON('/source', { file: i.file, line: i.line || 1, context: 3 }).then((f) => {
636
+ app.busy.delete('src:' + fk);
637
+ app.frames.set(fk, f && Array.isArray(f.lines) ? f : null);
638
+ rerender();
639
+ });
640
+ }
641
+ if (i.kind === 'server' && !app.tails.has(i.id) && !app.busy.has('tail:' + i.id)) {
642
+ app.busy.add('tail:' + i.id);
643
+ void app.getJSON('/logs').then((r) => {
644
+ app.busy.delete('tail:' + i.id);
645
+ const lines = r && Array.isArray(r.lines) ? r.lines.filter((l) => l.t <= i.time + 1000).slice(-40) : [];
646
+ app.tails.set(i.id, lines);
647
+ rerender();
648
+ });
649
+ }
650
+ if (i.kind === 'build' && i.code && !app.codes.has(i.code) && !app.busy.has('code:' + i.code)) {
651
+ const code = i.code;
652
+ app.busy.add('code:' + code);
653
+ void app.getJSON('/code', { code }).then((r) => {
654
+ app.busy.delete('code:' + code);
655
+ app.codes.set(code, r && typeof r.title === 'string' ? { title: r.title, explanation: r.explanation || '' } : null);
656
+ rerender();
657
+ });
658
+ }
659
+ const xk = fixKey(i);
660
+ if (i.kind === 'build' && i.code && i.file && !app.fixes.has(xk) && !app.busy.has('fx:' + xk)) {
661
+ app.busy.add('fx:' + xk);
662
+ void app.getJSON('/fix', { file: i.file, code: i.code }).then((r) => {
663
+ app.busy.delete('fx:' + xk);
664
+ app.fixes.set(xk, r && Array.isArray(r.changes) ? r.changes : null);
665
+ rerender();
666
+ });
667
+ }
668
+ }
669
+ function go(app, n) {
670
+ const list = app.visibleIssues();
671
+ if (n < 0 || n >= list.length || n === app.cur)
672
+ return;
673
+ app.cur = n;
674
+ app.runtimeTab = 'stack';
675
+ renderModal(app);
676
+ prepareIssue(app, list[n]);
677
+ }
678
+ async function applyFix(app, all) {
679
+ const i = app.visibleIssues()[app.cur];
680
+ if (!i || i.kind !== 'build' || app.busy.has('fix'))
681
+ return;
682
+ if (!all && !fixFor(app, i))
683
+ return;
684
+ app.busy.add('fix');
685
+ renderModal(app);
686
+ const { status, data } = await app.postJSON('/fix', all ? { all: true } : { file: i.file, code: i.code });
687
+ app.busy.delete('fix');
688
+ if (status === 200 && data && data.applied) {
689
+ app.busy.add('fixed');
690
+ const n = Array.isArray(data.changes) ? data.changes.length : 1;
691
+ app.toast(all ? 'Applied ' + plural(n, 'fix', 'fixes') : 'Fix applied', all ? '' : basename(i.file || ''), 'check');
692
+ }
693
+ else if (status === 200) {
694
+ app.toast('Nothing to fix', '', 'check');
695
+ }
696
+ else {
697
+ app.toast('The fix could not be applied', data?.error || String(status || 'offline'), 'x');
698
+ }
699
+ renderModal(app);
700
+ }
701
+ async function restart(app) {
702
+ const i = app.visibleIssues()[app.cur];
703
+ const target = i?.server?.restart;
704
+ if (!i || !target || app.busy.has('restart'))
705
+ return;
706
+ app.busy.add('restart');
707
+ renderModal(app);
708
+ const ok = await app.runAction('restart-sidecar', { target });
709
+ app.busy.delete('restart');
710
+ if (ok) {
711
+ // The dev server does not announce a recovery; the restart is the fix.
712
+ app.issues = app.issues.filter((x) => x.id !== i.id);
713
+ app.issuesChanged(false);
714
+ }
715
+ else {
716
+ renderModal(app);
717
+ }
718
+ }
719
+ function copyAI(app) {
720
+ const i = app.visibleIssues()[app.cur];
721
+ if (!i)
722
+ return;
723
+ const frame = app.frames.get(frameKey(i)) || null;
724
+ const md = copyForAI(frame && !i.served ? { ...i, file: frame.file } : i, frame, { url: location.pathname + location.search, route: app.pageRoute().route });
725
+ void copyText(md).then((ok) => app.toast(ok ? 'Copied for AI' : 'Could not copy', ok ? 'Markdown' : '', ok ? 'check' : 'x'));
726
+ }
727
+ export function modalClick(app, act, t, _e) {
728
+ const i = app.visibleIssues()[app.cur];
729
+ switch (act) {
730
+ case 'dismiss':
731
+ app.closeModal();
732
+ return true;
733
+ case 'prev':
734
+ go(app, app.cur - 1);
735
+ return true;
736
+ case 'next':
737
+ go(app, app.cur + 1);
738
+ return true;
739
+ case 'goto':
740
+ go(app, Number(t.dataset.n));
741
+ return true;
742
+ case 'open-issue':
743
+ if (i)
744
+ void app.openInEditor(i.file, i.line, i.col);
745
+ return true;
746
+ case 'copy-ai':
747
+ copyAI(app);
748
+ return true;
749
+ case 'copy-log': {
750
+ const log = i?.server?.log?.join('\n') || '';
751
+ void copyText(log).then((ok) => app.toast(ok ? 'Log copied' : 'Could not copy', ok ? plural(i?.server?.log?.length || 0, 'line') : '', ok ? 'check' : 'x'));
752
+ return true;
753
+ }
754
+ case 'fix':
755
+ void applyFix(app, false);
756
+ return true;
757
+ case 'fix-all':
758
+ void applyFix(app, true);
759
+ return true;
760
+ case 'restart':
761
+ void restart(app);
762
+ return true;
763
+ case 'ignore':
764
+ if (i)
765
+ app.ignore(i.id);
766
+ return true;
767
+ case 'reload':
768
+ location.reload();
769
+ return true;
770
+ case 'rt-tab':
771
+ app.runtimeTab = t.dataset.v === 'console' ? 'console' : t.dataset.v === 'requests' ? 'requests' : 'stack';
772
+ renderModal(app);
773
+ return true;
774
+ case 'code-pop':
775
+ app.codePop = i && i.code && app.codePop !== i.code ? i.code : '';
776
+ renderModal(app);
777
+ return true;
778
+ case 'last-good':
779
+ app.showLastGood();
780
+ return true;
781
+ case 'unfold': {
782
+ const k = t.dataset.k || '';
783
+ if (app.unfold.has(k))
784
+ app.unfold.delete(k);
785
+ else
786
+ app.unfold.add(k);
787
+ renderModal(app);
788
+ return true;
789
+ }
790
+ }
791
+ return false;
792
+ }
793
+ export function modalKey(app, e, typing) {
794
+ if (e.key === 'Escape') {
795
+ e.preventDefault();
796
+ if (app.codePop) {
797
+ app.codePop = '';
798
+ renderModal(app);
799
+ }
800
+ else {
801
+ app.closeModal();
802
+ }
803
+ return true;
804
+ }
805
+ if (typing || e.ctrlKey || e.metaKey || e.altKey)
806
+ return false;
807
+ const i = app.visibleIssues()[app.cur];
808
+ switch (e.key) {
809
+ case 'ArrowLeft':
810
+ go(app, app.cur - 1);
811
+ break;
812
+ case 'ArrowRight':
813
+ go(app, app.cur + 1);
814
+ break;
815
+ case 'o':
816
+ case 'O':
817
+ if (i && canOpen(i))
818
+ void app.openInEditor(i.file, i.line, i.col);
819
+ break;
820
+ case 'f':
821
+ case 'F':
822
+ void applyFix(app, false);
823
+ break;
824
+ case 'r':
825
+ case 'R':
826
+ if (i && i.kind === 'runtime')
827
+ location.reload();
828
+ else
829
+ return false;
830
+ break;
831
+ default:
832
+ return false;
833
+ }
834
+ e.preventDefault();
835
+ return true;
836
+ }