@lunora/vite 0.0.0 → 1.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +105 -0
- package/README.md +117 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +374 -0
- package/dist/index.d.ts +374 -0
- package/dist/index.mjs +155 -0
- package/dist/packem_shared/CLASS_A_WIRING-CZVcjgKo.mjs +106 -0
- package/dist/packem_shared/DEV_WORKER_ENV_VALUE-Coo6bgVz.mjs +36 -0
- package/dist/packem_shared/STUDIO_PATH-5ppCdBHa.mjs +210 -0
- package/dist/packem_shared/WORKER_STARTUP_HINT-DhsXUW8k.mjs +81 -0
- package/dist/packem_shared/codegenPlugin-MuvbqAP8.mjs +218 -0
- package/dist/packem_shared/devVariablesPlugin-CVjkQay7.mjs +21 -0
- package/dist/packem_shared/log-BjO9EWah.mjs +8 -0
- package/dist/packem_shared/logStreamPlugin-CqvZ17kd.mjs +61 -0
- package/dist/packem_shared/planViteRemoteBindings-QN5ncUS1.mjs +50 -0
- package/dist/packem_shared/reconcileWranglerCrons-PxGwfCp_.mjs +29 -0
- package/dist/packem_shared/wranglerValidatorPlugin-CEoJEghS.mjs +66 -0
- package/package.json +53 -17
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { detectAgentRules } from '@lunora/config';
|
|
2
|
+
import { handleSeedRequest, handleSchemaEditRequest, handlePolicyScaffoldRequest, SEED_ENDPOINT, SCHEMA_EDIT_ENDPOINT, POLICY_SCAFFOLD_ENDPOINT, serveJsonHandler, renderStudioHtml, resolveAdminToken, studioAssetsStamp, loadStudioAssets } from '@lunora/config/studio-host';
|
|
3
|
+
|
|
4
|
+
const STUDIO_PATH = "/__lunora";
|
|
5
|
+
const STUDIO_SCRIPT_PATH = `${STUDIO_PATH}/studio.js`;
|
|
6
|
+
const STUDIO_STYLE_PATH = `${STUDIO_PATH}/styles.css`;
|
|
7
|
+
const LEADING_SLASH = /^\//;
|
|
8
|
+
const TRAILING_SLASH = /\/$/;
|
|
9
|
+
const JSON_ENDPOINT_HANDLERS = {
|
|
10
|
+
[POLICY_SCAFFOLD_ENDPOINT]: handlePolicyScaffoldRequest,
|
|
11
|
+
[SCHEMA_EDIT_ENDPOINT]: handleSchemaEditRequest,
|
|
12
|
+
[SEED_ENDPOINT]: handleSeedRequest
|
|
13
|
+
};
|
|
14
|
+
const STATE_CHANGING_ENDPOINTS = new Set(Object.keys(JSON_ENDPOINT_HANDLERS));
|
|
15
|
+
const headerValue = (raw) => {
|
|
16
|
+
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
17
|
+
return typeof value === "string" ? value.trim().toLowerCase() : void 0;
|
|
18
|
+
};
|
|
19
|
+
const isLoopbackAddress = (remoteAddress) => {
|
|
20
|
+
if (remoteAddress === void 0 || remoteAddress === "") {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
const address = remoteAddress.toLowerCase();
|
|
24
|
+
const v4 = address.startsWith("::ffff:") ? address.slice(7) : address;
|
|
25
|
+
if (v4 === "::1") {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return v4.startsWith("127.");
|
|
29
|
+
};
|
|
30
|
+
const hostnameOf = (host) => {
|
|
31
|
+
if (host === void 0) {
|
|
32
|
+
return void 0;
|
|
33
|
+
}
|
|
34
|
+
if (host.startsWith("[")) {
|
|
35
|
+
const close = host.indexOf("]");
|
|
36
|
+
return close === -1 ? host.slice(1) : host.slice(1, close);
|
|
37
|
+
}
|
|
38
|
+
const colon = host.indexOf(":");
|
|
39
|
+
return colon === -1 ? host : host.slice(0, colon);
|
|
40
|
+
};
|
|
41
|
+
const LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["0.0.0.0", "127.0.0.1", "::1", "localhost"]);
|
|
42
|
+
const transportRejectionReason = (request) => {
|
|
43
|
+
if (!isLoopbackAddress(request.socket?.remoteAddress ?? void 0)) {
|
|
44
|
+
return "Lunora studio is only available on loopback connections in dev.";
|
|
45
|
+
}
|
|
46
|
+
const host = hostnameOf(headerValue(request.headers?.host));
|
|
47
|
+
if (host !== void 0 && !LOOPBACK_HOSTS.has(host)) {
|
|
48
|
+
return "Lunora studio rejects a non-localhost Host header in dev.";
|
|
49
|
+
}
|
|
50
|
+
return void 0;
|
|
51
|
+
};
|
|
52
|
+
const originRejectionReason = (headers) => {
|
|
53
|
+
const secFetchSite = headerValue(headers["sec-fetch-site"]);
|
|
54
|
+
if (secFetchSite !== void 0) {
|
|
55
|
+
return secFetchSite === "same-origin" || secFetchSite === "same-site" || secFetchSite === "none" ? void 0 : "cross-origin request rejected";
|
|
56
|
+
}
|
|
57
|
+
const origin = headerValue(headers.origin);
|
|
58
|
+
if (origin === void 0 || origin === "null") {
|
|
59
|
+
return void 0;
|
|
60
|
+
}
|
|
61
|
+
let originHost;
|
|
62
|
+
try {
|
|
63
|
+
originHost = new URL(origin).host.toLowerCase();
|
|
64
|
+
} catch {
|
|
65
|
+
return "invalid origin header";
|
|
66
|
+
}
|
|
67
|
+
const host = headerValue(headers.host);
|
|
68
|
+
return host === void 0 || originHost !== host ? "cross-origin request rejected" : void 0;
|
|
69
|
+
};
|
|
70
|
+
const csrfRejectionReason = (request) => {
|
|
71
|
+
const headers = request.headers ?? {};
|
|
72
|
+
const originReason = originRejectionReason(headers);
|
|
73
|
+
if (originReason !== void 0) {
|
|
74
|
+
return originReason;
|
|
75
|
+
}
|
|
76
|
+
const method = (request.method ?? "GET").toUpperCase();
|
|
77
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
78
|
+
const contentType = headerValue(headers["content-type"]);
|
|
79
|
+
if (!contentType?.startsWith("application/json")) {
|
|
80
|
+
return "content-type must be application/json";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return void 0;
|
|
84
|
+
};
|
|
85
|
+
const sendOk = (response, body, contentType) => {
|
|
86
|
+
response.statusCode = 200;
|
|
87
|
+
response.setHeader("Content-Type", contentType);
|
|
88
|
+
response.end(body);
|
|
89
|
+
};
|
|
90
|
+
const buildStudioUrl = (input) => {
|
|
91
|
+
const path = STUDIO_PATH.replace(LEADING_SLASH, "");
|
|
92
|
+
if (input.resolvedLocal !== void 0 && input.resolvedLocal !== "") {
|
|
93
|
+
const origin = input.resolvedLocal.endsWith("/") ? input.resolvedLocal.slice(0, -1) : input.resolvedLocal;
|
|
94
|
+
return `${origin}/${path}`;
|
|
95
|
+
}
|
|
96
|
+
const base = input.base === void 0 || input.base === "/" ? "" : input.base.replace(TRAILING_SLASH, "");
|
|
97
|
+
if (input.address === void 0 || typeof input.address === "string") {
|
|
98
|
+
return `http://localhost:5173${base}/${path}`;
|
|
99
|
+
}
|
|
100
|
+
const host = input.address.address === "::" || input.address.address === "0.0.0.0" ? "localhost" : input.address.address;
|
|
101
|
+
const bracketed = host.includes(":") ? `[${host}]` : host;
|
|
102
|
+
return `http://${bracketed}:${String(input.address.port)}${base}/${path}`;
|
|
103
|
+
};
|
|
104
|
+
const pathnameOf = (url) => {
|
|
105
|
+
try {
|
|
106
|
+
return new URL(url, "http://localhost").pathname.replace(TRAILING_SLASH, "");
|
|
107
|
+
} catch {
|
|
108
|
+
return url;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const createStudioHandler = (server, isNonLoopbackBind) => {
|
|
112
|
+
let assets;
|
|
113
|
+
let assetsStamp;
|
|
114
|
+
let html;
|
|
115
|
+
const projectRoot = server.config.root ?? process.cwd();
|
|
116
|
+
const serveStaticAsset = (pathname, response) => {
|
|
117
|
+
const stamp = studioAssetsStamp();
|
|
118
|
+
if (assets === void 0 || stamp !== assetsStamp) {
|
|
119
|
+
assets = loadStudioAssets(server.config.logger);
|
|
120
|
+
assetsStamp = stamp;
|
|
121
|
+
}
|
|
122
|
+
if (assets === void 0) {
|
|
123
|
+
response.statusCode = 501;
|
|
124
|
+
response.setHeader("Content-Type", "text/plain");
|
|
125
|
+
response.end("Lunora studio assets not found — install and build @lunora/studio.");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const isScript = pathname === STUDIO_SCRIPT_PATH;
|
|
129
|
+
sendOk(response, isScript ? assets.script : assets.styles, isScript ? "text/javascript; charset=utf-8" : "text/css; charset=utf-8");
|
|
130
|
+
};
|
|
131
|
+
return (request, response, next) => {
|
|
132
|
+
const pathname = pathnameOf(request.url ?? "");
|
|
133
|
+
if (pathname !== STUDIO_PATH && !pathname.startsWith(`${STUDIO_PATH}/`)) {
|
|
134
|
+
next();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (isNonLoopbackBind || transportRejectionReason(request) !== void 0) {
|
|
138
|
+
response.statusCode = 403;
|
|
139
|
+
response.setHeader("Content-Type", "text/plain");
|
|
140
|
+
response.end("Lunora studio is only available on loopback hosts in dev.");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (STATE_CHANGING_ENDPOINTS.has(pathname)) {
|
|
144
|
+
const csrf = csrfRejectionReason(request);
|
|
145
|
+
if (csrf !== void 0) {
|
|
146
|
+
response.statusCode = 403;
|
|
147
|
+
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
148
|
+
response.end(JSON.stringify({ error: csrf, ok: false }));
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const jsonHandler = JSON_ENDPOINT_HANDLERS[pathname];
|
|
153
|
+
if (jsonHandler !== void 0) {
|
|
154
|
+
serveJsonHandler(request, response, jsonHandler, projectRoot);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (pathname === STUDIO_SCRIPT_PATH || pathname === STUDIO_STYLE_PATH) {
|
|
158
|
+
serveStaticAsset(pathname, response);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
html ??= renderStudioHtml({
|
|
162
|
+
adminToken: resolveAdminToken(projectRoot),
|
|
163
|
+
basePath: STUDIO_PATH,
|
|
164
|
+
// Loopback-only dev route (it 403s on a non-loopback bind), so the
|
|
165
|
+
// developer owns the data — let them edit rows, run-as a user, and edit
|
|
166
|
+
// the schema by default.
|
|
167
|
+
dataEditable: true,
|
|
168
|
+
rulesInstalled: detectAgentRules(projectRoot).installed,
|
|
169
|
+
runAsIdentity: true,
|
|
170
|
+
schemaEditable: true,
|
|
171
|
+
scriptSrc: STUDIO_SCRIPT_PATH,
|
|
172
|
+
styleHref: STUDIO_STYLE_PATH
|
|
173
|
+
});
|
|
174
|
+
sendOk(response, html, "text/html; charset=utf-8");
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
const studioPlugin = () => {
|
|
178
|
+
return {
|
|
179
|
+
apply: "serve",
|
|
180
|
+
configureServer(server) {
|
|
181
|
+
const configuredHost = server.config.server?.host;
|
|
182
|
+
const isNonLoopbackBind = configuredHost !== void 0 && configuredHost !== false && configuredHost !== "localhost" && configuredHost !== "127.0.0.1" && configuredHost !== "::1";
|
|
183
|
+
server.middlewares.use(createStudioHandler(server, isNonLoopbackBind));
|
|
184
|
+
return () => {
|
|
185
|
+
const announce = () => {
|
|
186
|
+
const url = buildStudioUrl({
|
|
187
|
+
address: server.httpServer?.address() ?? void 0,
|
|
188
|
+
base: server.config.base,
|
|
189
|
+
resolvedLocal: server.resolvedUrls?.local[0]
|
|
190
|
+
});
|
|
191
|
+
server.config.logger.info(` \x1B[32m➜\x1B[39m \x1B[1mLunora\x1B[22m: \x1B[36m${url}\x1B[39m`);
|
|
192
|
+
};
|
|
193
|
+
if (typeof server.printUrls === "function") {
|
|
194
|
+
const printUrls = server.printUrls.bind(server);
|
|
195
|
+
server.printUrls = () => {
|
|
196
|
+
printUrls();
|
|
197
|
+
announce();
|
|
198
|
+
};
|
|
199
|
+
} else if (server.httpServer?.listening === true) {
|
|
200
|
+
announce();
|
|
201
|
+
} else {
|
|
202
|
+
server.httpServer?.once("listening", announce);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
},
|
|
206
|
+
name: "lunora:studio"
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
export { STUDIO_PATH, STUDIO_SCRIPT_PATH, STUDIO_STYLE_PATH, buildStudioUrl, studioPlugin };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const WORKER_STARTUP_HINT = [
|
|
2
|
+
"",
|
|
3
|
+
" ┌─ Lunora ──────────────────────────────────────────────────────────────",
|
|
4
|
+
" │ Your Worker entry threw while loading, so the dev server couldn't read",
|
|
5
|
+
" │ its exports. The TypeError above comes from inside the Cloudflare",
|
|
6
|
+
" │ runtime and hides the file that actually failed. It is almost always:",
|
|
7
|
+
" │",
|
|
8
|
+
" │ • A circular import in lunora/. A query/mutation/action module ran at",
|
|
9
|
+
" │ the top level before `v`/`query`/`mutation` finished initializing,",
|
|
10
|
+
" │ so they were `undefined` (hence `reading 'string'`/`'id'`/…).",
|
|
11
|
+
" │ → Import `v`/`query`/`mutation` only from `lunora/_generated/server`,",
|
|
12
|
+
" │ and don't import one lunora/ function module from another at the",
|
|
13
|
+
" │ top level.",
|
|
14
|
+
" │",
|
|
15
|
+
" │ • Stale or missing generated files.",
|
|
16
|
+
" │ → Re-run `lunora codegen`, then restart the dev server.",
|
|
17
|
+
" │",
|
|
18
|
+
" │ Tip: check the lunora/ files you edited most recently — the throw is at",
|
|
19
|
+
" │ their module top level.",
|
|
20
|
+
" └───────────────────────────────────────────────────────────────────────"
|
|
21
|
+
].join("\n");
|
|
22
|
+
const RUNNER_WORKER_RE = /runner-worker[/\\]index\.js/u;
|
|
23
|
+
const EXPORT_TYPES_PROBE_RE = /getWorkerEntryExportTypes/u;
|
|
24
|
+
const isWorkerEntryEvalError = (error) => {
|
|
25
|
+
if (!(error instanceof Error)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const haystack = `${error.stack ?? ""}
|
|
29
|
+
${error.message}`;
|
|
30
|
+
return RUNNER_WORKER_RE.test(haystack) || EXPORT_TYPES_PROBE_RE.test(haystack);
|
|
31
|
+
};
|
|
32
|
+
const HINTED = /* @__PURE__ */ Symbol.for("lunora.workerStartupHintApplied");
|
|
33
|
+
const augmentWorkerStartupError = (error) => {
|
|
34
|
+
if (!isWorkerEntryEvalError(error)) {
|
|
35
|
+
return error;
|
|
36
|
+
}
|
|
37
|
+
const flagged = error;
|
|
38
|
+
if (flagged[HINTED]) {
|
|
39
|
+
return error;
|
|
40
|
+
}
|
|
41
|
+
if (Object.isFrozen(flagged) || Object.isSealed(flagged)) {
|
|
42
|
+
return error;
|
|
43
|
+
}
|
|
44
|
+
flagged[HINTED] = true;
|
|
45
|
+
flagged.message = `${flagged.message}
|
|
46
|
+
${WORKER_STARTUP_HINT}`;
|
|
47
|
+
if (typeof flagged.stack === "string") {
|
|
48
|
+
flagged.stack = `${flagged.stack}
|
|
49
|
+
${WORKER_STARTUP_HINT}`;
|
|
50
|
+
}
|
|
51
|
+
return error;
|
|
52
|
+
};
|
|
53
|
+
const wrapHookFunction = (function_) => async (...arguments_) => {
|
|
54
|
+
try {
|
|
55
|
+
return await function_(...arguments_);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
throw augmentWorkerStartupError(error);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const wrapHook = (hook) => {
|
|
61
|
+
if (typeof hook === "function") {
|
|
62
|
+
return wrapHookFunction(hook);
|
|
63
|
+
}
|
|
64
|
+
if (hook !== null && typeof hook === "object" && "handler" in hook && typeof hook["handler"] === "function") {
|
|
65
|
+
return { ...hook, handler: wrapHookFunction(hook.handler) };
|
|
66
|
+
}
|
|
67
|
+
return hook;
|
|
68
|
+
};
|
|
69
|
+
const WRAPPED_HOOKS = ["configureServer", "buildStart"];
|
|
70
|
+
const withWorkerStartupHint = (plugins) => plugins.map((plugin) => {
|
|
71
|
+
let next = plugin;
|
|
72
|
+
for (const hookName of WRAPPED_HOOKS) {
|
|
73
|
+
const hook = plugin[hookName];
|
|
74
|
+
if (hook !== void 0) {
|
|
75
|
+
next = { ...next, [hookName]: wrapHook(hook) };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return next;
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export { WORKER_STARTUP_HINT, augmentWorkerStartupError, isWorkerEntryEvalError, withWorkerStartupHint };
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { resolve, sep, join } from 'node:path';
|
|
3
|
+
import { createCodegenProject, refreshCodegenProject, runCodegen, CodegenDiagnosticError } from '@lunora/codegen';
|
|
4
|
+
import { inferLunoraBindings, reconcileWranglerBindings } from '@lunora/config';
|
|
5
|
+
import { reconcileWranglerCrons } from './reconcileWranglerCrons-PxGwfCp_.mjs';
|
|
6
|
+
import { L as LUNORA_TAG, a as advisoryLine } from './log-BjO9EWah.mjs';
|
|
7
|
+
|
|
8
|
+
const DEBOUNCE_MS = 100;
|
|
9
|
+
const TSCONFIG_VARIANT_RE = /[/\\]tsconfig\..+\.json$/u;
|
|
10
|
+
const formatExportGapOverlay = (gaps) => {
|
|
11
|
+
const lines = gaps.map((gap) => ` • ${gap.kind} "${gap.exportName}" — class ${gap.className} is not exported by your worker entry.`);
|
|
12
|
+
const hints = [...new Set(gaps.map((gap) => gap.module))].map((module) => ` export * from "./lunora/_generated/${module}";`);
|
|
13
|
+
return [
|
|
14
|
+
`[lunora] ${String(gaps.length)} declared ${gaps.length === 1 ? "binding is" : "bindings are"} not exported by your worker entry — \`wrangler deploy\` will fail.`,
|
|
15
|
+
...lines,
|
|
16
|
+
"",
|
|
17
|
+
"Add to your worker entry:",
|
|
18
|
+
...hints
|
|
19
|
+
].join("\n");
|
|
20
|
+
};
|
|
21
|
+
const reconcileBindingsSafely = async (options, logger, onExportGaps) => {
|
|
22
|
+
try {
|
|
23
|
+
const inferred = await inferLunoraBindings({ projectRoot: options.projectRoot, schemaDir: options.schemaDir });
|
|
24
|
+
const reconciled = reconcileWranglerBindings(options.projectRoot, inferred);
|
|
25
|
+
if (reconciled.changed) {
|
|
26
|
+
logger.info?.(`${LUNORA_TAG} inferred bindings → ${reconciled.added.join(", ")} (written to ${reconciled.wranglerPath ?? "wrangler.jsonc"})`);
|
|
27
|
+
}
|
|
28
|
+
for (const warning of reconciled.warnings) {
|
|
29
|
+
logger.warn(`${LUNORA_TAG} ${warning}`);
|
|
30
|
+
}
|
|
31
|
+
if (reconciled.exportGaps.length > 0) {
|
|
32
|
+
onExportGaps?.(reconciled.exportGaps);
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
36
|
+
logger.warn(`${LUNORA_TAG} binding inference skipped: ${message}`);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const runCodegenSafely = (options, logger, overlay, project) => {
|
|
40
|
+
const schemaPath = join(options.projectRoot, options.schemaDir, "schema.ts");
|
|
41
|
+
if (!existsSync(schemaPath)) {
|
|
42
|
+
logger.warn(`${LUNORA_TAG} schema.ts not found at ${schemaPath} — codegen skipped`);
|
|
43
|
+
return void 0;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const result = runCodegen({ apiSpec: options.apiSpec, lunoraDirectory: options.schemaDir, project, projectRoot: options.projectRoot });
|
|
47
|
+
try {
|
|
48
|
+
const reconciled = reconcileWranglerCrons(options.projectRoot, result.cronTriggers);
|
|
49
|
+
if (reconciled.changed) {
|
|
50
|
+
logger.info?.(
|
|
51
|
+
`${LUNORA_TAG} synced ${result.cronTriggers.length.toFixed(0)} cron trigger(s) into ${reconciled.wranglerPath ?? "wrangler.jsonc"}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
} catch (cronError) {
|
|
55
|
+
const message = cronError instanceof Error ? cronError.message : String(cronError);
|
|
56
|
+
logger.warn(`${LUNORA_TAG} cron trigger sync skipped: ${message}`);
|
|
57
|
+
}
|
|
58
|
+
for (const advisory of result.advisories) {
|
|
59
|
+
const line = advisoryLine(advisory.level, advisory.name, advisory.detail, advisory.remediation);
|
|
60
|
+
if (advisory.level === "ERROR") {
|
|
61
|
+
logger.error(line);
|
|
62
|
+
} else {
|
|
63
|
+
logger.warn(line);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return resolve(result.outputDirectory);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
69
|
+
logger.error(`${LUNORA_TAG} codegen failed: ${message}`);
|
|
70
|
+
overlay?.onError(error, message);
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const codegenPlugin = (options) => {
|
|
75
|
+
const absoluteSchemaDirectory = resolve(options.projectRoot, options.schemaDir);
|
|
76
|
+
let absoluteGeneratedDirectory = resolve(options.projectRoot, options.generatedDir);
|
|
77
|
+
let debounceTimer;
|
|
78
|
+
let devServer;
|
|
79
|
+
return {
|
|
80
|
+
async buildStart() {
|
|
81
|
+
const logger = {
|
|
82
|
+
error: (message) => {
|
|
83
|
+
console.error(message);
|
|
84
|
+
},
|
|
85
|
+
info: (message) => {
|
|
86
|
+
console.info(message);
|
|
87
|
+
},
|
|
88
|
+
warn: (message) => {
|
|
89
|
+
console.warn(message);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const outputDirectory = runCodegenSafely(options, logger);
|
|
93
|
+
if (outputDirectory !== void 0) {
|
|
94
|
+
absoluteGeneratedDirectory = outputDirectory;
|
|
95
|
+
}
|
|
96
|
+
await reconcileBindingsSafely(options, logger, (gaps) => {
|
|
97
|
+
devServer?.hot.send({
|
|
98
|
+
err: { loc: void 0, message: formatExportGapOverlay(gaps), stack: "" },
|
|
99
|
+
type: "error"
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
configureServer(server) {
|
|
104
|
+
devServer = server;
|
|
105
|
+
server.watcher.add(absoluteSchemaDirectory);
|
|
106
|
+
const serverLogger = {
|
|
107
|
+
error: (message) => {
|
|
108
|
+
server.config.logger.error(message);
|
|
109
|
+
},
|
|
110
|
+
info: (message) => {
|
|
111
|
+
server.config.logger.info(message);
|
|
112
|
+
},
|
|
113
|
+
warn: (message) => {
|
|
114
|
+
server.config.logger.warn(message);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const overlay = {
|
|
118
|
+
onError(error, message) {
|
|
119
|
+
const loc = error instanceof CodegenDiagnosticError ? { column: error.column, file: error.file, line: error.line } : void 0;
|
|
120
|
+
const overlayMessage = loc === void 0 ? `[lunora] codegen failed: ${message}
|
|
121
|
+
(see terminal for full stack trace and file location)` : `[lunora] codegen failed: ${message}`;
|
|
122
|
+
devServer?.hot.send({
|
|
123
|
+
err: {
|
|
124
|
+
loc,
|
|
125
|
+
message: overlayMessage,
|
|
126
|
+
stack: error instanceof Error ? error.stack ?? "" : ""
|
|
127
|
+
},
|
|
128
|
+
type: "error"
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const isInside = (path, directory) => path === directory || path.startsWith(directory + sep);
|
|
133
|
+
let closed = false;
|
|
134
|
+
let cachedProject;
|
|
135
|
+
const invalidateGenerated = () => {
|
|
136
|
+
const environments = server.environments;
|
|
137
|
+
const graphs = [];
|
|
138
|
+
if (environments !== void 0) {
|
|
139
|
+
for (const environment of Object.values(environments)) {
|
|
140
|
+
if (environment.moduleGraph !== void 0) {
|
|
141
|
+
graphs.push(environment.moduleGraph);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (graphs.length === 0) {
|
|
146
|
+
graphs.push(server.moduleGraph);
|
|
147
|
+
}
|
|
148
|
+
for (const graph of graphs) {
|
|
149
|
+
for (const moduleEntry of graph.idToModuleMap.values()) {
|
|
150
|
+
if (moduleEntry.id && isInside(moduleEntry.id, absoluteGeneratedDirectory)) {
|
|
151
|
+
graph.invalidateModule(moduleEntry);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const onChange = (file) => {
|
|
157
|
+
const normalized = resolve(file);
|
|
158
|
+
if (!isInside(normalized, absoluteSchemaDirectory)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (isInside(normalized, absoluteGeneratedDirectory)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (normalized.endsWith(`${sep}tsconfig.json`) || TSCONFIG_VARIANT_RE.test(normalized)) {
|
|
165
|
+
cachedProject = void 0;
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (!normalized.endsWith(".ts")) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (normalized.includes(`${sep}__tests__${sep}`) || normalized.endsWith(".test.ts") || normalized.endsWith(".spec.ts")) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (debounceTimer) {
|
|
175
|
+
clearTimeout(debounceTimer);
|
|
176
|
+
}
|
|
177
|
+
debounceTimer = setTimeout(() => {
|
|
178
|
+
debounceTimer = void 0;
|
|
179
|
+
if (closed) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (cachedProject === void 0) {
|
|
183
|
+
cachedProject = createCodegenProject(absoluteSchemaDirectory);
|
|
184
|
+
} else {
|
|
185
|
+
refreshCodegenProject(cachedProject, absoluteSchemaDirectory);
|
|
186
|
+
}
|
|
187
|
+
const outputDirectory = runCodegenSafely(options, serverLogger, overlay, cachedProject);
|
|
188
|
+
if (outputDirectory === void 0) {
|
|
189
|
+
cachedProject = void 0;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
absoluteGeneratedDirectory = outputDirectory;
|
|
193
|
+
invalidateGenerated();
|
|
194
|
+
server.hot.send({ type: "full-reload" });
|
|
195
|
+
}, DEBOUNCE_MS);
|
|
196
|
+
};
|
|
197
|
+
server.watcher.on("add", onChange);
|
|
198
|
+
server.watcher.on("change", onChange);
|
|
199
|
+
server.watcher.on("unlink", onChange);
|
|
200
|
+
return () => {
|
|
201
|
+
server.httpServer?.once("close", () => {
|
|
202
|
+
closed = true;
|
|
203
|
+
cachedProject = void 0;
|
|
204
|
+
if (debounceTimer) {
|
|
205
|
+
clearTimeout(debounceTimer);
|
|
206
|
+
debounceTimer = void 0;
|
|
207
|
+
}
|
|
208
|
+
server.watcher.off("add", onChange);
|
|
209
|
+
server.watcher.off("change", onChange);
|
|
210
|
+
server.watcher.off("unlink", onChange);
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
name: "lunora:codegen"
|
|
215
|
+
};
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export { codegenPlugin as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ensureDevVariables, createConfirm } from '@lunora/config';
|
|
2
|
+
import { l as lunoraLine } from './log-BjO9EWah.mjs';
|
|
3
|
+
|
|
4
|
+
const devVariablesPlugin = (options) => {
|
|
5
|
+
return {
|
|
6
|
+
apply: "serve",
|
|
7
|
+
async configResolved() {
|
|
8
|
+
await ensureDevVariables({
|
|
9
|
+
confirm: createConfirm("[lunora] "),
|
|
10
|
+
cwd: options.projectRoot,
|
|
11
|
+
info: (message) => {
|
|
12
|
+
console.info(lunoraLine(message));
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
enforce: "pre",
|
|
17
|
+
name: "lunora:dev-vars"
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { devVariablesPlugin as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { paintBadge, BADGES } from '@lunora/config';
|
|
2
|
+
|
|
3
|
+
const LUNORA_TAG = paintBadge(BADGES.lunora);
|
|
4
|
+
const lunoraLine = (message) => `${LUNORA_TAG} ${message}`;
|
|
5
|
+
const ADVISORY_BADGE = { ERROR: BADGES.error, INFO: BADGES.info, WARN: BADGES.warn };
|
|
6
|
+
const advisoryLine = (level, name, detail, remediation) => `${paintBadge(ADVISORY_BADGE[level])} ${name}: ${detail} — ${remediation}`;
|
|
7
|
+
|
|
8
|
+
export { LUNORA_TAG as L, advisoryLine as a, lunoraLine as l };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { LUNORA_EVENT_SOURCE, formatLunoraEvent } from '@lunora/config';
|
|
2
|
+
|
|
3
|
+
const LUNORA_MARKER = `"source":"${LUNORA_EVENT_SOURCE}"`;
|
|
4
|
+
const ANSI = {
|
|
5
|
+
error: "\x1B[31m",
|
|
6
|
+
info: "\x1B[36m",
|
|
7
|
+
reset: "\x1B[0m",
|
|
8
|
+
warn: "\x1B[33m"
|
|
9
|
+
};
|
|
10
|
+
const decorate = (text, level, colour) => colour ? `${ANSI[level]}[lunora]${ANSI.reset} ${text}` : `[lunora] ${text}`;
|
|
11
|
+
const chunkToText = (chunk) => {
|
|
12
|
+
if (typeof chunk === "string") {
|
|
13
|
+
return chunk;
|
|
14
|
+
}
|
|
15
|
+
return Buffer.isBuffer(chunk) ? chunk.toString("utf8") : void 0;
|
|
16
|
+
};
|
|
17
|
+
const rewriteChunk = (text, colour) => text.split("\n").map((segment) => {
|
|
18
|
+
const formatted = formatLunoraEvent(segment);
|
|
19
|
+
return formatted ? decorate(formatted.text, formatted.level, colour) : segment;
|
|
20
|
+
}).join("\n");
|
|
21
|
+
const wrapWrite = (original, colour) => (...args) => {
|
|
22
|
+
try {
|
|
23
|
+
const text = chunkToText(args[0]);
|
|
24
|
+
if (text?.includes(LUNORA_MARKER)) {
|
|
25
|
+
return original(rewriteChunk(text, colour), ...args.slice(1));
|
|
26
|
+
}
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
return original(...args);
|
|
30
|
+
};
|
|
31
|
+
const patchStream = (stream) => {
|
|
32
|
+
const original = stream.write.bind(stream);
|
|
33
|
+
stream.write = wrapWrite(original, stream.isTTY === true);
|
|
34
|
+
return () => {
|
|
35
|
+
stream.write = original;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const logStreamPlugin = () => {
|
|
39
|
+
let restore;
|
|
40
|
+
return {
|
|
41
|
+
apply: "serve",
|
|
42
|
+
configureServer(server) {
|
|
43
|
+
if (restore) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const restoreStdout = patchStream(process.stdout);
|
|
47
|
+
const restoreStderr = patchStream(process.stderr);
|
|
48
|
+
restore = () => {
|
|
49
|
+
restoreStdout();
|
|
50
|
+
restoreStderr();
|
|
51
|
+
restore = void 0;
|
|
52
|
+
};
|
|
53
|
+
server.httpServer?.once("close", () => {
|
|
54
|
+
restore?.();
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
name: "lunora:log-stream"
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { logStreamPlugin as default };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { resolveRemoteEnabled, readProjectRemotePreference, materializeRemoteWranglerConfig } from '@lunora/config';
|
|
2
|
+
|
|
3
|
+
const noopCleanup = () => {
|
|
4
|
+
};
|
|
5
|
+
const planViteRemoteBindings = (options) => {
|
|
6
|
+
const readPreference = options.readPreference ?? readProjectRemotePreference;
|
|
7
|
+
const enabled = resolveRemoteEnabled({
|
|
8
|
+
configPreference: readPreference(options.projectRoot),
|
|
9
|
+
envValue: options.remoteEnv ?? process.env["LUNORA_REMOTE"]
|
|
10
|
+
});
|
|
11
|
+
if (!enabled) {
|
|
12
|
+
return { cleanup: noopCleanup, enabled: false };
|
|
13
|
+
}
|
|
14
|
+
const materialize = options.materialize ?? materializeRemoteWranglerConfig;
|
|
15
|
+
const result = materialize({ enabled: true, projectRoot: options.projectRoot });
|
|
16
|
+
return {
|
|
17
|
+
// The materializer always returns an idempotent, never-throwing `cleanup`.
|
|
18
|
+
cleanup: result.cleanup,
|
|
19
|
+
configPath: result.configPath,
|
|
20
|
+
enabled: true,
|
|
21
|
+
reason: result.reason
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
const withRemoteBindings = (options, isServe, plan) => {
|
|
25
|
+
if (!plan.enabled || plan.configPath === void 0) {
|
|
26
|
+
return options;
|
|
27
|
+
}
|
|
28
|
+
const existing = options;
|
|
29
|
+
if (typeof existing.configPath === "string") {
|
|
30
|
+
return options;
|
|
31
|
+
}
|
|
32
|
+
if (!isServe()) {
|
|
33
|
+
return options;
|
|
34
|
+
}
|
|
35
|
+
return { ...options, configPath: plan.configPath };
|
|
36
|
+
};
|
|
37
|
+
const remoteBindingsCleanupPlugin = (cleanup) => {
|
|
38
|
+
return {
|
|
39
|
+
buildEnd() {
|
|
40
|
+
cleanup();
|
|
41
|
+
},
|
|
42
|
+
closeBundle() {
|
|
43
|
+
cleanup();
|
|
44
|
+
},
|
|
45
|
+
enforce: "pre",
|
|
46
|
+
name: "lunora:remote-bindings-cleanup"
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { planViteRemoteBindings, remoteBindingsCleanupPlugin, withRemoteBindings };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { writeFileSync } from 'node:fs';
|
|
2
|
+
import { findWranglerFile, readWranglerJsonc } from '@lunora/config';
|
|
3
|
+
import { modify, applyEdits } from 'jsonc-parser';
|
|
4
|
+
|
|
5
|
+
const sameTriggers = (a, b) => a.length === b.length && a.every((value, index) => value === b[index]);
|
|
6
|
+
const reconcileWranglerCrons = (projectRoot, cronTriggers) => {
|
|
7
|
+
const wranglerPath = findWranglerFile(projectRoot);
|
|
8
|
+
if (!wranglerPath) {
|
|
9
|
+
return { changed: false, reason: "wrangler.jsonc not found" };
|
|
10
|
+
}
|
|
11
|
+
const { parsed, text } = readWranglerJsonc(wranglerPath);
|
|
12
|
+
if (parsed === void 0) {
|
|
13
|
+
return { changed: false, reason: `failed to parse ${wranglerPath} as JSONC`, wranglerPath };
|
|
14
|
+
}
|
|
15
|
+
const existing = Array.isArray(parsed.triggers?.crons) ? parsed.triggers.crons.filter((value) => typeof value === "string") : [];
|
|
16
|
+
if (sameTriggers(existing, cronTriggers)) {
|
|
17
|
+
return { changed: false, reason: "triggers.crons already in sync", wranglerPath };
|
|
18
|
+
}
|
|
19
|
+
const edits = modify(text, ["triggers", "crons"], [...cronTriggers], {
|
|
20
|
+
formattingOptions: { insertSpaces: true, tabSize: 4 }
|
|
21
|
+
});
|
|
22
|
+
if (edits.length === 0) {
|
|
23
|
+
return { changed: false, reason: "no structural edit produced", wranglerPath };
|
|
24
|
+
}
|
|
25
|
+
writeFileSync(wranglerPath, applyEdits(text, edits), "utf8");
|
|
26
|
+
return { changed: true, wranglerPath };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { reconcileWranglerCrons };
|