Skip to content

Commit cc9fa00

Browse files
authored
fix(fetch-http-handler): set duplex on request when body present (#1279)
* fix(fetch-http-handler): set duplex on request when body present * comment about duplex half
1 parent 534128e commit cc9fa00

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

.changeset/tiny-eyes-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/fetch-http-handler": patch
3+
---
4+
5+
set duplex on fetch options

packages/fetch-http-handler/src/fetch-http-handler.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ export const keepAliveSupport = {
1919
supported: Boolean(typeof Request !== "undefined" && "keepalive" in new Request("https://[::1]")),
2020
};
2121

22+
/**
23+
* @internal
24+
*/
25+
type AdditionalRequestParameters = {
26+
// This is required in Node.js when Request has a body, and does nothing in the browser.
27+
// Duplex: half means the request is fully transmitted before attempting to process the response.
28+
// As of writing this is the only accepted value in https://fetch.spec.whatwg.org/.
29+
duplex?: "half";
30+
};
31+
2232
/**
2333
* @public
2434
*
@@ -91,16 +101,23 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerConfig> {
91101
// Request constructor doesn't allow GET/HEAD request with body
92102
// ref: https://github.com/whatwg/fetch/issues/551
93103
const body = method === "GET" || method === "HEAD" ? undefined : request.body;
94-
const requestOptions: RequestInit = { body, headers: new Headers(request.headers), method: method };
104+
const requestOptions: RequestInit & AdditionalRequestParameters = {
105+
body,
106+
headers: new Headers(request.headers),
107+
method: method,
108+
};
109+
if (body) {
110+
requestOptions.duplex = "half";
111+
}
95112

96113
// some browsers support abort signal
97114
if (typeof AbortController !== "undefined") {
98-
(requestOptions as any)["signal"] = abortSignal;
115+
requestOptions.signal = abortSignal as AbortSignal;
99116
}
100117

101118
// some browsers support keepalive
102119
if (keepAliveSupport.supported) {
103-
(requestOptions as any)["keepalive"] = keepAlive;
120+
requestOptions.keepalive = keepAlive;
104121
}
105122

106123
const fetchRequest = new Request(url, requestOptions);

0 commit comments

Comments
 (0)