-
-
Notifications
You must be signed in to change notification settings - Fork 76
fix(media): use fetch for direct transfers #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import http from 'node:http' | ||
| import https from 'node:https' | ||
| import type { Readable } from 'node:stream' | ||
| import { Readable } from 'node:stream' | ||
|
|
||
| import type { Logger } from '@infra/log/types' | ||
| import { DEFAULT_MEDIA_HOSTS } from '@media/constants' | ||
|
|
@@ -311,6 +311,35 @@ export class WaMediaTransferClient { | |
| init: TransferRequestInit, | ||
| agent: WaProxyAgent | undefined | ||
| ): Promise<InternalTransferResponse> { | ||
| if (!agent && typeof fetch === 'function') { | ||
| const fetchInit = { | ||
| method: init.method ?? 'GET', | ||
| headers: init.headers, | ||
| body: init.body, | ||
| signal: init.signal ?? undefined | ||
| } as Parameters<typeof fetch>[1] & { duplex?: 'half' } | ||
| if (init.body && !(init.body instanceof Uint8Array)) { | ||
| fetchInit.duplex = 'half' | ||
| } | ||
|
|
||
| const response = await fetch(url, fetchInit) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The fetch path follows HTTP redirects by default (redirect: 'follow'), while the retained http/https agent path does not follow redirects. This changes the observable semantics of StreamTransferResponse for 3xx responses: fetch transparently follows the redirect and reports the final status/ok, whereas the old path surfaced the 3xx. It also means the reported Prompt for AI agents |
||
| const headers: Record<string, string> = {} | ||
| response.headers.forEach((value, key) => { | ||
| headers[key] = value | ||
| }) | ||
| const body = response.body ? Readable.fromWeb(response.body) : null | ||
| return { | ||
| status: response.status, | ||
| ok: response.ok, | ||
| headers, | ||
| body, | ||
| // eslint-disable-next-line @typescript-eslint/require-await | ||
| cancel: async () => { | ||
| body?.destroy() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const parsed = new URL(url) | ||
| const transport = parsed.protocol === 'https:' ? https : http | ||
| return new Promise<InternalTransferResponse>((resolve, reject) => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The new fetch path's riskiest behavior—uploading a Node
Readablebody through fetch, which relies on the newly addedduplex: 'half'option inWaMediaTransferClient.httpRequest—has no test coverage. The new fetch test only uploads aUint8Array, and the only Readable-upload test executes through the agent (http) path, so a regression in duplex streaming would pass CI. Add a fetch-path upload test that passes aReadablebody and asserts the round-tripped bytes.Prompt for AI agents