diff options
author | claudiahdz <cghr1990@gmail.com> | 2020-03-31 23:09:27 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:12:58 +0300 |
commit | e97662e25f92840f7e8a683f8e2cda2f5874d945 (patch) | |
tree | 121568f55a85593e09e6e0e98b7c3b9bc09b086f /node_modules/agent-base | |
parent | 29be1c9d1845d36c6eb2ef9f2c0ec2132066bb0f (diff) |
chore: rm libcipm
Diffstat (limited to 'node_modules/agent-base')
-rw-r--r-- | node_modules/agent-base/.travis.yml | 24 | ||||
-rw-r--r-- | node_modules/agent-base/History.md | 113 | ||||
-rw-r--r-- | node_modules/agent-base/README.md | 145 | ||||
-rw-r--r-- | node_modules/agent-base/index.d.ts | 43 | ||||
-rw-r--r-- | node_modules/agent-base/index.js | 170 | ||||
-rw-r--r-- | node_modules/agent-base/package.json | 68 | ||||
-rw-r--r-- | node_modules/agent-base/patch-core.js | 51 | ||||
-rw-r--r-- | node_modules/agent-base/test/ssl-cert-snakeoil.key | 15 | ||||
-rw-r--r-- | node_modules/agent-base/test/ssl-cert-snakeoil.pem | 12 | ||||
-rw-r--r-- | node_modules/agent-base/test/test.js | 728 |
10 files changed, 0 insertions, 1369 deletions
diff --git a/node_modules/agent-base/.travis.yml b/node_modules/agent-base/.travis.yml deleted file mode 100644 index 76200951f..000000000 --- a/node_modules/agent-base/.travis.yml +++ /dev/null @@ -1,24 +0,0 @@ -sudo: false - -language: node_js - -node_js: - - "4" - - "5" - - "6" - - "7" - - "8" - - "9" - - "10" - -install: - - PATH="`npm bin`:`npm bin -g`:$PATH" - # Install dependencies and build - - npm install - -script: - # Output useful info for debugging - - node --version - - npm --version - # Run tests - - npm test diff --git a/node_modules/agent-base/History.md b/node_modules/agent-base/History.md deleted file mode 100644 index 80c88dc40..000000000 --- a/node_modules/agent-base/History.md +++ /dev/null @@ -1,113 +0,0 @@ - -4.2.0 / 2018-01-15 -================== - - * Add support for returning an `http.Agent` instance - * Optimize promisifying logic - * Set `timeout` to null for proper cleanup - * Remove Node.js <= 0.11.3 special-casing from test case - -4.1.2 / 2017-11-20 -================== - - * test Node 9 on Travis - * ensure that `https.get()` uses the patched `https.request()` - -4.1.1 / 2017-07-20 -================== - - * Correct `https.request()` with a String (#9) - -4.1.0 / 2017-06-26 -================== - - * mix in Agent options into Request options - * throw when nothing is returned from agent-base callback - * do not modify the options object for https requests - -4.0.1 / 2017-06-13 -================== - - * add `this` context tests and fixes - -4.0.0 / 2017-06-06 -================== - - * drop support for Node.js < 4 - * drop old versions of Node.js from Travis-CI - * specify Node.js >= 4.0.0 in `engines.node` - * remove more old code - * remove "extend" dependency - * remove "semver" dependency - * make the Promise logic a bit cleaner - * add async function pseudo-example to README - * use direct return in README example - -3.0.0 / 2017-06-02 -================== - - * drop support for Node.js v0.8 and v0.10 - * add support for async, Promises, and direct return - * add a couple `options` test cases - * implement a `"timeout"` option - * rename main file to `index.js` - * test Node 8 on Travis - -2.1.1 / 2017-05-30 -================== - - * Revert [`fe2162e`](https://github.com/TooTallNate/node-agent-base/commit/fe2162e0ba18123f5b301cba4de1e9dd74e437cd) and [`270bdc9`](https://github.com/TooTallNate/node-agent-base/commit/270bdc92eb8e3bd0444d1e5266e8e9390aeb3095) (fixes #7) - -2.1.0 / 2017-05-26 -================== - - * unref is not supported for node < 0.9.1 (@pi0) - * add tests to dangling socket (@pi0) - * check unref() is supported (@pi0) - * fix dangling sockets problem (@pi0) - * add basic "ws" module tests - * make `Agent` be subclassable - * turn `addRequest()` into a named function - * test: Node.js v4 likes to call `cork` on the stream (#3, @tomhughes) - * travis: test node v4, v5, v6 and v7 - -2.0.1 / 2015-09-10 -================== - - * package: update "semver" to v5.0.1 for WebPack (#1, @vhpoet) - -2.0.0 / 2015-07-10 -================== - - * refactor to patch Node.js core for more consistent `opts` values - * ensure that HTTP(s) default port numbers are always given - * test: use ssl-cert-snakeoil SSL certs - * test: add tests for arbitrary options - * README: add API section - * README: make the Agent HTTP/HTTPS generic in the example - * README: use SVG for Travis-CI badge - -1.0.2 / 2015-06-27 -================== - - * agent: set `req._hadError` to true after emitting "error" - * package: update "mocha" to v2 - * test: add artificial HTTP GET request test - * test: add artificial data events test - * test: fix artifical GET response test on node > v0.11.3 - * test: use a real timeout for the async error test - -1.0.1 / 2013-09-09 -================== - - * Fix passing an "error" object to the callback function on the first tick - -1.0.0 / 2013-09-09 -================== - - * New API: now you pass a callback function directly - -0.0.1 / 2013-07-09 -================== - - * Initial release diff --git a/node_modules/agent-base/README.md b/node_modules/agent-base/README.md deleted file mode 100644 index dbeceab8a..000000000 --- a/node_modules/agent-base/README.md +++ /dev/null @@ -1,145 +0,0 @@ -agent-base -========== -### Turn a function into an [`http.Agent`][http.Agent] instance -[![Build Status](https://travis-ci.org/TooTallNate/node-agent-base.svg?branch=master)](https://travis-ci.org/TooTallNate/node-agent-base) - -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 (v4a) 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 diff --git a/node_modules/agent-base/index.d.ts b/node_modules/agent-base/index.d.ts deleted file mode 100644 index ff6788bdc..000000000 --- a/node_modules/agent-base/index.d.ts +++ /dev/null @@ -1,43 +0,0 @@ -// Type definitions for agent-base 4.2.1 -// Project: https://github.com/TooTallNate/node-agent-base -// Definitions by: Christopher Quadflieg <https://github.com/Shinigami92> - -/// <reference types="node" /> -import { EventEmitter } from 'events'; - -declare namespace Agent { - export type AgentCallback = ( - req?: any, - opts?: { - secureEndpoint: boolean; - } - ) => void; - - export interface AgentOptions { - timeout?: number; - host?: string; - port?: number; - [key: string]: any; - } - - export interface Agent extends EventEmitter { - _promisifiedCallback: boolean; - timeout: number | null; - options?: AgentOptions; - callback: AgentCallback; - addRequest: (req?: any, opts?: any) => void; - freeSocket: (socket: any, opts: any) => void; - } -} - -/** - * Base `http.Agent` implementation. - * No pooling/keep-alive is implemented by default. - */ -declare function Agent(opts?: Agent.AgentOptions): Agent.Agent; -declare function Agent( - callback: Agent.AgentCallback, - opts?: Agent.AgentOptions -): Agent.Agent; - -export = Agent; diff --git a/node_modules/agent-base/index.js b/node_modules/agent-base/index.js deleted file mode 100644 index 0ee6b2969..000000000 --- a/node_modules/agent-base/index.js +++ /dev/null @@ -1,170 +0,0 @@ -'use strict'; -require('./patch-core'); -const inherits = require('util').inherits; -const promisify = require('es6-promisify'); -const EventEmitter = require('events').EventEmitter; - -module.exports = Agent; - -function isAgent(v) { - return v && typeof v.addRequest === 'function'; -} - -/** - * Base `http.Agent` implementation. - * No pooling/keep-alive is implemented by default. - * - * @param {Function} callback - * @api public - */ -function Agent(callback, _opts) { - if (!(this instanceof Agent)) { - return new Agent(callback, _opts); - } - - EventEmitter.call(this); - - // The callback gets promisified if it has 3 parameters - // (i.e. it has a callback function) lazily - this._promisifiedCallback = false; - - let opts = _opts; - if ('function' === typeof callback) { - this.callback = callback; - } else if (callback) { - opts = callback; - } - - // timeout for the socket to be returned from the callback - this.timeout = (opts && opts.timeout) || null; - - this.options = opts; -} -inherits(Agent, EventEmitter); - -/** - * Override this function in your subclass! - */ -Agent.prototype.callback = function callback(req, opts) { - 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 - */ -Agent.prototype.addRequest = function addRequest(req, _opts) { - const ownOpts = Object.assign({}, _opts); - - // Set default `host` for HTTP to localhost - if (null == ownOpts.host) { - ownOpts.host = 'localhost'; - } - - // Set default `port` for HTTP if none was explicitly specified - if (null == ownOpts.port) { - ownOpts.port = ownOpts.secureEndpoint ? 443 : 80; - } - - const opts = Object.assign({}, this.options, ownOpts); - - 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; - - // Create the `stream.Duplex` instance - let timeout; - let timedOut = false; - const timeoutMs = this.timeout; - const freeSocket = this.freeSocket; - - function onerror(err) { - 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; - } - - function ontimeout() { - timeout = null; - timedOut = true; - const err = new Error( - 'A "socket" was not created for HTTP request before ' + timeoutMs + 'ms' - ); - err.code = 'ETIMEOUT'; - onerror(err); - } - - function callbackError(err) { - if (timedOut) return; - if (timeout != null) { - clearTimeout(timeout); - timeout = null; - } - onerror(err); - } - - function onsocket(socket) { - if (timedOut) return; - if (timeout != null) { - clearTimeout(timeout); - timeout = null; - } - if (isAgent(socket)) { - // `socket` is actually an http.Agent instance, so relinquish - // responsibility for this `req` to the Agent from here on - socket.addRequest(req, opts); - } else if (socket) { - function onfree() { - freeSocket(socket, opts); - } - socket.on('free', onfree); - req.onSocket(socket); - } else { - const err = new Error( - 'no Duplex stream was returned to agent-base for `' + req.method + ' ' + req.path + '`' - ); - onerror(err); - } - } - - if (!this._promisifiedCallback && this.callback.length >= 3) { - // Legacy callback function - convert to a Promise - this.callback = promisify(this.callback, this); - this._promisifiedCallback = true; - } - - if (timeoutMs > 0) { - timeout = setTimeout(ontimeout, timeoutMs); - } - - try { - Promise.resolve(this.callback(req, opts)).then(onsocket, callbackError); - } catch (err) { - Promise.reject(err).catch(callbackError); - } -}; - -Agent.prototype.freeSocket = function freeSocket(socket, opts) { - // TODO reuse sockets - socket.destroy(); -}; diff --git a/node_modules/agent-base/package.json b/node_modules/agent-base/package.json deleted file mode 100644 index 70da68723..000000000 --- a/node_modules/agent-base/package.json +++ /dev/null @@ -1,68 +0,0 @@ -{ - "_from": "agent-base@4", - "_id": "agent-base@4.3.0", - "_inBundle": false, - "_integrity": "sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==", - "_location": "/agent-base", - "_phantomChildren": {}, - "_requested": { - "type": "range", - "registry": true, - "raw": "agent-base@4", - "name": "agent-base", - "escapedName": "agent-base", - "rawSpec": "4", - "saveSpec": null, - "fetchSpec": "4" - }, - "_requiredBy": [ - "/http-proxy-agent", - "/https-proxy-agent" - ], - "_resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.3.0.tgz", - "_shasum": "8165f01c436009bccad0b1d122f05ed770efc6ee", - "_spec": "agent-base@4", - "_where": "/Users/isaacs/dev/npm/cli/node_modules/http-proxy-agent", - "author": { - "name": "Nathan Rajlich", - "email": "nathan@tootallnate.net", - "url": "http://n8.io/" - }, - "bugs": { - "url": "https://github.com/TooTallNate/node-agent-base/issues" - }, - "bundleDependencies": false, - "dependencies": { - "es6-promisify": "^5.0.0" - }, - "deprecated": false, - "description": "Turn a function into an `http.Agent` instance", - "devDependencies": { - "@types/es6-promisify": "^5.0.0", - "@types/node": "^10.5.3", - "mocha": "^3.4.2", - "ws": "^3.0.0" - }, - "engines": { - "node": ">= 4.0.0" - }, - "homepage": "https://github.com/TooTallNate/node-agent-base#readme", - "keywords": [ - "http", - "agent", - "base", - "barebones", - "https" - ], - "license": "MIT", - "main": "./index.js", - "name": "agent-base", - "repository": { - "type": "git", - "url": "git://github.com/TooTallNate/node-agent-base.git" - }, - "scripts": { - "test": "mocha --reporter spec" - }, - "version": "4.3.0" -} diff --git a/node_modules/agent-base/patch-core.js b/node_modules/agent-base/patch-core.js deleted file mode 100644 index 21cbbb675..000000000 --- a/node_modules/agent-base/patch-core.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; -const url = require('url'); -const https = require('https'); - -/** - * This currently needs to be applied to all Node.js versions - * in order to determine if the `req` is an HTTP or HTTPS request. - * - * There is currently no PR attempting to move this property upstream. - */ -const patchMarker = "__agent_base_https_request_patched__"; -if (!https.request[patchMarker]) { - https.request = (function(request) { - return function(_options, cb) { - let options; - if (typeof _options === 'string') { - options = url.parse(_options); - } else { - options = Object.assign({}, _options); - } - if (null == options.port) { - options.port = 443; - } - options.secureEndpoint = true; - return request.call(https, options, cb); - }; - })(https.request); - https.request[patchMarker] = true; -} - -/** - * This is needed for Node.js >= 9.0.0 to make sure `https.get()` uses the - * patched `https.request()`. - * - * Ref: https://github.com/nodejs/node/commit/5118f31 - */ -https.get = function (_url, _options, cb) { - let options; - if (typeof _url === 'string' && _options && typeof _options !== 'function') { - options = Object.assign({}, url.parse(_url), _options); - } else if (!_options && !cb) { - options = _url; - } else if (!cb) { - options = _url; - cb = _options; - } - - const req = https.request(options, cb); - req.end(); - return req; -}; diff --git a/node_modules/agent-base/test/ssl-cert-snakeoil.key b/node_modules/agent-base/test/ssl-cert-snakeoil.key deleted file mode 100644 index fd1250122..000000000 --- a/node_modules/agent-base/test/ssl-cert-snakeoil.key +++ /dev/null @@ -1,15 +0,0 @@ ------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----- diff --git a/node_modules/agent-base/test/ssl-cert-snakeoil.pem b/node_modules/agent-base/test/ssl-cert-snakeoil.pem deleted file mode 100644 index b115a5e91..000000000 --- a/node_modules/agent-base/test/ssl-cert-snakeoil.pem +++ /dev/null @@ -1,12 +0,0 @@ ------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----- diff --git a/node_modules/agent-base/test/test.js b/node_modules/agent-base/test/test.js deleted file mode 100644 index 0f372c076..000000000 --- a/node_modules/agent-base/test/test.js +++ /dev/null @@ -1,728 +0,0 @@ -/** - * 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('../'); - -var PassthroughAgent = Agent(function(req, opts) { - return opts.secureEndpoint ? https.globalAgent : http.globalAgent; -}); - -describe('Agent', 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(); - 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); - }); - it('should be mixed in with HTTP request options', function(done) { - var agent = new Agent({ - host: 'my-proxy.com', - port: 3128, - foo: 'bar' - }); - agent.callback = function(req, opts, fn) { - assert.equal('bar', opts.foo); - assert.equal('a', opts.b); - - // `host` and `port` are special-cases, and should always be - // overwritten in the request `opts` inside the agent-base callback - assert.equal('localhost', opts.host); - assert.equal(80, opts.port); - done(); - }; - var opts = { - b: 'a', - agent: agent - }; - http.get(opts); - }); - }); - 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() {}; - - 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(); - }); - }); - - // 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(); - }); - }); - - // 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 support the 3-argument `https.get()`', function(done) { - var agent = new Agent(function(req, opts, fn) { - assert.equal('google.com', opts.host); - assert.equal('/q', opts.pathname || opts.path); - assert.equal('881', opts.port); - assert.equal('bar', opts.foo); - done(); - }); - - https.get( - 'https://google.com:881/q', - { - 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 - }); - }); - - it('should not re-patch https.request', () => { - var patchModulePath = "../patch-core"; - var patchedRequest = https.request; - - delete require.cache[require.resolve(patchModulePath)]; - require(patchModulePath); - - assert.equal(patchedRequest, https.request); - assert.equal(true, https.request.__agent_base_https_request_patched__); - }); - - 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(); - }); - }); - - // 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(); - wss.removeListener('connection', onconnection); - 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(); - }); - }); - - // 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(); - }); - }); -}); |