11'use strict'
22
3- const {
4- ClientClosedError,
5- InvalidArgumentError,
6- ClientDestroyedError
7- } = require ( './core/errors' )
8- const { kClients, kRunning } = require ( './core/symbols' )
9- const Dispatcher = require ( './dispatcher' )
3+ const { InvalidArgumentError } = require ( './core/errors' )
4+ const { kClients, kRunning, kClose, kDestroy, kDispatch } = require ( './core/symbols' )
5+ const DispatcherBase = require ( './dispatcher-base' )
106const Pool = require ( './pool' )
117const Client = require ( './client' )
128const util = require ( './core/util' )
139const RedirectHandler = require ( './handler/redirect' )
1410const { WeakRef, FinalizationRegistry } = require ( './compat/dispatcher-weakref' ) ( )
1511
16- const kDestroyed = Symbol ( 'destroyed' )
17- const kClosed = Symbol ( 'closed' )
1812const kOnConnect = Symbol ( 'onConnect' )
1913const kOnDisconnect = Symbol ( 'onDisconnect' )
2014const kOnConnectionError = Symbol ( 'onConnectionError' )
@@ -30,7 +24,7 @@ function defaultFactory (origin, opts) {
3024 : new Pool ( origin , opts )
3125}
3226
33- class Agent extends Dispatcher {
27+ class Agent extends DispatcherBase {
3428 constructor ( { factory = defaultFactory , maxRedirections = 0 , connect, ...options } = { } ) {
3529 super ( )
3630
@@ -60,8 +54,6 @@ class Agent extends Dispatcher {
6054 this [ kClients ] . delete ( key )
6155 }
6256 } )
63- this [ kClosed ] = false
64- this [ kDestroyed ] = false
6557
6658 const agent = this
6759
@@ -94,76 +86,38 @@ class Agent extends Dispatcher {
9486 return ret
9587 }
9688
97- dispatch ( opts , handler ) {
98- if ( ! handler || typeof handler !== 'object' ) {
99- throw new InvalidArgumentError ( 'handler must be an object.' )
89+ [ kDispatch ] ( opts , handler ) {
90+ let key
91+ if ( opts . origin && ( typeof opts . origin === 'string' || opts . origin instanceof URL ) ) {
92+ key = String ( opts . origin )
93+ } else {
94+ throw new InvalidArgumentError ( 'opts.origin must be a non-empty string or URL.' )
10095 }
10196
102- try {
103- if ( ! opts || typeof opts !== 'object' ) {
104- throw new InvalidArgumentError ( 'opts must be an object.' )
105- }
106-
107- let key
108- if ( opts . origin && ( typeof opts . origin === 'string' || opts . origin instanceof URL ) ) {
109- key = String ( opts . origin )
110- } else {
111- throw new InvalidArgumentError ( 'opts.origin must be a non-empty string or URL.' )
112- }
113-
114- if ( this [ kDestroyed ] ) {
115- throw new ClientDestroyedError ( )
116- }
117-
118- if ( this [ kClosed ] ) {
119- throw new ClientClosedError ( )
120- }
97+ const ref = this [ kClients ] . get ( key )
12198
122- const ref = this [ kClients ] . get ( key )
123-
124- let dispatcher = ref ? ref . deref ( ) : null
125- if ( ! dispatcher ) {
126- dispatcher = this [ kFactory ] ( opts . origin , this [ kOptions ] )
127- . on ( 'drain' , this [ kOnDrain ] )
128- . on ( 'connect' , this [ kOnConnect ] )
129- . on ( 'disconnect' , this [ kOnDisconnect ] )
130- . on ( 'connectionError' , this [ kOnConnectionError ] )
131-
132- this [ kClients ] . set ( key , new WeakRef ( dispatcher ) )
133- this [ kFinalizer ] . register ( dispatcher , key )
134- }
135-
136- const { maxRedirections = this [ kMaxRedirections ] } = opts
137- if ( maxRedirections != null && maxRedirections !== 0 ) {
138- opts = { ...opts , maxRedirections : 0 } // Stop sub dispatcher from also redirecting.
139- handler = new RedirectHandler ( this , maxRedirections , opts , handler )
140- }
141-
142- return dispatcher . dispatch ( opts , handler )
143- } catch ( err ) {
144- if ( typeof handler . onError !== 'function' ) {
145- throw new InvalidArgumentError ( 'invalid onError method' )
146- }
99+ let dispatcher = ref ? ref . deref ( ) : null
100+ if ( ! dispatcher ) {
101+ dispatcher = this [ kFactory ] ( opts . origin , this [ kOptions ] )
102+ . on ( 'drain' , this [ kOnDrain ] )
103+ . on ( 'connect' , this [ kOnConnect ] )
104+ . on ( 'disconnect' , this [ kOnDisconnect ] )
105+ . on ( 'connectionError' , this [ kOnConnectionError ] )
147106
148- handler . onError ( err )
107+ this [ kClients ] . set ( key , new WeakRef ( dispatcher ) )
108+ this [ kFinalizer ] . register ( dispatcher , key )
149109 }
150- }
151-
152- get closed ( ) {
153- return this [ kClosed ]
154- }
155-
156- get destroyed ( ) {
157- return this [ kDestroyed ]
158- }
159110
160- close ( callback ) {
161- if ( callback != null && typeof callback !== 'function' ) {
162- throw new InvalidArgumentError ( 'callback must be a function' )
111+ const { maxRedirections = this [ kMaxRedirections ] } = opts
112+ if ( maxRedirections != null && maxRedirections !== 0 ) {
113+ opts = { ...opts , maxRedirections : 0 } // Stop sub dispatcher from also redirecting.
114+ handler = new RedirectHandler ( this , maxRedirections , opts , handler )
163115 }
164116
165- this [ kClosed ] = true
117+ return dispatcher . dispatch ( opts , handler )
118+ }
166119
120+ async [ kClose ] ( ) {
167121 const closePromises = [ ]
168122 for ( const ref of this [ kClients ] . values ( ) ) {
169123 const client = ref . deref ( )
@@ -173,27 +127,10 @@ class Agent extends Dispatcher {
173127 }
174128 }
175129
176- if ( ! callback ) {
177- return Promise . all ( closePromises )
178- }
179-
180- // Should never error.
181- Promise . all ( closePromises ) . then ( ( ) => process . nextTick ( callback ) )
130+ await Promise . all ( closePromises )
182131 }
183132
184- destroy ( err , callback ) {
185- if ( typeof err === 'function' ) {
186- callback = err
187- err = null
188- }
189-
190- if ( callback != null && typeof callback !== 'function' ) {
191- throw new InvalidArgumentError ( 'callback must be a function' )
192- }
193-
194- this [ kClosed ] = true
195- this [ kDestroyed ] = true
196-
133+ async [ kDestroy ] ( err ) {
197134 const destroyPromises = [ ]
198135 for ( const ref of this [ kClients ] . values ( ) ) {
199136 const client = ref . deref ( )
@@ -203,12 +140,7 @@ class Agent extends Dispatcher {
203140 }
204141 }
205142
206- if ( ! callback ) {
207- return Promise . all ( destroyPromises )
208- }
209-
210- // Should never error.
211- Promise . all ( destroyPromises ) . then ( ( ) => process . nextTick ( callback ) )
143+ await Promise . all ( destroyPromises )
212144 }
213145}
214146
0 commit comments