hagent: HTTP Agent for Whistle
hagent is a foundational Node.js module designed to provide HTTP and HTTPS agent functionality specifically tailored for use with Whistle, a cross-platform network debugging and proxy tool. It enables Whistle to manage and proxy network requests at a lower level, handling connection pooling and socket management. Currently at version `0.9.3`, the package has not seen active development since 2020, with its last commit dating back approximately four years. This indicates an abandoned status, meaning it is no longer maintained and may not be compatible with newer Node.js versions or contain unpatched vulnerabilities. Its core functionality revolves around extending Node.js's native `http.Agent` to integrate with Whistle's proxying mechanisms, offering control over persistent connections.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
cause Attempting to use `require()` on a package that is ESM-only, or more likely in this context, attempting to use `import` with a CommonJS-only package like `hagent`.fixEnsure you are using `require()` for this CommonJS-only package: `const { HttpClientAgent } = require('hagent');` -
TypeError: Cannot read property 'request' of undefined (or similar for http/https methods)
cause This error typically indicates that the `agent` object passed to `http.request` or `https.request` is not a valid agent instance, or that the `http` module itself is not correctly imported.fixVerify that `hagent` is correctly installed (`npm install hagent`) and that `HttpClientAgent` or `HttpsClientAgent` are correctly imported and instantiated before being passed to Node.js's native `http.request` or `https.request` methods.
Warnings
- breaking The `hagent` package has been abandoned since April 2020. It is unlikely to be compatible with modern Node.js versions (e.g., Node.js 16+), potentially leading to runtime errors, unexpected behavior, or security vulnerabilities.
- gotcha This package is strictly CommonJS (`require`) and does not provide ES Module (`import`) support. Attempting to use `import { HttpClientAgent } from 'hagent'` will result in a module resolution error.
- gotcha Due to its abandoned status, `hagent` likely contains unpatched security vulnerabilities or bugs. Using it in production environments is highly discouraged as it poses a significant security risk.
Install
-
npm install hagent -
yarn add hagent -
pnpm add hagent
Imports
- HttpClientAgent
import { HttpClientAgent } from 'hagent'; // hagent@0.9.3 is CommonJS-only const HttpClientAgent = require('hagent').HttpClientAgent; // Not strictly wrong, but destructuring is cleanerconst { HttpClientAgent } = require('hagent'); - HttpsClientAgent
import { HttpsClientAgent } from 'hagent'; // hagent@0.9.3 is CommonJS-onlyconst { HttpsClientAgent } = require('hagent'); - hagent (default export)
const hagent = require('hagent'); // Then access hagent.HttpClientAgent or hagent.HttpsClientAgent
Quickstart
const http = require('http');
const { HttpClientAgent } = require('hagent');
// Configure a basic HTTP agent for use with Whistle
const agent = new HttpClientAgent({
// These options are standard for Node.js http.Agent
keepAlive: true,
maxSockets: 10,
maxFreeSockets: 5,
// The 'whistle' option is specific to how hagent integrates
// It typically holds configurations like the Whistle server address
// For demonstration, we'll use a placeholder whistle server address
whistle: {
host: '127.0.0.1',
port: 8899, // Default Whistle proxy port
},
});
const options = {
hostname: 'example.com',
port: 80,
path: '/',
method: 'GET',
agent: agent, // Use the hagent instance
};
const req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
console.log('Response Body Snippet:', rawData.substring(0, 200) + '...');
} catch (e) {
console.error(e.message);
}
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.end();