@genroc/eval-node 0.0.0-edge.d1df786 → 0.0.0-edge.d46a8ebf
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 +58 -34
- package/dist/eval.js +27 -14
- package/dist/import.js +133 -126
- package/dist/realm.js +63 -80
- package/dist/worker.js +52 -64
- package/eval.ts +31 -20
- package/import.ts +219 -148
- package/package.json +1 -1
- package/realm.ts +58 -82
- package/worker.ts +59 -67
package/dist/import.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The code-phase resolver: manifest on stdin, `{"code": [...]}` on stdout,
|
|
3
|
-
// with
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
// code; "build" typechecks and bundles. A separate types hook would mean a second `tsc`
|
|
8
|
-
// over the same project.
|
|
9
|
-
import { spawn } from "node:child_process";
|
|
10
|
-
import { access, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
// The code-phase resolver: manifest on stdin, `{"code": [...]}` on stdout, diagnostic on stderr
|
|
3
|
+
// with a non-zero exit (specs/source-resolution.md). Two modes share one `tsc` pass: "types"
|
|
4
|
+
// writes declarations only, "build" typechecks and bundles.
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { access, writeFile } from "node:fs/promises";
|
|
11
7
|
import { builtinModules } from "node:module";
|
|
12
|
-
import { dirname, join,
|
|
13
|
-
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { dirname, join, resolve } from "node:path";
|
|
14
9
|
import commonjs from "@rollup/plugin-commonjs";
|
|
15
10
|
import json from "@rollup/plugin-json";
|
|
16
11
|
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
|
@@ -29,11 +24,6 @@ async function exists(path) {
|
|
|
29
24
|
return false;
|
|
30
25
|
}
|
|
31
26
|
}
|
|
32
|
-
/** Creates the parent directory, which `.genroc-cache/` relies on: nothing else makes it. */
|
|
33
|
-
async function write(path, content) {
|
|
34
|
-
await mkdir(dirname(path), { recursive: true });
|
|
35
|
-
await writeFile(path, content);
|
|
36
|
-
}
|
|
37
27
|
// ── JSON Schema → TypeScript ───────────────────────────────────────────────────
|
|
38
28
|
/** Only genroc's keyword set is handled; anything else its strict decoder would have
|
|
39
29
|
* refused before this ran (internal/schema, allowedKeywords). */
|
|
@@ -86,45 +76,35 @@ function objectType(s, used) {
|
|
|
86
76
|
const required = new Set(s.required ?? []);
|
|
87
77
|
const lines = [];
|
|
88
78
|
for (const [key, sub] of Object.entries(props)) {
|
|
89
|
-
const doc = typeof sub.description === "string"
|
|
90
|
-
|
|
79
|
+
const doc = typeof sub.description === "string"
|
|
80
|
+
? ` /** ${sub.description} */\n`
|
|
81
|
+
: "";
|
|
82
|
+
lines.push(`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${nest(tsType(sub, used))};`);
|
|
91
83
|
}
|
|
92
84
|
if (s.additionalProperties && typeof s.additionalProperties === "object") {
|
|
93
|
-
lines.push(` [key: string]: ${tsType(s.additionalProperties, used)};`);
|
|
85
|
+
lines.push(` [key: string]: ${nest(tsType(s.additionalProperties, used))};`);
|
|
94
86
|
}
|
|
95
87
|
if (lines.length === 0)
|
|
96
88
|
return "Record<string, unknown>";
|
|
97
89
|
return `{\n${lines.join("\n")}\n}`;
|
|
98
90
|
}
|
|
91
|
+
/** Every line but the first, which is already placed by the property that opens it. */
|
|
92
|
+
const nest = (t) => t.replace(/\n/g, "\n ");
|
|
99
93
|
function union(parts) {
|
|
100
94
|
const seen = [...new Set(parts)];
|
|
101
95
|
return seen.length === 0 ? "unknown" : seen.join(" | ");
|
|
102
96
|
}
|
|
103
97
|
const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
104
98
|
const propKey = (k) => (IDENT.test(k) ? k : JSON.stringify(k));
|
|
105
|
-
const identifier = (n) =>
|
|
106
|
-
function deref(s, defs) {
|
|
107
|
-
let cur = s;
|
|
108
|
-
for (let i = 0; cur && typeof cur.$ref === "string" && i < 16; i++) {
|
|
109
|
-
cur = defs[cur.$ref.replace(/^#\/\$defs\//, "")];
|
|
110
|
-
}
|
|
111
|
-
return cur;
|
|
112
|
-
}
|
|
113
|
-
/** The manifest's `input` is the type of the whole ACTION input — for /eval that is
|
|
114
|
-
* `{code, input, timeout_ms, …}`, and only its `input` field is bound as the script's
|
|
115
|
-
* parameter. genroc cannot know that; this file owns the evaluator's wire contract, so
|
|
116
|
-
* the navigation belongs here rather than in genctl. */
|
|
117
|
-
function scriptInput(site, defs) {
|
|
118
|
-
const action = deref(site.input, defs);
|
|
119
|
-
const bound = action?.properties?.input;
|
|
120
|
-
return bound ?? site.input;
|
|
121
|
-
}
|
|
99
|
+
const identifier = (n) => IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`;
|
|
122
100
|
/** Emits one named type per reachable $def rather than inlining: a task output may
|
|
123
101
|
* reference itself (specs/recursive-type-inference.md) and inlining would not terminate. */
|
|
124
|
-
function declarations(
|
|
102
|
+
function declarations(at) {
|
|
103
|
+
const { site, where } = at;
|
|
104
|
+
const defs = where.$defs ?? {};
|
|
125
105
|
const used = new Set();
|
|
126
|
-
const input = tsType(
|
|
127
|
-
const output = tsType(site.
|
|
106
|
+
const input = tsType(site.types?.Input, used);
|
|
107
|
+
const output = tsType(site.types?.Output, used);
|
|
128
108
|
const emitted = [];
|
|
129
109
|
const done = new Set();
|
|
130
110
|
while (true) {
|
|
@@ -137,7 +117,7 @@ function declarations(site, defs) {
|
|
|
137
117
|
}
|
|
138
118
|
return [
|
|
139
119
|
"// Generated by genroc. Do not edit - regenerate with `genctl types`.",
|
|
140
|
-
`// ${
|
|
120
|
+
`// ${where.name} (${address(site.pointer)})`,
|
|
141
121
|
"",
|
|
142
122
|
...emitted,
|
|
143
123
|
emitted.length ? "" : "",
|
|
@@ -153,9 +133,8 @@ function typesPathFor(scriptPath) {
|
|
|
153
133
|
return scriptPath.replace(/\.[^.\/]+$/, "") + ".genroc.d.ts";
|
|
154
134
|
}
|
|
155
135
|
// ── typecheck ──────────────────────────────────────────────────────────────────
|
|
156
|
-
/** The
|
|
157
|
-
*
|
|
158
|
-
* the project root: above it is not this project. */
|
|
136
|
+
/** The tsconfig the author's editor reads, so editor and apply agree. Stops at the project
|
|
137
|
+
* root. */
|
|
159
138
|
async function nearestTsconfig(from, root) {
|
|
160
139
|
for (let dir = from;; dir = dirname(dir)) {
|
|
161
140
|
const candidate = join(dir, "tsconfig.json");
|
|
@@ -165,26 +144,22 @@ async function nearestTsconfig(from, root) {
|
|
|
165
144
|
return null;
|
|
166
145
|
}
|
|
167
146
|
}
|
|
168
|
-
async function typecheck(
|
|
169
|
-
//
|
|
170
|
-
// coexist with it. The suffix is what keeps the scratch area out of its way.
|
|
171
|
-
const dir = join(root, ".genroc-cache");
|
|
172
|
-
await write(join(dir, ".gitignore"), "*\n");
|
|
173
|
-
// One tsc per distinct base config: `extends` takes a single base, so merging two would
|
|
147
|
+
async function typecheck(sites) {
|
|
148
|
+
// One program per distinct base config: `extends` takes a single base, so merging two would
|
|
174
149
|
// check each script under the other author's options.
|
|
175
150
|
const groups = new Map();
|
|
176
|
-
for (const
|
|
177
|
-
const base = (await nearestTsconfig(dirname(
|
|
151
|
+
for (const at of sites) {
|
|
152
|
+
const base = (await nearestTsconfig(dirname(at.file), root)) ?? "";
|
|
178
153
|
const group = groups.get(base);
|
|
179
154
|
if (group)
|
|
180
|
-
group.push(
|
|
155
|
+
group.push(at);
|
|
181
156
|
else
|
|
182
|
-
groups.set(base, [
|
|
157
|
+
groups.set(base, [at]);
|
|
183
158
|
}
|
|
184
|
-
|
|
159
|
+
const diagnostics = [];
|
|
185
160
|
for (const [base, group] of groups) {
|
|
186
161
|
const config = {
|
|
187
|
-
...(base ? { extends:
|
|
162
|
+
...(base ? { extends: base } : {}),
|
|
188
163
|
compilerOptions: {
|
|
189
164
|
noEmit: true,
|
|
190
165
|
strict: true,
|
|
@@ -195,39 +170,36 @@ async function typecheck(root, sites) {
|
|
|
195
170
|
// `lib` DESCRIBES the realm and is written after `extends` so a base cannot widen it:
|
|
196
171
|
// a worker thread has no document, whatever an author's config claims.
|
|
197
172
|
lib: ["esnext", "webworker"],
|
|
198
|
-
// `types` is the author's
|
|
199
|
-
//
|
|
200
|
-
// base config there is nothing to opt in with, so the default stays none.
|
|
173
|
+
// `types` is the author's opt-in to node globals, which the realm has. With no base
|
|
174
|
+
// config there is nothing to opt in with, so it stays none.
|
|
201
175
|
...(base ? {} : { types: [] }),
|
|
202
176
|
},
|
|
203
|
-
files: group.flatMap((s) => [
|
|
177
|
+
files: group.flatMap((s) => [s.file, typesPathFor(s.file)]),
|
|
204
178
|
// `files` overrides the base's, but a base `include` survives beside it and would
|
|
205
179
|
// drag the author's whole tree in, to be checked under the worker lib.
|
|
206
180
|
include: [],
|
|
207
181
|
};
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
182
|
+
// Never written; anchors `@types` lookup at the root. Not `tsconfig.json`: that is often the
|
|
183
|
+
// base it extends, and tsc refuses a circular extends.
|
|
184
|
+
const parsed = ts.parseJsonConfigFileContent(config, ts.sys, root, undefined, join(root, "tsconfig.genroc.json"));
|
|
185
|
+
const program = ts.createProgram({
|
|
186
|
+
rootNames: parsed.fileNames,
|
|
187
|
+
options: parsed.options,
|
|
188
|
+
projectReferences: parsed.projectReferences,
|
|
189
|
+
configFileParsingDiagnostics: parsed.errors,
|
|
190
|
+
});
|
|
191
|
+
diagnostics.push(...parsed.errors, ...ts.getPreEmitDiagnostics(program));
|
|
211
192
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
proc.stderr.on("data", (c) => (err += c));
|
|
223
|
-
const code = await new Promise((resolve, reject) => {
|
|
224
|
-
proc.on("error", reject);
|
|
225
|
-
proc.on("close", (c) => resolve(c ?? 1));
|
|
226
|
-
});
|
|
227
|
-
if (code !== 0) {
|
|
228
|
-
// tsc reports on stdout; the exit code IS the type check, so this is the diagnostic
|
|
229
|
-
// genctl surfaces and the reason a failed import never produces a string.
|
|
230
|
-
die([out, err].filter(Boolean).join("\n").trimEnd());
|
|
193
|
+
if (diagnostics.length > 0) {
|
|
194
|
+
// The diagnostics are the whole type check: this is what genctl surfaces, and the reason a
|
|
195
|
+
// failed import never produces a string.
|
|
196
|
+
die(ts
|
|
197
|
+
.formatDiagnostics(diagnostics, {
|
|
198
|
+
getCanonicalFileName: (f) => f,
|
|
199
|
+
getCurrentDirectory: () => root,
|
|
200
|
+
getNewLine: () => "\n",
|
|
201
|
+
})
|
|
202
|
+
.trimEnd());
|
|
231
203
|
}
|
|
232
204
|
}
|
|
233
205
|
// ── bundle ─────────────────────────────────────────────────────────────────────
|
|
@@ -250,12 +222,13 @@ const transpile = {
|
|
|
250
222
|
return { code: out.outputText, map: out.sourceMapText ?? null };
|
|
251
223
|
},
|
|
252
224
|
};
|
|
253
|
-
const BUILTIN = new Set([
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
*
|
|
225
|
+
const BUILTIN = new Set([
|
|
226
|
+
...builtinModules,
|
|
227
|
+
...builtinModules.map((m) => `node:${m}`),
|
|
228
|
+
]);
|
|
229
|
+
/** Resolves through TYPESCRIPT under the typecheck's config, so a `paths` alias that compiles
|
|
230
|
+
* also bundles. A `.d.ts` resolution is declined (a type, not code), leaving node_modules to
|
|
231
|
+
* nodeResolve. */
|
|
259
232
|
function tsResolve(configPath) {
|
|
260
233
|
let options = {};
|
|
261
234
|
if (configPath) {
|
|
@@ -270,47 +243,46 @@ function tsResolve(configPath) {
|
|
|
270
243
|
const { resolvedModule } = ts.resolveModuleName(source, importer, options, ts.sys);
|
|
271
244
|
if (!resolvedModule || resolvedModule.isExternalLibraryImport)
|
|
272
245
|
return null;
|
|
273
|
-
return resolvedModule.resolvedFileName.endsWith(".d.ts")
|
|
246
|
+
return resolvedModule.resolvedFileName.endsWith(".d.ts")
|
|
247
|
+
? null
|
|
248
|
+
: resolvedModule.resolvedFileName;
|
|
274
249
|
},
|
|
275
250
|
};
|
|
276
251
|
}
|
|
277
|
-
/**
|
|
278
|
-
*
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
// Builtins are
|
|
282
|
-
//
|
|
283
|
-
// of a module that will not be there, which bundles clean and fails at runtime.
|
|
252
|
+
/** A self-contained ES module whose default export is the author's own, unwrapped. Bundling here
|
|
253
|
+
* means a definition version pins its code forever. */
|
|
254
|
+
async function bundle(at) {
|
|
255
|
+
const site = at.site;
|
|
256
|
+
// Builtins are externalised; anything else unresolved is a REFUSAL, since rollup's default
|
|
257
|
+
// leaves an import that bundles clean and fails at runtime.
|
|
284
258
|
const built = await rollup({
|
|
285
|
-
input:
|
|
259
|
+
input: at.file,
|
|
286
260
|
external: (id) => BUILTIN.has(id),
|
|
287
261
|
plugins: [
|
|
288
|
-
tsResolve(await nearestTsconfig(dirname(
|
|
262
|
+
tsResolve(await nearestTsconfig(dirname(at.file), root)),
|
|
289
263
|
nodeResolve({ extensions: [".ts", ".tsx", ".mjs", ".js", ".json"] }),
|
|
290
264
|
commonjs(),
|
|
291
|
-
//
|
|
292
|
-
// did natively; without it rollup hands the JSON to the JS parser.
|
|
265
|
+
// Inlines `.json` imports; without it rollup hands JSON to the JS parser.
|
|
293
266
|
json(),
|
|
294
267
|
transpile,
|
|
295
268
|
],
|
|
296
269
|
onwarn(warning) {
|
|
297
270
|
if (warning.code === "UNRESOLVED_IMPORT") {
|
|
298
|
-
die(`${
|
|
271
|
+
die(`${at.file}: cannot resolve ${warning.exporter ?? "an import"} — is it installed?`);
|
|
299
272
|
}
|
|
300
273
|
},
|
|
301
|
-
}).catch((e) => die(`${
|
|
302
|
-
const { output } = await built.generate({
|
|
274
|
+
}).catch((e) => die(`${at.file}: ${e instanceof Error ? e.message : String(e)}`));
|
|
275
|
+
const { output } = await built.generate({
|
|
276
|
+
format: "es",
|
|
277
|
+
inlineDynamicImports: true,
|
|
278
|
+
});
|
|
303
279
|
await built.close();
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
` throw new Error(${JSON.stringify(`${site.path} has no default export function`)});`,
|
|
311
|
-
"}",
|
|
312
|
-
"return await __genroc_main(input);",
|
|
313
|
-
].join("\n");
|
|
280
|
+
// Refused here rather than in the realm: the evaluator can only report it against a running
|
|
281
|
+
// instance, and the file it names is on this machine.
|
|
282
|
+
if (!output[0].exports.includes("default")) {
|
|
283
|
+
die(`${at.file}: a script must \`export default\` the function to run`);
|
|
284
|
+
}
|
|
285
|
+
return output[0].code;
|
|
314
286
|
}
|
|
315
287
|
// ── main ───────────────────────────────────────────────────────────────────────
|
|
316
288
|
const stdin = await new Promise((resolve, reject) => {
|
|
@@ -321,32 +293,67 @@ const stdin = await new Promise((resolve, reject) => {
|
|
|
321
293
|
process.stdin.on("error", reject);
|
|
322
294
|
});
|
|
323
295
|
const manifest = JSON.parse(stdin);
|
|
324
|
-
|
|
296
|
+
// genctl runs a resolver with the project root as its cwd, so the manifest need not say so.
|
|
297
|
+
const root = process.cwd();
|
|
298
|
+
if (!manifest || !Array.isArray(manifest.processes))
|
|
325
299
|
die("stdin is not a genroc resolver manifest");
|
|
300
|
+
/** A pointer as the address it is, so a generated comment or an error can be pasted into
|
|
301
|
+
* `genctl schema`. A key no identifier can spell is bracketed, as the grammar spells it. */
|
|
302
|
+
function address(pointer) {
|
|
303
|
+
return pointer
|
|
304
|
+
.map((seg) => typeof seg === "string" && /^[A-Za-z_][A-Za-z0-9_]*$/.test(seg)
|
|
305
|
+
? `.${seg}`
|
|
306
|
+
: `[${JSON.stringify(seg)}]`)
|
|
307
|
+
.join("")
|
|
308
|
+
.replace(/^\./, "");
|
|
309
|
+
}
|
|
310
|
+
const located = manifest.processes.flatMap((where) => where.sites.map((site) => ({
|
|
311
|
+
site,
|
|
312
|
+
where,
|
|
313
|
+
file: resolve(where.dir, site.argument),
|
|
314
|
+
})));
|
|
315
|
+
// The `code`-field contract is this resolver's, not genctl's, so it checks the directive landed
|
|
316
|
+
// there: in a child call (the scaffold's shape) or an external task's input.
|
|
317
|
+
for (const at of located) {
|
|
318
|
+
// The action's kind is a field; where in it the directive sits is the pointer.
|
|
319
|
+
const kind = at.site.level === "action" ? (at.site.action ?? "") : "";
|
|
320
|
+
const slot = at.site.pointer.slice(-2).join(".");
|
|
321
|
+
if ((kind !== "child" && kind !== "external") || slot !== "input.code") {
|
|
322
|
+
die(`${join(at.where.dir, at.where.file)}: ${address(at.site.pointer)}: an evaluated script ` +
|
|
323
|
+
"belongs in the `code` field of a child or external task's input, and this is " +
|
|
324
|
+
`${kind ? `a ${kind} task's ` : ""}\`${slot}\`.`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// genctl does not know the argument is a file, so this resolver reports a missing one.
|
|
328
|
+
for (const at of located) {
|
|
329
|
+
if (!existsSync(at.file)) {
|
|
330
|
+
die(`${join(at.where.dir, at.where.file)}: ${address(at.site.pointer)}: ` +
|
|
331
|
+
`"$import: ${at.site.argument}" names no file (looked at ${at.file})`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
326
334
|
// One script at two sites with different input types is a refusal, not a union: the union
|
|
327
335
|
// is sound and would typecheck a body that is wrong at one of the sites.
|
|
328
336
|
const byPath = new Map();
|
|
329
|
-
for (const
|
|
330
|
-
const seen = byPath.get(
|
|
331
|
-
const defsOf = (x) => (manifest.schemas[x.process]?.$defs ?? {});
|
|
337
|
+
for (const at of located) {
|
|
338
|
+
const seen = byPath.get(at.file);
|
|
332
339
|
if (seen &&
|
|
333
|
-
JSON.stringify(
|
|
334
|
-
|
|
335
|
-
|
|
340
|
+
JSON.stringify(seen.site.types) !== JSON.stringify(at.site.types)) {
|
|
341
|
+
die(`${at.file} is imported at ${address(seen.site.pointer)} and ` +
|
|
342
|
+
`${address(at.site.pointer)} with different input types.\n` +
|
|
336
343
|
"Split it into two scripts, or make the two call sites pass the same shape.");
|
|
337
344
|
}
|
|
338
|
-
byPath.set(
|
|
345
|
+
byPath.set(at.file, at);
|
|
339
346
|
}
|
|
340
|
-
for (const
|
|
341
|
-
|
|
342
|
-
await write(typesPathFor(site.path), declarations(site, defs));
|
|
347
|
+
for (const at of byPath.values()) {
|
|
348
|
+
await writeFile(typesPathFor(at.file), declarations(at));
|
|
343
349
|
}
|
|
344
350
|
if (manifest.mode === "types") {
|
|
351
|
+
console.log(JSON.stringify(manifest, null, 2));
|
|
345
352
|
process.exit(0);
|
|
346
353
|
}
|
|
347
|
-
await typecheck(
|
|
354
|
+
await typecheck([...byPath.values()]);
|
|
348
355
|
const code = [];
|
|
349
|
-
for (const
|
|
350
|
-
code.push(await bundle(
|
|
356
|
+
for (const at of located) {
|
|
357
|
+
code.push(await bundle(at));
|
|
351
358
|
}
|
|
352
359
|
process.stdout.write(JSON.stringify({ code }));
|
package/dist/realm.js
CHANGED
|
@@ -1,81 +1,42 @@
|
|
|
1
|
-
// The evaluation realm
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
// The evaluation realm: one Worker per execution, killable mid-loop by eval.ts, which owns the
|
|
2
|
+
// budget. Everything touching the script's VALUE lives here, the only realm it exists in.
|
|
3
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
4
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
5
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
6
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
return path;
|
|
10
|
+
};
|
|
11
|
+
import { registerHooks } from "node:module";
|
|
8
12
|
import { parentPort } from "node:worker_threads";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// inlines everything else — and a function built by the AsyncFunction constructor has no
|
|
13
|
-
// `require` in scope. Passing one in is what makes an import of a builtin work at runtime rather than at
|
|
14
|
-
// typecheck only. Resolution is anchored here, which is right: only builtins reach it.
|
|
15
|
-
const scriptRequire = createRequire(import.meta.url);
|
|
13
|
+
// IMPORTED under our own URL rather than compiled from a string, so frames carry the author's
|
|
14
|
+
// line numbers and builtin imports resolve normally.
|
|
15
|
+
const SCRIPT_URL = "script:main";
|
|
16
16
|
const STACK_BYTES = 2_048;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return 0;
|
|
28
|
-
}
|
|
29
|
-
catch (err) {
|
|
30
|
-
return reportedLine(err) - 1;
|
|
31
|
-
}
|
|
32
|
-
})();
|
|
33
|
-
// V8 marks a frame compiled by the AsyncFunction constructor with the site that CALLED the
|
|
34
|
-
// constructor, then the script's OWN position:
|
|
35
|
-
// at inner (eval at run (file:///…/worker.ts:107:10), <anonymous>:6:9)
|
|
36
|
-
// `eval at` is therefore what separates script frames from runner plumbing — matched without
|
|
37
|
-
// the function name, which is whatever encloses the `new AsyncFunction` below. The LAST such
|
|
38
|
-
// frame is the body's top level: frames interleave, since a script can throw inside a native
|
|
39
|
-
// callback.
|
|
40
|
-
const SCRIPT_FRAME = /\(eval at /;
|
|
41
|
-
// The trailing `<anonymous>:LINE:COL` — the script's position, after the host file's own.
|
|
42
|
-
const POSITION = /<anonymous>:(\d+):(\d+)\)?\s*$/;
|
|
43
|
-
// ` at name (` — absent on the top-level frame, which V8 names `eval`.
|
|
44
|
-
const FRAME_NAME = /^\s*at\s+(?:async\s+)?([^\s(]+)\s*\(/;
|
|
45
|
-
/** Line number of the throw as the engine reported it, or 1 if the stack is unreadable. */
|
|
46
|
-
function reportedLine(err) {
|
|
47
|
-
const stack = err instanceof Error && typeof err.stack === "string" ? err.stack : "";
|
|
48
|
-
const frame = stack.split("\n").find((l) => SCRIPT_FRAME.test(l)) ?? "";
|
|
49
|
-
const m = frame.match(POSITION);
|
|
50
|
-
return m ? Number(m[1]) : 1;
|
|
51
|
-
}
|
|
52
|
-
/** Renumbers each script frame to the line the AUTHOR wrote and drops the runner's own.
|
|
53
|
-
* Rewriting the whole location is also what keeps the runner's path out of a script's
|
|
54
|
-
* stack — V8 puts it inside every compiled frame. */
|
|
55
|
-
function scriptStack(err, offset) {
|
|
17
|
+
// The only channel a load hook has to the source. Written per request and read once — a realm
|
|
18
|
+
// evaluates one script and is then discarded, so no second execution can observe it.
|
|
19
|
+
let source = "";
|
|
20
|
+
registerHooks({
|
|
21
|
+
resolve: (specifier, context, next) => specifier === SCRIPT_URL ? { url: SCRIPT_URL, shortCircuit: true } : next(specifier, context),
|
|
22
|
+
load: (url, context, next) => url === SCRIPT_URL ? { format: "module", source, shortCircuit: true } : next(url, context),
|
|
23
|
+
});
|
|
24
|
+
/** Keeps the frames that are the script's own. Everything below the last of them is runner
|
|
25
|
+
* plumbing the author cannot act on, and V8 puts this file's path in it. */
|
|
26
|
+
function scriptStack(err) {
|
|
56
27
|
if (!(err instanceof Error) || typeof err.stack !== "string")
|
|
57
28
|
return undefined;
|
|
58
29
|
const lines = err.stack.split("\n");
|
|
59
|
-
let
|
|
30
|
+
let last = 0;
|
|
60
31
|
for (let i = 0; i < lines.length; i++)
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
const pos = line.match(POSITION);
|
|
66
|
-
if (!pos)
|
|
67
|
-
return line; // the `Error: message` header and native frames, kept as-is
|
|
68
|
-
const name = line.match(FRAME_NAME)?.[1];
|
|
69
|
-
const at = name && name !== "eval" && name !== "anonymous" ? `at ${name} ` : "at ";
|
|
70
|
-
const indent = line.match(/^\s*/)[0];
|
|
71
|
-
return `${indent}${at}(script:${Math.max(1, Number(pos[1]) - offset)}:${pos[2]})`;
|
|
72
|
-
})
|
|
73
|
-
.join("\n");
|
|
74
|
-
return frames.length > STACK_BYTES ? frames.slice(0, STACK_BYTES) : frames;
|
|
32
|
+
if (lines[i].includes(SCRIPT_URL))
|
|
33
|
+
last = i;
|
|
34
|
+
const stack = lines.slice(0, last + 1).join("\n");
|
|
35
|
+
return stack.length > STACK_BYTES ? stack.slice(0, STACK_BYTES) : stack;
|
|
75
36
|
}
|
|
76
|
-
function describe(err, kind
|
|
37
|
+
function describe(err, kind) {
|
|
77
38
|
if (err instanceof Error) {
|
|
78
|
-
return { kind, name: err.name, message: err.message, stack: scriptStack(err
|
|
39
|
+
return { kind, name: err.name, message: err.message, stack: scriptStack(err) };
|
|
79
40
|
}
|
|
80
41
|
// A script may throw a non-Error (`throw {code: "x"}`), so name/message must not assume one.
|
|
81
42
|
return { kind, name: "Thrown", message: safeText(err) };
|
|
@@ -88,23 +49,38 @@ function safeText(v) {
|
|
|
88
49
|
return String(v);
|
|
89
50
|
}
|
|
90
51
|
}
|
|
52
|
+
/** Unparseable code or an unresolvable import is broken code; anything else the import throws is
|
|
53
|
+
* the module's top level running, i.e. the script throwing. */
|
|
54
|
+
function unloadable(err) {
|
|
55
|
+
const code = err?.code;
|
|
56
|
+
return err instanceof SyntaxError || (typeof code === "string" && code.startsWith("ERR_MODULE"));
|
|
57
|
+
}
|
|
91
58
|
async function run(req) {
|
|
92
|
-
|
|
93
|
-
let
|
|
59
|
+
source = req.code;
|
|
60
|
+
let main;
|
|
94
61
|
try {
|
|
95
|
-
|
|
96
|
-
// never be hit. Repeated compilation is the price of the fresh global object.
|
|
97
|
-
fn = new AsyncFunction("input", "require", STRICT + req.code);
|
|
62
|
+
main = (await import(__rewriteRelativeImportExtension(SCRIPT_URL))).default;
|
|
98
63
|
}
|
|
99
64
|
catch (err) {
|
|
100
|
-
return { ok: false, failure: describe(err, "compile_error"
|
|
65
|
+
return { ok: false, failure: describe(err, unloadable(err) ? "compile_error" : "threw") };
|
|
66
|
+
}
|
|
67
|
+
if (typeof main !== "function") {
|
|
68
|
+
const got = main === undefined ? "no default export" : `a ${typeof main}`;
|
|
69
|
+
return {
|
|
70
|
+
ok: false,
|
|
71
|
+
failure: {
|
|
72
|
+
kind: "compile_error",
|
|
73
|
+
name: "NoDefaultExport",
|
|
74
|
+
message: `a script must export default a function; this one has ${got}`,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
101
77
|
}
|
|
102
78
|
let value;
|
|
103
79
|
try {
|
|
104
|
-
value = await
|
|
80
|
+
value = await main(req.input);
|
|
105
81
|
}
|
|
106
82
|
catch (err) {
|
|
107
|
-
return { ok: false, failure: describe(err, "threw"
|
|
83
|
+
return { ok: false, failure: describe(err, "threw") };
|
|
108
84
|
}
|
|
109
85
|
try {
|
|
110
86
|
// undefined stringifies to undefined, not "undefined"; an empty body is how genroc
|
|
@@ -112,11 +88,18 @@ async function run(req) {
|
|
|
112
88
|
return { ok: true, body: value === undefined ? "" : JSON.stringify(value) ?? "" };
|
|
113
89
|
}
|
|
114
90
|
catch (err) {
|
|
115
|
-
return { ok: false, failure: describe(err, "nonserializable"
|
|
91
|
+
return { ok: false, failure: describe(err, "nonserializable") };
|
|
116
92
|
}
|
|
117
93
|
}
|
|
94
|
+
// eval.ts terminates this thread the moment the reply lands, while stdio may still be in the
|
|
95
|
+
// pipe. An empty write's callback fires once the queue drains, making the reply a barrier.
|
|
96
|
+
function flush(stream) {
|
|
97
|
+
return new Promise((resolve) => stream.write("", () => resolve()));
|
|
98
|
+
}
|
|
118
99
|
// Non-null because this module only ever runs as a worker entry point; a null port here
|
|
119
100
|
// would mean eval.ts loaded it as a plain module, which nothing does.
|
|
120
101
|
parentPort.on("message", async (req) => {
|
|
121
|
-
|
|
102
|
+
const reply = await run(req);
|
|
103
|
+
await Promise.all([flush(process.stdout), flush(process.stderr)]);
|
|
104
|
+
parentPort.postMessage(reply);
|
|
122
105
|
});
|