summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js')
-rw-r--r--deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js58
1 files changed, 30 insertions, 28 deletions
diff --git a/deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js b/deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js
index df3ca727a7..b1f42e6317 100644
--- a/deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js
+++ b/deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/index.js
@@ -1,20 +1,15 @@
'use strict';
-
-/**
- * Module dependencies.
- */
-
require('./patch-core');
const inherits = require('util').inherits;
const promisify = require('es6-promisify');
const EventEmitter = require('events').EventEmitter;
-/**
- * Module exports.
- */
-
module.exports = Agent;
+function isAgent(v) {
+ return v && typeof v.addRequest === 'function';
+}
+
/**
* Base `http.Agent` implementation.
* No pooling/keep-alive is implemented by default.
@@ -22,7 +17,6 @@ module.exports = Agent;
* @param {Function} callback
* @api public
*/
-
function Agent(callback, _opts) {
if (!(this instanceof Agent)) {
return new Agent(callback, _opts);
@@ -30,6 +24,10 @@ function Agent(callback, _opts) {
EventEmitter.call(this);
+ // The callback gets promisified if it has 3 parameters
+ // (i.e. it has a callback function) lazily
+ this._promisifiedCallback = false;
+
let opts = _opts;
if ('function' === typeof callback) {
this.callback = callback;
@@ -59,19 +57,15 @@ Agent.prototype.callback = function callback(req, opts) {
*
* @api public
*/
-
-Agent.prototype.addRequest = function addRequest(
- req,
- _opts
-) {
+Agent.prototype.addRequest = function addRequest(req, _opts) {
const ownOpts = Object.assign({}, _opts);
- // set default `host` for HTTP to localhost
+ // Set default `host` for HTTP to localhost
if (null == ownOpts.host) {
ownOpts.host = 'localhost';
}
- // set default `port` for HTTP if none was explicitly specified
+ // Set default `port` for HTTP if none was explicitly specified
if (null == ownOpts.port) {
ownOpts.port = ownOpts.secureEndpoint ? 443 : 80;
}
@@ -79,7 +73,7 @@ Agent.prototype.addRequest = function addRequest(
const opts = Object.assign({}, this.options, ownOpts);
if (opts.host && opts.path) {
- // if both a `host` and `path` are specified then it's most likely the
+ // If both a `host` and `path` are specified then it's most likely the
// result of a `url.parse()` call... we need to remove the `path` portion so
// that `net.connect()` doesn't attempt to open that as a unix socket file.
delete opts.path;
@@ -91,12 +85,12 @@ Agent.prototype.addRequest = function addRequest(
delete opts.defaultPort;
delete opts.createConnection;
- // hint to use "Connection: close"
+ // Hint to use "Connection: close"
// XXX: non-documented `http` module API :(
req._last = true;
req.shouldKeepAlive = false;
- // create the `stream.Duplex` instance
+ // Create the `stream.Duplex` instance
let timeout;
let timedOut = false;
const timeoutMs = this.timeout;
@@ -110,6 +104,7 @@ Agent.prototype.addRequest = function addRequest(
}
function ontimeout() {
+ timeout = null;
timedOut = true;
const err = new Error(
'A "socket" was not created for HTTP request before ' + timeoutMs + 'ms'
@@ -122,6 +117,7 @@ Agent.prototype.addRequest = function addRequest(
if (timedOut) return;
if (timeout != null) {
clearTimeout(timeout);
+ timeout = null;
}
onerror(err);
}
@@ -130,18 +126,26 @@ Agent.prototype.addRequest = function addRequest(
if (timedOut) return;
if (timeout != null) {
clearTimeout(timeout);
+ timeout = null;
}
- if (socket) {
+ if (isAgent(socket)) {
+ // `socket` is actually an http.Agent instance, so relinquish
+ // responsibility for this `req` to the Agent from here on
+ socket.addRequest(req, opts);
+ } else if (socket) {
req.onSocket(socket);
} else {
- const err = new Error(`no Duplex stream was returned to agent-base for \`${req.method} ${req.path}\``);
+ const err = new Error(
+ `no Duplex stream was returned to agent-base for \`${req.method} ${req.path}\``
+ );
onerror(err);
}
}
- if (this.callback.length >= 3) {
- // legacy callback function, convert to Promise
+ if (!this._promisifiedCallback && this.callback.length >= 3) {
+ // Legacy callback function - convert to a Promise
this.callback = promisify(this.callback, this);
+ this._promisifiedCallback = true;
}
if (timeoutMs > 0) {
@@ -149,10 +153,8 @@ Agent.prototype.addRequest = function addRequest(
}
try {
- Promise.resolve(this.callback(req, opts))
- .then(onsocket, callbackError);
+ Promise.resolve(this.callback(req, opts)).then(onsocket, callbackError);
} catch (err) {
- Promise.reject(err)
- .catch(callbackError);
+ Promise.reject(err).catch(callbackError);
}
};