{"id":16398,"library":"infinity-agent","title":"Node.js HTTP/HTTPS Agent with Infinite Sockets","description":"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.","status":"active","version":"2.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/floatdrop/infinity-agent","tags":["javascript","http","https","agent","maxSockets"],"install":[{"cmd":"npm install infinity-agent","lang":"bash","label":"npm"},{"cmd":"yarn add infinity-agent","lang":"bash","label":"yarn"},{"cmd":"pnpm add infinity-agent","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package primarily targets CommonJS as shown in its usage examples. For ESM, a default import is typical, but Node.js's interoperability layers might handle `require` directly.","wrong":"import infinityAgent from 'infinity-agent';","symbol":"infinityAgent","correct":"const infinityAgent = require('infinity-agent');"},{"note":"The agent instances are exposed as `globalAgent` properties on the `http` and `https` objects exported by the package. Direct named imports from subpaths are not supported.","wrong":"import { globalAgent } from 'infinity-agent/http';","symbol":"http.globalAgent","correct":"const { http, https } = require('infinity-agent');\nconst infiniteHttpAgent = http.globalAgent;"},{"note":"Remember to access the `globalAgent` property, as `infinityAgent.https` itself is the module's wrapper, not the agent instance.","wrong":"const infiniteHttpsAgent = require('infinity-agent').https;","symbol":"https.globalAgent","correct":"const { http, https } = require('infinity-agent');\nconst infiniteHttpsAgent = https.globalAgent;"}],"quickstart":{"code":"const infinityAgent = require('infinity-agent');\nconst http = require('http');\nconst https = require('https');\n\nasync function makeRequests() {\n  console.log('Making 5 concurrent HTTP requests with infinity-agent...');\n  const httpPromises = Array.from({ length: 5 }, (_, i) => {\n    return new Promise((resolve, reject) => {\n      http.get('http://jsonplaceholder.typicode.com/posts/' + (i + 1), { agent: infinityAgent.http.globalAgent }, (res) => {\n        let data = '';\n        res.on('data', (chunk) => (data += chunk));\n        res.on('end', () => {\n          console.log(`HTTP Request ${i + 1} finished (Status: ${res.statusCode})`);\n          resolve(JSON.parse(data).id);\n        });\n      }).on('error', reject);\n    });\n  });\n\n  console.log('Making 5 concurrent HTTPS requests with infinity-agent...');\n  const httpsPromises = Array.from({ length: 5 }, (_, i) => {\n    return new Promise((resolve, reject) => {\n      https.get('https://jsonplaceholder.typicode.com/comments/' + (i + 1), { agent: infinityAgent.https.globalAgent }, (res) => {\n        let data = '';\n        res.on('data', (chunk) => (data += chunk));\n        res.on('end', () => {\n          console.log(`HTTPS Request ${i + 1} finished (Status: ${res.statusCode})`);\n          resolve(JSON.parse(data).id);\n        });\n      }).on('error', reject);\n    });\n  });\n\n  try {\n    const httpResults = await Promise.all(httpPromises);\n    const httpsResults = await Promise.all(httpsPromises);\n    console.log('\\nAll HTTP requests completed successfully. IDs:', httpResults);\n    console.log('All HTTPS requests completed successfully. IDs:', httpsResults);\n  } catch (error) {\n    console.error('\\nOne or more requests failed:', error.message);\n  }\n}\n\nmakeRequests();","lang":"javascript","description":"This quickstart demonstrates how to use `infinity-agent` to perform multiple concurrent HTTP and HTTPS GET requests, leveraging its `maxSockets: Infinity` configuration to manage an unbounded connection pool."},"warnings":[{"fix":"If `keepAlive` is essential, ensure your agent instance is explicitly configured with `keepAlive: true` during instantiation, or consider managing custom agents with specific `maxSockets` values rather than relying solely on `infinity-agent` for full core parity.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Monitor system resources (memory, file descriptors) in high-load environments. Implement rate limiting or backpressure mechanisms at the application level if external services cannot handle the unbounded concurrency, or if system resources become a bottleneck.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure `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;`","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.","error":"TypeError: Cannot read properties of undefined (reading 'globalAgent')"},{"fix":"While `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.","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.","error":"Error: socket hang up"}],"ecosystem":"npm"}