@@ -85,20 +85,35 @@ function createMultipartStream(entries: readonly MultipartEntry[]): MultipartFor
85
85
} ;
86
86
}
87
87
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
+ }
102
117
}
103
118
104
119
function createFormData ( entries : readonly MultipartEntry [ ] ) : FormData {
@@ -149,7 +164,7 @@ export async function createMultipartRequestInit(
149
164
method : 'POST' | 'PUT' ,
150
165
entries : readonly MultipartEntry [ ] ,
151
166
) : Promise < RequestInit > {
152
- if ( detectStreamSupport ( ) ) {
167
+ if ( await detectStreamSupport ( ) ) {
153
168
const { stream, boundary } = createMultipartStream ( entries ) ;
154
169
155
170
return {
0 commit comments