mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +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 isFetchSupported = typeof fetch !== 'undefined';
|
||||||
const isReadableStreamSupported = isFetchSupported && typeof ReadableStream !== '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 && (() => {
|
const supportsRequestStream = isReadableStreamSupported && (() => {
|
||||||
let duplexAccessed = false;
|
let duplexAccessed = false;
|
||||||
|
|
||||||
@@ -80,7 +86,7 @@ const getBodyLength = async (body) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(utils.isString(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(
|
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
|
||||||
requestContentLength,
|
requestContentLength,
|
||||||
progressEventReducer(onUploadProgress)
|
progressEventReducer(onUploadProgress)
|
||||||
));
|
), null, encodeText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,7 +185,7 @@ export default isFetchSupported && (async (config) => {
|
|||||||
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
|
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
|
||||||
responseContentLength,
|
responseContentLength,
|
||||||
progressEventReducer(onDownloadProgress, true)
|
progressEventReducer(onDownloadProgress, true)
|
||||||
), isStreamResponse && onFinish),
|
), isStreamResponse && onFinish, encodeText),
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
export const streamChunk = function* (chunk, chunkSize) {
|
export const streamChunk = function* (chunk, chunkSize) {
|
||||||
let len = chunk.byteLength;
|
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, encode) {
|
||||||
|
|
||||||
export const readBytes = async function* (iterable, chunkSize) {
|
|
||||||
for await (const chunk of iterable) {
|
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) => {
|
export const trackStream = (stream, chunkSize, onProgress, onFinish, encode) => {
|
||||||
const iterator = readBytes(stream, chunkSize);
|
const iterator = readBytes(stream, chunkSize, encode);
|
||||||
|
|
||||||
let bytes = 0;
|
let bytes = 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user