Registry / http-networking / http-https-agent

http-https-agent

JSON →
library1.0.2jsnpmunverified

`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-agent
INSTALL
IMPORT
SIG · HTTP-HTTPS-AGENT
H
http-https-agent
http-networkingjavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

httpHttpsAgent
import httpHttpsAgent from 'http-https-agent';
const httpHttpsAgent = require('http-https-agent');
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.

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.

import httpHttpsAgent from 'http-https-agent'; import https from 'https'; import http from 'http'; // Create an agent factory with global options, e.g., keepAlive const getAgent = httpHttpsAgent({ keepAlive: true, maxSockets: 10, timeout: 60000 // 60 seconds }); // Get an HTTPS agent for an HTTPS URL const httpsUrl = 'https://jsonplaceholder.typicode.com/posts/1'; const myHttpsAgent = getAgent(httpsUrl); console.log(`HTTPS Agent created for ${httpsUrl}:`, myHttpsAgent instanceof https.Agent); // Should be true // Get an HTTP agent for an HTTP URL const httpUrl = 'http://example.com'; // Note: Many public APIs enforce HTTPS const myHttpAgent = getAgent(httpUrl); console.log(`HTTP Agent created for ${httpUrl}:`, myHttpAgent instanceof http.Agent); // Should be true // Example of using the agent in a request (simplified for demonstration) https.get({ hostname: 'jsonplaceholder.typicode.com', path: '/posts/1', agent: myHttpsAgent }, (res) => { console.log('HTTPS Request status:', res.statusCode); res.resume(); }).on('error', (e) => { console.error('HTTPS Request error:', e.message); });
Debug
Known issues
gotchaThe 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.
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.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: getAgent is not a function
Attempting to call the module's default export directly before it has been initialized with options, or incorrectly assuming `require()` directly returns the agent.
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');`
ERR_MODULE_NOT_FOUND or Cannot find module 'http-https-agent'
The package is not installed or the import path is incorrect, or a module resolution issue in a mixed ESM/CJS project.
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.
TypeError: Parameter 'url' must be a string.
Providing a non-string or malformed URL to the agent retrieval function.
fix
Ensure the argument passed to the agent retrieval function is a valid URL string, including the protocol (e.g., `'https://example.com'`).
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
lodash.startswithrequiredUsed internally for protocol detection on the input URL string.
Agent activity
6 hits · last 30 days
node
6
Resources
http-https-agent — npm install http-https-agent · libregistry