more logging

This commit is contained in:
Moon Man 2024-02-02 14:34:02 -05:00
parent 6fb0cfe889
commit db737af36f
3 changed files with 18 additions and 11 deletions

1
dist/main.d.ts vendored
View File

@ -7,6 +7,7 @@ type ServerHandlerCallback = (reply: "reply" | "noreply", ...extraArgs: any[]) =
type ServerHandler = (term: any, from: any, state: any, callback: ServerHandlerCallback) => void;
export declare class Port extends Duplex {
readonly bert: Bert;
private lenBuffer;
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean);
_read(): any;
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;

13
dist/main.js vendored
View File

@ -3,6 +3,7 @@ import { Duplex } from "node:stream";
const log = (msg) => process.stderr.write(`${msg}\r\n`);
export class Port extends Duplex {
bert;
lenBuffer = Buffer.alloc(4);
constructor(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues) {
super({ objectMode: true });
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
@ -26,16 +27,18 @@ export class Port extends Duplex {
return null;
}
}
else
else {
this.push(null);
return null;
}
}
_write(obj, encodingOrCallback, callback) {
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
log(`Port was written to.`);
try {
const term = this.bert.encode(obj, true);
const len = Buffer.alloc(4);
len.writeUInt32BE(term.length, 0);
process.stdout.write(len);
const term = this.bert.encode(obj, false);
this.lenBuffer.writeUInt32BE(term.length);
process.stdout.write(this.lenBuffer);
process.stdout.write(term, actualCallback);
return true;
}

View File

@ -10,6 +10,7 @@ const log = (msg: string) => process.stderr.write(`${msg}\r\n`);
export class Port extends Duplex {
public readonly bert: Bert;
private lenBuffer = Buffer.alloc(4);
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean) {
super({ objectMode: true });
@ -36,18 +37,20 @@ export class Port extends Duplex {
return null;
}
}
else return null;
else {
this.push(null);
return null;
}
}
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) {
const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
log(`Port was written to.`);
try {
const term = this.bert.encode(obj, true);
const len = Buffer.alloc(4);
len.writeUInt32BE(term.length, 0);
process.stdout.write(len);
const term = this.bert.encode(obj, false);
this.lenBuffer.writeUInt32BE(term.length);
process.stdout.write(this.lenBuffer);
process.stdout.write(term, actualCallback);
return true;
}