@@ -10,84 +10,73 @@ const methods = require('methods');
1010const request = require ( '../..' ) ;
1111const AgentBase = require ( '../agent-base' ) ;
1212
13- /**
14- * Expose `Agent`.
15- */
16-
17- module . exports = Agent ;
18-
1913/**
2014 * Initialize a new `Agent`.
2115 *
2216 * @api public
2317 */
2418
25- function Agent ( options ) {
26- if ( ! ( this instanceof Agent ) ) {
27- return new Agent ( options ) ;
28- }
19+ class Agent extends AgentBase {
20+ constructor ( options ) {
21+ super ( ) ;
2922
30- AgentBase . call ( this ) ;
31- this . jar = new CookieJar ( ) ;
23+ this . jar = new CookieJar ( ) ;
3224
33- if ( options ) {
34- if ( options . ca ) {
35- this . ca ( options . ca ) ;
36- }
25+ if ( options ) {
26+ if ( options . ca ) {
27+ this . ca ( options . ca ) ;
28+ }
3729
38- if ( options . key ) {
39- this . key ( options . key ) ;
40- }
30+ if ( options . key ) {
31+ this . key ( options . key ) ;
32+ }
4133
42- if ( options . pfx ) {
43- this . pfx ( options . pfx ) ;
44- }
34+ if ( options . pfx ) {
35+ this . pfx ( options . pfx ) ;
36+ }
4537
46- if ( options . cert ) {
47- this . cert ( options . cert ) ;
48- }
38+ if ( options . cert ) {
39+ this . cert ( options . cert ) ;
40+ }
4941
50- if ( options . rejectUnauthorized === false ) {
51- this . disableTLSCerts ( ) ;
42+ if ( options . rejectUnauthorized === false ) {
43+ this . disableTLSCerts ( ) ;
44+ }
5245 }
5346 }
54- }
5547
56- Agent . prototype = Object . create ( AgentBase . prototype ) ;
57-
58- /**
59- * Save the cookies in the given `res` to
60- * the agent's cookie jar for persistence.
61- *
62- * @param {Response } res
63- * @api private
64- */
65-
66- Agent . prototype . _saveCookies = function ( res ) {
67- const cookies = res . headers [ 'set-cookie' ] ;
68- if ( cookies ) {
69- const url = parse ( res . request ?. url || '' )
70- this . jar . setCookies ( cookies , url . hostname , url . pathname ) ;
48+ /**
49+ * Save the cookies in the given `res` to
50+ * the agent's cookie jar for persistence.
51+ *
52+ * @param {Response } res
53+ * @api private
54+ */
55+ _saveCookies ( res ) {
56+ const cookies = res . headers [ 'set-cookie' ] ;
57+ if ( cookies ) {
58+ const url = parse ( res . request ?. url || '' ) ;
59+ this . jar . setCookies ( cookies , url . hostname , url . pathname ) ;
60+ }
7161 }
72- } ;
73-
74- /**
75- * Attach cookies when available to the given `req`.
76- *
77- * @param {Request } req
78- * @api private
79- */
8062
81- Agent . prototype . _attachCookies = function ( request_ ) {
82- const url = parse ( request_ . url ) ;
83- const access = new CookieAccessInfo (
84- url . hostname ,
85- url . pathname ,
86- url . protocol === 'https:'
87- ) ;
88- const cookies = this . jar . getCookies ( access ) . toValueString ( ) ;
89- request_ . cookies = cookies ;
90- } ;
63+ /**
64+ * Attach cookies when available to the given `req`.
65+ *
66+ * @param {Request } req
67+ * @api private
68+ */
69+ _attachCookies ( request_ ) {
70+ const url = parse ( request_ . url ) ;
71+ const access = new CookieAccessInfo (
72+ url . hostname ,
73+ url . pathname ,
74+ url . protocol === 'https:'
75+ ) ;
76+ const cookies = this . jar . getCookies ( access ) . toValueString ( ) ;
77+ request_ . cookies = cookies ;
78+ }
79+ }
9180
9281for ( const name of methods ) {
9382 const method = name . toUpperCase ( ) ;
@@ -109,3 +98,13 @@ for (const name of methods) {
10998}
11099
111100Agent . prototype . del = Agent . prototype . delete ;
101+
102+ // create a Proxy that can instantiate a new Agent without using `new` keyword
103+ // (for backward compatibility and chaining)
104+ const proxyAgent = new Proxy ( Agent , {
105+ apply ( target , thisArg , argumentsList ) {
106+ return new target ( ...argumentsList ) ;
107+ }
108+ } ) ;
109+
110+ module . exports = proxyAgent ;
0 commit comments