{"id":17013761,"url":"https://github.com/dnlup/agent-11","last_synced_at":"2025-03-22T16:31:08.763Z","repository":{"id":37055981,"uuid":"287250568","full_name":"dnlup/agent-11","owner":"dnlup","description":"A simple pool manager for undici","archived":false,"fork":false,"pushed_at":"2023-03-10T23:57:37.000Z","size":596,"stargazers_count":6,"open_issues_count":15,"forks_count":0,"subscribers_count":3,"default_branch":"next","last_synced_at":"2025-03-14T09:17:48.299Z","etag":null,"topics":["http","nodejs","undici"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dnlup.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-13T10:35:39.000Z","updated_at":"2025-02-04T00:50:56.000Z","dependencies_parsed_at":"2024-10-28T14:11:46.847Z","dependency_job_id":"ced68840-244a-45ba-8a8a-b331254a00e3","html_url":"https://github.com/dnlup/agent-11","commit_stats":{"total_commits":107,"total_committers":2,"mean_commits":53.5,"dds":0.485981308411215,"last_synced_commit":"ef3dfdd9deb0fb8c9c423b0e02077d9b64914a35"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnlup%2Fagent-11","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnlup%2Fagent-11/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnlup%2Fagent-11/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnlup%2Fagent-11/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dnlup","download_url":"https://codeload.github.com/dnlup/agent-11/tar.gz/refs/heads/next","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244986502,"owners_count":20543024,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","nodejs","undici"],"created_at":"2024-10-14T06:14:08.823Z","updated_at":"2025-03-22T16:31:08.376Z","avatar_url":"https://github.com/dnlup.png","language":"JavaScript","readme":"# agent-11\n[![npm version](https://badge.fury.io/js/%40dnlup%2Fagent-11.svg)](https://badge.fury.io/js/%40dnlup%2Fagent-11)\n![Tests](https://github.com/dnlup/agent-11/workflows/Tests/badge.svg)\n[![codecov](https://codecov.io/gh/dnlup/agent-11/branch/next/graph/badge.svg?token=GW9FKJPL5L)](https://codecov.io/gh/dnlup/agent-11)\n[![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)\n\n\u003e A simple pool manager for [`undici`](https://github.com/nodejs/undici).\n\nYou might find this module useful if you use [`undici`](https://github.com/nodejs/undici) and need to manage connections to different hosts and you don't know them ahead of time, so you can't\ncreate static clients.\n\n`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.\nIf you don't request this connection after a certain amount of time, `agent-11` will close it.\n\n\u003c!-- toc --\u003e\n\n- [Installation](#installation)\n    + [Requirements](#requirements)\n    + [latest stable version](#latest-stable-version)\n    + [latest development version](#latest-development-version)\n- [Usage](#usage)\n- [API](#api)\n  * [Class: `Agent11`](#class-agent11)\n    + [Static method: `Agent11.urlToObject(url)`](#static-method-agent11urltoobjecturl)\n    + [Static method: `Agent11.getKey(url[, options])`](#static-method-agent11getkeyurl-options)\n    + [new `Agent11([options])`](#new-agent11options)\n    + [`agent.getConnection(url, [options])`](#agentgetconnectionurl-options)\n    + [`agent.close()`](#agentclose)\n    + [`agent.destroy([error])`](#agentdestroyerror)\n- [Contributing](#contributing)\n\n\u003c!-- tocstop --\u003e\n\n## Installation\n\n#### Requirements\n\n`agent-11` requires that you already have installed `undici` in your project.\n\n#### latest stable version\n\n```bash\n$ npm i @dnlup/agent-11\n```\n\n#### latest development version\n\n```bash\n$ npm i @dnlup/agent-11@next\n```\n\n## Usage\n\n```js\nconst Agent11 = require('@dnlup/agent-11')\n\nconst agent = new Agent11({\n  closeTimeout: 6e5, // inactive connections will be closed after 600000 millieconds \n  connectionOptions: {\n    pipelining: 10\n  }\n}\n\nconst conn1 = agent.getConnection('http://localhost:3000/some/path') // use conn1 to make requests with undici API to locahost:3000\n\nconst conn2 = agent.getConnection(new URL('http://localhost:4000/some/other/path', {\n  socketPath: '/tmp/agent-11.sock' // these options are merged with the default `connectionOptions` passed when creating the agent\n})\n\nconst conn3 = agent.getConnection({\n  protocol: 'http:',\n  hostname: 'localhost',\n  port: 5000\n})\n\n// close all the agent connections\nagent.close().then().catch(console.error)\n\n// destroy all the agent connections\nagent.destroy(new Error('no more!')).then().catch(console.error)\n```\n## API\n\n\u003e The module directly exports a [`Agent11`](#class-agent11) class, which is the connections manager.\n\n### Class: `Agent11`\n\nIt manages `undici`'s pool connections.\n\n#### Static method: `Agent11.urlToObject(url)`\n\n* `url` `\u003cstring||URL|Object\u003e`: the url to convert.\n* Returns: `\u003cObject\u003e` A url-like object with the properties `protocol`, `hostname` and `port`.\n\n#### Static method: `Agent11.getKey(url[, options])`\n\n* `url` `\u003cObject\u003e`: a url-like object.\n* `options` `\u003cObject\u003e`: connection options. See [undici documentation](https://github.com/nodejs/undici#new-undiciclienturl-opts).\n* Returns: `\u003cstring\u003e`: the key that maps the url.\n\nThis method creates a key that maps a connection pool to a url.\n\n#### new `Agent11([options])`\n\n* `options` `\u003cObject\u003e`\n  * `closeTimeout` `\u003cnumber\u003e`: the time (in milliseconds) of inactivity, after which it will close a connection. **Default:** `60000`.\n  * `maxHosts` `\u003cnumber\u003e`: the maximum number of connections to different hosts. **Default:** `Infinity` .\n  * `connectionOptions`: the default options to use to create a new connection. See [undici documentation](https://github.com/nodejs/undici#new-undiciclienturl-opts).\n\n#### `agent.getConnection(url, [options])`\n\n* `url` `\u003cstring|URL|Object\u003e`: the url to connect to.\n* `options` `\u003cObject\u003e`: the connection options.\n* Returns: [`Pool`](https://github.com/nodejs/undici#new-undicipoolurl-opts)\n\nThe 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.\nIt returns a `Pool` instance connected to the given `url` and `options`.\n\n#### `agent.close()`\n\n* Returns: `\u003cPromise\u003e`\n\nIt closes all the `Pool` connections gracefully.\n\n#### `agent.destroy([error])`\n\n* `error` `\u003cError\u003e`: the error to emit when destroying the connections.\n* Returns: `\u003cPromise\u003e`\n\nIt destroys all the `Pool` connections. It optionally takes an error parameter.\n\n## Contributing\n\nYou found a bug or want to discuss and implement a new feature? This project welcomes contributions.\n\nThe code follows the [standardjs](https://standardjs.com/) style guide.\n\nEvery contribution should pass the existing tests or implementing new ones if that's the case.\n\n```bash\n# Run tests\n$ npm test\n\n# Lint the code\n$ npm lint\n\n# Create the TOC in the README\n$ npm run doc\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnlup%2Fagent-11","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdnlup%2Fagent-11","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnlup%2Fagent-11/lists"}