Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: classify agent #1777

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;