From 39c440909c39b8e5758646647203ec7dec490c6b Mon Sep 17 00:00:00 2001 From: Dima Voytenko Date: Sat, 23 Dec 2023 08:09:29 -0800 Subject: [PATCH] ConnectOptions should include 'origin' field (#2532) * ConnectOptions should include 'origin' field * Update origin field in Client.connect and Pool.connect --- test/types/agent.test-d.ts | 9 +++++++-- test/types/dispatcher.test-d.ts | 9 +++++++-- types/balanced-pool.d.ts | 11 +++++++++++ types/client.d.ts | 11 +++++++++++ types/dispatcher.d.ts | 1 + types/pool.d.ts | 11 +++++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/test/types/agent.test-d.ts b/test/types/agent.test-d.ts index 5e5275f6a58..5c12c480018 100644 --- a/test/types/agent.test-d.ts +++ b/test/types/agent.test-d.ts @@ -86,8 +86,13 @@ expectAssignable(new Agent({ factory: () => new Dispatcher() })) })) // connect - expectAssignable>(agent.connect({ path: '' })) - expectAssignable(agent.connect({ path: '' }, (err, data) => { + expectAssignable>(agent.connect({ origin: '', path: '' })) + expectAssignable>(agent.connect({ origin: new URL('http://localhost'), path: '' })) + expectAssignable(agent.connect({ origin: '', path: '' }, (err, data) => { + expectAssignable(err) + expectAssignable(data) + })) + expectAssignable(agent.connect({ origin: new URL('http://localhost'), path: '' }, (err, data) => { expectAssignable(err) expectAssignable(data) })) diff --git a/test/types/dispatcher.test-d.ts b/test/types/dispatcher.test-d.ts index cd4ebfd68e3..e40113bba5c 100644 --- a/test/types/dispatcher.test-d.ts +++ b/test/types/dispatcher.test-d.ts @@ -26,8 +26,13 @@ expectAssignable(new Dispatcher()) expectAssignable(dispatcher.dispatch({ origin: new URL('http://localhost'), path: '', method: 'GET' }, {})) // connect - expectAssignable>(dispatcher.connect({ path: '', maxRedirections: 0 })) - expectAssignable(dispatcher.connect({ path: '' }, (err, data) => { + expectAssignable>(dispatcher.connect({ origin: '', path: '', maxRedirections: 0 })) + expectAssignable>(dispatcher.connect({ origin: new URL('http://localhost'), path: '', maxRedirections: 0 })) + expectAssignable(dispatcher.connect({ origin: '', path: '' }, (err, data) => { + expectAssignable(err) + expectAssignable(data) + })) + expectAssignable(dispatcher.connect({ origin: new URL('http://localhost'), path: '' }, (err, data) => { expectAssignable(err) expectAssignable(data) })) diff --git a/types/balanced-pool.d.ts b/types/balanced-pool.d.ts index d1e9375875f..7f930f4108c 100644 --- a/types/balanced-pool.d.ts +++ b/types/balanced-pool.d.ts @@ -4,6 +4,8 @@ import { URL } from 'url' export default BalancedPool +type BalancedPoolConnectOptions = Omit; + declare class BalancedPool extends Dispatcher { constructor(url: string | string[] | URL | URL[], options?: Pool.Options); @@ -15,4 +17,13 @@ declare class BalancedPool extends Dispatcher { closed: boolean; /** `true` after `pool.destroyed()` has been called or `pool.close()` has been called and the pool shutdown has completed. */ destroyed: boolean; + + // Override dispatcher APIs. + override connect( + options: BalancedPoolConnectOptions + ): Promise; + override connect( + options: BalancedPoolConnectOptions, + callback: (err: Error | null, data: Dispatcher.ConnectData) => void + ): void; } diff --git a/types/client.d.ts b/types/client.d.ts index 56e78cc9765..d0a5379f33c 100644 --- a/types/client.d.ts +++ b/types/client.d.ts @@ -3,6 +3,8 @@ import { TlsOptions } from 'tls' import Dispatcher from './dispatcher' import buildConnector from "./connector"; +type ClientConnectOptions = Omit; + /** * A basic HTTP/1.1 client, mapped on top a single TCP/TLS connection. Pipelining is disabled by default. */ @@ -14,6 +16,15 @@ export class Client extends Dispatcher { closed: boolean; /** `true` after `client.destroyed()` has been called or `client.close()` has been called and the client shutdown has completed. */ destroyed: boolean; + + // Override dispatcher APIs. + override connect( + options: ClientConnectOptions + ): Promise; + override connect( + options: ClientConnectOptions, + callback: (err: Error | null, data: Dispatcher.ConnectData) => void + ): void; } export declare namespace Client { diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 24bf1519a25..5988c8a9c7d 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -121,6 +121,7 @@ declare namespace Dispatcher { expectContinue?: boolean; } export interface ConnectOptions { + origin: string | URL; path: string; /** Default: `null` */ headers?: IncomingHttpHeaders | string[] | null; diff --git a/types/pool.d.ts b/types/pool.d.ts index 7747d48261b..bad5ba0308e 100644 --- a/types/pool.d.ts +++ b/types/pool.d.ts @@ -5,6 +5,8 @@ import Dispatcher from "./dispatcher"; export default Pool +type PoolConnectOptions = Omit; + declare class Pool extends Dispatcher { constructor(url: string | URL, options?: Pool.Options) /** `true` after `pool.close()` has been called. */ @@ -13,6 +15,15 @@ declare class Pool extends Dispatcher { destroyed: boolean; /** Aggregate stats for a Pool. */ readonly stats: TPoolStats; + + // Override dispatcher APIs. + override connect( + options: PoolConnectOptions + ): Promise; + override connect( + options: PoolConnectOptions, + callback: (err: Error | null, data: Dispatcher.ConnectData) => void + ): void; } declare namespace Pool {