@endge/computation-sandbox 0.1.12 → 0.1.19

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.
@@ -5,14 +5,14 @@ export interface QuickJSComputationSandboxOptions {
5
5
  }
6
6
  /** Batched Worker pool adapter for the core computation runtime. */
7
7
  export declare class QuickJSComputationSandbox implements ComputationSandboxAdapter {
8
- private readonly workers;
9
- private readonly queue;
10
- private nextId;
11
- private scheduled;
12
- private disposed;
8
+ private readonly _workers;
9
+ private readonly _queue;
10
+ private _nextId;
11
+ private _scheduled;
12
+ private _disposed;
13
13
  constructor(options?: QuickJSComputationSandboxOptions);
14
14
  execute(request: ComputationSandboxRequest): Promise<unknown>;
15
15
  dispose(): void;
16
- private flush;
16
+ private _flush;
17
17
  }
18
18
  export declare function createQuickJSComputationSandbox(options?: QuickJSComputationSandboxOptions): QuickJSComputationSandbox;
@@ -161794,20 +161794,20 @@ Additional information: BADCLIENT: Bad error code, ${h} not found in range ${o}.
161794
161794
  var xft = Nft(), Hde = /* @__PURE__ */ Sft(xft);
161795
161795
  class Tft {
161796
161796
  constructor(Bt = {}) {
161797
- dN(this, "quickJs", vft(nft));
161798
- dN(this, "moduleSources", /* @__PURE__ */ new Map());
161799
- dN(this, "executionTimeoutMs");
161800
- dN(this, "memoryLimitBytes");
161801
- dN(this, "stackLimitBytes");
161802
- this.executionTimeoutMs = Bt.executionTimeoutMs ?? 250, this.memoryLimitBytes = Bt.memoryLimitBytes ?? 32 * 1024 * 1024, this.stackLimitBytes = Bt.stackLimitBytes ?? 512 * 1024;
161797
+ dN(this, "_quickJs", vft(nft));
161798
+ dN(this, "_moduleSources", /* @__PURE__ */ new Map());
161799
+ dN(this, "_executionTimeoutMs");
161800
+ dN(this, "_memoryLimitBytes");
161801
+ dN(this, "_stackLimitBytes");
161802
+ this._executionTimeoutMs = Bt.executionTimeoutMs ?? 250, this._memoryLimitBytes = Bt.memoryLimitBytes ?? 32 * 1024 * 1024, this._stackLimitBytes = Bt.stackLimitBytes ?? 512 * 1024;
161803
161803
  }
161804
161804
  async execute(Bt) {
161805
- Tz(Bt.inputs, "inputs"), this.moduleSources.has(Bt.moduleKey) || this.moduleSources.set(Bt.moduleKey, Rft(Bt.source));
161806
- const Lr = (await this.quickJs).newRuntime();
161807
- Lr.setMemoryLimit(this.memoryLimitBytes), Lr.setMaxStackSize(this.stackLimitBytes), Lr.setInterruptHandler(wft(Date.now() + this.executionTimeoutMs));
161805
+ Tz(Bt.inputs, "inputs"), this._moduleSources.has(Bt.moduleKey) || this._moduleSources.set(Bt.moduleKey, Rft(Bt.source));
161806
+ const Lr = (await this._quickJs).newRuntime();
161807
+ Lr.setMemoryLimit(this._memoryLimitBytes), Lr.setMaxStackSize(this._stackLimitBytes), Lr.setInterruptHandler(wft(Date.now() + this._executionTimeoutMs));
161808
161808
  const Qn = Lr.newContext();
161809
161809
  try {
161810
- const JA = Qn.evalCode(Mft(this.moduleSources.get(Bt.moduleKey), Bt.inputs));
161810
+ const JA = Qn.evalCode(Mft(this._moduleSources.get(Bt.moduleKey), Bt.inputs));
161811
161811
  if (JA.error) {
161812
161812
  const ra = Qn.dump(JA.error);
161813
161813
  throw JA.error.dispose(), new Error(Lft(ra));
@@ -161970,7 +161970,7 @@ function Tz(Ge, Bt, $e = /* @__PURE__ */ new Set()) {
161970
161970
  if (Ge === null || typeof Ge == "string" || typeof Ge == "boolean" || typeof Ge == "number" && Number.isFinite(Ge))
161971
161971
  return;
161972
161972
  if (typeof Ge != "object")
161973
- throw new Error(`Computation ${Bt} must be JSON-compatible.`);
161973
+ throw new TypeError(`Computation ${Bt} must be JSON-compatible.`);
161974
161974
  if ($e.has(Ge))
161975
161975
  throw new Error(`Computation ${Bt} must be JSON-compatible.`);
161976
161976
  $e.add(Ge);
@@ -161987,7 +161987,7 @@ function Lft(Ge) {
161987
161987
  return typeof Ge == "object" && Ge && "message" in Ge ? String(Ge.message) : String(Ge);
161988
161988
  }
161989
161989
  const Pft = new Tft();
161990
- self.onmessage = async (Ge) => {
161990
+ globalThis.onmessage = async (Ge) => {
161991
161991
  var $e;
161992
161992
  if ((($e = Ge.data) == null ? void 0 : $e.type) !== "execute-batch")
161993
161993
  return;
@@ -161999,7 +161999,7 @@ self.onmessage = async (Ge) => {
161999
161999
  } catch (Qn) {
162000
162000
  Bt.push({ id: Lr.id, ok: !1, message: Qn instanceof Error ? Qn.message : String(Qn) });
162001
162001
  }
162002
- self.postMessage({ type: "execute-batch-result", results: Bt });
162002
+ globalThis.postMessage({ type: "execute-batch-result", results: Bt });
162003
162003
  };
162004
162004
  var Uft = class {
162005
162005
  constructor(Ge) {
@@ -2,122 +2,131 @@ var a = Object.defineProperty;
2
2
  var h = (o, e, t) => e in o ? a(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
3
3
  var s = (o, e, t) => h(o, typeof e != "symbol" ? e + "" : e, t);
4
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 {
5
+ class _ {
38
6
  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;
7
+ s(this, "_worker", this._createWorker());
8
+ s(this, "_queued", []);
9
+ s(this, "_active", null);
10
+ s(this, "_timer", null);
11
+ s(this, "_busy", !1);
12
+ s(this, "_disposed", !1);
13
+ this._watchdogMs = e;
46
14
  }
47
15
  execute(e) {
48
- if (this.disposed) {
49
- for (const t of e) t.reject(new Error("Computation Worker was disposed."));
16
+ if (this._disposed) {
17
+ for (const t of e)
18
+ t.reject(new Error("Computation Worker was disposed."));
50
19
  return;
51
20
  }
52
- this.queued.push(e), this.pump();
21
+ this._queued.push(e), this._pump();
53
22
  }
54
23
  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;
24
+ this._disposed = !0, this._timer && clearTimeout(this._timer), this._timer = null, this._worker.terminate();
25
+ for (const e of this._active ?? [])
26
+ e.reject(new Error("Computation Worker was disposed."));
27
+ for (const e of this._queued)
28
+ for (const t of e)
29
+ t.reject(new Error("Computation Worker was disposed."));
30
+ this._active = null, this._queued.splice(0), this._busy = !1;
59
31
  }
60
- pump() {
61
- if (this.disposed || this.busy || !this.queued.length)
32
+ _pump() {
33
+ if (this._disposed || this._busy || !this._queued.length)
62
34
  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;
35
+ this._busy = !0;
36
+ const e = this._queued.shift();
37
+ this._active = e, this._timer = setTimeout(() => {
38
+ this._worker.terminate(), this._worker = this._createWorker();
39
+ for (const t of e)
40
+ t.reject(new Error(`Computation Worker exceeded ${this._watchdogMs} ms watchdog.`));
41
+ this._active = null, this._timer = null, this._busy = !1, this._pump();
42
+ }, this._watchdogMs), this._worker.onmessage = (t) => {
43
+ this._timer && clearTimeout(this._timer), this._timer = null;
71
44
  const r = new Map(t.data.results.map((i) => [i.id, i]));
72
45
  for (const i of e) {
73
46
  const u = r.get(i.id);
74
47
  u ? u.ok ? i.resolve(u.value) : i.reject(new Error(u.message)) : i.reject(new Error("Computation Worker returned no result."));
75
48
  }
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({
49
+ this._active = null, this._busy = !1, this._pump();
50
+ }, this._worker.onerror = (t) => {
51
+ this._timer && clearTimeout(this._timer), this._timer = null;
52
+ for (const r of e)
53
+ r.reject(new Error(t.message || "Computation Worker failed."));
54
+ this._worker.terminate(), this._worker = this._createWorker(), this._active = null, this._busy = !1, this._pump();
55
+ }, this._worker.postMessage({
82
56
  type: "execute-batch",
83
57
  requests: e.map(({ id: t, request: r }) => ({ id: t, request: r }))
84
58
  });
85
59
  }
86
- createWorker() {
60
+ _createWorker() {
87
61
  return new Worker(new URL(
88
62
  /* @vite-ignore */
89
- "" + new URL("assets/computation-sandbox.worker-DWFleMLd.js", import.meta.url).href,
63
+ "" + new URL("assets/computation-sandbox.worker-BX0goQiF.js", import.meta.url).href,
90
64
  import.meta.url
91
65
  ), { type: "module" });
92
66
  }
93
67
  }
94
- function m(o) {
68
+ class c {
69
+ constructor(e = {}) {
70
+ s(this, "_workers");
71
+ s(this, "_queue", []);
72
+ s(this, "_nextId", 1);
73
+ s(this, "_scheduled", !1);
74
+ s(this, "_disposed", !1);
75
+ const t = typeof navigator > "u" ? 2 : navigator.hardwareConcurrency || 2, r = e.workerCount ?? Math.min(4, Math.max(1, t - 1)), i = e.watchdogMs ?? 500;
76
+ this._workers = Array.from({ length: r }, () => new _(i));
77
+ }
78
+ execute(e) {
79
+ return this._disposed ? Promise.reject(new Error("Computation sandbox is disposed.")) : new Promise((t, r) => {
80
+ this._queue.push({ id: this._nextId++, request: e, resolve: t, reject: r }), this._scheduled || (this._scheduled = !0, queueMicrotask(() => this._flush()));
81
+ });
82
+ }
83
+ dispose() {
84
+ this._disposed = !0;
85
+ for (const e of this._queue)
86
+ e.reject(new Error("Computation sandbox is disposed."));
87
+ this._queue.splice(0);
88
+ for (const e of this._workers)
89
+ e.dispose();
90
+ }
91
+ _flush() {
92
+ if (this._scheduled = !1, this._disposed || !this._queue.length)
93
+ return;
94
+ const e = Array.from({ length: this._workers.length }, () => []);
95
+ let t = 0;
96
+ for (; this._queue.length; )
97
+ e[t++ % e.length].push(this._queue.shift());
98
+ e.forEach((r, i) => {
99
+ r.length && this._workers[i].execute(r);
100
+ });
101
+ }
102
+ }
103
+ function l(o) {
95
104
  return new c(o);
96
105
  }
97
- class p extends d {
106
+ class m extends d {
98
107
  constructor() {
99
108
  super(...arguments);
100
- s(this, "adapter", null);
109
+ s(this, "_adapter", null);
101
110
  }
102
111
  setup() {
103
- this.adapter = m(), n.runtime.computation.setSandboxAdapter(this.adapter);
112
+ this._adapter = l(), n.runtime.computation.setSandboxAdapter(this._adapter);
104
113
  }
105
114
  reset() {
106
- n.runtime.computation.setSandboxAdapter(null), this.adapter = null;
115
+ n.runtime.computation.setSandboxAdapter(null), this._adapter = null;
107
116
  }
108
117
  }
109
- const k = {
118
+ const w = {
110
119
  id: "@endge/computation-sandbox",
111
120
  install() {
112
121
  n.defineModule({
113
122
  key: "computationSandbox",
114
- module: new p(),
123
+ module: new m(),
115
124
  before: "runtime"
116
125
  });
117
126
  }
118
127
  };
119
128
  export {
120
- k as EndgeComputationSandboxPlugin,
129
+ w as EndgeComputationSandboxPlugin,
121
130
  c as QuickJSComputationSandbox,
122
- m as createQuickJSComputationSandbox
131
+ l as createQuickJSComputationSandbox
123
132
  };
@@ -6,11 +6,11 @@ export interface QuickJSComputationVMOptions {
6
6
  }
7
7
  /** One isolated QuickJS VM factory used inside a Worker. */
8
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;
9
+ private readonly _quickJs;
10
+ private readonly _moduleSources;
11
+ private readonly _executionTimeoutMs;
12
+ private readonly _memoryLimitBytes;
13
+ private readonly _stackLimitBytes;
14
14
  constructor(options?: QuickJSComputationVMOptions);
15
15
  execute(request: ComputationSandboxRequest): Promise<unknown>;
16
16
  }
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@endge/computation-sandbox",
3
- "version": "0.1.12",
3
+ "type": "module",
4
+ "version": "0.1.19",
4
5
  "private": false,
5
6
  "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
7
  "exports": {
11
8
  ".": {
12
9
  "types": "./dist/main.d.ts",
13
10
  "import": "./dist/computation-sandbox.js"
14
11
  }
15
12
  },
13
+ "main": "./dist/computation-sandbox.js",
14
+ "module": "./dist/computation-sandbox.js",
15
+ "types": "./dist/main.d.ts",
16
16
  "files": [
17
17
  "dist"
18
18
  ],
@@ -20,20 +20,24 @@
20
20
  "dev": "vite build --watch",
21
21
  "build": "vite build",
22
22
  "test": "vitest",
23
- "typecheck": "tsc --noEmit"
23
+ "typecheck": "tsc --noEmit",
24
+ "lint-check": "eslint . --cache",
25
+ "lint-fix": "eslint . --fix --cache"
26
+ },
27
+ "peerDependencies": {
28
+ "@endge/core": "^3.0.1 || ^4.0.0 || ^5.0.0"
24
29
  },
25
30
  "dependencies": {
26
31
  "@jitl/quickjs-ng-wasmfile-release-sync": "^0.32.0",
27
32
  "quickjs-emscripten-core": "^0.32.0",
28
33
  "typescript": "~6.0.3"
29
34
  },
30
- "peerDependencies": {
31
- "@endge/core": "^3.0.1"
32
- },
33
35
  "devDependencies": {
34
- "@endge/core": "^3.0.1",
36
+ "@antfu/eslint-config": "9.3.0",
37
+ "@endge/core": "^5.0.0",
35
38
  "@endge/raph": "^3.0.11",
36
- "@endge/utils": "^0.24.13",
39
+ "@endge/utils": "^0.25.0",
40
+ "eslint": "10.9.1",
37
41
  "rimraf": "^6.0.1",
38
42
  "unplugin-dts": "^1.0.3",
39
43
  "vite": "^6.1.0",