fix(socket-handler): Fix socket handler race condition by differentiating between async and sync handlers. Now, async socket handlers complete their setup before initial data is emitted, ensuring that no data is lost. Documentation and tests have been updated to reflect this change.

This commit is contained in:
2025-05-29 00:24:57 +00:00
parent d42fa8b1e9
commit e4aade4a9a
11 changed files with 1338 additions and 6 deletions

View File

@ -1,6 +1,6 @@
# SmartProxy Development Plan
## Implementation Plan: Socket Handler Function Support (Simplified)
## Implementation Plan: Socket Handler Function Support (Simplified) ✅ COMPLETED
### Overview
Add support for custom socket handler functions with the simplest possible API - just pass a function that receives the socket.
@ -286,4 +286,31 @@ Keep it simple. The user just wants to handle a socket.
- ✅ The function is called when a connection matches the route
- ✅ Error handling prevents crashes
- ✅ No performance impact on existing routes
- ✅ Clean, simple API that's easy to understand
- ✅ Clean, simple API that's easy to understand
---
## Implementation Notes (Completed)
### What Was Implemented
1. **Type Definitions** - Added 'socket-handler' to TRouteActionType and TSocketHandler type
2. **Route Handler** - Added socket-handler case in RouteConnectionHandler switch statement
3. **Error Handling** - Both sync and async errors are caught and logged
4. **Initial Data Handling** - Initial chunks are re-emitted to handler's listeners
5. **Helper Functions** - Added createSocketHandlerRoute and pre-built handlers (echo, proxy, etc.)
6. **Full Test Coverage** - All test cases pass including async handlers and error handling
### Key Implementation Details
- Socket handlers require initial data from client to trigger routing (not TLS handshake)
- The handler receives the raw socket after route matching
- Both sync and async handlers are supported
- Errors in handlers terminate the connection gracefully
- Helper utilities provide common patterns (echo server, TCP proxy, line protocol)
### Usage Notes
- Clients must send initial data to trigger the handler (even just a newline)
- The socket is passed directly to the handler function
- Handler has complete control over the socket lifecycle
- No special context object needed - keeps it simple
**Total implementation time: ~3 hours**