mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
|
||||
|
||||
export const streamChunk = function* (chunk, chunkSize) {
|
||||
let len = chunk.byteLength;
|
||||
|
||||
@@ -17,16 +18,14 @@ export const streamChunk = function* (chunk, chunkSize) {
|
||||
}
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
export const readBytes = async function* (iterable, chunkSize) {
|
||||
export const readBytes = async function* (iterable, chunkSize, encode) {
|
||||
for await (const chunk of iterable) {
|
||||
yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encoder.encode(String(chunk))), chunkSize);
|
||||
yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encode(String(chunk))), chunkSize);
|
||||
}
|
||||
}
|
||||
|
||||
export const trackStream = (stream, chunkSize, onProgress, onFinish) => {
|
||||
const iterator = readBytes(stream, chunkSize);
|
||||
export const trackStream = (stream, chunkSize, onProgress, onFinish, encode) => {
|
||||
const iterator = readBytes(stream, chunkSize, encode);
|
||||
|
||||
let bytes = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user