slige/web/runtime.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-11-21 11:08:07 +00:00
export class Runtime {
constructor(private port: number) {}
async connect(): Promise<RuntimeConnection> {
return new RuntimeConnection(
await Deno.connect({
port: this.port,
}),
);
}
}
export class RuntimeConnection {
constructor(private connection: Deno.Conn) {}
async write(text: string): Promise<void> {
const req = new TextEncoder().encode(text);
await this.connection.write(req);
}
async send<T>(value: T): Promise<void> {
await this.write(JSON.stringify(value));
}
async read(): Promise<string> {
let result = "";
while (true) {
const buf = new Uint8Array(256);
const readRes = await this.connection.read(buf);
result += new TextDecoder().decode(buf);
if (readRes == null) {
break;
}
}
return result;
}
async receive<T>(): Promise<T> {
return JSON.parse(await this.read()) as T;
}
close() {
this.connection.close();
}
}