fix: use a node-compatible proxy agent for baileys media upload - #2734
Open
maonamassaaceleradora wants to merge 1 commit into
Conversation
Baileys 7.0.0-rc13 performs media uploads with Node's native http/https modules instead of fetch/Undici, and passes the fetchAgent option straight to http.request as its `agent`. An Undici ProxyAgent does not implement Node's Agent interface, so every media upload fails as soon as an instance has a proxy configured. The failure is easy to misdiagnose because text messages keep working: they go through the websocket, which already uses the Node agent. Only uploads break, with PTT audio returning "400 Media upload failed on all hosts" and video uploads timing out. Build fetchAgent with makeProxyAgent() so both the websocket and the media upload path get a Node-compatible agent.
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates Baileys proxy configuration so Sequence diagram for proxied Baileys media uploadsequenceDiagram
participant Client
participant Baileys as Baileys
participant HTTP as Node_http_https
participant Proxy as Proxy
participant MediaHost
Client->>Baileys: sendMedia
Baileys->>HTTP: http.request(fetchAgent)
HTTP->>Proxy: upload media
Proxy->>MediaHost: forward upload
MediaHost-->>Proxy: upload response
Proxy-->>HTTP: response
HTTP-->>Baileys: upload result
Baileys-->>Client: media message sent
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. If the replacement agent is incompatible, WhatsApp media uploads may fail and can be fixed by reverting or retrying them. If it routes requests incorrectly, media could be sent through an unintended network path; that exposure would not be undone by a revert, though its scope is bounded.
This branch has not been deployed
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.
📋 Description
Every media upload fails when an instance has a proxy enabled.
createClient()builds the Baileys socket options with two different agents:makeProxyAgentUndici()returns an UndiciProxyAgent, which was the right choice while Baileys performed its media uploads throughfetch. Baileys7.0.0-rc13no longer does: on the Node runtime it uploads media with Node's nativehttps/httpmodules, and passes thefetchAgentoption straight through tohttp.request()as itsagent.An Undici
ProxyAgentdoes not implement Node'shttp.Agentinterface, so the upload request never gets a usable agent and fails against every media host.Why this is easy to misdiagnose: sending text keeps working perfectly. Text goes over the websocket, which uses the
agentoption — already a Node-compatible agent. Only the upload path is broken, and it fails differently per media type:400 Media upload failed on all hostsSo the instance looks healthy, connects fine, and sends messages — only media silently breaks, and only when a proxy is configured.
The fix builds
fetchAgentwithmakeProxyAgent()as well, so both the websocket and the media upload path receive a Node-compatible agent.🔗 Related Issue
No issue filed; diagnosed on one of our production instances after upgrading to the Baileys
rc13line.🧪 Type of Change
🧪 Testing
Verified on one of our production instances with a proxy configured:
/message/sendWhatsAppAudioreturned400 Media upload failed on all hosts, video uploads timed out, while/message/sendTextworked normally.npm run lint:checkandnpx tsc --noEmitboth pass with no errors.✅ Checklist
📝 Additional Notes
This change leaves
makeProxyAgentUndici()insrc/utils/makeProxyAgent.tsexported but no longer referenced anywhere. I deliberately kept it rather than deleting it in this PR, to keep the diff focused on the fix — happy to remove it in this PR or a follow-up if you would prefer that.Worth noting for anyone tracking the Baileys upgrade: this is a behavioural change on the Baileys side, not a regression introduced here. The
fetchAgentoption kept its name but changed what it is handed to.🤖 Generated with Claude Code
Summary by Sourcery
Restore Baileys media uploads when proxy support is enabled.
Bug Fixes:
Tests: