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