| 1 |
'use strict' |
| 2 |
|
| 3 |
const { InvalidArgumentError, MaxOriginsReachedError } = require('../core/errors') |
| 4 |
const { kBusy, kClients, kConnected, kRunning, kClose, kDestroy, kDispatch, kUrl } = require('../core/symbols') |
| 5 |
const DispatcherBase = require('./dispatcher-base') |
| 6 |
const Pool = require('./pool') |
| 7 |
const Client = require('./client') |
| 8 |
const util = require('../core/util') |
| 9 |
|
| 10 |
const kOnConnect = Symbol('onConnect') |
| 11 |
const kOnDisconnect = Symbol('onDisconnect') |
| 12 |
const kOnConnectionError = Symbol('onConnectionError') |
| 13 |
const kOnDrain = Symbol('onDrain') |
| 14 |
const kFactory = Symbol('factory') |
| 15 |
const kOptions = Symbol('options') |
| 16 |
const kOrigins = Symbol('origins') |
| 17 |
|
| 18 |
function defaultFactory (origin, opts) { |
| 19 |
return opts && opts.connections === 1 |
| 20 |
? new Client(origin, opts) |
| 21 |
: new Pool(origin, opts) |
| 22 |
} |
| 23 |
|
| 24 |
class Agent extends DispatcherBase { |
| 25 |
constructor ({ factory = defaultFactory, maxOrigins = Infinity, connect, ...options } = {}) { |
| 26 |
if (typeof factory !== 'function') { |
| 27 |
throw new InvalidArgumentError('factory must be a function.') |
| 28 |
} |
| 29 |
|
| 30 |
if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') { |
| 31 |
throw new InvalidArgumentError('connect must be a function or an object') |
| 32 |
} |
| 33 |
|
| 34 |
if (typeof maxOrigins !== 'number' || Number.isNaN(maxOrigins) || maxOrigins <= 0) { |
| 35 |
throw new InvalidArgumentError('maxOrigins must be a number greater than 0') |
| 36 |
} |
| 37 |
|
| 38 |
super(options) |
| 39 |
|
| 40 |
if (connect && typeof connect !== 'function') { |
| 41 |
connect = { ...connect } |
| 42 |
} |
| 43 |
|
| 44 |
this[kOptions] = { ...util.deepClone(options), maxOrigins, connect } |
| 45 |
this[kFactory] = factory |
| 46 |
this[kClients] = new Map() |
| 47 |
this[kOrigins] = new Set() |
| 48 |
|
| 49 |
this[kOnDrain] = (origin, targets) => { |
| 50 |
this.emit('drain', origin, [this, ...targets]) |
| 51 |
} |
| 52 |
|
| 53 |
this[kOnConnect] = (origin, targets) => { |
| 54 |
this.emit('connect', origin, [this, ...targets]) |
| 55 |
} |
| 56 |
|
| 57 |
this[kOnDisconnect] = (origin, targets, err) => { |
| 58 |
this.emit('disconnect', origin, [this, ...targets], err) |
| 59 |
} |
| 60 |
|
| 61 |
this[kOnConnectionError] = (origin, targets, err) => { |
| 62 |
this.emit('connectionError', origin, [this, ...targets], err) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
get [kRunning] () { |
| 67 |
let ret = 0 |
| 68 |
for (const dispatcher of this[kClients].values()) { |
| 69 |
ret += dispatcher[kRunning] |
| 70 |
} |
| 71 |
return ret |
| 72 |
} |
| 73 |
|
| 74 |
[kDispatch] (opts, handler) { |
| 75 |
let origin |
| 76 |
if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) { |
| 77 |
origin = String(opts.origin) |
| 78 |
} else { |
| 79 |
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.') |
| 80 |
} |
| 81 |
|
| 82 |
const allowH2 = opts.allowH2 ?? this[kOptions].allowH2 |
| 83 |
const key = allowH2 === false ? `${origin}#http1-only` : origin |
| 84 |
|
| 85 |
if (this[kOrigins].size >= this[kOptions].maxOrigins && !this[kOrigins].has(origin)) { |
| 86 |
throw new MaxOriginsReachedError() |
| 87 |
} |
| 88 |
|
| 89 |
let dispatcher = this[kClients].get(key) |
| 90 |
if (!dispatcher) { |
| 91 |
dispatcher = this[kFactory](opts.origin, allowH2 === false |
| 92 |
? { ...this[kOptions], allowH2: false } |
| 93 |
: this[kOptions]) |
| 94 |
|
| 95 |
const closeClientIfUnused = () => { |
| 96 |
if (this[kClients].get(key) !== dispatcher) { |
| 97 |
return |
| 98 |
} |
| 99 |
|
| 100 |
if (dispatcher[kConnected] > 0 || dispatcher[kBusy]) { |
| 101 |
return |
| 102 |
} |
| 103 |
|
| 104 |
this[kClients].delete(key) |
| 105 |
if (!dispatcher.destroyed) { |
| 106 |
dispatcher.close() |
| 107 |
} |
| 108 |
|
| 109 |
let hasOrigin = false |
| 110 |
for (const client of this[kClients].values()) { |
| 111 |
if (client[kUrl].origin === dispatcher[kUrl].origin) { |
| 112 |
hasOrigin = true |
| 113 |
break |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (!hasOrigin) { |
| 118 |
this[kOrigins].delete(dispatcher[kUrl].origin) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
dispatcher |
| 123 |
.on('drain', this[kOnDrain]) |
| 124 |
.on('connect', this[kOnConnect]) |
| 125 |
.on('disconnect', (origin, targets, err) => { |
| 126 |
closeClientIfUnused() |
| 127 |
this[kOnDisconnect](origin, targets, err) |
| 128 |
}) |
| 129 |
.on('connectionError', (origin, targets, err) => { |
| 130 |
closeClientIfUnused() |
| 131 |
this[kOnConnectionError](origin, targets, err) |
| 132 |
}) |
| 133 |
|
| 134 |
this[kClients].set(key, dispatcher) |
| 135 |
this[kOrigins].add(origin) |
| 136 |
} |
| 137 |
|
| 138 |
return dispatcher.dispatch(opts, handler) |
| 139 |
} |
| 140 |
|
| 141 |
[kClose] () { |
| 142 |
const closePromises = [] |
| 143 |
for (const dispatcher of this[kClients].values()) { |
| 144 |
closePromises.push(dispatcher.close()) |
| 145 |
} |
| 146 |
this[kClients].clear() |
| 147 |
|
| 148 |
return Promise.all(closePromises) |
| 149 |
} |
| 150 |
|
| 151 |
[kDestroy] (err) { |
| 152 |
const destroyPromises = [] |
| 153 |
for (const dispatcher of this[kClients].values()) { |
| 154 |
destroyPromises.push(dispatcher.destroy(err)) |
| 155 |
} |
| 156 |
this[kClients].clear() |
| 157 |
|
| 158 |
return Promise.all(destroyPromises) |
| 159 |
} |
| 160 |
|
| 161 |
get stats () { |
| 162 |
const allClientStats = {} |
| 163 |
for (const dispatcher of this[kClients].values()) { |
| 164 |
if (dispatcher.stats) { |
| 165 |
allClientStats[dispatcher[kUrl].origin] = dispatcher.stats |
| 166 |
} |
| 167 |
} |
| 168 |
return allClientStats |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
module.exports = Agent |