Turn a function into an http.Agent instance. Create custom HTTP/HTTPS agents by implementing a connect() function that returns sockets or delegates to other agents.
Create custom `http.Agent` instances by implementing a `connect()` function. This skill helps you build specialized HTTP/HTTPS agents for proxies, custom protocols, or advanced networking scenarios.
The `agent-base` module (v7.1.4) provides an abstract base class for creating custom `http.Agent` implementations. Instead of manually managing socket connections, you define a `connect()` function that creates the underlying socket. The agent handles the rest.
```bash
npm install agent-base
```
When a user asks to create a custom HTTP agent using `agent-base`, follow these steps:
1. **Understand the Use Case**
- Ask what type of connections they need (HTTP, HTTPS, or both)
- Determine if they need proxy support, custom protocols, or special socket handling
- Check if async operations are required
2. **Create the Agent Class**
- Import `Agent` from `agent-base`
- Import necessary Node.js modules (`net`, `tls`, `http`, `https`)
- Create a class that extends `Agent`
3. **Implement the `connect()` Method**
- Accept `req` (request) and `opts` (options) parameters
- Check `opts.secureEndpoint` to determine if HTTPS is being used
- Return appropriate socket type:
- `net.Socket` for HTTP
- `tls.Socket` for HTTPS
- Another `http.Agent` instance to delegate
- Any `Duplex` stream
- Use `async` if asynchronous operations are needed
4. **Configure Agent Options**
- Set `keepAlive: true` to reuse connections
- Configure timeout, maxSockets, and other `http.Agent` options as needed
5. **Usage Pattern**
- Instantiate the agent
- Pass it as the `agent` option to `http.get()`, `http.request()`, or `https.request()`
```typescript
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import { Agent } from 'agent-base';
class MyAgent extends Agent {
connect(req, opts) {
// Use TLS for HTTPS, plain TCP for HTTP
if (opts.secureEndpoint) {
return tls.connect(opts);
} else {
return net.connect(opts);
}
}
}
// Create agent with keep-alive
const agent = new MyAgent({ keepAlive: true });
// Use with HTTP request
http.get('http://nodejs.org/api/', { agent }, (res) => {
console.log('Status:', res.statusCode);
res.pipe(process.stdout);
});
```
```typescript
import { Agent } from 'agent-base';
import * as net from 'net';
class AsyncAgent extends Agent {
async connect(req, opts) {
// Perform async operations (e.g., lookup, authentication)
await someAsyncOperation();
// Return socket after async work completes
return net.connect(opts);
}
}
```
```typescript
import { Agent } from 'agent-base';
import { HttpProxyAgent } from 'http-proxy-agent';
class ConditionalProxyAgent extends Agent {
connect(req, opts) {
// Use proxy for specific domains
if (opts.host.includes('example.com')) {
const proxy = new HttpProxyAgent('http://proxy.corp.com:8080');
return proxy;
}
// Direct connection otherwise
return net.connect(opts);
}
}
```
Users may want to know about existing implementations:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/agent-base-http-agent/raw