maybe
This commit is contained in:
parent
22ed1edbcc
commit
5c4f45a0dc
|
@ -5,8 +5,10 @@ import { Duplex } from "node:stream";
|
||||||
type WriteCallback = (error: Error | null | undefined) => void;
|
type WriteCallback = (error: Error | null | undefined) => void;
|
||||||
export declare class Port extends Duplex {
|
export declare class Port extends Duplex {
|
||||||
readonly bert: Bert;
|
readonly bert: Bert;
|
||||||
|
private originalStdout;
|
||||||
|
private readonly fakeStdout;
|
||||||
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean);
|
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean);
|
||||||
_read(): void;
|
_read(): any;
|
||||||
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
|
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
|
|
@ -2,9 +2,16 @@ import { Bert } from "./bert.js";
|
||||||
import { Duplex } from "node:stream";
|
import { Duplex } from "node:stream";
|
||||||
export class Port extends Duplex {
|
export class Port extends Duplex {
|
||||||
bert;
|
bert;
|
||||||
|
originalStdout;
|
||||||
|
fakeStdout = () => true;
|
||||||
constructor(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues) {
|
constructor(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues) {
|
||||||
super({ objectMode: true });
|
super({ objectMode: true });
|
||||||
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
|
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
|
||||||
|
this.originalStdout = process.stdout;
|
||||||
|
process.stdout.write = this.fakeStdout;
|
||||||
|
process.stdin.on("readable", () => {
|
||||||
|
while (this._read()) { }
|
||||||
|
});
|
||||||
}
|
}
|
||||||
_read() {
|
_read() {
|
||||||
const lenBytes = process.stdin.read(4);
|
const lenBytes = process.stdin.read(4);
|
||||||
|
@ -13,12 +20,17 @@ export class Port extends Duplex {
|
||||||
process.stderr.write(`Got term length: ${termLen}\n`);
|
process.stderr.write(`Got term length: ${termLen}\n`);
|
||||||
const termBytes = process.stdin.read(termLen);
|
const termBytes = process.stdin.read(termLen);
|
||||||
if (termBytes) {
|
if (termBytes) {
|
||||||
this.push(this.bert.decode(termBytes));
|
const decoded = this.bert.decode(termBytes);
|
||||||
|
this.push(decoded);
|
||||||
|
return decoded;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
_write(obj, encodingOrCallback, callback) {
|
_write(obj, encodingOrCallback, callback) {
|
||||||
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||||
|
@ -26,8 +38,11 @@ export class Port extends Duplex {
|
||||||
const term = this.bert.encode(obj, true);
|
const term = this.bert.encode(obj, true);
|
||||||
const len = Buffer.alloc(4);
|
const len = Buffer.alloc(4);
|
||||||
len.writeUInt32BE(term.length, 0);
|
len.writeUInt32BE(term.length, 0);
|
||||||
|
process.stdout.write = this.originalStdout;
|
||||||
process.stdout.write(len);
|
process.stdout.write(len);
|
||||||
return process.stdout.write(term, actualCallback);
|
process.stdout.write(term, actualCallback);
|
||||||
|
process.stdout.write = this.fakeStdout;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
process.stderr.write(`Error writing: ${error}\n`);
|
process.stderr.write(`Error writing: ${error}\n`);
|
||||||
|
|
18
src/main.ts
18
src/main.ts
|
@ -5,10 +5,17 @@ type WriteCallback = (error: Error | null | undefined) => void;
|
||||||
|
|
||||||
export class Port extends Duplex {
|
export class Port extends Duplex {
|
||||||
public readonly bert: Bert;
|
public readonly bert: Bert;
|
||||||
|
private originalStdout;
|
||||||
|
private readonly fakeStdout = () => true;
|
||||||
|
|
||||||
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean) {
|
constructor(allBinariesAsString?: boolean, mapKeyAsAtom?: boolean, decodeUndefinedValues?: boolean) {
|
||||||
super({ objectMode: true });
|
super({ objectMode: true });
|
||||||
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
|
this.bert = new Bert(allBinariesAsString, mapKeyAsAtom, decodeUndefinedValues);
|
||||||
|
this.originalStdout = process.stdout;
|
||||||
|
process.stdout.write = this.fakeStdout;
|
||||||
|
process.stdin.on("readable", () => {
|
||||||
|
while(this._read()) {}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_read() {
|
_read() {
|
||||||
|
@ -19,12 +26,16 @@ export class Port extends Duplex {
|
||||||
const termBytes = process.stdin.read(termLen);
|
const termBytes = process.stdin.read(termLen);
|
||||||
|
|
||||||
if (termBytes) {
|
if (termBytes) {
|
||||||
this.push(this.bert.decode(termBytes));
|
const decoded = this.bert.decode(termBytes);
|
||||||
|
this.push(decoded);
|
||||||
|
return decoded;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
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) {
|
||||||
|
@ -35,8 +46,11 @@ export class Port extends Duplex {
|
||||||
const len = Buffer.alloc(4);
|
const len = Buffer.alloc(4);
|
||||||
len.writeUInt32BE(term.length, 0);
|
len.writeUInt32BE(term.length, 0);
|
||||||
|
|
||||||
|
process.stdout.write = this.originalStdout as any;
|
||||||
process.stdout.write(len);
|
process.stdout.write(len);
|
||||||
return process.stdout.write(term, actualCallback);
|
process.stdout.write(term, actualCallback);
|
||||||
|
process.stdout.write = this.fakeStdout;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
process.stderr.write(`Error writing: ${error}\n`);
|
process.stderr.write(`Error writing: ${error}\n`);
|
||||||
|
|
Loading…
Reference in New Issue