feat(websocket): add HTTP/HTTPS proxy support
Merged
CI
10mo ago+3637+3637+0123456789012345678901234567890123456789-172-172-012345678901234567890123456789
Summary
Add proxy option to WebSocket constructor for connecting through HTTP CONNECT proxies.
Features
- Support for
ws://andwss://through HTTP proxies - Support for
ws://andwss://through HTTPS proxies (withrejectUnauthorized: false) - Proxy authentication via URL credentials (Basic auth)
- Custom proxy headers support
- Full TLS options (
ca,cert,key, etc.) for target connections usingSSLConfig.fromJS
API
// String format
new WebSocket("wss://example.com", { proxy: "http://proxy:8080" })
// With credentials
new WebSocket("wss://example.com", { proxy: "http://user:pass@proxy:8080" })
// Object format with custom headers
new WebSocket("wss://example.com", {
proxy: { url: "http://proxy:8080", headers: { "X-Custom": "value" } }
})
// HTTPS proxy
new WebSocket("ws://example.com", {
proxy: "https://proxy:8443",
tls: { rejectUnauthorized: false }
})
Implementation
| File | Changes |
|---|---|
WebSocketUpgradeClient.zig | Proxy state machine and CONNECT handling |
WebSocketProxyTunnel.zig | New - TLS tunnel inside CONNECT for wss:// through HTTP proxy |
JSWebSocket.cpp | Parse proxy option and TLS options using SSLConfig.fromJS |
WebSocket.cpp | Pass proxy parameters to Zig, handle HTTPS proxy socket selection |
bun.d.ts | Add proxy and full TLS options to WebSocket types |
Supported Scenarios
| Scenario | Status |
|---|---|
| ws:// through HTTP proxy | ✅ Working |
| wss:// through HTTP proxy | ✅ Working (TLS tunnel) |
| ws:// through HTTPS proxy | ✅ Working (with rejectUnauthorized: false) |
| wss:// through HTTPS proxy | ✅ Working (with rejectUnauthorized: false) |
| Proxy authentication (Basic) | ✅ Working |
| Custom proxy headers | ✅ Working |
| Custom CA for HTTPS proxy | ✅ Working |
Test plan
- API tests verify proxy option is accepted in various formats
- Functional tests with local HTTP CONNECT proxy server
- Proxy authentication tests (Basic auth)
- HTTPS proxy tests with
rejectUnauthorized: false - Error handling tests (auth failures, wrong credentials)
Run tests: bun test test/js/web/websocket/websocket-proxy.test.ts
Changelog
- Added
proxyoption toWebSocketconstructor for HTTP/HTTPS proxy support - Added full TLS options (
ca,cert,key,passphrase, etc.) toWebSocketconstructor
🤖 Generated with Claude Code