diff --git a/dist/main.d.ts b/dist/main.d.ts
index 4d51085..ba780fe 100644
--- a/dist/main.d.ts
+++ b/dist/main.d.ts
@@ -1,9 +1,11 @@
///
///
import { Duplex } from "node:stream";
+type WriteCallback = (error: Error | null | undefined) => void;
export declare class Port extends Duplex {
constructor();
- _read(): void;
- _write(obj: Buffer, _encoding: any, callback: Function): void;
+ read(): any;
+ write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
_final(): void;
}
+export {};
diff --git a/dist/main.js b/dist/main.js
index b554e87..1bce387 100644
--- a/dist/main.js
+++ b/dist/main.js
@@ -1,42 +1,41 @@
import { Bert } from "./bert.js";
import { Duplex } from "node:stream";
const bert = new Bert();
-let termLen;
-let termBin;
const fakeWrite = () => undefined;
process.stdout.write = fakeWrite;
export class Port extends Duplex {
constructor() {
super({ objectMode: true });
}
- _read() {
- {
- let term;
- if (termLen === undefined && null !== (termBin = process.stdin.read(4))) {
- termLen = bert.bytesToInt(termBin, 4, true);
+ read() {
+ const lenBytes = process.stdin.read(4);
+ if (lenBytes) {
+ const termLen = bert.bytesToInt(lenBytes, 4, true);
+ const termBytes = process.stdin.read(termLen);
+ if (termBytes) {
+ return bert.decode(termBytes);
}
- if (termLen !== undefined &&
- null !== (term = process.stdin.read(termLen))) {
- termLen = undefined;
- this.push(bert.decode(term));
+ else {
+ process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
+ return null;
}
}
+ else
+ return null;
}
- _write(obj, _encoding, callback) {
- let term;
+ write(obj, encodingOrCallback, callback) {
+ const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
try {
- term = bert.encode(obj, true);
+ const term = bert.encode(obj, true);
+ const len = Buffer.alloc(4);
+ len.writeUInt32BE(term.length, 0);
+ process.stdout.write(len);
+ return process.stdout.write(term, actualCallback);
}
catch (error) {
- console.error(error);
- process.exit(1);
+ process.stderr.write(`Error writing: ${error}\n`);
+ return false;
}
- const len = Buffer.alloc(4);
- len.writeUInt32BE(term.length, 0);
- process.stdout.write = this._write;
- process.stdout.write(len);
- process.stdout.write(term, callback);
- process.stdout.write = fakeWrite;
}
// When all the data is done passing, it stops.
_final() {
diff --git a/src/main.ts b/src/main.ts
index 5ff86f2..e5efda4 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -3,52 +3,53 @@ import { Duplex } from "node:stream";
const bert = new Bert();
-let termLen: number | undefined;
-let termBin: Buffer;
-
const fakeWrite = () => undefined;
process.stdout.write = fakeWrite as any;
+type WriteCallback = (error: Error | null | undefined) => void;
+
export class Port extends Duplex {
- constructor() {
- super({ objectMode: true });
- }
-
- _read() {
- {
- let term;
- if (termLen === undefined && null !== (termBin = process.stdin.read(4))) {
- termLen = bert.bytesToInt(termBin, 4, true);
- }
- if (
- termLen !== undefined &&
- null !== (term = process.stdin.read(termLen))
- ) {
- termLen = undefined;
- this.push(bert.decode(term));
- }
+ constructor() {
+ super({ objectMode: true });
}
- }
- _write(obj: Buffer, _encoding: any, callback: Function) {
- let term: Buffer;
- try {
- term = bert.encode(obj, true);
- } catch (error) {
- console.error(error);
- process.exit(1);
+ read() {
+ const lenBytes = process.stdin.read(4);
+ if (lenBytes) {
+ const termLen = bert.bytesToInt(lenBytes, 4, true);
+ const termBytes = process.stdin.read(termLen);
+
+ if (termBytes) {
+ return bert.decode(termBytes);
+ }
+ else {
+ process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
+ return null
+ }
+ }
+ else return null;
}
- const len = Buffer.alloc(4);
- len.writeUInt32BE(term.length, 0);
- process.stdout.write = this._write as any;
- process.stdout.write(len);
- process.stdout.write(term, callback as any);
- process.stdout.write = fakeWrite as any;
- }
- // When all the data is done passing, it stops.
- _final() {
- this.push(null);
- }
+ 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 len = Buffer.alloc(4);
+ len.writeUInt32BE(term.length, 0);
+
+ process.stdout.write(len);
+ return process.stdout.write(term, actualCallback);
+ }
+ catch (error) {
+ process.stderr.write(`Error writing: ${error}\n`);
+ return false;
+ }
+ }
+
+ // When all the data is done passing, it stops.
+ _final() {
+ this.push(null);
+ }
}