Keep-Alive HTTP/S Agent
raw JSON →This package provides a basic HTTP and HTTPS connection pooling agent for Node.js, designed to reuse sockets for improved performance by reducing the overhead of establishing new TCP connections. Originally published as version 0.0.1 in December 2012, the package appears to be abandoned, with no significant updates or active development since its initial release. Its primary differentiator was its simplicity, aiming to complement Node.js's default `Agent` rather than replace it. However, the Node.js ecosystem has evolved significantly since then, with native `http.Agent` offering `keepAlive` functionality and more feature-rich, actively maintained alternatives like `agentkeepalive` now widely available. Due to its age and lack of maintenance, it is not recommended for use in modern applications.
Common errors
error TypeError: KeepAliveAgent is not a constructor ↓
new http.Agent({ keepAlive: true }) or new (require('agentkeepalive').HttpAgent)() instead. error ERR_REQUIRE_ESM ↓
agentkeepalive or Node.js's built-in Agent features. error ECONNRESET ↓
keep-alive implementations. Upgrade to agentkeepalive (which includes better timeout handling) or ensure your client-side keepAlive and timeout settings are less than the server's keepAliveTimeout. Warnings
breaking This package (v0.0.1 from 2012) is severely outdated and is not compatible with modern Node.js features or best practices. It likely contains unpatched security vulnerabilities and may not function as expected or at all with current Node.js versions (e.g., Node.js 16+). ↓
gotcha Node.js versions prior to 8.0.0 had a potential Denial of Service (DoS) vulnerability (CVE-2019-5739) related to HTTP/S keep-alive connections remaining open for extended periods. This package predates these fixes and would not benefit from the default 5-second `server.keepAliveTimeout` introduced in Node.js 8.0.0. ↓
gotcha Using an unmaintained package for network operations can lead to `ECONNRESET` errors due to mismatched client/server connection timeouts. Modern `keep-alive` agents or Node.js's built-in options have better mechanisms to handle these scenarios. ↓
gotcha There is no community support, bug fixes, or security patches for this abandoned package. Relying on it introduces significant operational risks and technical debt. ↓
Install
npm install keep-alive-agent yarn add keep-alive-agent pnpm add keep-alive-agent Imports
- KeepAliveAgent wrong
import { KeepAliveAgent } from 'keep-alive-agent';correctconst KeepAliveAgent = require('keep-alive-agent'); - KeepAliveAgent.Secure wrong
import { Secure } from 'keep-alive-agent';correctconst KeepAliveAgent = require('keep-alive-agent'); const agent = new KeepAliveAgent.Secure();
Quickstart
const http = require('http');
const https = require('https');
const KeepAliveAgent = require('keep-alive-agent');
// Ensure these environment variables are set for testing
const HTTP_HOSTNAME = process.env.TEST_HOSTNAME_HTTP || 'example.com';
const HTTPS_HOSTNAME = process.env.TEST_HOSTNAME_HTTPS || 'google.com';
const httpAgent = new KeepAliveAgent({ maxSockets: 10 });
const httpsAgent = new KeepAliveAgent.Secure({ maxSockets: 10 });
// Example using HTTP keep-alive agent
const httpOptions = {
hostname: HTTP_HOSTNAME,
port: 80,
path: '/',
agent: httpAgent,
};
http.get(httpOptions, (response) => {
console.log(`HTTP Status: ${response.statusCode}`);
response.pipe(process.stdout);
}).on('error', (e) => {
console.error(`HTTP request error: ${e.message}`);
});
// Example using HTTPS keep-alive agent
const httpsOptions = {
hostname: HTTPS_HOSTNAME,
port: 443,
path: '/',
agent: httpsAgent,
};
https.get(httpsOptions, (response) => {
console.log(`HTTPS Status: ${response.statusCode}`);
response.pipe(process.stdout);
}).on('error', (e) => {
console.error(`HTTPS request error: ${e.message}`);
});