npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dnlup/agent-11

v3.0.0

Published

A simple undici pool manager

Downloads

9

Readme

agent-11

npm version Tests codecov Known Vulnerabilities

A simple pool manager for undici.

You might find this module useful if you use undici and need to manage connections to different hosts and you don't know them ahead of time, so you can't create static clients.

agent-11 controls undici's 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.

Installation

Requirements

agent-11 requires that you already have installed undici in your project.

latest stable version

$ npm i @dnlup/agent-11

latest development version

$ npm i @dnlup/agent-11@next

Usage

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/some/path') // use conn1 to make requests with undici API to locahost:3000

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
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, which is the connections manager.

Class: Agent11

It manages undici's pool connections.

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.
  • Returns: <string>: the key that maps the url.

This method creates a key that maps a connection pool to a url.

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.

agent.getConnection(url, [options])

  • url <string|URL|Object>: the url to connect to.
  • options <Object>: the connection options.
  • Returns: Pool

The parameters are the same ones as undici. 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 style guide.

Every contribution should pass the existing tests or implementing new ones if that's the case.

# Run tests
$ npm test

# Lint the code
$ npm lint

# Create the TOC in the README
$ npm run doc