From c7c1e93beba7310d7c2cc9647dd211a686d21cac Mon Sep 17 00:00:00 2001 From: Nabeel Bukhari Date: Fri, 4 Aug 2023 20:35:27 +0200 Subject: [PATCH] feat: return socket from createConnection (#113) As per http.agent [createConnection](https://nodejs.org/api/http.html#agentcreateconnectionoptions-callback) method, the method should return a socket but I found that current implemenation of createConnection for Agent doesn't return socket. Co-authored-by: Nabeel Bukhari --- index.d.ts | 3 +++ lib/agent.js | 1 + lib/https_agent.js | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index c2ce7d2..2bc70a6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,6 @@ import * as http from 'http'; import * as https from 'https'; +import * as net from 'net'; interface PlainObject { [key: string]: any; @@ -8,6 +9,7 @@ interface PlainObject { declare class HttpAgent extends http.Agent { constructor(opts?: AgentKeepAlive.HttpOptions); readonly statusChanged: boolean; + createConnection(options: net.NetConnectOpts, cb?: Function): net.Socket; createSocket(req: http.IncomingMessage, options: http.RequestOptions, cb: Function): void; getCurrentStatus(): AgentKeepAlive.AgentStatus; } @@ -52,6 +54,7 @@ declare namespace AgentKeepAlive { export class HttpsAgent extends https.Agent { constructor(opts?: HttpsOptions); readonly statusChanged: boolean; + createConnection(options: net.NetConnectOpts, cb?: Function): net.Socket; createSocket(req: http.IncomingMessage, options: http.RequestOptions, cb: Function): void; getCurrentStatus(): AgentStatus; } diff --git a/lib/agent.js b/lib/agent.js index a7065b5..867988f 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -230,6 +230,7 @@ class Agent extends OriginalAgent { const newSocket = super.createConnection(options, onNewCreate); if (newSocket) onNewCreate(null, newSocket); + return newSocket; } get statusChanged() { diff --git a/lib/https_agent.js b/lib/https_agent.js index 73f529d..344fb32 100644 --- a/lib/https_agent.js +++ b/lib/https_agent.js @@ -25,8 +25,8 @@ class HttpsAgent extends HttpAgent { }; } - createConnection(options) { - const socket = this[CREATE_HTTPS_CONNECTION](options); + createConnection(options, oncreate) { + const socket = this[CREATE_HTTPS_CONNECTION](options, oncreate); this[INIT_SOCKET](socket, options); return socket; }