@tangle-network/sdk-cli-runner 0.3.6 → 0.3.8
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.mjs +98 -12
- 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 {
|
|
@@ -775,13 +778,15 @@ function readProcessIdentity(pid) {
|
|
|
775
778
|
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
776
779
|
const fields = stat.slice(stat.lastIndexOf(")") + 2).trim().split(/\s+/);
|
|
777
780
|
const group = Number(fields[2]);
|
|
778
|
-
|
|
781
|
+
const state = fields[0];
|
|
782
|
+
const start = fields[19];
|
|
783
|
+
if (!Number.isSafeInteger(group) || !start || !state) throw new Error(`Cannot read process identity for ${pid}`);
|
|
779
784
|
return {
|
|
780
785
|
pid,
|
|
781
786
|
parent: Number(fields[1]),
|
|
782
787
|
group,
|
|
783
|
-
start
|
|
784
|
-
state
|
|
788
|
+
start,
|
|
789
|
+
state
|
|
785
790
|
};
|
|
786
791
|
} catch (error) {
|
|
787
792
|
if (["ENOENT", "ESRCH"].includes(error.code ?? "")) return void 0;
|
|
@@ -804,8 +809,7 @@ var OwnedProcessTree = class {
|
|
|
804
809
|
capture() {
|
|
805
810
|
const queue = [...this.processes.values()];
|
|
806
811
|
const seen = /* @__PURE__ */ new Set();
|
|
807
|
-
for (
|
|
808
|
-
const expected = queue[index];
|
|
812
|
+
for (const expected of queue) {
|
|
809
813
|
const key = `${expected.pid}:${expected.start}`;
|
|
810
814
|
if (seen.has(key)) continue;
|
|
811
815
|
seen.add(key);
|
|
@@ -976,6 +980,78 @@ var RollingByteCapture = class {
|
|
|
976
980
|
}
|
|
977
981
|
};
|
|
978
982
|
//#endregion
|
|
983
|
+
//#region src/stdout-file.ts
|
|
984
|
+
const POLL_MS = 20;
|
|
985
|
+
const CHUNK_BYTES = 256 * 1024;
|
|
986
|
+
/**
|
|
987
|
+
* A child's stdout as an unlinked regular file the parent follows.
|
|
988
|
+
*
|
|
989
|
+
* Bun CLIs such as OpenCode end with `process.exit()`, which drops stdout
|
|
990
|
+
* still queued for a pipe: the last event arrives cut, or not at all (OpenCode
|
|
991
|
+
* 1.19.4 delivered 134 of 400 200 KB lines, exit 0). A write to a regular file
|
|
992
|
+
* has landed when it returns, so exit cannot lose it.
|
|
993
|
+
*/
|
|
994
|
+
var StdoutFile = class {
|
|
995
|
+
fd;
|
|
996
|
+
position = 0;
|
|
997
|
+
ended;
|
|
998
|
+
stream = new PassThrough();
|
|
999
|
+
constructor() {
|
|
1000
|
+
const path = join(tmpdir(), `cli-stdout-${randomUUID()}`);
|
|
1001
|
+
this.fd = openSync(path, "wx+", 384);
|
|
1002
|
+
unlinkSync(path);
|
|
1003
|
+
this.ended = once(this.stream, "end");
|
|
1004
|
+
}
|
|
1005
|
+
stdio(stdio = [
|
|
1006
|
+
"pipe",
|
|
1007
|
+
"pipe",
|
|
1008
|
+
"pipe"
|
|
1009
|
+
]) {
|
|
1010
|
+
const io = Array.isArray(stdio) ? [...stdio] : [
|
|
1011
|
+
stdio,
|
|
1012
|
+
stdio,
|
|
1013
|
+
stdio
|
|
1014
|
+
];
|
|
1015
|
+
io[1] = this.fd;
|
|
1016
|
+
return io;
|
|
1017
|
+
}
|
|
1018
|
+
/** The child never started: nothing will follow the file. */
|
|
1019
|
+
close() {
|
|
1020
|
+
closeSync(this.fd);
|
|
1021
|
+
}
|
|
1022
|
+
/** Stand in for `child.stdout`; the stream ends once the child is gone and every byte is read. */
|
|
1023
|
+
follow(child) {
|
|
1024
|
+
child.stdout = this.stream;
|
|
1025
|
+
const chunk = Buffer.allocUnsafe(CHUNK_BYTES);
|
|
1026
|
+
const drain = () => {
|
|
1027
|
+
for (let n = readSync(this.fd, chunk, 0, CHUNK_BYTES, this.position); n > 0; n = readSync(this.fd, chunk, 0, CHUNK_BYTES, this.position)) {
|
|
1028
|
+
this.position += n;
|
|
1029
|
+
this.stream.write(Buffer.from(chunk.subarray(0, n)));
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
let timer = setTimeout(function poll() {
|
|
1033
|
+
drain();
|
|
1034
|
+
timer = setTimeout(poll, POLL_MS);
|
|
1035
|
+
}, 0);
|
|
1036
|
+
let finished = false;
|
|
1037
|
+
const finish = () => {
|
|
1038
|
+
if (finished) return;
|
|
1039
|
+
finished = true;
|
|
1040
|
+
clearTimeout(timer);
|
|
1041
|
+
drain();
|
|
1042
|
+
this.close();
|
|
1043
|
+
this.stream.end();
|
|
1044
|
+
};
|
|
1045
|
+
child.once("exit", finish);
|
|
1046
|
+
child.once("error", finish);
|
|
1047
|
+
}
|
|
1048
|
+
};
|
|
1049
|
+
/** Parsed stdout goes through a file; raw or ignored stdout keeps the caller's stdio. */
|
|
1050
|
+
function stdoutFileFor(stdio, parsed) {
|
|
1051
|
+
const out = Array.isArray(stdio) ? stdio[1] : stdio ?? "pipe";
|
|
1052
|
+
return parsed && out === "pipe" ? new StdoutFile() : void 0;
|
|
1053
|
+
}
|
|
1054
|
+
//#endregion
|
|
979
1055
|
//#region src/stream-parser.ts
|
|
980
1056
|
/**
|
|
981
1057
|
* Creates a stdout stream parser. Three modes:
|
|
@@ -1051,10 +1127,12 @@ function spawnCliProcess(config, parserConfig) {
|
|
|
1051
1127
|
assertRawCaptureStdio(config);
|
|
1052
1128
|
let killer;
|
|
1053
1129
|
const capture = openProcessCapture(config, () => killer?.kill("SIGTERM"));
|
|
1130
|
+
const stdout = stdoutFileFor(config.spawnOptions?.stdio, !!parserConfig);
|
|
1054
1131
|
let child;
|
|
1055
1132
|
try {
|
|
1056
|
-
child = spawnChild(config, processEnv, detached);
|
|
1133
|
+
child = spawnChild(config, processEnv, detached, stdout);
|
|
1057
1134
|
} catch (error) {
|
|
1135
|
+
stdout?.close();
|
|
1058
1136
|
capture.closeAfterSpawnFailure(error);
|
|
1059
1137
|
throw error;
|
|
1060
1138
|
}
|
|
@@ -1063,7 +1141,8 @@ function spawnCliProcess(config, parserConfig) {
|
|
|
1063
1141
|
launch: { child },
|
|
1064
1142
|
killer,
|
|
1065
1143
|
capture,
|
|
1066
|
-
detached
|
|
1144
|
+
detached,
|
|
1145
|
+
stdout
|
|
1067
1146
|
});
|
|
1068
1147
|
}
|
|
1069
1148
|
/**
|
|
@@ -1080,6 +1159,7 @@ async function launchCliProcess(config, parserConfig) {
|
|
|
1080
1159
|
assertRawCaptureStdio(config);
|
|
1081
1160
|
let killer;
|
|
1082
1161
|
const capture = openProcessCapture(config, () => killer?.kill("SIGTERM"));
|
|
1162
|
+
const stdout = stdoutFileFor(config.spawnOptions?.stdio, !!parserConfig);
|
|
1083
1163
|
let launch;
|
|
1084
1164
|
try {
|
|
1085
1165
|
const launched = launcher({
|
|
@@ -1087,7 +1167,7 @@ async function launchCliProcess(config, parserConfig) {
|
|
|
1087
1167
|
args: config.args,
|
|
1088
1168
|
cwd: config.cwd,
|
|
1089
1169
|
env: buildChildEnv(config),
|
|
1090
|
-
stdio: config.spawnOptions?.stdio ?? [
|
|
1170
|
+
stdio: stdout?.stdio(config.spawnOptions?.stdio) ?? config.spawnOptions?.stdio ?? [
|
|
1091
1171
|
"pipe",
|
|
1092
1172
|
"pipe",
|
|
1093
1173
|
"pipe"
|
|
@@ -1097,6 +1177,7 @@ async function launchCliProcess(config, parserConfig) {
|
|
|
1097
1177
|
});
|
|
1098
1178
|
launch = launched instanceof Promise ? await launched : launched;
|
|
1099
1179
|
} catch (error) {
|
|
1180
|
+
stdout?.close();
|
|
1100
1181
|
capture.closeAfterSpawnFailure(error);
|
|
1101
1182
|
throw error;
|
|
1102
1183
|
}
|
|
@@ -1105,7 +1186,8 @@ async function launchCliProcess(config, parserConfig) {
|
|
|
1105
1186
|
launch,
|
|
1106
1187
|
killer,
|
|
1107
1188
|
capture,
|
|
1108
|
-
detached
|
|
1189
|
+
detached,
|
|
1190
|
+
stdout
|
|
1109
1191
|
});
|
|
1110
1192
|
}
|
|
1111
1193
|
function isDetached(config) {
|
|
@@ -1117,6 +1199,7 @@ function openProcessCapture(config, onFailure) {
|
|
|
1117
1199
|
function attachCliProcess(config, parserConfig, run) {
|
|
1118
1200
|
const { launch, killer, capture, detached } = run;
|
|
1119
1201
|
const child = launch.child;
|
|
1202
|
+
run.stdout?.follow(child);
|
|
1120
1203
|
const spawnedAt = Date.now();
|
|
1121
1204
|
const stdin = watchStdinFailure(child, killer, config.stdinFailure);
|
|
1122
1205
|
const group = config.ownProcessGroup && detached && process.platform !== "win32" && child.pid ? new CliProcessGroup(child.pid) : void 0;
|
|
@@ -1133,7 +1216,8 @@ function attachCliProcess(config, parserConfig, run) {
|
|
|
1133
1216
|
abort,
|
|
1134
1217
|
group,
|
|
1135
1218
|
killer,
|
|
1136
|
-
release: launch.release?.bind(launch)
|
|
1219
|
+
release: launch.release?.bind(launch),
|
|
1220
|
+
stdoutEnded: run.stdout?.ended
|
|
1137
1221
|
});
|
|
1138
1222
|
return {
|
|
1139
1223
|
pid: child.pid,
|
|
@@ -1174,7 +1258,7 @@ function assertRawCaptureStdio(config) {
|
|
|
1174
1258
|
const io = config.spawnOptions.stdio;
|
|
1175
1259
|
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
1260
|
}
|
|
1177
|
-
function spawnChild(config, processEnv, detached) {
|
|
1261
|
+
function spawnChild(config, processEnv, detached, stdout) {
|
|
1178
1262
|
const { command, args, cwd, spawnOptions, isolatedIdentity } = config;
|
|
1179
1263
|
const isolated = isolatedIdentity !== void 0;
|
|
1180
1264
|
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 +1282,8 @@ function spawnChild(config, processEnv, detached) {
|
|
|
1198
1282
|
"pipe"
|
|
1199
1283
|
],
|
|
1200
1284
|
detached,
|
|
1201
|
-
...isolated ? safeSpawnOptions : spawnOptions
|
|
1285
|
+
...isolated ? safeSpawnOptions : spawnOptions,
|
|
1286
|
+
...stdout ? { stdio: stdout.stdio(spawnOptions?.stdio) } : {}
|
|
1202
1287
|
});
|
|
1203
1288
|
}
|
|
1204
1289
|
function watchStdinFailure(child, killer, mode = "stop") {
|
|
@@ -1266,6 +1351,7 @@ function createExitPromise(child, run) {
|
|
|
1266
1351
|
return new Promise((resolve) => {
|
|
1267
1352
|
child.once("close", async (code, exitSignal) => {
|
|
1268
1353
|
deadlines.clearAll();
|
|
1354
|
+
await run.stdoutEnded;
|
|
1269
1355
|
endParser?.();
|
|
1270
1356
|
const processGroupError = await group?.stop().then(() => void 0, (error) => error instanceof Error ? error.message : String(error));
|
|
1271
1357
|
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.
|
|
3
|
+
"version": "0.3.8",
|
|
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": "
|
|
30
|
-
"mergedAt": "2026-11-
|
|
29
|
+
"sourceSha": "c9c94777cfcfd997fa2b5913e42c8c622fcd0850",
|
|
30
|
+
"mergedAt": "2026-11-05T14:40:47-08:00"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"build": "tsdown",
|