@@ -44,7 +44,7 @@ import {
4444 Context
4545} from '@elastic/transport/lib/types'
4646import { RedactionOptions } from '@elastic/transport/lib/Transport'
47- import BaseConnection , { prepareHeaders } from '@elastic/transport/lib/connection/BaseConnection'
47+ import BaseConnection , { prepareHeaders , ConnectionOptions } from '@elastic/transport/lib/connection/BaseConnection'
4848import SniffingTransport from './sniffingTransport'
4949import Helpers from './helpers'
5050import API from './api'
@@ -237,7 +237,35 @@ export default class Client extends API {
237237 diagnostic : this . diagnostic ,
238238 caFingerprint : options . caFingerprint
239239 } )
240- this . connectionPool . addConnection ( options . node ?? options . nodes )
240+
241+ // ensure default connection values are inherited when creating new connections
242+ // see https://github.com/elastic/elasticsearch-js/issues/1791
243+ const nodes = options . node ?? options . nodes
244+ let nodeOptions : Array < string | ConnectionOptions > = Array . isArray ( nodes ) ? nodes : [ nodes ]
245+ type ConnectionDefaults = Record < string , any >
246+ nodeOptions = nodeOptions . map ( opt => {
247+ const { tls, headers, auth, requestTimeout : timeout , agent, proxy, caFingerprint } = options
248+ let defaults : ConnectionDefaults = { tls, headers, auth, timeout, agent, proxy, caFingerprint }
249+
250+ // strip undefined values from defaults
251+ defaults = Object . keys ( defaults ) . reduce ( ( acc : ConnectionDefaults , key ) => {
252+ const val = defaults [ key ]
253+ if ( val !== undefined ) acc [ key ] = val
254+ return acc
255+ } , { } )
256+
257+ let newOpts
258+ if ( typeof opt === 'string' ) {
259+ newOpts = {
260+ url : new URL ( opt )
261+ }
262+ } else {
263+ newOpts = opt
264+ }
265+
266+ return { ...defaults , ...newOpts }
267+ } )
268+ this . connectionPool . addConnection ( nodeOptions )
241269 }
242270
243271 this . transport = new options . Transport ( {
@@ -282,7 +310,7 @@ export default class Client extends API {
282310 // Merge the new options with the initial ones
283311 // @ts -expect-error kChild symbol is for internal use only
284312 const options : ClientOptions = Object . assign ( { } , this [ kInitialOptions ] , opts )
285- // Pass to the child client the parent instances that cannot be overriden
313+ // Pass to the child client the parent instances that cannot be overridden
286314 // @ts -expect-error kInitialOptions symbol is for internal use only
287315 options [ kChild ] = {
288316 connectionPool : this . connectionPool ,
0 commit comments