MCPcopy
hub / github.com/nodejs/undici / [kDispatch]

Method [kDispatch]

lib/dispatcher/agent.js:74–139  ·  view source on GitHub ↗
(opts, handler)

Source from the content-addressed store, hash-verified

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)

Callers

nothing calls this directly

Calls 6

hasMethod · 0.65
getMethod · 0.65
addMethod · 0.65
onMethod · 0.45
setMethod · 0.45
dispatchMethod · 0.45

Tested by

no test coverage detected