TangJieHao
TangJieHao2mo ago

WritableSteam not opening.

constructor() {
this.command = new Deno.Command(this.exec, {
args: ["analysis", "-model", this.model, "-config", this.config],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});

this.AI = this.command.spawn();
this.stdin = this.AI.stdin.getWriter();
this.stdout = this.AI.stdout.getReader();
this.stderr = this.AI.stderr.getReader();
}

async writeToStdin(data: string): Promise<void> {
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
await this.stdin.write(encoded);
}

async readFromStdout(): Promise<string> {
let buffer = "";
const decoder = new TextDecoder();
while (true) {
const { value, done } = await this.stdout.read();
if (done) {
return buffer.trim();
}

buffer += decoder.decode(value, { stream: true });

}
}
constructor() {
this.command = new Deno.Command(this.exec, {
args: ["analysis", "-model", this.model, "-config", this.config],
stdin: "piped",
stdout: "piped",
stderr: "piped",
});

this.AI = this.command.spawn();
this.stdin = this.AI.stdin.getWriter();
this.stdout = this.AI.stdout.getReader();
this.stderr = this.AI.stderr.getReader();
}

async writeToStdin(data: string): Promise<void> {
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
await this.stdin.write(encoded);
}

async readFromStdout(): Promise<string> {
let buffer = "";
const decoder = new TextDecoder();
while (true) {
const { value, done } = await this.stdout.read();
if (done) {
return buffer.trim();
}

buffer += decoder.decode(value, { stream: true });

}
}
I seem to only be able to write to the stdin once. I have tried closing and getting a new getWriter with every new write call but I get same issue that stdin is closed or is closing
1 Reply
TangJieHao
TangJieHao2mo ago
So if I put releaseLock at the end this seems to unlock the stdin however it still gives me the error that even though the stream isn't locked anymore writer wont write anymore
async writeToStdin(data: string): Promise<void> {
console.log("Writing to stdin");
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
console.log(this.stdin);
const writer = await this.stdin.getWriter();
console.log(writer);
await writer.write(encoded);
writer.close();
writer.releaseLock();
}
async writeToStdin(data: string): Promise<void> {
console.log("Writing to stdin");
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
console.log(this.stdin);
const writer = await this.stdin.getWriter();
console.log(writer);
await writer.write(encoded);
writer.close();
writer.releaseLock();
}
Writing to stdin
WritableStream { locked: false }
WritableStreamDefaultWriter {
closed: Promise { undefined },
desiredSize: 0,
ready: Promise { undefined }
}
error: Uncaught (in promise) TypeError: The stream is closing or is closed.
await writer.write(encoded);
Writing to stdin
WritableStream { locked: false }
WritableStreamDefaultWriter {
closed: Promise { undefined },
desiredSize: 0,
ready: Promise { undefined }
}
error: Uncaught (in promise) TypeError: The stream is closing or is closed.
await writer.write(encoded);
It looks like I may have found my solution. The reader doesn't actually know when the stream is done because its continious so I need to exit early.
async writeToStdin(data: string): Promise<void> {
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
await this.stdinWriter.ready;
await this.stdinWriter.write(encoded);
}

async readFromStdout(): Promise<string> {
let buffer = "";
const decoder = new TextDecoder();
while (true) {
const { value, done } = await this.stdoutReader.read();
if (done) {
return buffer.trim();
}
buffer += decoder.decode(value, { stream: true });
console.log(buffer);

if (buffer.endsWith("\n")) {
return buffer.trim();
}
}
}
async writeToStdin(data: string): Promise<void> {
const encoder = new TextEncoder();
const encoded = encoder.encode(data);
await this.stdinWriter.ready;
await this.stdinWriter.write(encoded);
}

async readFromStdout(): Promise<string> {
let buffer = "";
const decoder = new TextDecoder();
while (true) {
const { value, done } = await this.stdoutReader.read();
if (done) {
return buffer.trim();
}
buffer += decoder.decode(value, { stream: true });
console.log(buffer);

if (buffer.endsWith("\n")) {
return buffer.trim();
}
}
}