noderl/dist/main.js

53 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-01-28 15:45:09 +00:00
import { Bert } from "./bert.js";
2024-01-28 11:32:24 +00:00
import { Duplex } from "node:stream";
export class Port extends Duplex {
2024-01-28 15:39:03 +00:00
bert;
2024-01-28 16:31:37 +00:00
originalStdout;
fakeStdout = () => true;
2024-01-28 15:45:09 +00:00
constructor(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues) {
2024-01-28 11:32:24 +00:00
super({ objectMode: true });
2024-01-28 15:45:09 +00:00
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
2024-01-28 16:31:37 +00:00
this.originalStdout = process.stdout;
process.stdout.write = this.fakeStdout;
process.stdin.on("readable", () => {
while (this._read()) { }
});
2024-01-28 11:32:24 +00:00
}
2024-01-28 15:12:30 +00:00
_read() {
2024-01-28 15:01:33 +00:00
const lenBytes = process.stdin.read(4);
if (lenBytes) {
2024-01-28 15:39:03 +00:00
const termLen = this.bert.bytesToInt(lenBytes, 4, true);
2024-01-28 17:35:02 +00:00
process.stderr.write(`Got incoming term length: ${termLen} (bytes: <<${lenBytes.toString('hex').match(/../g).map((hex) => parseInt(`0x${hex}`).toString()).join(', ')}>>)\n`);
2024-01-28 15:01:33 +00:00
const termBytes = process.stdin.read(termLen);
if (termBytes) {
2024-01-28 16:31:37 +00:00
const decoded = this.bert.decode(termBytes);
this.push(decoded);
return decoded;
2024-01-28 11:32:24 +00:00
}
2024-01-28 15:01:33 +00:00
else {
2024-01-28 16:57:08 +00:00
process.stderr.write(`Term read got erroneous null.\n`);
2024-01-28 16:31:37 +00:00
return null;
2024-01-28 11:32:24 +00:00
}
}
2024-01-28 16:31:37 +00:00
else
return null;
2024-01-28 11:32:24 +00:00
}
2024-01-28 15:12:30 +00:00
_write(obj, encodingOrCallback, callback) {
2024-01-28 15:01:33 +00:00
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
2024-01-28 11:32:24 +00:00
try {
2024-01-28 15:39:03 +00:00
const term = this.bert.encode(obj, true);
2024-01-28 15:01:33 +00:00
const len = Buffer.alloc(4);
len.writeUInt32BE(term.length, 0);
2024-01-28 16:31:37 +00:00
process.stdout.write = this.originalStdout;
2024-01-28 15:01:33 +00:00
process.stdout.write(len);
2024-01-28 16:31:37 +00:00
process.stdout.write(term, actualCallback);
process.stdout.write = this.fakeStdout;
return true;
2024-01-28 11:32:24 +00:00
}
catch (error) {
2024-01-28 15:01:33 +00:00
process.stderr.write(`Error writing: ${error}\n`);
return false;
2024-01-28 11:32:24 +00:00
}
}
}