Skip to content

Commit

Permalink
Handle chunked response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
akiradev committed Jan 9, 2025
1 parent 9227ef9 commit eff8a84
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/clientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,14 @@ let handleResponseStream = async <TAcc>({ body, callback, initialValue }: {
let buffer = Buffer.alloc(0);

return new Promise((resolve, reject) => {
let accumulator = initialValue;

if (body !== null) {
body.getReader().read().then(({ done, value }) => {
let accumulator = initialValue;

const reader = body.getReader();

reader.read().then(function processChunk({ done, value }): any {
if (done) {
resolve(accumulator);
return resolve(accumulator);
}

buffer = Buffer.concat([buffer, value]);
Expand All @@ -520,6 +522,8 @@ let handleResponseStream = async <TAcc>({ body, callback, initialValue }: {
return;
}
});

return reader.read().then(processChunk);
}).catch(e => {
reject(e);
})
Expand Down

0 comments on commit eff8a84

Please sign in to comment.