This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a multi-platform SSE (Server-Sent Events) client toolkit supporting Web, WeChat Mini Programs, and Baidu Smart Mini Programs. The project uses a monorepo structure with pnpm workspaces and Turbo for build orchestration.
packages/
├── sse-kit/ # Main SSE client library
├── local-service/ # NestJS development server for testing
└── taro-demo/ # Taro demo application for testing
All commands should be run from the repository root using pnpm.
pnpm install- Install all dependenciespnpm dev:sdk- Start SSE kit development mode with hot reloadpnpm build:sdk- Build the SSE kit library
pnpm dev:h5- Web/H5 development (includes SDK and test server)pnpm dev:weapp- WeChat Mini Program developmentpnpm dev:swan- Baidu Smart Mini Program developmentpnpm dev:server- Start the NestJS test server only
pnpm test- Run tests in local-service package- Test server endpoint:
curl -X POST http://localhost:3000/stream/numbers -H "Content-Type: application/json" -d '{}'
pnpm check-types- Run TypeScript type checking across all packages
The SSE kit uses a custom build system that creates platform-specific bundles:
Platforms are defined in packages/sse-kit/config/const.js:
h5- Web browsersweapp- WeChat Mini Programsswan- Baidu Smart Mini Programsrn- React Native
Use file naming conventions for platform-specific implementations:
index.ts- Default implementationindex.weapp.ts- WeChat Mini Program specificindex.swan.ts- Baidu Smart Mini Program specificindex.rn.ts- React Native specific
Each platform generates both CJS and ESM bundles in packages/sse-kit/lib/:
bundle.{platform}.cjs.jsbundle.{platform}.esm.js- Corresponding
.d.tsfiles
Main class in packages/sse-kit/src/models/SSEProcessor.ts:
- Implements async iterator pattern via
message()method - Supports request timeout (default 60s)
- Platform-agnostic streaming via
requestStreamingutilities - Includes abort/close functionality
- Multi-platform networking: Different implementations for web vs mini-programs
- Data preprocessing: Configurable data transformation callbacks
- Lifecycle hooks: onHeadersReceived, onComplete, onError callbacks
- Request timeout: Configurable timeout with abort handling
- Console logging: Debug logging that can be enabled/disabled
Located in packages/sse-kit/src/utils/requestStreaming/:
- Platform-specific HTTP streaming implementations
- Handles differences between fetch API, XMLHttpRequest, and mini-program APIs
- Manages chunk processing and data parsing
- Baidu Smart Mini Program may have Chinese character encoding issues - use base64 when needed
- Each platform has different networking APIs requiring separate implementations
- The build system uses Rollup with custom Babel plugins for platform-specific bundling
- Set
enableConsole: truein SSEProcessor options for detailed logging - Use the local-service package to test streaming endpoints during development
The repository is currently on branch fix_abortError working on abort error handling improvements.
Key Issues Addressed:
- Error Swallowing: Fixed critical issue in encodeBuffer.ts where original parsing errors were being masked
- Resource Leaks: Implemented proper ReadableStream reader cleanup to prevent SSE connection resource leaks during abort operations
- Request Duplication: Added deduplication mechanisms to prevent browser auto-retry causing backend to receive duplicate requests during 10s stalled scenarios
- Code Structure: Optimized codebase while maintaining 100% API compatibility and multi-platform build architecture
Major Changes:
- Created
common.tsutilities for shared functionality across platforms while preserving platform-specific build separation - Fixed error propagation in
encodeBuffer.ts- now returns undefined instead of throwing masked JSON errors - Added proper resource cleanup for streaming readers in web platform implementation
- Implemented RequestState class for consistent state management across platforms
- Converted all Chinese comments and strings to English for GitHub publication
Files Modified:
src/utils/requestStreaming/common.ts(NEW) - Shared utilities and RequestState managementsrc/utils/requestStreaming/index.ts- Enhanced with proper reader cleanup and error handlingsrc/utils/encodeBuffer.ts- Fixed error masking, updated to English commentssrc/models/SSEProcessor.ts- Updated error messages and comments to Englishconfig/const.js,loaders/index.js- Converted build system comments to English- All utility files - Converted JSDoc comments from Chinese to English
Build Verification: All platform bundles (weapp, swan, h5, rn) build successfully with no TypeScript errors. The multi-platform architecture is preserved while code maintainability is significantly improved.