feat: allow custom http agent for streaming websocket connections#504
Open
valeriudev wants to merge 1 commit into
Open
feat: allow custom http agent for streaming websocket connections#504valeriudev wants to merge 1 commit into
valeriudev wants to merge 1 commit into
Conversation
Closes deepgram#493. Streaming WebSocket connections (listen, speak, agent) now accept an optional Node http/https `agent`, so traffic can be routed through an HTTP/HTTPS proxy such as HttpsProxyAgent. Set `agent` on the client to apply it to every connection, or pass it to connect() / createConnection() to override the client-level default. Node-only; ignored in browser and web-worker runtimes, which use the native WebSocket and cannot accept a custom agent. The agent is injected by wrapping the ws class that ReconnectingWebSocket instantiates, so no Fern-generated code is patched. Adds unit coverage for the injection and override behavior, plus a self-contained manual smoke test (tests/manual/ws-proxy-smoke.ts) that opens a real Deepgram connection through a local CONNECT proxy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Closes #493.
Streaming WebSocket connections (
listen.v1/listen.v2,speak.v1,agent.v1) can now be routed through a custom Nodehttp/httpsagent — e.g. anHttpsProxyAgent— so streaming traffic transits an HTTP/HTTPS egress proxy, exactly like the issue describes for proxy-controlled environments (Kubernetes egress proxies, enterprise networks, regional gateways).Before this, only REST honored a custom fetcher; the WebSocket upgrade went straight out via
wswith no way to inject an agent. This implements the issue author's preferred option (forwardagenton the connect args), delivered through theWebSocket?: anyslot they identified as "the perfect injection point":agentoption on the client (new DeepgramClient({ apiKey, agent })) applies to every streaming connection.agentonconnect()/createConnection()overrides the client-level default.wssubclass intoReconnectingWebSocket'sWebSocketslot, which addsagentto the underlyingnew ws.WebSocket(url, protocols, { ...opts, agent })call. No Fern-generated code is patched, so it survives regenerations, and reconnects continue to use the proxy (the original failure mode was a tightgetaddrinfo EAI_AGAINretry loop when in-cluster DNS stuttered — with the proxy resolving the host, that's bypassed).The option is Node-only (ignored in browser/web-worker runtimes, which use the native
WebSocketthat cannot accept an agent), excluded from query params, and ignored when atransportFactoryis supplied (custom transports own their own networking).Types of changes
Checklist
Further comments
Why this approach. The issue listed three options. I implemented option 1 (explicit
agent) because it keeps the public surface tiny and is symmetric with theagentoptionwsalready accepts. I deliberately did not implement option 3 (auto-honoringHTTPS_PROXY/HTTP_PROXY): the SDK's REST layer uses barefetchand does not auto-honor proxy env vars either, so there's no existing parity to match, and adding it would pull ~4 runtime deps into a currently single-dependency (ws) SDK plus force a Node-18 version pin onhttps-proxy-agent. Happy to follow up with an opt-inproxy: \"env\"flag in a separate PR if maintainers want it (and want to weigh whether REST should get symmetric treatment).Testing. Added unit coverage for agent injection, the client-vs-per-connection override, cross-service wiring, and query-param exclusion. Also added a dependency-free manual smoke test (
tests/manual/ws-proxy-smoke.ts) that stands up a local CONNECT proxy and opens a real Deepgramlistensocket through it — verified end-to-end that the WebSocket opens and the traffic transits the proxy.make build,make test(552 + 132), andmake lintare green.