@genroc/eval-node 0.0.0-edge.f85ea85 → 0.0.0-edge.f8ffa72f
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 +67 -33
- package/dist/eval.js +85 -0
- package/dist/import.js +377 -0
- package/dist/realm.js +113 -0
- package/dist/worker.js +305 -0
- package/eval.ts +31 -5
- package/import.ts +218 -126
- package/package.json +6 -4
- package/realm.ts +61 -77
- package/worker.ts +49 -17
- package/bin/import.mjs +0 -4
- package/bin/worker.mjs +0 -2
package/import.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
1
2
|
// The code-phase resolver: manifest on stdin, `{"code": [...]}` on stdout, non-zero exit
|
|
2
3
|
// with the diagnostic on stderr. genctl never parses TypeScript and this never parses YAML
|
|
3
4
|
// — the manifest is the whole contract. See specs/source-resolution.md.
|
|
@@ -6,11 +7,10 @@
|
|
|
6
7
|
// code; "build" typechecks and bundles. A separate types hook would mean a second `tsc`
|
|
7
8
|
// over the same project.
|
|
8
9
|
|
|
9
|
-
import {
|
|
10
|
-
import { access,
|
|
10
|
+
import { existsSync } from "node:fs";
|
|
11
|
+
import { access, writeFile } from "node:fs/promises";
|
|
11
12
|
import { builtinModules } from "node:module";
|
|
12
|
-
import { dirname, join,
|
|
13
|
-
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { dirname, join, resolve } from "node:path";
|
|
14
14
|
|
|
15
15
|
import commonjs from "@rollup/plugin-commonjs";
|
|
16
16
|
import json from "@rollup/plugin-json";
|
|
@@ -21,20 +21,37 @@ import ts from "typescript";
|
|
|
21
21
|
type Schema = Record<string, any>;
|
|
22
22
|
|
|
23
23
|
type Site = {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
// Which namespace the directive sits in: `process`, `task` or `action`.
|
|
25
|
+
level: "process" | "task" | "action";
|
|
26
26
|
task?: string;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
// What the site IS, at action level: the action's type, and the process a child call names.
|
|
28
|
+
action?: string;
|
|
29
|
+
child?: string;
|
|
30
|
+
// The slot as a path of keys and indices, so nothing has to unescape a JSON Pointer.
|
|
31
|
+
pointer: (string | number)[];
|
|
32
|
+
// Everything after `$<resolver>:`, verbatim. genctl does not read it as a path — that is
|
|
33
|
+
// this resolver's reading of it.
|
|
34
|
+
argument: string;
|
|
35
|
+
// What .genroc asked genctl to type, keyed by the name it chose. This resolver reads Input
|
|
36
|
+
// and Output; which ADDRESS each came from is the config's business, not ours.
|
|
37
|
+
types?: Record<string, Schema>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type ManifestProcess = {
|
|
41
|
+
name: string;
|
|
42
|
+
// The definition's own location, split because a relative argument is relative to the
|
|
43
|
+
// DIRECTORY — joining is ours to do, and this is the base.
|
|
44
|
+
dir: string;
|
|
45
|
+
file: string;
|
|
46
|
+
sites: Site[];
|
|
47
|
+
// Only what this process's fragments reach — a `$ref` in `types` points here.
|
|
48
|
+
$defs?: Record<string, Schema>;
|
|
31
49
|
};
|
|
32
50
|
|
|
33
51
|
type Manifest = {
|
|
34
52
|
mode: "types" | "build";
|
|
35
53
|
root: string;
|
|
36
|
-
|
|
37
|
-
sites: Site[];
|
|
54
|
+
processes: ManifestProcess[];
|
|
38
55
|
};
|
|
39
56
|
|
|
40
57
|
function die(message: string): never {
|
|
@@ -51,12 +68,6 @@ async function exists(path: string): Promise<boolean> {
|
|
|
51
68
|
}
|
|
52
69
|
}
|
|
53
70
|
|
|
54
|
-
/** Creates the parent directory, which `.genroc-cache/` relies on: nothing else makes it. */
|
|
55
|
-
async function write(path: string, content: string): Promise<void> {
|
|
56
|
-
await mkdir(dirname(path), { recursive: true });
|
|
57
|
-
await writeFile(path, content);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
71
|
// ── JSON Schema → TypeScript ───────────────────────────────────────────────────
|
|
61
72
|
|
|
62
73
|
/** Only genroc's keyword set is handled; anything else its strict decoder would have
|
|
@@ -71,13 +82,16 @@ function tsType(s: Schema | undefined, used: Set<string>): string {
|
|
|
71
82
|
if (Array.isArray(s.enum)) {
|
|
72
83
|
return s.enum.map((v: unknown) => JSON.stringify(v)).join(" | ") || "never";
|
|
73
84
|
}
|
|
74
|
-
if (Array.isArray(s.anyOf))
|
|
75
|
-
|
|
85
|
+
if (Array.isArray(s.anyOf))
|
|
86
|
+
return union(s.anyOf.map((a: Schema) => tsType(a, used)));
|
|
87
|
+
if (Array.isArray(s.oneOf))
|
|
88
|
+
return union(s.oneOf.map((a: Schema) => tsType(a, used)));
|
|
76
89
|
if (Array.isArray(s.allOf)) {
|
|
77
90
|
return s.allOf.map((a: Schema) => tsType(a, used)).join(" & ") || "unknown";
|
|
78
91
|
}
|
|
79
92
|
|
|
80
|
-
const types: string[] =
|
|
93
|
+
const types: string[] =
|
|
94
|
+
s.type === undefined ? [] : Array.isArray(s.type) ? s.type : [s.type];
|
|
81
95
|
if (types.length === 0) {
|
|
82
96
|
// The top type: `{}` means unknown, not "an empty object". specs/unknown-type.md.
|
|
83
97
|
return s.properties ? objectType(s, used) : "unknown";
|
|
@@ -110,16 +124,26 @@ function objectType(s: Schema, used: Set<string>): string {
|
|
|
110
124
|
const required = new Set<string>(s.required ?? []);
|
|
111
125
|
const lines: string[] = [];
|
|
112
126
|
for (const [key, sub] of Object.entries(props)) {
|
|
113
|
-
const doc =
|
|
114
|
-
|
|
127
|
+
const doc =
|
|
128
|
+
typeof sub.description === "string"
|
|
129
|
+
? ` /** ${sub.description} */\n`
|
|
130
|
+
: "";
|
|
131
|
+
lines.push(
|
|
132
|
+
`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${nest(tsType(sub, used))};`,
|
|
133
|
+
);
|
|
115
134
|
}
|
|
116
135
|
if (s.additionalProperties && typeof s.additionalProperties === "object") {
|
|
117
|
-
lines.push(
|
|
136
|
+
lines.push(
|
|
137
|
+
` [key: string]: ${nest(tsType(s.additionalProperties, used))};`,
|
|
138
|
+
);
|
|
118
139
|
}
|
|
119
140
|
if (lines.length === 0) return "Record<string, unknown>";
|
|
120
141
|
return `{\n${lines.join("\n")}\n}`;
|
|
121
142
|
}
|
|
122
143
|
|
|
144
|
+
/** Every line but the first, which is already placed by the property that opens it. */
|
|
145
|
+
const nest = (t: string) => t.replace(/\n/g, "\n ");
|
|
146
|
+
|
|
123
147
|
function union(parts: string[]): string {
|
|
124
148
|
const seen = [...new Set(parts)];
|
|
125
149
|
return seen.length === 0 ? "unknown" : seen.join(" | ");
|
|
@@ -127,32 +151,17 @@ function union(parts: string[]): string {
|
|
|
127
151
|
|
|
128
152
|
const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
129
153
|
const propKey = (k: string) => (IDENT.test(k) ? k : JSON.stringify(k));
|
|
130
|
-
const identifier = (n: string) =>
|
|
131
|
-
|
|
132
|
-
function deref(s: Schema | undefined, defs: Record<string, Schema>): Schema | undefined {
|
|
133
|
-
let cur = s;
|
|
134
|
-
for (let i = 0; cur && typeof cur.$ref === "string" && i < 16; i++) {
|
|
135
|
-
cur = defs[cur.$ref.replace(/^#\/\$defs\//, "")];
|
|
136
|
-
}
|
|
137
|
-
return cur;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** The manifest's `input` is the type of the whole ACTION input — for /eval that is
|
|
141
|
-
* `{code, input, timeout_ms, …}`, and only its `input` field is bound as the script's
|
|
142
|
-
* parameter. genroc cannot know that; this file owns the evaluator's wire contract, so
|
|
143
|
-
* the navigation belongs here rather than in genctl. */
|
|
144
|
-
function scriptInput(site: Site, defs: Record<string, Schema>): Schema | undefined {
|
|
145
|
-
const action = deref(site.input, defs);
|
|
146
|
-
const bound = action?.properties?.input;
|
|
147
|
-
return bound ?? site.input;
|
|
148
|
-
}
|
|
154
|
+
const identifier = (n: string) =>
|
|
155
|
+
IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`;
|
|
149
156
|
|
|
150
157
|
/** Emits one named type per reachable $def rather than inlining: a task output may
|
|
151
158
|
* reference itself (specs/recursive-type-inference.md) and inlining would not terminate. */
|
|
152
|
-
function declarations(
|
|
159
|
+
function declarations(at: Located): string {
|
|
160
|
+
const { site, where } = at;
|
|
161
|
+
const defs = where.$defs ?? {};
|
|
153
162
|
const used = new Set<string>();
|
|
154
|
-
const input = tsType(
|
|
155
|
-
const output = tsType(site.
|
|
163
|
+
const input = tsType(site.types?.Input, used);
|
|
164
|
+
const output = tsType(site.types?.Output, used);
|
|
156
165
|
|
|
157
166
|
const emitted: string[] = [];
|
|
158
167
|
const done = new Set<string>();
|
|
@@ -166,7 +175,7 @@ function declarations(site: Site, defs: Record<string, Schema>): string {
|
|
|
166
175
|
|
|
167
176
|
return [
|
|
168
177
|
"// Generated by genroc. Do not edit - regenerate with `genctl types`.",
|
|
169
|
-
`// ${
|
|
178
|
+
`// ${where.name} (${address(site.pointer)})`,
|
|
170
179
|
"",
|
|
171
180
|
...emitted,
|
|
172
181
|
emitted.length ? "" : "",
|
|
@@ -188,7 +197,10 @@ function typesPathFor(scriptPath: string): string {
|
|
|
188
197
|
/** The nearest tsconfig above the script — the one the author's editor already reads. Two
|
|
189
198
|
* different configs mean a red editor over a clean apply, or the reverse. The walk stops at
|
|
190
199
|
* the project root: above it is not this project. */
|
|
191
|
-
async function nearestTsconfig(
|
|
200
|
+
async function nearestTsconfig(
|
|
201
|
+
from: string,
|
|
202
|
+
root: string,
|
|
203
|
+
): Promise<string | null> {
|
|
192
204
|
for (let dir = from; ; dir = dirname(dir)) {
|
|
193
205
|
const candidate = join(dir, "tsconfig.json");
|
|
194
206
|
if (await exists(candidate)) return candidate;
|
|
@@ -196,26 +208,21 @@ async function nearestTsconfig(from: string, root: string): Promise<string | nul
|
|
|
196
208
|
}
|
|
197
209
|
}
|
|
198
210
|
|
|
199
|
-
async function typecheck(
|
|
200
|
-
//
|
|
201
|
-
// coexist with it. The suffix is what keeps the scratch area out of its way.
|
|
202
|
-
const dir = join(root, ".genroc-cache");
|
|
203
|
-
await write(join(dir, ".gitignore"), "*\n");
|
|
204
|
-
|
|
205
|
-
// One tsc per distinct base config: `extends` takes a single base, so merging two would
|
|
211
|
+
async function typecheck(sites: Located[]): Promise<void> {
|
|
212
|
+
// One program per distinct base config: `extends` takes a single base, so merging two would
|
|
206
213
|
// check each script under the other author's options.
|
|
207
|
-
const groups = new Map<string,
|
|
208
|
-
for (const
|
|
209
|
-
const base = (await nearestTsconfig(dirname(
|
|
214
|
+
const groups = new Map<string, Located[]>();
|
|
215
|
+
for (const at of sites) {
|
|
216
|
+
const base = (await nearestTsconfig(dirname(at.file), root)) ?? "";
|
|
210
217
|
const group = groups.get(base);
|
|
211
|
-
if (group) group.push(
|
|
212
|
-
else groups.set(base, [
|
|
218
|
+
if (group) group.push(at);
|
|
219
|
+
else groups.set(base, [at]);
|
|
213
220
|
}
|
|
214
221
|
|
|
215
|
-
|
|
222
|
+
const diagnostics: ts.Diagnostic[] = [];
|
|
216
223
|
for (const [base, group] of groups) {
|
|
217
224
|
const config: Record<string, unknown> = {
|
|
218
|
-
...(base ? { extends:
|
|
225
|
+
...(base ? { extends: base } : {}),
|
|
219
226
|
compilerOptions: {
|
|
220
227
|
noEmit: true,
|
|
221
228
|
strict: true,
|
|
@@ -231,35 +238,42 @@ async function typecheck(root: string, sites: Site[]): Promise<void> {
|
|
|
231
238
|
// base config there is nothing to opt in with, so the default stays none.
|
|
232
239
|
...(base ? {} : { types: [] }),
|
|
233
240
|
},
|
|
234
|
-
files: group.flatMap((s) => [
|
|
241
|
+
files: group.flatMap((s) => [s.file, typesPathFor(s.file)]),
|
|
235
242
|
// `files` overrides the base's, but a base `include` survives beside it and would
|
|
236
243
|
// drag the author's whole tree in, to be checked under the worker lib.
|
|
237
244
|
include: [],
|
|
238
245
|
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
246
|
+
// Never written: the name only anchors `@types` lookup at the root. It must not be
|
|
247
|
+
// `tsconfig.json`: that is the base the config `extends` whenever the author keeps one at
|
|
248
|
+
// the root, and tsc refuses a config that extends its own path as circular.
|
|
249
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
250
|
+
config,
|
|
251
|
+
ts.sys,
|
|
252
|
+
root,
|
|
253
|
+
undefined,
|
|
254
|
+
join(root, "tsconfig.genroc.json"),
|
|
255
|
+
);
|
|
256
|
+
const program = ts.createProgram({
|
|
257
|
+
rootNames: parsed.fileNames,
|
|
258
|
+
options: parsed.options,
|
|
259
|
+
projectReferences: parsed.projectReferences,
|
|
260
|
+
configFileParsingDiagnostics: parsed.errors,
|
|
261
|
+
});
|
|
262
|
+
diagnostics.push(...parsed.errors, ...ts.getPreEmitDiagnostics(program));
|
|
242
263
|
}
|
|
243
|
-
}
|
|
244
264
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
proc.on("close", (c) => resolve(c ?? 1));
|
|
258
|
-
});
|
|
259
|
-
if (code !== 0) {
|
|
260
|
-
// tsc reports on stdout; the exit code IS the type check, so this is the diagnostic
|
|
261
|
-
// genctl surfaces and the reason a failed import never produces a string.
|
|
262
|
-
die([out, err].filter(Boolean).join("\n").trimEnd());
|
|
265
|
+
if (diagnostics.length > 0) {
|
|
266
|
+
// The diagnostics are the whole type check: this is what genctl surfaces, and the reason a
|
|
267
|
+
// failed import never produces a string.
|
|
268
|
+
die(
|
|
269
|
+
ts
|
|
270
|
+
.formatDiagnostics(diagnostics, {
|
|
271
|
+
getCanonicalFileName: (f) => f,
|
|
272
|
+
getCurrentDirectory: () => root,
|
|
273
|
+
getNewLine: () => "\n",
|
|
274
|
+
})
|
|
275
|
+
.trimEnd(),
|
|
276
|
+
);
|
|
263
277
|
}
|
|
264
278
|
}
|
|
265
279
|
|
|
@@ -284,7 +298,10 @@ const transpile: Plugin = {
|
|
|
284
298
|
},
|
|
285
299
|
};
|
|
286
300
|
|
|
287
|
-
const BUILTIN = new Set([
|
|
301
|
+
const BUILTIN = new Set([
|
|
302
|
+
...builtinModules,
|
|
303
|
+
...builtinModules.map((m) => `node:${m}`),
|
|
304
|
+
]);
|
|
288
305
|
|
|
289
306
|
/** Resolves imports through TYPESCRIPT, using the same config the typecheck ran under, so a
|
|
290
307
|
* `paths` alias that compiles also bundles. Reimplementing `paths` here would be a second
|
|
@@ -295,31 +312,44 @@ function tsResolve(configPath: string | null): Plugin {
|
|
|
295
312
|
let options: ts.CompilerOptions = {};
|
|
296
313
|
if (configPath) {
|
|
297
314
|
const read = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
298
|
-
options = ts.parseJsonConfigFileContent(
|
|
315
|
+
options = ts.parseJsonConfigFileContent(
|
|
316
|
+
read.config ?? {},
|
|
317
|
+
ts.sys,
|
|
318
|
+
dirname(configPath),
|
|
319
|
+
).options;
|
|
299
320
|
}
|
|
300
321
|
return {
|
|
301
322
|
name: "genroc-ts-resolve",
|
|
302
323
|
resolveId(source, importer) {
|
|
303
324
|
if (!importer || BUILTIN.has(source)) return null;
|
|
304
|
-
const { resolvedModule } = ts.resolveModuleName(
|
|
305
|
-
|
|
306
|
-
|
|
325
|
+
const { resolvedModule } = ts.resolveModuleName(
|
|
326
|
+
source,
|
|
327
|
+
importer,
|
|
328
|
+
options,
|
|
329
|
+
ts.sys,
|
|
330
|
+
);
|
|
331
|
+
if (!resolvedModule || resolvedModule.isExternalLibraryImport)
|
|
332
|
+
return null;
|
|
333
|
+
return resolvedModule.resolvedFileName.endsWith(".d.ts")
|
|
334
|
+
? null
|
|
335
|
+
: resolvedModule.resolvedFileName;
|
|
307
336
|
},
|
|
308
337
|
};
|
|
309
338
|
}
|
|
310
339
|
|
|
311
|
-
/** Bundles to
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
async function bundle(
|
|
315
|
-
|
|
316
|
-
//
|
|
340
|
+
/** Bundles to a self-contained ES module, which is what the evaluator imports: the default
|
|
341
|
+
* export it calls is the author's own, so nothing wraps or rewrites the code between the two.
|
|
342
|
+
* Bundling is entirely the importer's job, so a definition version pins its code forever. */
|
|
343
|
+
async function bundle(at: Located): Promise<string> {
|
|
344
|
+
const site = at.site;
|
|
345
|
+
// Builtins are EXTERNALISED as imports the realm resolves natively. Anything else
|
|
346
|
+
// unresolved is a REFUSAL, not an external: rollup's default is to leave it as an import
|
|
317
347
|
// of a module that will not be there, which bundles clean and fails at runtime.
|
|
318
348
|
const built = await rollup({
|
|
319
|
-
input:
|
|
349
|
+
input: at.file,
|
|
320
350
|
external: (id) => BUILTIN.has(id),
|
|
321
351
|
plugins: [
|
|
322
|
-
tsResolve(await nearestTsconfig(dirname(
|
|
352
|
+
tsResolve(await nearestTsconfig(dirname(at.file), root)),
|
|
323
353
|
nodeResolve({ extensions: [".ts", ".tsx", ".mjs", ".js", ".json"] }),
|
|
324
354
|
commonjs(),
|
|
325
355
|
// A `.json` import is a data file inlined at build time, which the previous bundler
|
|
@@ -329,23 +359,26 @@ async function bundle(site: Site, root: string): Promise<string> {
|
|
|
329
359
|
],
|
|
330
360
|
onwarn(warning) {
|
|
331
361
|
if (warning.code === "UNRESOLVED_IMPORT") {
|
|
332
|
-
die(
|
|
362
|
+
die(
|
|
363
|
+
`${at.file}: cannot resolve ${warning.exporter ?? "an import"} — is it installed?`,
|
|
364
|
+
);
|
|
333
365
|
}
|
|
334
366
|
},
|
|
335
|
-
}).catch((e: unknown) =>
|
|
367
|
+
}).catch((e: unknown) =>
|
|
368
|
+
die(`${at.file}: ${e instanceof Error ? e.message : String(e)}`),
|
|
369
|
+
);
|
|
336
370
|
|
|
337
|
-
const { output } = await built.generate({
|
|
371
|
+
const { output } = await built.generate({
|
|
372
|
+
format: "es",
|
|
373
|
+
inlineDynamicImports: true,
|
|
374
|
+
});
|
|
338
375
|
await built.close();
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
` throw new Error(${JSON.stringify(`${site.path} has no default export function`)});`,
|
|
346
|
-
"}",
|
|
347
|
-
"return await __genroc_main(input);",
|
|
348
|
-
].join("\n");
|
|
376
|
+
// Refused here rather than in the realm: the evaluator can only report it against a running
|
|
377
|
+
// instance, and the file it names is on this machine.
|
|
378
|
+
if (!output[0].exports.includes("default")) {
|
|
379
|
+
die(`${at.file}: a script must \`export default\` the function to run`);
|
|
380
|
+
}
|
|
381
|
+
return output[0].code;
|
|
349
382
|
}
|
|
350
383
|
|
|
351
384
|
// ── main ───────────────────────────────────────────────────────────────────────
|
|
@@ -358,40 +391,99 @@ const stdin: string = await new Promise((resolve, reject) => {
|
|
|
358
391
|
process.stdin.on("error", reject);
|
|
359
392
|
});
|
|
360
393
|
const manifest = JSON.parse(stdin) as Manifest;
|
|
361
|
-
|
|
394
|
+
// genctl runs a resolver with the project root as its cwd, so the manifest need not say so.
|
|
395
|
+
const root = process.cwd();
|
|
396
|
+
if (!manifest || !Array.isArray(manifest.processes))
|
|
397
|
+
die("stdin is not a genroc resolver manifest");
|
|
398
|
+
|
|
399
|
+
/** Every site with the process it sits in and the file its argument names, in the manifest's own
|
|
400
|
+
* order — which is the order `code` must answer in. genctl passes the argument verbatim, so
|
|
401
|
+
* joining it to the definition's directory is this resolver's business: only it knows the
|
|
402
|
+
* argument is a path at all. */
|
|
403
|
+
type Located = { site: Site; where: ManifestProcess; file: string };
|
|
404
|
+
|
|
405
|
+
/** A pointer as the address it is, so a generated comment or an error can be pasted into
|
|
406
|
+
* `genctl schema`. A key no identifier can spell is bracketed, as the grammar spells it. */
|
|
407
|
+
function address(pointer: (string | number)[]): string {
|
|
408
|
+
return pointer
|
|
409
|
+
.map((seg) =>
|
|
410
|
+
typeof seg === "string" && /^[A-Za-z_][A-Za-z0-9_]*$/.test(seg)
|
|
411
|
+
? `.${seg}`
|
|
412
|
+
: `[${JSON.stringify(seg)}]`,
|
|
413
|
+
)
|
|
414
|
+
.join("")
|
|
415
|
+
.replace(/^\./, "");
|
|
416
|
+
}
|
|
417
|
+
const located: Located[] = manifest.processes.flatMap((where) =>
|
|
418
|
+
where.sites.map((site) => ({
|
|
419
|
+
site,
|
|
420
|
+
where,
|
|
421
|
+
file: resolve(where.dir, site.argument),
|
|
422
|
+
})),
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
// genctl is agnostic about what a script is for; the contract that an evaluation request carries
|
|
426
|
+
// its module in `code` is THIS resolver's, so it is the one that checks a directive landed there.
|
|
427
|
+
// `child` is the shape the scaffold uses — a call to a process that forwards to the evaluator —
|
|
428
|
+
// and `external` is the same request made directly.
|
|
429
|
+
for (const at of located) {
|
|
430
|
+
// Two halves, from the two places that carry them: the action's KIND is a field, and WHERE in
|
|
431
|
+
// it the directive sits is the pointer. A child call is the shape the scaffold generates — to a
|
|
432
|
+
// process that forwards to the evaluator — and an external task is the same request made
|
|
433
|
+
// directly.
|
|
434
|
+
const kind = at.site.level === "action" ? (at.site.action ?? "") : "";
|
|
435
|
+
const slot = at.site.pointer.slice(-2).join(".");
|
|
436
|
+
if ((kind !== "child" && kind !== "external") || slot !== "input.code") {
|
|
437
|
+
die(
|
|
438
|
+
`${join(at.where.dir, at.where.file)}: ${address(at.site.pointer)}: an evaluated script ` +
|
|
439
|
+
"belongs in the `code` field of a child or external task's input, and this is " +
|
|
440
|
+
`${kind ? `a ${kind} task's ` : ""}\`${slot}\`.`,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// genctl no longer stats the argument — it does not know it is a file — so the resolver that
|
|
446
|
+
// does is the one that must say when it is not there.
|
|
447
|
+
for (const at of located) {
|
|
448
|
+
if (!existsSync(at.file)) {
|
|
449
|
+
die(
|
|
450
|
+
`${join(at.where.dir, at.where.file)}: ${address(at.site.pointer)}: ` +
|
|
451
|
+
`"$import: ${at.site.argument}" names no file (looked at ${at.file})`,
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
362
455
|
|
|
363
456
|
// One script at two sites with different input types is a refusal, not a union: the union
|
|
364
457
|
// is sound and would typecheck a body that is wrong at one of the sites.
|
|
365
|
-
const byPath = new Map<string,
|
|
366
|
-
for (const
|
|
367
|
-
const seen = byPath.get(
|
|
368
|
-
const defsOf = (x: Site) => (manifest.schemas[x.process]?.$defs ?? {}) as Record<string, Schema>;
|
|
458
|
+
const byPath = new Map<string, Located>();
|
|
459
|
+
for (const at of located) {
|
|
460
|
+
const seen = byPath.get(at.file);
|
|
369
461
|
if (
|
|
370
462
|
seen &&
|
|
371
|
-
JSON.stringify(
|
|
372
|
-
JSON.stringify(scriptInput(site, defsOf(site)))
|
|
463
|
+
JSON.stringify(seen.site.types) !== JSON.stringify(at.site.types)
|
|
373
464
|
) {
|
|
374
465
|
die(
|
|
375
|
-
`${
|
|
466
|
+
`${at.file} is imported at ${address(seen.site.pointer)} and ` +
|
|
467
|
+
`${address(at.site.pointer)} with different input types.\n` +
|
|
376
468
|
"Split it into two scripts, or make the two call sites pass the same shape.",
|
|
377
469
|
);
|
|
378
470
|
}
|
|
379
|
-
byPath.set(
|
|
471
|
+
byPath.set(at.file, at);
|
|
380
472
|
}
|
|
381
473
|
|
|
382
|
-
for (const
|
|
383
|
-
|
|
384
|
-
await write(typesPathFor(site.path), declarations(site, defs));
|
|
474
|
+
for (const at of byPath.values()) {
|
|
475
|
+
await writeFile(typesPathFor(at.file), declarations(at));
|
|
385
476
|
}
|
|
386
477
|
|
|
387
478
|
if (manifest.mode === "types") {
|
|
479
|
+
console.log(JSON.stringify(manifest, null, 2));
|
|
388
480
|
process.exit(0);
|
|
389
481
|
}
|
|
390
482
|
|
|
391
|
-
await typecheck(
|
|
483
|
+
await typecheck([...byPath.values()]);
|
|
392
484
|
|
|
393
485
|
const code: string[] = [];
|
|
394
|
-
for (const
|
|
395
|
-
code.push(await bundle(
|
|
486
|
+
for (const at of located) {
|
|
487
|
+
code.push(await bundle(at));
|
|
396
488
|
}
|
|
397
489
|
process.stdout.write(JSON.stringify({ code }));
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genroc/eval-node",
|
|
3
|
-
"version": "0.0.0-edge.
|
|
3
|
+
"version": "0.0.0-edge.f8ffa72f",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
|
+
"build": "tsc -p tsconfig.build.json",
|
|
10
|
+
"prepublishOnly": "npm run build",
|
|
9
11
|
"work": "node worker.ts",
|
|
10
12
|
"typecheck": "tsc --noEmit"
|
|
11
13
|
},
|
|
@@ -28,11 +30,11 @@
|
|
|
28
30
|
},
|
|
29
31
|
"homepage": "https://genroc.org",
|
|
30
32
|
"bin": {
|
|
31
|
-
"genroc-import": "
|
|
32
|
-
"genroc-eval-node": "
|
|
33
|
+
"genroc-import": "dist/import.js",
|
|
34
|
+
"genroc-eval-node": "dist/worker.js"
|
|
33
35
|
},
|
|
34
36
|
"files": [
|
|
35
|
-
"
|
|
37
|
+
"dist",
|
|
36
38
|
"*.ts",
|
|
37
39
|
"tsconfig.json",
|
|
38
40
|
"README.md"
|