-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: need to cleanup event listeners if last stream is readable
fix: #35452 PR-URL: #41954 Fixes: #35452 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
Showing
3 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { pipeline, Duplex, PassThrough, Writable } = require('stream'); | ||
const assert = require('assert'); | ||
|
||
process.on('uncaughtException', common.mustCall((err) => { | ||
assert.strictEqual(err.message, 'no way'); | ||
}, 2)); | ||
|
||
// Ensure that listeners is removed if last stream is readble | ||
// And other stream's listeners unchanged | ||
const a = new PassThrough(); | ||
a.end('foobar'); | ||
const b = new Duplex({ | ||
write(chunk, encoding, callback) { | ||
callback(); | ||
} | ||
}); | ||
pipeline(a, b, common.mustCall((error) => { | ||
if (error) { | ||
assert.ifError(error); | ||
} | ||
|
||
assert(a.listenerCount('error') > 0); | ||
assert.strictEqual(b.listenerCount('error'), 0); | ||
setTimeout(() => { | ||
assert.strictEqual(b.listenerCount('error'), 0); | ||
b.destroy(new Error('no way')); | ||
}, 100); | ||
})); | ||
|
||
// Async generators | ||
const c = new PassThrough(); | ||
c.end('foobar'); | ||
const d = pipeline( | ||
c, | ||
async function* (source) { | ||
for await (const chunk of source) { | ||
yield String(chunk).toUpperCase(); | ||
} | ||
}, | ||
common.mustCall((error) => { | ||
if (error) { | ||
assert.ifError(error); | ||
} | ||
|
||
assert(c.listenerCount('error') > 0); | ||
assert.strictEqual(d.listenerCount('error'), 0); | ||
setTimeout(() => { | ||
assert.strictEqual(b.listenerCount('error'), 0); | ||
d.destroy(new Error('no way')); | ||
}, 100); | ||
}) | ||
); | ||
|
||
// If last stream is not readable, will not throw and remove listeners | ||
const e = new PassThrough(); | ||
e.end('foobar'); | ||
const f = new Writable({ | ||
write(chunk, encoding, callback) { | ||
callback(); | ||
} | ||
}); | ||
pipeline(e, f, common.mustCall((error) => { | ||
if (error) { | ||
assert.ifError(error); | ||
} | ||
|
||
assert(e.listenerCount('error') > 0); | ||
assert(f.listenerCount('error') > 0); | ||
setTimeout(() => { | ||
assert(f.listenerCount('error') > 0); | ||
f.destroy(new Error('no way')); | ||
}, 100); | ||
})); |