Skip to content

Commit

Permalink
classify agent
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Aug 18, 2023
1 parent cfb7b5e commit fca95a3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 72 deletions.
27 changes: 16 additions & 11 deletions src/agent-base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
function Agent() {
this._defaults = [];
}

for (const fn of [
const defaults = [
'use',
'on',
'once',
Expand All @@ -25,18 +21,27 @@ for (const fn of [
'pfx',
'cert',
'disableTLSCerts'
]) {
]

class Agent {
constructor () {
this._defaults = [];
}

_setDefaults (request) {
for (const def of this._defaults) {
request[def.fn](...def.args);
}
}
}

for (const fn of defaults) {
// Default setting for all requests from this agent
Agent.prototype[fn] = function (...args) {
this._defaults.push({ fn, args });
return this;
};
}

Agent.prototype._setDefaults = function (request) {
for (const def of this._defaults) {
request[def.fn](...def.args);
}
};

module.exports = Agent;
121 changes: 60 additions & 61 deletions src/node/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,73 @@ const methods = require('methods');
const request = require('../..');
const AgentBase = require('../agent-base');

/**
* Expose `Agent`.
*/

module.exports = Agent;

/**
* Initialize a new `Agent`.
*
* @api public
*/

function Agent(options) {
if (!(this instanceof Agent)) {
return new Agent(options);
}
class Agent extends AgentBase {
constructor (options) {
super();

AgentBase.call(this);
this.jar = new CookieJar();
this.jar = new CookieJar();

if (options) {
if (options.ca) {
this.ca(options.ca);
}
if (options) {
if (options.ca) {
this.ca(options.ca);
}

if (options.key) {
this.key(options.key);
}
if (options.key) {
this.key(options.key);
}

if (options.pfx) {
this.pfx(options.pfx);
}
if (options.pfx) {
this.pfx(options.pfx);
}

if (options.cert) {
this.cert(options.cert);
}
if (options.cert) {
this.cert(options.cert);
}

if (options.rejectUnauthorized === false) {
this.disableTLSCerts();
if (options.rejectUnauthorized === false) {
this.disableTLSCerts();
}
}
}
}

Agent.prototype = Object.create(AgentBase.prototype);

/**
* Save the cookies in the given `res` to
* the agent's cookie jar for persistence.
*
* @param {Response} res
* @api private
*/

Agent.prototype._saveCookies = function (res) {
const cookies = res.headers['set-cookie'];
if (cookies) {
const url = parse(res.request?.url || '')
this.jar.setCookies(cookies, url.hostname, url.pathname);
/**
* Save the cookies in the given `res` to
* the agent's cookie jar for persistence.
*
* @param {Response} res
* @api private
*/
_saveCookies (res) {
const cookies = res.headers['set-cookie'];
if (cookies) {
const url = parse(res.request?.url || '');
this.jar.setCookies(cookies, url.hostname, url.pathname);
}
}
};

/**
* Attach cookies when available to the given `req`.
*
* @param {Request} req
* @api private
*/

Agent.prototype._attachCookies = function (request_) {
const url = parse(request_.url);
const access = new CookieAccessInfo(
url.hostname,
url.pathname,
url.protocol === 'https:'
);
const cookies = this.jar.getCookies(access).toValueString();
request_.cookies = cookies;
};
/**
* Attach cookies when available to the given `req`.
*
* @param {Request} req
* @api private
*/
_attachCookies (request_) {
const url = parse(request_.url);
const access = new CookieAccessInfo(
url.hostname,
url.pathname,
url.protocol === 'https:'
);
const cookies = this.jar.getCookies(access).toValueString();
request_.cookies = cookies;
}
}

for (const name of methods) {
const method = name.toUpperCase();
Expand All @@ -109,3 +98,13 @@ for (const name of methods) {
}

Agent.prototype.del = Agent.prototype.delete;

// create a Proxy that can instantiate a new Agent without using `new` keyword
// (for backward compatibility and chaining)
const proxyAgent = new Proxy(Agent, {
apply (target, thisArg, argumentsList) {
return new target(...argumentsList);
}
});

module.exports = proxyAgent;

0 comments on commit fca95a3

Please sign in to comment.