Skip to content

Commit

Permalink
fix(fetch): fixed ReferenceError issue when TextEncoderis not avail…
Browse files Browse the repository at this point in the history
…able in the environment;
  • Loading branch information
DigitalBrainJS committed May 20, 2024
1 parent e58616b commit cf35b75
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,7 +86,7 @@ const getBodyLength = async (body) => {
}

if(utils.isString(body)) {
return (await new TextEncoder().encode(body)).byteLength;
return (await encodeText(body)).byteLength;
}
}

Expand Down Expand Up @@ -144,7 +150,7 @@ export default isFetchSupported && (async (config) => {
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
requestContentLength,
progressEventReducer(onUploadProgress)
));
), null, encodeText);
}
}

Expand Down Expand Up @@ -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
);
}
Expand Down
11 changes: 5 additions & 6 deletions lib/helpers/trackStream.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


export const streamChunk = function* (chunk, chunkSize) {
let len = chunk.byteLength;

Expand All @@ -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;

Expand Down

0 comments on commit cf35b75

Please sign in to comment.