{"id":29926674,"url":"https://github.com/ecomfe/sse-kit","last_synced_at":"2025-09-15T02:34:00.907Z","repository":{"id":272597429,"uuid":"916480508","full_name":"ecomfe/sse-kit","owner":"ecomfe","description":"A generic server-sent-event handling library with dedicated package for microapp, taro, etc...","archived":false,"fork":false,"pushed_at":"2025-05-15T06:48:29.000Z","size":626,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-25T08:05:22.512Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecomfe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-01-14T07:17:27.000Z","updated_at":"2025-05-20T03:05:50.000Z","dependencies_parsed_at":"2025-01-15T14:23:26.707Z","dependency_job_id":"e367b92f-c817-4923-9d43-1360f53700c7","html_url":"https://github.com/ecomfe/sse-kit","commit_stats":null,"previous_names":["ecomfe/sse-kit"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ecomfe/sse-kit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsse-kit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsse-kit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsse-kit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsse-kit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecomfe","download_url":"https://codeload.github.com/ecomfe/sse-kit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsse-kit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268392187,"owners_count":24243297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-02T12:42:32.669Z","updated_at":"2025-08-02T12:43:11.895Z","avatar_url":"https://github.com/ecomfe.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# SSE-Kit\n\n一个支持多端的 SSE 客户端工具包\n支持 Web、微信小程序、百度小程序\n\n[![npm version](https://img.shields.io/npm/v/sse-kit.svg)](https://www.npmjs.com/package/sse-kit)\n[![license](https://img.shields.io/npm/l/sse-kit.svg)](https://github.com/ecomfe/sse-kit/blob/main/LICENSE)\n[![GitHub](https://img.shields.io/github/stars/ecomfe/sse-kit?style=flat)](https://github.com/ecomfe/sse-kit)\n\n\u003c/div\u003e\n\n\n## ✨ 特性\n- 🌐 多端支持：Web、微信小程序、百度小程序\n- 📦 开箱即用：完整的 TypeScript 类型支持\n- 🛠 功能丰富：支持数据预处理、生命周期回调、请求中断等\n\n## 🚀 快速开始\n\n### 安装\n```bash\nnpm install sse-kit\n# 或者\npnpm add sse-kit\n```\n\n### 基础使用\n```typescript\nimport { SSEProcessor } from 'sse-kit/lib/bundle.h5.esm';  // Web 环境\n// import { SSEProcessor } from 'sse-kit/lib/bundle.weapp.esm';  // 微信小程序\n// import { SSEProcessor } from 'sse-kit/lib/bundle.swan.esm';   // 百度小程序\n\n// 创建 SSE 实例\nconst sse = new SSEProcessor({\n    url: 'https://api.example.com/sse',\n    method: 'POST'\n});\n\n// 接收数据流\nfor await (const data of sse.message()) {\n    console.log('收到消息:', data);\n}\n```\n\n## 📖 详细使用\n\n### 完整配置示例\n```typescript\nconst sse = new SSEProcessor({\n    // 基础配置\n    url: 'https://api.example.com/sse',\n    method: 'POST',\n    reqParams: { userId: '123' },\n    headers: { 'Authorization': 'Bearer token' },\n    \n    // 高级配置\n    timeout: 60000,        // 超时时间：60s\n    enableConsole: true,   // 开启调试日志\n    \n    // 生命周期回调\n    onHeadersReceived: (headers) =\u003e console.log('连接成功'),\n    onComplete: () =\u003e console.log('请求完成'),\n    onError: (err) =\u003e console.error('请求错误', err),\n    \n    // 数据预处理\n    preprocessDataCallback: (data) =\u003e data\n});\n```\n\n### 实例方法\n- `message()`: 获取数据流\n- `getCurrentEventId()`: 获取当前事件 ID\n- `close()`: 中断请求\n\n## ⚠️ 注意事项\n1. 请求超时默认为 60 秒\n2. 百度小程序环境下可能出现中文乱码，建议使用 base64 编码传输数据\n3. 开启 `enableConsole: true` 可以查看详细日志，便于调试\n\n## 🔧 本地开发\n\n### 启动开发服务\n```bash\n# 安装依赖\npnpm install\n\n# 启动开发环境（选择一个）\npnpm dev:h5     # Web 环境\npnpm dev:weapp  # 微信小程序\npnpm dev:swan   # 百度小程序\n\n# 启动本地测试服务\npnpm dev:server\n```\n\n### 目录结构\n```\n├─lib\n├── bundle.h5.cjs.js\n├── bundle.h5.esm.js\n├── bundle.swan.cjs.js\n├── bundle.swan.esm.js\n├── bundle.weapp.cjs.js\n└── bundle.weapp.esm.js\n```\n\n### 使用\n```\n// 按照当前平台引入 SDK；\nimport { SSEProcessor } from 'sse-kit/lib/xxx';\n\n// 实例化 SSE 请求；\nconst sseInstance = new SSEProcessor({\n    url: 'xxx',\n    method: \"POST\",\n    reqParams: {},\n    onHeadersReceived: () =\u003e {\n        // 请求连接建立成功，收到 response header；\n    },\n    onComplete: () =\u003e {\n        // 请求完成；\n    },\n    onError: () =\u003e {\n        // 请求出错；\n    },\n})；\n\n// 获取 SSE 请求数据；\nfor await (const chunk of sseInstance.message()) {\n   console.info('获取到新的 chunk----------:', chunk);\n}\n```\n\n## 开发\n\n```\n$ pnpm install\n\n// 启动指定平台的 demo 项目，默认启动对应 server 服务，对 sdk 进行构建并 watch 改动；\n$ pnpm dev:h5/weapp/swan \n\n// 本地调试 \n$ pnpm run dev:sdk\n\n// 构建 sse-kit SDK\n$ pnpm run build:sdk\n\n```\n\n### local-service\n\u003e 用于本地调试的后端服务；\n\n```\n// 启动本地用于调试的后端服务；\n$ pnpm run dev:server\n\n// 确认服务是否启动成功；\n$ curl -X POST http://localhost:3000/stream/numbers -H \"Content-Type: application/json\" -d '{}'\n\n```\n\n### taro-demo\n\u003e 用于本地调试的 Taro 项目\n```\n$ pnpm dev:h5/weapp/swan \n```\n- 构建结果在 `taro-demo/dist/*` 目录下，不同端会创建对应的子目录；\n- 本地调试时，需要配置 `local-service` 作为后端服务；\n\n### sse-kit-sdk\n```\n// 本地调试 \n$ pnpm run dev:sdk\n\n// 构建产物\n$ pnpm run build:sdk\n```\n#### 多端支持\n- 当前支持 web、小程序（微信/百度），在`config/const.ts`中配置；\n- 构建时，会根据平台标识自动构建对应平台的产物；\n- 编写针对制定端平台的代码时，需要通过文件命名标识，例如：`index.weapp.ts`；","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fsse-kit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecomfe%2Fsse-kit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fsse-kit/lists"}