360 Secure SkillHub
360 安全技能中心

Create custom http.Agent.

agent-base是创建自定义http.Agent的工具,核心基于抽象类封装,通过实现connect()方法创建底层Socket或委托代理,支持异步函数,可区分HTTP/HTTPS。典型场景包括开发各类代理、自定义HTTP连接逻辑,若有成熟方案会优先推荐社区子类,还提供可运行的TS/JS示例。

openlark
openlark
热度 27.98
最近更新 2026-05-16 10:16:47
分类:开发依赖:纯提示词应用场景:代码生成 / API 集成

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add agent-base
Metadata
name
agent-base
description
Create custom http.Agent.

agent-base — Custom HTTP Agent Helper

Helps users create custom http.Agent subclasses based on the agent-base module, for scenarios such as HTTP request proxying, custom socket connections, etc.

Trigger Scenarios

Use when the user needs to write HTTP/HTTPS/SOCKS/PAC proxies, custom HTTP connection logic, or extend http.Agent.

Module Overview

agent-base wraps an ordinary function into an http.Agent instance. It is an abstract class that requires defining the connect(req, opts) method to create the underlying Socket.

  • connect() can return any Duplex stream, or another http.Agent instance to delegate the request
  • connect() can be an async function
  • opts.secureEndpoint is used to distinguish between HTTP / HTTPS

Quick Start

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) {
    if (opts.secureEndpoint) {
      return tls.connect(opts);
    } else {
      return net.connect(opts);
    }
  }
}

const agent = new MyAgent({ keepAlive: true });
http.get('http://nodejs.org/api/', { agent }, (res) => {
  console.log('"response" event!', res.headers);
  res.pipe(process.stdout);
});

Workflow

  1. Identify user requirements: Pure HTTP/HTTPS proxy? SOCKS? PAC? Or custom routing?
  2. If a mature solution exists, prioritize recommending community subclasses (see below)
  3. If custom implementation is needed, write the connect() method based on the Agent abstract class
  4. Provide complete, runnable TypeScript/JavaScript examples

Proxy Details

See references/proxy-details.md, which includes:

  • http-proxy-agent — HTTP endpoint proxy
  • https-proxy-agent — HTTPS endpoint proxy
  • pac-proxy-agent — PAC file proxy
  • socks-proxy-agent — SOCKS proxy
  • Selection guide and comparison of each subclass