npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

client-proxy-sdk

v1.0.17

Published

一个基于AnyProxy的客户端代理SDK,用于捕获和处理HTTP响应

Readme

Client Proxy SDK

一个基于 AnyProxy 的客户端代理 SDK,用于捕获和处理 HTTP 响应。支持自动证书安装、智能端口检测和跨平台兼容。

特性

  • 🚀 一键启动: 自动处理证书生成和安装
  • 🔒 HTTPS 支持: 完整的 HTTPS 代理功能,自动证书管理
  • 🎯 精准捕获: 支持自定义 URL 监听规则
  • 🔧 智能配置: 自动检测可用端口,避免冲突
  • 📦 双模块: 同时支持 ES6 import 和 CommonJS require
  • 🎨 事件驱动: 基于 EventEmitter 的响应式 API
  • 🌍 跨平台: 支持 Windows、macOS 和 Linux
  • 🛡️ 智能证书: 自动检测、生成和安装 CA 证书

安装

npm install client-proxy-sdk

使用方法

ES6 Import

import ClientProxySDK from "client-proxy-sdk";

const proxy = new ClientProxySDK({
  port: 8001, // 代理端口
  webPort: 8002, // Web界面端口
  silent: false, // 是否静默模式
  watchUrls: ["api.example.com", "api.test.com"], // 监听的URL
});

// 监听响应
proxy.on("response", (data) => {
  console.log("捕获到响应:", data.url);
  console.log("响应数据:", data.data);
});

// 监听错误
proxy.on("error", (error) => {
  console.error("代理错误:", error);
});

// 启动代理
try {
  const result = await proxy.start();
  console.log("代理启动成功:", result);
} catch (error) {
  console.error("启动失败:", error);
}

CommonJS Require

const ClientProxySDK = require("client-proxy-sdk");

const proxy = new ClientProxySDK({ port: 8001 });
// ... 其他用法相同

API

构造函数

const proxy = new ClientProxySDK(options);

配置选项 (options)

| 参数 | 类型 | 默认值 | 描述 | | ----------------- | -------- | ------ | ------------------- | | port | number | 8001 | 代理服务器端口 | | webPort | number | 8002 | Web 管理界面端口 | | watchUrls | string[] | [] | 需要监听的 URL 列表 | | forceProxyHttps | boolean | true | 是否强制代理 HTTPS | | silent | boolean | false | 是否静默模式 |

方法

start()

启动代理服务器。会自动执行以下流程:

  1. 检查是否已启动
  2. 检查证书是否已安装
  3. 如果证书未安装,自动生成并安装证书
  4. 启动代理服务

返回: Promise

stop()

停止代理服务器

返回: Promise

getStatus()

获取当前状态

返回: Status 对象

checkCert()

检查证书是否已安装

返回: Promise

generateCertificate()

生成 CA 证书文件

返回: Promise - 证书文件路径

quickInstallCert()

快速安装证书(跨平台支持)

返回: Promise<{message: string}>

openCertDirectory()

打开证书目录(用于手动安装)

返回: void

事件

'response'

当捕获到目标响应时触发

回调参数:

  • data.url (string): 请求 URL
  • data.headers (object): 响应头
  • data.data (string): 响应数据

'error'

当发生错误时触发

回调参数:

  • error (Error): 错误对象

证书安装

自动安装

SDK 会在首次启动时自动检测并安装证书:

  • Windows: 使用 certutil 命令自动安装到受信任的根证书颁发机构
  • macOS: 使用 security 命令安装到系统钥匙串
  • Linux: 提示手动安装

手动安装

如果自动安装失败,SDK 会自动打开证书目录,您可以手动安装:

  1. 双击证书文件
  2. 选择"安装证书"
  3. 选择"本地计算机"
  4. 选择"将所有的证书都放入下列存储"
  5. 选择"受信任的根证书颁发机构"

故障排除

端口被占用

SDK 会自动检测端口占用情况,如果默认端口被占用,会自动选择其他可用端口。

证书问题

如果遇到证书相关问题:

  1. 确保以管理员权限运行(Windows)
  2. 检查防火墙设置
  3. 手动安装证书

HTTPS 代理不工作

确保:

  1. 证书已正确安装
  2. forceProxyHttps 选项为 true
  3. 浏览器信任了代理证书

调试

启用详细日志:

const proxy = new ClientProxySDK({
  silent: false, // 启用日志输出
  // ... 其他配置
});

TypeScript 支持

本包包含完整的 TypeScript 类型定义,支持智能提示和类型检查。

许可证

MIT