Commit a43ba0e
committed
Refactor socksclient.ts into modular protocol handlers
Split the large socksclient.ts file (1213 lines) into smaller, more
maintainable modules to improve code organization and readability.
## New Structure
```
src/client/
├── socksclient.ts (1072 lines, -141 lines)
├── protocols/
│ └── socks4-handler.ts (145 lines)
└── udp/
└── udp-frame.ts (140 lines)
```
## Changes Made
**1. Extracted UDP Frame Utilities (udp/udp-frame.ts)**
- createUDPFrame() - SOCKS5 UDP frame construction
- parseUDPFrame() - SOCKS5 UDP frame parsing
- Full JSDoc documentation preserved
- Used by both static methods and internally
**2. Extracted SOCKS4 Protocol Handler (protocols/socks4-handler.ts)**
- Socks4Handler class with dependency injection pattern
- sendInitialHandshake() - SOCKS4/4a handshake
- handleFinalHandshakeResponse() - Connection/bind response
- handleIncomingConnectionResponse() - BIND incoming connection
- Clean separation while maintaining access to client state
**3. Refactored Main SocksClient**
- Static UDP methods now delegate to extracted functions
- SOCKS4 methods delegate to Socks4Handler instance
- Added clear section comments for remaining SOCKS5 code
- Reduced from 1213 to 1072 lines (11.6% reduction)
- Removed unused imports (Socks4Response, ipv4ToInt32)
## Benefits
✅ Improved Maintainability - Smaller, focused files
✅ Better Code Organization - Protocol logic separated
✅ Easier Navigation - Clear module boundaries
✅ 100% Backward Compatible - No API changes
✅ Fully Tested - All 65 tests passing
✅ Type Safe - All type definitions generated
## Testing
- ✅ All 65 tests passing
- ✅ TypeScript compilation successful
- ✅ Type definitions generated correctly
- ✅ No breaking changes to public API
SOCKS5 remains in the main file due to complex async authentication
flows and deep state coupling - extracted to the extent practical
while maintaining code clarity.1 parent 73d5f6b commit a43ba0e
File tree
6 files changed
+421
-179
lines changed- src/client
- protocols
- udp
- typings/client
- protocols
- udp
6 files changed
+421
-179
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
0 commit comments