@dnlup/agent-11
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -5,2 +5,13 @@ # Changelog | ||
## [2.0.0](https://github.com/dnlup/agent-11/compare/v1.0.2...v2.0.0) (2020-12-09) | ||
### ⚠ BREAKING CHANGES | ||
* some error messages have changed. | ||
### Features | ||
* add full url support ([34a54e1](https://github.com/dnlup/agent-11/commit/34a54e134698ac1fb4c38f62784fa11ad1661712)) | ||
### [1.0.2](https://github.com/dnlup/agent-11/compare/v1.0.1...v1.0.2) (2020-12-01) | ||
@@ -7,0 +18,0 @@ |
89
index.js
@@ -15,6 +15,3 @@ 'use strict' | ||
kStore, | ||
kTimersMap, | ||
kGetKey, | ||
kGetKeyFromString, | ||
kGetKeyFromObject | ||
kTimersMap | ||
} = require('./symbols') | ||
@@ -51,2 +48,39 @@ | ||
static urlToObject (url) { | ||
if (typeof url === 'string' && url.length) { | ||
const match = URL_REG.exec(url) | ||
return { | ||
protocol: match && match[1], | ||
hostname: match && match[2], | ||
port: match && match[3] | ||
} | ||
// perf: this branch has polymorphic inline caches | ||
} else if (typeof url === 'object' && url !== null) { | ||
return { | ||
protocol: url.protocol, | ||
hostname: url.hostname, | ||
port: url.port | ||
} | ||
} | ||
throw new TypeError(`Invalid url, received: ${url}`) | ||
} | ||
static getKey (url, options) { | ||
let key = url.protocol || 'http:' | ||
// perf: this part has polymorphic inline caches | ||
if (key.charAt(key.length - 1) !== ':') { | ||
key += ':' | ||
} | ||
key += url.hostname | ||
if ((typeof url.port === 'string' && url.port.length) || typeof url.port === 'number') { | ||
key += ':' | ||
key += url.port | ||
} | ||
if (options && options.socketPath) { | ||
key += ':' | ||
key += options.socketPath | ||
} | ||
return key | ||
} | ||
[kSetupConnection] (url, options) { | ||
@@ -86,42 +120,3 @@ if (this.size === this[kMaxHosts]) { | ||
[kGetKeyFromString] (url) { | ||
const match = URL_REG.exec(url) | ||
return this[kGetKeyFromObject]({ | ||
protocol: match && match[1], | ||
hostname: match && match[2], | ||
port: match && match[3] | ||
}) | ||
} | ||
[kGetKeyFromObject] (url) { | ||
let key = url.protocol || 'http:' | ||
if (key.charAt(key.length - 1) !== ':') { | ||
key += ':' | ||
} | ||
key += url.hostname | ||
if (typeof url.port === 'string' || typeof url.port === 'number') { | ||
key += ':' | ||
key += url.port | ||
} | ||
return key | ||
} | ||
[kGetKey] (url, options) { | ||
let key = '' | ||
if (typeof url === 'string') { | ||
key = this[kGetKeyFromString](url) | ||
} else if (typeof url === 'object' && url !== null) { | ||
key = this[kGetKeyFromObject](url) | ||
} else { | ||
throw new TypeError(`Can't get key from url: '${url}'`) | ||
} | ||
if (options && options.socketPath) { | ||
key += ':' | ||
key += options.socketPath | ||
} | ||
return key | ||
} | ||
getConnection (url, options) { | ||
const key = this[kGetKey](url, options) | ||
connection (key, url, options) { | ||
if (this[kHostsMap].has(key)) { | ||
@@ -138,2 +133,8 @@ const pool = this[kHostsMap].get(key) | ||
getConnection (url, options) { | ||
url = Agent11.urlToObject(url) | ||
const key = Agent11.getKey(url, options) | ||
return this.connection(key, url, options) | ||
} | ||
close () { | ||
@@ -140,0 +141,0 @@ const closing = [] |
{ | ||
"name": "@dnlup/agent-11", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "A simple undici pool manager", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # agent-11 | ||
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. | ||
You might find this module useful if you use [`undici`](https://github.com/nodejs/undici) and need to manage connections to different and unknown hosts. | ||
@@ -24,2 +24,4 @@ `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. | ||
* [Class: `Agent11`](#class-agent11) | ||
+ [Static method: `Agent11.urlToObject(url)`](#static-method-agent11urltoobjecturl) | ||
+ [Static method: `Agent11.getKey(url[, options])`](#static-method-agent11getkeyurl-options) | ||
+ [new `Agent11([options])`](#new-agent11options) | ||
@@ -63,8 +65,14 @@ + [`agent.getConnection(url, [options])`](#agentgetconnectionurl-options) | ||
const conn1 = agent.getConnection('http://localhost:3000') // use conn1 to make requests with undici API | ||
const conn1 = agent.getConnection('http://localhost:3000/some/path') // use conn1 to make requests with undici API to locahost:3000 | ||
const conn2 = agent.getConnection(new URL('http://localhost:4000', { | ||
const conn2 = agent.getConnection(new URL('http://localhost:4000/some/other/path', { | ||
socketPath: '/tmp/agent-11.sock' // these options are merged with the default `connectionOptions` passed when creating the agent | ||
}) | ||
const conn3 = agent.getConnection({ | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: 5000 | ||
}) | ||
// close all the agent connections | ||
@@ -84,2 +92,15 @@ agent.close().then().catch(console.error) | ||
#### Static method: `Agent11.urlToObject(url)` | ||
* `url` `<string||URL|Object>`: the url to convert. | ||
* Returns: `<Object>` A url-like object with the properties `protocol`, `hostname` and `port`. | ||
#### Static method: `Agent11.getKey(url[, options])` | ||
* `url` `<Object>`: a url-like object. | ||
* `options` `<Object>`: connection options. See [undici documentation](https://github.com/nodejs/undici#new-undiciclienturl-opts). | ||
* Returns: `<string>`: the key that maps the url. | ||
This method creates a key that maps a connection pool to a url. | ||
#### new `Agent11([options])` | ||
@@ -132,1 +153,2 @@ | ||
``` | ||
@@ -13,6 +13,3 @@ 'use strict' | ||
kStore: Symbol('kStore'), | ||
kTimersMap: Symbol('kTimersMap'), | ||
kGetKey: Symbol('kGetKey'), | ||
kGetKeyFromString: Symbol('kGetKeyFromString'), | ||
kGetKeyFromObject: Symbol('kGetKeyFromObject') | ||
kTimersMap: Symbol('kTimersMap') | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13059
150
150