fix: use webstream

splashcat-exporter-v2
spacemeowx2 2024-01-30 15:38:39 +08:00 committed by imspace
parent a5fde51eeb
commit f69aaef6d7
2 changed files with 12 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { io } from "../../deps.ts"; import { readLines } from "../utils.ts";
import { Transport } from "./types.ts"; import { Transport } from "./types.ts";
export class DenoIO implements Transport { export class DenoIO implements Transport {
@ -8,7 +8,7 @@ export class DenoIO implements Transport {
reader: ReadableStream<Uint8Array>; reader: ReadableStream<Uint8Array>;
writer: WritableStream<Uint8Array>; writer: WritableStream<Uint8Array>;
}) { }) {
this.lines = io.readLines(reader); this.lines = readLines(reader);
this.writer = writer; this.writer = writer;
} }
async recv(): Promise<string | undefined> { async recv(): Promise<string | undefined> {
@ -21,10 +21,13 @@ export class DenoIO implements Transport {
return undefined; return undefined;
} }
async send(data: string) { async send(data: string) {
await writeAll( const writer = this.writer.getWriter();
this.writer, try {
new TextEncoder().encode(data + "\n"), await writer.ready;
); await writer.write(new TextEncoder().encode(data + "\n"));
} finally {
writer.releaseLock();
}
} }
async close() { async close() {
await this.writer.close(); await this.writer.close();

View File

@ -7,11 +7,11 @@ import {
import { base64, uuid } from "../deps.ts"; import { base64, uuid } from "../deps.ts";
import { Env } from "./env.ts"; import { Env } from "./env.ts";
async function* _readlines() { export async function* readLines(readable: ReadableStream<Uint8Array>) {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
let buffer = ""; let buffer = "";
for await (const chunk of Deno.stdin.readable) { for await (const chunk of readable) {
buffer += decoder.decode(chunk); buffer += decoder.decode(chunk);
let lineEndIndex; let lineEndIndex;
@ -28,7 +28,7 @@ async function* _readlines() {
} }
} }
const stdinLines = _readlines(); const stdinLines = readLines(Deno.stdin.readable);
export async function readline( export async function readline(
{ skipEmpty = true }: { skipEmpty?: boolean } = {}, { skipEmpty = true }: { skipEmpty?: boolean } = {},
) { ) {