11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  PoolBase,
51cb0ef41Sopenharmony_ci  kClients,
61cb0ef41Sopenharmony_ci  kNeedDrain,
71cb0ef41Sopenharmony_ci  kAddClient,
81cb0ef41Sopenharmony_ci  kGetDispatcher
91cb0ef41Sopenharmony_ci} = require('./pool-base')
101cb0ef41Sopenharmony_ciconst Client = require('./client')
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  InvalidArgumentError
131cb0ef41Sopenharmony_ci} = require('./core/errors')
141cb0ef41Sopenharmony_ciconst util = require('./core/util')
151cb0ef41Sopenharmony_ciconst { kUrl, kInterceptors } = require('./core/symbols')
161cb0ef41Sopenharmony_ciconst buildConnector = require('./core/connect')
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst kOptions = Symbol('options')
191cb0ef41Sopenharmony_ciconst kConnections = Symbol('connections')
201cb0ef41Sopenharmony_ciconst kFactory = Symbol('factory')
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cifunction defaultFactory (origin, opts) {
231cb0ef41Sopenharmony_ci  return new Client(origin, opts)
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciclass Pool extends PoolBase {
271cb0ef41Sopenharmony_ci  constructor (origin, {
281cb0ef41Sopenharmony_ci    connections,
291cb0ef41Sopenharmony_ci    factory = defaultFactory,
301cb0ef41Sopenharmony_ci    connect,
311cb0ef41Sopenharmony_ci    connectTimeout,
321cb0ef41Sopenharmony_ci    tls,
331cb0ef41Sopenharmony_ci    maxCachedSessions,
341cb0ef41Sopenharmony_ci    socketPath,
351cb0ef41Sopenharmony_ci    autoSelectFamily,
361cb0ef41Sopenharmony_ci    autoSelectFamilyAttemptTimeout,
371cb0ef41Sopenharmony_ci    allowH2,
381cb0ef41Sopenharmony_ci    ...options
391cb0ef41Sopenharmony_ci  } = {}) {
401cb0ef41Sopenharmony_ci    super()
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    if (connections != null && (!Number.isFinite(connections) || connections < 0)) {
431cb0ef41Sopenharmony_ci      throw new InvalidArgumentError('invalid connections')
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    if (typeof factory !== 'function') {
471cb0ef41Sopenharmony_ci      throw new InvalidArgumentError('factory must be a function.')
481cb0ef41Sopenharmony_ci    }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
511cb0ef41Sopenharmony_ci      throw new InvalidArgumentError('connect must be a function or an object')
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    if (typeof connect !== 'function') {
551cb0ef41Sopenharmony_ci      connect = buildConnector({
561cb0ef41Sopenharmony_ci        ...tls,
571cb0ef41Sopenharmony_ci        maxCachedSessions,
581cb0ef41Sopenharmony_ci        allowH2,
591cb0ef41Sopenharmony_ci        socketPath,
601cb0ef41Sopenharmony_ci        timeout: connectTimeout,
611cb0ef41Sopenharmony_ci        ...(util.nodeHasAutoSelectFamily && autoSelectFamily ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
621cb0ef41Sopenharmony_ci        ...connect
631cb0ef41Sopenharmony_ci      })
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    this[kInterceptors] = options.interceptors && options.interceptors.Pool && Array.isArray(options.interceptors.Pool)
671cb0ef41Sopenharmony_ci      ? options.interceptors.Pool
681cb0ef41Sopenharmony_ci      : []
691cb0ef41Sopenharmony_ci    this[kConnections] = connections || null
701cb0ef41Sopenharmony_ci    this[kUrl] = util.parseOrigin(origin)
711cb0ef41Sopenharmony_ci    this[kOptions] = { ...util.deepClone(options), connect, allowH2 }
721cb0ef41Sopenharmony_ci    this[kOptions].interceptors = options.interceptors
731cb0ef41Sopenharmony_ci      ? { ...options.interceptors }
741cb0ef41Sopenharmony_ci      : undefined
751cb0ef41Sopenharmony_ci    this[kFactory] = factory
761cb0ef41Sopenharmony_ci  }
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  [kGetDispatcher] () {
791cb0ef41Sopenharmony_ci    let dispatcher = this[kClients].find(dispatcher => !dispatcher[kNeedDrain])
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci    if (dispatcher) {
821cb0ef41Sopenharmony_ci      return dispatcher
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    if (!this[kConnections] || this[kClients].length < this[kConnections]) {
861cb0ef41Sopenharmony_ci      dispatcher = this[kFactory](this[kUrl], this[kOptions])
871cb0ef41Sopenharmony_ci      this[kAddClient](dispatcher)
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci    return dispatcher
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_cimodule.exports = Pool
95