@celestia-island/hikari 0.55.364 → 0.55.366

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celestia-island/hikari",
3
- "version": "0.55.364",
3
+ "version": "0.55.366",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Hikari UI library, Vue 3 runtime — one design system for Vue 3 and Rust/WASM applications",
@@ -36,9 +36,14 @@ export declare const BOOT_RECOVERY_MESSAGE: string;
36
36
  /** Default failure-panel retry label (English; override through `copy.retry`). */
37
37
  export declare const BOOT_RECOVERY_RETRY: string;
38
38
 
39
- /** Marker attribute on the injected inline `<script>` tag. */
39
+ /** Marker attribute on the injected `<script>` tag (inline or, with
40
+ * `external: true`, the `src` tag). */
40
41
  export declare const BOOT_RECOVERY_ATTR: "data-hk-boot-recovery";
41
42
 
43
+ /** The emitted asset's file name when `external: true` (output root). Also
44
+ * the URL path the dev middleware serves it from. */
45
+ export declare const BOOT_RECOVERY_SCRIPT: string;
46
+
42
47
  /** Default name of the unsupported-browser entry (the family's `__appBlock`). */
43
48
  export declare const BOOT_RECOVERY_BLOCK_GLOBAL: "__appBlock";
44
49
 
@@ -357,6 +362,21 @@ export interface BootRecoveryPluginOptions {
357
362
  * 1024 bytes); use it only when the host sends an explicit charset header.
358
363
  */
359
364
  injectTo?: "head" | "head-prepend";
365
+ /**
366
+ * Emit the recovery script as a same-origin CLASSIC file
367
+ * (`hk-boot-recovery.js`, `src`, no async/defer) instead of inlining it.
368
+ *
369
+ * Use this in every host served over HTTP with `script-src 'self'` — an
370
+ * inline script there is blocked on EVERY page load (the browser skips it
371
+ * and logs a violation), so the recovery panel can never paint. Inline
372
+ * stays the default because the Tauri shells hash the inline text into
373
+ * their compile-time CSP.
374
+ *
375
+ * PLUGIN-ONLY: `bootRecoveryScript()` ignores it (the script text is the
376
+ * same either way) — it selects the DELIVERY shape, which only the plugin
377
+ * controls.
378
+ */
379
+ external?: boolean;
360
380
  }
361
381
 
362
382
  /**
@@ -0,0 +1,154 @@
1
+ /**
2
+ * `external: true` — the CSP-safe shape (2026-11-14 chest incident).
3
+ *
4
+ * A host served over HTTP with `script-src 'self'` cannot run the INLINE
5
+ * recovery script at all: the browser logs a violation and skips it on every
6
+ * page load, so the boot-failure panel never paints. The option emits the
7
+ * same script as a same-origin classic file instead.
8
+ *
9
+ * The contract pinned here:
10
+ * - the emitted asset is byte-identical to what the inline form would put
11
+ * in the document (one script, one source of truth);
12
+ * - the injected tag is a `src` reference with NO async/defer — a plain
13
+ * classic script is parser-blocking, so the heal still lands before the
14
+ * app's modules evaluate (the reason the inline form was chosen);
15
+ * - the marker attribute survives, so a host that still hashes the script
16
+ * (the Tauri shells) can find it;
17
+ * - inline remains the DEFAULT (the shells' compile-time CSP hashes it).
18
+ */
19
+
20
+ import { describe, expect, it } from "vitest";
21
+
22
+ import {
23
+ BOOT_RECOVERY_ATTR,
24
+ BOOT_RECOVERY_SCRIPT,
25
+ bootRecoveryPlugin,
26
+ bootRecoveryScript,
27
+ } from "./boot-recovery.mjs";
28
+
29
+ /** Drive the plugin the way Vite does, without a real build. */
30
+ function drivePlugin(
31
+ plugin: ReturnType<typeof bootRecoveryPlugin>,
32
+ base = "/",
33
+ ) {
34
+ const emitted: Array<{ fileName: string; source: string }> = [];
35
+ const ctx = {
36
+ emitFile(file: { fileName: string; source: string }) {
37
+ emitted.push(file);
38
+ },
39
+ warn() {},
40
+ };
41
+ // Vite types the hooks as `ObjectHook<Fn> | Fn`; these are plain functions
42
+ // here, so narrow once instead of spraying casts.
43
+ type Hook = (...args: unknown[]) => unknown;
44
+ const asFn = (hook: unknown): Hook => hook as Hook;
45
+ asFn(plugin.configResolved).call(ctx, { base } as never);
46
+ asFn(plugin.generateBundle).call(ctx);
47
+ const tags = asFn(plugin.transformIndexHtml).call(ctx, "", {
48
+ filename: "/app/index.html",
49
+ });
50
+ return { emitted, tags: (tags ?? []) as Tag[] };
51
+ }
52
+
53
+ interface Tag {
54
+ tag: string;
55
+ attrs: Record<string, string>;
56
+ children?: string;
57
+ injectTo: string;
58
+ }
59
+
60
+ describe("bootRecoveryPlugin({ external: true })", () => {
61
+ it("emits the script at the output root, byte-identical to the inline form", () => {
62
+ const { emitted } = drivePlugin(bootRecoveryPlugin({ external: true }));
63
+ expect(emitted).toHaveLength(1);
64
+ expect(emitted[0].fileName).toBe(BOOT_RECOVERY_SCRIPT);
65
+ // One source of truth: the asset IS what the inline path would inline.
66
+ expect(emitted[0].source).toBe(bootRecoveryScript({}));
67
+ });
68
+
69
+ it("injects a same-origin classic src tag with no async/defer", () => {
70
+ const { tags } = drivePlugin(bootRecoveryPlugin({ external: true }));
71
+ expect(tags).toHaveLength(1);
72
+ const [tag] = tags;
73
+ expect(tag.tag).toBe("script");
74
+ expect(tag.attrs.src).toBe(`/${BOOT_RECOVERY_SCRIPT}`);
75
+ // The marker survives so a hashing host can still locate it.
76
+ expect(tag.attrs[BOOT_RECOVERY_ATTR]).toBe("");
77
+ // Parse-time execution: a deferred/async script would push the heal past
78
+ // the module evaluation it exists to catch.
79
+ expect(tag.attrs.async).toBeUndefined();
80
+ expect(tag.attrs.defer).toBeUndefined();
81
+ // Nothing inline — that is the whole point.
82
+ expect(tag.children).toBeUndefined();
83
+ expect(tag.injectTo).toBe("head");
84
+ });
85
+
86
+ it("prefixes a sub-path base", () => {
87
+ const { tags } = drivePlugin(bootRecoveryPlugin({ external: true }), "/chest/");
88
+ const [tag] = tags;
89
+ expect(tag.attrs.src).toBe(`/chest/${BOOT_RECOVERY_SCRIPT}`);
90
+ });
91
+
92
+ it("keeps inline as the DEFAULT (the Tauri shells hash the inline text)", () => {
93
+ const { emitted, tags } = drivePlugin(bootRecoveryPlugin());
94
+ expect(emitted).toHaveLength(0);
95
+ const [tag] = tags;
96
+ expect(tag.attrs.src).toBeUndefined();
97
+ expect(tag.children).toBe(bootRecoveryScript({}));
98
+ });
99
+ });
100
+
101
+ describe("dev server (external mode has no bundle to serve from)", () => {
102
+ it("serves the script from memory as JavaScript, not the HTML fallback", () => {
103
+ // In dev `generateBundle` never runs, so a SPA dev server would answer
104
+ // the injected src with index.html (`200 text/html`) and the browser
105
+ // would refuse to execute it — the watchdog dies AND every dev load
106
+ // logs a MIME refusal. The plugin must serve the text itself.
107
+ const plugin = bootRecoveryPlugin({ external: true });
108
+ const handlers: Array<(req: unknown, res: unknown, next: () => void) => void> = [];
109
+ const server = {
110
+ config: { base: "/" },
111
+ middlewares: { use: (fn: (typeof handlers)[number]) => handlers.push(fn) },
112
+ };
113
+ (plugin.configureServer as unknown as (s: typeof server) => void)(server);
114
+ expect(handlers).toHaveLength(1);
115
+
116
+ const served: { status?: number; headers?: Record<string, string>; body?: string } = {};
117
+ const res = {
118
+ set statusCode(v: number) { served.status = v; },
119
+ setHeader(k: string, v: string) { (served.headers ??= {})[k] = v; },
120
+ end(body?: string) { served.body = body; },
121
+ };
122
+ let nexted = false;
123
+ handlers[0]({ url: "/hk-boot-recovery.js", method: "GET" }, res, () => { nexted = true; });
124
+
125
+ expect(nexted).toBe(false);
126
+ expect(served.status).toBe(200);
127
+ expect(served.headers?.["Content-Type"]).toContain("application/javascript");
128
+ expect(served.body).toBe(bootRecoveryScript({}));
129
+ });
130
+
131
+ it("passes unrelated paths through", () => {
132
+ const plugin = bootRecoveryPlugin({ external: true });
133
+ const handlers: Array<(req: unknown, res: unknown, next: () => void) => void> = [];
134
+ (plugin.configureServer as unknown as (s: unknown) => void)({
135
+ config: { base: "/" },
136
+ middlewares: { use: (fn: (typeof handlers)[number]) => handlers.push(fn) },
137
+ });
138
+ let nexted = false;
139
+ handlers[0]({ url: "/app.js", method: "GET" }, { setHeader() {}, end() {} }, () => { nexted = true; });
140
+ expect(nexted).toBe(true);
141
+ });
142
+
143
+ it("installs no middleware when inline is in use", () => {
144
+ // The hook itself stays defined (Vite plugin objects do not gain or lose
145
+ // hooks conditionally); it must simply register nothing.
146
+ const handlers: unknown[] = [];
147
+ const plugin = bootRecoveryPlugin();
148
+ (plugin.configureServer as unknown as (s: unknown) => void)({
149
+ config: { base: "/" },
150
+ middlewares: { use: (fn: unknown) => handlers.push(fn) },
151
+ });
152
+ expect(handlers).toHaveLength(0);
153
+ });
154
+ });
@@ -15,8 +15,9 @@
15
15
  * static loading screen sits on screen forever — with no way for the user to
16
16
  * tell a slow boot from a dead one.
17
17
  *
18
- * The recovery is ONE inline, framework-free script placed in the page's
19
- * `<head>`, with two independent halves (the second one optional):
18
+ * The recovery is ONE framework-free script placed in the page head —
19
+ * inline by default, or a same-origin classic file with `external: true` —
20
+ * with two independent halves (the second one optional):
20
21
  *
21
22
  * 1. ORIGIN HEAL (always on) — when the observed hostname is the configured
22
23
  * origin's hostname with crash-recovery debris glued onto it, replace the
@@ -196,6 +197,23 @@ export const BOOT_RECOVERY_RETRY = "Reload";
196
197
  */
197
198
  export const BOOT_RECOVERY_ATTR = "data-hk-boot-recovery";
198
199
 
200
+ /**
201
+ * The emitted asset's name when `external: true` (output root).
202
+ *
203
+ * WHY THE OPTION EXISTS: an INLINE classic script is the right shape for a
204
+ * host whose CSP already admits it — the Tauri shells hash the script into
205
+ * their compile-time policy, and inline parsing-time execution is exactly
206
+ * what a crash-recovery heal needs. A HOST SERVED OVER HTTP WITH A
207
+ * `script-src 'self'` POLICY cannot admit it at all: the browser logs a
208
+ * violation and SKIPS the script on every single page load, so the panel
209
+ * this module exists for can never paint while the console fills with
210
+ * errors. `external: true` emits the same script as a same-origin classic
211
+ * script (`src`, no `async`/`defer`), which keeps parse-time execution and
212
+ * satisfies `script-src 'self'` unchanged. (2026-11-14: chest's faces ran
213
+ * with the inline form and reported the violation on every load.)
214
+ */
215
+ export const BOOT_RECOVERY_SCRIPT = "hk-boot-recovery.js";
216
+
199
217
  /**
200
218
  * Default name of the `window` function that raises the browser-block panel.
201
219
  * It matches the family's long-standing hand-rolled global (`__appBlock`), so a
@@ -2558,8 +2576,10 @@ export function bootRecoveryScript(options = {}) {
2558
2576
  }
2559
2577
 
2560
2578
  /**
2561
- * The recovery as a Vite plugin: ONE inline, classic (non-module) `<script>`
2562
- * carrying `data-hk-boot-recovery`, injected into the configured HTML entries.
2579
+ * The recovery as a Vite plugin: ONE classic (non-module) `<script>`
2580
+ * carrying `data-hk-boot-recovery`, injected into the configured HTML
2581
+ * entries — inline by default, a same-origin `src` reference with
2582
+ * `external: true`.
2563
2583
  *
2564
2584
  * MULTI-ENTRY: by default only the entry whose basename is `index.html` is
2565
2585
  * instrumented. A shell that builds more than one page (a host builds five;
@@ -2593,7 +2613,17 @@ export function bootRecoveryScript(options = {}) {
2593
2613
  * storageKey?: string,
2594
2614
  * },
2595
2615
  * injectTo?: "head" | "head-prepend",
2616
+ * external?: boolean,
2596
2617
  * }} [options]
2618
+ *
2619
+ * `external: true` emits the same script as `hk-boot-recovery.js` (a
2620
+ * same-origin classic script, no async/defer) instead of inlining it — the
2621
+ * shape a host with a `script-src 'self'` policy needs, since an inline
2622
+ * script there is blocked on every load. Trade-off, stated plainly: the
2623
+ * heal keeps its parse-time ordering, but it now needs one extra same-origin
2624
+ * fetch; if that fetch fails the watchdog is silently absent (for the
2625
+ * "server can't even serve JS" failures this module targets, the inline form
2626
+ * would have failed too).
2597
2627
  * @returns {import("vite").Plugin}
2598
2628
  */
2599
2629
  export function bootRecoveryPlugin(options = {}) {
@@ -2623,15 +2653,57 @@ export function bootRecoveryPlugin(options = {}) {
2623
2653
  // transform (where a host may never notice the stack).
2624
2654
  resolveOptions(opts);
2625
2655
 
2656
+ // `external: true` moves the script out of the document (see
2657
+ // BOOT_RECOVERY_SCRIPT). The base prefix keeps sub-path deployments
2658
+ // resolving; the emitted asset always lands at the output root.
2659
+ const external = opts.external === true;
2660
+ let base = "/";
2661
+
2626
2662
  return {
2627
2663
  name: "hikari-boot-recovery",
2628
2664
 
2665
+ configResolved(config) {
2666
+ base =
2667
+ config && typeof config.base === "string" && config.base
2668
+ ? config.base
2669
+ : "/";
2670
+ },
2671
+
2672
+ generateBundle() {
2673
+ if (!external) return;
2674
+ // Fail early if the script cannot be produced (same reason the inline
2675
+ // path builds it during the transform): a build that ships an
2676
+ // index.html pointing at a missing recovery asset is worse than a
2677
+ // build error.
2678
+ this.emitFile({
2679
+ type: "asset",
2680
+ fileName: BOOT_RECOVERY_SCRIPT,
2681
+ source: bootRecoveryScript(opts),
2682
+ });
2683
+ },
2684
+
2629
2685
  transformIndexHtml(_html, ctx) {
2630
2686
  const filename = ctx && typeof ctx.filename === "string" ? ctx.filename : "";
2631
2687
  if (!htmlFileMatches(filename, opts.htmlFiles)) {
2632
2688
  warnSkipped(this, filename);
2633
2689
  return [];
2634
2690
  }
2691
+ if (external) {
2692
+ const prefix = base === "/" || base === "" ? "/" : base.replace(/\/?$/, "/");
2693
+ return [
2694
+ {
2695
+ tag: "script",
2696
+ // NO async / defer: a parser-blocking classic script runs at the
2697
+ // same point the inline form did, so the heal still lands before
2698
+ // the app's modules evaluate.
2699
+ attrs: {
2700
+ [BOOT_RECOVERY_ATTR]: "",
2701
+ src: `${prefix}${BOOT_RECOVERY_SCRIPT}`,
2702
+ },
2703
+ injectTo: normaliseInjectTo(opts.injectTo),
2704
+ },
2705
+ ];
2706
+ }
2635
2707
  return [
2636
2708
  {
2637
2709
  tag: "script",
@@ -2641,5 +2713,45 @@ export function bootRecoveryPlugin(options = {}) {
2641
2713
  },
2642
2714
  ];
2643
2715
  },
2716
+
2717
+ // `generateBundle` is build-only, so in DEV the emitted asset does not
2718
+ // exist — and a SPA dev server answers `/hk-boot-recovery.js` with the
2719
+ // HTML fallback (`200 text/html`), which browsers refuse to execute as a
2720
+ // classic script: the watchdog silently never installs AND every dev load
2721
+ // logs a MIME refusal (the exact noise class this option exists to
2722
+ // remove). Serve it from memory instead. Both the base-prefixed and the
2723
+ // bare form are accepted, mirroring boot-chrome.
2724
+ configureServer(server) {
2725
+ if (!external) return;
2726
+ const devBase =
2727
+ server && server.config && typeof server.config.base === "string"
2728
+ ? server.config.base
2729
+ : "/";
2730
+ const prefix = devBase === "/" || devBase === "" ? "/" : devBase.replace(/\/?$/, "/");
2731
+ const urls = new Set([`${prefix}${BOOT_RECOVERY_SCRIPT}`, `/${BOOT_RECOVERY_SCRIPT}`]);
2732
+ server.middlewares.use((req, res, next) => {
2733
+ const path = (req.url || "").split("?")[0];
2734
+ if (!urls.has(path)) {
2735
+ next();
2736
+ return;
2737
+ }
2738
+ let body;
2739
+ try {
2740
+ body = bootRecoveryScript(opts);
2741
+ } catch (err) {
2742
+ next(err);
2743
+ return;
2744
+ }
2745
+ res.statusCode = 200;
2746
+ res.setHeader("Content-Type", "application/javascript; charset=utf-8");
2747
+ res.setHeader("Cache-Control", "no-cache");
2748
+ res.setHeader("Content-Length", Buffer.byteLength(body));
2749
+ if (req.method === "HEAD") {
2750
+ res.end();
2751
+ return;
2752
+ }
2753
+ res.end(body);
2754
+ });
2755
+ },
2644
2756
  };
2645
2757
  }