@dnlup/agent-11
Advanced tools
Comparing version 1.0.0-2 to 1.0.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.0.0](https://github.com/dnlup/agent-11/compare/v1.0.0-2...v1.0.0) (2020-10-28) | ||
### Bug Fixes | ||
* rename destroyTimeout option ([7d1e61e](https://github.com/dnlup/agent-11/commit/7d1e61e363c1d2e3bc6210aaa57e6f1e407b7dd6)) | ||
## [1.0.0-2](https://github.com/dnlup/agent-11/compare/v1.0.0-1...v1.0.0-2) (2020-10-22) | ||
@@ -7,0 +14,0 @@ |
32
index.js
@@ -8,3 +8,3 @@ 'use strict' | ||
kHostsMap, | ||
kDestroyTimeout, | ||
kCloseTimeout, | ||
kMaxHosts, | ||
@@ -14,2 +14,3 @@ kDefaultConnectionOptions, | ||
kRemove, | ||
kClose, | ||
kRefresh, | ||
@@ -23,17 +24,16 @@ kStore, | ||
function onTimeout (pool, client, key) { | ||
client[kHostsMap].delete(key) | ||
client[kTimersMap].delete(pool) | ||
function onTimeout (key, pool, client) { | ||
client[kRemove](key, pool) | ||
pool.close(noop) | ||
} | ||
class Agent { | ||
class Agent11 { | ||
constructor ({ | ||
destroyTimeout = 6e4, | ||
closeTimeout = 6e4, | ||
maxHosts = Infinity, | ||
connectionOptions = {} | ||
} = {}) { | ||
if (typeof destroyTimeout !== 'number' || destroyTimeout <= 0) { | ||
if (typeof closeTimeout !== 'number' || closeTimeout <= 0) { | ||
throw new Error( | ||
'destroyTimeout must be a number > 0' | ||
'closeTimeout must be a number > 0' | ||
) | ||
@@ -45,3 +45,3 @@ } | ||
this[kHostsMap] = new Map() | ||
this[kDestroyTimeout] = destroyTimeout | ||
this[kCloseTimeout] = closeTimeout | ||
this[kMaxHosts] = maxHosts | ||
@@ -70,3 +70,3 @@ this[kDefaultConnectionOptions] = connectionOptions | ||
pool, | ||
setTimeout(onTimeout, this[kDestroyTimeout], pool, this, key) | ||
setTimeout(onTimeout, this[kCloseTimeout], key, pool, this) | ||
) | ||
@@ -77,6 +77,10 @@ } | ||
this[kHostsMap].delete(key) | ||
clearTimeout(this[kTimersMap].get(pool)) | ||
this[kTimersMap].delete(pool) | ||
} | ||
[kClose] (key, pool) { | ||
clearTimeout(this[kTimersMap].get(pool)) | ||
this[kRemove](key, pool) | ||
} | ||
get size () { | ||
@@ -125,3 +129,3 @@ return this[kHostsMap].size | ||
closing.push(pool.close()) | ||
this[kRemove](key, pool) | ||
this[kClose](key, pool) | ||
} | ||
@@ -135,3 +139,3 @@ return Promise.all(closing) | ||
closing.push(pool.destroy(err)) | ||
this[kRemove](key, pool) | ||
this[kClose](key, pool) | ||
} | ||
@@ -142,2 +146,2 @@ return Promise.all(closing) | ||
module.exports = Agent | ||
module.exports = Agent11 |
{ | ||
"name": "@dnlup/agent-11", | ||
"version": "1.0.0-2", | ||
"version": "1.0.0", | ||
"description": "A simple undici pool manager", | ||
@@ -15,2 +15,3 @@ "main": "index.js", | ||
"coverage": "tap --coverage-report=html", | ||
"trace:ic": "deoptigate benchmarks/agent-11.js", | ||
"doc": "markdown-toc -i README.md", | ||
@@ -48,5 +49,6 @@ "prerelease": "npm cit", | ||
"devDependencies": { | ||
"deoptigate": "^0.5.0", | ||
"husky": "^4.3.0", | ||
"markdown-toc": "^1.2.0", | ||
"snazzy": "^8.0.0", | ||
"snazzy": "^9.0.0", | ||
"standard": "^15.0.0", | ||
@@ -53,0 +55,0 @@ "standard-version": "^9.0.0", |
118
README.md
# agent-11 | ||
[![npm version](https://badge.fury.io/js/%40dnlup%2Fagent-11.svg)](https://badge.fury.io/js/%40dnlup%2Fagent-11) | ||
![Tests](https://github.com/dnlup/agent-11/workflows/Tests/badge.svg) | ||
[![Coverage Status](https://coveralls.io/repos/github/dnlup/agent-11/badge.svg?branch=next)](https://coveralls.io/github/dnlup/agent-11?branch=next) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/dnlup/agent-11/badge.svg?targetFile=package.json)](https://snyk.io/test/github/dnlup/agent-11?targetFile=package.json) | ||
A simple pool manager for `undici`. | ||
> A simple pool manager for [`undici`](https://github.com/nodejs/undici). | ||
**This is still alhpa** | ||
You might find this module useful if you are using [`undici`](https://github.com/nodejs/undici) and need to manage connections to different and unknown hosts. | ||
`agent-11` controls [`undici`'s](https://github.com/nodejs/undici) pool connections to different hosts. Each time you request a new one, it creates a new pool. | ||
If you don't request this connection after a certain amount of time, `agent-11` will close it. | ||
<!-- toc --> | ||
- [Installation](#installation) | ||
+ [Requirements](#requirements) | ||
+ [latest stable version](#latest-stable-version) | ||
+ [latest development version](#latest-development-version) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
* [Class: `Agent11`](#class-agent11) | ||
+ [new `Agent11([options])`](#new-agent11options) | ||
+ [`agent.getConnection(url, [options])`](#agentgetconnectionurl-options) | ||
+ [`agent.close()`](#agentclose) | ||
+ [`agent.destroy([error])`](#agentdestroyerror) | ||
- [Contributing](#contributing) | ||
<!-- tocstop --> | ||
## Installation | ||
#### Requirements | ||
`agent-11` requires that you already have installed `undici` in your project. | ||
#### latest stable version | ||
```bash | ||
$ npm i @dnlup/agent-11 | ||
``` | ||
#### latest development version | ||
```bash | ||
$ npm i @dnlup/agent-11@next | ||
``` | ||
## Usage | ||
```js | ||
const Agent11 = require('@dnlup/agent-11') | ||
const agent = new Agent11({ | ||
closeTimeout: 6e5, // inactive connections will be closed after 600000 millieconds | ||
connectionOptions: { | ||
pipelining: 10 | ||
} | ||
} | ||
const conn1 = agent.getConnection('http://localhost:3000') // use conn1 to make requests with undici API | ||
const conn2 = agent.getConnection(new URL('http://localhost:4000', { | ||
socketPath: '/tmp/agent-11.sock' // these options are merged with the default `connectionOptions` passed when creating the agent | ||
}) | ||
// close all the agent connections | ||
agent.close().then().catch(console.error) | ||
// destroy all the agent connections | ||
agent.destroy(new Error('no more!')).then().catch(console.error) | ||
``` | ||
## API | ||
> The module directly exports a [`Agent11`](#class-agent11) class, which is the connections manager. | ||
### Class: `Agent11` | ||
It manages `undici`'s pool connections. | ||
#### new `Agent11([options])` | ||
* `options` `<Object>` | ||
* `closeTimeout` `<number>`: the time (in milliseconds) of inactivity, after which it will close a connection. **Default:** `60000`. | ||
* `maxHosts` `<number>`: the maximum number of connections to different hosts. **Default:** `Infinity` . | ||
* `connectionOptions`: the default options to use to create a new connection. See [undici documentation](https://github.com/nodejs/undici#new-undiciclienturl-opts). | ||
#### `agent.getConnection(url, [options])` | ||
* `url` `<string|URL|Object>`: the url to connect to. | ||
* `options` `<Object>`: the connection options. | ||
* Returns: [`Pool`](https://github.com/nodejs/undici#new-undicipoolurl-opts) | ||
The parameters are the same ones as [`undici`](https://github.com/nodejs/undici#new-undiciclienturl-opts). It will merge the `options` object with the `connectionOptions` specified when creating the class instance. | ||
It returns a `Pool` instance connected to the given `url` and `options`. | ||
#### `agent.close()` | ||
* Returns: `<Promise>` | ||
It closes all the `Pool` connections gracefully. | ||
#### `agent.destroy([error])` | ||
* `error` `<Error>`: the error to emit when destroying the connections. | ||
* Returns: `<Promise>` | ||
It destroys all the `Pool` connections. It optionally takes an error parameter. | ||
## Contributing | ||
You found a bug or want to discuss and implement a new feature? This project welcomes contributions. | ||
The code follows the [standardjs](https://standardjs.com/) style guide. | ||
Every contribution should pass the existing tests or implementing new ones if that's the case. | ||
```bash | ||
# Run tests | ||
$ npm test | ||
# Lint the code | ||
$ npm lint | ||
# Create the TOC in the README | ||
$ npm run doc | ||
``` |
@@ -5,3 +5,3 @@ 'use strict' | ||
kHostsMap: Symbol('kHostsMap'), | ||
kDestroyTimeout: Symbol('kDestroyTimeout'), | ||
kCloseTimeout: Symbol('kDestroyTimeout'), | ||
kMaxHosts: Symbol('kMaxHosts'), | ||
@@ -11,2 +11,3 @@ kDefaultConnectionOptions: Symbol('kDefaultConnectionOptions'), | ||
kRemove: Symbol('kRemove'), | ||
kClose: Symbol('kClose'), | ||
kRefresh: Symbol('kRefresh'), | ||
@@ -13,0 +14,0 @@ kStore: Symbol('kStore'), |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10970
135
0
128
8