1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3    if (k2 === undefined) k2 = k;
4    var desc = Object.getOwnPropertyDescriptor(m, k);
5    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6      desc = { enumerable: true, get: function() { return m[k]; } };
7    }
8    Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10    if (k2 === undefined) k2 = k;
11    o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14    Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16    o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19    if (mod && mod.__esModule) return mod;
20    var result = {};
21    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22    __setModuleDefault(result, mod);
23    return result;
24};
25var __exportStar = (this && this.__exportStar) || function(m, exports) {
26    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.Agent = void 0;
30const http = __importStar(require("http"));
31__exportStar(require("./helpers"), exports);
32const INTERNAL = Symbol('AgentBaseInternalState');
33class Agent extends http.Agent {
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
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')
61            return false;
62        return stack
63            .split('\n')
64            .some((l) => l.indexOf('(https.js:') !== -1 ||
65            l.indexOf('node:https:') !== -1);
66    }
67    createSocket(req, options, cb) {
68        const connectOpts = {
69            ...options,
70            secureEndpoint: this.isSecureEndpoint(options),
71        };
72        Promise.resolve()
73            .then(() => this.connect(req, connectOpts))
74            .then((socket) => {
75            if (socket instanceof http.Agent) {
76                // @ts-expect-error `addRequest()` isn't defined in `@types/node`
77                return socket.addRequest(req, connectOpts);
78            }
79            this[INTERNAL].currentSocket = socket;
80            // @ts-expect-error `createSocket()` isn't defined in `@types/node`
81            super.createSocket(req, options, cb);
82        }, cb);
83    }
84    createConnection() {
85        const socket = this[INTERNAL].currentSocket;
86        this[INTERNAL].currentSocket = undefined;
87        if (!socket) {
88            throw new Error('No socket was returned in the `connect()` function');
89        }
90        return socket;
91    }
92    get defaultPort() {
93        return (this[INTERNAL].defaultPort ??
94            (this.protocol === 'https:' ? 443 : 80));
95    }
96    set defaultPort(v) {
97        if (this[INTERNAL]) {
98            this[INTERNAL].defaultPort = v;
99        }
100    }
101    get protocol() {
102        return (this[INTERNAL].protocol ??
103            (this.isSecureEndpoint() ? 'https:' : 'http:'));
104    }
105    set protocol(v) {
106        if (this[INTERNAL]) {
107            this[INTERNAL].protocol = v;
108        }
109    }
110}
111exports.Agent = Agent;
112//# sourceMappingURL=index.js.map