diff --git a/src/agent-base.js b/src/agent-base.js index e20b0dfb..af94779e 100644 --- a/src/agent-base.js +++ b/src/agent-base.js @@ -1,8 +1,4 @@ -function Agent() { - this._defaults = []; -} - -for (const fn of [ +const defaults = [ 'use', 'on', 'once', @@ -25,7 +21,21 @@ 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 }); @@ -33,10 +43,5 @@ for (const fn of [ }; } -Agent.prototype._setDefaults = function (request) { - for (const def of this._defaults) { - request[def.fn](...def.args); - } -}; module.exports = Agent; diff --git a/src/node/agent.js b/src/node/agent.js index 0761dc68..8b556765 100644 --- a/src/node/agent.js +++ b/src/node/agent.js @@ -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(); @@ -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;