@krate/runtime 0.5.0-beta.3 → 0.5.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/dist/action-context.js +1 -1
  2. package/dist/actions.d.ts +7 -0
  3. package/dist/actions.js +34 -10
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/assets-static.d.ts +10 -1
  7. package/dist/assets-static.js +10 -2
  8. package/dist/base-path.d.ts +7 -0
  9. package/dist/base-path.js +20 -0
  10. package/dist/boundary.js +1 -1
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/cache-adapter.js +1 -1
  14. package/dist/cache-scope.d.ts +8 -0
  15. package/dist/cache-scope.js +14 -1
  16. package/dist/cloudflare-workers.d.ts +2 -0
  17. package/dist/cloudflare-workers.js +27 -0
  18. package/dist/context.d.ts +5 -0
  19. package/dist/context.js +28 -17
  20. package/dist/dev-overlay.d.ts +54 -3
  21. package/dist/dev-overlay.js +496 -91
  22. package/dist/dom.d.ts +4 -0
  23. package/dist/dom.js +62 -16
  24. package/dist/effects.js +43 -10
  25. package/dist/events.d.ts +2 -0
  26. package/dist/events.js +18 -0
  27. package/dist/flight-props.d.ts +7 -0
  28. package/dist/flight-props.js +105 -0
  29. package/dist/handler-html.js +4 -4
  30. package/dist/handler-routing.d.ts +2 -23
  31. package/dist/handler-routing.js +9 -190
  32. package/dist/handler-segments.d.ts +6 -0
  33. package/dist/handler-segments.js +18 -4
  34. package/dist/head-tags.d.ts +9 -0
  35. package/dist/head-tags.js +93 -6
  36. package/dist/highlight-bind.js +1 -1
  37. package/dist/highlight.d.ts +3 -2
  38. package/dist/highlight.js +17 -12
  39. package/dist/hints.d.ts +25 -2
  40. package/dist/hints.js +147 -74
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +3 -0
  43. package/dist/island-swap.d.ts +47 -0
  44. package/dist/island-swap.js +635 -0
  45. package/dist/jsx-dev-runtime.d.ts +1 -1
  46. package/dist/jsx-types.d.ts +99 -3
  47. package/dist/krate-core.js +1 -1
  48. package/dist/krate-dev.js +407 -80
  49. package/dist/krate-f-actions.js +1 -1
  50. package/dist/krate-f-classes.js +1 -1
  51. package/dist/krate-f-highlight.js +1 -1
  52. package/dist/krate-f-list.js +1 -1
  53. package/dist/krate-f-navigation.js +1 -1
  54. package/dist/krate-f-router-lite.js +1 -1
  55. package/dist/krate-f-router.js +1 -1
  56. package/dist/krate-features.json +455 -0
  57. package/dist/krate-hydrate-lite.js +5 -5
  58. package/dist/krate-hydrate.js +5 -5
  59. package/dist/krate-island-swap.js +1 -0
  60. package/dist/krate-runtime.js +5 -5
  61. package/dist/krate-suspense.js +1 -1
  62. package/dist/list.d.ts +14 -4
  63. package/dist/list.js +30 -23
  64. package/dist/metadata.js +5 -2
  65. package/dist/navigation-react.d.ts +13 -0
  66. package/dist/navigation-react.js +86 -0
  67. package/dist/navigation.d.ts +19 -0
  68. package/dist/navigation.js +35 -0
  69. package/dist/node-server.js +3 -3
  70. package/dist/og.js +1 -1
  71. package/dist/platform.d.ts +52 -0
  72. package/dist/platform.js +57 -0
  73. package/dist/react-compat.d.ts +39 -0
  74. package/dist/react-compat.js +359 -0
  75. package/dist/react-server.d.ts +63 -0
  76. package/dist/react-server.js +125 -0
  77. package/dist/region-wire.d.ts +10 -0
  78. package/dist/region-wire.js +154 -0
  79. package/dist/request-context.js +1 -1
  80. package/dist/request-extras.d.ts +4 -0
  81. package/dist/request-extras.js +51 -0
  82. package/dist/resp.js +1 -1
  83. package/dist/route-source.d.ts +39 -0
  84. package/dist/route-source.js +532 -0
  85. package/dist/router-lite.js +3 -9
  86. package/dist/router.d.ts +1 -1
  87. package/dist/router.js +675 -100
  88. package/dist/server-handler.d.ts +69 -1
  89. package/dist/server-handler.js +515 -96
  90. package/dist/server-renderer.js +16 -7
  91. package/dist/server.d.ts +73 -16
  92. package/dist/server.js +619 -102
  93. package/dist/sha256.js +9 -9
  94. package/dist/signal.js +47 -20
  95. package/dist/site-config.d.ts +15 -0
  96. package/dist/site-config.js +28 -0
  97. package/dist/slot.d.ts +12 -0
  98. package/dist/slot.js +23 -0
  99. package/dist/ssr-core.d.ts +2 -0
  100. package/dist/ssr-core.js +12 -4
  101. package/dist/suspense.js +34 -2
  102. package/dist/testing.d.ts +62 -0
  103. package/dist/testing.js +90 -0
  104. package/dist/use-id.js +1 -1
  105. package/package.json +28 -3
  106. package/dist/action-context.test.d.ts +0 -1
  107. package/dist/action-context.test.js +0 -72
  108. package/dist/actions.test.d.ts +0 -1
  109. package/dist/actions.test.js +0 -477
  110. package/dist/cache-adapter.test.d.ts +0 -1
  111. package/dist/cache-adapter.test.js +0 -244
  112. package/dist/cache.test.d.ts +0 -1
  113. package/dist/cache.test.js +0 -233
  114. package/dist/class-component.test.d.ts +0 -1
  115. package/dist/class-component.test.js +0 -462
  116. package/dist/class-helpers.test.d.ts +0 -1
  117. package/dist/class-helpers.test.js +0 -169
  118. package/dist/context.test.d.ts +0 -1
  119. package/dist/context.test.js +0 -57
  120. package/dist/derived.test.d.ts +0 -1
  121. package/dist/derived.test.js +0 -140
  122. package/dist/dev-overlay.test.d.ts +0 -1
  123. package/dist/dev-overlay.test.js +0 -152
  124. package/dist/discrete.test.d.ts +0 -1
  125. package/dist/discrete.test.js +0 -100
  126. package/dist/dom.test.d.ts +0 -1
  127. package/dist/dom.test.js +0 -31
  128. package/dist/effect-errors.test.d.ts +0 -1
  129. package/dist/effect-errors.test.js +0 -163
  130. package/dist/effects.test.d.ts +0 -1
  131. package/dist/effects.test.js +0 -485
  132. package/dist/events.test.d.ts +0 -1
  133. package/dist/events.test.js +0 -447
  134. package/dist/handler-html.test.d.ts +0 -1
  135. package/dist/handler-html.test.js +0 -256
  136. package/dist/handler-routing.test.d.ts +0 -1
  137. package/dist/handler-routing.test.js +0 -99
  138. package/dist/handler-segments.test.d.ts +0 -1
  139. package/dist/handler-segments.test.js +0 -217
  140. package/dist/head-tags.test.d.ts +0 -1
  141. package/dist/head-tags.test.js +0 -131
  142. package/dist/highlight-bind.test.d.ts +0 -1
  143. package/dist/highlight-bind.test.js +0 -124
  144. package/dist/highlight.test.d.ts +0 -1
  145. package/dist/highlight.test.js +0 -56
  146. package/dist/lanes.test.d.ts +0 -1
  147. package/dist/lanes.test.js +0 -59
  148. package/dist/list.test.d.ts +0 -1
  149. package/dist/list.test.js +0 -344
  150. package/dist/metadata.test.d.ts +0 -1
  151. package/dist/metadata.test.js +0 -167
  152. package/dist/navigation.test.d.ts +0 -1
  153. package/dist/navigation.test.js +0 -130
  154. package/dist/node-server.test.d.ts +0 -1
  155. package/dist/node-server.test.js +0 -492
  156. package/dist/optimistic-lanes.test.d.ts +0 -1
  157. package/dist/optimistic-lanes.test.js +0 -45
  158. package/dist/patterns.test.d.ts +0 -1
  159. package/dist/patterns.test.js +0 -98
  160. package/dist/portal.test.d.ts +0 -1
  161. package/dist/portal.test.js +0 -99
  162. package/dist/react-effects.test.d.ts +0 -1
  163. package/dist/react-effects.test.js +0 -96
  164. package/dist/react-suspense.test.d.ts +0 -1
  165. package/dist/react-suspense.test.js +0 -194
  166. package/dist/react.test.d.ts +0 -1
  167. package/dist/react.test.js +0 -60
  168. package/dist/react19.test.d.ts +0 -1
  169. package/dist/react19.test.js +0 -176
  170. package/dist/reactivity-core.test.d.ts +0 -1
  171. package/dist/reactivity-core.test.js +0 -152
  172. package/dist/reconcile.test.d.ts +0 -1
  173. package/dist/reconcile.test.js +0 -110
  174. package/dist/refs.test.d.ts +0 -1
  175. package/dist/refs.test.js +0 -257
  176. package/dist/router-layout.test.d.ts +0 -1
  177. package/dist/router-layout.test.js +0 -249
  178. package/dist/router-nav.test.d.ts +0 -1
  179. package/dist/router-nav.test.js +0 -464
  180. package/dist/router-slots.test.d.ts +0 -1
  181. package/dist/router-slots.test.js +0 -144
  182. package/dist/router-speculation.test.d.ts +0 -1
  183. package/dist/router-speculation.test.js +0 -25
  184. package/dist/router-styles.test.d.ts +0 -1
  185. package/dist/router-styles.test.js +0 -98
  186. package/dist/router.test.d.ts +0 -1
  187. package/dist/router.test.js +0 -36
  188. package/dist/server-async.test.d.ts +0 -1
  189. package/dist/server-async.test.js +0 -216
  190. package/dist/server-handler.test.d.ts +0 -1
  191. package/dist/server-handler.test.js +0 -535
  192. package/dist/server-highlight.test.d.ts +0 -1
  193. package/dist/server-highlight.test.js +0 -32
  194. package/dist/server-renderer.test.d.ts +0 -1
  195. package/dist/server-renderer.test.js +0 -266
  196. package/dist/server.test.d.ts +0 -1
  197. package/dist/server.test.js +0 -88
  198. package/dist/sha256.test.d.ts +0 -1
  199. package/dist/sha256.test.js +0 -13
  200. package/dist/signal-lanes.test.d.ts +0 -1
  201. package/dist/signal-lanes.test.js +0 -32
  202. package/dist/signal.test.d.ts +0 -1
  203. package/dist/signal.test.js +0 -68
  204. package/dist/slots.test.d.ts +0 -1
  205. package/dist/slots.test.js +0 -169
  206. package/dist/snapshots.test.d.ts +0 -1
  207. package/dist/snapshots.test.js +0 -208
  208. package/dist/ssr-core-behavior.test.d.ts +0 -1
  209. package/dist/ssr-core-behavior.test.js +0 -620
  210. package/dist/ssr-core.test.d.ts +0 -1
  211. package/dist/ssr-core.test.js +0 -324
  212. package/dist/static-memory.test.d.ts +0 -1
  213. package/dist/static-memory.test.js +0 -26
  214. package/dist/storage.test.d.ts +0 -1
  215. package/dist/storage.test.js +0 -46
  216. package/dist/suspense-actions.test.d.ts +0 -1
  217. package/dist/suspense-actions.test.js +0 -72
  218. package/dist/suspense.test.d.ts +0 -1
  219. package/dist/suspense.test.js +0 -518
package/dist/krate-dev.js CHANGED
@@ -1,11 +1,42 @@
1
1
  var __krate_exports = (() => {
2
+ // src/site-config.ts
3
+ var CFG_BLOCK_ID = "__krate_cfg__";
4
+ function siteConfig() {
5
+ const g = globalThis;
6
+ if (g.__KRATE_CFG__) return g.__KRATE_CFG__;
7
+ const doc = g.document;
8
+ const el = doc && typeof doc.getElementById === "function" ? doc.getElementById(CFG_BLOCK_ID) : null;
9
+ if (el) {
10
+ try {
11
+ const parsed = JSON.parse(el.textContent || "{}");
12
+ if (parsed && typeof parsed === "object") {
13
+ g.__KRATE_CFG__ = parsed;
14
+ return parsed;
15
+ }
16
+ } catch {
17
+ }
18
+ }
19
+ return {};
20
+ }
21
+
2
22
  // src/dev-overlay.ts
23
+ function isNativeOrigin() {
24
+ try {
25
+ return location.protocol === "krate:" || location.origin === "https://app.localhost";
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
3
30
  function devConfig() {
4
31
  const raw = window.__KRATE_DEV__;
5
- const runtime = window.__KRATE_CFG__;
32
+ const runtime = siteConfig();
33
+ const native = isNativeOrigin();
6
34
  return {
7
35
  sse: raw?.sse ?? "/__krate/hotreload",
8
- errors: raw?.errors ?? "/__krate/client-error",
36
+ frames: raw?.frames ?? (native ? "/__krate/hmr" : ""),
37
+ events: raw?.events ?? (native ? "/__krate/dev/events" : ""),
38
+ memory: raw?.memory ?? (native ? "/__krate/dev/memory" : ""),
39
+ errors: raw?.errors ?? (native ? "/__krate/dev/client-error" : "/__krate/client-error"),
9
40
  open: raw?.open ?? "/__krate/open",
10
41
  overlay: raw?.overlay ?? true,
11
42
  toolbar: raw?.toolbar ?? true,
@@ -60,7 +91,9 @@ var __krate_exports = (() => {
60
91
  if (!part) continue;
61
92
  const nums = decodeVLQ(part);
62
93
  genCol += nums[0] || 0;
63
- if (nums.length >= 4) {
94
+ if (nums.length === 1) {
95
+ segs.push({ genCol, src: -1, srcLine: 0, srcCol: 0 });
96
+ } else if (nums.length >= 4) {
64
97
  src += nums[1];
65
98
  srcLine += nums[2];
66
99
  srcCol += nums[3];
@@ -78,7 +111,8 @@ var __krate_exports = (() => {
78
111
  if (cached) return cached;
79
112
  const p = (async () => {
80
113
  try {
81
- if (/^[a-z]+:\/\//i.test(url) && !url.startsWith(location.origin)) return null;
114
+ const u = new URL(url, location.href);
115
+ if (u.protocol !== location.protocol || u.host !== location.host) return null;
82
116
  const res = await fetch(url + ".map");
83
117
  if (!res.ok) return null;
84
118
  const json = await res.json();
@@ -93,7 +127,7 @@ var __krate_exports = (() => {
93
127
  }
94
128
  function parseStackFrame(line) {
95
129
  const patterns = [
96
- /^(.*?)((?:https?:)?\/\/[^\s)]+|\/[^\s):]+):(\d+):(\d+)(.*)$/
130
+ /^(.*?)((?:[a-z][a-z0-9+.-]*:)?\/\/[^\s)]+|\/[^\s):]+):(\d+):(\d+)(.*)$/
97
131
  ];
98
132
  for (const re of patterns) {
99
133
  const m = re.exec(line);
@@ -103,19 +137,27 @@ var __krate_exports = (() => {
103
137
  return null;
104
138
  }
105
139
  function remapLocation(map, line, col) {
106
- const segs = map.lines[line - 1];
107
- if (!segs || segs.length === 0) return null;
108
140
  let best = null;
109
- for (const s of segs) {
110
- if (s.genCol <= col - 1) best = s;
111
- else break;
141
+ const segs = map.lines[line - 1];
142
+ if (segs && segs.length > 0) {
143
+ for (const s of segs) {
144
+ if (s.genCol <= col - 1) best = s;
145
+ else break;
146
+ }
147
+ if (!best) best = segs[0];
148
+ } else {
149
+ for (let i = Math.min(line - 1, map.lines.length) - 1; i >= 0 && !best; i--) {
150
+ const prev = map.lines[i];
151
+ if (prev && prev.length > 0) best = prev[prev.length - 1];
152
+ }
153
+ if (!best) return null;
112
154
  }
113
- if (!best) best = segs[0];
155
+ if (best.src < 0) return null;
114
156
  const source = map.sources[best.src] || "";
115
157
  return { source, line: best.srcLine + 1, col: best.srcCol + 1 };
116
158
  }
117
159
  async function symbolicateStack(stack) {
118
- if (!stack || stack.indexOf(".js") === -1) return stack;
160
+ if (!stack || !stack.includes(".js")) return stack;
119
161
  const lines = stack.split("\n");
120
162
  const out = await Promise.all(
121
163
  lines.map(async (line) => {
@@ -124,12 +166,21 @@ var __krate_exports = (() => {
124
166
  const map = await loadSourceMap(frame.url);
125
167
  if (!map) return line;
126
168
  const loc = remapLocation(map, frame.line, frame.col);
127
- if (!loc) return line;
169
+ if (!loc || !loc.source || loc.source === "krate-hydration.js") return line;
128
170
  return frame.prefix + loc.source + ":" + loc.line + ":" + loc.col + frame.suffix;
129
171
  })
130
172
  );
131
173
  return out.join("\n");
132
174
  }
175
+ function formatArg(a) {
176
+ if (a instanceof Error) return a.message || String(a);
177
+ if (typeof a === "string") return a;
178
+ try {
179
+ return JSON.stringify(a) ?? String(a);
180
+ } catch {
181
+ return String(a);
182
+ }
183
+ }
133
184
  function routeMatches(pattern, pathname, basePath = "") {
134
185
  const a = norm(stripBase(pathname, basePath)).split("/");
135
186
  const b = norm(pattern).split("/");
@@ -143,8 +194,79 @@ var __krate_exports = (() => {
143
194
  }
144
195
  return a.length === b.length;
145
196
  }
197
+ function parseFrames(buf) {
198
+ const values = [];
199
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
200
+ const td = new TextDecoder();
201
+ let pos = 0;
202
+ while (buf.length - pos >= 4) {
203
+ const n = view.getUint32(pos);
204
+ let end = pos + 4 + n;
205
+ if (buf.length < end + 2) break;
206
+ const json = td.decode(buf.subarray(pos + 4, end));
207
+ const blobs = view.getUint16(end);
208
+ end += 2;
209
+ let complete = true;
210
+ for (let i = 0; i < blobs; i++) {
211
+ if (buf.length < end + 4) {
212
+ complete = false;
213
+ break;
214
+ }
215
+ end += 4 + view.getUint32(end);
216
+ if (buf.length < end) {
217
+ complete = false;
218
+ break;
219
+ }
220
+ }
221
+ if (!complete) break;
222
+ try {
223
+ values.push(JSON.parse(json));
224
+ } catch {
225
+ }
226
+ pos = end;
227
+ }
228
+ return { values, rest: buf.subarray(pos) };
229
+ }
230
+ function hostStackOf(reason) {
231
+ const data = reason?.data;
232
+ const stack = data && typeof data === "object" ? data.stack : void 0;
233
+ return typeof stack === "string" ? stack : "";
234
+ }
235
+ function hostEventLine(ev) {
236
+ const t = ev.time ? new Date(ev.time).toLocaleTimeString() : "";
237
+ const dur = ev.durNs ? (ev.durNs / 1e6 >= 10 ? Math.round(ev.durNs / 1e6) : Math.round(ev.durNs / 1e5) / 10) + "ms" : "";
238
+ const err = ev.code ? " " + ev.code + (ev.message ? ": " + ev.message : "") : "";
239
+ return [t, ev.kind, ev.fn, dur].filter(Boolean).join(" ") + err;
240
+ }
241
+ function formatBytes(n) {
242
+ if (!Number.isFinite(n) || n < 0) return "n/a";
243
+ if (n >= 1 << 30) return (n / (1 << 30)).toFixed(2) + " GiB";
244
+ if (n >= 1 << 20) return (n / (1 << 20)).toFixed(1) + " MiB";
245
+ if (n >= 1 << 10) return Math.round(n / (1 << 10)) + " KiB";
246
+ return Math.round(n) + " B";
247
+ }
248
+ function jsHeapBytes() {
249
+ const m = typeof performance !== "undefined" ? performance.memory : void 0;
250
+ return m && typeof m.usedJSHeapSize === "number" ? m.usedJSHeapSize : null;
251
+ }
252
+ function memoryRows(rep, domNodes, heap) {
253
+ const rows = [];
254
+ if (rep && rep.procs) {
255
+ for (const p of rep.procs) {
256
+ rows.push([p.role + " (" + p.pid + ")", "PSS " + formatBytes(p.pss) + " / private dirty " + formatBytes(p.privateDirty)]);
257
+ }
258
+ if (rep.total) rows.push(["total", "PSS " + formatBytes(rep.total.pss) + " / private dirty " + formatBytes(rep.total.privateDirty)]);
259
+ } else {
260
+ rows.push(["processes", "unavailable"]);
261
+ }
262
+ rows.push(["DOM nodes", String(domNodes)]);
263
+ rows.push(["JS heap", heap === null ? "unavailable in this engine (see the web process row)" : formatBytes(heap)]);
264
+ return rows;
265
+ }
266
+ var ICON_MEM = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="4" y="6" width="16" height="12" rx="1"/><path d="M8 10v4M12 10v4M16 10v4"/><path d="M7 18v2M12 18v2M17 18v2"/></svg>';
146
267
  var ICON_RELOAD = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12a9 9 0 1 1-2.64-6.36"/><path d="M21 3v6h-6"/></svg>';
147
268
  var ICON_ALERT = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>';
269
+ var ICON_HOST = '<svg class="ic" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="4" width="18" height="12" rx="2"/><path d="M8 20h8"/><path d="M12 16v4"/></svg>';
148
270
  var STYLE = `
149
271
  :host { all: initial; }
150
272
  * { box-sizing: border-box; }
@@ -247,6 +369,31 @@ var __krate_exports = (() => {
247
369
  .frame .src { color: #d4d4d4; }
248
370
  .frame .caret { color: var(--red); }
249
371
  .hint { margin-top: 8px; color: #a3e635; }
372
+ .hostpanel {
373
+ position: fixed; right: 12px; bottom: 12px; z-index: 2147483002;
374
+ width: min(560px, calc(100vw - 24px)); max-height: 50vh; display: flex; flex-direction: column;
375
+ background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
376
+ box-shadow: 0 16px 48px rgba(0,0,0,.5); overflow: hidden;
377
+ font: 12px/1.45 "SF Mono", Menlo, Monaco, Consolas, "Courier New", monospace; color: var(--fg);
378
+ }
379
+ .hostpanel[hidden] { display: none; }
380
+ .hostpanel .card-head { padding: 8px 12px; }
381
+ .hostrows { overflow: auto; padding: 4px 0; }
382
+ .hostrow { padding: 2px 12px; white-space: pre-wrap; word-break: break-word; }
383
+ .hostrow.err { color: var(--red); }
384
+ .hostrow.sig { color: var(--muted); }
385
+ .mempanel {
386
+ position: fixed; right: 12px; bottom: 12px; z-index: 2147483002;
387
+ width: min(460px, calc(100vw - 24px));
388
+ background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
389
+ box-shadow: 0 16px 48px rgba(0,0,0,.5); overflow: hidden;
390
+ font: 12px/1.45 "SF Mono", Menlo, Monaco, Consolas, "Courier New", monospace; color: var(--fg);
391
+ }
392
+ .mempanel[hidden] { display: none; }
393
+ .mempanel .card-head { padding: 8px 12px; }
394
+ .memrow { display: flex; gap: 12px; justify-content: space-between; padding: 2px 12px; }
395
+ .memrow .k { color: var(--muted); }
396
+ .memrow .v { text-align: right; }
250
397
  .stack {
251
398
  margin-top: 10px; color: var(--muted); white-space: pre-wrap; word-break: break-word;
252
399
  max-height: 320px; overflow: auto;
@@ -257,6 +404,7 @@ var __krate_exports = (() => {
257
404
  this.cfg = devConfig();
258
405
  this.buildDiags = [];
259
406
  this.runtimeErrors = [];
407
+ this.hostSeq = 0;
260
408
  this.seen = /* @__PURE__ */ new Set();
261
409
  }
262
410
  init() {
@@ -277,7 +425,7 @@ var __krate_exports = (() => {
277
425
  if (this.cfg.toolbar) {
278
426
  this.toolbar = document.createElement("div");
279
427
  this.toolbar.className = "toolbar";
280
- this.toolbar.innerHTML = '<button class="tb-btn errors" type="button" title="Show errors">' + ICON_ALERT + '<span class="tb-label">Errors</span></button><div class="tb-center"><span class="brand">krate</span><span class="route"></span></div><button class="tb-btn reload" type="button" title="Reload">' + ICON_RELOAD + '<span class="tb-label">Reload</span></button>';
428
+ this.toolbar.innerHTML = '<button class="tb-btn errors" type="button" title="Show errors">' + ICON_ALERT + '<span class="tb-label">Errors</span></button>' + (this.cfg.events ? '<button class="tb-btn host" type="button" title="Host calls">' + ICON_HOST + '<span class="tb-label">Host</span></button>' : "") + '<div class="tb-center"><span class="brand">krate</span><span class="route"></span></div>' + (this.cfg.memory ? '<button class="tb-btn mem" type="button" title="Memory">' + ICON_MEM + '<span class="tb-label">Memory</span></button>' : "") + '<button class="tb-btn reload" type="button" title="Reload">' + ICON_RELOAD + '<span class="tb-label">Reload</span></button>';
281
429
  this.shadow.appendChild(this.toolbar);
282
430
  this.routeEl = this.toolbar.querySelector(".route");
283
431
  this.routeEl.textContent = location.pathname;
@@ -287,7 +435,8 @@ var __krate_exports = (() => {
287
435
  document.addEventListener("keydown", (e) => {
288
436
  if (e.key === "Escape") this.hide();
289
437
  });
290
- this.connectSSE();
438
+ if (this.cfg.frames) this.connectFrames();
439
+ else this.connectSSE();
291
440
  this.captureRuntimeErrors();
292
441
  }
293
442
  onClick(e) {
@@ -303,6 +452,22 @@ var __krate_exports = (() => {
303
452
  else this.hide();
304
453
  return;
305
454
  }
455
+ if (t.classList.contains("host")) {
456
+ this.toggleHost();
457
+ return;
458
+ }
459
+ if (t.classList.contains("mem")) {
460
+ this.toggleMemory();
461
+ return;
462
+ }
463
+ if (t.classList.contains("memclose")) {
464
+ this.toggleMemory(false);
465
+ return;
466
+ }
467
+ if (t.classList.contains("hostclose")) {
468
+ this.toggleHost(false);
469
+ return;
470
+ }
306
471
  if (t.classList.contains("reload")) {
307
472
  location.reload();
308
473
  return;
@@ -350,49 +515,159 @@ var __krate_exports = (() => {
350
515
  if (connectedOnce) location.reload();
351
516
  connectedOnce = true;
352
517
  });
353
- sse.addEventListener("reload", (e) => {
354
- if (this.buildDiags.length > 0) {
355
- this.buildDiags = [];
356
- this.setStatus(true);
357
- this.render();
358
- }
359
- let cssOnly = false;
518
+ const parse = (e) => {
360
519
  try {
361
- const d = JSON.parse(e.data);
362
- cssOnly = d.cssOnly === true;
363
- if (d.pages && Array.isArray(d.pages) && !d.pages.some(
364
- (p) => p === "/404" || p === "/500" || routeMatches(p, location.pathname, this.cfg.basePath)
365
- )) {
366
- return;
367
- }
520
+ return JSON.parse(e.data);
368
521
  } catch {
522
+ return null;
369
523
  }
370
- if (cssOnly) {
371
- this.swapStylesheets().catch(() => location.reload());
372
- return;
373
- }
374
- location.reload();
375
- });
376
- sse.addEventListener("build-error", (e) => {
377
- try {
378
- const d = JSON.parse(e.data);
379
- this.buildDiags = Array.isArray(d.diagnostics) ? d.diagnostics : [];
380
- } catch {
381
- this.buildDiags = [];
524
+ };
525
+ sse.addEventListener("reload", (e) => this.onReload(parse(e)));
526
+ sse.addEventListener("build-error", (e) => this.onBuildError(parse(e)));
527
+ sse.addEventListener("client-error", (e) => this.onClientError(parse(e)));
528
+ }
529
+ /**
530
+ * Krate Native: the same events as SSE, as `{hmr: {type, ...}}` frames on a
531
+ * streaming fetch. Reconnects after the stream ends; a second "connected"
532
+ * means the shell restarted, so the page is stale.
533
+ */
534
+ connectFrames() {
535
+ let connectedOnce = false;
536
+ const run = async () => {
537
+ const res = await fetch(this.cfg.frames, { cache: "no-store" });
538
+ if (!res.ok || !res.body) throw new Error("hmr stream unavailable");
539
+ const reader = res.body.getReader();
540
+ let buf = new Uint8Array(0);
541
+ for (; ; ) {
542
+ const { done, value } = await reader.read();
543
+ if (done) return;
544
+ const joined = new Uint8Array(buf.length + value.length);
545
+ joined.set(buf);
546
+ joined.set(value, buf.length);
547
+ const parsed = parseFrames(joined);
548
+ buf = parsed.rest;
549
+ for (const v of parsed.values) {
550
+ const m = v?.hmr;
551
+ if (!m || typeof m !== "object") continue;
552
+ if (m.type === "connected") {
553
+ if (connectedOnce) location.reload();
554
+ connectedOnce = true;
555
+ } else if (m.type === "reload") this.onReload(m);
556
+ else if (m.type === "build-error") this.onBuildError(m);
557
+ else if (m.type === "client-error") this.onClientError(m);
558
+ }
382
559
  }
383
- this.setStatus(this.buildDiags.length === 0);
560
+ };
561
+ const loop = () => {
562
+ run().then(
563
+ () => setTimeout(loop, 1e3),
564
+ () => setTimeout(loop, 1e3)
565
+ );
566
+ };
567
+ loop();
568
+ }
569
+ onReload(d) {
570
+ if (this.buildDiags.length > 0) {
571
+ this.buildDiags = [];
572
+ this.setStatus(true);
384
573
  this.render();
385
- });
386
- sse.addEventListener("client-error", (e) => {
387
- try {
388
- const d = JSON.parse(e.data);
389
- if (!d.clientError) return;
390
- const err = d.clientError;
391
- if (this.seen.has(err.message + "|" + (err.stack || ""))) return;
392
- this.addRuntime("Runtime Error", err.message, err.stack);
393
- } catch {
574
+ }
575
+ let cssOnly = false;
576
+ if (d) {
577
+ cssOnly = d.cssOnly === true;
578
+ const pages = d.pages;
579
+ if (pages && Array.isArray(pages) && !pages.some((p) => p === "/404" || p === "/500" || routeMatches(p, location.pathname, this.cfg.basePath))) {
580
+ return;
394
581
  }
395
- });
582
+ }
583
+ if (cssOnly) {
584
+ this.swapStylesheets().catch(() => location.reload());
585
+ return;
586
+ }
587
+ location.reload();
588
+ }
589
+ onBuildError(d) {
590
+ this.buildDiags = d && Array.isArray(d.diagnostics) ? d.diagnostics : [];
591
+ this.setStatus(this.buildDiags.length === 0);
592
+ this.render();
593
+ }
594
+ onClientError(d) {
595
+ if (!d || !d.clientError) return;
596
+ const err = d.clientError;
597
+ if (this.seen.has(err.message + "|" + (err.stack || ""))) return;
598
+ this.addRuntime("Runtime Error", err.message, err.stack);
599
+ }
600
+ toggleHost(force) {
601
+ if (!this.cfg.events) return;
602
+ if (!this.hostPanel) {
603
+ const el = document.createElement("div");
604
+ el.className = "hostpanel";
605
+ el.hidden = true;
606
+ el.innerHTML = '<div class="card-head"><span class="title" style="color:inherit">Host calls</span><span class="spacer"></span><button class="btn hostclose" type="button">Close</button></div><div class="hostrows"></div>';
607
+ this.shadow.appendChild(el);
608
+ this.hostPanel = el;
609
+ this.hostRows = el.querySelector(".hostrows");
610
+ }
611
+ const open = force ?? this.hostPanel.hidden;
612
+ this.hostPanel.hidden = !open;
613
+ if (this.hostTimer) clearInterval(this.hostTimer);
614
+ this.hostTimer = void 0;
615
+ if (open) {
616
+ void this.pollHost();
617
+ this.hostTimer = setInterval(() => void this.pollHost(), 1e3);
618
+ }
619
+ }
620
+ /** The Memory panel samples only while it is open (every 2 seconds). */
621
+ toggleMemory(force) {
622
+ if (!this.cfg.memory) return;
623
+ if (!this.memPanel) {
624
+ const el = document.createElement("div");
625
+ el.className = "mempanel";
626
+ el.hidden = true;
627
+ el.innerHTML = '<div class="card-head"><span class="title" style="color:inherit">Memory</span><span class="spacer"></span><button class="btn memclose" type="button">Close</button></div><div class="memrows"></div>';
628
+ this.shadow.appendChild(el);
629
+ this.memPanel = el;
630
+ this.memRows = el.querySelector(".memrows");
631
+ }
632
+ const open = force ?? this.memPanel.hidden;
633
+ this.memPanel.hidden = !open;
634
+ if (this.memTimer) clearInterval(this.memTimer);
635
+ this.memTimer = void 0;
636
+ if (open) {
637
+ void this.pollMemory();
638
+ this.memTimer = setInterval(() => void this.pollMemory(), 2e3);
639
+ }
640
+ }
641
+ async pollMemory() {
642
+ let rep = null;
643
+ try {
644
+ const res = await fetch(this.cfg.memory, { cache: "no-store" });
645
+ if (res.ok) rep = await res.json();
646
+ } catch {
647
+ }
648
+ const rows = memoryRows(rep, document.getElementsByTagName("*").length, jsHeapBytes());
649
+ this.memRows.innerHTML = rows.map(([k, v]) => '<div class="memrow"><span class="k">' + esc(k) + '</span><span class="v">' + esc(v) + "</span></div>").join("");
650
+ }
651
+ async pollHost() {
652
+ try {
653
+ const res = await fetch(this.cfg.events + "?since=" + this.hostSeq, { cache: "no-store" });
654
+ if (!res.ok) return;
655
+ const parsed = parseFrames(new Uint8Array(await res.arrayBuffer()));
656
+ const rows = this.hostRows;
657
+ const pinned = rows.scrollTop + rows.clientHeight >= rows.scrollHeight - 8;
658
+ for (const v of parsed.values) {
659
+ const ev = v?.ev;
660
+ if (!ev) continue;
661
+ this.hostSeq = Math.max(this.hostSeq, ev.seq);
662
+ const row = document.createElement("div");
663
+ row.className = "hostrow" + (ev.code ? " err" : ev.kind === "signal" ? " sig" : "");
664
+ row.textContent = hostEventLine(ev);
665
+ rows.appendChild(row);
666
+ }
667
+ while (rows.childElementCount > 200) rows.firstElementChild.remove();
668
+ if (pinned) rows.scrollTop = rows.scrollHeight;
669
+ } catch {
670
+ }
396
671
  }
397
672
  /**
398
673
  * Apply a CSS-only rebuild without a full reload: fetch the freshly built
@@ -438,44 +713,95 @@ var __krate_exports = (() => {
438
713
  }
439
714
  }
440
715
  captureRuntimeErrors() {
441
- window.addEventListener("error", (e) => {
442
- const stack = e.error?.stack || "";
443
- const message = e.message || "Unknown error";
444
- this.report({
445
- message,
446
- stack,
447
- url: e.filename,
448
- line: e.lineno,
449
- col: e.colno,
450
- kind: "error"
451
- });
452
- this.addRuntime("Runtime Error", message, stack);
453
- });
454
- window.addEventListener("unhandledrejection", (e) => {
455
- const reason = e.reason;
456
- const message = reason instanceof Error ? reason.message : String(reason);
457
- const stack = reason instanceof Error ? reason.stack : "";
458
- this.report({ message, stack, url: location.href, kind: "unhandledrejection" });
459
- this.addRuntime("Unhandled Promise Rejection", message, stack);
716
+ window.addEventListener("error", (e) => this.onError(e));
717
+ window.addEventListener("unhandledrejection", (e) => this.onRejection(e));
718
+ window.addEventListener("error", (e) => this.onResourceError(e), true);
719
+ this.captureConsole();
720
+ const early = window.__KRATE_EARLY_ERRORS__;
721
+ window.__KRATE_EARLY_ERRORS__ = void 0;
722
+ for (const e of early ?? []) {
723
+ if (e.type === "console") this.onConsole(e.message, e.stack);
724
+ else if (e.type === "error" && e.target && e.target !== window) this.onResourceError(e);
725
+ else if (e.type === "error") this.onError(e);
726
+ else this.onRejection(e);
727
+ }
728
+ }
729
+ captureConsole() {
730
+ const c = console;
731
+ const orig = c.error;
732
+ c.error = (...args) => {
733
+ try {
734
+ const first = args.find((a) => a instanceof Error);
735
+ this.onConsole(args.map(formatArg).join(" "), first?.stack);
736
+ } catch {
737
+ }
738
+ return orig.apply(c, args);
739
+ };
740
+ }
741
+ onConsole(message, stack) {
742
+ this.report({ message, stack, url: location.href, kind: "console" });
743
+ this.addRuntime("console.error", message, stack);
744
+ }
745
+ onResourceError(e) {
746
+ const t = e.target;
747
+ if (!t || t === window || !t.tagName) return;
748
+ const src = t.src || t.href || "";
749
+ if (!src) return;
750
+ const message = "failed to load <" + t.tagName.toLowerCase() + "> " + src;
751
+ this.report({ message, url: src, kind: "resource" });
752
+ this.addRuntime("Resource Error", message);
753
+ }
754
+ onError(e) {
755
+ const stack = e.error?.stack || "";
756
+ const message = e.message || "Unknown error";
757
+ const hostStack = hostStackOf(e.error);
758
+ this.report({
759
+ message,
760
+ stack: hostStack ? stack + "\nHost stack:\n" + hostStack : stack,
761
+ url: e.filename,
762
+ line: e.lineno,
763
+ col: e.colno,
764
+ kind: "error"
460
765
  });
766
+ this.addRuntime("Runtime Error", message, stack, hostStack);
767
+ }
768
+ onRejection(e) {
769
+ const reason = e.reason;
770
+ const message = reason instanceof Error ? reason.message : String(reason);
771
+ const stack = reason instanceof Error ? reason.stack : "";
772
+ const hostStack = hostStackOf(reason);
773
+ this.report({ message, stack: hostStack ? (stack || "") + "\nHost stack:\n" + hostStack : stack, url: location.href, kind: "unhandledrejection" });
774
+ this.addRuntime("Unhandled Promise Rejection", message, stack, hostStack);
461
775
  }
462
776
  report(payload) {
463
777
  const key = payload.message + "|" + (payload.stack || "");
464
778
  if (this.seen.has(key)) return;
465
779
  this.seen.add(key);
780
+ const win = globalThis.__KRATE_WINDOW__;
781
+ if (typeof win === "string" && win) payload.window = win;
782
+ if (!payload.stack || !payload.stack.includes(".js")) {
783
+ this.send(payload);
784
+ return;
785
+ }
786
+ const raw = payload.stack;
787
+ Promise.race([symbolicateStack(raw), new Promise((r) => setTimeout(() => r(raw), 1500))]).catch(() => raw).then((stack) => this.send({ ...payload, stack }));
788
+ }
789
+ send(payload) {
790
+ const body = JSON.stringify(payload);
466
791
  try {
467
- const body = JSON.stringify(payload);
468
- if (navigator.sendBeacon) {
469
- navigator.sendBeacon(this.cfg.errors, new Blob([body], { type: "application/json" }));
470
- } else {
471
- fetch(this.cfg.errors, { method: "POST", body, headers: { "Content-Type": "application/json" }, keepalive: true }).catch(() => {
472
- });
792
+ if (!this.cfg.frames && navigator.sendBeacon) {
793
+ if (navigator.sendBeacon(this.cfg.errors, new Blob([body], { type: "application/json" }))) return;
473
794
  }
474
795
  } catch {
475
796
  }
797
+ try {
798
+ fetch(this.cfg.errors, { method: "POST", body, headers: { "Content-Type": "application/json" }, keepalive: true }).catch(() => {
799
+ });
800
+ } catch {
801
+ }
476
802
  }
477
- addRuntime(title, message, stack) {
478
- const entry = { title, message, stack };
803
+ addRuntime(title, message, stack, hostStack) {
804
+ const entry = { title, message, stack, hostStack };
479
805
  this.runtimeErrors.push(entry);
480
806
  this.setStatus(false);
481
807
  this.render();
@@ -508,7 +834,8 @@ var __krate_exports = (() => {
508
834
  return '<div class="diag">' + actions + '<div class="msg">' + esc(d.message) + "</div>" + this.frameHTML(d) + hint + "</div>";
509
835
  }
510
836
  runtimeHTML(r) {
511
- const stack = r.stack ? '<div class="stack">' + esc(r.stack) + "</div>" : "";
837
+ const host = r.hostStack ? '<div class="stack">Host stack:\n' + esc(r.hostStack) + "</div>" : "";
838
+ const stack = host + (r.stack ? '<div class="stack">' + esc(r.stack) + "</div>" : "");
512
839
  return '<div class="diag"><div class="msg">' + esc(r.message) + "</div>" + stack + "</div>";
513
840
  }
514
841
  render() {