Skip to content

Commit 7b72ec6

Browse files
committed
fix readable stream as body check on safari
1 parent 9da0aec commit 7b72ec6

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/utils.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,20 @@ function createMultipartStream(entries: readonly MultipartEntry[]): MultipartFor
8585
};
8686
}
8787

88-
async function detectStreamSupport(): Promise<boolean> {
89-
try {
90-
const testStream = new ReadableStream({
91-
start(controller) {
92-
controller.enqueue(new Uint8Array([0])); // Minimal stream chunk
93-
controller.close();
94-
},
95-
});
96-
97-
const request = new Request('data:text/plain;charset=utf-8,42', {
98-
method: 'POST',
99-
body: testStream,
100-
// Required for streaming request body in some browsers,
101-
// or it will fail and assume it's not supported
102-
// @ts-ignore
103-
duplex: 'half',
104-
});
105-
106-
// If fails to handle fetch(Request), it's likely not a native implementation of
107-
// Fetch API, so it's not supported.
108-
await fetch(request.clone());
109-
110-
const body = await request.text();
111-
// if different from '\x00', it's likely not supported
112-
return body === '\x00';
113-
} catch {
114-
return false;
115-
}
88+
function detectStreamSupport() {
89+
let duplexAccessed = false;
90+
91+
const hasContentType = new Request('data:text/plain;charset=utf-8,42', {
92+
body: new ReadableStream(),
93+
method: 'POST',
94+
// @ts-ignore
95+
get duplex() {
96+
duplexAccessed = true;
97+
return 'half';
98+
},
99+
}).headers.has('Content-Type');
100+
101+
return duplexAccessed && !hasContentType;
116102
}
117103

118104
function createFormData(entries: readonly MultipartEntry[]): FormData {
@@ -163,7 +149,7 @@ export async function createMultipartRequestInit(
163149
method: 'POST' | 'PUT',
164150
entries: readonly MultipartEntry[],
165151
): Promise<RequestInit> {
166-
if (await detectStreamSupport()) {
152+
if (detectStreamSupport()) {
167153
const { stream, boundary } = createMultipartStream(entries);
168154

169155
return {

0 commit comments

Comments
 (0)