This commit is contained in:
Moon Man 2024-01-28 11:31:37 -05:00
parent 22ed1edbcc
commit 5c4f45a0dc
3 changed files with 36 additions and 5 deletions

4
dist/main.d.ts vendored
View File

@ -5,8 +5,10 @@ import { Duplex } from "node:stream";
type WriteCallback = (error: Error | null | undefined) => void;
export declare class Port extends Duplex {
readonly bert: Bert;
private originalStdout;
private readonly fakeStdout;
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean);
_read(): void;
_read(): any;
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
}
export {};

19
dist/main.js vendored
View File

@ -2,9 +2,16 @@ import { Bert } from "./bert.js";
import { Duplex } from "node:stream";
export class Port extends Duplex {
bert;
originalStdout;
fakeStdout = () => true;
constructor(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues) {
super({ objectMode: true });
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
this.originalStdout = process.stdout;
process.stdout.write = this.fakeStdout;
process.stdin.on("readable", () => {
while (this._read()) { }
});
}
_read() {
const lenBytes = process.stdin.read(4);
@ -13,12 +20,17 @@ export class Port extends Duplex {
process.stderr.write(`Got term length: ${termLen}\n`);
const termBytes = process.stdin.read(termLen);
if (termBytes) {
this.push(this.bert.decode(termBytes));
const decoded = this.bert.decode(termBytes);
this.push(decoded);
return decoded;
}
else {
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
return null;
}
}
else
return null;
}
_write(obj, encodingOrCallback, callback) {
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
@ -26,8 +38,11 @@ export class Port extends Duplex {
const term = this.bert.encode(obj, true);
const len = Buffer.alloc(4);
len.writeUInt32BE(term.length, 0);
process.stdout.write = this.originalStdout;
process.stdout.write(len);
return process.stdout.write(term, actualCallback);
process.stdout.write(term, actualCallback);
process.stdout.write = this.fakeStdout;
return true;
}
catch (error) {
process.stderr.write(`Error writing: ${error}\n`);

View File

@ -5,10 +5,17 @@ type WriteCallback = (error: Error | null | undefined) => void;
export class Port extends Duplex {
public readonly bert: Bert;
private originalStdout;
private readonly fakeStdout = () => true;
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean) {
super({ objectMode: true });
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
this.originalStdout = process.stdout;
process.stdout.write = this.fakeStdout;
process.stdin.on("readable", () => {
while(this._read()) {}
});
}
_read() {
@ -19,12 +26,16 @@ export class Port extends Duplex {
const termBytes = process.stdin.read(termLen);
if (termBytes) {
this.push(this.bert.decode(termBytes));
const decoded = this.bert.decode(termBytes);
this.push(decoded);
return decoded;
}
else {
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
return null;
}
}
else return null;
}
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) {
@ -35,8 +46,11 @@ export class Port extends Duplex {
const len = Buffer.alloc(4);
len.writeUInt32BE(term.length, 0);
process.stdout.write = this.originalStdout as any;
process.stdout.write(len);
return process.stdout.write(term, actualCallback);
process.stdout.write(term, actualCallback);
process.stdout.write = this.fakeStdout;
return true;
}
catch (error) {
process.stderr.write(`Error writing: ${error}\n`);