@mandujs/core 0.22.0 → 0.23.0

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.
@@ -1,492 +1,527 @@
1
- /**
2
- * @mandujs/core/desktop — window factory
3
- *
4
- * Wraps `webview-bun` (optional peer dependency, MIT, tr1ckydev/webview-bun
5
- * 2.4.0+). Phase 9c R0 diagnostic:
6
- * - docs/bun/phase-9-diagnostics/webview-bun-ffi.md
7
- *
8
- * Design rules:
9
- * 1. **Lazy import** — `webview-bun` must NOT be loaded when this module is
10
- * merely imported. A web-only project running `bun test` should pass
11
- * even if the peer is absent. The import happens on the first
12
- * `createWindow()` call, with a clear install-me error on failure.
13
- * 2. **No side-channel globals** — each handle is self-contained; multiple
14
- * windows are allowed in a single process (though not a common use
15
- * case).
16
- * 3. **Never surface the `Webview` instance** — consumers only see
17
- * {@link WindowHandle}. Backend swaps (Bun.WebView native, direct FFI)
18
- * stay transparent.
19
- *
20
- * Threading model: `webview-bun`'s `run()` is blocking and must be on the
21
- * thread that owns the window. For use with `Bun.serve()`, the standard
22
- * pattern is **Worker-based**: launch the server on the main thread, spawn
23
- * a Worker, and call `createWindow()` inside it. See `./worker.ts` for the
24
- * canonical entry. When `autoRun: false`, callers who control their own
25
- * event loop (e.g. running the window on the main thread while the HTTP
26
- * server sits in a Worker) can call `handle.run()` themselves.
27
- */
28
-
29
- import type {
30
- WindowHandle,
31
- WindowOptions,
32
- WindowSizeHint,
33
- } from "./types.js";
34
-
35
- // ─── Optional-peer loader ───────────────────────────────────────────────────
36
-
37
- /**
38
- * Cached module once loaded. We do NOT pre-load at module evaluation — if
39
- * `webview-bun` is missing, `import @mandujs/core/desktop` must still succeed
40
- * (so `bun test` in a CI without the peer passes cleanly).
41
- */
42
- type WebviewBunModule = {
43
- // We intentionally type the imported module as `any` here because
44
- // `webview-bun` publishes types that depend on its FFI pointers. A tighter
45
- // type contract is not worth pulling the peer's type graph into `core`.
46
- // Consumers never see this — they work against WindowHandle.
47
- Webview: new (
48
- debug?: boolean,
49
- size?: { width: number; height: number; hint: number } | null,
50
- window?: unknown,
51
- ) => {
52
- title: string;
53
- size: { width: number; height: number; hint: number };
54
- navigate(url: string): void;
55
- setHTML(html: string): void;
56
- init(source: string): void;
57
- eval(source: string): void;
58
- bind(name: string, cb: (...args: unknown[]) => unknown): void;
59
- unbind(name: string): void;
60
- run(): void;
61
- destroy(): void;
62
- };
63
- SizeHint: { NONE: number; MIN: number; MAX: number; FIXED: number };
64
- };
65
-
66
- let webviewBunCache: WebviewBunModule | null = null;
67
-
68
- /**
69
- * Lazy-load `webview-bun`. Throws with an actionable error message when the
70
- * peer is missing — the only surface on which end users hit this is desktop
71
- * launch, so we can afford a long-form hint.
72
- *
73
- * @internal
74
- */
75
- export async function _loadWebviewBun(): Promise<WebviewBunModule> {
76
- if (webviewBunCache) return webviewBunCache;
77
- try {
78
- // Dynamic import so `bun test` in a CI without `webview-bun` installed
79
- // still passes. The import specifier is a bare module — no file-path
80
- // probing — so bundlers can tree-shake the whole desktop subtree in a
81
- // web-only build.
82
- //
83
- // `@ts-ignore` is used because `webview-bun` is an OPTIONAL peer — tsc
84
- // must not hard-fail module resolution when the peer is absent. The
85
- // runtime behaviour is guarded: the try/catch below rethrows a clean
86
- // "please install" error if the import itself rejects at runtime.
87
- // @ts-ignore -- optional peer, may not be resolvable at typecheck time
88
- const mod = (await import("webview-bun")) as unknown as WebviewBunModule;
89
- webviewBunCache = mod;
90
- return mod;
91
- } catch (cause) {
92
- throw new Error(
93
- [
94
- "[@mandujs/core/desktop] Failed to load the optional peer 'webview-bun'.",
95
- "Install it alongside Mandu for desktop targets:",
96
- "",
97
- " bun add webview-bun",
98
- "",
99
- "Then pin the version in package.json. Tested: ^2.4.0 (MIT).",
100
- "Docs: https://github.com/tr1ckydev/webview-bun",
101
- ].join("\n"),
102
- { cause: cause as Error },
103
- );
104
- }
105
- }
106
-
107
- /**
108
- * Reset the lazy-load cache. Tests only.
109
- *
110
- * @internal
111
- */
112
- export function _resetWebviewBunCache(): void {
113
- webviewBunCache = null;
114
- }
115
-
116
- // ─── Size hint mapping ──────────────────────────────────────────────────────
117
-
118
- /**
119
- * Map the string hint to `webview-bun`'s `SizeHint` numeric enum. We accept
120
- * the string because (a) the string survives Worker `postMessage` cleanly
121
- * and (b) it doesn't pin our public API to the peer's enum numbering.
122
- *
123
- * @internal
124
- */
125
- export function _mapSizeHint(
126
- hint: WindowSizeHint | undefined,
127
- enumRef: WebviewBunModule["SizeHint"],
128
- ): number {
129
- switch (hint) {
130
- case "fixed":
131
- return enumRef.FIXED;
132
- case "min":
133
- return enumRef.MIN;
134
- case "max":
135
- return enumRef.MAX;
136
- case "none":
137
- case undefined:
138
- default:
139
- return enumRef.NONE;
140
- }
141
- }
142
-
143
- // ─── Option validation ──────────────────────────────────────────────────────
144
-
145
- /**
146
- * Validates `options` before we touch the FFI peer. Throws `TypeError` on
147
- * the first problem found.
148
- *
149
- * @internal
150
- */
151
- export function _validateOptions(options: WindowOptions): void {
152
- if (!options || typeof options !== "object") {
153
- throw new TypeError(
154
- "[@mandujs/core/desktop] createWindow: options must be an object.",
155
- );
156
- }
157
- if (typeof options.url !== "string" || options.url.length === 0) {
158
- throw new TypeError(
159
- "[@mandujs/core/desktop] createWindow: 'url' must be a non-empty string.",
160
- );
161
- }
162
- // Accept http/https/file/data — reject everything else. Remote URLs work
163
- // but are actively discouraged; document that elsewhere.
164
- const allowedProtocols = ["http:", "https:", "file:", "data:"];
165
- let parsed: URL;
166
- try {
167
- parsed = new URL(options.url);
168
- } catch {
169
- throw new TypeError(
170
- `[@mandujs/core/desktop] createWindow: 'url' is not a valid URL: ${JSON.stringify(
171
- options.url,
172
- )}.`,
173
- );
174
- }
175
- if (!allowedProtocols.includes(parsed.protocol)) {
176
- throw new TypeError(
177
- `[@mandujs/core/desktop] createWindow: 'url' protocol ${parsed.protocol} is not allowed (use http/https/file/data).`,
178
- );
179
- }
180
- if (options.width !== undefined) {
181
- if (
182
- typeof options.width !== "number" ||
183
- !Number.isFinite(options.width) ||
184
- options.width <= 0
185
- ) {
186
- throw new TypeError(
187
- "[@mandujs/core/desktop] createWindow: 'width' must be a positive finite number.",
188
- );
189
- }
190
- }
191
- if (options.height !== undefined) {
192
- if (
193
- typeof options.height !== "number" ||
194
- !Number.isFinite(options.height) ||
195
- options.height <= 0
196
- ) {
197
- throw new TypeError(
198
- "[@mandujs/core/desktop] createWindow: 'height' must be a positive finite number.",
199
- );
200
- }
201
- }
202
- if (
203
- options.hint !== undefined &&
204
- !["none", "min", "max", "fixed"].includes(options.hint)
205
- ) {
206
- throw new TypeError(
207
- `[@mandujs/core/desktop] createWindow: 'hint' must be one of none|min|max|fixed (got ${JSON.stringify(
208
- options.hint,
209
- )}).`,
210
- );
211
- }
212
- if (options.handlers !== undefined) {
213
- if (typeof options.handlers !== "object" || options.handlers === null) {
214
- throw new TypeError(
215
- "[@mandujs/core/desktop] createWindow: 'handlers' must be an object of functions.",
216
- );
217
- }
218
- for (const [name, fn] of Object.entries(options.handlers)) {
219
- if (typeof fn !== "function") {
220
- throw new TypeError(
221
- `[@mandujs/core/desktop] createWindow: handlers.${name} must be a function.`,
222
- );
223
- }
224
- }
225
- }
226
- }
227
-
228
- // ─── Defaults ───────────────────────────────────────────────────────────────
229
-
230
- /** @internal */
231
- export const _DEFAULTS: Required<Pick<WindowOptions, "title" | "width" | "height" | "hint" | "debug">> = {
232
- title: "Mandu Desktop",
233
- width: 1024,
234
- height: 768,
235
- hint: "none",
236
- debug: false,
237
- };
238
-
239
- // ─── createWindow ───────────────────────────────────────────────────────────
240
-
241
- /**
242
- * Create a desktop window backed by the system WebView (WebView2 on Windows,
243
- * WKWebView on macOS, WebKitGTK on Linux). Optional peer `webview-bun` must
244
- * be installed.
245
- *
246
- * The returned {@link WindowHandle} does NOT auto-start the platform event
247
- * loop — callers must either call `handle.run()` (blocking) or await
248
- * `handle.closed`. In Worker-based setups the loop is typically started by
249
- * the Worker host (see `./worker.ts`).
250
- *
251
- * @example Main-thread use (window only, no HTTP server):
252
- * ```ts
253
- * import { createWindow } from "@mandujs/core/desktop";
254
- *
255
- * const win = await createWindow({
256
- * url: "https://example.com",
257
- * title: "Read later",
258
- * width: 1200,
259
- * height: 800,
260
- * });
261
- * win.run(); // blocks until user closes
262
- * ```
263
- *
264
- * @example With a Mandu server (Worker pattern — recommended):
265
- * ```ts
266
- * // main.ts
267
- * import { startServer } from "@mandujs/core";
268
- * import manifest from "../../.mandu/manifest.json" with { type: "json" };
269
- *
270
- * const server = startServer(manifest, { port: 0, hostname: "127.0.0.1" });
271
- * const worker = new Worker(new URL("./worker.ts", import.meta.url));
272
- * worker.postMessage({
273
- * type: "open",
274
- * options: { url: `http://127.0.0.1:${server.server.port}`, title: "My App" },
275
- * });
276
- * ```
277
- */
278
- export async function createWindow(
279
- options: WindowOptions,
280
- ): Promise<WindowHandle> {
281
- _validateOptions(options);
282
-
283
- const { Webview, SizeHint } = await _loadWebviewBun();
284
-
285
- const merged = {
286
- ..._DEFAULTS,
287
- ...options,
288
- };
289
- const hintNum = _mapSizeHint(merged.hint, SizeHint);
290
-
291
- // Construct the webview. `webview-bun` uses constructor args for size+hint
292
- // and exposes setters for title/size post-construction.
293
- const wv = new Webview(merged.debug, {
294
- width: merged.width,
295
- height: merged.height,
296
- hint: hintNum,
297
- });
298
-
299
- // Title must be set post-ctor — webview-bun API shape.
300
- try {
301
- wv.title = merged.title;
302
- } catch (error) {
303
- // Some libwebview builds throw if the window hasn't been realized yet;
304
- // best-effort, not fatal.
305
- if (merged.debug) {
306
- console.warn("[@mandujs/core/desktop] title set warning:", error);
307
- }
308
- }
309
-
310
- // Pre-register handlers BEFORE navigation so the page's first script doesn't
311
- // see an undefined global.
312
- if (options.handlers) {
313
- for (const [name, fn] of Object.entries(options.handlers)) {
314
- try {
315
- wv.bind(name, fn);
316
- } catch (error) {
317
- throw new Error(
318
- `[@mandujs/core/desktop] Failed to bind handler "${name}": ${
319
- error instanceof Error ? error.message : String(error)
320
- }`,
321
- );
322
- }
323
- }
324
- }
325
-
326
- // Set up closed-signal wiring. `webview-bun` does not expose a native
327
- // close event, so we rely on `run()` returning OR an explicit `destroy()`
328
- // call to flip the flag.
329
- let closed = false;
330
- let resolveClosed: (() => void) | null = null;
331
- const closedPromise = new Promise<void>((resolve) => {
332
- resolveClosed = resolve;
333
- });
334
- const closeCallbacks: Array<() => void> = [];
335
-
336
- function markClosed(): void {
337
- if (closed) return;
338
- closed = true;
339
- // Run user callbacks first so their exceptions don't prevent Promise
340
- // resolution. We swallow exceptions to match `setTimeout` semantics.
341
- for (const cb of closeCallbacks) {
342
- try {
343
- cb();
344
- } catch (error) {
345
- console.error(
346
- "[@mandujs/core/desktop] onClose callback threw:",
347
- error,
348
- );
349
- }
350
- }
351
- if (options.onClose) {
352
- try {
353
- const result = options.onClose();
354
- if (result instanceof Promise) {
355
- result.catch((error) =>
356
- console.error(
357
- "[@mandujs/core/desktop] onClose (options) threw:",
358
- error,
359
- ),
360
- );
361
- }
362
- } catch (error) {
363
- console.error(
364
- "[@mandujs/core/desktop] onClose (options) threw:",
365
- error,
366
- );
367
- }
368
- }
369
- resolveClosed?.();
370
- }
371
-
372
- // Navigate AFTER handlers are registered, so the first page load can
373
- // already call any bound globals.
374
- try {
375
- wv.navigate(merged.url);
376
- } catch (error) {
377
- // Navigation failure is fatal — tear down and rethrow.
378
- try {
379
- wv.destroy();
380
- } catch {
381
- /* ignore cleanup errors */
382
- }
383
- throw new Error(
384
- `[@mandujs/core/desktop] navigate() failed: ${
385
- error instanceof Error ? error.message : String(error)
386
- }`,
387
- );
388
- }
389
-
390
- // Fire onReady on the next microtask so callers that chain `await
391
- // createWindow(...)` can attach listeners first.
392
- if (options.onReady) {
393
- queueMicrotask(() => {
394
- try {
395
- const result = options.onReady!();
396
- if (result instanceof Promise) {
397
- result.catch((error) =>
398
- console.error(
399
- "[@mandujs/core/desktop] onReady threw:",
400
- error,
401
- ),
402
- );
403
- }
404
- } catch (error) {
405
- console.error("[@mandujs/core/desktop] onReady threw:", error);
406
- }
407
- });
408
- }
409
-
410
- const handle: WindowHandle = {
411
- async close() {
412
- if (closed) return;
413
- try {
414
- wv.destroy();
415
- } catch (error) {
416
- // `webview-bun` #35: destroy() from a timer doesn't always interrupt
417
- // run(). We still mark closed so the `closed` promise resolves — the
418
- // native run() will exit on its own once the user closes the shell.
419
- if (merged.debug) {
420
- console.warn("[@mandujs/core/desktop] destroy() warning:", error);
421
- }
422
- }
423
- markClosed();
424
- },
425
- onClose(cb: () => void) {
426
- if (closed) {
427
- // Match `addEventListener('load')` semantics on a ready document —
428
- // fire on the next microtask so ordering is deterministic.
429
- queueMicrotask(() => {
430
- try {
431
- cb();
432
- } catch (error) {
433
- console.error(
434
- "[@mandujs/core/desktop] onClose callback threw:",
435
- error,
436
- );
437
- }
438
- });
439
- return;
440
- }
441
- closeCallbacks.push(cb);
442
- },
443
- async eval(js: string) {
444
- if (closed) {
445
- throw new Error(
446
- "[@mandujs/core/desktop] eval() called on closed window.",
447
- );
448
- }
449
- if (typeof js !== "string" || js.length === 0) {
450
- throw new TypeError(
451
- "[@mandujs/core/desktop] eval: 'js' must be a non-empty string.",
452
- );
453
- }
454
- wv.eval(js);
455
- },
456
- bind(name: string, fn: (...args: unknown[]) => unknown) {
457
- if (closed) {
458
- throw new Error(
459
- "[@mandujs/core/desktop] bind() called on closed window.",
460
- );
461
- }
462
- if (typeof name !== "string" || name.length === 0) {
463
- throw new TypeError(
464
- "[@mandujs/core/desktop] bind: 'name' must be a non-empty string.",
465
- );
466
- }
467
- if (typeof fn !== "function") {
468
- throw new TypeError(
469
- "[@mandujs/core/desktop] bind: 'fn' must be a function.",
470
- );
471
- }
472
- wv.bind(name, fn);
473
- },
474
- closed: closedPromise,
475
- run() {
476
- if (closed) {
477
- // No-op — already closed. `webview-bun`'s run() on a destroyed
478
- // instance would crash; avoid that class of footgun.
479
- return;
480
- }
481
- try {
482
- wv.run();
483
- } finally {
484
- // run() returned the window was closed (either natively or via
485
- // destroy()). Flip the flag.
486
- markClosed();
487
- }
488
- },
489
- };
490
-
491
- return handle;
492
- }
1
+ /**
2
+ * @mandujs/core/desktop — window factory
3
+ *
4
+ * Wraps `webview-bun` (optional peer dependency, MIT, tr1ckydev/webview-bun
5
+ * 2.4.0+). Phase 9c R0 diagnostic:
6
+ * - docs/bun/phase-9-diagnostics/webview-bun-ffi.md
7
+ *
8
+ * Design rules:
9
+ * 1. **Lazy import** — `webview-bun` must NOT be loaded when this module is
10
+ * merely imported. A web-only project running `bun test` should pass
11
+ * even if the peer is absent. The import happens on the first
12
+ * `createWindow()` call, with a clear install-me error on failure.
13
+ * 2. **No side-channel globals** — each handle is self-contained; multiple
14
+ * windows are allowed in a single process (though not a common use
15
+ * case).
16
+ * 3. **Never surface the `Webview` instance** — consumers only see
17
+ * {@link WindowHandle}. Backend swaps (Bun.WebView native, direct FFI)
18
+ * stay transparent.
19
+ *
20
+ * Threading model: `webview-bun`'s `run()` is blocking and must be on the
21
+ * thread that owns the window. For use with `Bun.serve()`, the standard
22
+ * pattern is **Worker-based**: launch the server on the main thread, spawn
23
+ * a Worker, and call `createWindow()` inside it. See `./worker.ts` for the
24
+ * canonical entry. When `autoRun: false`, callers who control their own
25
+ * event loop (e.g. running the window on the main thread while the HTTP
26
+ * server sits in a Worker) can call `handle.run()` themselves.
27
+ */
28
+
29
+ import type {
30
+ WindowHandle,
31
+ WindowOptions,
32
+ WindowSizeHint,
33
+ } from "./types.js";
34
+
35
+ // ─── Optional-peer loader ───────────────────────────────────────────────────
36
+
37
+ /**
38
+ * Cached module once loaded. We do NOT pre-load at module evaluation — if
39
+ * `webview-bun` is missing, `import @mandujs/core/desktop` must still succeed
40
+ * (so `bun test` in a CI without the peer passes cleanly).
41
+ */
42
+ type WebviewBunModule = {
43
+ // We intentionally type the imported module as `any` here because
44
+ // `webview-bun` publishes types that depend on its FFI pointers. A tighter
45
+ // type contract is not worth pulling the peer's type graph into `core`.
46
+ // Consumers never see this — they work against WindowHandle.
47
+ Webview: new (
48
+ debug?: boolean,
49
+ size?: { width: number; height: number; hint: number } | null,
50
+ window?: unknown,
51
+ ) => {
52
+ title: string;
53
+ size: { width: number; height: number; hint: number };
54
+ navigate(url: string): void;
55
+ setHTML(html: string): void;
56
+ init(source: string): void;
57
+ eval(source: string): void;
58
+ bind(name: string, cb: (...args: unknown[]) => unknown): void;
59
+ unbind(name: string): void;
60
+ run(): void;
61
+ destroy(): void;
62
+ };
63
+ SizeHint: { NONE: number; MIN: number; MAX: number; FIXED: number };
64
+ };
65
+
66
+ let webviewBunCache: WebviewBunModule | null = null;
67
+
68
+ /**
69
+ * Lazy-load `webview-bun`. Throws with an actionable error message when the
70
+ * peer is missing — the only surface on which end users hit this is desktop
71
+ * launch, so we can afford a long-form hint.
72
+ *
73
+ * @internal
74
+ */
75
+ export async function _loadWebviewBun(): Promise<WebviewBunModule> {
76
+ if (webviewBunCache) return webviewBunCache;
77
+ try {
78
+ // Dynamic import so `bun test` in a CI without `webview-bun` installed
79
+ // still passes. The import specifier is a bare module — no file-path
80
+ // probing — so bundlers can tree-shake the whole desktop subtree in a
81
+ // web-only build.
82
+ //
83
+ // `@ts-ignore` is used because `webview-bun` is an OPTIONAL peer — tsc
84
+ // must not hard-fail module resolution when the peer is absent. The
85
+ // runtime behaviour is guarded: the try/catch below rethrows a clean
86
+ // "please install" error if the import itself rejects at runtime.
87
+ // @ts-ignore -- optional peer, may not be resolvable at typecheck time
88
+ const mod = (await import("webview-bun")) as unknown as WebviewBunModule;
89
+ webviewBunCache = mod;
90
+ return mod;
91
+ } catch (cause) {
92
+ throw new Error(
93
+ [
94
+ "[@mandujs/core/desktop] Failed to load the optional peer 'webview-bun'.",
95
+ "Install it alongside Mandu for desktop targets:",
96
+ "",
97
+ " bun add webview-bun",
98
+ "",
99
+ "Then pin the version in package.json. Tested: ^2.4.0 (MIT).",
100
+ "Docs: https://github.com/tr1ckydev/webview-bun",
101
+ ].join("\n"),
102
+ { cause: cause as Error },
103
+ );
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Reset the lazy-load cache. Tests only.
109
+ *
110
+ * @internal
111
+ */
112
+ export function _resetWebviewBunCache(): void {
113
+ webviewBunCache = null;
114
+ }
115
+
116
+ // ─── Size hint mapping ──────────────────────────────────────────────────────
117
+
118
+ /**
119
+ * Map the string hint to `webview-bun`'s `SizeHint` numeric enum. We accept
120
+ * the string because (a) the string survives Worker `postMessage` cleanly
121
+ * and (b) it doesn't pin our public API to the peer's enum numbering.
122
+ *
123
+ * @internal
124
+ */
125
+ export function _mapSizeHint(
126
+ hint: WindowSizeHint | undefined,
127
+ enumRef: WebviewBunModule["SizeHint"],
128
+ ): number {
129
+ switch (hint) {
130
+ case "fixed":
131
+ return enumRef.FIXED;
132
+ case "min":
133
+ return enumRef.MIN;
134
+ case "max":
135
+ return enumRef.MAX;
136
+ case "none":
137
+ case undefined:
138
+ default:
139
+ return enumRef.NONE;
140
+ }
141
+ }
142
+
143
+ // ─── Option validation ──────────────────────────────────────────────────────
144
+
145
+ /**
146
+ * Validates `options` before we touch the FFI peer. Throws `TypeError` on
147
+ * the first problem found.
148
+ *
149
+ * @internal
150
+ */
151
+ export function _validateOptions(options: WindowOptions): void {
152
+ if (!options || typeof options !== "object") {
153
+ throw new TypeError(
154
+ "[@mandujs/core/desktop] createWindow: options must be an object.",
155
+ );
156
+ }
157
+ if (typeof options.url !== "string" || options.url.length === 0) {
158
+ throw new TypeError(
159
+ "[@mandujs/core/desktop] createWindow: 'url' must be a non-empty string.",
160
+ );
161
+ }
162
+ // Accept http/https/file/data — reject everything else. Remote URLs work
163
+ // but are actively discouraged; document that elsewhere.
164
+ const allowedProtocols = ["http:", "https:", "file:", "data:"];
165
+ let parsed: URL;
166
+ try {
167
+ parsed = new URL(options.url);
168
+ } catch {
169
+ throw new TypeError(
170
+ `[@mandujs/core/desktop] createWindow: 'url' is not a valid URL: ${JSON.stringify(
171
+ options.url,
172
+ )}.`,
173
+ );
174
+ }
175
+ if (!allowedProtocols.includes(parsed.protocol)) {
176
+ throw new TypeError(
177
+ `[@mandujs/core/desktop] createWindow: 'url' protocol ${parsed.protocol} is not allowed (use http/https/file/data).`,
178
+ );
179
+ }
180
+ if (options.width !== undefined) {
181
+ if (
182
+ typeof options.width !== "number" ||
183
+ !Number.isFinite(options.width) ||
184
+ options.width <= 0
185
+ ) {
186
+ throw new TypeError(
187
+ "[@mandujs/core/desktop] createWindow: 'width' must be a positive finite number.",
188
+ );
189
+ }
190
+ }
191
+ if (options.height !== undefined) {
192
+ if (
193
+ typeof options.height !== "number" ||
194
+ !Number.isFinite(options.height) ||
195
+ options.height <= 0
196
+ ) {
197
+ throw new TypeError(
198
+ "[@mandujs/core/desktop] createWindow: 'height' must be a positive finite number.",
199
+ );
200
+ }
201
+ }
202
+ if (
203
+ options.hint !== undefined &&
204
+ !["none", "min", "max", "fixed"].includes(options.hint)
205
+ ) {
206
+ throw new TypeError(
207
+ `[@mandujs/core/desktop] createWindow: 'hint' must be one of none|min|max|fixed (got ${JSON.stringify(
208
+ options.hint,
209
+ )}).`,
210
+ );
211
+ }
212
+ if (options.handlers !== undefined) {
213
+ if (typeof options.handlers !== "object" || options.handlers === null) {
214
+ throw new TypeError(
215
+ "[@mandujs/core/desktop] createWindow: 'handlers' must be an object of functions.",
216
+ );
217
+ }
218
+ for (const [name, fn] of Object.entries(options.handlers)) {
219
+ if (typeof fn !== "function") {
220
+ throw new TypeError(
221
+ `[@mandujs/core/desktop] createWindow: handlers.${name} must be a function.`,
222
+ );
223
+ }
224
+ }
225
+ }
226
+ }
227
+
228
+ // ─── Defaults ───────────────────────────────────────────────────────────────
229
+
230
+ /** @internal */
231
+ export const _DEFAULTS: Required<Pick<WindowOptions, "title" | "width" | "height" | "hint" | "debug">> = {
232
+ title: "Mandu Desktop",
233
+ width: 1024,
234
+ height: 768,
235
+ hint: "none",
236
+ debug: false,
237
+ };
238
+
239
+ // ─── createWindow ───────────────────────────────────────────────────────────
240
+
241
+ /**
242
+ * Create a desktop window backed by the system WebView (WebView2 on Windows,
243
+ * WKWebView on macOS, WebKitGTK on Linux). Optional peer `webview-bun` must
244
+ * be installed.
245
+ *
246
+ * The returned {@link WindowHandle} does NOT auto-start the platform event
247
+ * loop — callers must either call `handle.run()` (blocking) or await
248
+ * `handle.closed`. In Worker-based setups the loop is typically started by
249
+ * the Worker host (see `./worker.ts`).
250
+ *
251
+ * @example Main-thread use (window only, no HTTP server):
252
+ * ```ts
253
+ * import { createWindow } from "@mandujs/core/desktop";
254
+ *
255
+ * const win = await createWindow({
256
+ * url: "https://example.com",
257
+ * title: "Read later",
258
+ * width: 1200,
259
+ * height: 800,
260
+ * });
261
+ * win.run(); // blocks until user closes
262
+ * ```
263
+ *
264
+ * @example With a Mandu server (Worker pattern — recommended):
265
+ * ```ts
266
+ * // main.ts
267
+ * import { startServer } from "@mandujs/core";
268
+ * import manifest from "../../.mandu/manifest.json" with { type: "json" };
269
+ *
270
+ * const server = startServer(manifest, { port: 0, hostname: "127.0.0.1" });
271
+ * const worker = new Worker(new URL("./worker.ts", import.meta.url));
272
+ * worker.postMessage({
273
+ * type: "open",
274
+ * options: { url: `http://127.0.0.1:${server.server.port}`, title: "My App" },
275
+ * });
276
+ * ```
277
+ */
278
+ export async function createWindow(
279
+ options: WindowOptions,
280
+ ): Promise<WindowHandle> {
281
+ _validateOptions(options);
282
+
283
+ // Phase 11 C / M-02 FFI fallback path. When `MANDU_DESKTOP_INLINE_FFI=1`
284
+ // is set, OR when `webview-bun` dynamic import fails at runtime, we try
285
+ // the `bun:ffi` fallback that binds directly to the upstream
286
+ // `webview/webview` C library. The fallback is a supply-chain mitigation
287
+ // for the webview-bun single-maintainer risk — see
288
+ // `docs/bun/phase-9-diagnostics/webview-bun-ffi.md` §8.
289
+ //
290
+ // Behaviour matrix:
291
+ // MANDU_DESKTOP_INLINE_FFI=1:
292
+ // SKIP webview-bun, go straight to FFI fallback.
293
+ // webview-bun resolves cleanly:
294
+ // → primary path (normal flow below).
295
+ // webview-bun rejects with a module-not-found error AND fallback
296
+ // succeeds: log a one-time hint, use fallback.
297
+ // Both fail: rethrow the webview-bun "install me" error (the original
298
+ // actionable hint).
299
+ const forceFFI = process.env.MANDU_DESKTOP_INLINE_FFI === "1";
300
+ if (forceFFI) {
301
+ const { createFallbackWebview } = await import("./webview-fallback.js");
302
+ return createFallbackWebview(options);
303
+ }
304
+
305
+ let peer: WebviewBunModule;
306
+ try {
307
+ peer = await _loadWebviewBun();
308
+ } catch (primaryError) {
309
+ // Try the fallback. If it also fails, surface the PRIMARY error since
310
+ // it carries the actionable "bun add webview-bun" hint users expect.
311
+ try {
312
+ const { createFallbackWebview } = await import("./webview-fallback.js");
313
+ return await createFallbackWebview(options);
314
+ } catch {
315
+ throw primaryError;
316
+ }
317
+ }
318
+ const { Webview, SizeHint } = peer;
319
+
320
+ const merged = {
321
+ ..._DEFAULTS,
322
+ ...options,
323
+ };
324
+ const hintNum = _mapSizeHint(merged.hint, SizeHint);
325
+
326
+ // Construct the webview. `webview-bun` uses constructor args for size+hint
327
+ // and exposes setters for title/size post-construction.
328
+ const wv = new Webview(merged.debug, {
329
+ width: merged.width,
330
+ height: merged.height,
331
+ hint: hintNum,
332
+ });
333
+
334
+ // Title must be set post-ctor — webview-bun API shape.
335
+ try {
336
+ wv.title = merged.title;
337
+ } catch (error) {
338
+ // Some libwebview builds throw if the window hasn't been realized yet;
339
+ // best-effort, not fatal.
340
+ if (merged.debug) {
341
+ console.warn("[@mandujs/core/desktop] title set warning:", error);
342
+ }
343
+ }
344
+
345
+ // Pre-register handlers BEFORE navigation so the page's first script doesn't
346
+ // see an undefined global.
347
+ if (options.handlers) {
348
+ for (const [name, fn] of Object.entries(options.handlers)) {
349
+ try {
350
+ wv.bind(name, fn);
351
+ } catch (error) {
352
+ throw new Error(
353
+ `[@mandujs/core/desktop] Failed to bind handler "${name}": ${
354
+ error instanceof Error ? error.message : String(error)
355
+ }`,
356
+ );
357
+ }
358
+ }
359
+ }
360
+
361
+ // Set up closed-signal wiring. `webview-bun` does not expose a native
362
+ // close event, so we rely on `run()` returning OR an explicit `destroy()`
363
+ // call to flip the flag.
364
+ let closed = false;
365
+ let resolveClosed: (() => void) | null = null;
366
+ const closedPromise = new Promise<void>((resolve) => {
367
+ resolveClosed = resolve;
368
+ });
369
+ const closeCallbacks: Array<() => void> = [];
370
+
371
+ function markClosed(): void {
372
+ if (closed) return;
373
+ closed = true;
374
+ // Run user callbacks first so their exceptions don't prevent Promise
375
+ // resolution. We swallow exceptions to match `setTimeout` semantics.
376
+ for (const cb of closeCallbacks) {
377
+ try {
378
+ cb();
379
+ } catch (error) {
380
+ console.error(
381
+ "[@mandujs/core/desktop] onClose callback threw:",
382
+ error,
383
+ );
384
+ }
385
+ }
386
+ if (options.onClose) {
387
+ try {
388
+ const result = options.onClose();
389
+ if (result instanceof Promise) {
390
+ result.catch((error) =>
391
+ console.error(
392
+ "[@mandujs/core/desktop] onClose (options) threw:",
393
+ error,
394
+ ),
395
+ );
396
+ }
397
+ } catch (error) {
398
+ console.error(
399
+ "[@mandujs/core/desktop] onClose (options) threw:",
400
+ error,
401
+ );
402
+ }
403
+ }
404
+ resolveClosed?.();
405
+ }
406
+
407
+ // Navigate AFTER handlers are registered, so the first page load can
408
+ // already call any bound globals.
409
+ try {
410
+ wv.navigate(merged.url);
411
+ } catch (error) {
412
+ // Navigation failure is fatal — tear down and rethrow.
413
+ try {
414
+ wv.destroy();
415
+ } catch {
416
+ /* ignore cleanup errors */
417
+ }
418
+ throw new Error(
419
+ `[@mandujs/core/desktop] navigate() failed: ${
420
+ error instanceof Error ? error.message : String(error)
421
+ }`,
422
+ );
423
+ }
424
+
425
+ // Fire onReady on the next microtask so callers that chain `await
426
+ // createWindow(...)` can attach listeners first.
427
+ if (options.onReady) {
428
+ queueMicrotask(() => {
429
+ try {
430
+ const result = options.onReady!();
431
+ if (result instanceof Promise) {
432
+ result.catch((error) =>
433
+ console.error(
434
+ "[@mandujs/core/desktop] onReady threw:",
435
+ error,
436
+ ),
437
+ );
438
+ }
439
+ } catch (error) {
440
+ console.error("[@mandujs/core/desktop] onReady threw:", error);
441
+ }
442
+ });
443
+ }
444
+
445
+ const handle: WindowHandle = {
446
+ async close() {
447
+ if (closed) return;
448
+ try {
449
+ wv.destroy();
450
+ } catch (error) {
451
+ // `webview-bun` #35: destroy() from a timer doesn't always interrupt
452
+ // run(). We still mark closed so the `closed` promise resolves — the
453
+ // native run() will exit on its own once the user closes the shell.
454
+ if (merged.debug) {
455
+ console.warn("[@mandujs/core/desktop] destroy() warning:", error);
456
+ }
457
+ }
458
+ markClosed();
459
+ },
460
+ onClose(cb: () => void) {
461
+ if (closed) {
462
+ // Match `addEventListener('load')` semantics on a ready document
463
+ // fire on the next microtask so ordering is deterministic.
464
+ queueMicrotask(() => {
465
+ try {
466
+ cb();
467
+ } catch (error) {
468
+ console.error(
469
+ "[@mandujs/core/desktop] onClose callback threw:",
470
+ error,
471
+ );
472
+ }
473
+ });
474
+ return;
475
+ }
476
+ closeCallbacks.push(cb);
477
+ },
478
+ async eval(js: string) {
479
+ if (closed) {
480
+ throw new Error(
481
+ "[@mandujs/core/desktop] eval() called on closed window.",
482
+ );
483
+ }
484
+ if (typeof js !== "string" || js.length === 0) {
485
+ throw new TypeError(
486
+ "[@mandujs/core/desktop] eval: 'js' must be a non-empty string.",
487
+ );
488
+ }
489
+ wv.eval(js);
490
+ },
491
+ bind(name: string, fn: (...args: unknown[]) => unknown) {
492
+ if (closed) {
493
+ throw new Error(
494
+ "[@mandujs/core/desktop] bind() called on closed window.",
495
+ );
496
+ }
497
+ if (typeof name !== "string" || name.length === 0) {
498
+ throw new TypeError(
499
+ "[@mandujs/core/desktop] bind: 'name' must be a non-empty string.",
500
+ );
501
+ }
502
+ if (typeof fn !== "function") {
503
+ throw new TypeError(
504
+ "[@mandujs/core/desktop] bind: 'fn' must be a function.",
505
+ );
506
+ }
507
+ wv.bind(name, fn);
508
+ },
509
+ closed: closedPromise,
510
+ run() {
511
+ if (closed) {
512
+ // No-op — already closed. `webview-bun`'s run() on a destroyed
513
+ // instance would crash; avoid that class of footgun.
514
+ return;
515
+ }
516
+ try {
517
+ wv.run();
518
+ } finally {
519
+ // run() returned → the window was closed (either natively or via
520
+ // destroy()). Flip the flag.
521
+ markClosed();
522
+ }
523
+ },
524
+ };
525
+
526
+ return handle;
527
+ }