Skip to content

Commit 83e92cb

Browse files
authoredAug 19, 2023
Merge pull request #1777 from jimmywarting/classify
classify agent
2 parents cfb7b5e + fca95a3 commit 83e92cb

File tree

2 files changed

+76
-72
lines changed

2 files changed

+76
-72
lines changed
 

‎src/agent-base.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
function Agent() {
2-
this._defaults = [];
3-
}
4-
5-
for (const fn of [
1+
const defaults = [
62
'use',
73
'on',
84
'once',
@@ -25,18 +21,27 @@ for (const fn of [
2521
'pfx',
2622
'cert',
2723
'disableTLSCerts'
28-
]) {
24+
]
25+
26+
class Agent {
27+
constructor () {
28+
this._defaults = [];
29+
}
30+
31+
_setDefaults (request) {
32+
for (const def of this._defaults) {
33+
request[def.fn](...def.args);
34+
}
35+
}
36+
}
37+
38+
for (const fn of defaults) {
2939
// Default setting for all requests from this agent
3040
Agent.prototype[fn] = function (...args) {
3141
this._defaults.push({ fn, args });
3242
return this;
3343
};
3444
}
3545

36-
Agent.prototype._setDefaults = function (request) {
37-
for (const def of this._defaults) {
38-
request[def.fn](...def.args);
39-
}
40-
};
4146

4247
module.exports = Agent;

‎src/node/agent.js

+60-61
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,73 @@ const methods = require('methods');
1010
const request = require('../..');
1111
const 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

9281
for (const name of methods) {
9382
const method = name.toUpperCase();
@@ -109,3 +98,13 @@ for (const name of methods) {
10998
}
11099

111100
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

Comments
 (0)
Please sign in to comment.