Skip to content

Commit d1d359d

Browse files
fix(fetch): fix cases when ReadableStream or Response.body are not available; (#6377)
1 parent 8e4314b commit d1d359d

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

lib/adapters/fetch.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const fetchProgressDecorator = (total, fn) => {
1818
}
1919

2020
const 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

3738
const 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+
3948
const 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

4961
const 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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,4 @@
216216
"@commitlint/config-conventional"
217217
]
218218
}
219-
}
219+
}

0 commit comments

Comments
 (0)