@endge/computation-sandbox 0.1.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.
- package/dist/EndgeComputationSandboxPlugin.d.ts +2 -0
- package/dist/QuickJSComputationSandbox.d.ts +18 -0
- package/dist/assets/computation-sandbox.worker-DNprzyNC.js +160453 -0
- package/dist/assets/emscripten-module.browser-kfxGthPQ.js +287 -0
- package/dist/assets/ffi-BwEcb6v9.js +8 -0
- package/dist/assets/module-ES6BEMUI-eYR24ymV.js +1020 -0
- package/dist/computation-sandbox.js +123 -0
- package/dist/main.d.ts +2 -0
- package/dist/worker/QuickJSComputationVM.d.ts +16 -0
- package/dist/worker/computation-sandbox.protocol.d.ts +20 -0
- package/dist/worker/computation-sandbox.worker.d.ts +1 -0
- package/package.json +39 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
var a = Object.defineProperty;
|
|
2
|
+
var h = (o, e, t) => e in o ? a(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
|
|
3
|
+
var s = (o, e, t) => h(o, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { Endge as n, EndgeModule as d } from "@endge/core";
|
|
5
|
+
class c {
|
|
6
|
+
constructor(e = {}) {
|
|
7
|
+
s(this, "workers");
|
|
8
|
+
s(this, "queue", []);
|
|
9
|
+
s(this, "nextId", 1);
|
|
10
|
+
s(this, "scheduled", !1);
|
|
11
|
+
s(this, "disposed", !1);
|
|
12
|
+
const t = typeof navigator > "u" ? 2 : navigator.hardwareConcurrency || 2, r = e.workerCount ?? Math.min(4, Math.max(1, t - 1)), i = e.watchdogMs ?? 500;
|
|
13
|
+
this.workers = Array.from({ length: r }, () => new l(i));
|
|
14
|
+
}
|
|
15
|
+
execute(e) {
|
|
16
|
+
return this.disposed ? Promise.reject(new Error("Computation sandbox is disposed.")) : new Promise((t, r) => {
|
|
17
|
+
this.queue.push({ id: this.nextId++, request: e, resolve: t, reject: r }), this.scheduled || (this.scheduled = !0, queueMicrotask(() => this.flush()));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
dispose() {
|
|
21
|
+
this.disposed = !0;
|
|
22
|
+
for (const e of this.queue) e.reject(new Error("Computation sandbox is disposed."));
|
|
23
|
+
this.queue.splice(0);
|
|
24
|
+
for (const e of this.workers) e.dispose();
|
|
25
|
+
}
|
|
26
|
+
flush() {
|
|
27
|
+
if (this.scheduled = !1, this.disposed || !this.queue.length)
|
|
28
|
+
return;
|
|
29
|
+
const e = Array.from({ length: this.workers.length }, () => []);
|
|
30
|
+
let t = 0;
|
|
31
|
+
for (; this.queue.length; ) e[t++ % e.length].push(this.queue.shift());
|
|
32
|
+
e.forEach((r, i) => {
|
|
33
|
+
r.length && this.workers[i].execute(r);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class l {
|
|
38
|
+
constructor(e) {
|
|
39
|
+
s(this, "worker", this.createWorker());
|
|
40
|
+
s(this, "queued", []);
|
|
41
|
+
s(this, "active", null);
|
|
42
|
+
s(this, "timer", null);
|
|
43
|
+
s(this, "busy", !1);
|
|
44
|
+
s(this, "disposed", !1);
|
|
45
|
+
this.watchdogMs = e;
|
|
46
|
+
}
|
|
47
|
+
execute(e) {
|
|
48
|
+
if (this.disposed) {
|
|
49
|
+
for (const t of e) t.reject(new Error("Computation Worker was disposed."));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.queued.push(e), this.pump();
|
|
53
|
+
}
|
|
54
|
+
dispose() {
|
|
55
|
+
this.disposed = !0, this.timer && clearTimeout(this.timer), this.timer = null, this.worker.terminate();
|
|
56
|
+
for (const e of this.active ?? []) e.reject(new Error("Computation Worker was disposed."));
|
|
57
|
+
for (const e of this.queued) for (const t of e) t.reject(new Error("Computation Worker was disposed."));
|
|
58
|
+
this.active = null, this.queued.splice(0), this.busy = !1;
|
|
59
|
+
}
|
|
60
|
+
pump() {
|
|
61
|
+
if (this.disposed || this.busy || !this.queued.length)
|
|
62
|
+
return;
|
|
63
|
+
this.busy = !0;
|
|
64
|
+
const e = this.queued.shift();
|
|
65
|
+
this.active = e, this.timer = setTimeout(() => {
|
|
66
|
+
this.worker.terminate(), this.worker = this.createWorker();
|
|
67
|
+
for (const t of e) t.reject(new Error(`Computation Worker exceeded ${this.watchdogMs} ms watchdog.`));
|
|
68
|
+
this.active = null, this.timer = null, this.busy = !1, this.pump();
|
|
69
|
+
}, this.watchdogMs), this.worker.onmessage = (t) => {
|
|
70
|
+
this.timer && clearTimeout(this.timer), this.timer = null;
|
|
71
|
+
const r = new Map(t.data.results.map((i) => [i.id, i]));
|
|
72
|
+
for (const i of e) {
|
|
73
|
+
const u = r.get(i.id);
|
|
74
|
+
u ? u.ok ? i.resolve(u.value) : i.reject(new Error(u.message)) : i.reject(new Error("Computation Worker returned no result."));
|
|
75
|
+
}
|
|
76
|
+
this.active = null, this.busy = !1, this.pump();
|
|
77
|
+
}, this.worker.onerror = (t) => {
|
|
78
|
+
this.timer && clearTimeout(this.timer), this.timer = null;
|
|
79
|
+
for (const r of e) r.reject(new Error(t.message || "Computation Worker failed."));
|
|
80
|
+
this.worker.terminate(), this.worker = this.createWorker(), this.active = null, this.busy = !1, this.pump();
|
|
81
|
+
}, this.worker.postMessage({
|
|
82
|
+
type: "execute-batch",
|
|
83
|
+
requests: e.map(({ id: t, request: r }) => ({ id: t, request: r }))
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
createWorker() {
|
|
87
|
+
return new Worker(new URL(
|
|
88
|
+
/* @vite-ignore */
|
|
89
|
+
"" + new URL("assets/computation-sandbox.worker-DNprzyNC.js", import.meta.url).href,
|
|
90
|
+
import.meta.url
|
|
91
|
+
), { type: "module" });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function m(o) {
|
|
95
|
+
return new c(o);
|
|
96
|
+
}
|
|
97
|
+
class p extends d {
|
|
98
|
+
constructor() {
|
|
99
|
+
super(...arguments);
|
|
100
|
+
s(this, "adapter", null);
|
|
101
|
+
}
|
|
102
|
+
setup() {
|
|
103
|
+
this.adapter = m(), n.runtime.computation.setSandboxAdapter(this.adapter);
|
|
104
|
+
}
|
|
105
|
+
reset() {
|
|
106
|
+
n.runtime.computation.setSandboxAdapter(null), this.adapter = null;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const k = {
|
|
110
|
+
id: "@endge/computation-sandbox",
|
|
111
|
+
install() {
|
|
112
|
+
n.defineModule({
|
|
113
|
+
key: "computationSandbox",
|
|
114
|
+
module: new p(),
|
|
115
|
+
before: "runtime"
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
export {
|
|
120
|
+
k as EndgeComputationSandboxPlugin,
|
|
121
|
+
c as QuickJSComputationSandbox,
|
|
122
|
+
m as createQuickJSComputationSandbox
|
|
123
|
+
};
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ComputationSandboxRequest } from '@endge/core';
|
|
2
|
+
export interface QuickJSComputationVMOptions {
|
|
3
|
+
executionTimeoutMs?: number;
|
|
4
|
+
memoryLimitBytes?: number;
|
|
5
|
+
stackLimitBytes?: number;
|
|
6
|
+
}
|
|
7
|
+
/** One isolated QuickJS VM factory used inside a Worker. */
|
|
8
|
+
export declare class QuickJSComputationVM {
|
|
9
|
+
private readonly quickJs;
|
|
10
|
+
private readonly moduleSources;
|
|
11
|
+
private readonly executionTimeoutMs;
|
|
12
|
+
private readonly memoryLimitBytes;
|
|
13
|
+
private readonly stackLimitBytes;
|
|
14
|
+
constructor(options?: QuickJSComputationVMOptions);
|
|
15
|
+
execute(request: ComputationSandboxRequest): Promise<unknown>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ComputationSandboxRequest } from '@endge/core';
|
|
2
|
+
export interface SandboxBatchRequest {
|
|
3
|
+
type: 'execute-batch';
|
|
4
|
+
requests: Array<{
|
|
5
|
+
id: number;
|
|
6
|
+
request: ComputationSandboxRequest;
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
export interface SandboxBatchResponse {
|
|
10
|
+
type: 'execute-batch-result';
|
|
11
|
+
results: Array<{
|
|
12
|
+
id: number;
|
|
13
|
+
ok: true;
|
|
14
|
+
value: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
id: number;
|
|
17
|
+
ok: false;
|
|
18
|
+
message: string;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@endge/computation-sandbox",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/computation-sandbox.js",
|
|
8
|
+
"module": "./dist/computation-sandbox.js",
|
|
9
|
+
"types": "./dist/main.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/main.d.ts",
|
|
13
|
+
"import": "./dist/computation-sandbox.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@jitl/quickjs-ng-wasmfile-release-sync": "^0.32.0",
|
|
21
|
+
"quickjs-emscripten-core": "^0.32.0",
|
|
22
|
+
"typescript": "^5.9.3"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@endge/core": "^1.2.7"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"rimraf": "^6.0.1",
|
|
29
|
+
"vite": "^6.1.0",
|
|
30
|
+
"vite-plugin-dts": "^4.5.0",
|
|
31
|
+
"vitest": "^2.1.9",
|
|
32
|
+
"@endge/core": "1.2.7"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "vite build",
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"typecheck": "tsc --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|