@bastani/atomic-codemode 0.9.90-alpha.1 → 0.10.0-alpha.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/runtime/host.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAE1D,OAAO,EAAE,mBAAmB,EAAiC,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../../src/runtime/host.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAE1D,OAAO,EAAE,mBAAmB,EAAiC,MAAM,gBAAgB,CAAC;AA6FpF;;;;;;GAMG;AACH,qBAAa,eAAgB,SAAQ,mBAAmB;IACvD,YAAY,OAAO,GAAE,sBAA2B,EAE/C;CACD"}
|
package/dist/runtime/host.js
CHANGED
|
@@ -5,6 +5,15 @@ function defaultWorkerUrl() {
|
|
|
5
5
|
// `.ts` when running from source (tests, tsx), `.js` from the published dist.
|
|
6
6
|
return new URL(import.meta.url.endsWith(".ts") ? "./worker.ts" : "./worker.js", import.meta.url);
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* An error a stopping worker reports, or its termination's failure, reaches no run; it is only kept from being thrown.
|
|
10
|
+
* A module function, so a worker still exiting holds nothing of the run through it.
|
|
11
|
+
*/
|
|
12
|
+
function ignoreWorkerError() { }
|
|
13
|
+
/** Settles when `worker` exits, holding nothing of the run that started it. */
|
|
14
|
+
function workerExit(worker) {
|
|
15
|
+
return new Promise((resolve) => worker.once("exit", () => resolve()));
|
|
16
|
+
}
|
|
8
17
|
/**
|
|
9
18
|
* Each execution gets its own worker thread and QuickJS VM. A fresh worker per run keeps
|
|
10
19
|
* termination simple: a runaway script, including one that only spins the microtask queue, is
|
|
@@ -35,34 +44,43 @@ function workerLauncher(options) {
|
|
|
35
44
|
}
|
|
36
45
|
if (releaseWorker)
|
|
37
46
|
worker.once("exit", releaseWorker);
|
|
38
|
-
|
|
47
|
+
const onMessage = (message) => {
|
|
39
48
|
// Node 24/26 --watch leaks dependency reports onto the worker channel (nodejs/node#65044).
|
|
40
49
|
if (typeof message === "object" &&
|
|
41
50
|
message !== null &&
|
|
42
51
|
("watch:import" in message || "watch:require" in message))
|
|
43
52
|
return;
|
|
44
53
|
events.message(message);
|
|
45
|
-
}
|
|
46
|
-
|
|
54
|
+
};
|
|
55
|
+
const onError = (error) => {
|
|
47
56
|
events.failure({
|
|
48
57
|
kind: "sandbox",
|
|
49
58
|
name: error instanceof Error ? error.name : undefined,
|
|
50
59
|
message: errorMessage(error),
|
|
51
60
|
});
|
|
52
|
-
}
|
|
53
|
-
|
|
61
|
+
};
|
|
62
|
+
const onExit = (code) => {
|
|
54
63
|
events.failure({ kind: "sandbox", message: `Worker exited with code ${code} before the script settled` });
|
|
55
|
-
}
|
|
64
|
+
};
|
|
65
|
+
worker.on("message", onMessage);
|
|
66
|
+
worker.on("error", onError);
|
|
67
|
+
worker.on("exit", onExit);
|
|
56
68
|
const stop = async () => {
|
|
57
69
|
Atomics.store(new Int32Array(interrupt), 0, 1);
|
|
70
|
+
// A stopped run hears nothing more from its worker. The listeners reach the run's options and, through
|
|
71
|
+
// them, whatever the caller gave the run, so a worker still exiting must not keep them (or that) alive.
|
|
72
|
+
worker.off("message", onMessage);
|
|
73
|
+
worker.off("exit", onExit);
|
|
74
|
+
worker.off("error", onError);
|
|
75
|
+
worker.on("error", ignoreWorkerError);
|
|
58
76
|
// A non-interruptible built-in can delay Bun's terminate() until it returns (#10748).
|
|
59
77
|
// Do not hold up the result while that worker exits.
|
|
60
|
-
void worker.terminate().catch(
|
|
78
|
+
void worker.terminate().catch(ignoreWorkerError);
|
|
61
79
|
// terminate() refs the worker internally; unref it after requesting termination.
|
|
62
80
|
worker.unref();
|
|
63
81
|
};
|
|
64
82
|
try {
|
|
65
|
-
options.onWorkerStarted?.(
|
|
83
|
+
options.onWorkerStarted?.(workerExit(worker));
|
|
66
84
|
}
|
|
67
85
|
catch (error) {
|
|
68
86
|
void stop();
|
package/dist/runtime/host.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.js","sourceRoot":"","sources":["../../src/runtime/host.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C,OAAO,EAA2B,eAAe,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAmB,MAAM,gBAAgB,CAAC;AAGpF,SAAS,gBAAgB;IACxB,8EAA8E;IAC9E,OAAO,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,aAAa,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;AAClG,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,cAAc,CAAC,OAA+B;IACtD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QAC7B,IAAI,MAA0B,CAAC;QAC/B,IAAI,CAAC;YACJ,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,eAAe,EAAE,CAAC,CAAC;QACpD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,2BAA2B,YAAY,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACnE,CAAC;QACD,MAAM,SAAS,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAAC;QAC3C,MAAM,UAAU,GAAe,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QACpE,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,EAAE,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;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,KAAK,IAAI,EAAE,CAAC;YACZ,MAAM,KAAK,CAAC;QACb,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC;IACjE,CAAC,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,OAAO,eAAgB,SAAQ,mBAAmB;IACvD,YAAY,OAAO,GAA2B,EAAE;QAC/C,KAAK,CAAC,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;IACzC,CAAC;CACD","sourcesContent":["import { Worker } from \"node:worker_threads\";\nimport type { CodemodeSandboxOptions } from \"../types.ts\";\nimport { type CodemodeWasmModule, loadQuickJSWasm } from \"../wasm.ts\";\nimport { CodemodeSandboxBase, errorMessage, type VmLauncher } from \"./execution.ts\";\nimport type { WorkerData } from \"./protocol.ts\";\n\nfunction defaultWorkerUrl(): URL {\n\t// `.ts` when running from source (tests, tsx), `.js` from the published dist.\n\treturn new URL(import.meta.url.endsWith(\".ts\") ? \"./worker.ts\" : \"./worker.js\", import.meta.url);\n}\n\n/**\n * An error a stopping worker reports, or its termination's failure, reaches no run; it is only kept from being thrown.\n * A module function, so a worker still exiting holds nothing of the run through it.\n */\nfunction ignoreWorkerError(): void {}\n\n/** Settles when `worker` exits, holding nothing of the run that started it. */\nfunction workerExit(worker: Worker): Promise<void> {\n\treturn new Promise<void>((resolve) => worker.once(\"exit\", () => resolve()));\n}\n\n/**\n * Each execution gets its own worker thread and QuickJS VM. A fresh worker per run keeps\n * termination simple: a runaway script, including one that only spins the microtask queue, is\n * killed with `terminate()` and cannot poison a later run. Before terminating, the host sets a\n * shared interrupt flag that the VM polls, because Bun's `terminate()` cannot stop a thread that\n * is spinning in wasm. Stopping requests termination without waiting for exit: some built-ins do\n * not poll the flag, so on Bun a worker can outlive the script's result.\n */\nfunction workerLauncher(options: CodemodeSandboxOptions): VmLauncher {\n\treturn async (data, events) => {\n\t\tlet module: CodemodeWasmModule;\n\t\ttry {\n\t\t\tmodule = await (options.wasm ?? loadQuickJSWasm());\n\t\t} catch (error) {\n\t\t\tthrow new Error(`Failed to load QuickJS: ${errorMessage(error)}`);\n\t\t}\n\t\tconst interrupt = new SharedArrayBuffer(4);\n\t\tconst workerData: WorkerData = { ...data, wasm: module, interrupt };\n\t\tconst releaseWorker = options.acquireWorker?.();\n\t\tlet worker: Worker;\n\t\ttry {\n\t\t\tworker = new Worker(options.workerUrl ?? defaultWorkerUrl(), { workerData });\n\t\t} catch (error) {\n\t\t\treleaseWorker?.();\n\t\t\tthrow new Error(`Failed to start worker: ${errorMessage(error)}`);\n\t\t}\n\t\tif (releaseWorker) worker.once(\"exit\", releaseWorker);\n\t\tconst onMessage = (message: unknown) => {\n\t\t\t// Node 24/26 --watch leaks dependency reports onto the worker channel (nodejs/node#65044).\n\t\t\tif (\n\t\t\t\ttypeof message === \"object\" &&\n\t\t\t\tmessage !== null &&\n\t\t\t\t(\"watch:import\" in message || \"watch:require\" in message)\n\t\t\t)\n\t\t\t\treturn;\n\t\t\tevents.message(message);\n\t\t};\n\t\tconst onError = (error: unknown) => {\n\t\t\tevents.failure({\n\t\t\t\tkind: \"sandbox\",\n\t\t\t\tname: error instanceof Error ? error.name : undefined,\n\t\t\t\tmessage: errorMessage(error),\n\t\t\t});\n\t\t};\n\t\tconst onExit = (code: number) => {\n\t\t\tevents.failure({ kind: \"sandbox\", message: `Worker exited with code ${code} before the script settled` });\n\t\t};\n\t\tworker.on(\"message\", onMessage);\n\t\tworker.on(\"error\", onError);\n\t\tworker.on(\"exit\", onExit);\n\t\tconst stop = async (): Promise<void> => {\n\t\t\tAtomics.store(new Int32Array(interrupt), 0, 1);\n\t\t\t// A stopped run hears nothing more from its worker. The listeners reach the run's options and, through\n\t\t\t// them, whatever the caller gave the run, so a worker still exiting must not keep them (or that) alive.\n\t\t\tworker.off(\"message\", onMessage);\n\t\t\tworker.off(\"exit\", onExit);\n\t\t\tworker.off(\"error\", onError);\n\t\t\tworker.on(\"error\", ignoreWorkerError);\n\t\t\t// A non-interruptible built-in can delay Bun's terminate() until it returns (#10748).\n\t\t\t// Do not hold up the result while that worker exits.\n\t\t\tvoid worker.terminate().catch(ignoreWorkerError);\n\t\t\t// terminate() refs the worker internally; unref it after requesting termination.\n\t\t\tworker.unref();\n\t\t};\n\t\ttry {\n\t\t\toptions.onWorkerStarted?.(workerExit(worker));\n\t\t} catch (error) {\n\t\t\tvoid stop();\n\t\t\tthrow error;\n\t\t}\n\t\treturn { post: (message) => worker.postMessage(message), stop };\n\t};\n}\n\n/**\n * Runs JavaScript in a QuickJS VM (a separate wasm instance) inside a worker thread. The worker\n * keeps script execution off the host thread: QuickJS runs synchronously, so a spinning script on\n * the host thread would block its event loop. Requires `node:worker_threads` (Node or Bun); see\n * `InlineCodemodeSandbox` and `RemoteCodemodeSandbox` in `@bastani/atomic-codemode/portable`\n * for other runtimes.\n */\nexport class CodemodeSandbox extends CodemodeSandboxBase {\n\tconstructor(options: CodemodeSandboxOptions = {}) {\n\t\tsuper(options, workerLauncher(options));\n\t}\n}\n"]}
|
package/package.json
CHANGED