From d777191e330319653596f7dc6ac7d154cdcced6c Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Fri, 25 Oct 2024 13:06:28 +0900 Subject: [PATCH] remove workaround using linsterCount as it didn't work. Instead use workaround reading callstack in `connect()` call and pause the socket when it's from @npmcli/agent. This prevents the undesirable initial read of the socket. --- ext/node/polyfills/net.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/ext/node/polyfills/net.ts b/ext/node/polyfills/net.ts index 8b598d0bfc..b29fa96300 100644 --- a/ext/node/polyfills/net.ts +++ b/ext/node/polyfills/net.ts @@ -360,21 +360,9 @@ function _afterConnect( socket._unrefTimer(); - const connectListeners = socket.listenerCount("connect"); - const readyListeners = socket.listenerCount("ready"); - const dataListeners = socket.listenerCount("data"); - socket.emit("connect"); socket.emit("ready"); - // Note: This is Deno specific logic - // If there's no listener for the connect, ready, data event, - // we delay the first read. This is necessary for http.request to work properly. - // See https://github.com/denoland/deno/pull/25470#issuecomment-2435077722 - if (connectListeners === 0 && readyListeners === 0 && dataListeners === 0) { - return; - } - // Start the first read, or get an immediate EOF. // this doesn't actually consume any bytes, because len=0. if (readable && !socket.isPaused()) { @@ -1598,6 +1586,15 @@ export function connect(...args: unknown[]) { debug("createConnection", normalized); const socket = new Socket(options); + // If it's called from @npmcli/agent, 'connect' event on Socket happens + // before 'socket' event on ClientRequst, and that causes initial read + // happening before op_node_http_request_with_conn(), and http reqeust + // doesn't work. The below pause() call prevent that initial read for + // @npmcli/agent. + if (new Error().stack?.includes("@npmcli/agent")) { + socket.pause(); + } + if (netClientSocketChannel.hasSubscribers) { netClientSocketChannel.publish({ socket,