diff --git a/dist/main.d.ts b/dist/main.d.ts index dedf6f0..e272e88 100644 --- a/dist/main.d.ts +++ b/dist/main.d.ts @@ -1,10 +1,12 @@ /// /// +import { Bert } from "./bert.js"; import { Duplex } from "node:stream"; type WriteCallback = (error: Error | null | undefined) => void; export declare class Port extends Duplex { - constructor(); - _read(): any; + readonly bert: Bert; + constructor(bert: Bert); + _read(): void; _write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean; } export {}; diff --git a/dist/main.js b/dist/main.js index 99bf54b..8f30a58 100644 --- a/dist/main.js +++ b/dist/main.js @@ -1,32 +1,28 @@ -import { Bert } from "./bert.js"; import { Duplex } from "node:stream"; -const bert = new Bert(); -const fakeWrite = () => undefined; -process.stdout.write = fakeWrite; export class Port extends Duplex { - constructor() { + bert; + constructor(bert) { super({ objectMode: true }); + this.bert = bert; } _read() { const lenBytes = process.stdin.read(4); if (lenBytes) { - const termLen = bert.bytesToInt(lenBytes, 4, true); + const termLen = this.bert.bytesToInt(lenBytes, 4, true); + process.stderr.write(`Got term length: ${termLen}\n`); const termBytes = process.stdin.read(termLen); if (termBytes) { - return bert.decode(termBytes); + this.push(this.bert.decode(termBytes)); } 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; try { - const term = bert.encode(obj, true); + const term = this.bert.encode(obj, true); const len = Buffer.alloc(4); len.writeUInt32BE(term.length, 0); process.stdout.write(len); diff --git a/src/main.ts b/src/main.ts index 1d3d49e..e60e1b4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,40 +1,37 @@ import { Bert } from "./bert.js"; import { Duplex } from "node:stream"; -const bert = new Bert(); - -const fakeWrite = () => undefined; -process.stdout.write = fakeWrite as any; - type WriteCallback = (error: Error | null | undefined) => void; export class Port extends Duplex { - constructor() { + public readonly bert: Bert; + + constructor(bert: Bert) { super({ objectMode: true }); + this.bert = bert; } - read() { + _read() { const lenBytes = process.stdin.read(4); if (lenBytes) { - const termLen = bert.bytesToInt(lenBytes, 4, true); + const termLen = this.bert.bytesToInt(lenBytes, 4, true); + process.stderr.write(`Got term length: ${termLen}\n`); const termBytes = process.stdin.read(termLen); if (termBytes) { - return bert.decode(termBytes); + this.push(this.bert.decode(termBytes)); } 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) { + _write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) { const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined; try { - const term = bert.encode(obj, true); + const term = this.bert.encode(obj, true); const len = Buffer.alloc(4); len.writeUInt32BE(term.length, 0);