2
0
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:
Dmitriy Mozgovoy
2024-05-03 20:22:11 +03:00
committed by GitHub
parent 8e4314bfd6
commit d1d359da34
2 changed files with 24 additions and 12 deletions
+23 -11
View File
@@ -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);
} }
} });