@eryx-org/eryx 0.0.0 → 0.15.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/README.md +281 -4
- package/eryx-sandbox.conditional-host-import-0.wasm +0 -0
- package/eryx-sandbox.conditional-host-import-1.wasm +0 -0
- package/eryx-sandbox.conditional-suspending-1-i32-to-i32.wasm +0 -0
- package/eryx-sandbox.core.wasm +0 -0
- package/eryx-sandbox.core10.wasm +0 -0
- package/eryx-sandbox.core11.wasm +0 -0
- package/eryx-sandbox.core12.wasm +0 -0
- package/eryx-sandbox.core13.wasm +0 -0
- package/eryx-sandbox.core2.wasm +0 -0
- package/eryx-sandbox.core3.wasm +0 -0
- package/eryx-sandbox.core4.wasm +0 -0
- package/eryx-sandbox.core5.wasm +0 -0
- package/eryx-sandbox.core6.wasm +0 -0
- package/eryx-sandbox.core7.wasm +0 -0
- package/eryx-sandbox.core8.wasm +0 -0
- package/eryx-sandbox.core9.wasm +0 -0
- package/eryx-sandbox.d.ts +163 -0
- package/eryx-sandbox.js +43541 -0
- package/index.d.ts +331 -0
- package/index.js +698 -0
- package/instance.js +174 -0
- package/interfaces/eryx-net-tcp.d.ts +30 -0
- package/interfaces/eryx-net-tls.d.ts +24 -0
- package/interfaces/wasi-cli-environment.d.ts +3 -0
- package/interfaces/wasi-cli-exit.d.ts +3 -0
- package/interfaces/wasi-cli-stderr.d.ts +3 -0
- package/interfaces/wasi-cli-stdin.d.ts +3 -0
- package/interfaces/wasi-cli-stdout.d.ts +3 -0
- package/interfaces/wasi-cli-terminal-input.d.ts +8 -0
- package/interfaces/wasi-cli-terminal-output.d.ts +8 -0
- package/interfaces/wasi-cli-terminal-stderr.d.ts +3 -0
- package/interfaces/wasi-cli-terminal-stdin.d.ts +3 -0
- package/interfaces/wasi-cli-terminal-stdout.d.ts +3 -0
- package/interfaces/wasi-clocks-monotonic-clock.d.ts +8 -0
- package/interfaces/wasi-clocks-wall-clock.d.ts +7 -0
- package/interfaces/wasi-filesystem-preopens.d.ts +3 -0
- package/interfaces/wasi-filesystem-types.d.ts +207 -0
- package/interfaces/wasi-io-error.d.ts +8 -0
- package/interfaces/wasi-io-poll.d.ts +10 -0
- package/interfaces/wasi-io-streams.d.ts +33 -0
- package/interfaces/wasi-random-random.d.ts +2 -0
- package/interfaces/wasi-sockets-instance-network.d.ts +3 -0
- package/interfaces/wasi-sockets-ip-name-lookup.d.ts +15 -0
- package/interfaces/wasi-sockets-network.d.ts +92 -0
- package/interfaces/wasi-sockets-tcp-create-socket.d.ts +5 -0
- package/interfaces/wasi-sockets-tcp.d.ts +52 -0
- package/interfaces/wasi-sockets-udp-create-socket.d.ts +5 -0
- package/interfaces/wasi-sockets-udp.d.ts +51 -0
- package/package.json +69 -3
- package/python-stdlib.tar.gz +0 -0
- package/shims/callbacks.d.ts +45 -0
- package/shims/callbacks.js +469 -0
- package/shims/ip.js +46 -0
- package/shims/net-fetch.js +264 -0
- package/shims/net-node.js +236 -0
- package/shims/net.d.ts +84 -0
- package/shims/net.js +255 -0
- package/shims/preview2-node.js +35 -0
- package/shims/raw-json.js +111 -0
- package/shims/sockets.js +53 -0
- package/stdlib-loader.js +174 -0
- package/worker.js +100 -0
package/worker.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker entry for a Sandbox created with `timeoutMs` (see index.js).
|
|
3
|
+
*
|
|
4
|
+
* Runs one sandbox instance off the creating thread, so that thread can
|
|
5
|
+
* terminate the Worker when a call overruns its deadline. Callbacks, output,
|
|
6
|
+
* traces and networking are forwarded to the creating thread, which keeps the
|
|
7
|
+
* handlers, the journal and the network policy.
|
|
8
|
+
*
|
|
9
|
+
* Protocol (all messages are plain objects):
|
|
10
|
+
* - in: {type: "call", id, method, args, callbacks, options}
|
|
11
|
+
* {type: "reply", id, value} | {type: "reply", id, error}
|
|
12
|
+
* - out: {type: "ready"} | {type: "failed", error}
|
|
13
|
+
* {type: "result", id, value} | {type: "result", id, error}
|
|
14
|
+
* {type: "request", id, target, args} awaits a "reply"
|
|
15
|
+
* {type: "notify", target, args} no reply
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { _fileTree, decodeError, encodeError, instantiateSandbox, isNode } from "./instance.js";
|
|
19
|
+
import { sleep } from "./shims/callbacks.js";
|
|
20
|
+
|
|
21
|
+
const port = isNode ? (await import("node:worker_threads")).parentPort : globalThis;
|
|
22
|
+
const post = (message) => port.postMessage(message);
|
|
23
|
+
const onMessage = (fn) =>
|
|
24
|
+
isNode ? port.on("message", fn) : port.addEventListener("message", (e) => fn(e.data));
|
|
25
|
+
|
|
26
|
+
let nextId = 0;
|
|
27
|
+
const pending = new Map();
|
|
28
|
+
|
|
29
|
+
/** Run `target` on the creating thread and wait for its result. */
|
|
30
|
+
function request(target, args) {
|
|
31
|
+
const id = nextId++;
|
|
32
|
+
post({ type: "request", id, target, args });
|
|
33
|
+
return new Promise((resolve, reject) => pending.set(id, { resolve, reject }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const forward = (target) => (...args) => request(target, args);
|
|
37
|
+
const notify = (target) => (...args) => post({ type: "notify", target, args });
|
|
38
|
+
|
|
39
|
+
// Set from each call, so the sync imports answer like the host's own would.
|
|
40
|
+
let callbacks = [];
|
|
41
|
+
let options = { pythonTracing: false, reuseEmptyCallbacks: false };
|
|
42
|
+
|
|
43
|
+
const cb = {
|
|
44
|
+
invoke: forward("invoke"),
|
|
45
|
+
sleep,
|
|
46
|
+
listCallbacks: () => callbacks,
|
|
47
|
+
getExecutionOptions: () => options,
|
|
48
|
+
reportTrace: notify("reportTrace"),
|
|
49
|
+
reportOutput: notify("reportOutput"),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const net = {
|
|
53
|
+
tcp: {
|
|
54
|
+
connect: forward("tcp.connect"),
|
|
55
|
+
read: forward("tcp.read"),
|
|
56
|
+
write: forward("tcp.write"),
|
|
57
|
+
close: notify("tcp.close"),
|
|
58
|
+
},
|
|
59
|
+
tls: {
|
|
60
|
+
upgrade: forward("tls.upgrade"),
|
|
61
|
+
read: forward("tls.read"),
|
|
62
|
+
write: forward("tls.write"),
|
|
63
|
+
close: notify("tls.close"),
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
let instance;
|
|
68
|
+
|
|
69
|
+
onMessage(async (message) => {
|
|
70
|
+
switch (message.type) {
|
|
71
|
+
case "call":
|
|
72
|
+
callbacks = message.callbacks;
|
|
73
|
+
options = message.options;
|
|
74
|
+
try {
|
|
75
|
+
const value = await instance[message.method](...message.args);
|
|
76
|
+
post({ type: "result", id: message.id, value });
|
|
77
|
+
} catch (e) {
|
|
78
|
+
post({ type: "result", id: message.id, ...encodeError(e) });
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case "reply": {
|
|
82
|
+
const { resolve, reject } = pending.get(message.id);
|
|
83
|
+
pending.delete(message.id);
|
|
84
|
+
if ("value" in message) resolve(message.value);
|
|
85
|
+
else reject(decodeError(message));
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Instantiate as soon as the listener is up; the creating thread waits for
|
|
92
|
+
// "ready" before its first call.
|
|
93
|
+
try {
|
|
94
|
+
// Core modules are compiled by the creating thread, so workers don't
|
|
95
|
+
// compile them again.
|
|
96
|
+
instance = await instantiateSandbox(_fileTree, cb, net, (name) => request("module", [name]));
|
|
97
|
+
post({ type: "ready" });
|
|
98
|
+
} catch (e) {
|
|
99
|
+
post({ type: "failed", ...encodeError(e) });
|
|
100
|
+
}
|