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);
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
const len = Buffer.alloc(4);
|
const len = Buffer.alloc(4);
|
||||||
len.writeUInt32BE(term.length, 0);
|
len.writeUInt32BE(term.length, 0);
|
||||||
process.stdout.write = this._write;
|
|
||||||
process.stdout.write(len);
|
process.stdout.write(len);
|
||||||
process.stdout.write(term, callback);
|
return process.stdout.write(term, actualCallback);
|
||||||
process.stdout.write = fakeWrite;
|
}
|
||||||
|
catch (error) {
|
||||||
|
process.stderr.write(`Error writing: ${error}\n`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// When all the data is done passing, it stops.
|
// When all the data is done passing, it stops.
|
||||||
_final() {
|
_final() {
|
||||||
|
|
49
src/main.ts
49
src/main.ts
|
@ -3,47 +3,48 @@ 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() {
|
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 (
|
else {
|
||||||
termLen !== undefined &&
|
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
||||||
null !== (term = process.stdin.read(termLen))
|
return null
|
||||||
) {
|
|
||||||
termLen = undefined;
|
|
||||||
this.push(bert.decode(term));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
_write(obj: Buffer, _encoding: any, callback: Function) {
|
write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined) {
|
||||||
let term: Buffer;
|
const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
term = bert.encode(obj, true);
|
const term = bert.encode(obj, true);
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
const len = Buffer.alloc(4);
|
const len = Buffer.alloc(4);
|
||||||
len.writeUInt32BE(term.length, 0);
|
len.writeUInt32BE(term.length, 0);
|
||||||
process.stdout.write = this._write as any;
|
|
||||||
process.stdout.write(len);
|
process.stdout.write(len);
|
||||||
process.stdout.write(term, callback as any);
|
return process.stdout.write(term, actualCallback);
|
||||||
process.stdout.write = fakeWrite as any;
|
}
|
||||||
|
catch (error) {
|
||||||
|
process.stderr.write(`Error writing: ${error}\n`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When all the data is done passing, it stops.
|
// When all the data is done passing, it stops.
|
||||||
|
|
Loading…
Reference in New Issue