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 🙏

© 2026 – Pkg Stats / Ryan Hefner

encrypted-chat-sdk

v0.1.0

Published

端对端加密通信SDK

Readme

端对端加密通信SDK

一个基于WebSocket的端对端加密通信SDK,为客服系统及多渠道接入提供基础通信能力。

🎯 总体目标

将 chat-room 项目封装成可复用的端对端加密通信SDK,为客服系统及多渠道接入提供基础通信能力。

📁 项目结构

lib/
├── core/           # 核心通信
├── crypto/         # 加密模块
├── session/        # 会话管理
├── adapters/       # 平台适配器
└── utils/          # 工具函数

🔧 技术栈

  • 语言: TypeScript (提供类型安全)
  • 运行时: Node.js + 浏览器双环境支持
  • 加密库: Web Crypto API + node:crypto
  • 测试: Jest + Puppeteer

🧪 测试覆盖

  • 单元测试: 已完成核心模块测试
    • CryptoEngine: 加密引擎单元测试
    • SessionManager: 会话管理单元测试
    • ChatRoomClient: 聊天室适配器单元测试
    • EncryptedChatSDK: SDK核心功能单元测试
  • 集成测试: 已完成关键流程测试
    • 加密通信流程: 密钥交换、加密、解密
    • 会话建立与管理: 创建、获取、删除会话
    • 端到端通信: 模拟完整的加密消息传输流程

🚀 快速开始

# 克隆项目
git clone https://github.com/songquanpeng/chat-room.git
cd encrypted-chat-sdk

# 安装依赖
npm install

# 构建项目
npm run build

📦 核心功能

1. WebSocket通信

  • 连接管理(建立、重连、关闭)
  • 消息序列化/反序列化
  • 心跳机制实现
  • 连接状态管理

2. 端对端加密

  • AES-GCM加密算法
  • ECDH密钥交换
  • 消息认证码(MAC)
  • 防止重放攻击机制

3. 会话管理

  • 会话建立、维持、销毁
  • 多会话并行管理
  • 会话状态持久化

📚 API 使用示例

基本使用

import { EncryptedChatSDK } from 'encrypted-chat-sdk';

const sdk = new EncryptedChatSDK({
  // 连接至 chat-room(Socket.IO)
  serverUrl: 'http://localhost:3000',
  transport: 'socketio',
  roomId: '/demo',
  username: 'alice',
  appId: 'your-app-id'
});

// 连接到服务器
await sdk.connect();

// 创建会话
const sessionId = await sdk.createSession(['user1', 'user2']);

// 发送加密消息
await sdk.sendMessage(sessionId, 'Hello, encrypted world!');

// 断开连接
await sdk.disconnect();

通信与UI解耦示例

使用 CommAPI 可以将通信逻辑与UI逻辑分离,提高代码可维护性:

import { SDKCommAPI, PostMessageCommAPI } from 'encrypted-chat-sdk';
import { EncryptedChatSDK } from 'encrypted-chat-sdk';

// 1. 创建通信API(可以是基于SDK的加密通信,也可以是基于postMessage的页面间通信)
const sdk = new EncryptedChatSDK({
  serverUrl: 'http://localhost:3000',
  transport: 'socketio',
  roomId: '/demo',
  username: 'alice',
  appId: 'your-app-id'
});

// 2. 创建通信API适配器
const commAPI = new SDKCommAPI(sdk);

// 3. 注册消息处理器
commAPI.onMessage((message, from, sessionId) => {
  console.log(`收到来自 ${from} 的消息: ${message}`);
});

// 4. 连接并发送消息
await commAPI.connect();
const sessionId = await commAPI.createSession(['user1', 'user2']);
await commAPI.sendMessage(sessionId, 'Hello via CommAPI!');

跨窗口/iframe通信示例

// 主窗口
import { PostMessageCommAPI } from 'encrypted-chat-sdk';

// 创建基于postMessage的通信API
const commAPI = new PostMessageCommAPI({
  target: iframe.contentWindow,
  origin: '*'
});

// 注册消息处理器
commAPI.onMessage((message) => {
  console.log(`收到iframe消息: ${message}`);
});

// 发送消息到iframe
commAPI.sendMessage('main-session', 'Hello from main window!');

// iframe内
import { PostMessageCommAPI } from 'encrypted-chat-sdk';

// 创建基于postMessage的通信API
const commAPI = new PostMessageCommAPI({
  target: window.parent,
  origin: '*'
});

// 注册消息处理器
commAPI.onMessage((message) => {
  console.log(`收到主窗口消息: ${message}`);
});

// 发送消息到主窗口
commAPI.sendMessage('main-session', 'Hello from iframe!');

📋 开发计划

  • [x] 基础WebSocket通信
  • [x] ECDH密钥交换
  • [x] AES-GCM消息加密
  • [x] 会话管理
  • [x] 多平台适配器
  • [x] 通信API抽象
  • [x] 单元测试
  • [x] 集成测试
  • [ ] 浏览器兼容性测试
  • [ ] 性能优化
  • [ ] 文档完善

📄 许可证

MIT