@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
63 lines • 2.31 kB
JavaScript
import { closeSync, createReadStream, fstatSync, write } from "node:fs";
import { Socket } from "node:net";
import { Duplex } from "node:stream";
/** Windows can report FIFO mode bits while Stats.isFIFO() returns false, so pipes are recognised by mode. */
export function isFifoMode(mode) {
return (mode & 0o170000) === 0o010000;
}
/** One stream owns a verified inherited pipe/socket, including Bun's file-stream adoption path. */
export function inheritedDuplex(fd) {
const readFd = typeof fd === "number" ? fd : fd[0];
const writeFd = typeof fd === "number" ? fd : fd[1];
try {
const stat = fstatSync(readFd);
if (!stat.isSocket() && !isFifoMode(stat.mode))
return undefined;
if (typeof fd === "number" && stat.isSocket() && !process.versions.bun)
return new Socket({ fd, readable: true, writable: true });
}
catch {
return undefined;
}
const input = createReadStream("", { fd: readFd, autoClose: true });
const stream = new Duplex({
read() {
input.resume();
},
write(chunk, _encoding, done) {
if (chunk.length === 0)
return done();
const send = (offset) => {
write(writeFd, chunk, offset, chunk.length - offset, null, (error, count) => {
if (error)
done(error);
else if (count === 0)
done(new Error("private_channel_write_failed"));
else if (offset + count === chunk.length)
done();
else
send(offset + count);
});
};
send(0);
},
destroy(error, done) {
if (writeFd !== readFd)
closeSync(writeFd);
if (input.closed)
done(error);
else {
input.once("close", () => done(error));
input.destroy();
}
},
});
input.on("data", (chunk) => {
if (!stream.push(chunk))
input.pause();
});
input.on("end", () => stream.push(null));
input.on("error", (error) => stream.destroy(error));
return stream;
}
//# sourceMappingURL=inherited-duplex.js.map