• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const url = require('url');
3const https = require('https');
4
5/**
6 * This currently needs to be applied to all Node.js versions
7 * in order to determine if the `req` is an HTTP or HTTPS request.
8 *
9 * There is currently no PR attempting to move this property upstream.
10 */
11const patchMarker = "__agent_base_https_request_patched__";
12if (!https.request[patchMarker]) {
13  https.request = (function(request) {
14    return function(_options, cb) {
15      let options;
16      if (typeof _options === 'string') {
17        options = url.parse(_options);
18      } else {
19        options = Object.assign({}, _options);
20      }
21      if (null == options.port) {
22        options.port = 443;
23      }
24      options.secureEndpoint = true;
25      return request.call(https, options, cb);
26    };
27  })(https.request);
28  https.request[patchMarker] = true;
29}
30
31/**
32 * This is needed for Node.js >= 9.0.0 to make sure `https.get()` uses the
33 * patched `https.request()`.
34 *
35 * Ref: https://github.com/nodejs/node/commit/5118f31
36 */
37https.get = function (_url, _options, cb) {
38    let options;
39    if (typeof _url === 'string' && _options && typeof _options !== 'function') {
40      options = Object.assign({}, url.parse(_url), _options);
41    } else if (!_options && !cb) {
42      options = _url;
43    } else if (!cb) {
44      options = _url;
45      cb = _options;
46    }
47
48  const req = https.request(options, cb);
49  req.end();
50  return req;
51};
52