`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.
npm install http-https-agentVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.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.
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');`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.Ensure the argument passed to the agent retrieval function is a valid URL string, including the protocol (e.g., `'https://example.com'`).