@mostra/runtime 0.0.0-placeholder → 0.1.0
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 +202 -0
- package/README.md +38 -1
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -4
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +739 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/index.ts +1209 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +942 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1009 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +509 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The builder child's static scan of an artifact (INTERFACES §11): its `"use server"` modules with
|
|
3
|
+
* their exports and `dryRun()` opt-ins (D54, D55), the live sources of `live.ts` (D56) and the
|
|
4
|
+
* routes of `pages/` (D57). Parsing only; nothing in the artifact runs.
|
|
5
|
+
*/
|
|
6
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
7
|
+
import { extname, join } from "node:path";
|
|
8
|
+
import { createDiagnostic, type Diagnostic } from "@mostra/protocol";
|
|
9
|
+
import { parseSync } from "oxc-parser";
|
|
10
|
+
import type { FieldCoverage } from "../coverage.ts";
|
|
11
|
+
import type { PageRoute, ServerFunctionEntry } from "../types.ts";
|
|
12
|
+
import { scanFields } from "./fields.ts";
|
|
13
|
+
|
|
14
|
+
interface Node {
|
|
15
|
+
readonly type: string;
|
|
16
|
+
readonly start: number;
|
|
17
|
+
readonly end: number;
|
|
18
|
+
readonly [key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Every script extension the bundler loads; a "use server" module may have any of them. */
|
|
22
|
+
export const SCRIPT: ReadonlySet<string> = new Set([
|
|
23
|
+
".ts",
|
|
24
|
+
".tsx",
|
|
25
|
+
".mts",
|
|
26
|
+
".cts",
|
|
27
|
+
".js",
|
|
28
|
+
".jsx",
|
|
29
|
+
".mjs",
|
|
30
|
+
".cjs",
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
/** Oxc's language for a script file. */
|
|
34
|
+
function langOf(file: string): "ts" | "tsx" | "js" | "jsx" {
|
|
35
|
+
const ext = extname(file);
|
|
36
|
+
if (ext === ".tsx") return "tsx";
|
|
37
|
+
if (ext === ".jsx") return "jsx";
|
|
38
|
+
if (ext === ".ts" || ext === ".mts" || ext === ".cts") return "ts";
|
|
39
|
+
return "js";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether a script starts with the `"use server"` directive. `undefined` when it doesn't parse:
|
|
44
|
+
* the bundler, which parses the same way, then reports the syntax error.
|
|
45
|
+
*/
|
|
46
|
+
export function startsWithUseServer(file: string, text: string): boolean | undefined {
|
|
47
|
+
if (!text.includes("use server")) return false;
|
|
48
|
+
const parsed = parseSync(file, text, { lang: langOf(file) });
|
|
49
|
+
if (parsed.errors.length > 0) return undefined;
|
|
50
|
+
return isServerModule(parsed.program as unknown as Node);
|
|
51
|
+
}
|
|
52
|
+
const PAGE = new Set([".tsx", ".jsx", ".mdx"]);
|
|
53
|
+
const SERVER_PACKAGE = "@mostra/server";
|
|
54
|
+
|
|
55
|
+
/** What the scan found. */
|
|
56
|
+
export interface ScanResult {
|
|
57
|
+
/** `"use server"` modules (paths inside the artifact) with their exports. */
|
|
58
|
+
readonly serverModules: ReadonlyMap<string, readonly ServerFunctionEntry[]>;
|
|
59
|
+
readonly liveSources: readonly { readonly key: string; readonly always?: boolean }[];
|
|
60
|
+
readonly hasLive: boolean;
|
|
61
|
+
readonly pages: readonly PageRoute[];
|
|
62
|
+
/** The field IDs the artifact's own code can use (D109). */
|
|
63
|
+
readonly fields: FieldCoverage;
|
|
64
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
65
|
+
/**
|
|
66
|
+
* Server-code candidates (a script naming `"use server"`, or `live.ts`) that don't parse. A
|
|
67
|
+
* build's bundler reports them with full context; `mostra dev`, which bundles server code only
|
|
68
|
+
* from what the scan found, reports them itself.
|
|
69
|
+
*/
|
|
70
|
+
readonly unparsed: readonly Diagnostic[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function walk(dir: string, prefix: string, out: string[]): Promise<void> {
|
|
74
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
75
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
76
|
+
if (entry.isDirectory()) await walk(join(dir, entry.name), path, out);
|
|
77
|
+
else if (entry.isFile()) out.push(path);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function lineColumn(text: string, offset: number): { line: number; column: number } {
|
|
82
|
+
const before = text.slice(0, offset);
|
|
83
|
+
const line = before.split("\n").length;
|
|
84
|
+
return { line, column: offset - before.lastIndexOf("\n") };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Calls each child node of `node`. */
|
|
88
|
+
function children(node: Node, visit: (child: Node, parent: Node, key: string) => void): void {
|
|
89
|
+
for (const [key, value] of Object.entries(node)) {
|
|
90
|
+
if (Array.isArray(value)) {
|
|
91
|
+
for (const item of value)
|
|
92
|
+
if (item && typeof item === "object" && "type" in item) visit(item as Node, node, key);
|
|
93
|
+
} else if (value && typeof value === "object" && "type" in value) {
|
|
94
|
+
visit(value as Node, node, key);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Whether the module starts with the `"use server"` directive. */
|
|
100
|
+
function isServerModule(program: Node): boolean {
|
|
101
|
+
for (const statement of program["body"] as Node[]) {
|
|
102
|
+
if (statement.type !== "ExpressionStatement" || typeof statement["directive"] !== "string")
|
|
103
|
+
return false;
|
|
104
|
+
if (statement["directive"] === "use server") return true;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** TypeScript wrappers that compile away: `as`, `satisfies`, `!`, `<T>x` and parentheses. */
|
|
110
|
+
const ERASED_WRAPPERS = new Set([
|
|
111
|
+
"TSAsExpression",
|
|
112
|
+
"TSSatisfiesExpression",
|
|
113
|
+
"TSNonNullExpression",
|
|
114
|
+
"TSTypeAssertion",
|
|
115
|
+
"TSInstantiationExpression",
|
|
116
|
+
"ParenthesizedExpression",
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
/** The expression a wrapper chain wraps: `(async () => 1) satisfies F` is the arrow function. */
|
|
120
|
+
function unwrap(node: Node | undefined): Node | undefined {
|
|
121
|
+
let current = node;
|
|
122
|
+
while (current !== undefined && ERASED_WRAPPERS.has(current.type)) {
|
|
123
|
+
current = current["expression"] as Node | undefined;
|
|
124
|
+
}
|
|
125
|
+
return current;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const isAsyncFunction = (node: Node | undefined): boolean => {
|
|
129
|
+
const target = unwrap(node);
|
|
130
|
+
return (
|
|
131
|
+
target !== undefined &&
|
|
132
|
+
(target.type === "FunctionDeclaration" ||
|
|
133
|
+
target.type === "FunctionExpression" ||
|
|
134
|
+
target.type === "ArrowFunctionExpression") &&
|
|
135
|
+
target["async"] === true
|
|
136
|
+
);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Declarations that exist only for the type checker: types, interfaces, overload signatures and
|
|
141
|
+
* anything `declare`d. Enums and namespaces are values at run time, so they are not among them.
|
|
142
|
+
*/
|
|
143
|
+
const isErasedDeclaration = (node: Node): boolean =>
|
|
144
|
+
node["declare"] === true ||
|
|
145
|
+
node.type === "TSTypeAliasDeclaration" ||
|
|
146
|
+
node.type === "TSInterfaceDeclaration" ||
|
|
147
|
+
node.type === "TSDeclareFunction";
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Reads one `"use server"` module: every export must be an async function (React's contract),
|
|
151
|
+
* and an export opts into `verify`'s smoke test when it, or a module-level function it calls,
|
|
152
|
+
* calls `dryRun()` from `@mostra/server` (D55).
|
|
153
|
+
*/
|
|
154
|
+
function serverExports(
|
|
155
|
+
module: string,
|
|
156
|
+
text: string,
|
|
157
|
+
program: Node,
|
|
158
|
+
diagnostics: Diagnostic[],
|
|
159
|
+
): ServerFunctionEntry[] {
|
|
160
|
+
const dryRunNames = new Set<string>();
|
|
161
|
+
const namespaces = new Set<string>();
|
|
162
|
+
const functions = new Map<string, Node>();
|
|
163
|
+
const exported: { name: string; local: Node | undefined; at: number }[] = [];
|
|
164
|
+
const invalid = (at: number, message: string): void => {
|
|
165
|
+
diagnostics.push(
|
|
166
|
+
createDiagnostic("MOSTRA_ACTION_EXPORT_INVALID", {
|
|
167
|
+
message,
|
|
168
|
+
location: { file: module, ...lineColumn(text, at) },
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const remember = (declaration: Node): void => {
|
|
174
|
+
if (declaration.type === "FunctionDeclaration" && declaration["id"]) {
|
|
175
|
+
functions.set((declaration["id"] as Node)["name"] as string, declaration);
|
|
176
|
+
}
|
|
177
|
+
if (declaration.type === "VariableDeclaration") {
|
|
178
|
+
for (const declarator of declaration["declarations"] as Node[]) {
|
|
179
|
+
const id = declarator["id"] as Node;
|
|
180
|
+
if (id.type === "Identifier" && declarator["init"]) {
|
|
181
|
+
functions.set(id["name"] as string, declarator["init"] as Node);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
for (const statement of program["body"] as Node[]) {
|
|
188
|
+
if (
|
|
189
|
+
statement.type === "ImportDeclaration" &&
|
|
190
|
+
(statement["source"] as Node)["value"] === SERVER_PACKAGE
|
|
191
|
+
) {
|
|
192
|
+
for (const specifier of statement["specifiers"] as Node[]) {
|
|
193
|
+
const local = (specifier["local"] as Node)["name"] as string;
|
|
194
|
+
if (specifier.type === "ImportNamespaceSpecifier") namespaces.add(local);
|
|
195
|
+
const imported = specifier["imported"] as Node | undefined;
|
|
196
|
+
if (
|
|
197
|
+
specifier.type === "ImportSpecifier" &&
|
|
198
|
+
(imported?.["name"] ?? imported?.["value"]) === "dryRun"
|
|
199
|
+
) {
|
|
200
|
+
dryRunNames.add(local);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} else if (statement.type === "ExportNamedDeclaration") {
|
|
204
|
+
if (statement["exportKind"] === "type") continue;
|
|
205
|
+
if (statement["source"]) {
|
|
206
|
+
invalid(
|
|
207
|
+
statement.start,
|
|
208
|
+
`${module} re-exports from another module; a "use server" module exports only its own async functions.`,
|
|
209
|
+
);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const declaration = statement["declaration"] as Node | null;
|
|
213
|
+
if (declaration) {
|
|
214
|
+
if (isErasedDeclaration(declaration)) continue;
|
|
215
|
+
remember(declaration);
|
|
216
|
+
if (declaration.type === "FunctionDeclaration") {
|
|
217
|
+
const name = (declaration["id"] as Node)["name"] as string;
|
|
218
|
+
exported.push({ name, local: declaration, at: declaration.start });
|
|
219
|
+
} else if (declaration.type === "VariableDeclaration") {
|
|
220
|
+
for (const declarator of declaration["declarations"] as Node[]) {
|
|
221
|
+
const id = declarator["id"] as Node;
|
|
222
|
+
exported.push({
|
|
223
|
+
name: id.type === "Identifier" ? (id["name"] as string) : "(pattern)",
|
|
224
|
+
local: (declarator["init"] as Node | null) ?? undefined,
|
|
225
|
+
at: declarator.start,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
exported.push({ name: "(declaration)", local: declaration, at: declaration.start });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
for (const specifier of (statement["specifiers"] as Node[]) ?? []) {
|
|
233
|
+
if (specifier["exportKind"] === "type") continue;
|
|
234
|
+
const name = ((specifier["exported"] as Node)["name"] ??
|
|
235
|
+
(specifier["exported"] as Node)["value"]) as string;
|
|
236
|
+
const local = (specifier["local"] as Node)["name"] as string;
|
|
237
|
+
exported.push({
|
|
238
|
+
name,
|
|
239
|
+
local: { type: "Ref", start: specifier.start, end: specifier.end, name: local },
|
|
240
|
+
at: specifier.start,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
} else if (statement.type === "ExportDefaultDeclaration") {
|
|
244
|
+
const declaration = statement["declaration"] as Node;
|
|
245
|
+
if (declaration.type === "FunctionDeclaration" && declaration["id"]) remember(declaration);
|
|
246
|
+
exported.push({
|
|
247
|
+
name: "default",
|
|
248
|
+
local:
|
|
249
|
+
declaration.type === "Identifier"
|
|
250
|
+
? {
|
|
251
|
+
type: "Ref",
|
|
252
|
+
start: declaration.start,
|
|
253
|
+
end: declaration.end,
|
|
254
|
+
name: declaration["name"],
|
|
255
|
+
}
|
|
256
|
+
: declaration,
|
|
257
|
+
at: statement.start,
|
|
258
|
+
});
|
|
259
|
+
} else if (statement.type === "ExportAllDeclaration") {
|
|
260
|
+
invalid(
|
|
261
|
+
statement.start,
|
|
262
|
+
`${module} re-exports with \`export *\`; a "use server" module exports only its own async functions.`,
|
|
263
|
+
);
|
|
264
|
+
} else if (!isErasedDeclaration(statement)) {
|
|
265
|
+
remember(statement);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Which module-level functions call dryRun(), directly or through other module-level functions.
|
|
270
|
+
const calls = new Map<string, Set<string>>();
|
|
271
|
+
const direct = new Set<string>();
|
|
272
|
+
const scan = (node: Node, owner: string): void => {
|
|
273
|
+
if (node.type === "CallExpression") {
|
|
274
|
+
const callee = node["callee"] as Node;
|
|
275
|
+
if (callee.type === "Identifier") {
|
|
276
|
+
const name = callee["name"] as string;
|
|
277
|
+
if (dryRunNames.has(name)) direct.add(owner);
|
|
278
|
+
else calls.get(owner)?.add(name);
|
|
279
|
+
} else if (
|
|
280
|
+
callee.type === "MemberExpression" &&
|
|
281
|
+
!callee["computed"] &&
|
|
282
|
+
(callee["object"] as Node).type === "Identifier" &&
|
|
283
|
+
namespaces.has((callee["object"] as Node)["name"] as string) &&
|
|
284
|
+
(callee["property"] as Node)["name"] === "dryRun"
|
|
285
|
+
) {
|
|
286
|
+
direct.add(owner);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
children(node, (child) => scan(child, owner));
|
|
290
|
+
};
|
|
291
|
+
for (const [name, node] of functions) {
|
|
292
|
+
calls.set(name, new Set());
|
|
293
|
+
scan(node, name);
|
|
294
|
+
}
|
|
295
|
+
const optsIn = (name: string, seen = new Set<string>()): boolean => {
|
|
296
|
+
if (direct.has(name)) return true;
|
|
297
|
+
if (seen.has(name)) return false;
|
|
298
|
+
seen.add(name);
|
|
299
|
+
return [...(calls.get(name) ?? [])].some((callee) => optsIn(callee, seen));
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const entries: ServerFunctionEntry[] = [];
|
|
303
|
+
for (const { name, local, at } of exported) {
|
|
304
|
+
// Follow `export const x = y` (and `y!`, `y as F`) to the function `y` names.
|
|
305
|
+
let target = unwrap(local?.type === "Ref" ? functions.get(local["name"] as string) : local);
|
|
306
|
+
const followed = new Set<string>();
|
|
307
|
+
while (target?.type === "Identifier" && !followed.has(target["name"] as string)) {
|
|
308
|
+
followed.add(target["name"] as string);
|
|
309
|
+
target = unwrap(functions.get(target["name"] as string));
|
|
310
|
+
}
|
|
311
|
+
if (!isAsyncFunction(target)) {
|
|
312
|
+
invalid(
|
|
313
|
+
at,
|
|
314
|
+
`The export \`${name}\` of ${module} isn't an async function; a "use server" module exports only async functions.`,
|
|
315
|
+
);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
let dryRun: boolean;
|
|
319
|
+
if (local?.type === "Ref") dryRun = optsIn(local["name"] as string);
|
|
320
|
+
else {
|
|
321
|
+
const owner = `export:${name}`;
|
|
322
|
+
calls.set(owner, new Set());
|
|
323
|
+
scan(target as Node, owner);
|
|
324
|
+
dryRun = optsIn(owner);
|
|
325
|
+
}
|
|
326
|
+
entries.push({ id: `${module}#${name}`, module, export: name, dryRun });
|
|
327
|
+
}
|
|
328
|
+
return entries;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Whether a `defineLive({ … })` argument says `always: true` (D63): `true` or `false` when the
|
|
333
|
+
* object literal tells, `undefined` when it can't be known without running it (a spread, a
|
|
334
|
+
* computed key, a value other than a literal, or no object literal at all).
|
|
335
|
+
*/
|
|
336
|
+
function literalAlways(argument: Node | undefined): boolean | undefined {
|
|
337
|
+
if (argument?.type !== "ObjectExpression") return undefined;
|
|
338
|
+
let always: boolean | undefined = false;
|
|
339
|
+
for (const property of argument["properties"] as Node[]) {
|
|
340
|
+
if (property.type !== "Property" || property["computed"] === true) {
|
|
341
|
+
always = undefined;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
const key = property["key"] as Node;
|
|
345
|
+
const name = key.type === "Identifier" ? key["name"] : key["value"];
|
|
346
|
+
if (name !== "always") continue;
|
|
347
|
+
const value = property["value"] as Node;
|
|
348
|
+
always =
|
|
349
|
+
value.type === "Literal" && typeof value["value"] === "boolean"
|
|
350
|
+
? (value["value"] as boolean)
|
|
351
|
+
: undefined;
|
|
352
|
+
}
|
|
353
|
+
return always;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The exports of `live.ts` that are `defineLive(...)` calls (D56), with their literal `always`. */
|
|
357
|
+
function liveExports(program: Node): { key: string; always?: boolean }[] {
|
|
358
|
+
const keys: { key: string; always?: boolean }[] = [];
|
|
359
|
+
for (const statement of program["body"] as Node[]) {
|
|
360
|
+
if (statement.type !== "ExportNamedDeclaration") continue;
|
|
361
|
+
const declaration = statement["declaration"] as Node | null;
|
|
362
|
+
if (declaration?.type !== "VariableDeclaration") continue;
|
|
363
|
+
for (const declarator of declaration["declarations"] as Node[]) {
|
|
364
|
+
const id = declarator["id"] as Node;
|
|
365
|
+
const init = declarator["init"] as Node | null;
|
|
366
|
+
const callee = init?.type === "CallExpression" ? (init["callee"] as Node) : undefined;
|
|
367
|
+
const calleeName =
|
|
368
|
+
callee?.type === "Identifier"
|
|
369
|
+
? callee["name"]
|
|
370
|
+
: callee?.type === "MemberExpression"
|
|
371
|
+
? (callee["property"] as Node)["name"]
|
|
372
|
+
: undefined;
|
|
373
|
+
if (id.type === "Identifier" && calleeName === "defineLive") {
|
|
374
|
+
const always = literalAlways((init?.["arguments"] as Node[] | undefined)?.[0]);
|
|
375
|
+
keys.push({ key: id["name"] as string, ...(always === undefined ? {} : { always }) });
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return keys;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** `pages/reports/[id].tsx` → `/reports/:id`; `index` names its folder (D57). */
|
|
383
|
+
function routePath(file: string): string {
|
|
384
|
+
const segments = file
|
|
385
|
+
.slice("pages/".length, file.length - extname(file).length)
|
|
386
|
+
.split("/")
|
|
387
|
+
.filter((segment, index, all) => !(segment === "index" && index === all.length - 1))
|
|
388
|
+
.map((segment) => {
|
|
389
|
+
const rest = /^\[\.\.\.(.+)\]$/.exec(segment);
|
|
390
|
+
if (rest) return "*";
|
|
391
|
+
const param = /^\[(.+)\]$/.exec(segment);
|
|
392
|
+
return param ? `:${param[1]}` : segment;
|
|
393
|
+
});
|
|
394
|
+
return `/${segments.join("/")}`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Scans the snapshot in `sourceDir`. */
|
|
398
|
+
export async function scanArtifact(sourceDir: string): Promise<ScanResult> {
|
|
399
|
+
const files: string[] = [];
|
|
400
|
+
await walk(sourceDir, "", files);
|
|
401
|
+
const diagnostics: Diagnostic[] = [];
|
|
402
|
+
const serverModules = new Map<string, ServerFunctionEntry[]>();
|
|
403
|
+
const unparsed: Diagnostic[] = [];
|
|
404
|
+
let liveSources: { key: string; always?: boolean }[] = [];
|
|
405
|
+
for (const file of files.sort()) {
|
|
406
|
+
if (!SCRIPT.has(extname(file))) continue;
|
|
407
|
+
const text = await readFile(join(sourceDir, file), "utf8");
|
|
408
|
+
if (!text.includes("use server") && file !== "live.ts") continue;
|
|
409
|
+
const parsed = parseSync(file, text, { lang: langOf(file) });
|
|
410
|
+
if (parsed.errors.length > 0) {
|
|
411
|
+
// The bundler reports syntax errors with full context; dev reports them from here.
|
|
412
|
+
const first = parsed.errors[0];
|
|
413
|
+
const offset = first?.labels[0]?.start ?? 0;
|
|
414
|
+
const before = text.slice(0, offset).split("\n");
|
|
415
|
+
unparsed.push(
|
|
416
|
+
createDiagnostic("MOSTRA_BUILD_FAILED", {
|
|
417
|
+
message: `${file}: ${first?.message ?? "a syntax error"}.`,
|
|
418
|
+
location: {
|
|
419
|
+
file,
|
|
420
|
+
line: before.length,
|
|
421
|
+
column: (before.at(-1)?.length ?? 0) + 1,
|
|
422
|
+
},
|
|
423
|
+
}),
|
|
424
|
+
);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
const program = parsed.program as unknown as Node;
|
|
428
|
+
if (file === "live.ts") liveSources = liveExports(program);
|
|
429
|
+
if (isServerModule(program))
|
|
430
|
+
serverModules.set(file, serverExports(file, text, program, diagnostics));
|
|
431
|
+
}
|
|
432
|
+
const pages = files
|
|
433
|
+
.filter((file) => file.startsWith("pages/") && PAGE.has(extname(file)))
|
|
434
|
+
.sort()
|
|
435
|
+
.map((file) => ({ path: routePath(file), file }));
|
|
436
|
+
const byPath = new Map<string, string>();
|
|
437
|
+
for (const page of pages) {
|
|
438
|
+
const other = byPath.get(page.path);
|
|
439
|
+
if (other) {
|
|
440
|
+
diagnostics.push(
|
|
441
|
+
createDiagnostic("MOSTRA_BUILD_FAILED", {
|
|
442
|
+
message: `${other} and ${page.file} are both the page ${page.path}; keep one.`,
|
|
443
|
+
location: { file: page.file, line: 1, column: 1 },
|
|
444
|
+
}),
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
byPath.set(page.path, page.file);
|
|
448
|
+
}
|
|
449
|
+
const fields = await scanFields(sourceDir, files);
|
|
450
|
+
return {
|
|
451
|
+
serverModules,
|
|
452
|
+
liveSources,
|
|
453
|
+
hasLive: files.includes("live.ts"),
|
|
454
|
+
pages,
|
|
455
|
+
fields,
|
|
456
|
+
diagnostics,
|
|
457
|
+
unparsed,
|
|
458
|
+
};
|
|
459
|
+
}
|