Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: use explicit flag for when use has interacted with stream #3361

Merged
merged 2 commits 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
11 changes: 5 additions & 6 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 kUsed = Symbol('kUsed')
const kBytesRead = Symbol('kBytesRead')

const noop = () => {}
Expand All @@ -38,6 +39,7 @@ class BodyReadable extends Readable {
this[kConsume] = null
this[kBytesRead] = 0
this[kBody] = null
this[kUsed] = false
this[kContentType] = contentType
this[kContentLength] = Number.isFinite(contentLength) ? contentLength : null

Expand All @@ -48,7 +50,7 @@ class BodyReadable extends Readable {
this[kReading] = false
}

destroy (err) {
_destroy (err, callback) {
if (!err && !this._readableState.endEmitted) {
err = new RequestAbortedError()
}
Expand All @@ -57,15 +59,11 @@ class BodyReadable extends Readable {
this[kAbort]()
}

return super.destroy(err)
}

_destroy (err, callback) {
// Workaround for Node "bug". If the stream is destroyed in same
// tick as it is created, then a user who is waiting for a
// promise (i.e micro tick) for installing a 'error' listener will
// never get a chance and will always encounter an unhandled exception.
if (!this[kReading]) {
if (!this[kUsed]) {
setImmediate(() => {
callback(err)
})
Expand All @@ -77,6 +75,7 @@ class BodyReadable extends Readable {
on (ev, ...args) {
if (ev === 'data' || ev === 'readable') {
this[kReading] = true
this[kUsed] = true
}
return super.on(ev, ...args)
}
Expand Down
Loading