@demiurgejs/core 0.1.1 → 0.2.0-beta.2

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 (151) hide show
  1. package/README.md +5 -0
  2. package/bin/demiurge.mjs +51 -0
  3. package/dist/adapter/testing.d.ts +33 -0
  4. package/dist/adapter/testing.d.ts.map +1 -0
  5. package/dist/adapter/testing.js +251 -0
  6. package/dist/adapter/testing.js.map +1 -0
  7. package/dist/browser/file-router.d.ts.map +1 -1
  8. package/dist/chunks/document-shell-D1xo6cEA.js +35 -0
  9. package/dist/chunks/document-shell-D1xo6cEA.js.map +1 -0
  10. package/dist/chunks/image-codec-BGTx_t5K.js +141 -0
  11. package/dist/chunks/image-codec-BGTx_t5K.js.map +1 -0
  12. package/dist/chunks/images-B9MqcjSA.js +339 -0
  13. package/dist/chunks/images-B9MqcjSA.js.map +1 -0
  14. package/dist/chunks/request-metadata-D5NrT8ks.js +642 -0
  15. package/dist/chunks/request-metadata-D5NrT8ks.js.map +1 -0
  16. package/dist/chunks/streaming-BQ1bykDo.js +469 -0
  17. package/dist/chunks/streaming-BQ1bykDo.js.map +1 -0
  18. package/dist/chunks/{testing-DR3eQAvY.js → testing-B_iMFYz_.js} +2376 -1927
  19. package/dist/chunks/testing-B_iMFYz_.js.map +1 -0
  20. package/dist/cli.d.ts +42 -0
  21. package/dist/cli.d.ts.map +1 -0
  22. package/dist/cli.js +173 -0
  23. package/dist/cli.js.map +1 -0
  24. package/dist/document/deferred-scripts.d.ts +11 -0
  25. package/dist/document/deferred-scripts.d.ts.map +1 -0
  26. package/dist/document/index.d.ts +4 -2
  27. package/dist/document/index.d.ts.map +1 -1
  28. package/dist/document/render.d.ts +1 -1
  29. package/dist/document/render.d.ts.map +1 -1
  30. package/dist/document/scripts.d.ts +34 -1
  31. package/dist/document/scripts.d.ts.map +1 -1
  32. package/dist/edge/assets.d.ts +14 -0
  33. package/dist/edge/assets.d.ts.map +1 -0
  34. package/dist/edge/index.d.ts +19 -0
  35. package/dist/edge/index.d.ts.map +1 -0
  36. package/dist/edge/index.js +286 -0
  37. package/dist/edge/index.js.map +1 -0
  38. package/dist/edge/stores.d.ts +8 -0
  39. package/dist/edge/stores.d.ts.map +1 -0
  40. package/dist/edge/streaming.d.ts +7 -0
  41. package/dist/edge/streaming.d.ts.map +1 -0
  42. package/dist/index.d.ts +9 -9
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +894 -1043
  45. package/dist/index.js.map +1 -1
  46. package/dist/internal/testing.js +12 -11
  47. package/dist/internal/testing.js.map +1 -1
  48. package/dist/kv/index.d.ts +3 -0
  49. package/dist/kv/index.d.ts.map +1 -0
  50. package/dist/kv/index.js +103 -0
  51. package/dist/kv/index.js.map +1 -0
  52. package/dist/kv/store.d.ts +28 -0
  53. package/dist/kv/store.d.ts.map +1 -0
  54. package/dist/node/font.d.ts +5 -0
  55. package/dist/node/font.d.ts.map +1 -0
  56. package/dist/node/image.d.ts +15 -0
  57. package/dist/node/image.d.ts.map +1 -0
  58. package/dist/node/index.d.ts +4 -0
  59. package/dist/node/index.d.ts.map +1 -1
  60. package/dist/node/index.js +25 -23
  61. package/dist/node/index.js.map +1 -1
  62. package/dist/node/streaming.d.ts +2 -0
  63. package/dist/node/streaming.d.ts.map +1 -1
  64. package/dist/platform/analytics.d.ts +58 -5
  65. package/dist/platform/analytics.d.ts.map +1 -1
  66. package/dist/platform/font-assets.d.ts +19 -0
  67. package/dist/platform/font-assets.d.ts.map +1 -0
  68. package/dist/platform/fonts.d.ts +14 -4
  69. package/dist/platform/fonts.d.ts.map +1 -1
  70. package/dist/platform/image-codec.d.ts +15 -0
  71. package/dist/platform/image-codec.d.ts.map +1 -0
  72. package/dist/platform/image-url.d.ts +17 -0
  73. package/dist/platform/image-url.d.ts.map +1 -0
  74. package/dist/platform/image.d.ts +8 -0
  75. package/dist/platform/image.d.ts.map +1 -0
  76. package/dist/platform/images.d.ts +15 -0
  77. package/dist/platform/images.d.ts.map +1 -1
  78. package/dist/platform/index.d.ts +8 -5
  79. package/dist/platform/index.d.ts.map +1 -1
  80. package/dist/redis/index.d.ts +3 -0
  81. package/dist/redis/index.d.ts.map +1 -0
  82. package/dist/redis/index.js +171 -0
  83. package/dist/redis/index.js.map +1 -0
  84. package/dist/redis/store.d.ts +8 -0
  85. package/dist/redis/store.d.ts.map +1 -0
  86. package/dist/route/action.d.ts +11 -11
  87. package/dist/route/action.d.ts.map +1 -1
  88. package/dist/route/index.d.ts +2 -1
  89. package/dist/route/index.d.ts.map +1 -1
  90. package/dist/route/middleware.d.ts +7 -0
  91. package/dist/route/middleware.d.ts.map +1 -0
  92. package/dist/route/page.d.ts +2 -2
  93. package/dist/route/page.d.ts.map +1 -1
  94. package/dist/route/response.d.ts +19 -19
  95. package/dist/route/response.d.ts.map +1 -1
  96. package/dist/route/types.d.ts +55 -31
  97. package/dist/route/types.d.ts.map +1 -1
  98. package/dist/route/webhook.d.ts +1 -1
  99. package/dist/router/manifest.d.ts +1 -0
  100. package/dist/router/manifest.d.ts.map +1 -1
  101. package/dist/routing/generate.d.ts +2 -0
  102. package/dist/routing/generate.d.ts.map +1 -1
  103. package/dist/routing/index.d.ts +1 -1
  104. package/dist/routing/index.d.ts.map +1 -1
  105. package/dist/routing/types.d.ts +2 -0
  106. package/dist/routing/types.d.ts.map +1 -1
  107. package/dist/security/cors.d.ts.map +1 -1
  108. package/dist/security/index.d.ts +4 -2
  109. package/dist/security/index.d.ts.map +1 -1
  110. package/dist/security/policy.d.ts +5 -1
  111. package/dist/security/policy.d.ts.map +1 -1
  112. package/dist/security/types.d.ts +34 -13
  113. package/dist/security/types.d.ts.map +1 -1
  114. package/dist/security/verification.d.ts +8 -0
  115. package/dist/security/verification.d.ts.map +1 -0
  116. package/dist/server/document-shell.d.ts +12 -0
  117. package/dist/server/document-shell.d.ts.map +1 -0
  118. package/dist/server/errors.d.ts.map +1 -1
  119. package/dist/server/not-found.d.ts.map +1 -1
  120. package/dist/server/render-tree.d.ts +6 -1
  121. package/dist/server/render-tree.d.ts.map +1 -1
  122. package/dist/server/request-handler.d.ts +4 -1
  123. package/dist/server/request-handler.d.ts.map +1 -1
  124. package/dist/server/ssr.d.ts +1 -0
  125. package/dist/server/ssr.d.ts.map +1 -1
  126. package/dist/static/images.d.ts +13 -0
  127. package/dist/static/images.d.ts.map +1 -0
  128. package/dist/static/index.d.ts +18 -0
  129. package/dist/static/index.d.ts.map +1 -1
  130. package/dist/static/index.js +677 -129
  131. package/dist/static/index.js.map +1 -1
  132. package/dist/static/preview.d.ts +8 -0
  133. package/dist/static/preview.d.ts.map +1 -0
  134. package/dist/static/vercel.d.ts +34 -0
  135. package/dist/static/vercel.d.ts.map +1 -0
  136. package/dist/vite/index.d.ts +3 -2
  137. package/dist/vite/index.d.ts.map +1 -1
  138. package/dist/vite/index.js +782 -329
  139. package/dist/vite/index.js.map +1 -1
  140. package/dist/vite/plugin.d.ts +20 -0
  141. package/dist/vite/plugin.d.ts.map +1 -1
  142. package/dist/vite/policy-verification.d.ts +10 -0
  143. package/dist/vite/policy-verification.d.ts.map +1 -0
  144. package/package.json +38 -2
  145. package/dist/chunks/index-BnX9Qxbm.js +0 -44
  146. package/dist/chunks/index-BnX9Qxbm.js.map +0 -1
  147. package/dist/chunks/ssr-BJejKlkZ.js +0 -275
  148. package/dist/chunks/ssr-BJejKlkZ.js.map +0 -1
  149. package/dist/chunks/streaming-DN1DnbRE.js +0 -343
  150. package/dist/chunks/streaming-DN1DnbRE.js.map +0 -1
  151. package/dist/chunks/testing-DR3eQAvY.js.map +0 -1
@@ -1,54 +1,79 @@
1
- import { existsSync as U } from "node:fs";
2
- import { mkdir as tt, writeFile as et, readdir as B, readFile as rt } from "node:fs/promises";
3
- import { dirname as G, resolve as p, relative as g, sep as $, join as nt } from "node:path";
4
- import ot from "magic-string";
5
- import { parseAst as Y } from "vite";
6
- import { b as st, d as it } from "../chunks/ssr-BJejKlkZ.js";
7
- import { e as x, r as at, f as ct, h as ut, c as lt } from "../chunks/testing-DR3eQAvY.js";
1
+ import { existsSync as G } from "node:fs";
2
+ import { mkdir as Et, writeFile as Dt, readdir as ht, readFile as gt } from "node:fs/promises";
3
+ import { dirname as q, resolve as w, relative as R, sep as S, join as Nt } from "node:path";
4
+ import Pt from "magic-string";
5
+ import { transformWithEsbuild as Ot, parseAst as Y } from "vite";
6
+ import { e as Mt, f as It } from "../chunks/request-metadata-D5NrT8ks.js";
7
+ import { v as _t, f as Ct, g as jt, s as F, i as B, r as Ft, j as Tt, h as At, k as Vt } from "../chunks/testing-B_iMFYz_.js";
8
8
  import "react";
9
9
  import "react-dom/server";
10
10
  import "react/jsx-runtime";
11
- import { t as I, U as O, w as _, b as F, c as ft } from "../chunks/streaming-DN1DnbRE.js";
12
- const h = "@demiurgejs/core";
13
- async function dt(t) {
14
- const n = (await pt(t.routesDir)).map((r) => mt(t.routesDir, r)).filter((r) => !!r).sort((r, s) => r.pattern.localeCompare(s.pattern)), o = yt(n);
15
- return await tt(G(t.outputFile), { recursive: !0 }), await et(t.outputFile, o), {
16
- routes: n,
17
- source: o
11
+ import { c as Lt, t as X, U as Z, w as tt, b as T, d as kt, e as Wt } from "../chunks/streaming-BQ1bykDo.js";
12
+ import { p as Ht } from "../chunks/images-B9MqcjSA.js";
13
+ const $ = "@demiurgejs/core";
14
+ async function Jt(t) {
15
+ const e = await Bt(t.routesDir), n = e.map((i) => zt(t, i)).filter((i) => !!i), o = e.map((i) => Ut(t.routesDir, i, n)).filter((i) => !!i).sort((i, s) => i.pattern.localeCompare(s.pattern)), r = qt(o, n);
16
+ return await Et(q(t.outputFile), { recursive: !0 }), await Dt(t.outputFile, r), {
17
+ routes: o,
18
+ source: r
18
19
  };
19
20
  }
20
- async function pt(t) {
21
- return (await B(t, {
21
+ async function Bt(t) {
22
+ return (await ht(t, {
22
23
  recursive: !0,
23
24
  withFileTypes: !0
24
- })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => p(n.parentPath, n.name));
25
+ })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => w(n.parentPath, n.name));
25
26
  }
26
- function mt(t, e) {
27
- const o = g(t, e).split($).join("/").replace(/\.tsx?$/, "").split("/");
28
- if (o.at(-1)?.startsWith("@"))
27
+ function Ut(t, e, n) {
28
+ const r = R(t, e).split(S).join("/").replace(/\.tsx?$/, "").split("/");
29
+ if (r.at(-1)?.startsWith("@"))
29
30
  return null;
30
- const s = o.flatMap((i) => P(i) ? [] : i === "index" ? [] : i.startsWith("[...") && i.endsWith("]") ? [i] : i.startsWith("[") && i.endsWith("]") ? [i] : [i]);
31
+ const s = r.flatMap((c) => et(c) ? [] : c === "index" ? [] : c.startsWith("[...") && c.endsWith("]") ? [c] : c.startsWith("[") && c.endsWith("]") ? [c] : [c]), a = r.flatMap((c) => et(c) ? [] : c.startsWith("[...") && c.endsWith("]") ? [c.slice(4, -1)] : c.startsWith("[") && c.endsWith("]") ? [c.slice(1, -1)] : []);
31
32
  return {
32
- pathVars: o.flatMap((i) => P(i) ? [] : i.startsWith("[...") && i.endsWith("]") ? [i.slice(4, -1)] : i.startsWith("[") && i.endsWith("]") ? [i.slice(1, -1)] : []),
33
+ contextMiddleware: n.filter((c) => Gt(c.fileSegments, r)).map((c) => n.indexOf(c)),
34
+ fileSegments: r,
35
+ pathVars: a,
33
36
  pattern: `/${s.join("/")}`.replace(/\/$/, "") || "/"
34
37
  };
35
38
  }
36
- function P(t) {
39
+ function zt(t, e) {
40
+ const o = R(t.routesDir, e).split(S).join("/").replace(/\.tsx?$/, "").split("/");
41
+ if (o.at(-1) !== "@middleware")
42
+ return null;
43
+ let r = R(q(t.outputFile), e).split(S).join("/").replace(/\.tsx?$/, "");
44
+ return r.startsWith(".") || (r = `./${r}`), { file: e, fileSegments: o.slice(0, -1), importPath: r };
45
+ }
46
+ function Gt(t, e) {
47
+ return t.every(
48
+ (n, o) => e[o] === n
49
+ );
50
+ }
51
+ function et(t) {
37
52
  return t.startsWith("(") && t.endsWith(")");
38
53
  }
39
- function yt(t) {
54
+ function qt(t, e) {
40
55
  return `// Generated by Demiurge. Do not edit by hand.
41
- import "${h}";
42
- import type { PathValue } from "${h}";
56
+ import "${$}";
57
+ import type { MiddlewareContextOf, PathValue } from "${$}";
58
+ ${e.map(Yt).join(`
59
+ `)}
43
60
 
44
- declare module "${h}" {
61
+ ${e.map((n, o) => Kt(o)).join(`
62
+ `)}
63
+
64
+ declare module "${$}" {
45
65
  interface RoutePathVars {
46
- ${t.map(ht).join(`
66
+ ${t.map(Qt).join(`
47
67
  `)}
48
68
  }
49
69
 
50
70
  interface RouteConcretePaths {
51
- ${t.map(gt).join(`
71
+ ${t.map(Xt).join(`
72
+ `)}
73
+ }
74
+
75
+ interface RouteRequestContexts {
76
+ ${t.map(Zt).join(`
52
77
  `)}
53
78
  }
54
79
  }
@@ -56,327 +81,693 @@ ${t.map(gt).join(`
56
81
  export {};
57
82
  `;
58
83
  }
59
- function ht(t) {
84
+ function Yt(t, e) {
85
+ return `type __DemiurgeMiddleware${e} = typeof import(${JSON.stringify(t.importPath)}) extends { middleware?: infer T } ? T : never;`;
86
+ }
87
+ function Kt(t) {
88
+ return `type __DemiurgeMiddlewareContext${t} = MiddlewareContextOf<__DemiurgeMiddleware${t}>;`;
89
+ }
90
+ function Qt(t) {
60
91
  if (t.pathVars.length === 0)
61
92
  return ` ${JSON.stringify(t.pattern)}: {};`;
62
93
  const e = t.pathVars.map((n) => `${n}: PathValue`).join("; ");
63
94
  return ` ${JSON.stringify(t.pattern)}: { ${e} };`;
64
95
  }
65
- function gt(t) {
66
- return ` ${JSON.stringify(t.pattern)}: ${Rt(t.pattern)};`;
96
+ function Xt(t) {
97
+ return ` ${JSON.stringify(t.pattern)}: ${te(t.pattern)};`;
98
+ }
99
+ function Zt(t) {
100
+ const e = t.contextMiddleware.map((o) => `__DemiurgeMiddlewareContext${o}`), n = e.length > 0 ? e.join(" & ") : "Record<never, never>";
101
+ return ` ${JSON.stringify(t.pattern)}: ${n};`;
67
102
  }
68
- function Rt(t) {
69
- return t.includes("[") ? `\`${t.split(/(\[\.\.\.[^\]]+\]|\[[^\]]+\])/g).filter(Boolean).map((n) => n.startsWith("[") ? "${PathValue}" : wt(n)).join("")}\`` : JSON.stringify(t);
103
+ function te(t) {
104
+ return t.includes("[") ? `\`${t.split(/(\[\.\.\.[^\]]+\]|\[[^\]]+\])/g).filter(Boolean).map((n) => n.startsWith("[") ? "${PathValue}" : ee(n)).join("")}\`` : JSON.stringify(t);
70
105
  }
71
- function wt(t) {
106
+ function ee(t) {
72
107
  return t.replace(/[`$\\]/g, "\\$&");
73
108
  }
74
- const E = "virtual:demiurge/client-entry", v = `/@id/${E}`, D = `\0${E}`, q = "virtual:demiurge/server-entry", M = `\0${q}`, C = ".demiurge/route-manifest.d.ts";
75
- function ue(t = {}) {
109
+ const re = [
110
+ "GET",
111
+ "POST",
112
+ "PUT",
113
+ "PATCH",
114
+ "DELETE",
115
+ "OPTIONS",
116
+ "HEAD"
117
+ ], ne = /* @__PURE__ */ new Set([
118
+ "action",
119
+ "html",
120
+ "json",
121
+ "jsonl",
122
+ "notFound",
123
+ "redirect",
124
+ "response",
125
+ "sse",
126
+ "stream",
127
+ "text"
128
+ ]);
129
+ async function oe(t) {
130
+ const e = await gt(t, "utf8");
131
+ return await ie(e, t);
132
+ }
133
+ async function ie(t, e) {
134
+ const n = e.endsWith(".tsx") ? "tsx" : "ts", o = await Ot(t, e, {
135
+ format: "esm",
136
+ loader: n,
137
+ target: "esnext"
138
+ }), r = se(o.code);
139
+ return fe(r, e);
140
+ }
141
+ function se(t) {
142
+ const e = Y(t), n = pe(e), o = me(e), r = ae(e), i = ce(e), s = {}, a = /* @__PURE__ */ new Set();
143
+ let c;
144
+ for (const [u, f] of i) {
145
+ const g = r.get(f);
146
+ if (u === "policy") {
147
+ g && (c = wt(g, n, o));
148
+ continue;
149
+ }
150
+ if (!ye(u) || (a.add(u), !g)) continue;
151
+ const l = ue(g, n, o);
152
+ l && (s[u] = l);
153
+ }
154
+ return { capabilities: s, declaredMethods: a, policy: c };
155
+ }
156
+ function ae(t) {
157
+ const e = /* @__PURE__ */ new Map();
158
+ for (const n of d(t.body)) {
159
+ const o = n.type === "ExportNamedDeclaration" ? h(n.declaration) : n;
160
+ if (o?.type === "VariableDeclaration")
161
+ for (const r of d(o.declarations)) {
162
+ const i = D(r.id), s = h(r.init);
163
+ i && s && e.set(i, s);
164
+ }
165
+ }
166
+ return e;
167
+ }
168
+ function ce(t) {
169
+ const e = /* @__PURE__ */ new Map();
170
+ for (const n of d(t.body)) {
171
+ if (n.type !== "ExportNamedDeclaration") continue;
172
+ const o = h(n.declaration);
173
+ if (o?.type === "VariableDeclaration")
174
+ for (const r of d(o.declarations)) {
175
+ const i = D(r.id);
176
+ i && e.set(i, i);
177
+ }
178
+ for (const r of d(n.specifiers)) {
179
+ const i = E(r.exported), s = E(r.local);
180
+ i && s && e.set(i, s);
181
+ }
182
+ }
183
+ return e;
184
+ }
185
+ function ue(t, e, n) {
186
+ if (t.type !== "CallExpression") return;
187
+ const o = D(t.callee), r = o ? e.get(o) : void 0;
188
+ if (!r || !ne.has(r)) return;
189
+ const i = d(t.arguments), s = r === "action" ? i[0] : i[1];
190
+ if (!s) return {};
191
+ const a = rt(s, "cors", n), c = rt(
192
+ s,
193
+ "security",
194
+ n
195
+ );
196
+ return {
197
+ cors: P(a) ? a : void 0,
198
+ security: P(c) ? c : void 0
199
+ };
200
+ }
201
+ function rt(t, e, n) {
202
+ if (t.type === "Identifier") {
203
+ const r = n.get(String(t.name));
204
+ return P(r) ? r[e] : m;
205
+ }
206
+ if (t.type !== "ObjectExpression") return m;
207
+ let o;
208
+ for (const r of d(t.properties)) {
209
+ if (r.type !== "Property" || r.computed) return m;
210
+ E(r.key) === e && (o = b(h(r.value), n));
211
+ }
212
+ return o;
213
+ }
214
+ function wt(t, e, n) {
215
+ if (t.type === "CallExpression") {
216
+ const r = D(t.callee);
217
+ if (r && e.get(r) === "defineRoutePolicy") {
218
+ const i = d(t.arguments)[0];
219
+ return i ? wt(i, e, n) : void 0;
220
+ }
221
+ }
222
+ if (t.type !== "ObjectExpression") {
223
+ const r = b(t, n);
224
+ return P(r) ? r : void 0;
225
+ }
226
+ const o = {};
227
+ for (const r of d(t.properties)) {
228
+ if (r.type !== "Property" || r.computed) return;
229
+ const i = E(r.key), s = h(r.value);
230
+ if (!i || !s) return;
231
+ if (i === "document") {
232
+ const c = le(s, e, n);
233
+ if (!c) return;
234
+ o.document = c;
235
+ continue;
236
+ }
237
+ const a = b(s, n);
238
+ if (a === m) return;
239
+ o[i] = a;
240
+ }
241
+ return o;
242
+ }
243
+ function le(t, e, n) {
244
+ if (t.type !== "CallExpression") {
245
+ const a = b(t, n);
246
+ return P(a) ? a : void 0;
247
+ }
248
+ const o = h(t.callee);
249
+ if (o?.type !== "MemberExpression") return;
250
+ const r = D(o.object), i = E(o.property);
251
+ if (!r || e.get(r) !== "security" || !i)
252
+ return;
253
+ const s = b(d(t.arguments)[0], n);
254
+ if (!(s !== m && s !== void 0 && !P(s))) {
255
+ if (i === "api") return F.api(s);
256
+ if (i === "crossOriginIsolated")
257
+ return F.crossOriginIsolated(s);
258
+ if (i === "static") return F.static(s);
259
+ if (i === "strict") return F.strict(s);
260
+ }
261
+ }
262
+ function fe(t, e) {
263
+ const n = [], o = new Set(t.declaredMethods);
264
+ o.has("GET") && o.add("HEAD"), o.add("OPTIONS");
265
+ for (const [r, i] of Object.entries(
266
+ t.capabilities
267
+ )) {
268
+ if (i.cors) {
269
+ try {
270
+ _t(i.cors);
271
+ } catch (s) {
272
+ n.push(V(
273
+ "cors-invalid",
274
+ e,
275
+ U(s, "The CORS policy is invalid."),
276
+ r
277
+ ));
278
+ }
279
+ for (const s of i.cors.methods ?? [])
280
+ o.has(s) || n.push(V(
281
+ "cors-method-unavailable",
282
+ e,
283
+ `CORS method ${s} is not available from this route.`,
284
+ r
285
+ ));
286
+ }
287
+ nt(i.security, e, r, n);
288
+ }
289
+ if (nt(t.policy?.security, e, void 0, n), t.policy?.document)
290
+ try {
291
+ Ct(de(t.policy.document), {
292
+ nonce: "build-verification-nonce"
293
+ });
294
+ } catch (r) {
295
+ n.push(V(
296
+ "security-header-render-failed",
297
+ e,
298
+ U(r, "The security headers are invalid.")
299
+ ));
300
+ }
301
+ return n;
302
+ }
303
+ function de(t) {
304
+ const e = t.headers?.reportingEndpoints, n = !!(e && Object.keys(e).length), o = t.csp === !1 ? void 0 : t.csp?.reportTo;
305
+ return n || !o ? t : {
306
+ ...t,
307
+ csp: { ...t.csp, reportTo: void 0 },
308
+ headers: e ? { ...t.headers, reportingEndpoints: void 0 } : t.headers
309
+ };
310
+ }
311
+ function nt(t, e, n, o) {
312
+ if (t?.rateLimit)
313
+ try {
314
+ jt(t.rateLimit);
315
+ } catch (r) {
316
+ o.push(V(
317
+ "rate-limit-invalid",
318
+ e,
319
+ U(r, "The rate limit policy is invalid."),
320
+ n
321
+ ));
322
+ }
323
+ }
324
+ function V(t, e, n, o) {
325
+ return { code: t, exportName: o, file: e, message: n, severity: "error" };
326
+ }
327
+ function pe(t) {
328
+ const e = /* @__PURE__ */ new Map();
329
+ for (const n of d(t.body))
330
+ if (!(n.type !== "ImportDeclaration" || h(n.source)?.value !== "@demiurgejs/core"))
331
+ for (const o of d(n.specifiers)) {
332
+ if (o.type !== "ImportSpecifier") continue;
333
+ const r = D(o.local), i = E(o.imported);
334
+ r && i && e.set(r, i);
335
+ }
336
+ return e;
337
+ }
338
+ function me(t) {
339
+ const e = /* @__PURE__ */ new Map();
340
+ for (const n of d(t.body)) {
341
+ const o = n.type === "ExportNamedDeclaration" ? h(n.declaration) : n;
342
+ if (o?.type === "VariableDeclaration")
343
+ for (const r of d(o.declarations)) {
344
+ const i = D(r.id), s = h(r.init);
345
+ if (!i || !s) continue;
346
+ const a = b(s, e);
347
+ a !== m && e.set(i, a);
348
+ }
349
+ }
350
+ return e;
351
+ }
352
+ const m = Symbol("unresolved");
353
+ function b(t, e) {
354
+ if (t) {
355
+ if (t.type === "Literal") return t.value;
356
+ if (t.type === "Identifier")
357
+ return t.name === "undefined" ? void 0 : e.has(String(t.name)) ? e.get(String(t.name)) : m;
358
+ if (t.type === "TemplateLiteral")
359
+ return d(t.expressions).length ? m : d(t.quasis).map((n) => h(n.value)?.cooked ?? "").join("");
360
+ if (t.type === "UnaryExpression") {
361
+ const n = b(h(t.argument), e);
362
+ return typeof n != "number" ? m : t.operator === "-" ? -n : t.operator === "+" ? n : m;
363
+ }
364
+ if (t.type === "ArrayExpression") {
365
+ const n = [];
366
+ for (const o of d(t.elements)) {
367
+ const r = b(o, e);
368
+ if (r === m) return m;
369
+ n.push(r);
370
+ }
371
+ return n;
372
+ }
373
+ if (t.type === "ObjectExpression") {
374
+ const n = {};
375
+ for (const o of d(t.properties)) {
376
+ if (o.type !== "Property" || o.computed) return m;
377
+ const r = E(o.key), i = b(h(o.value), e);
378
+ if (!r || i === m) return m;
379
+ n[r] = i;
380
+ }
381
+ return n;
382
+ }
383
+ return m;
384
+ }
385
+ }
386
+ function ye(t) {
387
+ return re.includes(t);
388
+ }
389
+ function U(t, e) {
390
+ return t instanceof Error ? t.message : e;
391
+ }
392
+ function P(t) {
393
+ return !!t && typeof t == "object" && !Array.isArray(t);
394
+ }
395
+ function h(t) {
396
+ return t && typeof t == "object" && "type" in t ? t : void 0;
397
+ }
398
+ function d(t) {
399
+ return Array.isArray(t) ? t.map(h).filter((e) => !!e) : [];
400
+ }
401
+ function D(t) {
402
+ const e = h(t);
403
+ return e?.type === "Identifier" ? String(e.name) : void 0;
404
+ }
405
+ function E(t) {
406
+ const e = h(t);
407
+ return e?.type === "Identifier" || e?.type === "Literal" ? String(e.name ?? e.value) : void 0;
408
+ }
409
+ const W = "virtual:demiurge/client-entry", z = `/@id/${W}`, J = `\0${W}`, vt = "virtual:demiurge/server-entry", ot = `\0${vt}`, it = ".demiurge/route-manifest.d.ts";
410
+ function fr(t = {}) {
76
411
  let e = process.cwd(), n = !1;
77
- const o = `demiurge-${x()}`;
412
+ const o = `demiurge-${B()}`;
78
413
  return {
414
+ api: {
415
+ demiurge: !0,
416
+ fonts: t.fonts,
417
+ images: t.images,
418
+ staticDeployment: t.static?.deployment,
419
+ staticFileHeaders: t.static?.headers
420
+ },
79
421
  enforce: "post",
80
422
  name: "demiurge",
81
- config(r, s) {
82
- return It(r, s, o);
423
+ config(r, i) {
424
+ return Se(r, i, o);
83
425
  },
84
426
  configResolved(r) {
85
427
  e = r.root, n = r.command === "build";
86
428
  },
87
429
  resolveId(r) {
88
- return r === E ? D : r === q ? M : null;
430
+ return r === W ? J : r === vt ? ot : null;
89
431
  },
90
432
  load(r) {
91
- return r === D ? Ot(e, t) : r === M ? _t(e, t) : null;
433
+ return r === J ? Ee(e, t) : r === ot ? De(e, t) : null;
92
434
  },
93
435
  transform: {
94
436
  order: "post",
95
- handler(r, s, a) {
96
- if (!Et(e, t, s))
437
+ handler(r, i, s) {
438
+ if (!ve(e, t, i))
97
439
  return null;
98
- if (a?.ssr)
99
- return n ? null : $t(r, s);
100
- const i = bt(r);
101
- return i === r ? null : { code: i, map: null };
440
+ if (s?.ssr)
441
+ return n ? null : he(r, i);
442
+ const a = ge(r);
443
+ return a === r ? null : { code: a, map: null };
102
444
  }
103
445
  },
104
- generateBundle(r, s) {
105
- const a = Object.values(s).find(
106
- (c) => c.type === "chunk" && c.isEntry && c.facadeModuleId === D
446
+ generateBundle(r, i) {
447
+ const s = Object.values(i).find(
448
+ (u) => u.type === "chunk" && u.isEntry && u.facadeModuleId === J
107
449
  );
108
- if (!a)
450
+ if (!s)
109
451
  return;
110
- const i = Object.values(s).filter(
111
- (c) => c.type === "asset" && c.fileName.endsWith(".css")
112
- ).map((c) => `/${c.fileName}`), u = `/${a.fileName}`;
452
+ const a = Object.values(i).filter(
453
+ (u) => u.type === "asset" && u.fileName.endsWith(".css")
454
+ ).map((u) => `/${u.fileName}`), c = `/${s.fileName}`;
113
455
  this.emitFile({
114
456
  fileName: "index.html",
115
- source: qt({
116
- entrySrc: u,
457
+ source: Ue({
458
+ entrySrc: c,
117
459
  lang: t.document?.lang,
118
- styles: i,
460
+ styles: a,
119
461
  title: t.document?.title
120
462
  }),
121
463
  type: "asset"
122
464
  }), this.emitFile({
123
465
  fileName: "demiurge-manifest.json",
124
- source: JSON.stringify({ clientEntry: u, styles: i }, null, 2),
466
+ source: JSON.stringify({ clientEntry: c, styles: a }, null, 2),
125
467
  type: "asset"
126
468
  });
127
469
  },
128
470
  async buildStart() {
129
- n && await Ht(e, t), t.typedRoutes && await w(e, t);
471
+ if (n) {
472
+ await Le(e, t);
473
+ const r = await yt(e, t);
474
+ if (r.length)
475
+ throw new Error(Qe(r));
476
+ }
477
+ t.typedRoutes && await _(e, t);
130
478
  },
131
479
  configureServer(r) {
132
- if (t.typedRoutes) {
133
- const s = p(
480
+ const i = async () => {
481
+ const l = await yt(r.config.root, t);
482
+ for (const p of l)
483
+ r.config.logger.warn(St(p));
484
+ };
485
+ let s, a = Promise.resolve();
486
+ const c = () => {
487
+ a = a.then(i).catch(() => {
488
+ });
489
+ }, u = () => {
490
+ s && clearTimeout(s), s = setTimeout(() => {
491
+ s = void 0, c();
492
+ }, Ye), s.unref?.();
493
+ };
494
+ if (r.httpServer?.once("close", () => {
495
+ s && (clearTimeout(s), s = void 0);
496
+ }), c(), t.typedRoutes) {
497
+ const l = w(
134
498
  r.config.root,
135
499
  t.routesDir ?? "src/routes"
136
500
  );
137
- w(r.config.root, t), r.watcher.add(s), r.watcher.on("add", (a) => {
138
- N(s, a) && w(r.config.root, t);
139
- }), r.watcher.on("change", (a) => {
140
- N(s, a) && w(r.config.root, t);
141
- }), r.watcher.on("unlink", (a) => {
142
- N(s, a) && w(r.config.root, t);
501
+ _(r.config.root, t), r.watcher.add(l), r.watcher.on("add", (p) => {
502
+ A(l, p) && (_(r.config.root, t), u());
503
+ }), r.watcher.on("change", (p) => {
504
+ A(l, p) && (_(r.config.root, t), u());
505
+ }), r.watcher.on("unlink", (p) => {
506
+ A(l, p) && (_(r.config.root, t), u());
143
507
  });
508
+ } else {
509
+ const l = w(
510
+ r.config.root,
511
+ t.routesDir ?? "src/routes"
512
+ );
513
+ r.watcher.add(l);
514
+ for (const p of ["add", "change", "unlink"])
515
+ r.watcher.on(p, (N) => {
516
+ A(l, N) && u();
517
+ });
144
518
  }
145
- return r.middlewares.use(async (s, a, i) => {
519
+ const f = Ie(r, t), g = Lt({
520
+ fonts: t.fonts,
521
+ root: r.config.root
522
+ });
523
+ return r.middlewares.use(async (l, p, N) => {
146
524
  try {
147
- let u;
525
+ let v;
148
526
  try {
149
- u = I(s, {
527
+ v = X(l, {
150
528
  // Vite performs its own configured host allowlist check before
151
529
  // Demiurge's middleware runs. Production Node listeners require
152
530
  // an explicit application allowlist instead.
153
- allowedHosts: [s.headers.host ?? "localhost"]
531
+ allowedHosts: [l.headers.host ?? "localhost"]
154
532
  });
155
- } catch (R) {
156
- if (R instanceof O) {
157
- _(a);
533
+ } catch (Q) {
534
+ if (Q instanceof Z) {
535
+ tt(p);
158
536
  return;
159
537
  }
160
- throw R;
538
+ throw Q;
539
+ }
540
+ const O = await g(v);
541
+ if (O) {
542
+ await T(p, O);
543
+ return;
544
+ }
545
+ const M = await f(v);
546
+ if (M) {
547
+ await T(p, M);
548
+ return;
161
549
  }
162
- const c = p(
550
+ const C = w(
163
551
  r.config.root,
164
552
  t.routesDir ?? "src/routes"
165
- ), l = await k(r, s, c), d = j(
553
+ ), j = await dt(r, l, C), I = at(
166
554
  r,
167
555
  o
168
- ), y = await zt(
169
- l,
170
- u,
171
- Tt(t, d.transform)
556
+ ), H = await Ge(
557
+ j,
558
+ v,
559
+ _e(t, I.transform)
172
560
  );
173
- if (y === "next") {
174
- i();
561
+ if (H === "next") {
562
+ N();
175
563
  return;
176
564
  }
177
- W(y, d.nonce), await F(a, y);
178
- } catch (u) {
179
- i(u);
565
+ ut(H, I.nonce), await T(p, H);
566
+ } catch (v) {
567
+ N(v);
180
568
  }
181
569
  }), () => {
182
- r.middlewares.use(async (s, a, i) => {
570
+ r.middlewares.use(async (l, p, N) => {
183
571
  try {
184
- let u;
572
+ let v;
185
573
  try {
186
- u = I(s, {
187
- allowedHosts: [s.headers.host ?? "localhost"]
574
+ v = X(l, {
575
+ allowedHosts: [l.headers.host ?? "localhost"]
188
576
  });
189
- } catch (R) {
190
- if (R instanceof O) {
191
- _(a);
577
+ } catch (I) {
578
+ if (I instanceof Z) {
579
+ tt(p);
192
580
  return;
193
581
  }
194
- throw R;
582
+ throw I;
195
583
  }
196
- const c = p(
584
+ const O = w(
197
585
  r.config.root,
198
586
  t.routesDir ?? "src/routes"
199
- ), l = await k(r, s, c);
200
- kt(l, c);
201
- const d = j(
587
+ ), M = await dt(r, l, O);
588
+ Ve(M, O);
589
+ const C = at(
202
590
  r,
203
591
  o
204
- ), y = await at(
205
- l,
206
- u,
207
- Lt(t, d.transform)
592
+ ), j = await Ft(
593
+ M,
594
+ v,
595
+ Ae(t, C.transform)
208
596
  );
209
- W(
210
- y,
211
- d.nonce
212
- ), await F(a, y);
213
- } catch (u) {
214
- i(u);
597
+ ut(
598
+ j,
599
+ C.nonce
600
+ ), await T(p, j);
601
+ } catch (v) {
602
+ N(v);
215
603
  }
216
604
  });
217
605
  };
218
606
  }
219
607
  };
220
608
  }
221
- function $t(t, e) {
609
+ function he(t, e) {
222
610
  if (!t.includes("import.meta"))
223
611
  return null;
224
- const n = Y(t), o = m(n.body).find(
225
- (c) => c.type === "ImportDeclaration" && m(c.specifiers).some(
226
- (l) => f(l.local)?.type === "Identifier" && f(l.local)?.name === "meta"
612
+ const n = Y(t), o = x(n.body).find(
613
+ (u) => u.type === "ImportDeclaration" && x(u.specifiers).some(
614
+ (f) => y(f.local)?.type === "Identifier" && y(f.local)?.name === "meta"
227
615
  )
228
- ), r = o && m(o.specifiers).find(
229
- (c) => f(c.local)?.type === "Identifier" && f(c.local)?.name === "meta"
230
- ), s = r && f(r.local);
231
- if (!o || !r || !s)
616
+ ), r = o && x(o.specifiers).find(
617
+ (u) => y(u.local)?.type === "Identifier" && y(u.local)?.name === "meta"
618
+ ), i = r && y(r.local);
619
+ if (!o || !r || !i)
232
620
  return null;
233
- let a = "__demiurge_imported_meta__";
234
- for (; new RegExp(`\\b${a}\\b`).test(t); )
235
- a += "_";
236
- const i = new ot(t), u = f(r.imported);
237
- return i.overwrite(
238
- s.start,
239
- s.end,
240
- u?.start === s.start ? `meta as ${a}` : a
241
- ), i.appendLeft(o.end, `
242
- const meta = ${a};`), {
243
- code: i.toString(),
244
- map: i.generateMap({
621
+ let s = "__demiurge_imported_meta__";
622
+ for (; new RegExp(`\\b${s}\\b`).test(t); )
623
+ s += "_";
624
+ const a = new Pt(t), c = y(r.imported);
625
+ return a.overwrite(
626
+ i.start,
627
+ i.end,
628
+ c?.start === i.start ? `meta as ${s}` : s
629
+ ), a.appendLeft(o.end, `
630
+ const meta = ${s};`), {
631
+ code: a.toString(),
632
+ map: a.generateMap({
245
633
  hires: "boundary",
246
634
  includeContent: !0,
247
635
  source: e
248
636
  })
249
637
  };
250
638
  }
251
- function bt(t) {
252
- const e = Y(t), n = [], o = St(e), r = [];
253
- b(e, (i) => {
254
- if (i.type === "ImportDeclaration" && Nt(i)) {
255
- r.push(i);
639
+ function ge(t) {
640
+ const e = Y(t), n = [], o = we(e), r = [];
641
+ L(e, (a) => {
642
+ if (a.type === "ImportDeclaration" && xe(a)) {
643
+ r.push(a);
256
644
  return;
257
645
  }
258
- if (i.type !== "CallExpression" || !Dt(i.callee))
646
+ if (a.type !== "CallExpression" || !be(a.callee))
259
647
  return;
260
- const u = m(i.arguments)[0];
261
- if (u?.type === "ObjectExpression")
262
- for (const c of m(u.properties))
263
- c.type === "Property" && S(c.key) === "data" && n.push({ end: c.end, start: c.start });
648
+ const c = x(a.arguments)[0];
649
+ if (c?.type === "ObjectExpression")
650
+ for (const u of x(c.properties))
651
+ u.type === "Property" && k(u.key) === "data" && n.push({ end: u.end, start: u.start });
264
652
  });
265
- const s = [...n, ...o];
266
- if (s.length === 0)
653
+ const i = [...n, ...o];
654
+ if (i.length === 0)
267
655
  return t;
268
- const a = [...s];
269
- for (const i of r) {
270
- const u = m(i.specifiers).map((d) => f(d.local)).filter((d) => d?.type === "Identifier").map((d) => String(d.name)), c = xt(t, [
271
- ...s,
272
- { end: i.end, start: i.start }
273
- ]), l = u.find(
274
- (d) => new RegExp(`\\b${K(d)}\\b`).test(c)
656
+ const s = [...i];
657
+ for (const a of r) {
658
+ const c = x(a.specifiers).map((g) => y(g.local)).filter((g) => g?.type === "Identifier").map((g) => String(g.name)), u = Re(t, [
659
+ ...i,
660
+ { end: a.end, start: a.start }
661
+ ]), f = c.find(
662
+ (g) => new RegExp(`\\b${bt(g)}\\b`).test(u)
275
663
  );
276
- if (l)
664
+ if (f)
277
665
  throw new Error(
278
- `Server-only import ${JSON.stringify(l)} is used by client route code. Move that use into page data, a document contribution, or a client-safe module.`
666
+ `Server-only import ${JSON.stringify(f)} is used by client route code. Move that use into page data, a document contribution, or a client-safe module.`
279
667
  );
280
- a.push({ end: i.end, start: i.start });
668
+ s.push({ end: a.end, start: a.start });
281
669
  }
282
- return vt(
670
+ return $e(
283
671
  t,
284
- a,
285
- (i) => n.includes(i) ? "data: undefined" : o.includes(i) ? "undefined" : ""
672
+ s,
673
+ (a) => n.includes(a) ? "data: undefined" : o.includes(a) ? "undefined" : ""
286
674
  );
287
675
  }
288
- const T = /* @__PURE__ */ new Set(["links", "metadata", "scripts"]);
289
- function St(t) {
676
+ const st = /* @__PURE__ */ new Set(["links", "metadata", "scripts"]);
677
+ function we(t) {
290
678
  const e = /* @__PURE__ */ new Set();
291
- for (const o of m(t.body)) {
679
+ for (const o of x(t.body)) {
292
680
  if (o.type !== "ExportNamedDeclaration") continue;
293
- const r = f(o.declaration);
681
+ const r = y(o.declaration);
294
682
  if (r?.type === "VariableDeclaration")
295
- for (const s of m(r.declarations)) {
296
- const a = f(s.id);
297
- a?.type === "Identifier" && T.has(String(a.name)) && e.add(String(a.name));
683
+ for (const i of x(r.declarations)) {
684
+ const s = y(i.id);
685
+ s?.type === "Identifier" && st.has(String(s.name)) && e.add(String(s.name));
298
686
  }
299
- for (const s of m(o.specifiers)) {
300
- const a = S(s.exported), i = S(s.local);
301
- a && i && T.has(a) && e.add(i);
687
+ for (const i of x(o.specifiers)) {
688
+ const s = k(i.exported), a = k(i.local);
689
+ s && a && st.has(s) && e.add(a);
302
690
  }
303
691
  }
304
692
  const n = [];
305
- return b(t, (o) => {
693
+ return L(t, (o) => {
306
694
  if (o.type !== "VariableDeclarator") return;
307
- const r = f(o.id), s = f(o.init);
308
- r?.type === "Identifier" && e.has(String(r.name)) && s && n.push({ end: s.end, start: s.start });
695
+ const r = y(o.id), i = y(o.init);
696
+ r?.type === "Identifier" && e.has(String(r.name)) && i && n.push({ end: i.end, start: i.start });
309
697
  }), n;
310
698
  }
311
- function Et(t, e, n) {
312
- const o = n.split("?", 1)[0] ?? n, r = p(t, e.routesDir ?? "src/routes"), s = g(r, o);
313
- return s !== "" && s !== ".." && !s.startsWith(`..${$}`);
699
+ function ve(t, e, n) {
700
+ const o = n.split("?", 1)[0] ?? n, r = w(t, e.routesDir ?? "src/routes"), i = R(r, o);
701
+ return i !== "" && i !== ".." && !i.startsWith(`..${S}`);
314
702
  }
315
- function b(t, e) {
703
+ function L(t, e) {
316
704
  if (!t || typeof t != "object") return;
317
705
  if (Array.isArray(t)) {
318
- for (const o of t) b(o, e);
706
+ for (const o of t) L(o, e);
319
707
  return;
320
708
  }
321
709
  const n = t;
322
710
  if (typeof n.type == "string") {
323
711
  e(n);
324
712
  for (const [o, r] of Object.entries(n))
325
- (/* @__PURE__ */ new Set(["end", "loc", "start", "type"])).has(o) || b(r, e);
713
+ (/* @__PURE__ */ new Set(["end", "loc", "start", "type"])).has(o) || L(r, e);
326
714
  }
327
715
  }
328
- function f(t) {
716
+ function y(t) {
329
717
  return t && typeof t == "object" && "type" in t ? t : void 0;
330
718
  }
331
- function m(t) {
332
- return Array.isArray(t) ? t.map(f).filter((e) => !!e) : [];
719
+ function x(t) {
720
+ return Array.isArray(t) ? t.map(y).filter((e) => !!e) : [];
333
721
  }
334
- function Dt(t) {
335
- const e = f(t);
336
- return e?.type === "Identifier" ? e.name === "page" : e?.type === "MemberExpression" && S(e.property) === "page";
722
+ function be(t) {
723
+ const e = y(t);
724
+ return e?.type === "Identifier" ? e.name === "page" : e?.type === "MemberExpression" && k(e.property) === "page";
337
725
  }
338
- function S(t) {
339
- const e = f(t);
726
+ function k(t) {
727
+ const e = y(t);
340
728
  return e?.type === "Identifier" || e?.type === "Literal" ? String(e.name ?? e.value) : void 0;
341
729
  }
342
- function Nt(t) {
343
- const e = f(t.source)?.value;
730
+ function xe(t) {
731
+ const e = y(t.source)?.value;
344
732
  return typeof e == "string" && /(?:^|[/.-])server(?:[/.-]|$)/.test(e);
345
733
  }
346
- function xt(t, e) {
734
+ function Re(t, e) {
347
735
  const n = [...t];
348
736
  for (const { end: o, start: r } of e)
349
737
  n.fill(" ", r, o);
350
738
  return n.join("");
351
739
  }
352
- function vt(t, e, n) {
740
+ function $e(t, e, n) {
353
741
  return [...e].sort((o, r) => r.start - o.start).reduce(
354
742
  (o, r) => `${o.slice(0, r.start)}${n(r)}${o.slice(r.end)}`,
355
743
  t
356
744
  );
357
745
  }
358
- function K(t) {
746
+ function bt(t) {
359
747
  return t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
360
748
  }
361
- function It(t, e, n) {
749
+ function Se(t, e, n) {
362
750
  return {
363
751
  appType: "custom",
364
752
  build: {
365
753
  assetsInlineLimit: t.build?.assetsInlineLimit ?? 0,
366
754
  ...e.isSsrBuild && t.build?.target === void 0 && t.ssr?.target !== "webworker" ? { target: "node22.13" } : {},
367
755
  rollupOptions: {
368
- input: t.build?.rollupOptions?.input ?? E
756
+ input: t.build?.rollupOptions?.input ?? W
369
757
  }
370
758
  },
371
- ...e.command === "serve" ? { html: { cspNonce: n } } : {}
759
+ ...e.command === "serve" ? {
760
+ html: { cspNonce: n },
761
+ ssr: { external: [$] }
762
+ } : {}
372
763
  };
373
764
  }
374
- function Ot(t, e = {}) {
375
- const n = e.routesDir ?? "src/routes", o = Ct(t, e);
376
- return `import { hydrateFileRouter } from "${h}";
765
+ function Ee(t, e = {}) {
766
+ const n = e.routesDir ?? "src/routes", o = Me(t, e);
767
+ return `import { hydrateFileRouter } from "${$}";
377
768
  ${o}
378
769
 
379
- ${z(n, {
770
+ ${xt(n, {
380
771
  exportRoutes: !1,
381
772
  includeServerOnly: !1
382
773
  })}
@@ -384,13 +775,14 @@ ${z(n, {
384
775
  void hydrateFileRouter({ routes });
385
776
  `;
386
777
  }
387
- function _t(t, e = {}) {
778
+ function De(t, e = {}) {
388
779
  const n = e.routesDir ?? "src/routes";
389
- return `import { createRequestHandler } from "${h}";
780
+ return `import { createRequestHandler } from "${$}";
390
781
 
391
- ${z(n, {
782
+ ${xt(n, {
392
783
  exportRoutes: !0,
393
- includeServerOnly: !0
784
+ includeServerOnly: !0,
785
+ eagerModules: !0
394
786
  })}
395
787
 
396
788
  export function createHandler(options = {}) {
@@ -399,6 +791,7 @@ export function createHandler(options = {}) {
399
791
  return createRequestHandler({
400
792
  ...handlerOptions,
401
793
  routes,
794
+ routeModules,
402
795
  ssr: {
403
796
  clientEntry,
404
797
  lang: lang ?? ${JSON.stringify(e.document?.lang)},
@@ -409,13 +802,27 @@ export function createHandler(options = {}) {
409
802
  }
410
803
  `;
411
804
  }
412
- function z(t, {
805
+ function xt(t, {
413
806
  exportRoutes: e,
414
- includeServerOnly: n
807
+ eagerModules: n = !1,
808
+ includeServerOnly: o
415
809
  }) {
416
- const o = Pt(t, n), r = Mt(t);
417
- return `const routeModules = import.meta.glob(${JSON.stringify(o)});
418
- const routePrefix = ${JSON.stringify(r)};
810
+ const r = Pe(t, o), i = Oe(t);
811
+ return n ? `const eagerRouteModules = import.meta.glob(${JSON.stringify(r)}, { eager: true });
812
+ const routePrefix = ${JSON.stringify(i)};
813
+ export const routeModules = Object.fromEntries(
814
+ Object.entries(eagerRouteModules).map(([file, module]) => [
815
+ \`./routes/\${file.slice(routePrefix.length)}\`,
816
+ module,
817
+ ]),
818
+ );
819
+ ${e ? "export " : ""}const routes = Object.fromEntries(
820
+ Object.entries(routeModules).map(([file, module]) => [
821
+ file,
822
+ async () => module,
823
+ ]),
824
+ );` : `const routeModules = import.meta.glob(${JSON.stringify(r)});
825
+ const routePrefix = ${JSON.stringify(i)};
419
826
  ${e ? "export " : ""}const routes = Object.fromEntries(
420
827
  Object.entries(routeModules).map(([file, load]) => [
421
828
  \`./routes/\${file.slice(routePrefix.length)}\`,
@@ -423,41 +830,49 @@ ${e ? "export " : ""}const routes = Object.fromEntries(
423
830
  ]),
424
831
  );`;
425
832
  }
426
- const Ft = ["@middleware.ts", "@policy.ts"];
427
- function Pt(t, e) {
833
+ const Ne = ["@middleware.ts", "@policy.ts"];
834
+ function Pe(t, e) {
428
835
  const n = `/${t.replace(/^\/|\/$/g, "")}`, o = [`${n}/**/*.{ts,tsx}`];
429
836
  return e || o.push(
430
- ...Ft.flatMap((r) => [
837
+ ...Ne.flatMap((r) => [
431
838
  `!${n}/${r}`,
432
839
  `!${n}/**/${r}`
433
840
  ])
434
841
  ), o;
435
842
  }
436
- function Mt(t) {
843
+ function Oe(t) {
437
844
  return `/${t.replace(/^\/|\/$/g, "")}/`;
438
845
  }
439
- function Ct(t, e) {
846
+ function Me(t, e) {
440
847
  if (e.styles === !1)
441
848
  return "";
442
- const n = e.styles ?? "src/styles.css", o = p(t, n);
443
- return U(o) ? `import ${JSON.stringify(`/${n.replace(/^\/+/, "")}`)};` : "";
849
+ const n = e.styles ?? "src/styles.css", o = w(t, n);
850
+ return G(o) ? `import ${JSON.stringify(`/${n.replace(/^\/+/, "")}`)};` : "";
851
+ }
852
+ function Ie(t, e) {
853
+ const n = t.config.publicDir, o = e.images?.optimizerPath ?? "/_demiurge/image";
854
+ return n ? kt({
855
+ policy: e.images,
856
+ resolveVariant: (r) => Ht(r, o),
857
+ root: n
858
+ }) : async () => null;
444
859
  }
445
- function Tt(t, e) {
860
+ function _e(t, e) {
446
861
  return {
447
862
  dev: !0,
448
- ssr: X(t),
863
+ ssr: $t(t),
449
864
  transformDocument: e,
450
865
  renderPage: async (n, o) => {
451
866
  if (n.render.mode === "streaming")
452
- return await ft(n, {
867
+ return await Wt(n, {
453
868
  ...o,
454
- clientEntry: v,
869
+ clientEntry: z,
455
870
  transformDocument: e
456
871
  });
457
872
  const r = await e(
458
- it(n, {
873
+ It(n, {
459
874
  ...o,
460
- clientEntry: v
875
+ clientEntry: z
461
876
  })
462
877
  );
463
878
  return new Response(r, {
@@ -466,51 +881,51 @@ function Tt(t, e) {
466
881
  }
467
882
  };
468
883
  }
469
- function j(t, e) {
470
- const n = x(), o = x();
884
+ function at(t, e) {
885
+ const n = B(), o = B();
471
886
  return {
472
887
  nonce: n,
473
888
  transform: async (r) => {
474
- const s = jt(r, o), a = await t.transformIndexHtml("/", s);
475
- return A(
476
- A(a, o),
889
+ const i = Ce(r, o), s = await t.transformIndexHtml("/", i);
890
+ return ct(
891
+ ct(s, o),
477
892
  e,
478
893
  n
479
894
  );
480
895
  }
481
896
  };
482
897
  }
483
- function jt(t, e) {
898
+ function Ce(t, e) {
484
899
  const n = t.toLowerCase();
485
900
  let o = 0, r = "";
486
901
  for (; o < t.length; ) {
487
- const s = t.indexOf("<", o);
488
- if (s === -1)
902
+ const i = t.indexOf("<", o);
903
+ if (i === -1)
489
904
  return r + t.slice(o);
490
- if (r += t.slice(o, s), n.startsWith("<!--", s)) {
491
- const c = n.indexOf("-->", s + 4), l = c === -1 ? t.length : c + 3;
492
- r += t.slice(s, l), o = l;
905
+ if (r += t.slice(o, i), n.startsWith("<!--", i)) {
906
+ const u = n.indexOf("-->", i + 4), f = u === -1 ? t.length : u + 3;
907
+ r += t.slice(i, f), o = f;
493
908
  continue;
494
909
  }
495
- const a = n.slice(s).match(/^<\s*(script|style|link)\b/)?.[1];
496
- if (!a) {
497
- r += "<", o = s + 1;
910
+ const s = n.slice(i).match(/^<\s*(script|style|link)\b/)?.[1];
911
+ if (!s) {
912
+ r += "<", o = i + 1;
498
913
  continue;
499
914
  }
500
- const i = At(t, s);
501
- if (i === -1)
502
- return r + t.slice(s);
503
- const u = t.slice(s, i + 1);
504
- if (r += /(?:^|\s)nonce(?:\s|=|>)/i.test(u) ? u : Wt(u, e), o = i + 1, a === "script" || a === "style") {
505
- const c = n.indexOf(`</${a}`, o);
506
- if (c === -1)
915
+ const a = je(t, i);
916
+ if (a === -1)
917
+ return r + t.slice(i);
918
+ const c = t.slice(i, a + 1);
919
+ if (r += /(?:^|\s)nonce(?:\s|=|>)/i.test(c) ? c : Fe(c, e), o = a + 1, s === "script" || s === "style") {
920
+ const u = n.indexOf(`</${s}`, o);
921
+ if (u === -1)
507
922
  return r + t.slice(o);
508
- r += t.slice(o, c), o = c;
923
+ r += t.slice(o, u), o = u;
509
924
  }
510
925
  }
511
926
  return r;
512
927
  }
513
- function At(t, e) {
928
+ function je(t, e) {
514
929
  let n;
515
930
  for (let o = e + 1; o < t.length; o++) {
516
931
  const r = t[o];
@@ -525,13 +940,13 @@ function At(t, e) {
525
940
  }
526
941
  return -1;
527
942
  }
528
- function Wt(t, e) {
943
+ function Fe(t, e) {
529
944
  const n = /\/\s*>$/.test(t) ? t.lastIndexOf("/") : t.length - 1;
530
945
  return `${t.slice(0, n)} nonce="${e}"${t.slice(n)}`;
531
946
  }
532
- function A(t, e, n) {
947
+ function ct(t, e, n) {
533
948
  const o = new RegExp(
534
- `(\\snonce\\s*=\\s*)(["'])${K(e)}\\2`,
949
+ `(\\snonce\\s*=\\s*)(["'])${bt(e)}\\2`,
535
950
  "g"
536
951
  );
537
952
  return t.replace(
@@ -539,47 +954,47 @@ function A(t, e, n) {
539
954
  n ? `$1"${n}"` : ""
540
955
  );
541
956
  }
542
- function W(t, e) {
957
+ function ut(t, e) {
543
958
  const n = t.headers.get("content-type"), o = t.headers.get("content-security-policy");
544
959
  if (!n?.toLowerCase().startsWith("text/html") || !o)
545
960
  return;
546
- const r = `'nonce-${e}'`, s = V(
961
+ const r = `'nonce-${e}'`, i = lt(
547
962
  o,
548
963
  ["script-src-elem", "script-src"],
549
964
  "script-src",
550
965
  r
551
- ), a = Vt(
552
- s,
966
+ ), s = Te(
967
+ i,
553
968
  ["style-src-elem", "style-src"]
554
- ) ? s : V(
555
- s,
969
+ ) ? i : lt(
970
+ i,
556
971
  ["style-src-elem", "style-src"],
557
972
  "style-src",
558
973
  r
559
974
  );
560
- t.headers.set("content-security-policy", a), t.headers.set("cache-control", "private, no-store");
975
+ t.headers.set("content-security-policy", s), t.headers.set("cache-control", "private, no-store");
561
976
  }
562
- function V(t, e, n, o) {
563
- const r = t.split(";").map((l) => l.trim()), s = Q(r, e), a = r.find(
564
- (l) => l.split(/\s+/, 1)[0]?.toLowerCase() === "default-src"
977
+ function lt(t, e, n, o) {
978
+ const r = t.split(";").map((f) => f.trim()), i = Rt(r, e), s = r.find(
979
+ (f) => f.split(/\s+/, 1)[0]?.toLowerCase() === "default-src"
565
980
  );
566
- if (s === -1 && !a) return t;
567
- const [i, ...u] = (s === -1 ? a : r[s]).split(/\s+/), c = [
568
- s === -1 ? n : i,
981
+ if (i === -1 && !s) return t;
982
+ const [a, ...c] = (i === -1 ? s : r[i]).split(/\s+/), u = [
983
+ i === -1 ? n : a,
569
984
  .../* @__PURE__ */ new Set([
570
- ...u.filter((l) => l !== "'none'"),
985
+ ...c.filter((f) => f !== "'none'"),
571
986
  o
572
987
  ])
573
988
  ].join(" ");
574
- return s === -1 ? r.push(c) : r[s] = c, r.join("; ");
989
+ return i === -1 ? r.push(u) : r[i] = u, r.join("; ");
575
990
  }
576
- function Vt(t, e) {
577
- const n = t.split(";").map((a) => a.trim()), o = Q(n, e), s = (o === -1 ? n.find(
578
- (a) => a.split(/\s+/, 1)[0]?.toLowerCase() === "default-src"
991
+ function Te(t, e) {
992
+ const n = t.split(";").map((s) => s.trim()), o = Rt(n, e), i = (o === -1 ? n.find(
993
+ (s) => s.split(/\s+/, 1)[0]?.toLowerCase() === "default-src"
579
994
  ) : n[o])?.split(/\s+/).slice(1) ?? [];
580
- return s.includes("'unsafe-inline'") && !s.some((a) => /^'(?:nonce-|sha(?:256|384|512)-)/i.test(a));
995
+ return i.includes("'unsafe-inline'") && !i.some((s) => /^'(?:nonce-|sha(?:256|384|512)-)/i.test(s));
581
996
  }
582
- function Q(t, e) {
997
+ function Rt(t, e) {
583
998
  for (const n of e) {
584
999
  const o = t.findIndex(
585
1000
  (r) => r.split(/\s+/, 1)[0]?.toLowerCase() === n
@@ -588,76 +1003,76 @@ function Q(t, e) {
588
1003
  }
589
1004
  return -1;
590
1005
  }
591
- const L = Symbol.for("demiurge.devManifest");
592
- async function k(t, e, n) {
593
- const o = e, r = o[L];
1006
+ const ft = Symbol.for("demiurge.devManifest");
1007
+ async function dt(t, e, n) {
1008
+ const o = e, r = o[ft];
594
1009
  if (r)
595
1010
  return r;
596
- const s = lt(
597
- await Kt(t, n)
1011
+ const i = Vt(
1012
+ await ze(t, n)
598
1013
  );
599
- return o[L] = s, s;
1014
+ return o[ft] = i, i;
600
1015
  }
601
- function X(t) {
1016
+ function $t(t) {
602
1017
  return {
603
- clientEntry: v,
1018
+ clientEntry: z,
604
1019
  lang: t.document?.lang,
605
1020
  title: t.document?.title
606
1021
  };
607
1022
  }
608
- function Lt(t, e) {
1023
+ function Ae(t, e) {
609
1024
  return {
610
- ...X(t),
1025
+ ...$t(t),
611
1026
  dev: !0,
612
1027
  transformDocument: e
613
1028
  };
614
1029
  }
615
- const H = /* @__PURE__ */ new Set();
616
- function kt(t, e) {
617
- H.has(e) || t.fallbacks.notFound.some(
1030
+ const pt = /* @__PURE__ */ new Set();
1031
+ function Ve(t, e) {
1032
+ pt.has(e) || t.fallbacks.notFound.some(
618
1033
  (n) => n.fileSegments.length === 0
619
- ) || (H.add(e), console.warn(Yt(nt(e, "@not-found.tsx"))));
1034
+ ) || (pt.add(e), console.warn(Be(Nt(e, "@not-found.tsx"))));
620
1035
  }
621
- async function Ht(t, e = {}) {
622
- const n = e.routesDir ?? "src/routes", o = p(t, n);
623
- if (!U(o))
1036
+ async function Le(t, e = {}) {
1037
+ const n = e.routesDir ?? "src/routes", o = w(t, n);
1038
+ if (!G(o))
624
1039
  return;
625
- const r = await Z(o);
1040
+ const r = await K(o);
626
1041
  if (r.map(
627
- (i) => g(o, i).split($).join("/")
628
- ).some((i) => /^@not-found\.tsx?$/.test(i)))
1042
+ (a) => R(o, a).split(S).join("/")
1043
+ ).some((a) => /^@not-found\.tsx?$/.test(a)))
629
1044
  return;
630
- const a = await Jt(r);
631
- if (a)
1045
+ const s = await ke(r);
1046
+ if (s)
632
1047
  throw new Error(
633
- Gt(
1048
+ Je(
634
1049
  n,
635
- g(o, a).split($).join("/")
1050
+ R(o, s).split(S).join("/")
636
1051
  )
637
1052
  );
638
1053
  }
639
- async function Jt(t) {
1054
+ async function ke(t) {
640
1055
  for (const e of t)
641
- if (!g(G(e), e).startsWith("@") && Bt(await rt(e, "utf8")))
1056
+ if (!R(q(e), e).startsWith("@") && He(await gt(e, "utf8")))
642
1057
  return e;
643
1058
  }
644
- const Ut = h.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), J = new RegExp(
645
- `import\\s*\\{([^}]*)\\}\\s*from\\s*["']${Ut}["']`,
1059
+ const We = $.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), mt = new RegExp(
1060
+ `import\\s*\\{([^}]*)\\}\\s*from\\s*["']${We}["']`,
646
1061
  "g"
647
1062
  );
648
- function Bt(t) {
649
- const e = [...t.matchAll(J)].flatMap(
1063
+ function He(t) {
1064
+ const e = [...t.matchAll(mt)].flatMap(
650
1065
  (o) => o[1].split(",").flatMap((r) => {
651
- const [s, a] = r.trim().split(/\s+as\s+/);
652
- return s.replace(/^type\s+/, "") === "page" ? [a ?? s] : [];
1066
+ const [i, s] = r.trim().split(/\s+as\s+/);
1067
+ return i.replace(/^type\s+/, "") === "page" ? [s ?? i] : [];
653
1068
  })
654
1069
  );
655
1070
  if (e.length === 0)
656
1071
  return !1;
657
- const n = t.replace(J, "");
1072
+ const n = t.replace(mt, "");
658
1073
  return e.some((o) => new RegExp(`\\b${o}\\s*\\(`).test(n));
659
1074
  }
660
- function Gt(t, e) {
1075
+ function Je(t, e) {
661
1076
  return `Demiurge will not build an app with page routes and no ${t}/@not-found.tsx.
662
1077
 
663
1078
  "${e}" declares a page, so a browser can reach a path this app does not serve. Decide what that looks like rather than shipping the framework built-in.
@@ -670,7 +1085,7 @@ Create ${t}/@not-found.tsx:
670
1085
 
671
1086
  It renders inside the layouts above the requested path. Opt out with "export const layout = false".`;
672
1087
  }
673
- function Yt(t) {
1088
+ function Be(t) {
674
1089
  return `Demiurge is serving its built-in 404 because ${t} does not exist.
675
1090
 
676
1091
  Create it before building for production:
@@ -680,67 +1095,105 @@ Create it before building for production:
680
1095
  }
681
1096
  `;
682
1097
  }
683
- function qt({
1098
+ function Ue({
684
1099
  entrySrc: t,
685
1100
  lang: e,
686
1101
  links: n,
687
1102
  metadata: o,
688
1103
  nonce: r,
689
- scripts: s,
690
- styles: a,
691
- title: i
1104
+ scripts: i,
1105
+ styles: s,
1106
+ title: a
692
1107
  }) {
693
- return st({
1108
+ return Mt({
694
1109
  entrySrc: t,
695
1110
  lang: e,
696
1111
  links: n,
697
1112
  metadata: o,
698
1113
  nonce: r,
699
- scripts: s,
700
- styles: a,
701
- title: i
1114
+ scripts: i,
1115
+ styles: s,
1116
+ title: a
702
1117
  });
703
1118
  }
704
- async function w(t, e) {
705
- const n = p(t, e.routesDir ?? "src/routes"), o = p(
1119
+ async function _(t, e) {
1120
+ const n = w(t, e.routesDir ?? "src/routes"), o = w(
706
1121
  t,
707
- typeof e.typedRoutes == "object" ? e.typedRoutes.outputFile ?? C : C
1122
+ typeof e.typedRoutes == "object" ? e.typedRoutes.outputFile ?? it : it
708
1123
  );
709
- await dt({ outputFile: o, routesDir: n });
1124
+ await Jt({ outputFile: o, routesDir: n });
710
1125
  }
711
- function N(t, e) {
1126
+ function A(t, e) {
712
1127
  return e.startsWith(t) && /\.tsx?$/.test(e);
713
1128
  }
714
- async function Kt(t, e) {
715
- const n = await Z(e), o = {};
1129
+ async function ze(t, e) {
1130
+ const n = await K(e), o = {};
716
1131
  for (const r of n) {
717
- const s = Qt(e, r);
718
- o[s] = async () => await t.ssrLoadModule(r);
1132
+ const i = Xe(e, r);
1133
+ o[i] = async () => await t.ssrLoadModule(r);
719
1134
  }
720
1135
  return o;
721
1136
  }
722
- async function zt(t, e, n = {}) {
1137
+ async function Ge(t, e, n = {}) {
723
1138
  const o = new URL(e.url);
724
- return ct(t.routes, o.pathname) ? await ut(t, e, n) : "next";
1139
+ return Tt(t.routes, o.pathname) ? await At(t, e, n) : "next";
725
1140
  }
726
- async function Z(t) {
727
- return (await B(t, {
1141
+ async function K(t) {
1142
+ return (await ht(t, {
728
1143
  recursive: !0,
729
1144
  withFileTypes: !0
730
- })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => p(n.parentPath, n.name));
1145
+ })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => w(n.parentPath, n.name));
1146
+ }
1147
+ const qe = 8, Ye = 50;
1148
+ async function Ke(t, e, n) {
1149
+ const o = new Array(t.length);
1150
+ let r = 0;
1151
+ const i = Array.from(
1152
+ { length: Math.min(e, t.length) },
1153
+ async () => {
1154
+ for (; r < t.length; ) {
1155
+ const s = r++;
1156
+ o[s] = await n(t[s]);
1157
+ }
1158
+ }
1159
+ );
1160
+ return await Promise.all(i), o;
1161
+ }
1162
+ async function yt(t, e = {}) {
1163
+ const n = w(t, e.routesDir ?? "src/routes");
1164
+ return G(n) ? (await Ke(
1165
+ await K(n),
1166
+ qe,
1167
+ oe
1168
+ )).flat().sort(
1169
+ (r, i) => r.file.localeCompare(i.file) || (r.exportName ?? "").localeCompare(i.exportName ?? "") || r.code.localeCompare(i.code)
1170
+ ) : [];
1171
+ }
1172
+ function Qe(t) {
1173
+ return [
1174
+ "Demiurge found invalid static route policy:",
1175
+ ...t.map(St)
1176
+ ].join(`
1177
+ `);
1178
+ }
1179
+ function St(t) {
1180
+ return `${t.exportName ? `${t.file} export ${t.exportName}` : t.file}: [${t.code}] ${t.message}`;
731
1181
  }
732
- function Qt(t, e) {
733
- return `./routes/${g(t, e).split($).join("/")}`;
1182
+ function Xe(t, e) {
1183
+ return `./routes/${R(t, e).split(S).join("/")}`;
734
1184
  }
735
1185
  export {
736
- ue as demiurge,
737
- Ht as unstable_assertRootNotFoundRoute,
738
- Ot as unstable_createClientEntrySource,
739
- Kt as unstable_createDevRouteImporters,
740
- qt as unstable_createDocumentHtml,
741
- _t as unstable_createServerEntrySource,
742
- Bt as unstable_declaresPageRoute,
743
- zt as unstable_handleDevRequest,
744
- bt as unstable_stripClientPageData
1186
+ fr as demiurge,
1187
+ Le as unstable_assertRootNotFoundRoute,
1188
+ Ee as unstable_createClientEntrySource,
1189
+ ze as unstable_createDevRouteImporters,
1190
+ Ue as unstable_createDocumentHtml,
1191
+ De as unstable_createServerEntrySource,
1192
+ He as unstable_declaresPageRoute,
1193
+ Qe as unstable_formatStaticPolicyFindings,
1194
+ Ge as unstable_handleDevRequest,
1195
+ ge as unstable_stripClientPageData,
1196
+ yt as unstable_verifyRoutePolicies,
1197
+ ie as unstable_verifyRoutePolicySource
745
1198
  };
746
1199
  //# sourceMappingURL=index.js.map