diff --git a/src/jsonrpc/deno.ts b/src/jsonrpc/deno.ts index 8319533..3d3d235 100644 --- a/src/jsonrpc/deno.ts +++ b/src/jsonrpc/deno.ts @@ -1,4 +1,4 @@ -import { io } from "../../deps.ts"; +import { readLines } from "../utils.ts"; import { Transport } from "./types.ts"; export class DenoIO implements Transport { @@ -8,7 +8,7 @@ export class DenoIO implements Transport { reader: ReadableStream; writer: WritableStream; }) { - this.lines = io.readLines(reader); + this.lines = readLines(reader); this.writer = writer; } async recv(): Promise { @@ -21,10 +21,13 @@ export class DenoIO implements Transport { return undefined; } async send(data: string) { - await writeAll( - this.writer, - new TextEncoder().encode(data + "\n"), - ); + const writer = this.writer.getWriter(); + try { + await writer.ready; + await writer.write(new TextEncoder().encode(data + "\n")); + } finally { + writer.releaseLock(); + } } async close() { await this.writer.close(); diff --git a/src/utils.ts b/src/utils.ts index fb71ac5..fc2f988 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,11 +7,11 @@ import { import { base64, uuid } from "../deps.ts"; import { Env } from "./env.ts"; -async function* _readlines() { +export async function* readLines(readable: ReadableStream) { const decoder = new TextDecoder(); let buffer = ""; - for await (const chunk of Deno.stdin.readable) { + for await (const chunk of readable) { buffer += decoder.decode(chunk); let lineEndIndex; @@ -28,7 +28,7 @@ async function* _readlines() { } } -const stdinLines = _readlines(); +const stdinLines = readLines(Deno.stdin.readable); export async function readline( { skipEmpty = true }: { skipEmpty?: boolean } = {}, ) {