Skip to content

Commit

Permalink
fix: use explicit flag for when use has interacted with stream (#3361)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Jun 23, 2024
1 parent 3f4ef9a commit 59fc6d5
Showing 1 changed file with 5 additions and 6 deletions.
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

0 comments on commit 59fc6d5

Please sign in to comment.