Skip to content

Commit 8f17913

Browse files
committed
add fetch call again to detect non-native 'fetch' envs and remove
data url in the support check
1 parent 7b72ec6 commit 8f17913

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/utils.ts

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

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;
88+
async function detectStreamSupport() {
89+
try {
90+
let duplexAccessed = false;
91+
const hasContentType = new Request('https://', {
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+
// If fails to handle fetch(Request), it's likely not a native implementation of
102+
// Fetch API, so it's not supported.
103+
const res = await fetch(
104+
new Request('data:text/plain;charset=utf-8,42', {
105+
method: 'POST',
106+
body: new ReadableStream(),
107+
// @ts-ignore
108+
duplex: 'half',
109+
}),
110+
);
111+
const body = await res.text();
112+
113+
return duplexAccessed && !hasContentType && body === '\x00';
114+
} catch (error) {
115+
return false;
116+
}
102117
}
103118

104119
function createFormData(entries: readonly MultipartEntry[]): FormData {
@@ -149,7 +164,7 @@ export async function createMultipartRequestInit(
149164
method: 'POST' | 'PUT',
150165
entries: readonly MultipartEntry[],
151166
): Promise<RequestInit> {
152-
if (detectStreamSupport()) {
167+
if (await detectStreamSupport()) {
153168
const { stream, boundary } = createMultipartStream(entries);
154169

155170
return {

0 commit comments

Comments
 (0)