base.js 4.1 KB

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