Skip to content

Commit 89d50ad

Browse files
authored
Merge pull request #12 from bugii/fix/readable-stream-support-check-safari
Fix readable stream as body check on safari
2 parents ed78a0d + 096ae5d commit 89d50ad

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/utils.ts

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

88-
async function detectStreamSupport(): Promise<boolean> {
88+
async function detectStreamSupport() {
8989
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', {
90+
let duplexAccessed = false;
91+
const hasContentType = new Request('https://', {
92+
body: new ReadableStream(),
9893
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
10294
// @ts-ignore
103-
duplex: 'half',
104-
});
95+
get duplex() {
96+
duplexAccessed = true;
97+
return 'half';
98+
},
99+
}).headers.has('Content-Type');
105100

106101
// If fails to handle fetch(Request), it's likely not a native implementation of
107102
// 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 {
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) {
114115
return false;
115116
}
116117
}

0 commit comments

Comments
 (0)