export class PromiseSocket { #socket: Deno.DatagramConn; constructor() { this.#socket = Deno.listenDatagram({ port: 0, transport: 'udp', }); } send = async ( buffer: Uint8Array, host: string, port: number, ): Promise<Result<Uint8Array>> => { try { const message_buffer = await this.#send(buffer, host, port); return pass(message_buffer); } catch (err) { return fail(err); } }; close = () => { this.#socket.close(); }; #send = async ( buffer: Uint8Array, host: string, port: number, ): Promise<Uint8Array> => { const addr = { hostname: host, port, transport: 'udp', } satisfies Deno.NetAddr; await this.#socket.send(buffer, addr); const [message] = await this.#socket.receive(); return message; };}
deno bundle