Node.js HTTP/HTTPS Agent with Infinite Sockets
infinity-agent is a Node.js utility package that provides custom HTTP and HTTPS agents configured with `maxSockets` set to `Infinity`. It effectively mirrors the behavior of Node.js core `http.Agent` and `https.Agent` classes but modifies them to allow an unlimited number of concurrent outgoing requests. This can be critical for high-throughput applications or services that experience `maxSockets` exhaustion with default Node.js agents. The current stable version is 2.0.3. As a wrapper around Node.js core modules, its release cadence tends to be stable, with updates driven by bug fixes or significant changes in Node.js's underlying network modules. A key differentiator is its out-of-the-box support for an unbounded connection pool, requiring minimal configuration to achieve high concurrency without the usual `maxSockets` limitations. It also includes a minor modification to the `addRequest` method that disables `keepAlive` if the agent isn't explicitly configured for it and `maxSockets` is set to `Infinity`, which is a subtle behavioral difference from the standard Node.js agent.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'globalAgent')
cause Attempting to access `http.globalAgent` or `https.globalAgent` on a `require`d or `import`ed object before it's fully resolved, or if the `infinity-agent` package's structure has changed.fixEnsure `infinity-agent` is correctly imported and that you are accessing the `globalAgent` property on the `http` or `https` sub-objects. For CommonJS: `const { http, https } = require('infinity-agent'); const agent = http.globalAgent;` -
Error: socket hang up
cause This generic network error can occur due to various reasons including server-side issues, network interruptions, or subtle interactions with `keepAlive` behavior when `maxSockets` is infinite.fixWhile `infinity-agent` manages socket pooling, `socket hang up` often points to the remote server closing connections prematurely or network instability. Review server logs, ensure the remote service can handle sustained connections, and consider implementing robust retry mechanisms with exponential backoff in your client code.
Warnings
- gotcha The package includes a minor behavioral change in its `addRequest` method: `keepAlive` functionality is disabled if the agent is not explicitly configured for it, and `maxSockets` is set to `Infinity`. Users expecting exact parity with Node.js core `Agent` behavior in all `keepAlive` scenarios should be aware of this difference.
- gotcha While `infinity-agent` removes the `maxSockets` limit, it does not inherently prevent other forms of resource exhaustion in high-concurrency scenarios, such as excessive memory usage due to too many open sockets or file descriptors. Node.js itself has system-level limits.
Install
-
npm install infinity-agent -
yarn add infinity-agent -
pnpm add infinity-agent
Imports
- infinityAgent
import infinityAgent from 'infinity-agent';
const infinityAgent = require('infinity-agent'); - http.globalAgent
import { globalAgent } from 'infinity-agent/http';const { http, https } = require('infinity-agent'); const infiniteHttpAgent = http.globalAgent; - https.globalAgent
const infiniteHttpsAgent = require('infinity-agent').https;const { http, https } = require('infinity-agent'); const infiniteHttpsAgent = https.globalAgent;
Quickstart
const infinityAgent = require('infinity-agent');
const http = require('http');
const https = require('https');
async function makeRequests() {
console.log('Making 5 concurrent HTTP requests with infinity-agent...');
const httpPromises = Array.from({ length: 5 }, (_, i) => {
return new Promise((resolve, reject) => {
http.get('http://jsonplaceholder.typicode.com/posts/' + (i + 1), { agent: infinityAgent.http.globalAgent }, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
console.log(`HTTP Request ${i + 1} finished (Status: ${res.statusCode})`);
resolve(JSON.parse(data).id);
});
}).on('error', reject);
});
});
console.log('Making 5 concurrent HTTPS requests with infinity-agent...');
const httpsPromises = Array.from({ length: 5 }, (_, i) => {
return new Promise((resolve, reject) => {
https.get('https://jsonplaceholder.typicode.com/comments/' + (i + 1), { agent: infinityAgent.https.globalAgent }, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
console.log(`HTTPS Request ${i + 1} finished (Status: ${res.statusCode})`);
resolve(JSON.parse(data).id);
});
}).on('error', reject);
});
});
try {
const httpResults = await Promise.all(httpPromises);
const httpsResults = await Promise.all(httpsPromises);
console.log('\nAll HTTP requests completed successfully. IDs:', httpResults);
console.log('All HTTPS requests completed successfully. IDs:', httpsResults);
} catch (error) {
console.error('\nOne or more requests failed:', error.message);
}
}
makeRequests();