s3si.ts/src/jsonrpc/deno.ts

33 lines
744 B
TypeScript
Raw Normal View History

2024-01-30 01:30:56 -05:00
import { io } from "../../deps.ts";
2023-03-05 07:01:48 -05:00
import { Transport } from "./types.ts";
export class DenoIO implements Transport {
lines: AsyncIterableIterator<string>;
2024-01-30 01:30:56 -05:00
writer: WritableStream<Uint8Array>;
2023-03-05 07:01:48 -05:00
constructor({ reader, writer }: {
2024-01-30 01:30:56 -05:00
reader: ReadableStream<Uint8Array>;
writer: WritableStream<Uint8Array>;
2023-03-05 07:01:48 -05:00
}) {
this.lines = io.readLines(reader);
this.writer = writer;
}
async recv(): Promise<string | undefined> {
const result = await this.lines.next();
if (!result.done) {
2023-03-05 12:56:07 -05:00
return result.value;
2023-03-05 07:01:48 -05:00
}
return undefined;
}
async send(data: string) {
await writeAll(
this.writer,
new TextEncoder().encode(data + "\n"),
);
}
async close() {
await this.writer.close();
}
}