feat(websocket): add HTTP/HTTPS proxy support

Merged
CI
10mo ago
+3637-172

Summary

Add proxy option to WebSocket constructor for connecting through HTTP CONNECT proxies.

Features

  • Support for ws:// and wss:// through HTTP proxies
  • Support for ws:// and wss:// through HTTPS proxies (with rejectUnauthorized: false)
  • Proxy authentication via URL credentials (Basic auth)
  • Custom proxy headers support
  • Full TLS options (ca, cert, key, etc.) for target connections using SSLConfig.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

FileChanges
WebSocketUpgradeClient.zigProxy state machine and CONNECT handling
WebSocketProxyTunnel.zigNew - TLS tunnel inside CONNECT for wss:// through HTTP proxy
JSWebSocket.cppParse proxy option and TLS options using SSLConfig.fromJS
WebSocket.cppPass proxy parameters to Zig, handle HTTPS proxy socket selection
bun.d.tsAdd proxy and full TLS options to WebSocket types

Supported Scenarios

ScenarioStatus
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 proxy option to WebSocket constructor for HTTP/HTTPS proxy support
  • Added full TLS options (ca, cert, key, passphrase, etc.) to WebSocket constructor

🤖 Generated with Claude Code