export class Runtime { constructor(private port: number) {} async connect(): Promise { return new RuntimeConnection( await Deno.connect({ port: this.port, }), ); } } export class RuntimeConnection { constructor(private connection: Deno.Conn) {} async write(text: string): Promise { const req = new TextEncoder().encode(text); await this.connection.write(req); } async send(value: T): Promise { await this.write(JSON.stringify(value)); } async read(): Promise { 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(): Promise { return JSON.parse(await this.read()) as T; } close() { this.connection.close(); } }