better logging
This commit is contained in:
parent
362fda5b4b
commit
6edec2094f
|
@ -3,6 +3,8 @@
|
||||||
import { Bert } from "./bert.js";
|
import { Bert } from "./bert.js";
|
||||||
import { Duplex } from "node:stream";
|
import { Duplex } from "node:stream";
|
||||||
type WriteCallback = (error: Error | null | undefined) => void;
|
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 {
|
export declare class Port extends Duplex {
|
||||||
readonly bert: Bert;
|
readonly bert: Bert;
|
||||||
private originalStdout;
|
private originalStdout;
|
||||||
|
@ -11,4 +13,11 @@ export declare class Port extends Duplex {
|
||||||
_read(): any;
|
_read(): any;
|
||||||
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
|
_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 {};
|
export {};
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
42
src/main.ts
42
src/main.ts
|
@ -3,6 +3,9 @@ import { Duplex } from "node:stream";
|
||||||
|
|
||||||
type WriteCallback = (error: Error | null | undefined) => void;
|
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`);
|
const log = (msg: string) => process.stderr.write(`${msg}\r\n`);
|
||||||
|
|
||||||
export class Port extends Duplex {
|
export class Port extends Duplex {
|
||||||
|
@ -16,7 +19,7 @@ export class Port extends Duplex {
|
||||||
this.originalStdout = process.stdout;
|
this.originalStdout = process.stdout;
|
||||||
process.stdout.write = this.fakeStdout;
|
process.stdout.write = this.fakeStdout;
|
||||||
process.stdin.on("readable", () => {
|
process.stdin.on("readable", () => {
|
||||||
while(this._read()) {}
|
while (this._read()) { }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue