@demiurgejs/core 0.1.1 → 0.2.0-beta.1

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