@@ -44,7 +44,7 @@ import {
44
44
Context
45
45
} from '@elastic/transport/lib/types'
46
46
import { 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'
48
48
import SniffingTransport from './sniffingTransport'
49
49
import Helpers from './helpers'
50
50
import API from './api'
@@ -237,7 +237,35 @@ export default class Client extends API {
237
237
diagnostic : this . diagnostic ,
238
238
caFingerprint : options . caFingerprint
239
239
} )
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 )
241
269
}
242
270
243
271
this . transport = new options . Transport ( {
@@ -282,7 +310,7 @@ export default class Client extends API {
282
310
// Merge the new options with the initial ones
283
311
// @ts -expect-error kChild symbol is for internal use only
284
312
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
286
314
// @ts -expect-error kInitialOptions symbol is for internal use only
287
315
options [ kChild ] = {
288
316
connectionPool : this . connectionPool ,
0 commit comments