Skip to content

Commit

Permalink
fix decoding issue for request data chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nriver committed Dec 14, 2023
1 parent df85a5e commit 30c3c10
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/services/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ function exec(opts) {
}

let responseStr = '';
let chunks = [];

response.on('data', chunk => responseStr += chunk);
response.on('data', chunk => chunks.push(chunk));

response.on('end', () => {
// use Buffer instead of string concatenation to avoid implicit decoding for each chunk
// decode the entire data chunks explicitly as utf-8
responseStr = Buffer.concat(chunks).toString('utf-8')

if ([200, 201, 204].includes(response.statusCode)) {
try {
const jsonObj = responseStr.trim() ? JSON.parse(responseStr) : null;
Expand Down

0 comments on commit 30c3c10

Please sign in to comment.