maybe works now?
This commit is contained in:
parent
05c7599696
commit
f5f2341218
|
@ -1,9 +1,11 @@
|
||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
import { Duplex } from "node:stream";
|
import { Duplex } from "node:stream";
|
||||||
|
type WriteCallback = (error: Error | null | undefined) => void;
|
||||||
export declare class Port extends Duplex {
|
export declare class Port extends Duplex {
|
||||||
constructor();
|
constructor();
|
||||||
_read(): void;
|
read(): any;
|
||||||
_write(obj: Buffer, _encoding: any, callback: Function): void;
|
write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
|
||||||
_final(): void;
|
_final(): void;
|
||||||
}
|
}
|
||||||
|
export {};
|
||||||
|
|
|
@ -1,42 +1,41 @@
|
||||||
import { Bert } from "./bert.js";
|
import { Bert } from "./bert.js";
|
||||||
import { Duplex } from "node:stream";
|
import { Duplex } from "node:stream";
|
||||||
const bert = new Bert();
|
const bert = new Bert();
|
||||||
let termLen;
|
|
||||||
let termBin;
|
|
||||||
const fakeWrite = () => undefined;
|
const fakeWrite = () => undefined;
|
||||||
process.stdout.write = fakeWrite;
|
process.stdout.write = fakeWrite;
|
||||||
export class Port extends Duplex {
|
export class Port extends Duplex {
|
||||||
constructor() {
|
constructor() {
|
||||||
super({ objectMode: true });
|
super({ objectMode: true });
|
||||||
}
|
}
|
||||||
_read() {
|
read() {
|
||||||
{
|
const lenBytes = process.stdin.read(4);
|
||||||
let term;
|
if (lenBytes) {
|
||||||
if (termLen === undefined && null !== (termBin = process.stdin.read(4))) {
|
const termLen = bert.bytesToInt(lenBytes, 4, true);
|
||||||
termLen = bert.bytesToInt(termBin, 4, true);
|
const termBytes = process.stdin.read(termLen);
|
||||||
|
if (termBytes) {
|
||||||
|
return bert.decode(termBytes);
|
||||||
}
|
}
|
||||||
if (termLen !== undefined &&
|
else {
|
||||||
null !== (term = process.stdin.read(termLen))) {
|
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
||||||
termLen = undefined;
|
return null;
|
||||||
this.push(bert.decode(term));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
_write(obj, _encoding, callback) {
|
write(obj, encodingOrCallback, callback) {
|
||||||
let term;
|
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||||
try {
|
try {
|
||||||
term = bert.encode(obj, true);
|
const term = bert.encode(obj, true);
|
||||||
|
const len = Buffer.alloc(4);
|
||||||
|
len.writeUInt32BE(term.length, 0);
|
||||||
|
process.stdout.write(len);
|
||||||
|
return process.stdout.write(term, actualCallback);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
process.stderr.write(`Error writing: ${error}\n`);
|
||||||
process.exit(1);
|
return false;
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
// When all the data is done passing, it stops.
|
// When all the data is done passing, it stops.
|
||||||
_final() {
|
_final() {
|
||||||
|
|
79
src/main.ts
79
src/main.ts
|
@ -3,52 +3,53 @@ import { Duplex } from "node:stream";
|
||||||
|
|
||||||
const bert = new Bert();
|
const bert = new Bert();
|
||||||
|
|
||||||
let termLen: number | undefined;
|
|
||||||
let termBin: Buffer;
|
|
||||||
|
|
||||||
const fakeWrite = () => undefined;
|
const fakeWrite = () => undefined;
|
||||||
process.stdout.write = fakeWrite as any;
|
process.stdout.write = fakeWrite as any;
|
||||||
|
|
||||||
|
type WriteCallback = (error: Error | null | undefined) => void;
|
||||||
|
|
||||||
export class Port extends Duplex {
|
export class Port extends Duplex {
|
||||||
constructor() {
|
constructor() {
|
||||||
super({ objectMode: true });
|
super({ objectMode: true });
|
||||||
}
|
|
||||||
|
|
||||||
_read() {
|
|
||||||
{
|
|
||||||
let term;
|
|
||||||
if (termLen === undefined && null !== (termBin = process.stdin.read(4))) {
|
|
||||||
termLen = bert.bytesToInt(termBin, 4, true);
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
termLen !== undefined &&
|
|
||||||
null !== (term = process.stdin.read(termLen))
|
|
||||||
) {
|
|
||||||
termLen = undefined;
|
|
||||||
this.push(bert.decode(term));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_write(obj: Buffer, _encoding: any, callback: Function) {
|
read() {
|
||||||
let term: Buffer;
|
const lenBytes = process.stdin.read(4);
|
||||||
try {
|
if (lenBytes) {
|
||||||
term = bert.encode(obj, true);
|
const termLen = bert.bytesToInt(lenBytes, 4, true);
|
||||||
} catch (error) {
|
const termBytes = process.stdin.read(termLen);
|
||||||
console.error(error);
|
|
||||||
process.exit(1);
|
if (termBytes) {
|
||||||
|
return bert.decode(termBytes);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else return null;
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// When all the data is done passing, it stops.
|
write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) {
|
||||||
_final() {
|
const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||||
this.push(null);
|
|
||||||
}
|
try {
|
||||||
|
const term = bert.encode(obj, true);
|
||||||
|
const len = Buffer.alloc(4);
|
||||||
|
len.writeUInt32BE(term.length, 0);
|
||||||
|
|
||||||
|
process.stdout.write(len);
|
||||||
|
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() {
|
||||||
|
this.push(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue