@ai-ecoverse/slicc-kernel 1.69.0 → 1.70.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/dist/node.js CHANGED
@@ -5387,6 +5387,18 @@ function capturing(chunks2, max = Number.POSITIVE_INFINITY, overflow) {
5387
5387
  size += bytes.length;
5388
5388
  };
5389
5389
  }
5390
+ function captureFile(onData, control) {
5391
+ if (!control) return sinkFile(onData);
5392
+ control.opened = true;
5393
+ return new OpenFile({
5394
+ write: async (bytes) => {
5395
+ if (control.shut) throw new KernelError("EPIPE");
5396
+ onData(bytes.slice());
5397
+ return bytes.length;
5398
+ },
5399
+ close: () => control.ended()
5400
+ });
5401
+ }
5390
5402
  function concat2(chunks2) {
5391
5403
  const out = new Uint8Array(chunks2.reduce((n, c) => n + c.length, 0));
5392
5404
  let offset = 0;
@@ -5421,7 +5433,12 @@ var ChildTable = class {
5421
5433
  const fds = new FdTable();
5422
5434
  const captured = /* @__PURE__ */ new Map();
5423
5435
  try {
5424
- for (const [n, slot2] of stdio.entries()) fds.installAt(n, this.openSlot(slot2, n, captured));
5436
+ for (const [n, slot2] of stdio.entries()) {
5437
+ fds.installAt(
5438
+ n,
5439
+ "sameAs" in slot2 ? fds.get(slot2.sameAs).retain() : this.openSlot(slot2, n, captured)
5440
+ );
5441
+ }
5425
5442
  for (const slot2 of inherit2) {
5426
5443
  if (slot2.fd <= 2) continue;
5427
5444
  if ("device" in slot2) {
@@ -5478,7 +5495,7 @@ var ChildTable = class {
5478
5495
  if ("capture" in slot2) {
5479
5496
  const chunks2 = [];
5480
5497
  captured.set(n, chunks2);
5481
- return sinkFile(capturing(chunks2, slot2.max, slot2.overflow));
5498
+ return captureFile(capturing(chunks2, slot2.max, slot2.overflow), slot2.control);
5482
5499
  }
5483
5500
  return deviceFile("null");
5484
5501
  }
@@ -10333,9 +10350,11 @@ function capLimit(maxBuffer) {
10333
10350
  return Math.min(asked, RUN_CAPTURE_CAP);
10334
10351
  }
10335
10352
  function slot(how, n, extra) {
10353
+ if (how === "stdout") return { sameAs: 1 };
10354
+ if (typeof how === "number") return { fd: how };
10336
10355
  if (how === "inherit") return { fd: n };
10337
10356
  if (how === "ignore") return { none: true };
10338
- return extra;
10357
+ return extra();
10339
10358
  }
10340
10359
  function frame2(outcome, out, err) {
10341
10360
  const head = new TextEncoder().encode(JSON.stringify(outcome));
@@ -10365,12 +10384,20 @@ async function runChild(req, deps) {
10365
10384
  if (failure !== void 0) return;
10366
10385
  failure = code;
10367
10386
  if (pid !== 0) kill();
10387
+ decided.resolve();
10388
+ };
10389
+ const controls = [];
10390
+ const capture = () => {
10391
+ const { promise, resolve } = Promise.withResolvers();
10392
+ const control = { shut: false, ended: resolve, eof: promise };
10393
+ controls.push(control);
10394
+ return { capture: true, max, overflow: () => stop("ENOBUFS"), control };
10368
10395
  };
10369
- const capture = () => ({ capture: true, max, overflow: () => stop("ENOBUFS") });
10396
+ const decided = Promise.withResolvers();
10370
10397
  const stdio = [
10371
- slot(req.stdin, 0, { input: req.input ?? new Uint8Array(0) }),
10372
- slot(req.stdout, 1, capture()),
10373
- slot(req.stderr, 2, capture())
10398
+ slot(req.stdin, 0, () => ({ input: req.input ?? new Uint8Array(0) })),
10399
+ slot(req.stdout, 1, capture),
10400
+ slot(req.stderr, 2, capture)
10374
10401
  ];
10375
10402
  try {
10376
10403
  pid = await deps.children.spawn(
@@ -10387,8 +10414,11 @@ async function runChild(req, deps) {
10387
10414
  let status;
10388
10415
  try {
10389
10416
  status = await reaped(deps, pid);
10417
+ const open = controls.filter((c) => c.opened);
10418
+ await Promise.race([Promise.all(open.map((c) => c.eof)), decided.promise]);
10390
10419
  } finally {
10391
10420
  clearTimeout(timer);
10421
+ for (const control of controls) control.shut = true;
10392
10422
  }
10393
10423
  const out = deps.children.captured(pid, 1);
10394
10424
  const err = deps.children.captured(pid, 2);