@genroc/eval-node 0.0.0-edge.f58b8b6 → 0.0.0-edge.f71f2603
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 +114 -306
- package/dist/eval.js +9 -17
- package/dist/import.js +64 -100
- package/dist/realm.js +12 -17
- package/dist/worker.js +61 -76
- package/eval.ts +13 -23
- package/import.ts +83 -118
- package/package.json +1 -1
- package/realm.ts +12 -17
- package/worker.ts +66 -82
package/eval.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// the containment stays swappable.
|
|
5
|
-
//
|
|
6
|
-
// The containment is a Worker per execution (realm.ts). It is what makes the budget real:
|
|
7
|
-
// a synchronous busy loop never yields, so no in-process timer can interrupt it, and only a
|
|
8
|
-
// thread the host can kill bounds it.
|
|
1
|
+
// Imports a script module in its own realm (a Worker per execution, realm.ts), calls its default
|
|
2
|
+
// export, and classifies every outcome (README.md). Only a killable thread bounds a synchronous
|
|
3
|
+
// busy loop, which is what makes the budget real. Knows nothing of genroc; worker.ts does.
|
|
9
4
|
|
|
10
5
|
import { Worker } from "node:worker_threads";
|
|
11
6
|
|
|
@@ -16,9 +11,8 @@ export type EvalRequest = {
|
|
|
16
11
|
timeout_ms?: number;
|
|
17
12
|
};
|
|
18
13
|
|
|
19
|
-
/** Every kind
|
|
20
|
-
*
|
|
21
|
-
* faults releases its claim and lets another worker take the task. */
|
|
14
|
+
/** Every kind is PERMANENT: a retry fails identically. A faulting runner is not an outcome; it
|
|
15
|
+
* releases its claim. */
|
|
22
16
|
export type FailureKind = "compile_error" | "threw" | "timeout" | "nonserializable" | "exited";
|
|
23
17
|
|
|
24
18
|
export type EvalFailure = {
|
|
@@ -28,9 +22,8 @@ export type EvalFailure = {
|
|
|
28
22
|
stack?: string;
|
|
29
23
|
};
|
|
30
24
|
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
* crosses the worker boundary — structured clone would refuse a different set of values. */
|
|
25
|
+
/** JSON TEXT serialised in the realm, so a nonserializable return is a script fault, not a 500,
|
|
26
|
+
* and structured clone (which refuses a different set of values) never sees the value. */
|
|
34
27
|
export type EvalResult =
|
|
35
28
|
| { ok: true; body: string }
|
|
36
29
|
| { ok: false; failure: EvalFailure };
|
|
@@ -47,9 +40,8 @@ const REALM_URL = new URL(
|
|
|
47
40
|
import.meta.url,
|
|
48
41
|
);
|
|
49
42
|
|
|
50
|
-
/** Thrown, not returned: a realm that fails to start is the
|
|
51
|
-
* answers by releasing the claim
|
|
52
|
-
* return value. */
|
|
43
|
+
/** Thrown, not returned: a realm that fails to start is the runner faulting, which worker.ts
|
|
44
|
+
* answers by releasing the claim. */
|
|
53
45
|
class RealmFault extends Error {
|
|
54
46
|
constructor(message: string) {
|
|
55
47
|
super(message);
|
|
@@ -57,9 +49,8 @@ class RealmFault extends Error {
|
|
|
57
49
|
}
|
|
58
50
|
}
|
|
59
51
|
|
|
60
|
-
/** Thrown when `signal` aborts: the
|
|
61
|
-
*
|
|
62
|
-
* runner did not fault -- nothing is wrong, the answer is simply no longer wanted. */
|
|
52
|
+
/** Thrown when `signal` aborts: the answer is no longer wanted, so worker.ts releases rather
|
|
53
|
+
* than answers. Not a RealmFault: nothing is wrong. */
|
|
63
54
|
export class Cancelled extends Error {
|
|
64
55
|
constructor() {
|
|
65
56
|
super("cancelled");
|
|
@@ -84,9 +75,8 @@ export async function evaluate(req: EvalRequest, signal?: AbortSignal): Promise<
|
|
|
84
75
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
85
76
|
}
|
|
86
77
|
worker.once("message", (reply: WorkerReply) => resolve(reply));
|
|
87
|
-
// A script may
|
|
88
|
-
//
|
|
89
|
-
// this too, by which time the promise has settled and the first result stands.
|
|
78
|
+
// A script may `process.exit()`, which would otherwise look like a hang until the budget.
|
|
79
|
+
// Our own terminate() fires this too, after the promise has settled.
|
|
90
80
|
worker.once("exit", (code: number) => resolve(exited(code)));
|
|
91
81
|
worker.once("error", (err: Error) => reject(new RealmFault(errorText(err))));
|
|
92
82
|
worker.postMessage({ code: req.code, input: req.input } satisfies WorkerRequest);
|
package/import.ts
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The
|
|
3
|
-
// with
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
// Two modes, one binary: "types" writes the declarations an editor needs and returns no
|
|
7
|
-
// code; "build" typechecks and bundles. A separate types hook would mean a second `tsc`
|
|
8
|
-
// over the same project.
|
|
9
|
-
|
|
10
|
-
import { spawn } from "node:child_process";
|
|
2
|
+
// The typed-phase resolver: manifest on stdin, `{"values": [...]}` on stdout, diagnostic on stderr
|
|
3
|
+
// with a non-zero exit (specs/source-resolution.md). Two modes share one `tsc` pass: "generate"
|
|
4
|
+
// writes declarations only, "resolve" typechecks and bundles.
|
|
5
|
+
|
|
11
6
|
import { existsSync } from "node:fs";
|
|
12
|
-
import { access,
|
|
7
|
+
import { access, writeFile } from "node:fs/promises";
|
|
13
8
|
import { builtinModules } from "node:module";
|
|
14
|
-
import { dirname, join,
|
|
15
|
-
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { dirname, join, resolve } from "node:path";
|
|
16
10
|
|
|
17
11
|
import commonjs from "@rollup/plugin-commonjs";
|
|
18
12
|
import json from "@rollup/plugin-json";
|
|
@@ -31,9 +25,9 @@ type Site = {
|
|
|
31
25
|
child?: string;
|
|
32
26
|
// The slot as a path of keys and indices, so nothing has to unescape a JSON Pointer.
|
|
33
27
|
pointer: (string | number)[];
|
|
34
|
-
//
|
|
35
|
-
// this resolver's reading of
|
|
36
|
-
|
|
28
|
+
// The argument's words, split as sh splits single quotes. genctl does not read them as paths —
|
|
29
|
+
// that is this resolver's reading of them.
|
|
30
|
+
args: string[];
|
|
37
31
|
// What .genroc asked genctl to type, keyed by the name it chose. This resolver reads Input
|
|
38
32
|
// and Output; which ADDRESS each came from is the config's business, not ours.
|
|
39
33
|
types?: Record<string, Schema>;
|
|
@@ -51,8 +45,7 @@ type ManifestProcess = {
|
|
|
51
45
|
};
|
|
52
46
|
|
|
53
47
|
type Manifest = {
|
|
54
|
-
mode: "
|
|
55
|
-
root: string;
|
|
48
|
+
mode: "generate" | "resolve";
|
|
56
49
|
processes: ManifestProcess[];
|
|
57
50
|
};
|
|
58
51
|
|
|
@@ -70,12 +63,6 @@ async function exists(path: string): Promise<boolean> {
|
|
|
70
63
|
}
|
|
71
64
|
}
|
|
72
65
|
|
|
73
|
-
/** Creates the parent directory, which `.genroc-cache/` relies on: nothing else makes it. */
|
|
74
|
-
async function write(path: string, content: string): Promise<void> {
|
|
75
|
-
await mkdir(dirname(path), { recursive: true });
|
|
76
|
-
await writeFile(path, content);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
66
|
// ── JSON Schema → TypeScript ───────────────────────────────────────────────────
|
|
80
67
|
|
|
81
68
|
/** Only genroc's keyword set is handled; anything else its strict decoder would have
|
|
@@ -137,16 +124,21 @@ function objectType(s: Schema, used: Set<string>): string {
|
|
|
137
124
|
? ` /** ${sub.description} */\n`
|
|
138
125
|
: "";
|
|
139
126
|
lines.push(
|
|
140
|
-
`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${tsType(sub, used)};`,
|
|
127
|
+
`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${nest(tsType(sub, used))};`,
|
|
141
128
|
);
|
|
142
129
|
}
|
|
143
130
|
if (s.additionalProperties && typeof s.additionalProperties === "object") {
|
|
144
|
-
lines.push(
|
|
131
|
+
lines.push(
|
|
132
|
+
` [key: string]: ${nest(tsType(s.additionalProperties, used))};`,
|
|
133
|
+
);
|
|
145
134
|
}
|
|
146
135
|
if (lines.length === 0) return "Record<string, unknown>";
|
|
147
136
|
return `{\n${lines.join("\n")}\n}`;
|
|
148
137
|
}
|
|
149
138
|
|
|
139
|
+
/** Every line but the first, which is already placed by the property that opens it. */
|
|
140
|
+
const nest = (t: string) => t.replace(/\n/g, "\n ");
|
|
141
|
+
|
|
150
142
|
function union(parts: string[]): string {
|
|
151
143
|
const seen = [...new Set(parts)];
|
|
152
144
|
return seen.length === 0 ? "unknown" : seen.join(" | ");
|
|
@@ -157,17 +149,6 @@ const propKey = (k: string) => (IDENT.test(k) ? k : JSON.stringify(k));
|
|
|
157
149
|
const identifier = (n: string) =>
|
|
158
150
|
IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`;
|
|
159
151
|
|
|
160
|
-
function deref(
|
|
161
|
-
s: Schema | undefined,
|
|
162
|
-
defs: Record<string, Schema>,
|
|
163
|
-
): Schema | undefined {
|
|
164
|
-
let cur = s;
|
|
165
|
-
for (let i = 0; cur && typeof cur.$ref === "string" && i < 16; i++) {
|
|
166
|
-
cur = defs[cur.$ref.replace(/^#\/\$defs\//, "")];
|
|
167
|
-
}
|
|
168
|
-
return cur;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
152
|
/** Emits one named type per reachable $def rather than inlining: a task output may
|
|
172
153
|
* reference itself (specs/recursive-type-inference.md) and inlining would not terminate. */
|
|
173
154
|
function declarations(at: Located): string {
|
|
@@ -188,7 +169,7 @@ function declarations(at: Located): string {
|
|
|
188
169
|
}
|
|
189
170
|
|
|
190
171
|
return [
|
|
191
|
-
"// Generated by genroc. Do not edit - regenerate with `genctl
|
|
172
|
+
"// Generated by genroc. Do not edit - regenerate with `genctl generate`.",
|
|
192
173
|
`// ${where.name} (${address(site.pointer)})`,
|
|
193
174
|
"",
|
|
194
175
|
...emitted,
|
|
@@ -208,9 +189,8 @@ function typesPathFor(scriptPath: string): string {
|
|
|
208
189
|
|
|
209
190
|
// ── typecheck ──────────────────────────────────────────────────────────────────
|
|
210
191
|
|
|
211
|
-
/** The
|
|
212
|
-
*
|
|
213
|
-
* the project root: above it is not this project. */
|
|
192
|
+
/** The tsconfig the author's editor reads, so editor and apply agree. Stops at the project
|
|
193
|
+
* root. */
|
|
214
194
|
async function nearestTsconfig(
|
|
215
195
|
from: string,
|
|
216
196
|
root: string,
|
|
@@ -223,12 +203,7 @@ async function nearestTsconfig(
|
|
|
223
203
|
}
|
|
224
204
|
|
|
225
205
|
async function typecheck(sites: Located[]): Promise<void> {
|
|
226
|
-
//
|
|
227
|
-
// coexist with it. The suffix is what keeps the scratch area out of its way.
|
|
228
|
-
const dir = join(root, ".genroc-cache");
|
|
229
|
-
await write(join(dir, ".gitignore"), "*\n");
|
|
230
|
-
|
|
231
|
-
// One tsc per distinct base config: `extends` takes a single base, so merging two would
|
|
206
|
+
// One program per distinct base config: `extends` takes a single base, so merging two would
|
|
232
207
|
// check each script under the other author's options.
|
|
233
208
|
const groups = new Map<string, Located[]>();
|
|
234
209
|
for (const at of sites) {
|
|
@@ -238,10 +213,10 @@ async function typecheck(sites: Located[]): Promise<void> {
|
|
|
238
213
|
else groups.set(base, [at]);
|
|
239
214
|
}
|
|
240
215
|
|
|
241
|
-
|
|
216
|
+
const diagnostics: ts.Diagnostic[] = [];
|
|
242
217
|
for (const [base, group] of groups) {
|
|
243
218
|
const config: Record<string, unknown> = {
|
|
244
|
-
...(base ? { extends:
|
|
219
|
+
...(base ? { extends: base } : {}),
|
|
245
220
|
compilerOptions: {
|
|
246
221
|
noEmit: true,
|
|
247
222
|
strict: true,
|
|
@@ -252,46 +227,45 @@ async function typecheck(sites: Located[]): Promise<void> {
|
|
|
252
227
|
// `lib` DESCRIBES the realm and is written after `extends` so a base cannot widen it:
|
|
253
228
|
// a worker thread has no document, whatever an author's config claims.
|
|
254
229
|
lib: ["esnext", "webworker"],
|
|
255
|
-
// `types` is the author's
|
|
256
|
-
//
|
|
257
|
-
// base config there is nothing to opt in with, so the default stays none.
|
|
230
|
+
// `types` is the author's opt-in to node globals, which the realm has. With no base
|
|
231
|
+
// config there is nothing to opt in with, so it stays none.
|
|
258
232
|
...(base ? {} : { types: [] }),
|
|
259
233
|
},
|
|
260
|
-
files: group.flatMap((s) => [
|
|
261
|
-
relative(dir, s.file),
|
|
262
|
-
relative(dir, typesPathFor(s.file)),
|
|
263
|
-
]),
|
|
234
|
+
files: group.flatMap((s) => [s.file, typesPathFor(s.file)]),
|
|
264
235
|
// `files` overrides the base's, but a base `include` survives beside it and would
|
|
265
236
|
// drag the author's whole tree in, to be checked under the worker lib.
|
|
266
237
|
include: [],
|
|
267
238
|
};
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
239
|
+
// Never written; anchors `@types` lookup at the root. Not `tsconfig.json`: that is often the
|
|
240
|
+
// base it extends, and tsc refuses a circular extends.
|
|
241
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
242
|
+
config,
|
|
243
|
+
ts.sys,
|
|
244
|
+
root,
|
|
245
|
+
undefined,
|
|
246
|
+
join(root, "tsconfig.genroc.json"),
|
|
271
247
|
);
|
|
272
|
-
|
|
273
|
-
|
|
248
|
+
const program = ts.createProgram({
|
|
249
|
+
rootNames: parsed.fileNames,
|
|
250
|
+
options: parsed.options,
|
|
251
|
+
projectReferences: parsed.projectReferences,
|
|
252
|
+
configFileParsingDiagnostics: parsed.errors,
|
|
253
|
+
});
|
|
254
|
+
diagnostics.push(...parsed.errors, ...ts.getPreEmitDiagnostics(program));
|
|
274
255
|
}
|
|
275
|
-
}
|
|
276
256
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
proc.on("close", (c) => resolve(c ?? 1));
|
|
290
|
-
});
|
|
291
|
-
if (code !== 0) {
|
|
292
|
-
// tsc reports on stdout; the exit code IS the type check, so this is the diagnostic
|
|
293
|
-
// genctl surfaces and the reason a failed import never produces a string.
|
|
294
|
-
die([out, err].filter(Boolean).join("\n").trimEnd());
|
|
257
|
+
if (diagnostics.length > 0) {
|
|
258
|
+
// The diagnostics are the whole type check: this is what genctl surfaces, and the reason a
|
|
259
|
+
// failed import never produces a string.
|
|
260
|
+
die(
|
|
261
|
+
ts
|
|
262
|
+
.formatDiagnostics(diagnostics, {
|
|
263
|
+
getCanonicalFileName: (f) => f,
|
|
264
|
+
getCurrentDirectory: () => root,
|
|
265
|
+
getNewLine: () => "\n",
|
|
266
|
+
})
|
|
267
|
+
.trimEnd(),
|
|
268
|
+
);
|
|
295
269
|
}
|
|
296
270
|
}
|
|
297
271
|
|
|
@@ -321,11 +295,9 @@ const BUILTIN = new Set([
|
|
|
321
295
|
...builtinModules.map((m) => `node:${m}`),
|
|
322
296
|
]);
|
|
323
297
|
|
|
324
|
-
/** Resolves
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
* A package resolving to a `.d.ts` is declined — that is a type, not the implementation —
|
|
328
|
-
* which leaves node_modules to nodeResolve. */
|
|
298
|
+
/** Resolves through TYPESCRIPT under the typecheck's config, so a `paths` alias that compiles
|
|
299
|
+
* also bundles. A `.d.ts` resolution is declined (a type, not code), leaving node_modules to
|
|
300
|
+
* nodeResolve. */
|
|
329
301
|
function tsResolve(configPath: string | null): Plugin {
|
|
330
302
|
let options: ts.CompilerOptions = {};
|
|
331
303
|
if (configPath) {
|
|
@@ -355,14 +327,12 @@ function tsResolve(configPath: string | null): Plugin {
|
|
|
355
327
|
};
|
|
356
328
|
}
|
|
357
329
|
|
|
358
|
-
/**
|
|
359
|
-
*
|
|
360
|
-
* Bundling is entirely the importer's job, so a definition version pins its code forever. */
|
|
330
|
+
/** A self-contained ES module whose default export is the author's own, unwrapped. Bundling here
|
|
331
|
+
* means a definition version pins its code forever. */
|
|
361
332
|
async function bundle(at: Located): Promise<string> {
|
|
362
333
|
const site = at.site;
|
|
363
|
-
// Builtins are
|
|
364
|
-
//
|
|
365
|
-
// of a module that will not be there, which bundles clean and fails at runtime.
|
|
334
|
+
// Builtins are externalised; anything else unresolved is a REFUSAL, since rollup's default
|
|
335
|
+
// leaves an import that bundles clean and fails at runtime.
|
|
366
336
|
const built = await rollup({
|
|
367
337
|
input: at.file,
|
|
368
338
|
external: (id) => BUILTIN.has(id),
|
|
@@ -370,8 +340,7 @@ async function bundle(at: Located): Promise<string> {
|
|
|
370
340
|
tsResolve(await nearestTsconfig(dirname(at.file), root)),
|
|
371
341
|
nodeResolve({ extensions: [".ts", ".tsx", ".mjs", ".js", ".json"] }),
|
|
372
342
|
commonjs(),
|
|
373
|
-
//
|
|
374
|
-
// did natively; without it rollup hands the JSON to the JS parser.
|
|
343
|
+
// Inlines `.json` imports; without it rollup hands JSON to the JS parser.
|
|
375
344
|
json(),
|
|
376
345
|
transpile,
|
|
377
346
|
],
|
|
@@ -414,10 +383,8 @@ const root = process.cwd();
|
|
|
414
383
|
if (!manifest || !Array.isArray(manifest.processes))
|
|
415
384
|
die("stdin is not a genroc resolver manifest");
|
|
416
385
|
|
|
417
|
-
/**
|
|
418
|
-
*
|
|
419
|
-
* joining it to the definition's directory is this resolver's business: only it knows the
|
|
420
|
-
* argument is a path at all. */
|
|
386
|
+
/** Sites in manifest order, the order `code` must answer in. The argument is joined to the
|
|
387
|
+
* definition's directory here: only this resolver knows it is a path. */
|
|
421
388
|
type Located = { site: Site; where: ManifestProcess; file: string };
|
|
422
389
|
|
|
423
390
|
/** A pointer as the address it is, so a generated comment or an error can be pasted into
|
|
@@ -433,22 +400,22 @@ function address(pointer: (string | number)[]): string {
|
|
|
433
400
|
.replace(/^\./, "");
|
|
434
401
|
}
|
|
435
402
|
const located: Located[] = manifest.processes.flatMap((where) =>
|
|
436
|
-
where.sites.map((site) =>
|
|
437
|
-
site
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
403
|
+
where.sites.map((site) => {
|
|
404
|
+
const [script, ...rest] = site.args;
|
|
405
|
+
if (script === undefined || rest.length > 0) {
|
|
406
|
+
die(
|
|
407
|
+
`${join(where.dir, where.file)}: ${address(site.pointer)}: $import takes one script, ` +
|
|
408
|
+
`and is given ${site.args.length} arguments`,
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
return { site, where, file: resolve(where.dir, script) };
|
|
412
|
+
}),
|
|
441
413
|
);
|
|
442
414
|
|
|
443
|
-
//
|
|
444
|
-
//
|
|
445
|
-
// `child` is the shape the scaffold uses — a call to a process that forwards to the evaluator —
|
|
446
|
-
// and `external` is the same request made directly.
|
|
415
|
+
// The `code`-field contract is this resolver's, not genctl's, so it checks the directive landed
|
|
416
|
+
// there: in a child call (the scaffold's shape) or an external task's input.
|
|
447
417
|
for (const at of located) {
|
|
448
|
-
//
|
|
449
|
-
// it the directive sits is the pointer. A child call is the shape the scaffold generates — to a
|
|
450
|
-
// process that forwards to the evaluator — and an external task is the same request made
|
|
451
|
-
// directly.
|
|
418
|
+
// The action's kind is a field; where in it the directive sits is the pointer.
|
|
452
419
|
const kind = at.site.level === "action" ? (at.site.action ?? "") : "";
|
|
453
420
|
const slot = at.site.pointer.slice(-2).join(".");
|
|
454
421
|
if ((kind !== "child" && kind !== "external") || slot !== "input.code") {
|
|
@@ -460,13 +427,12 @@ for (const at of located) {
|
|
|
460
427
|
}
|
|
461
428
|
}
|
|
462
429
|
|
|
463
|
-
// genctl
|
|
464
|
-
// does is the one that must say when it is not there.
|
|
430
|
+
// genctl does not know the argument is a file, so this resolver reports a missing one.
|
|
465
431
|
for (const at of located) {
|
|
466
432
|
if (!existsSync(at.file)) {
|
|
467
433
|
die(
|
|
468
434
|
`${join(at.where.dir, at.where.file)}: ${address(at.site.pointer)}: ` +
|
|
469
|
-
`"$import: ${at.site.
|
|
435
|
+
`"$import: ${at.site.args[0]}" names no file (looked at ${at.file})`,
|
|
470
436
|
);
|
|
471
437
|
}
|
|
472
438
|
}
|
|
@@ -490,18 +456,17 @@ for (const at of located) {
|
|
|
490
456
|
}
|
|
491
457
|
|
|
492
458
|
for (const at of byPath.values()) {
|
|
493
|
-
await
|
|
459
|
+
await writeFile(typesPathFor(at.file), declarations(at));
|
|
494
460
|
}
|
|
495
461
|
|
|
496
|
-
if (manifest.mode === "
|
|
497
|
-
console.log(JSON.stringify(manifest, null, 2));
|
|
462
|
+
if (manifest.mode === "generate") {
|
|
498
463
|
process.exit(0);
|
|
499
464
|
}
|
|
500
465
|
|
|
501
466
|
await typecheck([...byPath.values()]);
|
|
502
467
|
|
|
503
|
-
const
|
|
468
|
+
const values: string[] = [];
|
|
504
469
|
for (const at of located) {
|
|
505
|
-
|
|
470
|
+
values.push(await bundle(at));
|
|
506
471
|
}
|
|
507
|
-
process.stdout.write(JSON.stringify({
|
|
472
|
+
process.stdout.write(JSON.stringify({ values }));
|
package/package.json
CHANGED
package/realm.ts
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
// The evaluation realm
|
|
2
|
-
//
|
|
3
|
-
// eval.ts owns the budget and does the killing; nothing here knows about time.
|
|
4
|
-
//
|
|
5
|
-
// Everything that touches the script's VALUE lives on this side of the boundary — loading,
|
|
6
|
-
// classifying, serialising — because this is the only realm the value exists in.
|
|
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.
|
|
7
3
|
|
|
8
4
|
import { registerHooks } from "node:module";
|
|
9
5
|
import { parentPort } from "node:worker_threads";
|
|
10
6
|
|
|
11
7
|
import type { EvalFailure, FailureKind, WorkerReply, WorkerRequest } from "./eval.ts";
|
|
12
8
|
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
// the way it does everywhere else.
|
|
9
|
+
// IMPORTED under our own URL rather than compiled from a string, so frames carry the author's
|
|
10
|
+
// line numbers and builtin imports resolve normally.
|
|
16
11
|
const SCRIPT_URL = "script:main";
|
|
17
12
|
const STACK_BYTES = 2_048;
|
|
18
13
|
|
|
@@ -40,7 +35,10 @@ function scriptStack(err: unknown): string | undefined {
|
|
|
40
35
|
|
|
41
36
|
function describe(err: unknown, kind: FailureKind): EvalFailure {
|
|
42
37
|
if (err instanceof Error) {
|
|
43
|
-
|
|
38
|
+
// A subclass inherits `name` from Error.prototype, so `class LimitExceeded extends Error {}`
|
|
39
|
+
// would report "Error"; a name the script set still wins.
|
|
40
|
+
const name = err.name === "Error" && err.constructor.name ? err.constructor.name : err.name;
|
|
41
|
+
return { kind, name, message: err.message, stack: scriptStack(err) };
|
|
44
42
|
}
|
|
45
43
|
// A script may throw a non-Error (`throw {code: "x"}`), so name/message must not assume one.
|
|
46
44
|
return { kind, name: "Thrown", message: safeText(err) };
|
|
@@ -54,9 +52,8 @@ function safeText(v: unknown): string {
|
|
|
54
52
|
}
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
/**
|
|
58
|
-
*
|
|
59
|
-
* which is the script throwing. */
|
|
55
|
+
/** Unparseable code or an unresolvable import is broken code; anything else the import throws is
|
|
56
|
+
* the module's top level running, i.e. the script throwing. */
|
|
60
57
|
function unloadable(err: unknown): boolean {
|
|
61
58
|
const code = (err as { code?: unknown } | null)?.code;
|
|
62
59
|
return err instanceof SyntaxError || (typeof code === "string" && code.startsWith("ERR_MODULE"));
|
|
@@ -99,10 +96,8 @@ async function run(req: WorkerRequest): Promise<WorkerReply> {
|
|
|
99
96
|
}
|
|
100
97
|
}
|
|
101
98
|
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
// write's callback fires once the queue ahead of it has drained, which makes the reply a barrier
|
|
105
|
-
// for `console` and a direct process.stdout.write alike: both are this stream.
|
|
99
|
+
// eval.ts terminates this thread the moment the reply lands, while stdio may still be in the
|
|
100
|
+
// pipe. An empty write's callback fires once the queue drains, making the reply a barrier.
|
|
106
101
|
function flush(stream: NodeJS.WriteStream): Promise<void> {
|
|
107
102
|
return new Promise((resolve) => stream.write("", () => resolve()));
|
|
108
103
|
}
|