@tangle-network/sdk-cli-runner 0.3.6 → 0.3.7

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.
Files changed (2) hide show
  1. package/dist/index.mjs +92 -7
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -6,6 +6,9 @@ import { AsyncLocalStorage } from "node:async_hooks";
6
6
  import { createHash, randomUUID } from "node:crypto";
7
7
  import { closeSync, constants, fstatSync, fsyncSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, unlinkSync, writeSync } from "node:fs";
8
8
  import { open } from "node:fs/promises";
9
+ import { once } from "node:events";
10
+ import { tmpdir } from "node:os";
11
+ import { PassThrough } from "node:stream";
9
12
  //#region src/attachment-error.ts
10
13
  /** An attachment cannot be delivered to a native CLI. */
11
14
  var InvalidAttachmentError = class extends Error {
@@ -976,6 +979,78 @@ var RollingByteCapture = class {
976
979
  }
977
980
  };
978
981
  //#endregion
982
+ //#region src/stdout-file.ts
983
+ const POLL_MS = 20;
984
+ const CHUNK_BYTES = 256 * 1024;
985
+ /**
986
+ * A child's stdout as an unlinked regular file the parent follows.
987
+ *
988
+ * Bun CLIs such as OpenCode end with `process.exit()`, which drops stdout
989
+ * still queued for a pipe: the last event arrives cut, or not at all (OpenCode
990
+ * 1.19.4 delivered 134 of 400 200 KB lines, exit 0). A write to a regular file
991
+ * has landed when it returns, so exit cannot lose it.
992
+ */
993
+ var StdoutFile = class {
994
+ fd;
995
+ position = 0;
996
+ ended;
997
+ stream = new PassThrough();
998
+ constructor() {
999
+ const path = join(tmpdir(), `cli-stdout-${randomUUID()}`);
1000
+ this.fd = openSync(path, "wx+", 384);
1001
+ unlinkSync(path);
1002
+ this.ended = once(this.stream, "end");
1003
+ }
1004
+ stdio(stdio = [
1005
+ "pipe",
1006
+ "pipe",
1007
+ "pipe"
1008
+ ]) {
1009
+ const io = Array.isArray(stdio) ? [...stdio] : [
1010
+ stdio,
1011
+ stdio,
1012
+ stdio
1013
+ ];
1014
+ io[1] = this.fd;
1015
+ return io;
1016
+ }
1017
+ /** The child never started: nothing will follow the file. */
1018
+ close() {
1019
+ closeSync(this.fd);
1020
+ }
1021
+ /** Stand in for `child.stdout`; the stream ends once the child is gone and every byte is read. */
1022
+ follow(child) {
1023
+ child.stdout = this.stream;
1024
+ const chunk = Buffer.allocUnsafe(CHUNK_BYTES);
1025
+ const drain = () => {
1026
+ for (let n = readSync(this.fd, chunk, 0, CHUNK_BYTES, this.position); n > 0; n = readSync(this.fd, chunk, 0, CHUNK_BYTES, this.position)) {
1027
+ this.position += n;
1028
+ this.stream.write(Buffer.from(chunk.subarray(0, n)));
1029
+ }
1030
+ };
1031
+ let timer = setTimeout(function poll() {
1032
+ drain();
1033
+ timer = setTimeout(poll, POLL_MS);
1034
+ }, 0);
1035
+ let finished = false;
1036
+ const finish = () => {
1037
+ if (finished) return;
1038
+ finished = true;
1039
+ clearTimeout(timer);
1040
+ drain();
1041
+ this.close();
1042
+ this.stream.end();
1043
+ };
1044
+ child.once("exit", finish);
1045
+ child.once("error", finish);
1046
+ }
1047
+ };
1048
+ /** Parsed stdout goes through a file; raw or ignored stdout keeps the caller's stdio. */
1049
+ function stdoutFileFor(stdio, parsed) {
1050
+ const out = Array.isArray(stdio) ? stdio[1] : stdio ?? "pipe";
1051
+ return parsed && out === "pipe" ? new StdoutFile() : void 0;
1052
+ }
1053
+ //#endregion
979
1054
  //#region src/stream-parser.ts
980
1055
  /**
981
1056
  * Creates a stdout stream parser. Three modes:
@@ -1051,10 +1126,12 @@ function spawnCliProcess(config, parserConfig) {
1051
1126
  assertRawCaptureStdio(config);
1052
1127
  let killer;
1053
1128
  const capture = openProcessCapture(config, () => killer?.kill("SIGTERM"));
1129
+ const stdout = stdoutFileFor(config.spawnOptions?.stdio, !!parserConfig);
1054
1130
  let child;
1055
1131
  try {
1056
- child = spawnChild(config, processEnv, detached);
1132
+ child = spawnChild(config, processEnv, detached, stdout);
1057
1133
  } catch (error) {
1134
+ stdout?.close();
1058
1135
  capture.closeAfterSpawnFailure(error);
1059
1136
  throw error;
1060
1137
  }
@@ -1063,7 +1140,8 @@ function spawnCliProcess(config, parserConfig) {
1063
1140
  launch: { child },
1064
1141
  killer,
1065
1142
  capture,
1066
- detached
1143
+ detached,
1144
+ stdout
1067
1145
  });
1068
1146
  }
1069
1147
  /**
@@ -1080,6 +1158,7 @@ async function launchCliProcess(config, parserConfig) {
1080
1158
  assertRawCaptureStdio(config);
1081
1159
  let killer;
1082
1160
  const capture = openProcessCapture(config, () => killer?.kill("SIGTERM"));
1161
+ const stdout = stdoutFileFor(config.spawnOptions?.stdio, !!parserConfig);
1083
1162
  let launch;
1084
1163
  try {
1085
1164
  const launched = launcher({
@@ -1087,7 +1166,7 @@ async function launchCliProcess(config, parserConfig) {
1087
1166
  args: config.args,
1088
1167
  cwd: config.cwd,
1089
1168
  env: buildChildEnv(config),
1090
- stdio: config.spawnOptions?.stdio ?? [
1169
+ stdio: stdout?.stdio(config.spawnOptions?.stdio) ?? config.spawnOptions?.stdio ?? [
1091
1170
  "pipe",
1092
1171
  "pipe",
1093
1172
  "pipe"
@@ -1097,6 +1176,7 @@ async function launchCliProcess(config, parserConfig) {
1097
1176
  });
1098
1177
  launch = launched instanceof Promise ? await launched : launched;
1099
1178
  } catch (error) {
1179
+ stdout?.close();
1100
1180
  capture.closeAfterSpawnFailure(error);
1101
1181
  throw error;
1102
1182
  }
@@ -1105,7 +1185,8 @@ async function launchCliProcess(config, parserConfig) {
1105
1185
  launch,
1106
1186
  killer,
1107
1187
  capture,
1108
- detached
1188
+ detached,
1189
+ stdout
1109
1190
  });
1110
1191
  }
1111
1192
  function isDetached(config) {
@@ -1117,6 +1198,7 @@ function openProcessCapture(config, onFailure) {
1117
1198
  function attachCliProcess(config, parserConfig, run) {
1118
1199
  const { launch, killer, capture, detached } = run;
1119
1200
  const child = launch.child;
1201
+ run.stdout?.follow(child);
1120
1202
  const spawnedAt = Date.now();
1121
1203
  const stdin = watchStdinFailure(child, killer, config.stdinFailure);
1122
1204
  const group = config.ownProcessGroup && detached && process.platform !== "win32" && child.pid ? new CliProcessGroup(child.pid) : void 0;
@@ -1133,7 +1215,8 @@ function attachCliProcess(config, parserConfig, run) {
1133
1215
  abort,
1134
1216
  group,
1135
1217
  killer,
1136
- release: launch.release?.bind(launch)
1218
+ release: launch.release?.bind(launch),
1219
+ stdoutEnded: run.stdout?.ended
1137
1220
  });
1138
1221
  return {
1139
1222
  pid: child.pid,
@@ -1174,7 +1257,7 @@ function assertRawCaptureStdio(config) {
1174
1257
  const io = config.spawnOptions.stdio;
1175
1258
  if (!Array.isArray(io) || io.length !== 3 || !["pipe", "ignore"].includes(String(io[0])) || io[1] !== "pipe" || io[2] !== "pipe") throw new Error("raw capture requires piped stdout and stderr, with pipe or ignored stdin");
1176
1259
  }
1177
- function spawnChild(config, processEnv, detached) {
1260
+ function spawnChild(config, processEnv, detached, stdout) {
1178
1261
  const { command, args, cwd, spawnOptions, isolatedIdentity } = config;
1179
1262
  const isolated = isolatedIdentity !== void 0;
1180
1263
  if (isolated && (process.platform !== "linux" || process.getuid?.() !== 0 || !Number.isSafeInteger(isolatedIdentity.uid) || isolatedIdentity.uid <= 0 || !Number.isSafeInteger(isolatedIdentity.gid) || isolatedIdentity.gid <= 0 || spawnOptions?.uid !== isolatedIdentity.uid || spawnOptions?.gid !== isolatedIdentity.gid)) throw new Error("Isolated CLI identity requires a matching root-owned Linux launch");
@@ -1198,7 +1281,8 @@ function spawnChild(config, processEnv, detached) {
1198
1281
  "pipe"
1199
1282
  ],
1200
1283
  detached,
1201
- ...isolated ? safeSpawnOptions : spawnOptions
1284
+ ...isolated ? safeSpawnOptions : spawnOptions,
1285
+ ...stdout ? { stdio: stdout.stdio(spawnOptions?.stdio) } : {}
1202
1286
  });
1203
1287
  }
1204
1288
  function watchStdinFailure(child, killer, mode = "stop") {
@@ -1266,6 +1350,7 @@ function createExitPromise(child, run) {
1266
1350
  return new Promise((resolve) => {
1267
1351
  child.once("close", async (code, exitSignal) => {
1268
1352
  deadlines.clearAll();
1353
+ await run.stdoutEnded;
1269
1354
  endParser?.();
1270
1355
  const processGroupError = await group?.stop().then(() => void 0, (error) => error instanceof Error ? error.message : String(error));
1271
1356
  const terminateError = await killer.terminated();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/sdk-cli-runner",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Generic CLI process spawning with stdin/stdout streaming and JSONL parsing",
5
5
  "type": "module",
6
6
  "main": "./dist/index.mjs",
@@ -26,8 +26,8 @@
26
26
  "registry": "https://registry.npmjs.org"
27
27
  },
28
28
  "tangleRelease": {
29
- "sourceSha": "4ebbdbe755569ab872dd1b5b540bbd0688c00dea",
30
- "mergedAt": "2026-11-02T08:29:09-08:00"
29
+ "sourceSha": "edbead512639304013f5386111711912e45d0679",
30
+ "mergedAt": "2026-11-04T12:32:11-08:00"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "tsdown",