noderl/dist/main.js

54 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";
2024-01-29 13:59:30 +00:00
const log = (msg) => process.stderr.write(`${msg}\r\n`);
2024-01-28 11:32:24 +00:00
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-29 13:59:30 +00:00
log(`Got incoming term length: ${termLen} (bytes: <<${lenBytes.toString('hex').match(/../g).map((hex) => parseInt(`0x${hex}`).toString()).join(', ')}>>).`);
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-29 13:59:30 +00:00
log("Term read got erroneous null.");
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-29 13:59:30 +00:00
log(`Error writing: ${error}`);
2024-01-28 15:01:33 +00:00
return false;
2024-01-28 11:32:24 +00:00
}
}
}