From 8ee1d99bf3ddd2bcb74be2dc5ad6e3839f3756cb Mon Sep 17 00:00:00 2001 From: Roj Date: Thu, 6 Jun 2024 16:16:47 +0300 Subject: [PATCH 1/2] Fix #185 --- packages/shim-deno/src/deno/internal/Conn.ts | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/shim-deno/src/deno/internal/Conn.ts b/packages/shim-deno/src/deno/internal/Conn.ts index 72607ac..2735bbf 100644 --- a/packages/shim-deno/src/deno/internal/Conn.ts +++ b/packages/shim-deno/src/deno/internal/Conn.ts @@ -1,6 +1,7 @@ /// import { Socket } from "net"; +import { once } from "events"; import { FsFile } from "../stable/classes/FsFile.js"; @@ -40,6 +41,38 @@ export class Conn extends FsFile implements Deno.Conn { unref(): void { this.#socket.unref(); } + + async read(p: Uint8Array): Promise { + let wait = false; + while (true) { + if (wait) { + try { + await once(this.#socket, "readable", { + signal: AbortSignal.timeout(5), + }); + } catch (error) { + if ( + !(error != null && typeof error === "object" && "name" in error && + error.name == "AbortError") + ) { + throw error; + } + } + wait = false; + } + try { + return await super.read(p); + } catch (error) { + if ( + !(error != null && typeof error === "object" && "code" in error && + error.code == "EAGAIN") + ) { + throw error; + } + wait = true; + } + } + } } export class TlsConn extends Conn implements Deno.TlsConn { From fe6d357a02ff0cf486e6acc13243537cb8a7a707 Mon Sep 17 00:00:00 2001 From: Roj Date: Thu, 6 Jun 2024 23:18:49 +0300 Subject: [PATCH 2/2] Simplify --- packages/shim-deno/src/deno/internal/Conn.ts | 37 ++++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/packages/shim-deno/src/deno/internal/Conn.ts b/packages/shim-deno/src/deno/internal/Conn.ts index 2735bbf..5ebdc0e 100644 --- a/packages/shim-deno/src/deno/internal/Conn.ts +++ b/packages/shim-deno/src/deno/internal/Conn.ts @@ -43,35 +43,18 @@ export class Conn extends FsFile implements Deno.Conn { } async read(p: Uint8Array): Promise { - let wait = false; - while (true) { - if (wait) { - try { - await once(this.#socket, "readable", { - signal: AbortSignal.timeout(5), - }); - } catch (error) { - if ( - !(error != null && typeof error === "object" && "name" in error && - error.name == "AbortError") - ) { - throw error; - } - } - wait = false; - } - try { - return await super.read(p); - } catch (error) { - if ( - !(error != null && typeof error === "object" && "code" in error && - error.code == "EAGAIN") - ) { - throw error; - } - wait = true; + try { + return await super.read(p); + } catch (error) { + if ( + !(error instanceof Error && "code" in error && + error.code == "EAGAIN") + ) { + throw error; } } + await once(this.#socket, "readable"); + return await super.read(p); } }