mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(fetch): fix cases when ReadableStream or Response.body are not available; (#6377)
This commit is contained in:
+23
-11
@@ -18,8 +18,9 @@ const fetchProgressDecorator = (total, fn) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isFetchSupported = typeof fetch !== 'undefined';
|
const isFetchSupported = typeof fetch !== 'undefined';
|
||||||
|
const isReadableStreamSupported = isFetchSupported && typeof ReadableStream !== 'undefined';
|
||||||
|
|
||||||
const supportsRequestStreams = isFetchSupported && (() => {
|
const supportsRequestStream = isReadableStreamSupported && (() => {
|
||||||
let duplexAccessed = false;
|
let duplexAccessed = false;
|
||||||
|
|
||||||
const hasContentType = new Request(platform.origin, {
|
const hasContentType = new Request(platform.origin, {
|
||||||
@@ -36,15 +37,26 @@ const supportsRequestStreams = isFetchSupported && (() => {
|
|||||||
|
|
||||||
const DEFAULT_CHUNK_SIZE = 64 * 1024;
|
const DEFAULT_CHUNK_SIZE = 64 * 1024;
|
||||||
|
|
||||||
|
const supportsResponseStream = isReadableStreamSupported && !!(()=> {
|
||||||
|
try {
|
||||||
|
return utils.isReadableStream(new Response('').body);
|
||||||
|
} catch(err) {
|
||||||
|
// return undefined
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
stream: (res) => res.body
|
stream: supportsResponseStream && ((res) => res.body)
|
||||||
};
|
};
|
||||||
|
|
||||||
isFetchSupported && ['text', 'arrayBuffer', 'blob', 'formData'].forEach(type => [
|
isFetchSupported && (((res) => {
|
||||||
resolvers[type] = utils.isFunction(Response.prototype[type]) ? (res) => res[type]() : (_, config) => {
|
['text', 'arrayBuffer', 'blob', 'formData', 'stream'].forEach(type => {
|
||||||
throw new AxiosError(`Response type ${type} is not supported`, AxiosError.ERR_NOT_SUPPORT, config);
|
!resolvers[type] && (resolvers[type] = utils.isFunction(res[type]) ? (res) => res[type]() :
|
||||||
}
|
(_, config) => {
|
||||||
])
|
throw new AxiosError(`Response type '${type}' is not supported`, AxiosError.ERR_NOT_SUPPORT, config);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
})(new Response));
|
||||||
|
|
||||||
const getBodyLength = async (body) => {
|
const getBodyLength = async (body) => {
|
||||||
if(utils.isBlob(body)) {
|
if(utils.isBlob(body)) {
|
||||||
@@ -74,7 +86,7 @@ const resolveBodyLength = async (headers, body) => {
|
|||||||
return length == null ? getBodyLength(body) : length;
|
return length == null ? getBodyLength(body) : length;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (config) => {
|
export default isFetchSupported && (async (config) => {
|
||||||
let {
|
let {
|
||||||
url,
|
url,
|
||||||
method,
|
method,
|
||||||
@@ -106,7 +118,7 @@ export default async (config) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (onUploadProgress && supportsRequestStreams && method !== 'get' && method !== 'head') {
|
if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
|
||||||
let requestContentLength = await resolveBodyLength(headers, data);
|
let requestContentLength = await resolveBodyLength(headers, data);
|
||||||
|
|
||||||
let _request = new Request(url, {
|
let _request = new Request(url, {
|
||||||
@@ -145,7 +157,7 @@ export default async (config) => {
|
|||||||
|
|
||||||
const isStreamResponse = responseType === 'stream' || responseType === 'response';
|
const isStreamResponse = responseType === 'stream' || responseType === 'response';
|
||||||
|
|
||||||
if (onDownloadProgress || isStreamResponse) {
|
if (supportsResponseStream && (onDownloadProgress || isStreamResponse)) {
|
||||||
const options = {};
|
const options = {};
|
||||||
|
|
||||||
Object.getOwnPropertyNames(response).forEach(prop => {
|
Object.getOwnPropertyNames(response).forEach(prop => {
|
||||||
@@ -192,6 +204,6 @@ export default async (config) => {
|
|||||||
|
|
||||||
throw AxiosError.from(err, code, config, request);
|
throw AxiosError.from(err, code, config, request);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -216,4 +216,4 @@
|
|||||||
"@commitlint/config-conventional"
|
"@commitlint/config-conventional"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user