Import Upstream version 6.0.2+~cs5.4.2

This commit is contained in:
zhouganqing 2023-02-17 11:10:55 +08:00
commit c36ffc8542
22 changed files with 2224 additions and 0 deletions

37
.editorconfig Normal file
View File

@ -0,0 +1,37 @@
root = 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

86
.eslintrc.js Normal file
View File

@ -0,0 +1,86 @@
module.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'
}
}

40
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,40 @@
name: 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

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/package-lock.json
/yarn.lock
/node_modules
/?.?s
/dist

145
README.md Normal file
View File

@ -0,0 +1,145 @@
agent-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

20
async-listen/Readme.md Normal file
View File

@ -0,0 +1,20 @@
# async-listen
`listen()` for use with `async` / `await`.
## Example
```typescript
import listen from 'async-listen';
import { createServer } from 'http';
async function main() {
const port = 3000;
const server = createServer();
const address = await listen(server, port);
console.log(address);
// 'http://127.0.0.1:3000'
}
main().catch(console.error);
```

3
async-listen/dist/src/index.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
/// <reference types="node" />
import { Server } from 'net';
export default function listen(server: Server, ...args: any[]): Promise<string>;

39
async-listen/dist/src/index.js vendored Normal file
View File

@ -0,0 +1,39 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const url_1 = require("url");
function listen(server, ...args) {
return new Promise((resolve, reject) => {
function cleanup() {
server.removeListener('error', onError);
}
function onError(err) {
cleanup();
reject(err);
}
args.push((err) => {
cleanup();
if (err)
return reject(err);
const address = server.address();
if (typeof address === 'string') {
resolve(address);
}
else {
// TODO: detect protocol type based on `server` shape
const protocol = 'http:';
const { address: hostname, port } = address;
const url = url_1.format({ protocol, hostname, port });
resolve(url);
}
});
server.on('error', onError);
try {
server.listen(...args);
}
catch (err) {
onError(err);
}
});
}
exports.default = listen;
//# sourceMappingURL=index.js.map

1
async-listen/dist/src/index.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAG7B,SAAwB,MAAM,CAAC,MAAc,EAAE,GAAG,IAAW;IAC5D,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,SAAS,OAAO;YACf,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACzC,CAAC;QACD,SAAS,OAAO,CAAC,GAAU;YAC1B,OAAO,EAAE,CAAC;YACV,MAAM,CAAC,GAAG,CAAC,CAAC;QACb,CAAC;QACD,IAAI,CAAC,IAAI,CACR,CAAC,GAAU,EAAQ,EAAE;YACpB,OAAO,EAAE,CAAC;YACV,IAAI,GAAG;gBAAE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;YAC5B,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;YACjC,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAChC,OAAO,CAAC,OAAO,CAAC,CAAC;aACjB;iBAAM;gBACN,qDAAqD;gBACrD,MAAM,QAAQ,GAAG,OAAO,CAAC;gBACzB,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;gBAC5C,MAAM,GAAG,GAAG,YAAM,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;gBACjD,OAAO,CAAC,GAAG,CAAC,CAAC;aACb;QACF,CAAC,CACD,CAAC;QACF,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC5B,IAAI;YACH,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC;SACvB;QAAC,OAAO,GAAG,EAAE;YACb,OAAO,CAAC,GAAG,CAAC,CAAC;SACb;IACF,CAAC,CAAC,CAAC;AACJ,CAAC;AAhCD,yBAgCC"}

39
async-listen/package.json Normal file
View File

@ -0,0 +1,39 @@
{
"name": "async-listen",
"version": "1.2.0",
"description": "`net.Server#listen()` helper that returns a Promise for async / await",
"main": "./dist/src/index",
"typings": "./dist/src/index",
"scripts": {
"prepublish": "tsc",
"test": "tsc && ava dist/test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/zeit/async-listen.git"
},
"files": [
"dist/src"
],
"keywords": [
"net",
"http",
"server",
"listen",
"es6",
"async",
"await",
"promise"
],
"author": "Nathan Rajlich <nate@zeit.co>",
"license": "MIT",
"bugs": {
"url": "https://github.com/zeit/async-listen/issues"
},
"homepage": "https://github.com/zeit/async-listen#readme",
"devDependencies": {
"@types/node": "10.12.21",
"ava": "0.16.0",
"typescript": "3.3.3"
}
}

64
package.json Normal file
View File

@ -0,0 +1,64 @@
{
"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 <nathan@tootallnate.net> (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"
}
}

345
src/index.ts Normal file
View File

@ -0,0 +1,345 @@
import 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<http.RequestOptions, keyof AgentRequestOptions> {
secureEndpoint: false;
}
export interface HttpsRequestOptions
extends AgentRequestOptions,
Omit<https.RequestOptions, keyof AgentRequestOptions> {
secureEndpoint: true;
}
export type RequestOptions = HttpRequestOptions | HttpsRequestOptions;
export type AgentLike = Pick<createAgent.Agent, 'addRequest'> | 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<createAgent.AgentCallbackReturn>;
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<createAgent.AgentCallbackReturn>;
callback(
req: createAgent.ClientRequest,
opts: createAgent.AgentOptions,
fn?: createAgent.AgentCallbackCallback
):
| createAgent.AgentCallbackReturn
| Promise<createAgent.AgentCallbackReturn>
| 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<typeof setTimeout> | 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;

33
src/promisify.ts Normal file
View File

@ -0,0 +1,33 @@
import {
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);
}
}
);
});
};
}

View File

@ -0,0 +1,15 @@
-----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-----

View File

@ -0,0 +1,12 @@
-----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-----

689
test/test-legacy.js Normal file
View File

@ -0,0 +1,689 @@
/**
* 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();
});
});
});
});

481
test/test.ts Normal file
View File

@ -0,0 +1,481 @@
import 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<http.IncomingMessage> => {
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<any> {
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<http.Agent> {
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<net.Socket> => {
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<net.Socket> => {
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<Agent> => {
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<net.Socket> => {
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;
}
});
}
});
});

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"strict": true,
"module": "CommonJS",
"target": "es2015",
"esModuleInterop": true,
"lib": ["esnext"],
"outDir": "dist",
"sourceMap": true,
"declaration": true
},
"include": ["src/**/*", "test/**/*"],
"exclude": ["node_modules"]
}

21
types-agent-base/LICENSE Executable file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) Microsoft Corporation.
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

64
types-agent-base/README.md Executable file
View File

@ -0,0 +1,64 @@
# Installation
> `npm install --save @types/agent-base`
# Summary
This package contains type definitions for agent-base (https://github.com/TooTallNate/node-agent-base#readme).
# Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/agent-base.
## [index.d.ts](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/agent-base/index.d.ts)
````ts
// Type definitions for agent-base 4.2
// Project: https://github.com/TooTallNate/node-agent-base#readme
// Definitions by: Christopher Quadflieg <https://github.com/Shinigami92>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { EventEmitter } from 'events';
declare namespace Agent {
type AgentCallback = (
req?: any,
opts?: {
secureEndpoint: boolean;
}
) => void;
interface AgentOptions {
timeout?: number | undefined;
host?: string | undefined;
port?: number | undefined;
[key: string]: any;
}
interface Agent extends EventEmitter {
_promisifiedCallback: boolean;
timeout: number | null;
options?: AgentOptions | undefined;
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;
````
### Additional Details
* Last updated: Tue, 06 Jul 2021 18:05:31 GMT
* Dependencies: [@types/node](https://npmjs.com/package/@types/node)
* Global values: none
# Credits
These definitions were written by [Christopher Quadflieg](https://github.com/Shinigami92).

44
types-agent-base/index.d.ts vendored Executable file
View File

@ -0,0 +1,44 @@
// Type definitions for agent-base 4.2
// Project: https://github.com/TooTallNate/node-agent-base#readme
// Definitions by: Christopher Quadflieg <https://github.com/Shinigami92>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { EventEmitter } from 'events';
declare namespace Agent {
type AgentCallback = (
req?: any,
opts?: {
secureEndpoint: boolean;
}
) => void;
interface AgentOptions {
timeout?: number | undefined;
host?: string | undefined;
port?: number | undefined;
[key: string]: any;
}
interface Agent extends EventEmitter {
_promisifiedCallback: boolean;
timeout: number | null;
options?: AgentOptions | undefined;
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;

27
types-agent-base/package.json Executable file
View File

@ -0,0 +1,27 @@
{
"name": "@types/agent-base",
"version": "4.2.2",
"description": "TypeScript definitions for agent-base",
"homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/agent-base",
"license": "MIT",
"contributors": [
{
"name": "Christopher Quadflieg",
"url": "https://github.com/Shinigami92",
"githubUsername": "Shinigami92"
}
],
"main": "",
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git",
"directory": "types/agent-base"
},
"scripts": {},
"dependencies": {
"@types/node": "*"
},
"typesPublisherContentHash": "f7c1c8e886d0ae16f561ff9f823b92b18b79833b370943976a8912e51fb299b0",
"typeScriptVersion": "3.6"
}