maybe works now?
This commit is contained in:
parent
05c7599696
commit
f5f2341218
|
@ -1,9 +1,11 @@
|
|||
/// <reference types="node" />
|
||||
/// <reference types="node" />
|
||||
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 {};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
const term = bert.encode(obj, true);
|
||||
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;
|
||||
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() {
|
||||
|
|
49
src/main.ts
49
src/main.ts
|
@ -3,47 +3,48 @@ 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);
|
||||
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: Buffer, _encoding: any, callback: Function) {
|
||||
let term: Buffer;
|
||||
write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) {
|
||||
const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||
|
||||
try {
|
||||
term = bert.encode(obj, true);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
const term = bert.encode(obj, true);
|
||||
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;
|
||||
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.
|
||||
|
|
Loading…
Reference in New Issue