Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: consider bytes read when dumping #3360

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const kBody = Symbol('kBody')
const kAbort = Symbol('kAbort')
const kContentType = Symbol('kContentType')
const kContentLength = Symbol('kContentLength')
const kBytesRead = Symbol('kBytesRead')

const noop = () => {}

Expand All @@ -35,9 +36,10 @@ class BodyReadable extends Readable {

this[kAbort] = abort
this[kConsume] = null
this[kBytesRead] = 0
this[kBody] = null
this[kContentType] = contentType
this[kContentLength] = contentLength
this[kContentLength] = Number.isFinite(contentLength) ? contentLength : null

// Is stream being consumed through Readable API?
// This is an optimization so that we avoid checking
Expand Down Expand Up @@ -99,6 +101,8 @@ class BodyReadable extends Readable {
}

push (chunk) {
this[kBytesRead] += chunk ? chunk.length : 0

if (this[kConsume] && chunk !== null) {
consumePush(this[kConsume], chunk)
return this[kReading] ? super.push(chunk) : true
Expand Down Expand Up @@ -151,7 +155,7 @@ class BodyReadable extends Readable {
}

async dump (opts) {
let limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
const limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
const signal = opts?.signal

if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) {
Expand All @@ -165,7 +169,7 @@ class BodyReadable extends Readable {
}

return await new Promise((resolve, reject) => {
if (this[kContentLength] > limit) {
if (this[kContentLength] > limit || this[kBytesRead] > limit) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we consider this a breaking change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

this.destroy(new AbortError())
}

Expand All @@ -185,8 +189,7 @@ class BodyReadable extends Readable {
})
.on('error', noop)
.on('data', function (chunk) {
limit -= chunk.length
if (limit <= 0) {
if (this[kBytesRead] > limit) {
this.destroy()
}
})
Expand Down
Loading