Skip to content

Commit e8af744

Browse files
committed
fix: support only for browser
1 parent 0b18ce4 commit e8af744

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/fetch.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -284,31 +284,39 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
284284
let loaded = 0;
285285
const contentLength =
286286
context.response!.headers.get("content-length")!;
287-
const _reader = context.response!.body!.getReader();
288-
const _decoder = new TextDecoder();
289-
const _chunks: string[] = [];
290-
291-
async function read(): Promise<string> {
292-
const { done, value } = await _reader.read();
293-
294-
if (done) {
295-
return _chunks.join("");
296-
}
297-
298-
loaded += value.byteLength;
299-
300-
if (context.options.onResponseProgress) {
301-
context.options.onResponseProgress(
302-
Math.round((loaded / Number.parseInt(contentLength)) * 100)
303-
);
287+
if (
288+
"getReader" in (context.response!.body as ReadableStream) &&
289+
typeof (context.response!.body as ReadableStream).getReader ===
290+
"function"
291+
) {
292+
const _reader = context.response!.body!.getReader();
293+
const _decoder = new TextDecoder();
294+
const _chunks: string[] = [];
295+
296+
async function read(): Promise<string> {
297+
const { done, value } = await _reader.read();
298+
299+
if (done) {
300+
return _chunks.join("");
301+
}
302+
303+
loaded += value.byteLength;
304+
305+
if (context.options.onResponseProgress) {
306+
context.options.onResponseProgress(
307+
Math.round((loaded / Number.parseInt(contentLength)) * 100)
308+
);
309+
}
310+
311+
const chunk = _decoder.decode(value, { stream: true });
312+
_chunks.push(chunk);
313+
return await read(); // read the next chunk
304314
}
305315

306-
const chunk = _decoder.decode(value, { stream: true });
307-
_chunks.push(chunk);
308-
return await read(); // read the next chunk
316+
return await read();
317+
} else {
318+
return context.response!.text();
309319
}
310-
311-
return await read();
312320
})();
313321
const parseFunction = context.options.parseResponse || destr;
314322
context.response._data = parseFunction(data);

0 commit comments

Comments
 (0)