You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

patch-core.js 1.4KB

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