@span-io/agent-link 0.1.3 → 0.1.4
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.js +18 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -161,35 +161,47 @@ function handleControl(message) {
|
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
function setupAgentPiping(agentId, proc) {
|
|
164
|
+
const rawFlushSize = Number.parseInt(process.env.AGENT_LINK_STREAM_FLUSH_CHARS ?? "16384", 10);
|
|
165
|
+
const flushSize = Number.isFinite(rawFlushSize) && rawFlushSize > 0 ? rawFlushSize : 16384;
|
|
164
166
|
const setupStream = (stream, name) => {
|
|
165
167
|
if (!stream)
|
|
166
168
|
return;
|
|
167
169
|
let buffer = "";
|
|
168
170
|
stream.setEncoding("utf8");
|
|
171
|
+
const flushChunk = (chunk) => {
|
|
172
|
+
if (chunk.length > 0) {
|
|
173
|
+
transport.sendLog(agentId, name, chunk);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
169
176
|
stream.on("data", (chunk) => {
|
|
170
177
|
buffer += chunk;
|
|
171
178
|
let index = buffer.indexOf("\n");
|
|
172
179
|
while (index >= 0) {
|
|
173
180
|
const line = buffer.slice(0, index + 1);
|
|
174
181
|
buffer = buffer.slice(index + 1);
|
|
175
|
-
|
|
182
|
+
flushChunk(line);
|
|
176
183
|
index = buffer.indexOf("\n");
|
|
177
184
|
}
|
|
185
|
+
// Prevent unbounded memory growth when tools stream without newlines.
|
|
186
|
+
while (buffer.length >= flushSize) {
|
|
187
|
+
const part = buffer.slice(0, flushSize);
|
|
188
|
+
buffer = buffer.slice(flushSize);
|
|
189
|
+
flushChunk(part);
|
|
190
|
+
}
|
|
178
191
|
});
|
|
179
192
|
stream.on("end", () => {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
193
|
+
flushChunk(buffer);
|
|
194
|
+
buffer = "";
|
|
183
195
|
});
|
|
184
196
|
};
|
|
185
197
|
setupStream(proc.child.stdout, "stdout");
|
|
186
198
|
setupStream(proc.child.stderr, "stderr");
|
|
187
|
-
proc.child.on("exit", (
|
|
199
|
+
proc.child.on("exit", (_code, _signal) => {
|
|
188
200
|
transport.sendStatus(agentId, "exited");
|
|
189
201
|
activeAgents.delete(agentId);
|
|
190
202
|
});
|
|
191
203
|
proc.child.on("error", (error) => {
|
|
192
|
-
transport.sendLog(agentId, "stderr",
|
|
204
|
+
transport.sendLog(agentId, "stderr", "Process error: " + error.message + "\n");
|
|
193
205
|
transport.sendStatus(agentId, "error");
|
|
194
206
|
activeAgents.delete(agentId);
|
|
195
207
|
});
|