base.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var {
  2. _nullishCoalesce
  3. } = require('@sentry/utils');
  4. Object.defineProperty(exports, '__esModule', { value: true });
  5. const http = require('http');
  6. require('https');
  7. /**
  8. * This code was originally forked from https://github.com/TooTallNate/proxy-agents/tree/b133295fd16f6475578b6b15bd9b4e33ecb0d0b7
  9. * With the following licence:
  10. *
  11. * (The MIT License)
  12. *
  13. * Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>*
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining
  16. * a copy of this software and associated documentation files (the
  17. * 'Software'), to deal in the Software without restriction, including
  18. * without limitation the rights to use, copy, modify, merge, publish,
  19. * distribute, sublicense, and/or sell copies of the Software, and to
  20. * permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:*
  22. *
  23. * The above copyright notice and this permission notice shall be
  24. * included in all copies or substantial portions of the Software.*
  25. *
  26. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  29. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  30. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  31. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  32. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. const INTERNAL = Symbol('AgentBaseInternalState');
  35. class Agent extends http.Agent {
  36. // Set by `http.Agent` - missing from `@types/node`
  37. constructor(opts) {
  38. super(opts);
  39. this[INTERNAL] = {};
  40. }
  41. /**
  42. * Determine whether this is an `http` or `https` request.
  43. */
  44. isSecureEndpoint(options) {
  45. if (options) {
  46. // First check the `secureEndpoint` property explicitly, since this
  47. // means that a parent `Agent` is "passing through" to this instance.
  48. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
  49. if (typeof (options ).secureEndpoint === 'boolean') {
  50. return options.secureEndpoint;
  51. }
  52. // If no explicit `secure` endpoint, check if `protocol` property is
  53. // set. This will usually be the case since using a full string URL
  54. // or `URL` instance should be the most common usage.
  55. if (typeof options.protocol === 'string') {
  56. return options.protocol === 'https:';
  57. }
  58. }
  59. // Finally, if no `protocol` property was set, then fall back to
  60. // checking the stack trace of the current call stack, and try to
  61. // detect the "https" module.
  62. const { stack } = new Error();
  63. if (typeof stack !== 'string') return false;
  64. return stack.split('\n').some(l => l.indexOf('(https.js:') !== -1 || l.indexOf('node:https:') !== -1);
  65. }
  66. createSocket(req, options, cb) {
  67. const connectOpts = {
  68. ...options,
  69. secureEndpoint: this.isSecureEndpoint(options),
  70. };
  71. Promise.resolve()
  72. .then(() => this.connect(req, connectOpts))
  73. .then(socket => {
  74. if (socket instanceof http.Agent) {
  75. // @ts-expect-error `addRequest()` isn't defined in `@types/node`
  76. return socket.addRequest(req, connectOpts);
  77. }
  78. this[INTERNAL].currentSocket = socket;
  79. // @ts-expect-error `createSocket()` isn't defined in `@types/node`
  80. super.createSocket(req, options, cb);
  81. }, cb);
  82. }
  83. createConnection() {
  84. const socket = this[INTERNAL].currentSocket;
  85. this[INTERNAL].currentSocket = undefined;
  86. if (!socket) {
  87. throw new Error('No socket was returned in the `connect()` function');
  88. }
  89. return socket;
  90. }
  91. get defaultPort() {
  92. return _nullishCoalesce(this[INTERNAL].defaultPort, () => ( (this.protocol === 'https:' ? 443 : 80)));
  93. }
  94. set defaultPort(v) {
  95. if (this[INTERNAL]) {
  96. this[INTERNAL].defaultPort = v;
  97. }
  98. }
  99. get protocol() {
  100. return _nullishCoalesce(this[INTERNAL].protocol, () => ( (this.isSecureEndpoint() ? 'https:' : 'http:')));
  101. }
  102. set protocol(v) {
  103. if (this[INTERNAL]) {
  104. this[INTERNAL].protocol = v;
  105. }
  106. }
  107. }
  108. exports.Agent = Agent;
  109. //# sourceMappingURL=base.js.map