{"id":17257,"library":"http-https-agent","title":"HTTP/HTTPS Agent Resolver","description":"`http-https-agent` is a small, focused utility package designed to simplify the selection of appropriate Node.js HTTP or HTTPS agents based on a given URL's protocol. It provides a single factory function that, when invoked with agent options (like `keepAlive`), returns another function capable of dynamically providing either an `http.Agent` or `https.Agent` instance. The package is currently at version 1.0.2. Due to its minimal scope and lack of recent updates (last published several years ago), it appears to be in a maintenance-only state. Its primary differentiator is consolidating agent management for dual-protocol scenarios, avoiding manual `if/else` checks for agent instantiation. It internally relies on `lodash.startswith` for protocol detection.","status":"maintenance","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/kesla/http-https-agent","tags":["javascript"],"install":[{"cmd":"npm install http-https-agent","lang":"bash","label":"npm"},{"cmd":"yarn add http-https-agent","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-https-agent","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally for protocol detection on the input URL string.","package":"lodash.startswith","optional":false}],"imports":[{"note":"The package uses a default export. For CommonJS, `require('http-https-agent')` directly returns the agent factory function. The 'wrong' entry is included because developers sometimes incorrectly assume named exports or try `require().default` in mixed environments.","wrong":"const httpHttpsAgent = require('http-https-agent');","symbol":"httpHttpsAgent","correct":"import httpHttpsAgent from 'http-https-agent';"}],"quickstart":{"code":"import httpHttpsAgent from 'http-https-agent';\nimport https from 'https';\nimport http from 'http';\n\n// Create an agent factory with global options, e.g., keepAlive\nconst getAgent = httpHttpsAgent({\n  keepAlive: true,\n  maxSockets: 10,\n  timeout: 60000 // 60 seconds\n});\n\n// Get an HTTPS agent for an HTTPS URL\nconst httpsUrl = 'https://jsonplaceholder.typicode.com/posts/1';\nconst myHttpsAgent = getAgent(httpsUrl);\nconsole.log(`HTTPS Agent created for ${httpsUrl}:`, myHttpsAgent instanceof https.Agent); // Should be true\n\n// Get an HTTP agent for an HTTP URL\nconst httpUrl = 'http://example.com'; // Note: Many public APIs enforce HTTPS\nconst myHttpAgent = getAgent(httpUrl);\nconsole.log(`HTTP Agent created for ${httpUrl}:`, myHttpAgent instanceof http.Agent); // Should be true\n\n// Example of using the agent in a request (simplified for demonstration)\nhttps.get({ hostname: 'jsonplaceholder.typicode.com', path: '/posts/1', agent: myHttpsAgent }, (res) => {\n  console.log('HTTPS Request status:', res.statusCode);\n  res.resume();\n}).on('error', (e) => {\n  console.error('HTTPS Request error:', e.message);\n});\n","lang":"javascript","description":"Demonstrates how to initialize the agent factory with options and then use the returned function to dynamically obtain an HTTP or HTTPS agent based on the provided URL, confirming their types."},"warnings":[{"fix":"Review the simplicity of the logic and consider implementing a custom solution or a more actively maintained alternative if advanced features or the latest Node.js compatibility are critical.","message":"The package has not been updated in several years (last version 1.0.2). While its core functionality is based on stable Node.js APIs (http.Agent, https.Agent), it might not leverage recent performance improvements, security patches, or new features in Node.js core or related ecosystem libraries.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Create a declaration file (e.g., 'http-https-agent.d.ts') with `declare module \"http-https-agent\" { function httpHttpsAgent(options?: any): (url: string) => import('http').Agent | import('https').Agent; export default httpHttpsAgent; }` or use `@ts-ignore` for quick fixes.","message":"The library does not provide TypeScript declaration files. Users working in TypeScript projects will need to provide their own type definitions (e.g., in a 'd.ts' file) to avoid compilation errors and benefit from type checking.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"This is an internal dependency of the package; no direct fix is available to the user other than considering alternative libraries if bundle size is a critical concern, or forking the library to remove the dependency.","message":"The package includes `lodash.startswith` as a dependency. In modern Node.js environments (Node.js 6+), `String.prototype.startsWith` is natively available, making the Lodash dependency potentially unnecessary and adding minor overhead to the bundle size if tree-shaking isn't fully effective.","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":"The module exports a factory function. First, call the factory with options (e.g., `{ keepAlive: true }`), then use the returned function to get an agent. Correct: `const getAgent = httpHttpsAgent({ keepAlive: true }); const agent = getAgent('https://example.com');`","cause":"Attempting to call the module's default export directly before it has been initialized with options, or incorrectly assuming `require()` directly returns the agent.","error":"TypeError: getAgent is not a function"},{"fix":"Ensure the package is installed: `npm install http-https-agent`. Verify the import statement: `import httpHttpsAgent from \"http-https-agent\";` for ESM or `const httpHttpsAgent = require(\"http-https-agent\");` for CommonJS.","cause":"The package is not installed or the import path is incorrect, or a module resolution issue in a mixed ESM/CJS project.","error":"ERR_MODULE_NOT_FOUND or Cannot find module 'http-https-agent'"},{"fix":"Ensure the argument passed to the agent retrieval function is a valid URL string, including the protocol (e.g., `'https://example.com'`).","cause":"Providing a non-string or malformed URL to the agent retrieval function.","error":"TypeError: Parameter 'url' must be a string."}],"ecosystem":"npm","meta_description":null}