try again
This commit is contained in:
parent
dc5c44a18d
commit
cca106a321
|
@ -1,10 +1,12 @@
|
|||
/// <reference types="node" />
|
||||
/// <reference types="node" />
|
||||
import { Bert } from "./bert.js";
|
||||
import { Duplex } from "node:stream";
|
||||
type WriteCallback = (error: Error | null | undefined) => void;
|
||||
export declare class Port extends Duplex {
|
||||
constructor();
|
||||
_read(): any;
|
||||
readonly bert: Bert;
|
||||
constructor(bert: Bert);
|
||||
_read(): void;
|
||||
_write(obj: any, encodingOrCallback?: BufferEncoding | WriteCallback, callback?: WriteCallback | undefined): boolean;
|
||||
}
|
||||
export {};
|
||||
|
|
|
@ -1,32 +1,28 @@
|
|||
import { Bert } from "./bert.js";
|
||||
import { Duplex } from "node:stream";
|
||||
const bert = new Bert();
|
||||
const fakeWrite = () => undefined;
|
||||
process.stdout.write = fakeWrite;
|
||||
export class Port extends Duplex {
|
||||
constructor() {
|
||||
bert;
|
||||
constructor(bert) {
|
||||
super({ objectMode: true });
|
||||
this.bert = bert;
|
||||
}
|
||||
_read() {
|
||||
const lenBytes = process.stdin.read(4);
|
||||
if (lenBytes) {
|
||||
const termLen = bert.bytesToInt(lenBytes, 4, true);
|
||||
const termLen = this.bert.bytesToInt(lenBytes, 4, true);
|
||||
process.stderr.write(`Got term length: ${termLen}\n`);
|
||||
const termBytes = process.stdin.read(termLen);
|
||||
if (termBytes) {
|
||||
return bert.decode(termBytes);
|
||||
this.push(this.bert.decode(termBytes));
|
||||
}
|
||||
else {
|
||||
process.stderr.write(`Read should have gotten ${termLen} bytes.\n`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
_write(obj, encodingOrCallback, callback) {
|
||||
const actualCallback = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||
try {
|
||||
const term = bert.encode(obj, true);
|
||||
const term = this.bert.encode(obj, true);
|
||||
const len = Buffer.alloc(4);
|
||||
len.writeUInt32BE(term.length, 0);
|
||||
process.stdout.write(len);
|
||||
|
|
23
src/main.ts
23
src/main.ts
|
@ -1,40 +1,37 @@
|
|||
import { Bert } from "./bert.js";
|
||||
import { Duplex } from "node:stream";
|
||||
|
||||
const bert = new Bert();
|
||||
|
||||
const fakeWrite = () => undefined;
|
||||
process.stdout.write = fakeWrite as any;
|
||||
|
||||
type WriteCallback = (error: Error | null | undefined) => void;
|
||||
|
||||
export class Port extends Duplex {
|
||||
constructor() {
|
||||
public readonly bert: Bert;
|
||||
|
||||
constructor(bert: Bert) {
|
||||
super({ objectMode: true });
|
||||
this.bert = bert;
|
||||
}
|
||||
|
||||
read() {
|
||||
_read() {
|
||||
const lenBytes = process.stdin.read(4);
|
||||
if (lenBytes) {
|
||||
const termLen = bert.bytesToInt(lenBytes, 4, true);
|
||||
const termLen = this.bert.bytesToInt(lenBytes, 4, true);
|
||||
process.stderr.write(`Got term length: ${termLen}\n`);
|
||||
const termBytes = process.stdin.read(termLen);
|
||||
|
||||
if (termBytes) {
|
||||
return bert.decode(termBytes);
|
||||
this.push(this.bert.decode(termBytes));
|
||||
}
|
||||
else {
|
||||
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) {
|
||||
const actualCallback: any = callback || typeof encodingOrCallback === "function" ? encodingOrCallback : undefined;
|
||||
|
||||
try {
|
||||
const term = bert.encode(obj, true);
|
||||
const term = this.bert.encode(obj, true);
|
||||
const len = Buffer.alloc(4);
|
||||
len.writeUInt32BE(term.length, 0);
|
||||
|
||||
|
|
Loading…
Reference in New Issue