summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/http-proxy-agent/node_modules/agent-base/patch-core.js
blob: 7cdacafa3ef1284856c7dbc411c2e9e021a2275b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var url = require('url');
var http = require('http');
var https = require('https');
var semver = require('semver');
var inherits = require('util').inherits;


// we only need to patch the `http.request()` and
// `http.ClientRequest` on older versions of Node.js
if (semver.lt(process.version, '0.11.8')) {
  // subclass the native ClientRequest to include the
  // passed in `options` object.
  http.ClientRequest = (function (_ClientRequest) {
    function ClientRequest (options, cb) {
      this._options = options;
      _ClientRequest.call(this, options, cb);
    }
    inherits(ClientRequest, _ClientRequest);

    return ClientRequest;
  })(http.ClientRequest);


  // need to re-define the `request()` method, since on node v0.8/v0.10
  // the closure-local ClientRequest is used, rather than the monkey
  // patched version we have created here.
  http.request = (function (request) {
    return function (options, cb) {
      if (typeof options === 'string') {
        options = url.parse(options);
      }
      if (options.protocol && options.protocol !== 'http:') {
        throw new Error('Protocol:' + options.protocol + ' not supported.');
      }
      return new http.ClientRequest(options, cb);
    };
  })(http.request);
}


// this currently needs to be applied to all Node.js versions
// (v0.8.x, v0.10.x, v0.12.x), in order to determine if the `req`
// is an HTTP or HTTPS request. There is currently no PR attempting
// to move this property upstream.
https.request = (function (request) {
  return function (options, cb) {
    if (typeof options === 'string') {
      options = url.parse(options);
    }
    if (null == options.port) options.port = 443;
    options.secureEndpoint = true;
    return request.call(https, options, cb);
  };
})(https.request);