Skip to content

Commit

Permalink
fix(fetch): fix case when ReadableStream or Response.body are not ava…
Browse files Browse the repository at this point in the history
…ilable;
  • Loading branch information
DigitalBrainJS committed May 2, 2024
1 parent 565c7d9 commit 7f9d344
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/adapters/fetch.js
Expand Up @@ -19,7 +19,7 @@ const fetchProgressDecorator = (total, fn) => {

const isFetchSupported = typeof fetch !== 'undefined';

const supportsRequestStreams = isFetchSupported && (() => {
const supportsRequestStream = isFetchSupported && typeof ReadableStream !== 'undefined' && (() => {
let duplexAccessed = false;

const hasContentType = new Request(platform.origin, {
Expand All @@ -36,15 +36,25 @@ const supportsRequestStreams = isFetchSupported && (() => {

const DEFAULT_CHUNK_SIZE = 64 * 1024;

const supportsResponseStream = isFetchSupported && !!(()=> {
try {
return utils.isReadableStream(new Response('').body);
} catch(err) {

Check failure on line 42 in lib/adapters/fetch.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Empty block statement

Check failure on line 42 in lib/adapters/fetch.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Empty block statement

Check failure on line 42 in lib/adapters/fetch.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Empty block statement

Check failure on line 42 in lib/adapters/fetch.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Empty block statement

Check failure on line 42 in lib/adapters/fetch.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Empty block statement

Check failure on line 42 in lib/adapters/fetch.js

View workflow job for this annotation

GitHub Actions / build (21.x)

Empty block statement
}
})();

const resolvers = {
stream: (res) => res.body
stream: supportsResponseStream && ((res) => res.body)
};

isFetchSupported && ['text', 'arrayBuffer', 'blob', 'formData'].forEach(type => [
resolvers[type] = utils.isFunction(Response.prototype[type]) ? (res) => res[type]() : (_, config) => {
throw new AxiosError(`Response type ${type} is not supported`, AxiosError.ERR_NOT_SUPPORT, config);
}
])
isFetchSupported && (((res) => {
['text', 'arrayBuffer', 'blob', 'formData', 'stream'].forEach(type => {
!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) => {
if(utils.isBlob(body)) {
Expand Down Expand Up @@ -74,7 +84,7 @@ const resolveBodyLength = async (headers, body) => {
return length == null ? getBodyLength(body) : length;
}

export default async (config) => {
export default isFetchSupported && (async (config) => {
let {
url,
method,
Expand Down Expand Up @@ -106,7 +116,7 @@ export default async (config) => {
}

try {
if (onUploadProgress && supportsRequestStreams && method !== 'get' && method !== 'head') {
if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
let requestContentLength = await resolveBodyLength(headers, data);

let _request = new Request(url, {
Expand Down Expand Up @@ -145,7 +155,7 @@ export default async (config) => {

const isStreamResponse = responseType === 'stream' || responseType === 'response';

if (onDownloadProgress || isStreamResponse) {
if (supportsResponseStream && (onDownloadProgress || isStreamResponse)) {
const options = {};

Object.getOwnPropertyNames(response).forEach(prop => {
Expand Down Expand Up @@ -192,6 +202,6 @@ export default async (config) => {

throw AxiosError.from(err, code, config, request);
}
}
});


0 comments on commit 7f9d344

Please sign in to comment.