mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
fix(fetch): fixed ReferenceError issue when TextEncoder is not available in the environment; (#6410)
This commit is contained in:
@@ -20,6 +20,12 @@ const fetchProgressDecorator = (total, fn) => {
|
||||
const isFetchSupported = typeof fetch !== 'undefined';
|
||||
const isReadableStreamSupported = isFetchSupported && typeof ReadableStream !== 'undefined';
|
||||
|
||||
// used only inside the fetch adapter
|
||||
const encodeText = isFetchSupported && (typeof TextEncoder !== 'undefined' ?
|
||||
((encoder) => (str) => encoder.encode(str))(new TextEncoder()) :
|
||||
async (str) => new Uint8Array(await new Response(str).arrayBuffer())
|
||||
);
|
||||
|
||||
const supportsRequestStream = isReadableStreamSupported && (() => {
|
||||
let duplexAccessed = false;
|
||||
|
||||
@@ -80,7 +86,7 @@ const getBodyLength = async (body) => {
|
||||
}
|
||||
|
||||
if(utils.isString(body)) {
|
||||
return (await new TextEncoder().encode(body)).byteLength;
|
||||
return (await encodeText(body)).byteLength;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +150,7 @@ export default isFetchSupported && (async (config) => {
|
||||
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
|
||||
requestContentLength,
|
||||
progressEventReducer(onUploadProgress)
|
||||
));
|
||||
), null, encodeText);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +185,7 @@ export default isFetchSupported && (async (config) => {
|
||||
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
|
||||
responseContentLength,
|
||||
progressEventReducer(onDownloadProgress, true)
|
||||
), isStreamResponse && onFinish),
|
||||
), isStreamResponse && onFinish, encodeText),
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user