@dokebi/template-inertia 0.4.26 → 0.4.28

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.
@@ -41,6 +41,8 @@ export class TemplateGeneration {
41
41
  importMap;
42
42
  rejected;
43
43
  rejectedWhy;
44
+ csp;
45
+ styles;
44
46
  setAside = new Set();
45
47
  /** D-0294: why each page set aside at render was (and when). */
46
48
  setAsideWhy = new Map();
@@ -57,7 +59,11 @@ export class TemplateGeneration {
57
59
  /** Pages the app's check refused. */
58
60
  rejected,
59
61
  /** D-0294: the refusals of `rejected`, with verifyTemplate's reasons. */
60
- rejectedWhy = []) {
62
+ rejectedWhy = [],
63
+ /** D-0297: the CSP sources its answers add, and what web made of them (`cspAddition`). */
64
+ csp = undefined,
65
+ /** D-0298: the URLs of the template's own stylesheets (the manifest's `styles`), in order. */
66
+ styles = []) {
61
67
  this.app = app;
62
68
  this.template = template;
63
69
  this.buildId = buildId;
@@ -68,6 +74,8 @@ export class TemplateGeneration {
68
74
  this.importMap = importMap;
69
75
  this.rejected = rejected;
70
76
  this.rejectedWhy = rejectedWhy;
77
+ this.csp = csp;
78
+ this.styles = styles;
71
79
  }
72
80
  /** The page table of the first HTML (JSON, without the pages set aside). */
73
81
  table() {
@@ -197,6 +205,7 @@ export class TemplateBundles {
197
205
  #repeat;
198
206
  #production;
199
207
  #words;
208
+ #cspAddition;
200
209
  #settings;
201
210
  #apps = new Map();
202
211
  #slots = new Map();
@@ -212,6 +221,7 @@ export class TemplateBundles {
212
221
  this.#repeat = options.repeat;
213
222
  this.#production = options.production ?? process.env.NODE_ENV === 'production';
214
223
  this.#words = options.words;
224
+ this.#cspAddition = options.cspAddition;
215
225
  this.#cacheRoot = resolve(options.settings.cacheDir);
216
226
  const store = this.#store;
217
227
  globalThis[TEMPLATE_REGISTRY] = Object.freeze({
@@ -567,6 +577,8 @@ export class TemplateBundles {
567
577
  throw new Refused('INERTIA_TEMPLATE_REJECTED', problems.map((problem) => problem.message).join('; '), { shared: needed });
568
578
  // The app's own judgement: signature and page contracts.
569
579
  let rejectPages = [];
580
+ // D-0297: the declared CSP sources the app lets in (all, unless verifyTemplate narrows them).
581
+ let allowedCsp = manifest.csp;
570
582
  if (this.#verify) {
571
583
  const verify = this.#verify;
572
584
  let timer;
@@ -606,6 +618,11 @@ export class TemplateBundles {
606
618
  if (!answer.accept)
607
619
  throw new Refused('INERTIA_TEMPLATE_REJECTED', `verifyTemplate refused it: ${String(answer.reason)}`);
608
620
  rejectPages = answer.rejectPages ?? [];
621
+ if (answer.csp !== undefined) {
622
+ if (answer.csp === null || typeof answer.csp !== 'object' || Array.isArray(answer.csp))
623
+ throw new Refused('INERTIA_TEMPLATE_REJECTED', 'verifyTemplate answered csp that is not an object of directive → sources');
624
+ allowedCsp = answer.csp;
625
+ }
609
626
  }
610
627
  else if (this.#production)
611
628
  throw new Refused('INERTIA_TEMPLATE_UNVERIFIED', 'no verifyTemplate: in production a bundle runs only after the app judged it');
@@ -679,7 +696,8 @@ export class TemplateBundles {
679
696
  const hasWords = Object.keys(manifest.files).some((file) => file.startsWith('locales/'));
680
697
  if (hasWords || slot.words)
681
698
  await this.#setWords(slot, hasWords ? join(dir, 'locales') : undefined, buildId);
682
- const generation = new TemplateGeneration(slot.app, slot.template, buildId, manifest, dir, pages, server, importMap, [...refused], refusedWhy);
699
+ const csp = this.#csp(slot, buildId, manifest.csp, allowedCsp);
700
+ const generation = new TemplateGeneration(slot.app, slot.template, buildId, manifest, dir, pages, server, importMap, [...refused], refusedWhy, csp, (manifest.styles ?? []).map(url));
683
701
  const previous = slot.current;
684
702
  slot.current = generation;
685
703
  slot.failure = undefined;
@@ -696,6 +714,8 @@ export class TemplateBundles {
696
714
  pages: pages.size,
697
715
  rejectedPages: [...refused],
698
716
  serverRendering: provided.server !== undefined,
717
+ styles: manifest.styles ?? [],
718
+ csp: csp?.sources ?? null,
699
719
  kept: slot.kept.map((kept) => kept.buildId),
700
720
  ms: Math.round(performance.now() - begin),
701
721
  });
@@ -709,6 +729,56 @@ export class TemplateBundles {
709
729
  this.#fail(slot, error);
710
730
  }
711
731
  }
732
+ /**
733
+ * D-0297: the CSP sources of a generation: the declared ones the app let in, made into web's
734
+ * addition once (each answer of the template then adds it). Undefined without a declaration.
735
+ */
736
+ #csp(slot, buildId, declared, allowed) {
737
+ if (declared === undefined)
738
+ return undefined;
739
+ const sources = {};
740
+ const refused = {};
741
+ for (const [name, list] of Object.entries(declared)) {
742
+ const allowedList = allowed?.[name];
743
+ const kept = Array.isArray(allowedList)
744
+ ? list.filter((source) => allowedList.includes(source))
745
+ : [];
746
+ const left = list.filter((source) => !kept.includes(source));
747
+ if (kept.length)
748
+ sources[name] = Object.freeze(kept);
749
+ if (left.length)
750
+ refused[name] = Object.freeze(left);
751
+ }
752
+ if (Object.keys(refused).length)
753
+ this.#logger.log('warn', 'Template bundle CSP sources left out by verifyTemplate', {
754
+ code: 'INERTIA_TEMPLATE_CSP_REFUSED',
755
+ app: slot.app,
756
+ template: slot.template,
757
+ buildId,
758
+ refused,
759
+ });
760
+ let addition;
761
+ if (Object.keys(sources).length) {
762
+ if (this.#cspAddition)
763
+ addition = this.#cspAddition(sources);
764
+ else {
765
+ const repeated = this.#repeat(`csp ${slot.app} ${slot.template}`);
766
+ if (repeated !== undefined)
767
+ this.#logger.log('warn', 'Template bundle CSP sources not used: web has no cspAddition', {
768
+ code: 'INERTIA_TEMPLATE_CSP_UNSUPPORTED',
769
+ app: slot.app,
770
+ template: slot.template,
771
+ sources,
772
+ fix: 'npm install @dokebi/web@^0.4.28',
773
+ });
774
+ }
775
+ }
776
+ return Object.freeze({
777
+ sources: Object.freeze(sources),
778
+ refused: Object.freeze(refused),
779
+ addition,
780
+ });
781
+ }
712
782
  /** D-0291 stage 3b: hands a generation's words (or none) to @dokebi/i18n. Never throws. */
713
783
  async #setWords(slot, directory, buildId) {
714
784
  const words = this.#words;
@@ -805,13 +875,17 @@ export class TemplateBundles {
805
875
  .filter((page) => !generation.setAside.has(page))
806
876
  .map((page) => generation.pages.get(page))
807
877
  .filter((urls) => urls !== undefined);
878
+ // D-0298: the template's own stylesheets first, in their order, then the pages' own.
879
+ const look = generation.styles
880
+ .map((href) => `<link rel="stylesheet"${attribute} href="${escapeHtml(href)}">`)
881
+ .join('');
808
882
  const css = [...new Set(first.flatMap((urls) => urls.css))]
809
883
  .map((href) => `<link rel="stylesheet" href="${escapeHtml(href)}">`)
810
884
  .join('');
811
885
  const preload = [...new Set(first.flatMap((urls) => [urls.js, ...urls.preload]))]
812
886
  .map((href) => `<link rel="modulepreload"${attribute} href="${escapeHtml(href)}">`)
813
887
  .join('');
814
- return `${map}${table}${css}${preload}`;
888
+ return `${map}${table}${look}${css}${preload}`;
815
889
  },
816
890
  async render(run, component) {
817
891
  // The page first: a failure that took the component's own bundle page sets that page
@@ -900,6 +974,12 @@ function stateOf(slot) {
900
974
  buildId: current.buildId,
901
975
  loadedAt: current.loadedAt,
902
976
  rejectedPages: Object.freeze([...current.rejectedWhy, ...current.setAsideWhy.values()]),
977
+ ...(current.csp === undefined
978
+ ? {}
979
+ : {
980
+ csp: current.csp.sources,
981
+ ...(Object.keys(current.csp.refused).length ? { cspRefused: current.csp.refused } : {}),
982
+ }),
903
983
  });
904
984
  return Object.freeze({
905
985
  ...base,
@@ -1,4 +1,4 @@
1
- export { buildTemplateBundle, packageEntry, TEMPLATE_CARRY_FOLDER, type TemplateBuildOptions, type TemplateBuildResult, templatePages, } from './template-build.js';
2
- export { checkTemplateBundle, compatibleLine, type ProvidedShared, SERVER_ONLY_SHARED, SHARED_DEFAULTS, sharedProblems, TEMPLATE_FORMAT, TEMPLATE_MANIFEST, TEMPLATE_MAX_BYTES, TEMPLATE_NAME, TEMPLATE_SIGNATURE, type TemplateCheck, type TemplateCheckLimits, type TemplateManifest, type TemplatePage, type TemplateProblem, } from './template-bundle.js';
3
- export { PREVIEW_PREFIX, previewCases, previewWords, startTemplatePreview, type TemplatePreview, type TemplatePreviewOptions, type TemplatePreviewPage, } from './template-preview.js';
1
+ export { buildTemplateBundle, packageEntry, TEMPLATE_CARRY_FOLDER, TEMPLATE_CSP_FILE, type TemplateBuildOptions, type TemplateBuildResult, templatePages, } from './template-build.js';
2
+ export { checkTemplateBundle, compatibleLine, cspDeclarationProblem, type ProvidedShared, SERVER_ONLY_SHARED, SHARED_DEFAULTS, sharedProblems, TEMPLATE_CSP_DIRECTIVES, TEMPLATE_FORMAT, TEMPLATE_MANIFEST, TEMPLATE_MAX_BYTES, TEMPLATE_NAME, TEMPLATE_SIGNATURE, TEMPLATE_STYLES_MAX, type TemplateCheck, type TemplateCheckLimits, type TemplateManifest, type TemplatePage, type TemplateProblem, } from './template-bundle.js';
3
+ export { PREVIEW_LOCALE_COOKIE, PREVIEW_PREFIX, type PreviewWordsOptions, previewCases, previewPolicy, previewWords, startTemplatePreview, type TemplatePreview, type TemplatePreviewOptions, type TemplatePreviewPage, } from './template-preview.js';
4
4
  export { type DokebiInertiaTemplateOptions, packageVersion, SHARED_FILE, type SharedFile, sharedEntryName, } from './template-shared.js';
package/dist/template.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // `@dokebi/template-inertia/template` (D-0291): template bundles — the format and its check, and
2
2
  // the build that `dokebi template build` runs in a template repository.
3
- export { buildTemplateBundle, packageEntry, TEMPLATE_CARRY_FOLDER, templatePages, } from './template-build.js';
4
- export { checkTemplateBundle, compatibleLine, SERVER_ONLY_SHARED, SHARED_DEFAULTS, sharedProblems, TEMPLATE_FORMAT, TEMPLATE_MANIFEST, TEMPLATE_MAX_BYTES, TEMPLATE_NAME, TEMPLATE_SIGNATURE, } from './template-bundle.js';
5
- export { PREVIEW_PREFIX, previewCases, previewWords, startTemplatePreview, } from './template-preview.js';
3
+ export { buildTemplateBundle, packageEntry, TEMPLATE_CARRY_FOLDER, TEMPLATE_CSP_FILE, templatePages, } from './template-build.js';
4
+ export { checkTemplateBundle, compatibleLine, cspDeclarationProblem, SERVER_ONLY_SHARED, SHARED_DEFAULTS, sharedProblems, TEMPLATE_CSP_DIRECTIVES, TEMPLATE_FORMAT, TEMPLATE_MANIFEST, TEMPLATE_MAX_BYTES, TEMPLATE_NAME, TEMPLATE_SIGNATURE, TEMPLATE_STYLES_MAX, } from './template-bundle.js';
5
+ export { PREVIEW_LOCALE_COOKIE, PREVIEW_PREFIX, previewCases, previewPolicy, previewWords, startTemplatePreview, } from './template-preview.js';
6
6
  export { packageVersion, SHARED_FILE, sharedEntryName, } from './template-shared.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.26",
3
+ "version": "0.4.28",
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",
@@ -65,7 +65,7 @@
65
65
  },
66
66
  "peerDependencies": {
67
67
  "@dokebi/core": "^0.4.2",
68
- "@dokebi/web": "^0.4.19",
68
+ "@dokebi/web": "^0.4.28",
69
69
  "fastify": "^5.12.7",
70
70
  "vue": "^3.5.0"
71
71
  },
@@ -81,7 +81,7 @@
81
81
  "dependencies": [
82
82
  {
83
83
  "name": "dokebi-web",
84
- "version": "^0.4.19"
84
+ "version": "^0.4.28"
85
85
  },
86
86
  {
87
87
  "name": "dokebi-session",
@@ -154,7 +154,7 @@
154
154
  "devDependencies": {
155
155
  "@dokebi/i18n": "0.2.10",
156
156
  "@dokebi/session": "0.4.11",
157
- "@dokebi/web": "0.4.27",
157
+ "@dokebi/web": "0.4.28",
158
158
  "@dokebi/websocket": "0.4.10",
159
159
  "@dokebi/websocket-hub": "0.4.6",
160
160
  "@inertiajs/vue3": "3.9.1",
@@ -8,4 +8,5 @@ export async function shared(_request: FastifyRequest) {
8
8
 
9
9
  // D-0291: with template bundles (options.dokebi-template-inertia.templates), export
10
10
  // verifyTemplate(bundle) here too: it judges a bundle (its signature, each page's meta.contract)
11
- // before the app uses it; in production a bundle is not used without it (AI.md §4.3).
11
+ // before the app uses it; in production a bundle is not used without it (AI.md §4.3). Its answer
12
+ // may also narrow the CSP sources the bundle declares (`csp`, D-0297).