@hansenexus/hud 0.4.0 → 0.5.1

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 (159) hide show
  1. package/README.md +634 -12
  2. package/dist/a11y-B5DUaiCs.js +246 -0
  3. package/dist/a11y-panel-p_t4eD7p.js +207 -0
  4. package/dist/{capabilities-CjulXFQX.js → access-C7zlaqj6.js} +1 -90
  5. package/dist/{access-CNvJOwKM.d.ts → access-D_ewAEJG.d.ts} +11 -4
  6. package/dist/access-panel-CEfTr9ZI.js +123 -0
  7. package/dist/adapter-BYoxPYTI.d.ts +33 -0
  8. package/dist/adapter-hcwmJtNx.js +65 -0
  9. package/dist/auto/runtime.d.ts +65 -0
  10. package/dist/auto/runtime.js +50 -0
  11. package/dist/auto.d.ts +15 -0
  12. package/dist/auto.js +33 -0
  13. package/dist/capabilities-rDSKjXpq.js +110 -0
  14. package/dist/capture-CwrIwLqi.js +300 -0
  15. package/dist/capture-buffer-Bbbo6omF.d.ts +35 -0
  16. package/dist/cli.d.ts +1 -0
  17. package/dist/cli.js +275 -0
  18. package/dist/data-panel-DhxAmP91.js +817 -0
  19. package/dist/data-store-ChF1td_P.js +164 -0
  20. package/dist/define-hud-CHPedaQ5.d.ts +50 -0
  21. package/dist/design-D0kGgPoP.js +87 -0
  22. package/dist/detect-BxHZ0pxW.d.ts +25 -0
  23. package/dist/detect-CHZiSeZs.js +63 -0
  24. package/dist/dispatch-panel-B-xr_Giy.js +279 -0
  25. package/dist/{env-vEjyJfPW.d.ts → env-CApnbI0r.d.ts} +2 -0
  26. package/dist/{env-DfxpPfs9.js → env-CaiFPIhF.js} +3 -0
  27. package/dist/file-xdSaQ2uR.js +103 -0
  28. package/dist/flags-DVtaJ8pQ.d.ts +58 -0
  29. package/dist/flags.d.ts +2 -0
  30. package/dist/flags.js +82 -0
  31. package/dist/force-state-panel-_otVYh9i.js +97 -0
  32. package/dist/generate-C8lQl3Eh.js +53 -0
  33. package/dist/handler-BpVhxEmU.js +106 -0
  34. package/dist/{handler-WmE4Lu7l.d.ts → handler-DtYVCZY6.d.ts} +1 -1
  35. package/dist/index-BAV_1W8a.d.ts +43 -0
  36. package/dist/index-BJ26pse9.d.ts +70 -0
  37. package/dist/index-BnQ0dTZf.d.ts +19 -0
  38. package/dist/index-CwfX-W87.d.ts +72 -0
  39. package/dist/index-DcH9V3cU.d.ts +48 -0
  40. package/dist/index-DqOyNf1u.d.ts +65 -0
  41. package/dist/index-DstI_ike.d.ts +66 -0
  42. package/dist/index-Dxz4fn4j.d.ts +42 -0
  43. package/dist/index-Hhh7OXzU.d.ts +129 -0
  44. package/dist/index-ReFbhQ9p.d.ts +25 -0
  45. package/dist/index-RgXfMBuW.d.ts +48 -0
  46. package/dist/{index-BYsdS-7d.d.ts → index-gCZBO53s.d.ts} +21 -3
  47. package/dist/index.d.ts +5 -5
  48. package/dist/index.js +710 -69
  49. package/dist/inspect-C9Ft8fqn.js +142 -0
  50. package/dist/inspect-root-BkaupBMp.js +429 -0
  51. package/dist/meta-Bv0sEao3.js +6 -0
  52. package/dist/meta-C_G7MB5w.d.ts +49 -0
  53. package/dist/mocks-CA-wM2SB.d.ts +109 -0
  54. package/dist/mocks-ESiRqk9o.js +150 -0
  55. package/dist/msw-browser-wjaYgkB7.d.ts +31 -0
  56. package/dist/next/config.cjs +201 -0
  57. package/dist/next/config.d.cts +87 -0
  58. package/dist/next/config.d.ts +64 -0
  59. package/dist/next/config.js +84 -0
  60. package/dist/next.d.ts +14 -4
  61. package/dist/next.js +5 -3
  62. package/dist/observe-CdDnDBRP.js +89 -0
  63. package/dist/observe-panel-Cgp1M8oT.js +347 -0
  64. package/dist/ops-panel-CEqDe_UT.js +234 -0
  65. package/dist/{payload-CLKDVZWl.d.ts → payload-MUQ9ciER.d.ts} +25 -1
  66. package/dist/plugins/a11y.d.ts +2 -0
  67. package/dist/plugins/a11y.js +2 -0
  68. package/dist/plugins/access/server.d.ts +5 -5
  69. package/dist/plugins/access/server.js +4 -3
  70. package/dist/plugins/access.d.ts +5 -5
  71. package/dist/plugins/access.js +16 -215
  72. package/dist/plugins/agent/server.d.ts +3 -38
  73. package/dist/plugins/agent/server.js +47 -21
  74. package/dist/plugins/agent.d.ts +4 -4
  75. package/dist/plugins/agent.js +6 -348
  76. package/dist/plugins/capture/server.d.ts +92 -0
  77. package/dist/plugins/capture/server.js +248 -0
  78. package/dist/plugins/capture.d.ts +24 -0
  79. package/dist/plugins/capture.js +70 -0
  80. package/dist/plugins/data/msw-browser.d.ts +2 -0
  81. package/dist/plugins/data/msw-browser.js +39 -0
  82. package/dist/plugins/data/msw-node.d.ts +36 -0
  83. package/dist/plugins/data/msw-node.js +68 -0
  84. package/dist/plugins/data/server.d.ts +257 -0
  85. package/dist/plugins/data/server.js +717 -0
  86. package/dist/plugins/data.d.ts +6 -0
  87. package/dist/plugins/data.js +51 -0
  88. package/dist/plugins/design/server.d.ts +54 -0
  89. package/dist/plugins/design/server.js +198 -0
  90. package/dist/plugins/design.d.ts +4 -124
  91. package/dist/plugins/design.js +4 -619
  92. package/dist/plugins/inspect/convex.d.ts +2 -0
  93. package/dist/plugins/inspect/convex.js +486 -0
  94. package/dist/plugins/inspect/react-query.d.ts +2 -0
  95. package/dist/plugins/inspect/react-query.js +237 -0
  96. package/dist/plugins/inspect/sockets.d.ts +51 -0
  97. package/dist/plugins/inspect/sockets.js +397 -0
  98. package/dist/plugins/inspect/swr.d.ts +2 -0
  99. package/dist/plugins/inspect/swr.js +218 -0
  100. package/dist/plugins/inspect.d.ts +4 -0
  101. package/dist/plugins/inspect.js +3 -0
  102. package/dist/plugins/observe.d.ts +4 -64
  103. package/dist/plugins/observe.js +1 -616
  104. package/dist/plugins/ops/server.d.ts +1 -1
  105. package/dist/plugins/ops/server.js +2 -1
  106. package/dist/plugins/ops.d.ts +2 -1
  107. package/dist/plugins/ops.js +15 -336
  108. package/dist/plugins/routes/server.d.ts +88 -0
  109. package/dist/plugins/routes/server.js +62 -0
  110. package/dist/plugins/routes.d.ts +3 -0
  111. package/dist/plugins/routes.js +216 -0
  112. package/dist/plugins/seo.d.ts +2 -0
  113. package/dist/plugins/seo.js +2 -0
  114. package/dist/plugins/vitals.d.ts +2 -25
  115. package/dist/plugins/vitals.js +3 -2
  116. package/dist/{plugins-CQ_0w1ot.d.ts → plugins-BZvDqGx5.d.ts} +33 -3
  117. package/dist/poll-CjKsWZi8.js +89 -0
  118. package/dist/requests-BwD6MKEq.js +282 -0
  119. package/dist/requests-CJrUf465.d.ts +50 -0
  120. package/dist/review.js +2 -2
  121. package/dist/same-origin-BGet22CC.js +21 -0
  122. package/dist/scan-DzJNXqOk.js +165 -0
  123. package/dist/seo-Dq9q_QkO.js +551 -0
  124. package/dist/seo-panel-CfiY-TnT.js +124 -0
  125. package/dist/server.d.ts +2 -2
  126. package/dist/server.js +2 -105
  127. package/dist/sessions-panel-COvhPAzv.js +98 -0
  128. package/dist/{shortcut-U6ftVY06.js → shortcut-s_mjGeU8.js} +1 -44
  129. package/dist/{types-QJUajwWq.js → state-CjjZPjPJ.js} +13 -8
  130. package/dist/store/convex.d.ts +33 -0
  131. package/dist/store/convex.js +52 -0
  132. package/dist/{vitals-panel-Dvc5dnR-.js → store-CQqIIKTc.js} +1 -29
  133. package/dist/store-Ync9eb1G.js +67 -0
  134. package/dist/store.d.ts +76 -0
  135. package/dist/store.js +188 -0
  136. package/dist/{styles--slk86ZQ.js → styles-BA8ueHoV.js} +13 -3
  137. package/dist/styles-DKU_P8MH.js +139 -0
  138. package/dist/styles-DPpDPmzi.js +68 -0
  139. package/dist/tokens-panel-CbMQfasZ.js +142 -0
  140. package/dist/types-B-ulf2Jp.js +8 -0
  141. package/dist/types-BIgGf-0C.d.ts +25 -0
  142. package/dist/types-BIkI-qN8.js +30 -0
  143. package/dist/{types-DADBRiGk.d.ts → types-Cxh4twqc.d.ts} +1 -1
  144. package/dist/types-DIdPfjtV.js +6 -0
  145. package/dist/types-DaJcsEjO.d.ts +44 -0
  146. package/dist/types-Dn68psVz.d.ts +118 -0
  147. package/dist/types-szkr2QOF.d.ts +39 -0
  148. package/dist/unlock-store-4V3vR4Eg.js +96 -0
  149. package/dist/variant-groups-TIfO3NIj.js +137 -0
  150. package/dist/variant-groups-aAIrcNK5.d.ts +36 -0
  151. package/dist/variants-panel-BDoSkwBJ.js +457 -0
  152. package/dist/variants-types-CcRf119M.d.ts +63 -0
  153. package/dist/variants.d.ts +43 -0
  154. package/dist/variants.js +83 -0
  155. package/dist/vitals-panel-BCmMUntW.js +30 -0
  156. package/dist/vite.d.ts +37 -0
  157. package/dist/vite.js +134 -0
  158. package/package.json +72 -5
  159. package/dist/define-hud-BBzsSl8b.d.ts +0 -36
@@ -0,0 +1,248 @@
1
+ import { n as HUD_HOST_TAG } from "../../marker-uZYPOJ4M.js";
2
+ import { i as definePlugin } from "../../plugins-DVy_vaYr.js";
3
+ import { n as CAPTURE_ID, t as CAPTURE_CAPABILITY } from "../../types-DIdPfjtV.js";
4
+ //#region src/plugins/capture/server.ts
5
+ const INSTALL_HINT = "bun add -d playwright && bunx playwright install chromium";
6
+ const BROWSER_HINT = "bunx playwright install chromium";
7
+ /** A screenshot cannot be taken on this server; the hint says what to install. */
8
+ var ScreenshotUnavailable = class extends Error {
9
+ hint;
10
+ constructor(message, hint) {
11
+ super(message);
12
+ this.hint = hint;
13
+ this.name = "ScreenshotUnavailable";
14
+ }
15
+ };
16
+ const MAX_SIDE = 4096;
17
+ const MIN_SIDE = 320;
18
+ const DEFAULT_VIEWPORT = {
19
+ width: 1280,
20
+ height: 800
21
+ };
22
+ function message(error) {
23
+ return error instanceof Error ? error.message : String(error);
24
+ }
25
+ function isMissingModule(error) {
26
+ const code = error?.code;
27
+ if (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") return true;
28
+ return /cannot find (module|package)|failed to (resolve|fetch)|not found/i.test(message(error));
29
+ }
30
+ /**
31
+ * `import("playwright")` on first use. A static specifier keeps the import
32
+ * analysable (Next externalises `playwright` on the server by default); the
33
+ * playground aliases it to a stub, as a browser cannot run it anyway.
34
+ */
35
+ async function launchChromium() {
36
+ let playwright;
37
+ try {
38
+ playwright = await import("playwright");
39
+ } catch (error) {
40
+ if (isMissingModule(error)) throw new ScreenshotUnavailable("Playwright is not installed", INSTALL_HINT);
41
+ throw error;
42
+ }
43
+ try {
44
+ return await playwright.chromium.launch();
45
+ } catch (error) {
46
+ throw new ScreenshotUnavailable(`Chromium failed to launch: ${message(error)}`, BROWSER_HINT);
47
+ }
48
+ }
49
+ function isRecord(value) {
50
+ return typeof value === "object" && value !== null && !Array.isArray(value);
51
+ }
52
+ function clampSide(value, fallback) {
53
+ if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
54
+ return Math.min(MAX_SIDE, Math.max(MIN_SIDE, Math.round(value)));
55
+ }
56
+ function parseBody(value) {
57
+ if (!isRecord(value) || typeof value.url !== "string") return null;
58
+ if (value.selector !== void 0 && typeof value.selector !== "string") return null;
59
+ const viewport = isRecord(value.viewport) ? value.viewport : {};
60
+ return {
61
+ url: value.url,
62
+ ...value.selector ? { selector: value.selector } : {},
63
+ viewport: {
64
+ width: clampSide(viewport.width, DEFAULT_VIEWPORT.width),
65
+ height: clampSide(viewport.height, DEFAULT_VIEWPORT.height)
66
+ }
67
+ };
68
+ }
69
+ /**
70
+ * The origins a screenshot may render: the one the request was made to and,
71
+ * when the browser sent it, the page's own `Origin`. Anything else is not
72
+ * this app, and a dev server must not become a headless browser for the web.
73
+ */
74
+ function allowedOrigins(request) {
75
+ const origins = /* @__PURE__ */ new Set();
76
+ try {
77
+ origins.add(new URL(request.url).origin);
78
+ } catch {}
79
+ const origin = request.headers.get("origin");
80
+ if (origin && origin !== "null") origins.add(origin);
81
+ return origins;
82
+ }
83
+ /** Same-origin only, http(s) only. */
84
+ function sameOriginUrl(url, request) {
85
+ let target;
86
+ try {
87
+ target = new URL(url);
88
+ } catch {
89
+ return null;
90
+ }
91
+ if (target.protocol !== "http:" && target.protocol !== "https:") return null;
92
+ return allowedOrigins(request).has(target.origin) ? target : null;
93
+ }
94
+ /** Width and height from a PNG's IHDR chunk; 0×0 when the bytes are not a PNG. */
95
+ function pngSize(png) {
96
+ if (png.length < 24 || [
97
+ 137,
98
+ 80,
99
+ 78,
100
+ 71,
101
+ 13,
102
+ 10,
103
+ 26,
104
+ 10
105
+ ].some((byte, i) => png[i] !== byte)) return {
106
+ width: 0,
107
+ height: 0
108
+ };
109
+ const view = new DataView(png.buffer, png.byteOffset, png.byteLength);
110
+ return {
111
+ width: view.getUint32(16),
112
+ height: view.getUint32(20)
113
+ };
114
+ }
115
+ function toBase64(bytes) {
116
+ if (typeof Buffer !== "undefined") return Buffer.from(bytes).toString("base64");
117
+ let binary = "";
118
+ for (const byte of bytes) binary += String.fromCharCode(byte);
119
+ return btoa(binary);
120
+ }
121
+ /** The developer's cookies for the page, so the headless render sees the same session. */
122
+ function cookiesFor(request, url) {
123
+ const header = request.headers.get("cookie");
124
+ if (!header) return [];
125
+ const cookies = [];
126
+ for (const pair of header.split(";")) {
127
+ const eq = pair.indexOf("=");
128
+ if (eq <= 0) continue;
129
+ const name = pair.slice(0, eq).trim();
130
+ if (name) cookies.push({
131
+ name,
132
+ value: pair.slice(eq + 1).trim(),
133
+ url: url.origin
134
+ });
135
+ }
136
+ return cookies;
137
+ }
138
+ /**
139
+ * A screenshot renders a page with the developer's cookies, so a page on
140
+ * another origin must not be able to trigger one through the browser. A JSON
141
+ * content type forces a CORS preflight that this route never answers, and
142
+ * `Sec-Fetch-Site` rejects cross-site requests outright where the browser
143
+ * sends it.
144
+ */
145
+ function refuseCrossSite(request) {
146
+ const site = request.headers.get("sec-fetch-site");
147
+ if (site && site !== "same-origin" && site !== "none") return Response.json({
148
+ ok: false,
149
+ error: "cross-site request refused"
150
+ }, { status: 403 });
151
+ if (!request.headers.get("content-type")?.toLowerCase().startsWith("application/json")) return Response.json({
152
+ ok: false,
153
+ error: "expected application/json"
154
+ }, { status: 415 });
155
+ return null;
156
+ }
157
+ const NO_STORE = { "cache-control": "no-store" };
158
+ function fail(status, error, hint) {
159
+ const body = {
160
+ ok: false,
161
+ error,
162
+ ...hint ? { hint } : {}
163
+ };
164
+ return Response.json(body, {
165
+ status,
166
+ headers: NO_STORE
167
+ });
168
+ }
169
+ /**
170
+ * Server half of the `capture` plugin: `POST /screenshot` renders a
171
+ * same-origin URL in headless Chromium at the grab's viewport, with the
172
+ * request's cookies, crops to the grabbed selector (the viewport when the
173
+ * element is not there) and answers the PNG base64 encoded. Non-same-origin
174
+ * URLs answer 400; a server without Playwright answers 503 with a setup hint,
175
+ * which the Dispatch panel shows while the dispatch itself goes on without
176
+ * an image.
177
+ */
178
+ function screenshot({ launch = launchChromium, loadTimeoutMs = 15e3, elementTimeoutMs = 5e3 } = {}) {
179
+ async function render(request, { url, selector, viewport = DEFAULT_VIEWPORT }) {
180
+ const browser = await launch();
181
+ try {
182
+ const context = await browser.newContext({
183
+ viewport,
184
+ deviceScaleFactor: 1
185
+ });
186
+ const target = new URL(url);
187
+ const cookies = cookiesFor(request, target);
188
+ if (cookies.length > 0) await context.addCookies(cookies);
189
+ const page = await context.newPage();
190
+ try {
191
+ await page.goto(target.href, {
192
+ waitUntil: "networkidle",
193
+ timeout: loadTimeoutMs
194
+ });
195
+ } catch (error) {
196
+ if (!/timeout/i.test(message(error))) throw error;
197
+ }
198
+ await page.addStyleTag({ content: `${HUD_HOST_TAG}{display:none!important}` });
199
+ let png;
200
+ let scope = "viewport";
201
+ if (selector) try {
202
+ png = await page.locator(selector).first().screenshot({
203
+ type: "png",
204
+ timeout: elementTimeoutMs
205
+ });
206
+ scope = "element";
207
+ } catch {
208
+ png = void 0;
209
+ }
210
+ png ??= await page.screenshot({
211
+ type: "png",
212
+ timeout: elementTimeoutMs
213
+ });
214
+ const { width, height } = pngSize(png);
215
+ const body = {
216
+ ok: true,
217
+ mime: "image/png",
218
+ base64: toBase64(png),
219
+ width,
220
+ height,
221
+ scope
222
+ };
223
+ return Response.json(body, { headers: NO_STORE });
224
+ } finally {
225
+ await browser.close().catch(() => {});
226
+ }
227
+ }
228
+ return definePlugin({
229
+ id: CAPTURE_ID,
230
+ capability: CAPTURE_CAPABILITY,
231
+ routes: { "/screenshot": { POST: async (request) => {
232
+ const refusal = refuseCrossSite(request);
233
+ if (refusal) return refusal;
234
+ const body = parseBody(await request.json().catch(() => null));
235
+ if (!body) return fail(400, "invalid screenshot request");
236
+ if (!sameOriginUrl(body.url, request)) return fail(400, "url must be same-origin");
237
+ try {
238
+ return await render(request, body);
239
+ } catch (error) {
240
+ if (error instanceof ScreenshotUnavailable) return fail(503, error.message, error.hint);
241
+ console.error("hud capture: screenshot failed", error);
242
+ return fail(502, `screenshot failed: ${message(error)}`);
243
+ }
244
+ } } }
245
+ });
246
+ }
247
+ //#endregion
248
+ export { BROWSER_HINT, CAPTURE_CAPABILITY, INSTALL_HINT, ScreenshotUnavailable, pngSize, sameOriginUrl, screenshot };
@@ -0,0 +1,24 @@
1
+ import { r as GrabPayload } from "../payload-MUQ9ciER.js";
2
+ import { s as HudPluginContext } from "../plugins-BZvDqGx5.js";
3
+ import "../index-gCZBO53s.js";
4
+ import { n as ScreenshotRequest, r as ScreenshotResponse, t as CAPTURE_CAPABILITY } from "../types-BIgGf-0C.js";
5
+ //#region src/plugins/capture/index.d.ts
6
+ export interface ScreenshotOptions {
7
+ /** Give up on a screenshot after this long. Default 30 s. */
8
+ timeoutMs?: number;
9
+ }
10
+ /**
11
+ * Client half of the `capture` plugin. No panel: on every grab it asks the
12
+ * server half to render the page headless and crop to the grabbed element,
13
+ * then attaches the PNG to the grab (`payload.screenshot`), where the Dispatch
14
+ * panel shows it and sends it along. Without Playwright on the server the
15
+ * grab carries the reason and a setup hint instead.
16
+ */
17
+ export declare function screenshot({ timeoutMs }?: ScreenshotOptions): {
18
+ readonly id: "capture";
19
+ readonly title: "Screenshot";
20
+ readonly capability: "capture.screenshot";
21
+ readonly onGrab: (payload: GrabPayload, ctx: HudPluginContext) => void;
22
+ };
23
+ //#endregion
24
+ export { CAPTURE_CAPABILITY, type ScreenshotRequest, type ScreenshotResponse };
@@ -0,0 +1,70 @@
1
+ import { i as definePlugin, n as HudApiError } from "../plugins-DVy_vaYr.js";
2
+ import { n as CAPTURE_ID, t as CAPTURE_CAPABILITY } from "../types-DIdPfjtV.js";
3
+ //#region src/plugins/capture/index.tsx
4
+ function failure(error) {
5
+ if (error instanceof HudApiError) {
6
+ const body = error.body;
7
+ if (body && typeof body.error === "string") return body.hint ? {
8
+ reason: body.error,
9
+ hint: body.hint
10
+ } : { reason: body.error };
11
+ if (error.status === 404) return {
12
+ reason: "no screenshot route",
13
+ hint: "Mount screenshot() from @hansenexus/hud/plugins/capture/server in the HUD route."
14
+ };
15
+ return { reason: `screenshot route answered ${error.status}` };
16
+ }
17
+ return { reason: error instanceof Error ? error.message : String(error) };
18
+ }
19
+ /**
20
+ * Client half of the `capture` plugin. No panel: on every grab it asks the
21
+ * server half to render the page headless and crop to the grabbed element,
22
+ * then attaches the PNG to the grab (`payload.screenshot`), where the Dispatch
23
+ * panel shows it and sends it along. Without Playwright on the server the
24
+ * grab carries the reason and a setup hint instead.
25
+ */
26
+ function screenshot({ timeoutMs = 3e4 } = {}) {
27
+ return definePlugin({
28
+ id: CAPTURE_ID,
29
+ title: "Screenshot",
30
+ capability: CAPTURE_CAPABILITY,
31
+ onGrab: (payload, ctx) => {
32
+ const current = () => ctx.grab.getState().payload;
33
+ const mine = () => current()?.grabbedAt === payload.grabbedAt;
34
+ ctx.grab.amend({ screenshot: { status: "pending" } });
35
+ ctx.api("/screenshot", {
36
+ method: "POST",
37
+ headers: { "content-type": "application/json" },
38
+ body: JSON.stringify({
39
+ url: payload.url,
40
+ selector: payload.anchor.selector,
41
+ viewport: payload.viewport
42
+ }),
43
+ signal: AbortSignal.timeout(timeoutMs)
44
+ }).then((res) => {
45
+ if (!mine()) return;
46
+ if (res.ok) {
47
+ const { ok: _ok, ...image } = res;
48
+ ctx.grab.amend({ screenshot: {
49
+ status: "ready",
50
+ ...image
51
+ } });
52
+ } else {
53
+ const { error: reason, hint } = res;
54
+ ctx.grab.amend({ screenshot: {
55
+ status: "unavailable",
56
+ reason,
57
+ ...hint ? { hint } : {}
58
+ } });
59
+ }
60
+ }).catch((error) => {
61
+ if (mine()) ctx.grab.amend({ screenshot: {
62
+ status: "unavailable",
63
+ ...failure(error)
64
+ } });
65
+ });
66
+ }
67
+ });
68
+ }
69
+ //#endregion
70
+ export { CAPTURE_CAPABILITY, screenshot };
@@ -0,0 +1,2 @@
1
+ import { i as startMockWorker, n as HudMockWorkerModule, r as MockWorkerOptions, t as HudMockWorkerHandle } from "../../msw-browser-wjaYgkB7.js";
2
+ export { HudMockWorkerHandle, HudMockWorkerModule, MockWorkerOptions, startMockWorker };
@@ -0,0 +1,39 @@
1
+ //#region src/plugins/data/msw-browser.ts
2
+ /**
3
+ * `msw/browser` resolves to nothing under the `node` condition, and Next
4
+ * compiles a client component for SSR too, so the import sits in a branch
5
+ * the server build can see is dead (`typeof window` is defined per layer).
6
+ */
7
+ async function loadMsw() {
8
+ if (typeof window !== "undefined") {
9
+ const [{ http }, { setupWorker }] = await Promise.all([import("msw"), import("msw/browser")]);
10
+ return {
11
+ http,
12
+ setupWorker
13
+ };
14
+ }
15
+ throw new Error("the MSW worker needs a browser");
16
+ }
17
+ /**
18
+ * Starts an MSW worker whose one handler asks the HUD's resolver, so client
19
+ * fetches follow the active scenarios. The data plugin loads this module on
20
+ * demand through the `worker` loader:
21
+ *
22
+ * ```ts
23
+ * data({ mocks: { scenarios, worker: () => import("@hansenexus/hud/plugins/data/msw-browser") } })
24
+ * ```
25
+ *
26
+ * Needs `msw` and its worker script in the app's public directory.
27
+ */
28
+ async function startMockWorker(resolve, { workerUrl } = {}) {
29
+ const { http, setupWorker } = await loadMsw();
30
+ const worker = setupWorker(http.all("*", ({ request }) => resolve(request)));
31
+ await worker.start({
32
+ onUnhandledFrame: "bypass",
33
+ quiet: true,
34
+ ...workerUrl ? { serviceWorker: { url: workerUrl } } : {}
35
+ });
36
+ return { stop: () => worker.stop() };
37
+ }
38
+ //#endregion
39
+ export { startMockWorker };
@@ -0,0 +1,36 @@
1
+ import { i as HudMockNodeStatus, s as HudMockScenario } from "../../mocks-CA-wM2SB.js";
2
+ import { t as HudStore } from "../../types-DaJcsEjO.js";
3
+ //#region src/plugins/data/msw-node.d.ts
4
+ export interface MockServerOptions {
5
+ /** The scenarios, the same list `dataServer({ mocks })` gets. */
6
+ scenarios: readonly HudMockScenario[];
7
+ /** Where the active set persists; the same store `dataServer()` writes. Default `file()`: `.hud/mocks.json`. */
8
+ store?: HudStore;
9
+ }
10
+ export interface HudMockServer {
11
+ status: HudMockNodeStatus;
12
+ /** Stops intercepting and forgets the registry. */
13
+ close: () => void;
14
+ }
15
+ export declare const MSW_INSTALL_HINT = "bun add -d msw";
16
+ /**
17
+ * Starts MSW in Node for the mock scenarios, so server-side fetches (React
18
+ * Server Components, Server Actions, route handlers) follow the active set
19
+ * like the browser's. Call it once from `instrumentation.ts`:
20
+ *
21
+ * ```ts
22
+ * export async function register() {
23
+ * if (process.env.NEXT_RUNTIME === "nodejs" && process.env.NODE_ENV === "development") {
24
+ * const { startMockServer } = await import("@hansenexus/hud/plugins/data/msw-node");
25
+ * const { scenarios } = await import("./hud/scenarios");
26
+ * await startMockServer({ scenarios });
27
+ * }
28
+ * }
29
+ * ```
30
+ *
31
+ * The active set is read from the store at start and updated in process by
32
+ * `dataServer()` on every toggle. `msw` is loaded here and nowhere else: when
33
+ * it is missing the HUD reports a setup hint instead of failing the server.
34
+ */
35
+ export declare function startMockServer({ scenarios, store }: MockServerOptions): Promise<HudMockServer>;
36
+ //#endregion
@@ -0,0 +1,68 @@
1
+ import { c as readMockRegistry, d as validateScenarios, f as writeMockRegistry, i as createMockResolver, n as MOCKS_DOCUMENT, t as MOCKS_COLLECTION, u as toMockActive } from "../../mocks-ESiRqk9o.js";
2
+ import { t as file } from "../../file-xdSaQ2uR.js";
3
+ //#region src/plugins/data/msw-node.ts
4
+ const MSW_INSTALL_HINT = "bun add -d msw";
5
+ /**
6
+ * Starts MSW in Node for the mock scenarios, so server-side fetches (React
7
+ * Server Components, Server Actions, route handlers) follow the active set
8
+ * like the browser's. Call it once from `instrumentation.ts`:
9
+ *
10
+ * ```ts
11
+ * export async function register() {
12
+ * if (process.env.NEXT_RUNTIME === "nodejs" && process.env.NODE_ENV === "development") {
13
+ * const { startMockServer } = await import("@hansenexus/hud/plugins/data/msw-node");
14
+ * const { scenarios } = await import("./hud/scenarios");
15
+ * await startMockServer({ scenarios });
16
+ * }
17
+ * }
18
+ * ```
19
+ *
20
+ * The active set is read from the store at start and updated in process by
21
+ * `dataServer()` on every toggle. `msw` is loaded here and nowhere else: when
22
+ * it is missing the HUD reports a setup hint instead of failing the server.
23
+ */
24
+ async function startMockServer({ scenarios, store = file() }) {
25
+ const table = validateScenarios(scenarios);
26
+ readMockRegistry()?.close();
27
+ let active = {};
28
+ try {
29
+ active = toMockActive(await store.get(MOCKS_COLLECTION, MOCKS_DOCUMENT), table);
30
+ } catch (error) {
31
+ console.warn("hud mocks: could not read the active scenarios; starting with none", error);
32
+ }
33
+ const resolve = createMockResolver(table, () => active);
34
+ let stop = () => {};
35
+ const registry = {
36
+ status: { status: "off" },
37
+ active: () => active,
38
+ apply(next) {
39
+ active = next;
40
+ },
41
+ close() {
42
+ const current = stop;
43
+ stop = () => {};
44
+ current();
45
+ if (readMockRegistry() === registry) writeMockRegistry(void 0);
46
+ }
47
+ };
48
+ writeMockRegistry(registry);
49
+ try {
50
+ const [{ http }, { setupServer }] = await Promise.all([import("msw"), import("msw/node")]);
51
+ const server = setupServer(http.all("*", ({ request }) => resolve(request)));
52
+ server.listen({ onUnhandledFrame: "bypass" });
53
+ stop = () => server.close();
54
+ registry.status = { status: "live" };
55
+ } catch (error) {
56
+ registry.status = {
57
+ status: "failed",
58
+ hint: `Server fetches are not mocked: msw could not start (${error instanceof Error ? error.message : String(error)}). Install it: ${MSW_INSTALL_HINT}.`
59
+ };
60
+ console.warn(`hud mocks: ${registry.status.hint}`);
61
+ }
62
+ return {
63
+ status: registry.status,
64
+ close: registry.close
65
+ };
66
+ }
67
+ //#endregion
68
+ export { MSW_INSTALL_HINT, startMockServer };