Skip to content

Commit 733f15f

Browse files
fix(fetch): fixed ReferenceError issue when TextEncoder is not available in the environment; (#6410)
1 parent 3041c61 commit 733f15f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lib/adapters/fetch.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const fetchProgressDecorator = (total, fn) => {
2020
const isFetchSupported = typeof fetch !== 'undefined';
2121
const isReadableStreamSupported = isFetchSupported && typeof ReadableStream !== 'undefined';
2222

23+
// used only inside the fetch adapter
24+
const encodeText = isFetchSupported && (typeof TextEncoder !== 'undefined' ?
25+
((encoder) => (str) => encoder.encode(str))(new TextEncoder()) :
26+
async (str) => new Uint8Array(await new Response(str).arrayBuffer())
27+
);
28+
2329
const supportsRequestStream = isReadableStreamSupported && (() => {
2430
let duplexAccessed = false;
2531

@@ -80,7 +86,7 @@ const getBodyLength = async (body) => {
8086
}
8187

8288
if(utils.isString(body)) {
83-
return (await new TextEncoder().encode(body)).byteLength;
89+
return (await encodeText(body)).byteLength;
8490
}
8591
}
8692

@@ -144,7 +150,7 @@ export default isFetchSupported && (async (config) => {
144150
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
145151
requestContentLength,
146152
progressEventReducer(onUploadProgress)
147-
));
153+
), null, encodeText);
148154
}
149155
}
150156

@@ -179,7 +185,7 @@ export default isFetchSupported && (async (config) => {
179185
trackStream(response.body, DEFAULT_CHUNK_SIZE, onDownloadProgress && fetchProgressDecorator(
180186
responseContentLength,
181187
progressEventReducer(onDownloadProgress, true)
182-
), isStreamResponse && onFinish),
188+
), isStreamResponse && onFinish, encodeText),
183189
options
184190
);
185191
}

lib/helpers/trackStream.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
23
export const streamChunk = function* (chunk, chunkSize) {
34
let len = chunk.byteLength;
45

@@ -17,16 +18,14 @@ export const streamChunk = function* (chunk, chunkSize) {
1718
}
1819
}
1920

20-
const encoder = new TextEncoder();
21-
22-
export const readBytes = async function* (iterable, chunkSize) {
21+
export const readBytes = async function* (iterable, chunkSize, encode) {
2322
for await (const chunk of iterable) {
24-
yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encoder.encode(String(chunk))), chunkSize);
23+
yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encode(String(chunk))), chunkSize);
2524
}
2625
}
2726

28-
export const trackStream = (stream, chunkSize, onProgress, onFinish) => {
29-
const iterator = readBytes(stream, chunkSize);
27+
export const trackStream = (stream, chunkSize, onProgress, onFinish, encode) => {
28+
const iterator = readBytes(stream, chunkSize, encode);
3029

3130
let bytes = 0;
3231

0 commit comments

Comments
 (0)