123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import { _nullishCoalesce } from '@sentry/utils';
- import * as http from 'http';
- import 'https';
- const INTERNAL = Symbol('AgentBaseInternalState');
- class Agent extends http.Agent {
-
- constructor(opts) {
- super(opts);
- this[INTERNAL] = {};
- }
-
- isSecureEndpoint(options) {
- if (options) {
-
-
-
- if (typeof (options ).secureEndpoint === 'boolean') {
- return options.secureEndpoint;
- }
-
-
-
- if (typeof options.protocol === 'string') {
- return options.protocol === 'https:';
- }
- }
-
-
-
- const { stack } = new Error();
- if (typeof stack !== 'string') return false;
- return stack.split('\n').some(l => l.indexOf('(https.js:') !== -1 || l.indexOf('node:https:') !== -1);
- }
- createSocket(req, options, cb) {
- const connectOpts = {
- ...options,
- secureEndpoint: this.isSecureEndpoint(options),
- };
- Promise.resolve()
- .then(() => this.connect(req, connectOpts))
- .then(socket => {
- if (socket instanceof http.Agent) {
-
- return socket.addRequest(req, connectOpts);
- }
- this[INTERNAL].currentSocket = socket;
-
- super.createSocket(req, options, cb);
- }, cb);
- }
- createConnection() {
- const socket = this[INTERNAL].currentSocket;
- this[INTERNAL].currentSocket = undefined;
- if (!socket) {
- throw new Error('No socket was returned in the `connect()` function');
- }
- return socket;
- }
- get defaultPort() {
- return _nullishCoalesce(this[INTERNAL].defaultPort, () => ( (this.protocol === 'https:' ? 443 : 80)));
- }
- set defaultPort(v) {
- if (this[INTERNAL]) {
- this[INTERNAL].defaultPort = v;
- }
- }
- get protocol() {
- return _nullishCoalesce(this[INTERNAL].protocol, () => ( (this.isSecureEndpoint() ? 'https:' : 'http:')));
- }
- set protocol(v) {
- if (this[INTERNAL]) {
- this[INTERNAL].protocol = v;
- }
- }
- }
- export { Agent };
|