pax_global_header00006660000000000000000000000064137446276610014532gustar00rootroot0000000000000052 comment=c4b8ea2e1a11bae023bb09b708050a50418204e9 node-agent-base-6.0.2/000077500000000000000000000000001374462766100144705ustar00rootroot00000000000000node-agent-base-6.0.2/.editorconfig000066400000000000000000000013131374462766100171430ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 4 tab_width = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{*.json,*.json.example,*.gyp,*.yml,*.yaml,*.workflow}] indent_style = space indent_size = 2 [{*.py,*.asm}] indent_style = space [*.py] indent_size = 4 [*.asm] indent_size = 8 [*.md] trim_trailing_whitespace = false # Ideal settings - some plugins might support these. [*.js] quote_type = single [{*.c,*.cc,*.h,*.hh,*.cpp,*.hpp,*.m,*.mm,*.mpp,*.js,*.java,*.go,*.rs,*.php,*.ng,*.jsx,*.ts,*.d,*.cs,*.swift}] curly_bracket_next_line = false spaces_around_operators = true spaces_around_brackets = outside # close enough to 1TB indent_brace_style = K&R node-agent-base-6.0.2/.eslintrc.js000066400000000000000000000055671374462766100167440ustar00rootroot00000000000000module.exports = { 'extends': [ 'airbnb', 'prettier' ], 'parser': '@typescript-eslint/parser', 'parserOptions': { 'ecmaVersion': 2018, 'sourceType': 'module', 'modules': true }, 'plugins': [ '@typescript-eslint' ], 'settings': { 'import/resolver': { 'typescript': { } } }, 'rules': { 'quotes': [ 2, 'single', { 'allowTemplateLiterals': true } ], 'class-methods-use-this': 0, 'consistent-return': 0, 'func-names': 0, 'global-require': 0, 'guard-for-in': 0, 'import/no-duplicates': 0, 'import/no-dynamic-require': 0, 'import/no-extraneous-dependencies': 0, 'import/prefer-default-export': 0, 'lines-between-class-members': 0, 'no-await-in-loop': 0, 'no-bitwise': 0, 'no-console': 0, 'no-continue': 0, 'no-control-regex': 0, 'no-empty': 0, 'no-loop-func': 0, 'no-nested-ternary': 0, 'no-param-reassign': 0, 'no-plusplus': 0, 'no-restricted-globals': 0, 'no-restricted-syntax': 0, 'no-shadow': 0, 'no-underscore-dangle': 0, 'no-use-before-define': 0, 'prefer-const': 0, 'prefer-destructuring': 0, 'camelcase': 0, 'no-unused-vars': 0, // in favor of '@typescript-eslint/no-unused-vars' // 'indent': 0 // in favor of '@typescript-eslint/indent' '@typescript-eslint/no-unused-vars': 'warn', // '@typescript-eslint/indent': ['error', 2] // this might conflict with a lot ongoing changes '@typescript-eslint/no-array-constructor': 'error', '@typescript-eslint/adjacent-overload-signatures': 'error', '@typescript-eslint/class-name-casing': 'error', '@typescript-eslint/interface-name-prefix': 'error', '@typescript-eslint/no-empty-interface': 'error', '@typescript-eslint/no-inferrable-types': 'error', '@typescript-eslint/no-misused-new': 'error', '@typescript-eslint/no-namespace': 'error', '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-parameter-properties': 'error', '@typescript-eslint/no-triple-slash-reference': 'error', '@typescript-eslint/prefer-namespace-keyword': 'error', '@typescript-eslint/type-annotation-spacing': 'error', // '@typescript-eslint/array-type': 'error', // '@typescript-eslint/ban-types': 'error', // '@typescript-eslint/explicit-function-return-type': 'warn', // '@typescript-eslint/explicit-member-accessibility': 'error', // '@typescript-eslint/member-delimiter-style': 'error', // '@typescript-eslint/no-angle-bracket-type-assertion': 'error', // '@typescript-eslint/no-explicit-any': 'warn', // '@typescript-eslint/no-object-literal-type-assertion': 'error', // '@typescript-eslint/no-use-before-define': 'error', // '@typescript-eslint/no-var-requires': 'error', // '@typescript-eslint/prefer-interface': 'error' } } node-agent-base-6.0.2/.github/000077500000000000000000000000001374462766100160305ustar00rootroot00000000000000node-agent-base-6.0.2/.github/workflows/000077500000000000000000000000001374462766100200655ustar00rootroot00000000000000node-agent-base-6.0.2/.github/workflows/test.yml000066400000000000000000000014271374462766100215730ustar00rootroot00000000000000name: Node CI on: [push] jobs: build: name: Test Node.js ${{ matrix.node-version }} on ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] node-version: [6.x, 8.x, 10.x, 12.x, 14.x, 15.x] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Print Node.js Version run: node --version - name: Install Dependencies run: npm install env: CI: true - name: Run "build" step run: npm run build --if-present env: CI: true - name: Run tests run: npm test env: CI: true node-agent-base-6.0.2/.gitignore000066400000000000000000000000701374462766100164550ustar00rootroot00000000000000/package-lock.json /yarn.lock /node_modules /?.?s /dist node-agent-base-6.0.2/README.md000066400000000000000000000117001374462766100157460ustar00rootroot00000000000000agent-base ========== ### Turn a function into an [`http.Agent`][http.Agent] instance [![Build Status](https://github.com/TooTallNate/node-agent-base/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/node-agent-base/actions?workflow=Node+CI) This module provides an `http.Agent` generator. That is, you pass it an async callback function, and it returns a new `http.Agent` instance that will invoke the given callback function when sending outbound HTTP requests. #### Some subclasses: Here's some more interesting uses of `agent-base`. Send a pull request to list yours! * [`http-proxy-agent`][http-proxy-agent]: An HTTP(s) proxy `http.Agent` implementation for HTTP endpoints * [`https-proxy-agent`][https-proxy-agent]: An HTTP(s) proxy `http.Agent` implementation for HTTPS endpoints * [`pac-proxy-agent`][pac-proxy-agent]: A PAC file proxy `http.Agent` implementation for HTTP and HTTPS * [`socks-proxy-agent`][socks-proxy-agent]: A SOCKS proxy `http.Agent` implementation for HTTP and HTTPS Installation ------------ Install with `npm`: ``` bash $ npm install agent-base ``` Example ------- Here's a minimal example that creates a new `net.Socket` connection to the server for every HTTP request (i.e. the equivalent of `agent: false` option): ```js var net = require('net'); var tls = require('tls'); var url = require('url'); var http = require('http'); var agent = require('agent-base'); var endpoint = 'http://nodejs.org/api/'; var parsed = url.parse(endpoint); // This is the important part! parsed.agent = agent(function (req, opts) { var socket; // `secureEndpoint` is true when using the https module if (opts.secureEndpoint) { socket = tls.connect(opts); } else { socket = net.connect(opts); } return socket; }); // Everything else works just like normal... http.get(parsed, function (res) { console.log('"response" event!', res.headers); res.pipe(process.stdout); }); ``` Returning a Promise or using an `async` function is also supported: ```js agent(async function (req, opts) { await sleep(1000); // etc… }); ``` Return another `http.Agent` instance to "pass through" the responsibility for that HTTP request to that agent: ```js agent(function (req, opts) { return opts.secureEndpoint ? https.globalAgent : http.globalAgent; }); ``` API --- ## Agent(Function callback[, Object options]) → [http.Agent][] Creates a base `http.Agent` that will execute the callback function `callback` for every HTTP request that it is used as the `agent` for. The callback function is responsible for creating a `stream.Duplex` instance of some kind that will be used as the underlying socket in the HTTP request. The `options` object accepts the following properties: * `timeout` - Number - Timeout for the `callback()` function in milliseconds. Defaults to Infinity (optional). The callback function should have the following signature: ### callback(http.ClientRequest req, Object options, Function cb) → undefined The ClientRequest `req` can be accessed to read request headers and and the path, etc. The `options` object contains the options passed to the `http.request()`/`https.request()` function call, and is formatted to be directly passed to `net.connect()`/`tls.connect()`, or however else you want a Socket to be created. Pass the created socket to the callback function `cb` once created, and the HTTP request will continue to proceed. If the `https` module is used to invoke the HTTP request, then the `secureEndpoint` property on `options` _will be set to `true`_. License ------- (The MIT License) Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [http-proxy-agent]: https://github.com/TooTallNate/node-http-proxy-agent [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent [pac-proxy-agent]: https://github.com/TooTallNate/node-pac-proxy-agent [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent [http.Agent]: https://nodejs.org/api/http.html#http_class_http_agent node-agent-base-6.0.2/package.json000066400000000000000000000031431374462766100167570ustar00rootroot00000000000000{ "name": "agent-base", "version": "6.0.2", "description": "Turn a function into an `http.Agent` instance", "main": "dist/src/index", "typings": "dist/src/index", "files": [ "dist/src", "src" ], "scripts": { "prebuild": "rimraf dist", "build": "tsc", "postbuild": "cpy --parents src test '!**/*.ts' dist", "test": "mocha --reporter spec dist/test/*.js", "test-lint": "eslint src --ext .js,.ts", "prepublishOnly": "npm run build" }, "repository": { "type": "git", "url": "git://github.com/TooTallNate/node-agent-base.git" }, "keywords": [ "http", "agent", "base", "barebones", "https" ], "author": "Nathan Rajlich (http://n8.io/)", "license": "MIT", "bugs": { "url": "https://github.com/TooTallNate/node-agent-base/issues" }, "dependencies": { "debug": "4" }, "devDependencies": { "@types/debug": "4", "@types/mocha": "^5.2.7", "@types/node": "^14.0.20", "@types/semver": "^7.1.0", "@types/ws": "^6.0.3", "@typescript-eslint/eslint-plugin": "1.6.0", "@typescript-eslint/parser": "1.1.0", "async-listen": "^1.2.0", "cpy-cli": "^2.0.0", "eslint": "5.16.0", "eslint-config-airbnb": "17.1.0", "eslint-config-prettier": "4.1.0", "eslint-import-resolver-typescript": "1.1.1", "eslint-plugin-import": "2.16.0", "eslint-plugin-jsx-a11y": "6.2.1", "eslint-plugin-react": "7.12.4", "mocha": "^6.2.0", "rimraf": "^3.0.0", "semver": "^7.1.2", "typescript": "^3.5.3", "ws": "^3.0.0" }, "engines": { "node": ">= 6.0.0" } } node-agent-base-6.0.2/src/000077500000000000000000000000001374462766100152575ustar00rootroot00000000000000node-agent-base-6.0.2/src/index.ts000066400000000000000000000214721374462766100167440ustar00rootroot00000000000000import net from 'net'; import http from 'http'; import https from 'https'; import { Duplex } from 'stream'; import { EventEmitter } from 'events'; import createDebug from 'debug'; import promisify from './promisify'; const debug = createDebug('agent-base'); function isAgent(v: any): v is createAgent.AgentLike { return Boolean(v) && typeof v.addRequest === 'function'; } function isSecureEndpoint(): boolean { 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); } function createAgent(opts?: createAgent.AgentOptions): createAgent.Agent; function createAgent( callback: createAgent.AgentCallback, opts?: createAgent.AgentOptions ): createAgent.Agent; function createAgent( callback?: createAgent.AgentCallback | createAgent.AgentOptions, opts?: createAgent.AgentOptions ) { return new createAgent.Agent(callback, opts); } namespace createAgent { export interface ClientRequest extends http.ClientRequest { _last?: boolean; _hadError?: boolean; method: string; } export interface AgentRequestOptions { host?: string; path?: string; // `port` on `http.RequestOptions` can be a string or undefined, // but `net.TcpNetConnectOpts` expects only a number port: number; } export interface HttpRequestOptions extends AgentRequestOptions, Omit { secureEndpoint: false; } export interface HttpsRequestOptions extends AgentRequestOptions, Omit { secureEndpoint: true; } export type RequestOptions = HttpRequestOptions | HttpsRequestOptions; export type AgentLike = Pick | http.Agent; export type AgentCallbackReturn = Duplex | AgentLike; export type AgentCallbackCallback = ( err?: Error | null, socket?: createAgent.AgentCallbackReturn ) => void; export type AgentCallbackPromise = ( req: createAgent.ClientRequest, opts: createAgent.RequestOptions ) => | createAgent.AgentCallbackReturn | Promise; export type AgentCallback = typeof Agent.prototype.callback; export type AgentOptions = { timeout?: number; }; /** * Base `http.Agent` implementation. * No pooling/keep-alive is implemented by default. * * @param {Function} callback * @api public */ export class Agent extends EventEmitter { public timeout: number | null; public maxFreeSockets: number; public maxTotalSockets: number; public maxSockets: number; public sockets: { [key: string]: net.Socket[]; }; public freeSockets: { [key: string]: net.Socket[]; }; public requests: { [key: string]: http.IncomingMessage[]; }; public options: https.AgentOptions; private promisifiedCallback?: createAgent.AgentCallbackPromise; private explicitDefaultPort?: number; private explicitProtocol?: string; constructor( callback?: createAgent.AgentCallback | createAgent.AgentOptions, _opts?: createAgent.AgentOptions ) { super(); let opts = _opts; if (typeof callback === 'function') { this.callback = callback; } else if (callback) { opts = callback; } // Timeout for the socket to be returned from the callback this.timeout = null; if (opts && typeof opts.timeout === 'number') { this.timeout = opts.timeout; } // These aren't actually used by `agent-base`, but are required // for the TypeScript definition files in `@types/node` :/ this.maxFreeSockets = 1; this.maxSockets = 1; this.maxTotalSockets = Infinity; this.sockets = {}; this.freeSockets = {}; this.requests = {}; this.options = {}; } get defaultPort(): number { if (typeof this.explicitDefaultPort === 'number') { return this.explicitDefaultPort; } return isSecureEndpoint() ? 443 : 80; } set defaultPort(v: number) { this.explicitDefaultPort = v; } get protocol(): string { if (typeof this.explicitProtocol === 'string') { return this.explicitProtocol; } return isSecureEndpoint() ? 'https:' : 'http:'; } set protocol(v: string) { this.explicitProtocol = v; } callback( req: createAgent.ClientRequest, opts: createAgent.RequestOptions, fn: createAgent.AgentCallbackCallback ): void; callback( req: createAgent.ClientRequest, opts: createAgent.RequestOptions ): | createAgent.AgentCallbackReturn | Promise; callback( req: createAgent.ClientRequest, opts: createAgent.AgentOptions, fn?: createAgent.AgentCallbackCallback ): | createAgent.AgentCallbackReturn | Promise | void { throw new Error( '"agent-base" has no default implementation, you must subclass and override `callback()`' ); } /** * Called by node-core's "_http_client.js" module when creating * a new HTTP request with this Agent instance. * * @api public */ addRequest(req: ClientRequest, _opts: RequestOptions): void { const opts: RequestOptions = { ..._opts }; if (typeof opts.secureEndpoint !== 'boolean') { opts.secureEndpoint = isSecureEndpoint(); } if (opts.host == null) { opts.host = 'localhost'; } if (opts.port == null) { opts.port = opts.secureEndpoint ? 443 : 80; } if (opts.protocol == null) { opts.protocol = opts.secureEndpoint ? 'https:' : 'http:'; } if (opts.host && opts.path) { // If both a `host` and `path` are specified then it's most // likely the result of a `url.parse()` call... we need to // remove the `path` portion so that `net.connect()` doesn't // attempt to open that as a unix socket file. delete opts.path; } delete opts.agent; delete opts.hostname; delete opts._defaultAgent; delete opts.defaultPort; delete opts.createConnection; // Hint to use "Connection: close" // XXX: non-documented `http` module API :( req._last = true; req.shouldKeepAlive = false; let timedOut = false; let timeoutId: ReturnType | null = null; const timeoutMs = opts.timeout || this.timeout; const onerror = (err: NodeJS.ErrnoException) => { if (req._hadError) return; req.emit('error', err); // For Safety. Some additional errors might fire later on // and we need to make sure we don't double-fire the error event. req._hadError = true; }; const ontimeout = () => { timeoutId = null; timedOut = true; const err: NodeJS.ErrnoException = new Error( `A "socket" was not created for HTTP request before ${timeoutMs}ms` ); err.code = 'ETIMEOUT'; onerror(err); }; const callbackError = (err: NodeJS.ErrnoException) => { if (timedOut) return; if (timeoutId !== null) { clearTimeout(timeoutId); timeoutId = null; } onerror(err); }; const onsocket = (socket: AgentCallbackReturn) => { if (timedOut) return; if (timeoutId != null) { clearTimeout(timeoutId); timeoutId = null; } if (isAgent(socket)) { // `socket` is actually an `http.Agent` instance, so // relinquish responsibility for this `req` to the Agent // from here on debug( 'Callback returned another Agent instance %o', socket.constructor.name ); (socket as createAgent.Agent).addRequest(req, opts); return; } if (socket) { socket.once('free', () => { this.freeSocket(socket as net.Socket, opts); }); req.onSocket(socket as net.Socket); return; } const err = new Error( `no Duplex stream was returned to agent-base for \`${req.method} ${req.path}\`` ); onerror(err); }; if (typeof this.callback !== 'function') { onerror(new Error('`callback` is not defined')); return; } if (!this.promisifiedCallback) { if (this.callback.length >= 3) { debug('Converting legacy callback function to promise'); this.promisifiedCallback = promisify(this.callback); } else { this.promisifiedCallback = this.callback; } } if (typeof timeoutMs === 'number' && timeoutMs > 0) { timeoutId = setTimeout(ontimeout, timeoutMs); } if ('port' in opts && typeof opts.port !== 'number') { opts.port = Number(opts.port); } try { debug( 'Resolving socket for %o request: %o', opts.protocol, `${req.method} ${req.path}` ); Promise.resolve(this.promisifiedCallback(req, opts)).then( onsocket, callbackError ); } catch (err) { Promise.reject(err).catch(callbackError); } } freeSocket(socket: net.Socket, opts: AgentOptions) { debug('Freeing socket %o %o', socket.constructor.name, opts); socket.destroy(); } destroy() { debug('Destroying agent %o', this.constructor.name); } } // So that `instanceof` works correctly createAgent.prototype = createAgent.Agent.prototype; } export = createAgent; node-agent-base-6.0.2/src/promisify.ts000066400000000000000000000012111374462766100176430ustar00rootroot00000000000000import { Agent, ClientRequest, RequestOptions, AgentCallbackCallback, AgentCallbackPromise, AgentCallbackReturn } from './index'; type LegacyCallback = ( req: ClientRequest, opts: RequestOptions, fn: AgentCallbackCallback ) => void; export default function promisify(fn: LegacyCallback): AgentCallbackPromise { return function(this: Agent, req: ClientRequest, opts: RequestOptions) { return new Promise((resolve, reject) => { fn.call( this, req, opts, (err: Error | null | undefined, rtn?: AgentCallbackReturn) => { if (err) { reject(err); } else { resolve(rtn); } } ); }); }; } node-agent-base-6.0.2/test/000077500000000000000000000000001374462766100154475ustar00rootroot00000000000000node-agent-base-6.0.2/test/ssl-cert-snakeoil.key000066400000000000000000000015671374462766100215310ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQCzURxIqzer0ACAbX/lHdsn4Gd9PLKrf7EeDYfIdV0HZKPD8WDr bBx2/fBu0OW2sjnzv/SVZbJ0DAuPE/p0+eT0qb2qC10iz9iTD7ribd7gxhirVb8y b3fBjXsxc8V8p4Ny1LcvNSqCjwUbJqdRogfoJeTiqPM58z5sNzuv5iq7iwIDAQAB AoGAPMQy4olrP0UotlzlJ36bowLP70ffgHCwU+/f4NWs5fF78c3du0oSx1w820Dd Z7E0JF8bgnlJJTxjumPZz0RUCugrEHBKJmzEz3cxF5E3+7NvteZcjKn9D67RrM5x 1/uSZ9cqKE9cYvY4fSuHx18diyZ4axR/wB1Pea2utjjDM+ECQQDb9ZbmmaWMiRpQ 5Up+loxP7BZNPsEVsm+DVJmEFbaFgGfncWBqSIqnPNjMwTwj0OigTwCAEGPkfRVW T0pbYWCxAkEA0LK7SCTwzyDmhASUalk0x+3uCAA6ryFdwJf/wd8TRAvVOmkTEldX uJ7ldLvfrONYO3v56uKTU/SoNdZYzKtO+wJAX2KM4ctXYy5BXztPpr2acz4qHa1N Bh+vBAC34fOYhyQ76r3b1btHhWZ5jbFuZwm9F2erC94Ps5IaoqcX07DSwQJAPKGw h2U0EPkd/3zVIZCJJQya+vgWFIs9EZcXVtvYXQyTBkVApTN66MhBIYjzkub5205J bVQmOV37AKklY1DhwQJAA1wos0cYxro02edzatxd0DIR2r4qqOqLkw6BhYHhq6HJ ZvIcQkHqdSXzdETFc01I1znDGGIrJHcnvKWgBPoEUg== -----END RSA PRIVATE KEY----- node-agent-base-6.0.2/test/ssl-cert-snakeoil.pem000066400000000000000000000012701374462766100215110ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIB1TCCAT4CCQDV5mPlzm9+izANBgkqhkiG9w0BAQUFADAvMS0wKwYDVQQDEyQ3 NTI3YmQ3Ny1hYjNlLTQ3NGItYWNlNy1lZWQ2MDUzOTMxZTcwHhcNMTUwNzA2MjI0 NTA3WhcNMjUwNzAzMjI0NTA3WjAvMS0wKwYDVQQDEyQ3NTI3YmQ3Ny1hYjNlLTQ3 NGItYWNlNy1lZWQ2MDUzOTMxZTcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB ALNRHEirN6vQAIBtf+Ud2yfgZ308sqt/sR4Nh8h1XQdko8PxYOtsHHb98G7Q5bay OfO/9JVlsnQMC48T+nT55PSpvaoLXSLP2JMPuuJt3uDGGKtVvzJvd8GNezFzxXyn g3LUty81KoKPBRsmp1GiB+gl5OKo8znzPmw3O6/mKruLAgMBAAEwDQYJKoZIhvcN AQEFBQADgYEACzoHUF8UV2Z6541Q2wKEA0UFUzmUjf/E1XwBO+1P15ZZ64uw34B4 1RwMPtAo9RY/PmICTWtNxWGxkzwb2JtDWtnxVER/lF8k2XcXPE76fxTHJF/BKk9J QU8OTD1dd9gHCBviQB9TqntRZ5X7axjtuWjb2umY+owBYzAHZkp1HKI= -----END CERTIFICATE----- node-agent-base-6.0.2/test/test-legacy.js000066400000000000000000000422561374462766100202370ustar00rootroot00000000000000/** * Module dependencies. */ var fs = require('fs'); var url = require('url'); var net = require('net'); var tls = require('tls'); var http = require('http'); var https = require('https'); var WebSocket = require('ws'); var assert = require('assert'); var events = require('events'); var inherits = require('util').inherits; var Agent = require('../src'); var PassthroughAgent = Agent(function(req, opts) { return opts.secureEndpoint ? https.globalAgent : http.globalAgent; }); describe('Agent (JavaScript)', function() { describe('subclass', function() { it('should be subclassable', function(done) { function MyAgent() { Agent.call(this); } inherits(MyAgent, Agent); MyAgent.prototype.callback = function(req, opts, fn) { assert.equal(req.path, '/foo'); assert.equal(req.getHeader('host'), '127.0.0.1:1234'); assert.equal(opts.secureEndpoint, true); done(); }; var info = url.parse('https://127.0.0.1:1234/foo'); info.agent = new MyAgent(); assert(info.agent instanceof Agent); assert(info.agent instanceof MyAgent); https.get(info); }); }); describe('options', function() { it('should support an options Object as first argument', function() { var agent = new Agent({ timeout: 1000 }); assert.equal(1000, agent.timeout); }); it('should support an options Object as second argument', function() { var agent = new Agent(function() {}, { timeout: 1000 }); assert.equal(1000, agent.timeout); }); }); describe('`this` context', function() { it('should be the Agent instance', function(done) { var called = false; var agent = new Agent(); agent.callback = function() { called = true; assert.equal(this, agent); }; var info = url.parse('http://127.0.0.1/foo'); info.agent = agent; var req = http.get(info); req.on('error', function(err) { assert(/no Duplex stream was returned/.test(err.message)); done(); }); }); it('should be the Agent instance with callback signature', function(done) { var called = false; var agent = new Agent(); agent.callback = function(req, opts, fn) { called = true; assert.equal(this, agent); fn(); }; var info = url.parse('http://127.0.0.1/foo'); info.agent = agent; var req = http.get(info); req.on('error', function(err) { assert(/no Duplex stream was returned/.test(err.message)); done(); }); }); }); describe('"error" event', function() { it('should be invoked on `http.ClientRequest` instance if `callback()` has not been defined', function(done) { var agent = new Agent(); var info = url.parse('http://127.0.0.1/foo'); info.agent = agent; var req = http.get(info); req.on('error', function(err) { assert.equal( '"agent-base" has no default implementation, you must subclass and override `callback()`', err.message ); done(); }); }); it('should be invoked on `http.ClientRequest` instance if Error passed to callback function on the first tick', function(done) { var agent = new Agent(function(req, opts, fn) { fn(new Error('is this caught?')); }); var info = url.parse('http://127.0.0.1/foo'); info.agent = agent; var req = http.get(info); req.on('error', function(err) { assert.equal('is this caught?', err.message); done(); }); }); it('should be invoked on `http.ClientRequest` instance if Error passed to callback function after the first tick', function(done) { var agent = new Agent(function(req, opts, fn) { setTimeout(function() { fn(new Error('is this caught?')); }, 10); }); var info = url.parse('http://127.0.0.1/foo'); info.agent = agent; var req = http.get(info); req.on('error', function(err) { assert.equal('is this caught?', err.message); done(); }); }); }); describe('artificial "streams"', function() { it('should send a GET request', function(done) { var stream = new events.EventEmitter(); // needed for the `http` module to call .write() on the stream stream.writable = true; stream.write = function(str) { assert(0 == str.indexOf('GET / HTTP/1.1')); done(); }; // needed for `http` module in Node.js 4 stream.cork = function() {}; stream.uncork = function() {}; var opts = { method: 'GET', host: '127.0.0.1', path: '/', port: 80, agent: new Agent(function(req, opts, fn) { fn(null, stream); }) }; var req = http.request(opts); req.end(); }); it('should receive a GET response', function(done) { var stream = new events.EventEmitter(); var opts = { method: 'GET', host: '127.0.0.1', path: '/', port: 80, agent: new Agent(function(req, opts, fn) { fn(null, stream); }) }; var req = http.request(opts, function(res) { assert.equal('1.0', res.httpVersion); assert.equal(200, res.statusCode); assert.equal('bar', res.headers.foo); assert.deepEqual(['1', '2'], res.headers['set-cookie']); done(); }); // have to wait for the "socket" event since `http.ClientRequest` // doesn't *actually* attach the listeners to the "stream" until // this happens req.once('socket', function() { var buf = Buffer.from( 'HTTP/1.0 200\r\n' + 'Foo: bar\r\n' + 'Set-Cookie: 1\r\n' + 'Set-Cookie: 2\r\n\r\n' ); stream.emit('data', buf); }); req.end(); }); }); describe('"http" module', function() { var server; var port; // setup test HTTP server before(function(done) { server = http.createServer(); server.listen(0, function() { port = server.address().port; done(); }); }); beforeEach(function() { server.removeAllListeners('request'); }); // shut down test HTTP server after(function(done) { server.once('close', function() { done(); }); server.close(); }); it('should work for basic HTTP requests', function(done) { var called = false; var agent = new Agent(function(req, opts, fn) { called = true; var socket = net.connect(opts); fn(null, socket); }); // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url); res.end(); }); var info = url.parse('http://127.0.0.1:' + port + '/foo'); info.agent = agent; http.get(info, function(res) { assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(called); done(); }); }); it('should support direct return in `connect()`', function(done) { var called = false; var agent = new Agent(function(req, opts) { called = true; return net.connect(opts); }); // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url); res.end(); }); var info = url.parse('http://127.0.0.1:' + port + '/foo'); info.agent = agent; http.get(info, function(res) { assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(called); done(); }); }); it('should support returning a Promise in `connect()`', function(done) { var called = false; var agent = new Agent(function(req, opts) { return new Promise(function(resolve, reject) { called = true; resolve(net.connect(opts)); }); }); // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url); res.end(); }); var info = url.parse('http://127.0.0.1:' + port + '/foo'); info.agent = agent; http.get(info, function(res) { assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(called); done(); }); }); it('should set the `Connection: close` response header', function(done) { var called = false; var agent = new Agent(function(req, opts, fn) { called = true; var socket = net.connect(opts); fn(null, socket); }); // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Url', req.url); assert.equal('close', req.headers.connection); res.end(); }); var info = url.parse('http://127.0.0.1:' + port + '/bar'); info.agent = agent; http.get(info, function(res) { assert.equal('/bar', res.headers['x-url']); assert.equal('close', res.headers.connection); assert(gotReq); assert(called); done(); }); }); it('should pass through options from `http.request()`', function(done) { var agent = new Agent(function(req, opts, fn) { assert.equal('google.com', opts.host); assert.equal('bar', opts.foo); done(); }); http.get({ host: 'google.com', foo: 'bar', agent: agent }); }); it('should default to port 80', function(done) { var agent = new Agent(function(req, opts, fn) { assert.equal(80, opts.port); done(); }); // (probably) not hitting a real HTTP server here, // so no need to add a httpServer request listener http.get({ host: '127.0.0.1', path: '/foo', agent: agent }); }); it('should support the "timeout" option', function(done) { // ensure we timeout after the "error" event had a chance to trigger this.timeout(1000); this.slow(800); var agent = new Agent( function(req, opts, fn) { // this function will time out }, { timeout: 100 } ); var opts = url.parse('http://nodejs.org'); opts.agent = agent; var req = http.get(opts); req.once('error', function(err) { assert.equal('ETIMEOUT', err.code); req.abort(); done(); }); }); it('should free sockets after use', function(done) { var agent = new Agent(function(req, opts, fn) { var socket = net.connect(opts); fn(null, socket); }); // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.end(); }); var info = url.parse('http://127.0.0.1:' + port + '/foo'); info.agent = agent; http.get(info, function(res) { res.socket.emit('free'); assert.equal(true, res.socket.destroyed); assert(gotReq); done(); }); }); describe('PassthroughAgent', function() { it('should pass through to `http.globalAgent`', function(done) { // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url); res.end(); }); var info = url.parse('http://127.0.0.1:' + port + '/foo'); info.agent = PassthroughAgent; http.get(info, function(res) { assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); done(); }); }); }); }); describe('"https" module', function() { var server; var port; // setup test HTTPS server before(function(done) { var options = { key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') }; server = https.createServer(options); server.listen(0, function() { port = server.address().port; done(); }); }); beforeEach(function() { server.removeAllListeners('request'); }); // shut down test HTTP server after(function(done) { server.once('close', function() { done(); }); server.close(); }); it('should not modify the passed in Options object', function(done) { var called = false; var agent = new Agent(function(req, opts, fn) { called = true; assert.equal(true, opts.secureEndpoint); assert.equal(443, opts.port); assert.equal('localhost', opts.host); }); var opts = { agent: agent }; var req = https.request(opts); assert.equal(true, called); assert.equal(false, 'secureEndpoint' in opts); assert.equal(false, 'port' in opts); done(); }); it('should work with a String URL', function(done) { var endpoint = 'https://127.0.0.1:' + port; var req = https.get(endpoint); // it's gonna error out since `rejectUnauthorized` is not being passed in req.on('error', function(err) { assert.equal(err.code, 'DEPTH_ZERO_SELF_SIGNED_CERT'); done(); }); }); it('should work for basic HTTPS requests', function(done) { var called = false; var agent = new Agent(function(req, opts, fn) { called = true; assert(opts.secureEndpoint); var socket = tls.connect(opts); fn(null, socket); }); // add HTTPS server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url); res.end(); }); var info = url.parse('https://127.0.0.1:' + port + '/foo'); info.agent = agent; info.rejectUnauthorized = false; https.get(info, function(res) { assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(called); done(); }); }); it('should pass through options from `https.request()`', function(done) { var agent = new Agent(function(req, opts, fn) { assert.equal('google.com', opts.host); assert.equal('bar', opts.foo); done(); }); https.get({ host: 'google.com', foo: 'bar', agent: agent }); }); it('should default to port 443', function(done) { var agent = new Agent(function(req, opts, fn) { assert.equal(true, opts.secureEndpoint); assert.equal(false, opts.rejectUnauthorized); assert.equal(443, opts.port); done(); }); // (probably) not hitting a real HTTPS server here, // so no need to add a httpsServer request listener https.get({ host: '127.0.0.1', path: '/foo', agent: agent, rejectUnauthorized: false }); }); describe('PassthroughAgent', function() { it('should pass through to `https.globalAgent`', function(done) { // add HTTP server "request" listener var gotReq = false; server.once('request', function(req, res) { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url); res.end(); }); var info = url.parse('https://127.0.0.1:' + port + '/foo'); info.agent = PassthroughAgent; info.rejectUnauthorized = false; https.get(info, function(res) { assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); done(); }); }); }); }); describe('"ws" server', function() { var wss; var server; var port; // setup test HTTP server before(function(done) { server = http.createServer(); wss = new WebSocket.Server({ server: server }); server.listen(0, function() { port = server.address().port; done(); }); }); beforeEach(function() { server.removeAllListeners('request'); wss.removeAllListeners('connection'); }); // shut down test HTTP server after(function(done) { server.once('close', function() { done(); }); server.close(); }); it('should work for basic WebSocket connections', function(done) { function onconnection(ws) { ws.on('message', function(data) { assert.equal('ping', data); ws.send('pong'); }); } wss.on('connection', onconnection); var agent = new Agent(function(req, opts, fn) { var socket = net.connect(opts); fn(null, socket); }); var client = new WebSocket('ws://127.0.0.1:' + port + '/', { agent: agent }); client.on('open', function() { client.send('ping'); }); client.on('message', function(data) { assert.equal('pong', data); client.close(); done(); }); }); }); describe('"wss" server', function() { var wss; var server; var port; // setup test HTTP server before(function(done) { var options = { key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') }; server = https.createServer(options); wss = new WebSocket.Server({ server: server }); server.listen(0, function() { port = server.address().port; done(); }); }); beforeEach(function() { server.removeAllListeners('request'); wss.removeAllListeners('connection'); }); // shut down test HTTP server after(function(done) { server.once('close', function() { done(); }); server.close(); }); it('should work for secure WebSocket connections', function(done) { function onconnection(ws) { ws.on('message', function(data) { assert.equal('ping', data); ws.send('pong'); }); } wss.on('connection', onconnection); var agent = new Agent(function(req, opts, fn) { var socket = tls.connect(opts); fn(null, socket); }); var client = new WebSocket('wss://127.0.0.1:' + port + '/', { agent: agent, rejectUnauthorized: false }); client.on('open', function() { client.send('ping'); }); client.on('message', function(data) { assert.equal('pong', data); client.close(); wss.removeListener('connection', onconnection); done(); }); }); }); }); node-agent-base-6.0.2/test/test.ts000066400000000000000000000304151374462766100170010ustar00rootroot00000000000000import fs from 'fs'; import net from 'net'; import tls from 'tls'; import url from 'url'; import http from 'http'; import https from 'https'; import assert from 'assert'; import listen from 'async-listen'; import { satisfies } from 'semver'; import { Agent, RequestOptions } from '../src'; // In Node 12+ you can just override `http.globalAgent`, but for older Node // versions we have to patch the internal `_http_agent` module instead // (see: https://github.com/nodejs/node/pull/25170). // @ts-ignore import httpAgent from '_http_agent'; const sleep = (n: number) => new Promise(r => setTimeout(r, n)); const req = (opts: https.RequestOptions): Promise => { return new Promise((resolve, reject) => { (opts.protocol === 'https:' ? https : http) .request(opts, resolve) .once('error', reject) .end(); }); }; const sslOptions = { key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') }; function json(res: http.IncomingMessage): Promise { return new Promise((resolve, reject) => { let data: string = ''; res.setEncoding('utf8'); res.on('data', b => { data += b; }); res.on('end', () => resolve(JSON.parse(data))); }); } describe('Agent (TypeScript)', () => { describe('subclass', () => { it('should be extendable (direct return)', () => { class MyAgent extends Agent { callback( req: http.ClientRequest, opts: RequestOptions ): http.Agent { return http.globalAgent; } } const agent = new MyAgent(); assert(agent instanceof Agent); assert(agent instanceof MyAgent); }); it('should be extendable (promise return)', () => { class MyAgent extends Agent { async callback( req: http.ClientRequest, opts: RequestOptions ): Promise { return Promise.resolve(http.globalAgent); } } const agent = new MyAgent(); assert(agent instanceof Agent); assert(agent instanceof MyAgent); }); }); describe('"http" module', () => { it('should work for basic HTTP requests', async () => { let gotReq = false; let gotCallback = false; const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { gotCallback = true; assert.equal(opts.secureEndpoint, false); assert.equal(opts.protocol, 'http:'); return net.connect(opts); } ); const server = http.createServer((req, res) => { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url || '/'); res.end(); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; try { const info = url.parse(`http://127.0.0.1:${port}/foo`); const res = await req({ agent, ...info }); assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(gotCallback); } finally { server.close(); } }); it('should not send a port number for the default port', async () => { const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { assert.equal(opts.secureEndpoint, false); assert.equal(opts.protocol, 'http:'); assert.equal(agent.defaultPort, port); assert.equal(opts.port, port); return net.connect(opts); } ); const server = http.createServer((req, res) => { res.end(JSON.stringify(req.headers)); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; agent.defaultPort = port; try { const info = url.parse(`http://127.0.0.1:${port}/foo`); const res = await req({ agent, ...info }); const body = await json(res); assert.equal(body.host, '127.0.0.1'); } finally { server.close(); } }); it('should work when overriding `http.globalAgent`', async () => { let gotReq = false; let gotCallback = false; const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { gotCallback = true; assert.equal(opts.secureEndpoint, false); assert.equal(opts.protocol, 'http:'); return net.connect(opts); } ); const server = http.createServer((req, res) => { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url || '/'); res.end(); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; // Override the default `http.Agent.globalAgent` const originalAgent = httpAgent.globalAgent; httpAgent.globalAgent = agent; try { const info = url.parse(`http://127.0.0.1:${port}/foo`); const res = await req(info); assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(gotCallback); } finally { server.close(); httpAgent.globalAgent = agent; } }); it('should work after the first tick of the `http.ClientRequest` instance', async () => { let gotReq = false; let gotCallback = false; const agent = new Agent( async ( req: http.ClientRequest, opts: RequestOptions ): Promise => { gotCallback = true; assert.equal(opts.secureEndpoint, false); assert.equal(opts.protocol, 'http:'); await sleep(10); return net.connect(opts); } ); const server = http.createServer((req, res) => { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url || '/'); res.end(); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; try { const info = url.parse(`http://127.0.0.1:${port}/foo`); const res = await req({ agent, ...info }); assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(gotCallback); } finally { server.close(); } }); it('should emit an "error" event on `http.ClientRequest` instance when callback throws sync', async () => { let gotError = false; let gotCallback = false; const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { gotCallback = true; throw new Error('bad'); } ); try { const info = url.parse('http://127.0.0.1/throws'); await req({ agent, ...info }); } catch (err) { gotError = true; assert.equal(err.message, 'bad'); } assert(gotError); assert(gotCallback); }); it('should emit an "error" event on `http.ClientRequest` instance when callback throws async', async () => { let gotError = false; let gotCallback = false; const agent = new Agent( async ( req: http.ClientRequest, opts: RequestOptions ): Promise => { gotCallback = true; await sleep(10); throw new Error('bad'); } ); try { const info = url.parse('http://127.0.0.1/throws'); await req({ agent, ...info }); } catch (err) { gotError = true; assert.equal(err.message, 'bad'); } assert(gotError); assert(gotCallback); }); }); describe('"https" module', () => { it('should work for basic HTTPS requests', async () => { let gotReq = false; let gotCallback = false; const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { gotCallback = true; assert.equal(opts.secureEndpoint, true); assert.equal(opts.protocol, 'https:'); return tls.connect(opts); } ); const server = https.createServer(sslOptions, (req, res) => { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url || '/'); res.end(); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; try { const info = url.parse(`https://127.0.0.1:${port}/foo`); const res = await req({ agent, rejectUnauthorized: false, ...info }); assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(gotCallback); } finally { server.close(); } }); it('should work when returning another `agent-base`', async () => { let gotReq = false; let gotCallback1 = false; let gotCallback2 = false; const agent1 = new Agent( async ( req: http.ClientRequest, opts: RequestOptions ): Promise => { gotCallback1 = true; assert.equal(opts.secureEndpoint, true); assert.equal(opts.protocol, 'https:'); return agent2; } ); const agent2 = new Agent( async ( req: http.ClientRequest, opts: RequestOptions ): Promise => { gotCallback2 = true; assert.equal(opts.secureEndpoint, true); assert.equal(opts.protocol, 'https:'); return tls.connect(opts); } ); const server = https.createServer(sslOptions, (req, res) => { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url || '/'); res.end(); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; try { const info = url.parse(`https://127.0.0.1:${port}/foo`); const res = await req({ agent: agent1, rejectUnauthorized: false, ...info }); assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(gotCallback1); assert(gotCallback2); } finally { server.close(); } }); it('should not send a port number for the default port', async () => { let reqCount = 0; const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { assert.equal(opts.secureEndpoint, true); assert.equal(opts.protocol, 'https:'); assert.equal(agent.defaultPort, port); assert.equal(opts.port, port); return tls.connect(opts); } ); const server = https.createServer(sslOptions, (req, res) => { reqCount++; res.end(JSON.stringify(req.headers)); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; agent.defaultPort = port; try { const info = url.parse(`https://127.0.0.1:${port}/foo`); const res = await req({ agent, rejectUnauthorized: false, ...info }); const body = await json(res); assert.equal(body.host, '127.0.0.1'); assert.equal(reqCount, 1); } finally { server.close(); } }); if (satisfies(process.version, '>= 10')) { it('should work when overriding `https.globalAgent`', async () => { let gotReq = false; let gotCallback = false; const agent = new Agent( (req: http.ClientRequest, opts: RequestOptions): net.Socket => { gotCallback = true; assert.equal(opts.secureEndpoint, true); assert.equal(opts.protocol, 'https:'); return tls.connect(opts); } ); const server = https.createServer(sslOptions, (req, res) => { gotReq = true; res.setHeader('X-Foo', 'bar'); res.setHeader('X-Url', req.url || '/'); res.end(); }); await listen(server); const addr = server.address(); if (!addr || typeof addr === 'string') { throw new Error('Server did not bind to a port'); } const { port } = addr; // Override the default `https.globalAgent` const originalAgent = https.globalAgent; https.globalAgent = agent; try { const info: https.RequestOptions = url.parse(`https://127.0.0.1:${port}/foo`); info.rejectUnauthorized = false; const res = await req(info); assert.equal('bar', res.headers['x-foo']); assert.equal('/foo', res.headers['x-url']); assert(gotReq); assert(gotCallback); } finally { server.close(); https.globalAgent = originalAgent; } }); } }); }); node-agent-base-6.0.2/tsconfig.json000066400000000000000000000004451374462766100172020ustar00rootroot00000000000000{ "compilerOptions": { "strict": true, "module": "CommonJS", "target": "es2015", "esModuleInterop": true, "lib": ["esnext"], "outDir": "dist", "sourceMap": true, "declaration": true }, "include": ["src/**/*", "test/**/*"], "exclude": ["node_modules"] }