Skip to content

Commit

Permalink
fix(fetch): fix cases when ReadableStream or Response.body are not av…
Browse files Browse the repository at this point in the history
…ailable;
  • Loading branch information
DigitalBrainJS committed May 3, 2024
1 parent 565c7d9 commit b21be21
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lib/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const fetchProgressDecorator = (total, fn) => {
}

const isFetchSupported = typeof fetch !== 'undefined';
const isReadableStreamSupported = isFetchSupported && typeof ReadableStream !== 'undefined';

const supportsRequestStreams = isFetchSupported && (() => {
const supportsRequestStream = isReadableStreamSupported && (() => {
let duplexAccessed = false;

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

const DEFAULT_CHUNK_SIZE = 64 * 1024;

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

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 +86,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 +118,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 +157,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 +204,6 @@ export default async (config) => {

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


0 comments on commit b21be21

Please sign in to comment.