sandboxedjs 0.1.12 → 0.1.14

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/index.cjs CHANGED
@@ -22547,7 +22547,13 @@ async function loadFactory() {
22547
22547
  const { createRequire } = await nodeBuiltin("module");
22548
22548
  const fs = await nodeBuiltin("fs/promises");
22549
22549
  const path = await nodeBuiltin("path");
22550
- const require2 = createRequire(path.join(process.cwd(), "index.js"));
22550
+ const packageRequire = createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
22551
+ let require2 = packageRequire;
22552
+ try {
22553
+ require2.resolve("@ffmpeg/core/wasm");
22554
+ } catch {
22555
+ require2 = createRequire(path.join(process.cwd(), "index.js"));
22556
+ }
22551
22557
  const wasmPath = require2.resolve("@ffmpeg/core/wasm");
22552
22558
  const wasm = await fs.readFile(wasmPath);
22553
22559
  const module = await Promise.resolve().then(() => (init_ffmpeg_core(), ffmpeg_core_exports));
@@ -23675,6 +23681,146 @@ var Session = class {
23675
23681
  }
23676
23682
  };
23677
23683
 
23684
+ // src/runtime/node-child-process-bridge.ts
23685
+ var KernelChildProcess = class {
23686
+ pid;
23687
+ command;
23688
+ args;
23689
+ parentPid;
23690
+ state = "running";
23691
+ exitCode;
23692
+ stdout = "";
23693
+ stderr = "";
23694
+ listeners = /* @__PURE__ */ new Map();
23695
+ kernel;
23696
+ cred;
23697
+ cwd;
23698
+ env;
23699
+ stdin = new Pipe();
23700
+ process;
23701
+ started = false;
23702
+ cancelled = false;
23703
+ constructor(kernel, cred, config, pid) {
23704
+ this.kernel = kernel;
23705
+ this.cred = cred;
23706
+ this.pid = pid;
23707
+ this.command = config.command;
23708
+ this.args = config.args?.slice() ?? [];
23709
+ this.parentPid = config.parentPid;
23710
+ this.cwd = config.cwd ?? "/";
23711
+ this.env = { ...config.env };
23712
+ }
23713
+ on(event, listener) {
23714
+ let listeners = this.listeners.get(event);
23715
+ if (!listeners) {
23716
+ listeners = /* @__PURE__ */ new Set();
23717
+ this.listeners.set(event, listeners);
23718
+ }
23719
+ listeners.add(listener);
23720
+ return this;
23721
+ }
23722
+ emit(event, ...args) {
23723
+ for (const listener of this.listeners.get(event) ?? []) listener(...args);
23724
+ }
23725
+ exec() {
23726
+ if (this.started) return;
23727
+ this.started = true;
23728
+ queueMicrotask(() => {
23729
+ if (this.cancelled) {
23730
+ this.finish(143);
23731
+ return;
23732
+ }
23733
+ const stdout = new ChildOutput(this, "stdout");
23734
+ const stderr = new ChildOutput(this, "stderr");
23735
+ try {
23736
+ this.process = this.kernel.spawn([this.command, ...this.args], {
23737
+ cwd: this.cwd,
23738
+ env: this.env,
23739
+ cred: this.cred,
23740
+ stdin: this.stdin,
23741
+ stdout,
23742
+ stderr,
23743
+ ppid: 1
23744
+ });
23745
+ void this.process.wait().then((code) => {
23746
+ stdout.end();
23747
+ stderr.end();
23748
+ this.finish(code);
23749
+ });
23750
+ } catch (error) {
23751
+ stderr.write(error instanceof Error ? error.message : String(error));
23752
+ stderr.end();
23753
+ stdout.end();
23754
+ this.finish(1);
23755
+ }
23756
+ });
23757
+ }
23758
+ sendStdin(data) {
23759
+ try {
23760
+ this.stdin.write(data);
23761
+ } catch {
23762
+ }
23763
+ }
23764
+ kill(signal = "SIGTERM") {
23765
+ if (this.process) {
23766
+ this.process.deliver(signal);
23767
+ return;
23768
+ }
23769
+ this.cancelled = true;
23770
+ }
23771
+ finish(code) {
23772
+ if (this.state === "exited") return;
23773
+ this.state = "exited";
23774
+ this.exitCode = code;
23775
+ this.emit("exit", code);
23776
+ }
23777
+ append(stream, text) {
23778
+ this[stream] += text;
23779
+ this.emit(stream, text);
23780
+ }
23781
+ };
23782
+ var ChildOutput = class {
23783
+ constructor(child, stream) {
23784
+ this.child = child;
23785
+ this.stream = stream;
23786
+ }
23787
+ child;
23788
+ stream;
23789
+ isTTY = false;
23790
+ closedState = false;
23791
+ decoder = new TextDecoder();
23792
+ get closed() {
23793
+ return this.closedState;
23794
+ }
23795
+ write(data) {
23796
+ if (this.closedState) return;
23797
+ const text = typeof data === "string" ? data : this.decoder.decode(data, { stream: true });
23798
+ if (text) this.child.append(this.stream, text);
23799
+ }
23800
+ end() {
23801
+ if (this.closedState) return;
23802
+ this.closedState = true;
23803
+ const tail2 = this.decoder.decode();
23804
+ if (tail2) this.child.append(this.stream, tail2);
23805
+ }
23806
+ };
23807
+ var nextBridgePid = 1073741824;
23808
+ function installNodeChildProcessBridge(pod, kernel, cred) {
23809
+ const manager = pod.processManager;
23810
+ const originalSpawn = manager.spawn;
23811
+ manager.spawn = (config) => {
23812
+ const resolved = kernel.resolveExecutable(config.command, config.cwd ?? "/", config.env ?? {}, cred);
23813
+ if (resolved?.kind === "builtin" && resolved.command?.name !== "node" && resolved.command?.name !== "nodejs") {
23814
+ return new KernelChildProcess(kernel, cred, config, nextBridgePid++);
23815
+ }
23816
+ return originalSpawn.call(manager, config);
23817
+ };
23818
+ return () => {
23819
+ if (manager.spawn === originalSpawn) return;
23820
+ manager.spawn = originalSpawn;
23821
+ };
23822
+ }
23823
+
23678
23824
  // src/container/container.ts
23679
23825
  var Container = class _Container {
23680
23826
  kernel;
@@ -23685,6 +23831,7 @@ var Container = class _Container {
23685
23831
  hooks;
23686
23832
  disposed = false;
23687
23833
  defaultSession = null;
23834
+ restoreNodeChildProcessBridge;
23688
23835
  constructor(kernel, pod, opts) {
23689
23836
  this.kernel = kernel;
23690
23837
  this.pod = pod;
@@ -23713,6 +23860,7 @@ var Container = class _Container {
23713
23860
  };
23714
23861
  if (opts.timeoutMs !== void 0) this.defaults.timeoutMs = opts.timeoutMs;
23715
23862
  this.hooks = { onStdout: opts.onStdout, onStderr: opts.onStderr };
23863
+ this.restoreNodeChildProcessBridge = installNodeChildProcessBridge(pod, kernel, cred);
23716
23864
  }
23717
23865
  // ── boot ──────────────────────────────────────────────────────────────────
23718
23866
  static async create(opts = {}) {
@@ -24123,6 +24271,7 @@ var Container = class _Container {
24123
24271
  dispose() {
24124
24272
  if (this.disposed) return;
24125
24273
  this.disposed = true;
24274
+ this.restoreNodeChildProcessBridge();
24126
24275
  this.kernel.dispose();
24127
24276
  try {
24128
24277
  this.pod.teardown();