@@ -18,8 +18,9 @@ const fetchProgressDecorator = (total, fn) => {
1818}
1919
2020const isFetchSupported = typeof fetch !== 'undefined' ;
21+ const isReadableStreamSupported = isFetchSupported && typeof ReadableStream !== 'undefined' ;
2122
22- const supportsRequestStreams = isFetchSupported && ( ( ) => {
23+ const supportsRequestStream = isReadableStreamSupported && ( ( ) => {
2324 let duplexAccessed = false ;
2425
2526 const hasContentType = new Request ( platform . origin , {
@@ -36,15 +37,26 @@ const supportsRequestStreams = isFetchSupported && (() => {
3637
3738const DEFAULT_CHUNK_SIZE = 64 * 1024 ;
3839
40+ const supportsResponseStream = isReadableStreamSupported && ! ! ( ( ) => {
41+ try {
42+ return utils . isReadableStream ( new Response ( '' ) . body ) ;
43+ } catch ( err ) {
44+ // return undefined
45+ }
46+ } ) ( ) ;
47+
3948const resolvers = {
40- stream : ( res ) => res . body
49+ stream : supportsResponseStream && ( ( res ) => res . body )
4150} ;
4251
43- isFetchSupported && [ 'text' , 'arrayBuffer' , 'blob' , 'formData' ] . forEach ( type => [
44- resolvers [ type ] = utils . isFunction ( Response . prototype [ type ] ) ? ( res ) => res [ type ] ( ) : ( _ , config ) => {
45- throw new AxiosError ( `Response type ${ type } is not supported` , AxiosError . ERR_NOT_SUPPORT , config ) ;
46- }
47- ] )
52+ isFetchSupported && ( ( ( res ) => {
53+ [ 'text' , 'arrayBuffer' , 'blob' , 'formData' , 'stream' ] . forEach ( type => {
54+ ! resolvers [ type ] && ( resolvers [ type ] = utils . isFunction ( res [ type ] ) ? ( res ) => res [ type ] ( ) :
55+ ( _ , config ) => {
56+ throw new AxiosError ( `Response type '${ type } ' is not supported` , AxiosError . ERR_NOT_SUPPORT , config ) ;
57+ } )
58+ } ) ;
59+ } ) ( new Response ) ) ;
4860
4961const getBodyLength = async ( body ) => {
5062 if ( utils . isBlob ( body ) ) {
@@ -74,7 +86,7 @@ const resolveBodyLength = async (headers, body) => {
7486 return length == null ? getBodyLength ( body ) : length ;
7587}
7688
77- export default async ( config ) => {
89+ export default isFetchSupported && ( async ( config ) => {
7890 let {
7991 url,
8092 method,
@@ -106,7 +118,7 @@ export default async (config) => {
106118 }
107119
108120 try {
109- if ( onUploadProgress && supportsRequestStreams && method !== 'get' && method !== 'head' ) {
121+ if ( onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head' ) {
110122 let requestContentLength = await resolveBodyLength ( headers , data ) ;
111123
112124 let _request = new Request ( url , {
@@ -145,7 +157,7 @@ export default async (config) => {
145157
146158 const isStreamResponse = responseType === 'stream' || responseType === 'response' ;
147159
148- if ( onDownloadProgress || isStreamResponse ) {
160+ if ( supportsResponseStream && ( onDownloadProgress || isStreamResponse ) ) {
149161 const options = { } ;
150162
151163 Object . getOwnPropertyNames ( response ) . forEach ( prop => {
@@ -192,6 +204,6 @@ export default async (config) => {
192204
193205 throw AxiosError . from ( err , code , config , request ) ;
194206 }
195- }
207+ } ) ;
196208
197209
0 commit comments