@dokebi/template-inertia 0.4.12 → 0.4.14

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/AI.md CHANGED
@@ -272,7 +272,7 @@ A module keeps its Vue pages in its own views folder (@dokebi/module AI.md §4a)
272
272
  - Cluster apps run one worker under `dokebi dev` (@dokebi/cluster); `DOKEBI_DEV_WORKERS` asks for more, which also works on a cold cache.
273
273
  - When a build marks absolute-path imports as `external`, the dev server prefixes those paths with `<base>`. In the dev server use a Vite plugin that resolves them to real files (see the playground's `vite.config.ts`).
274
274
  - Production (no `DOKEBI_VITE_DEV`) is unaffected. Never set this variable on a production server.
275
- - **Content-Security-Policy** (dokebi-web security headers, [D-0149](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0149-security-headers.md)): in development this plugin adds `ws://*:* wss://*:*` to `connect-src` for Vite's HMR socket, which connects to the page's own origin and `<base>` (`ws://localhost:<app port>/build/`, or `wss://<public host>/build/` through a tunnel; §5.1), and `worker-src 'self' blob:` for the `blob:` SharedWorker Vite's client pings a restarted dev host from (without it the page never reconnects); production adds nothing. Pages load their scripts from `'self'` (the page data is JSON, not a script); keep `'unsafe-inline'` in a policy of your own when a Vite framework plugin adds inline scripts in development.
275
+ - **Content-Security-Policy** (dokebi-web security headers, [D-0149](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0149-security-headers.md)): Vite's HMR socket connects to the page's own origin and `<base>` (`ws://localhost:<app port>/build/`, or `wss://<public host>/build/` through a tunnel; §5.1), which `connect-src 'self'` allows (CSP3), so nothing is added for it (0.4.13, [D-0249](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0249-dev-hmr-csp-self.md); before, `ws://*:* wss://*:*`). Only a project whose Vite config sends the socket elsewhere — `server.ws` (or the older `server.hmr`) with `host`, `protocol`, `port` or `clientPort` — gets that origin added in development, exactly: the scheme (both `ws:` and `wss:` when the page decides), the host (`*` when it is the page's) and the port (`clientPort`, else `port`; `*` when it is the page's), e.g. `clientPort: 5174` → `ws://*:5174 wss://*:5174`. The dev host reports it when an app process connects (debug `Vite HMR socket origin added to connect-src`). In development this plugin also adds `worker-src 'self' blob:` for the `blob:` SharedWorker Vite's client pings a restarted dev host from (without it the page never reconnects); production adds nothing. Pages load their scripts from `'self'` (the page data is JSON, not a script); keep `'unsafe-inline'` in a policy of your own when a Vite framework plugin adds inline scripts in development.
276
276
  - **Nonce policy** (0.4.12, [D-0247](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0247-csp-nonce.md); web 0.4.19 `contentSecurityPolicy: { nonce: true, styleNonce?: true }`): the first HTML — plain, sealed or server rendered, built or in development — writes the request's nonce (`web.cspNonce(request)`) on every module script and on the boot fallback's `<style>` and `<script>`, and adds `<meta property="csp-nonce" nonce="…">`, which Vite's client (development `<style>` of imported CSS) and its preload helper read. The page data block (`type="application/json"`) is never run and needs none. What the entry imports or creates is trusted by `'strict-dynamic'`. Inertia's own progress bar and error dialog styles take the nonce from `createInertiaApp({ nonce: document.querySelector('meta[property="csp-nonce"]')?.nonce || undefined })` (the generated `views/app.js` does; add it to an older entry before `styleNonce`). Checked in Chromium: a built Vue app with SSR and navigation (example 24), a seal host with server-rendered public pages and a framed page (example 80), and the Vite dev page with imported CSS, a dynamic import and the HMR client (`tests/dev-files.test.ts`).
277
277
  - The host answers a Vite middleware error with 500 (error log `INERTIA_VITE_HOST_FAILED`); 404 only when Vite has nothing for the URL. A host that does not answer is a 502 with error `INERTIA_VITE_DEV_FAILED` (once per 10 seconds, `repeated`); a host that cannot be reached at start is `VITE_DEV_UNAVAILABLE`.
278
278
  - Debug logs (D-0047): `Inertia settings resolved`, `Vite dev server ready` (ms, `shared`), `Vite dev route registered`, `Vite manifest read`, `Inertia SSR bundle loaded`, `Inertia SSR render deadline` (`timeoutMs`), `Inertia SSR render finished after its deadline` (a cut render that ended later), `Inertia SSR renders the source through Vite` (the source used), `Inertia scope created`, `Vite dev uses the host of dokebi dev`, `Vite dev starts a host in this process`, `Vite dev host connected` / `disconnected`; in the host `Vite dev host listening` (`port`, `hosts`), `Vite dev host started` (ms), `Vite dev host closed` (`connections`), `Vite dev host listens on IPv4 loopback only`, `Vite dev host port is taken on ::1, trying another`, `Vite dev host filters watcher events of unchanged files`, `Vite dev host ignored an event for a file that did not change` (the first and every 100th, `dropped`); `Vite dev host settings read` (`depsRepairIntervalMs`), `Vite dev host recorded itself in the cache folder` (`shared`), `Vite dev host re-optimized its dependencies` (`trigger`, `ms`, `repairs`), and per check only when debug is on `Vite dev host dependency cache matches` (`trigger`, `files`); per real file event, only when debug is on, `Vite dev host passes a file change`. A file whose state cannot be read is warn `INERTIA_VITE_WATCH_STAT_FAILED` (the event goes on). Per request, only when debug is on: `Vite dev route refused a host`, `Vite dev host closed an upgrade that is not Vite`, `Vite dev host refused a request without its key`, `Vite dev request ended by the browser`. Host listener and request failures are error `INERTIA_VITE_HOST_FAILED`.
package/dist/index.js CHANGED
@@ -519,21 +519,25 @@ class InertiaRuntime {
519
519
  }
520
520
  // Development (D-0045): one Vite for every scope, reached by the first `vite` (the host of
521
521
  // `dokebi dev`, shared by every worker, or one started in this process).
522
- const develop = viteDevRequested();
523
- // D-0149: the HMR socket of the Vite dev host is on its own port of the page's host name;
524
- // the default CSP (connect-src 'self') would block it. When the dev host restarts, Vite's
525
- // client pings it from a `blob:` SharedWorker, which script-src would block without
526
- // worker-src (community request A4). Development only.
522
+ // D-0251: a process that opens no web port (`dokebi-web.listen: false`, a worker next to the
523
+ // web process) serves no page: it reads no Vite manifest or SSR bundle and opens no Vite, so a
524
+ // worker starts without a build and `dokebi dev` gives it no Vite.
525
+ const serving = context.app.config['dokebi-web']?.listen !==
526
+ false;
527
+ const develop = serving && viteDevRequested();
528
+ // When the dev host restarts, Vite's client pings it from a `blob:` SharedWorker, which
529
+ // script-src would block without worker-src (community request A4). The HMR socket itself
530
+ // goes to the page's own origin, which connect-src 'self' allows; a Vite config that sends it
531
+ // elsewhere gets that origin added below (D-0249; it was ws://*:* wss://*:* before).
532
+ // Development only.
527
533
  if (develop)
528
534
  server.contentSecurityPolicy({
529
535
  plugin: 'dokebi-template-inertia',
530
- directives: {
531
- 'connect-src': ['ws://*:*', 'wss://*:*'],
532
- 'worker-src': ["'self'", 'blob:'],
533
- },
536
+ directives: { 'worker-src': ["'self'", 'blob:'] },
534
537
  });
535
538
  logger.log('debug', 'Inertia settings resolved', {
536
539
  develop,
540
+ serving,
537
541
  vite: this.settings.vite ? { ...this.settings.vite } : false,
538
542
  ssr: this.settings.ssr ? { ...this.settings.ssr } : false,
539
543
  devHosts: this.settings.devHosts.length,
@@ -652,7 +656,7 @@ class InertiaRuntime {
652
656
  nodeServer?.removeListener('upgrade', onUpgrade);
653
657
  });
654
658
  }
655
- const viteDev = (base) => {
659
+ const openDev = () => {
656
660
  if (!devServer) {
657
661
  const begin = performance.now();
658
662
  // A start failure reaches setup or the request that awaits the server.
@@ -669,7 +673,10 @@ class InertiaRuntime {
669
673
  code: 'INERTIA_VITE_DEV',
670
674
  });
671
675
  }
672
- const current = devServer;
676
+ return devServer;
677
+ };
678
+ const viteDev = (base) => {
679
+ const current = openDev();
673
680
  if (!devBases.has(base)) {
674
681
  devBases.add(base);
675
682
  // Modules, pre-bundled dependencies and @vite/client, under the Vite base.
@@ -732,6 +739,21 @@ class InertiaRuntime {
732
739
  }
733
740
  return current;
734
741
  };
742
+ // D-0249: the HMR socket's origin is the page's own unless the project's Vite config names
743
+ // another (server.ws / server.hmr host, protocol, port or clientPort): only the dev host
744
+ // knows, so it is asked now, while CSP sources may still be added (web freezes them at start).
745
+ if (develop && context.runtime.describing !== true) {
746
+ const opened = await openDev();
747
+ if (opened.hmrSources.length) {
748
+ server.contentSecurityPolicy({
749
+ plugin: 'dokebi-template-inertia',
750
+ directives: { 'connect-src': [...opened.hmrSources] },
751
+ });
752
+ logger.log('debug', 'Vite HMR socket origin added to connect-src', {
753
+ sources: opened.hmrSources,
754
+ });
755
+ }
756
+ }
735
757
  // Built files are served at their exact URL (no wildcard), once even if apps share chunks.
736
758
  const served = new Set();
737
759
  const loaded = new Set();
@@ -740,6 +762,8 @@ class InertiaRuntime {
740
762
  const built = async (options) => {
741
763
  if (describing)
742
764
  return { scripts: [], styles: [], version: 'describe' };
765
+ if (!serving)
766
+ return { scripts: [], styles: [], version: 'not-serving' };
743
767
  if (develop) {
744
768
  await viteDev(options.base);
745
769
  return {
@@ -839,7 +863,7 @@ class InertiaRuntime {
839
863
  const bundles = new Map();
840
864
  const stats = { renders: 0, failures: 0 };
841
865
  const serverRendering = async (ssr, vite) => {
842
- if (!ssr || describing)
866
+ if (!ssr || describing || !serving)
843
867
  return undefined;
844
868
  let render;
845
869
  if (develop) {
@@ -1037,7 +1061,7 @@ class InertiaRuntime {
1037
1061
  }
1038
1062
  export const createPlugin = definePlugin({
1039
1063
  name: 'dokebi-template-inertia',
1040
- version: '0.4.12',
1064
+ version: '0.4.14',
1041
1065
  core: '^0.4.2',
1042
1066
  options,
1043
1067
  entry: parseEntry,
@@ -39,6 +39,12 @@ export interface ViteHost {
39
39
  }
40
40
  /** What an app process uses: the host's files for `<base>*` and server rendering. */
41
41
  export interface ViteDev {
42
+ /**
43
+ * D-0249: the CSP sources of the HMR socket when the project's Vite config sends it somewhere
44
+ * other than the page's own origin (`server.ws` / `server.hmr` with `host`, `protocol`, `port`
45
+ * or `clientPort`); empty when it connects to the page's origin, which `'self'` allows.
46
+ */
47
+ readonly hmrSources: readonly string[];
42
48
  /** Answers a dev route request from the host (status, headers and body as Vite sent them). */
43
49
  forward(request: IncomingMessage, response: ServerResponse, report: (error: unknown) => void): void;
44
50
  /**
@@ -91,6 +97,14 @@ export declare function destroyRequest(outgoing: ClientRequest, error?: Error):
91
97
  * Connects this process to a Vite host and checks that it answers with the key. Requests share
92
98
  * keep-alive connections; `close` ends them (the host keeps running).
93
99
  */
100
+ /**
101
+ * D-0249: the CSP sources of an HMR socket Vite's client sends elsewhere than the page's origin,
102
+ * from the resolved `server.ws` (Vite 8; `server.hmr` is folded into it): the scheme (both when
103
+ * the config leaves it to the page), the host (`*` when the page's host is used) and the port
104
+ * (`clientPort`, else `port`; `*` when the page's port is used). None when the config names none
105
+ * of them: the client then connects to the page's own origin and `<base>`.
106
+ */
107
+ export declare function hmrSources(ws: unknown): string[];
94
108
  export declare function connectViteDev(logger: Logger, url: string, key: string): Promise<ViteDev>;
95
109
  /**
96
110
  * The Vite of this process: the host of `dokebi dev` when `DOKEBI_VITE_DEV_URL` and
package/dist/vite-dev.js CHANGED
@@ -285,7 +285,12 @@ export async function startViteHost(logger, root = process.cwd(), settings = {})
285
285
  if (path === HOST_PATH && !browser) {
286
286
  // An app process connects (it started or restarted): it is answered once the pre-bundled
287
287
  // dependencies match what Vite serves, so its pages get hashes that load (D-0128).
288
- void (cache ? cache.ensure('client').catch(report('deps')) : Promise.resolve()).then(() => sendJson(response, 200, { root, pid: process.pid }));
288
+ void (cache ? cache.ensure('client').catch(report('deps')) : Promise.resolve()).then(() => sendJson(response, 200, {
289
+ root,
290
+ pid: process.pid,
291
+ // D-0249: where Vite's client sends the HMR socket, when not to the page's origin.
292
+ hmr: hmrSources(server.config.server.ws),
293
+ }));
289
294
  }
290
295
  else if (path === SSR_PATH && request.method === 'POST' && !browser)
291
296
  void ssr(request, response, server);
@@ -422,6 +427,27 @@ export function destroyRequest(outgoing, error) {
422
427
  * Connects this process to a Vite host and checks that it answers with the key. Requests share
423
428
  * keep-alive connections; `close` ends them (the host keeps running).
424
429
  */
430
+ /**
431
+ * D-0249: the CSP sources of an HMR socket Vite's client sends elsewhere than the page's origin,
432
+ * from the resolved `server.ws` (Vite 8; `server.hmr` is folded into it): the scheme (both when
433
+ * the config leaves it to the page), the host (`*` when the page's host is used) and the port
434
+ * (`clientPort`, else `port`; `*` when the page's port is used). None when the config names none
435
+ * of them: the client then connects to the page's own origin and `<base>`.
436
+ */
437
+ export function hmrSources(ws) {
438
+ if (ws === null || typeof ws !== 'object')
439
+ return [];
440
+ const { host, protocol, port, clientPort } = ws;
441
+ const reach = clientPort ?? port;
442
+ const named = (value) => value !== undefined && value !== null && value !== '';
443
+ if (!named(host) && !named(protocol) && !named(reach))
444
+ return [];
445
+ const name = named(host) ? String(host) : '*';
446
+ const hostPart = name.includes(':') && !name.startsWith('[') ? `[${name}]` : name;
447
+ const portPart = named(reach) ? `:${Number(reach)}` : ':*';
448
+ const schemes = named(protocol) ? [String(protocol)] : ['ws', 'wss'];
449
+ return schemes.map((scheme) => `${scheme}://${hostPart}${portPart}`);
450
+ }
425
451
  export async function connectViteDev(logger, url, key) {
426
452
  const target = hostUrl(url);
427
453
  const host = target.hostname.replace(/^\[(.*)\]$/, '$1');
@@ -467,10 +493,15 @@ export async function connectViteDev(logger, url, key) {
467
493
  }
468
494
  outgoing.end(body);
469
495
  });
496
+ let sources = [];
470
497
  try {
471
- const { status } = await call('GET', HOST_PATH);
498
+ const { status, text } = await call('GET', HOST_PATH);
472
499
  if (status !== 200)
473
500
  throw new InertiaError('VITE_DEV_UNAVAILABLE', `The Vite dev host at ${target.origin} answered ${status} (wrong DOKEBI_VITE_DEV_KEY?)`);
501
+ // D-0249: a host before it sends no `hmr`: its socket is the page's origin, as before.
502
+ const answer = JSON.parse(text);
503
+ if (Array.isArray(answer.hmr))
504
+ sources = answer.hmr.filter((source) => typeof source === 'string');
474
505
  }
475
506
  catch (cause) {
476
507
  agent.destroy();
@@ -479,10 +510,11 @@ export async function connectViteDev(logger, url, key) {
479
510
  throw cause;
480
511
  throw new InertiaError('VITE_DEV_UNAVAILABLE', `The Vite dev host at ${target.origin} cannot be reached`, { cause });
481
512
  }
482
- logger.log('debug', 'Vite dev host connected', { url: target.origin });
513
+ logger.log('debug', 'Vite dev host connected', { url: target.origin, hmr: sources });
483
514
  /** HMR sockets joined to the host, ended by close(). */
484
515
  const joined = new Set();
485
516
  return {
517
+ hmrSources: Object.freeze(sources),
486
518
  upgrade(request, socket, head, report) {
487
519
  const upstream = connectNet({ host, port });
488
520
  joined.add(socket);
@@ -621,6 +653,7 @@ export async function openViteDev(logger, environment = process.env, root = proc
621
653
  throw error;
622
654
  }
623
655
  return {
656
+ hmrSources: client.hmrSources,
624
657
  forward: client.forward,
625
658
  render: client.render,
626
659
  upgrade: client.upgrade,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.12",
3
+ "version": "0.4.14",
4
4
  "description": "Inertia v3 server adapter for Dokebi: page JSON, Vite manifest assets and optional server-side rendering.",
5
5
  "keywords": [
6
6
  "dokebi",
@@ -145,7 +145,7 @@
145
145
  "devDependencies": {
146
146
  "@dokebi/i18n": "0.2.8",
147
147
  "@dokebi/session": "0.4.9",
148
- "@dokebi/web": "0.4.19",
148
+ "@dokebi/web": "0.4.20",
149
149
  "@dokebi/websocket": "0.4.9",
150
150
  "@dokebi/websocket-hub": "0.4.5",
151
151
  "@inertiajs/vue3": "3.9.1",