Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Brahmah committed Dec 3, 2023
1 parent 4203bba commit 0972ff8
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions index.js
Expand Up @@ -251,22 +251,24 @@ function readStream (stream, encoding, length, limit, callback) {
}))
}

function onData (chunk) {
if (complete) return

received += chunk.length

if (limit !== null && received > limit) {
done(createError(413, 'request entity too large', {
limit: limit,
received: received,
type: 'entity.too.large'
}))
} else if (decoder) {
buffer += decoder.write(chunk)
} else {
buffer.push(chunk)
}
function onData(chunk) {
if (complete) return;

// If 'chunk' is a string, convert it to a Buffer to measure bytes accurately
var bufferChunk = Buffer.from(chunk);
received += bufferChunk.byteLength;

if (limit !== null && received > limit) {
done(createError(413, 'request entity too large', {
limit: limit,
received: received,
type: 'entity.too.large'
}));
} else if (decoder) {
buffer += decoder.write(chunk);
} else {
buffer.push(bufferChunk); // Store buffer chunks instead of string chunks
}
}

function onEnd (err) {
Expand Down

0 comments on commit 0972ff8

Please sign in to comment.