@@ -10,84 +10,73 @@ const methods = require('methods');
10
10
const request = require ( '../..' ) ;
11
11
const AgentBase = require ( '../agent-base' ) ;
12
12
13
- /**
14
- * Expose `Agent`.
15
- */
16
-
17
- module . exports = Agent ;
18
-
19
13
/**
20
14
* Initialize a new `Agent`.
21
15
*
22
16
* @api public
23
17
*/
24
18
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 ( ) ;
29
22
30
- AgentBase . call ( this ) ;
31
- this . jar = new CookieJar ( ) ;
23
+ this . jar = new CookieJar ( ) ;
32
24
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
+ }
37
29
38
- if ( options . key ) {
39
- this . key ( options . key ) ;
40
- }
30
+ if ( options . key ) {
31
+ this . key ( options . key ) ;
32
+ }
41
33
42
- if ( options . pfx ) {
43
- this . pfx ( options . pfx ) ;
44
- }
34
+ if ( options . pfx ) {
35
+ this . pfx ( options . pfx ) ;
36
+ }
45
37
46
- if ( options . cert ) {
47
- this . cert ( options . cert ) ;
48
- }
38
+ if ( options . cert ) {
39
+ this . cert ( options . cert ) ;
40
+ }
49
41
50
- if ( options . rejectUnauthorized === false ) {
51
- this . disableTLSCerts ( ) ;
42
+ if ( options . rejectUnauthorized === false ) {
43
+ this . disableTLSCerts ( ) ;
44
+ }
52
45
}
53
46
}
54
- }
55
47
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
+ }
71
61
}
72
- } ;
73
-
74
- /**
75
- * Attach cookies when available to the given `req`.
76
- *
77
- * @param {Request } req
78
- * @api private
79
- */
80
62
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
+ }
91
80
92
81
for ( const name of methods ) {
93
82
const method = name . toUpperCase ( ) ;
@@ -109,3 +98,13 @@ for (const name of methods) {
109
98
}
110
99
111
100
Agent . 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