@lunora/config 0.0.0 → 1.0.0-alpha.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 (39) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +115 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/index.d.mts +1075 -0
  5. package/dist/index.d.ts +1075 -0
  6. package/dist/index.mjs +20 -0
  7. package/dist/packem_shared/AGENT_RULES_DIR-lcgC08aE.mjs +40 -0
  8. package/dist/packem_shared/DEV_VARS_EXAMPLE_FILE-dJPNTEnK.mjs +37 -0
  9. package/dist/packem_shared/LINKED_PROJECT_DIR-CXwXzV_C.mjs +52 -0
  10. package/dist/packem_shared/PACKAGE_SECRETS_REGISTRY-CySy5vR_.mjs +62 -0
  11. package/dist/packem_shared/REQUIRED_COMPATIBILITY_DATE-Dd1suoit.mjs +476 -0
  12. package/dist/packem_shared/applyAdditiveEdit-C-snTFEV.mjs +228 -0
  13. package/dist/packem_shared/buildPackageSecretsBlock-S74dgmwy.mjs +187 -0
  14. package/dist/packem_shared/classifyPolicyEdit-BHeAqF8P.mjs +99 -0
  15. package/dist/packem_shared/createConfirm-fvpdgJ9s.mjs +100 -0
  16. package/dist/packem_shared/detectFramework-Br-BcPBq.mjs +41 -0
  17. package/dist/packem_shared/discoverContainerInfo-BXFs6Wav.mjs +19 -0
  18. package/dist/packem_shared/discoverSchemaInfo-DWtypqpP.mjs +25 -0
  19. package/dist/packem_shared/discoverWorkflowInfo-CedvR0mn.mjs +19 -0
  20. package/dist/packem_shared/findWranglerFile-DwSuC-Kn.mjs +25 -0
  21. package/dist/packem_shared/formatLunoraEvent-D2fDeGB6.mjs +86 -0
  22. package/dist/packem_shared/handlePolicyScaffoldRequest-CiC2IGKx.mjs +103 -0
  23. package/dist/packem_shared/handleSchemaEditRequest-Df-Wrix-.mjs +99 -0
  24. package/dist/packem_shared/handleSeedRequest-DVCjaGO-.mjs +61 -0
  25. package/dist/packem_shared/inferLunoraBindings-0W3eRdIP.mjs +302 -0
  26. package/dist/packem_shared/injectRemoteFlags-C-WZAKLY.mjs +105 -0
  27. package/dist/packem_shared/interpretRemote-CtcIcB5-.mjs +34 -0
  28. package/dist/packem_shared/parseDevVariable-CJiq2IwE.mjs +30 -0
  29. package/dist/packem_shared/parseSchema-DSeyktvG.mjs +107 -0
  30. package/dist/packem_shared/policy-scaffold.d-DCmwn7zQ.d.mts +74 -0
  31. package/dist/packem_shared/policy-scaffold.d-DCmwn7zQ.d.ts +74 -0
  32. package/dist/packem_shared/reconcileWranglerBindings-ByJk3yLU.mjs +277 -0
  33. package/dist/packem_shared/renderStudioHtml-449Ysn75.mjs +37 -0
  34. package/dist/packem_shared/serveJsonHandler-B4OLTGLS.mjs +86 -0
  35. package/dist/packem_shared/studioAssetsStamp-Csk5RS4E.mjs +28 -0
  36. package/dist/studio-host/index.d.mts +227 -0
  37. package/dist/studio-host/index.d.ts +227 -0
  38. package/dist/studio-host/index.mjs +7 -0
  39. package/package.json +57 -17
@@ -0,0 +1,74 @@
1
+ /** Scaffold a new policy/role/permission stub file under `lunora/`. */
2
+ interface ScaffoldPolicyEdit {
3
+ readonly kind: "scaffoldPolicy";
4
+ /**
5
+ * Base name for the generated file and its exported policy-set identifier,
6
+ * e.g. `invoices` → `lunora/invoices.policies.ts` exporting `invoicesPolicies`.
7
+ */
8
+ readonly name: string;
9
+ /** Logical table the scaffolded policy guards (used in the stub body). */
10
+ readonly table: string;
11
+ }
12
+ /** Append `.use(rls(<policies>))` to an existing procedure's builder chain. */
13
+ interface WireRlsEdit {
14
+ /** Exported procedure name to wire, e.g. `listInvoices`. */
15
+ readonly exportName: string;
16
+ readonly kind: "wireRls";
17
+ /** Identifier of the policy set passed to `rls(...)`, e.g. `invoicesPolicies`. */
18
+ readonly policies: string;
19
+ }
20
+ /** Additive scaffolder edits — the only requests the scaffolder applies. */
21
+ type AdditivePolicyEdit = ScaffoldPolicyEdit | WireRlsEdit;
22
+ /**
23
+ * Destructive scaffolder requests — never applied. Rewriting an existing `when`
24
+ * body changes evaluation semantics silently, so it is refused (STOP condition
25
+ * in plan 025); carried as data so the editor can describe the request.
26
+ */
27
+ interface DestructivePolicyEdit {
28
+ readonly exportName?: string;
29
+ readonly kind: "rewritePolicyWhen";
30
+ readonly table?: string;
31
+ }
32
+ /** Any request the scaffolder can receive. */
33
+ type PolicyEdit = AdditivePolicyEdit | DestructivePolicyEdit;
34
+ /**
35
+ * Classify a scaffolder request. Additive requests ({@link AdditivePolicyEdit})
36
+ * apply directly; everything else (rewriting an existing predicate) changes
37
+ * evaluation semantics and is destructive.
38
+ */
39
+ declare const classifyPolicyEdit: (edit: PolicyEdit) => "additive" | "destructive";
40
+ /** Failure reasons a scaffolder request can report. */
41
+ type PolicyScaffoldFailureReason = "already-wired" | "destructive" | "invalid-identifier" | "unknown-procedure" | "unsupported-procedure-shape";
42
+ /** Tagged result of generating a stub file. */
43
+ type ScaffoldFileResult = {
44
+ fileName: string;
45
+ ok: true;
46
+ source: string;
47
+ } | {
48
+ ok: false;
49
+ reason: PolicyScaffoldFailureReason;
50
+ };
51
+ /** Tagged result of wiring a procedure. */
52
+ type WireResult = {
53
+ ok: false;
54
+ reason: PolicyScaffoldFailureReason;
55
+ } | {
56
+ ok: true;
57
+ text: string;
58
+ };
59
+ /**
60
+ * Generate the source of a new policy/role/permission stub file. The `when`
61
+ * predicate is a `() => false` skeleton with a TODO — the scaffolder never
62
+ * authors real logic, the developer fills it in. Pure (no I/O); the handler
63
+ * writes the returned source and refuses to overwrite an existing file.
64
+ */
65
+ declare const scaffoldPolicyFile: (edit: ScaffoldPolicyEdit) => ScaffoldFileResult;
66
+ /**
67
+ * Append `.use(rls(<policies>))` to a procedure's builder chain, preserving the
68
+ * terminal `.query(handler)` (and its handler body) byte-for-byte. Only the
69
+ * **builder** form can be wired; the bare-factory form (`query({ handler })`)
70
+ * has no chain and is reported `unsupported-procedure-shape` so the editor can
71
+ * tell the developer to convert it rather than silently rewriting their code.
72
+ */
73
+ declare const wireRlsIntoProcedure: (source: string, edit: WireRlsEdit) => WireResult;
74
+ export { AdditivePolicyEdit as A, DestructivePolicyEdit as D, PolicyEdit as P, ScaffoldPolicyEdit as S, WireRlsEdit as W, PolicyScaffoldFailureReason as a, ScaffoldFileResult as b, WireResult as c, classifyPolicyEdit as d, scaffoldPolicyFile as s, wireRlsIntoProcedure as w };
@@ -0,0 +1,277 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ import { containerBuildTag } from '@lunora/container';
3
+ import { modify, applyEdits } from 'jsonc-parser';
4
+ import { findWranglerFile, readWranglerJsonc } from './findWranglerFile-DwSuC-Kn.mjs';
5
+
6
+ const FORMATTING = { formattingOptions: { insertSpaces: true, tabSize: 4 } };
7
+ const D1_PLACEHOLDER_ID = "<replace-with-d1-create-id>";
8
+ const collectExportGaps = (inferred) => {
9
+ const gaps = [];
10
+ for (const container of inferred.containers) {
11
+ if (!container.exported) {
12
+ gaps.push({ className: container.className, exportName: container.exportName, kind: "container", module: "containers" });
13
+ }
14
+ }
15
+ for (const workflow of inferred.workflows) {
16
+ if (!workflow.exported) {
17
+ gaps.push({ className: workflow.className, exportName: workflow.exportName, kind: "workflow", module: "workflows" });
18
+ }
19
+ }
20
+ return gaps;
21
+ };
22
+ const collectHintBindingWarnings = (inferred, parsed) => {
23
+ const rules = [
24
+ [
25
+ inferred.usesKv && (parsed?.kv_namespaces?.length ?? 0) === 0,
26
+ "@lunora/kv is used but no kv_namespaces binding exists; add a kv_namespaces entry ({ binding, id }) and pass env.<BINDING> to createKv() — the namespace id can't be auto-provisioned."
27
+ ],
28
+ [
29
+ inferred.usesHyperdrive && (parsed?.hyperdrive?.length ?? 0) === 0,
30
+ "@lunora/hyperdrive is used but no hyperdrive binding exists; run 'wrangler hyperdrive create' and add a 'hyperdrive' binding ({ binding, id }) — the id can't be auto-provisioned."
31
+ ],
32
+ [
33
+ inferred.usesPipelines && (parsed?.pipelines?.length ?? 0) === 0,
34
+ "@lunora/pipelines is used but no pipelines binding exists; run 'wrangler pipelines create <name>' and add a 'pipelines' binding ({ binding, pipeline }) — the pipeline resource can't be auto-provisioned."
35
+ ]
36
+ ];
37
+ return rules.filter(([active]) => active).map(([, warning]) => warning);
38
+ };
39
+ const collectWarnings = (inferred, parsed) => {
40
+ const exported = new Set(inferred.durableObjects.map((object) => object.className));
41
+ const warnings = [];
42
+ const hasR2Bucket = (parsed?.r2_buckets?.length ?? 0) > 0;
43
+ const hasSessionStore = (parsed?.d1_databases?.some((binding) => binding.binding === "DB") ?? false) || inferred.needsD1;
44
+ if (inferred.usesStorage && !hasR2Bucket) {
45
+ warnings.push(
46
+ "@lunora/storage is used but R2 bucket bindings have user-defined names; add an r2_buckets entry and pass env.<BINDING> to createStorage()."
47
+ );
48
+ }
49
+ if (inferred.usesAuth && !exported.has("SessionDO") && !hasSessionStore) {
50
+ warnings.push(
51
+ "@lunora/auth is used but the worker entry exports no SessionDO; sessions are D1-backed, or export SessionDO to enable DO-backed sessions."
52
+ );
53
+ }
54
+ if (inferred.usesScheduler && !exported.has("SchedulerDO")) {
55
+ warnings.push("@lunora/scheduler is used but the worker entry exports no SchedulerDO; export it so the SCHEDULER binding can be provisioned.");
56
+ }
57
+ for (const container of inferred.containers) {
58
+ if (!container.exported) {
59
+ warnings.push(
60
+ `container "${container.exportName}" is declared but ${container.className} is not exported by the worker entry; add \`export * from "./lunora/_generated/containers"\` so its binding can be provisioned.`
61
+ );
62
+ }
63
+ }
64
+ for (const workflow of inferred.workflows) {
65
+ if (!workflow.exported) {
66
+ warnings.push(
67
+ `workflow "${workflow.exportName}" is declared but ${workflow.className} is not exported by the worker entry; add \`export * from "./lunora/_generated/workflows"\` so its binding can be provisioned.`
68
+ );
69
+ }
70
+ }
71
+ if (inferred.containers.length > 0 && parsed?.observability?.enabled === false) {
72
+ warnings.push("containers are declared but observability is explicitly disabled in wrangler.jsonc — container logs will not be captured.");
73
+ }
74
+ if (inferred.usesPayment) {
75
+ warnings.push(
76
+ "@lunora/payment is used; set the provider secrets in .dev.vars — STRIPE_SECRET_KEY + STRIPE_WEBHOOK_SECRET (Stripe) or POLAR_ACCESS_TOKEN + POLAR_WEBHOOK_SECRET (Polar)."
77
+ );
78
+ }
79
+ warnings.push(...collectHintBindingWarnings(inferred, parsed));
80
+ return warnings;
81
+ };
82
+ const applyModify = (text, path, value) => {
83
+ const edits = modify(text, [...path], value, FORMATTING);
84
+ return edits.length > 0 ? applyEdits(text, edits) : text;
85
+ };
86
+ const nextMigrationTag = (migrations) => {
87
+ const used = new Set(migrations.map((migration) => migration.tag));
88
+ let index = 1;
89
+ while (used.has(`v${String(index)}`)) {
90
+ index += 1;
91
+ }
92
+ return `v${String(index)}`;
93
+ };
94
+ const reconcileDurableObjects = (text, parsed, required) => {
95
+ const existingBindings = parsed.durable_objects?.bindings ?? [];
96
+ const existingNames = new Set(existingBindings.map((binding) => binding.name));
97
+ const missing = required.filter((object) => !existingNames.has(object.binding));
98
+ let nextText = text;
99
+ const added = [];
100
+ if (missing.length > 0) {
101
+ const nextBindings = [
102
+ ...existingBindings,
103
+ ...missing.map((object) => {
104
+ return { class_name: object.className, name: object.binding };
105
+ })
106
+ ];
107
+ nextText = applyModify(nextText, ["durable_objects", "bindings"], nextBindings);
108
+ added.push(...missing.map((object) => `${object.binding}/${object.className}`));
109
+ }
110
+ const migrations = parsed.migrations ?? [];
111
+ const registered = new Set(migrations.flatMap((migration) => [...migration.new_sqlite_classes ?? [], ...migration.new_classes ?? []]));
112
+ const missingClasses = required.map((object) => object.className).filter((className) => !registered.has(className));
113
+ if (missingClasses.length > 0) {
114
+ const nextMigrations = [...migrations, { new_sqlite_classes: missingClasses, tag: nextMigrationTag(migrations) }];
115
+ nextText = applyModify(nextText, ["migrations"], nextMigrations);
116
+ }
117
+ return { added, text: nextText };
118
+ };
119
+ const reconcileD1 = (text, parsed) => {
120
+ const d1Bindings = parsed.d1_databases ?? [];
121
+ if (d1Bindings.some((binding) => binding.binding === "DB")) {
122
+ return { added: [], text };
123
+ }
124
+ const databaseName = typeof parsed.name === "string" && parsed.name.length > 0 ? parsed.name : "lunora";
125
+ const nextD1 = [...d1Bindings, { binding: "DB", database_id: D1_PLACEHOLDER_ID, database_name: databaseName }];
126
+ return { added: ["DB (D1)"], text: applyModify(text, ["d1_databases"], nextD1) };
127
+ };
128
+ const reconcileSelfDescribing = (text, parsed, key, binding, label) => {
129
+ const current = parsed[key]?.binding;
130
+ if (typeof current === "string" && current.length > 0) {
131
+ return { added: [], text };
132
+ }
133
+ return { added: [label], text: applyModify(text, [key], { binding }) };
134
+ };
135
+ const reconcileAnalytics = (text, parsed) => {
136
+ if ((parsed.analytics_engine_datasets?.length ?? 0) > 0) {
137
+ return { added: [], text };
138
+ }
139
+ const nextDatasets = [{ binding: "ANALYTICS", dataset: "ANALYTICS" }];
140
+ return { added: ["ANALYTICS (Analytics Engine)"], text: applyModify(text, ["analytics_engine_datasets"], nextDatasets) };
141
+ };
142
+ const wranglerInstanceType = (instanceType) => {
143
+ if (typeof instanceType === "string") {
144
+ return instanceType;
145
+ }
146
+ const custom = {};
147
+ if (instanceType.diskMb !== void 0) {
148
+ custom.disk_mb = instanceType.diskMb;
149
+ }
150
+ if (instanceType.memoryMib !== void 0) {
151
+ custom.memory_mib = instanceType.memoryMib;
152
+ }
153
+ if (instanceType.vcpu !== void 0) {
154
+ custom.vcpu = instanceType.vcpu;
155
+ }
156
+ return custom;
157
+ };
158
+ const imageRefFor = (container) => {
159
+ if (container.image.kind === "dockerfile") {
160
+ return container.image.dockerfilePath;
161
+ }
162
+ if (container.image.kind === "registry") {
163
+ return container.image.reference;
164
+ }
165
+ return containerBuildTag(container.exportName);
166
+ };
167
+ const containerEntryFor = (container) => {
168
+ const entry = {
169
+ class_name: container.className,
170
+ image: imageRefFor(container)
171
+ };
172
+ if (container.image.kind === "dockerfile") {
173
+ entry.image_build_context = container.image.buildContext;
174
+ }
175
+ if (container.buildArgs !== void 0 && container.image.kind !== "registry") {
176
+ entry.image_vars = container.buildArgs;
177
+ }
178
+ if (container.instanceType !== void 0) {
179
+ entry.instance_type = wranglerInstanceType(container.instanceType);
180
+ }
181
+ if (container.maxInstances !== void 0) {
182
+ entry.max_instances = container.maxInstances;
183
+ }
184
+ if (container.name !== void 0) {
185
+ entry.name = container.name;
186
+ }
187
+ if (container.rollout?.stepPercentage !== void 0) {
188
+ entry.rollout_step_percentage = container.rollout.stepPercentage;
189
+ }
190
+ if (container.rollout?.gracePeriodSeconds !== void 0) {
191
+ entry.rollout_active_grace_period = container.rollout.gracePeriodSeconds;
192
+ }
193
+ return entry;
194
+ };
195
+ const reconcileContainers = (text, parsed, containers) => {
196
+ const existing = parsed.containers ?? [];
197
+ const existingClasses = new Set(existing.map((entry) => entry.class_name));
198
+ const missing = containers.filter((container) => !existingClasses.has(container.className));
199
+ if (missing.length === 0) {
200
+ return { added: [], text };
201
+ }
202
+ const nextText = applyModify(text, ["containers"], [...existing, ...missing.map((container) => containerEntryFor(container))]);
203
+ return { added: missing.map((container) => `containers/${container.className}`), text: nextText };
204
+ };
205
+ const reconcileObservability = (text, parsed) => {
206
+ if (parsed.observability !== void 0) {
207
+ return { added: [], text };
208
+ }
209
+ const nextText = applyModify(text, ["observability"], { enabled: true, head_sampling_rate: 1 });
210
+ return { added: ["observability"], text: nextText };
211
+ };
212
+ const workflowEntryFor = (workflow) => {
213
+ return { binding: workflow.bindingName, class_name: workflow.className, name: workflow.name };
214
+ };
215
+ const reconcileWorkflows = (text, parsed, workflows) => {
216
+ const existing = parsed.workflows ?? [];
217
+ const existingClasses = new Set(existing.map((entry) => entry.class_name));
218
+ const missing = workflows.filter((workflow) => !existingClasses.has(workflow.className));
219
+ if (missing.length === 0) {
220
+ return { added: [], text };
221
+ }
222
+ const nextText = applyModify(text, ["workflows"], [...existing, ...missing.map((workflow) => workflowEntryFor(workflow))]);
223
+ return { added: missing.map((workflow) => `workflows/${workflow.className}`), text: nextText };
224
+ };
225
+ const reconcileWranglerBindings = (projectRoot, inferred) => {
226
+ const wranglerPath = findWranglerFile(projectRoot);
227
+ const exportGaps = collectExportGaps(inferred);
228
+ if (!wranglerPath) {
229
+ return { added: [], changed: false, exportGaps, reason: "wrangler.jsonc not found", warnings: collectWarnings(inferred) };
230
+ }
231
+ const { parsed, text: original } = readWranglerJsonc(wranglerPath);
232
+ if (parsed === void 0) {
233
+ return { added: [], changed: false, exportGaps, reason: `failed to parse ${wranglerPath} as JSONC`, warnings: collectWarnings(inferred), wranglerPath };
234
+ }
235
+ const warnings = collectWarnings(inferred, parsed);
236
+ const exportedContainers = inferred.containers.filter((container) => container.exported);
237
+ const requiredDurableObjects = [
238
+ ...inferred.durableObjects,
239
+ ...exportedContainers.map((container) => {
240
+ return { binding: container.bindingName, className: container.className };
241
+ })
242
+ ];
243
+ const exportedWorkflows = inferred.workflows.filter((workflow) => workflow.exported);
244
+ const pipeline = [
245
+ { enabled: true, run: (text2) => reconcileDurableObjects(text2, parsed, requiredDurableObjects) },
246
+ { enabled: inferred.needsD1, run: (text2) => reconcileD1(text2, parsed) },
247
+ { enabled: inferred.usesAi, run: (text2) => reconcileSelfDescribing(text2, parsed, "ai", "AI", "AI (Workers AI)") },
248
+ { enabled: inferred.usesBrowser, run: (text2) => reconcileSelfDescribing(text2, parsed, "browser", "BROWSER", "BROWSER (Browser Rendering)") },
249
+ { enabled: inferred.usesImages, run: (text2) => reconcileSelfDescribing(text2, parsed, "images", "IMAGES", "IMAGES (Cloudflare Images)") },
250
+ { enabled: inferred.usesAnalytics, run: (text2) => reconcileAnalytics(text2, parsed) },
251
+ { enabled: true, run: (text2) => reconcileObservability(text2, parsed) },
252
+ { enabled: exportedContainers.length > 0, run: (text2) => reconcileContainers(text2, parsed, exportedContainers) },
253
+ { enabled: exportedWorkflows.length > 0, run: (text2) => reconcileWorkflows(text2, parsed, exportedWorkflows) }
254
+ ];
255
+ let text = original;
256
+ const added = [];
257
+ for (const step of pipeline) {
258
+ if (!step.enabled) {
259
+ continue;
260
+ }
261
+ const result = step.run(text);
262
+ text = result.text;
263
+ added.push(...result.added);
264
+ }
265
+ if (added.includes("DB (D1)")) {
266
+ warnings.push(
267
+ `wrote a DB binding with a placeholder database_id ("${D1_PLACEHOLDER_ID}") — run \`wrangler d1 create <name>\` and replace it before deploying.`
268
+ );
269
+ }
270
+ if (text === original) {
271
+ return { added: [], changed: false, exportGaps, reason: "bindings already in sync", warnings, wranglerPath };
272
+ }
273
+ writeFileSync(wranglerPath, text, "utf8");
274
+ return { added, changed: true, exportGaps, warnings, wranglerPath };
275
+ };
276
+
277
+ export { reconcileWranglerBindings };
@@ -0,0 +1,37 @@
1
+ const forInlineScript = (value) => JSON.stringify(value).replaceAll("<", String.raw`\u003c`);
2
+ const forAttribute = (value) => value.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;");
3
+ const renderStudioHtml = (config) => {
4
+ const settings = [`window.__LUNORA_BASE_PATH__=${forInlineScript(config.basePath)};`];
5
+ if (config.adminToken !== void 0 && config.adminToken !== "") {
6
+ settings.push(`window.__LUNORA_ADMIN_TOKEN__=${forInlineScript(config.adminToken)};`);
7
+ }
8
+ if (config.dataEditable === true) {
9
+ settings.push("window.__LUNORA_DATA_EDITABLE__=true;");
10
+ }
11
+ if (config.runAsIdentity === true) {
12
+ settings.push("window.__LUNORA_RUN_AS_IDENTITY__=true;");
13
+ }
14
+ if (config.schemaEditable === true) {
15
+ settings.push("window.__LUNORA_SCHEMA_EDITABLE__=true;");
16
+ }
17
+ if (config.rulesInstalled === false) {
18
+ settings.push("window.__LUNORA_RULES_INSTALLED__=false;");
19
+ }
20
+ return `<!doctype html>
21
+ <html lang="en">
22
+ <head>
23
+ <meta charset="UTF-8" />
24
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
25
+ <title>Lunora Studio</title>
26
+ <script>${settings.join("")}<\/script>
27
+ <link rel="stylesheet" href="${forAttribute(config.styleHref)}" />
28
+ </head>
29
+ <body>
30
+ <div id="root"></div>
31
+ <script type="module" src="${forAttribute(config.scriptSrc)}"><\/script>
32
+ </body>
33
+ </html>
34
+ `;
35
+ };
36
+
37
+ export { renderStudioHtml as default };
@@ -0,0 +1,86 @@
1
+ const MAX_BODY_BYTES = 1e6;
2
+ const readBody = async (request) => await new Promise((resolve, reject) => {
3
+ const chunks = [];
4
+ let size = 0;
5
+ request.on("data", (chunk) => {
6
+ size += chunk.length;
7
+ if (size > MAX_BODY_BYTES) {
8
+ reject(new Error("request body too large"));
9
+ return;
10
+ }
11
+ chunks.push(chunk);
12
+ });
13
+ request.on("end", () => {
14
+ resolve(Buffer.concat(chunks).toString("utf8"));
15
+ });
16
+ request.on("error", reject);
17
+ });
18
+ const respondJson = (response, status, body) => {
19
+ response.statusCode = status;
20
+ response.setHeader("Content-Type", "application/json; charset=utf-8");
21
+ response.end(JSON.stringify(body));
22
+ };
23
+ const headerValue = (raw) => {
24
+ const value = Array.isArray(raw) ? raw[0] : raw;
25
+ return typeof value === "string" ? value.trim().toLowerCase() : void 0;
26
+ };
27
+ const SAME_SITE_FETCH_VALUES = /* @__PURE__ */ new Set(["none", "same-origin", "same-site"]);
28
+ const originRejectionReason = (request) => {
29
+ const secFetchSite = headerValue(request.headers["sec-fetch-site"]);
30
+ if (secFetchSite !== void 0) {
31
+ return SAME_SITE_FETCH_VALUES.has(secFetchSite) ? void 0 : "cross-origin request rejected";
32
+ }
33
+ const origin = headerValue(request.headers.origin);
34
+ if (origin === void 0 || origin === "null") {
35
+ return void 0;
36
+ }
37
+ let originHost;
38
+ try {
39
+ originHost = new URL(origin).host.toLowerCase();
40
+ } catch {
41
+ return "invalid origin header";
42
+ }
43
+ return originHost === headerValue(request.headers.host) ? void 0 : "cross-origin request rejected";
44
+ };
45
+ const csrfRejectionReason = (request) => {
46
+ const method = (request.method ?? "GET").toUpperCase();
47
+ const isStateChanging = method !== "GET" && method !== "HEAD";
48
+ const originReason = originRejectionReason(request);
49
+ if (originReason !== void 0) {
50
+ return originReason;
51
+ }
52
+ if (isStateChanging) {
53
+ const contentType = headerValue(request.headers["content-type"]);
54
+ if (!contentType?.startsWith("application/json")) {
55
+ return "content-type must be application/json";
56
+ }
57
+ }
58
+ return void 0;
59
+ };
60
+ const serveJsonHandler = (request, response, handle, projectRoot) => {
61
+ const run = async () => {
62
+ try {
63
+ const rejection = csrfRejectionReason(request);
64
+ if (rejection !== void 0) {
65
+ respondJson(response, 403, { error: rejection, ok: false });
66
+ return;
67
+ }
68
+ const raw = request.method === "GET" ? "" : await readBody(request);
69
+ let parsed;
70
+ try {
71
+ parsed = raw === "" ? void 0 : JSON.parse(raw);
72
+ } catch {
73
+ respondJson(response, 400, { error: "invalid-json", ok: false });
74
+ return;
75
+ }
76
+ const result = handle({ body: parsed, method: request.method ?? "POST", projectRoot });
77
+ respondJson(response, result.status, result.body);
78
+ } catch (error) {
79
+ respondJson(response, 500, { error: error instanceof Error ? error.message : String(error), ok: false });
80
+ }
81
+ };
82
+ run().catch(() => {
83
+ });
84
+ };
85
+
86
+ export { serveJsonHandler };
@@ -0,0 +1,28 @@
1
+ import { readFileSync, statSync } from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+
4
+ const loadStudioAssets = (logger, resolveFrom = import.meta.url) => {
5
+ try {
6
+ const require = createRequire(resolveFrom);
7
+ return {
8
+ script: readFileSync(require.resolve("@lunora/studio/standalone/studio.js")),
9
+ styles: readFileSync(require.resolve("@lunora/studio/styles.css"))
10
+ };
11
+ } catch (error) {
12
+ const message = error instanceof Error ? error.message : String(error);
13
+ logger?.warnOnce?.(`[lunora] studio assets unavailable (build @lunora/studio?): ${message}`);
14
+ return void 0;
15
+ }
16
+ };
17
+ const studioAssetsStamp = (resolveFrom = import.meta.url) => {
18
+ try {
19
+ const require = createRequire(resolveFrom);
20
+ const scriptMtime = statSync(require.resolve("@lunora/studio/standalone/studio.js")).mtimeMs;
21
+ const stylesMtime = statSync(require.resolve("@lunora/studio/styles.css")).mtimeMs;
22
+ return Math.max(scriptMtime, stylesMtime);
23
+ } catch {
24
+ return void 0;
25
+ }
26
+ };
27
+
28
+ export { loadStudioAssets as default, studioAssetsStamp };