From 6edec2094f533eb7d8d7c887a98fb31706fc372f Mon Sep 17 00:00:00 2001 From: Moon Man Date: Tue, 30 Jan 2024 02:17:34 -0500 Subject: [PATCH] better logging --- dist/main.d.ts | 9 +++++++++ dist/main.js | 32 ++++++++++++++++++++++++++++++++ src/main.ts | 44 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/dist/main.d.ts b/dist/main.d.ts index 88167bf..81dd16a 100644 --- a/dist/main.d.ts +++ b/dist/main.d.ts @@ -3,6 +3,8 @@ import { Bert } from "./bert.js"; import { Duplex } from "node:stream"; type WriteCallback = (error: Error | null | undefined) => void; +type ServerHandlerCallback = (reply: "reply" | "noreply", ...extraArgs: any[]) => any; +type ServerHandler = (term: any, from: any, state: any, callback: ServerHandlerCallback) => void; export declare class Port extends Duplex { readonly bert: Bert; private originalStdout; @@ -11,4 +13,11 @@ export declare class Port extends Duplex { _read(): any; _write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean; } +export declare class Server { + private port; + private handler; + private state; + private handleTerm; + constructor(port: Port, handler: ServerHandler); +} export {}; diff --git a/dist/main.js b/dist/main.js index d55002a..cd18343 100644 --- a/dist/main.js +++ b/dist/main.js @@ -51,3 +51,35 @@ export class Port extends Duplex { } } } +export class Server { + port; + handler; + state = undefined; + handleTerm = (term) => { + if (this.state === undefined) { + this.state = term; + } + else { + this.handler(term, (t) => this.port.write(t), this.state, (reply, ...extraArgs) => { + if (reply === "reply") + this.port.write(term); + if (reply === "reply" && extraArgs.length === 2) { + this.state = extraArgs[1]; + } + else if (reply === "noreply" && extraArgs.length === 1) { + this.state = extraArgs[0]; + } + }); + } + }; + constructor(port, handler) { + this.port = port; + this.handler = handler; + port.on("readable", () => { + let term; + while (term = port.read()) { + this.handleTerm(term); + } + }); + } +} diff --git a/src/main.ts b/src/main.ts index ff685c2..5c1e185 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,9 @@ import { Duplex } from "node:stream"; type WriteCallback = (error: Error | null | undefined) => void; +type ServerHandlerCallback = (reply: "reply" | "noreply", ...extraArgs: any[]) => any; +type ServerHandler = (term: any, from: any, state: any, callback: ServerHandlerCallback) => void; + const log = (msg: string) => process.stderr.write(`${msg}\r\n`); export class Port extends Duplex { @@ -16,7 +19,7 @@ export class Port extends Duplex { this.originalStdout = process.stdout; process.stdout.write = this.fakeStdout; process.stdin.on("readable", () => { - while(this._read()) {} + while (this._read()) { } }); } @@ -42,7 +45,7 @@ export class Port extends Duplex { _write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) { const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined; - + try { const term = this.bert.encode(obj, true); const len = Buffer.alloc(4); @@ -60,3 +63,40 @@ export class Port extends Duplex { } } } + +export class Server { + private port; + private handler; + + private state = undefined; + + private handleTerm = (term: any): any => { + if (this.state === undefined) { + this.state = term; + } + else { + this.handler(term, (t: any) => this.port.write(t), this.state, (reply, ...extraArgs) => { + if (reply === "reply") this.port.write(term); + + if (reply === "reply" && extraArgs.length === 2) { + this.state = extraArgs[1]; + } + else if (reply === "noreply" && extraArgs.length === 1) { + this.state = extraArgs[0]; + } + }); + } + }; + + constructor(port: Port, handler: ServerHandler) { + this.port = port; + this.handler = handler; + + port.on("readable", () => { + let term; + while (term = port.read()) { + this.handleTerm(term); + } + }); + } +}