-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: Fixes missing 'unpipe' event
Currently when the destination emits an 'error', 'finish' or 'close' event the pipe calls unpipe to emit 'unpipe' and trigger the clean up of all it's listeners. When the source emits an 'end' event without {end: false} it calls end() on the destination leading it to emit a 'close', this will again lead to the pipe calling unpipe. However the source emitting an 'end' event along side {end: false} is the only time the cleanup gets ran directly without unpipe being called. This fixes that so the 'unpipe' event does get emitted and cleanup in turn gets ran by that event. Fixes: #11837 PR-URL: #11876 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
- Loading branch information
1 parent
592db37
commit f5a702e
Showing
2 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const {Writable, Readable} = require('stream'); | ||
class NullWriteable extends Writable { | ||
_write(chunk, encoding, callback) { | ||
return callback(); | ||
} | ||
} | ||
class QuickEndReadable extends Readable { | ||
_read() { | ||
this.push(null); | ||
} | ||
} | ||
class NeverEndReadable extends Readable { | ||
_read() {} | ||
} | ||
|
||
function noop() {} | ||
|
||
{ | ||
const dest = new NullWriteable(); | ||
const src = new QuickEndReadable(); | ||
dest.on('pipe', common.mustCall(noop)); | ||
dest.on('unpipe', common.mustCall(noop)); | ||
src.pipe(dest); | ||
setImmediate(() => { | ||
assert.strictEqual(src._readableState.pipesCount, 0); | ||
}); | ||
} | ||
|
||
{ | ||
const dest = new NullWriteable(); | ||
const src = new NeverEndReadable(); | ||
dest.on('pipe', common.mustCall(noop)); | ||
dest.on('unpipe', common.mustNotCall('unpipe should not have been emitted')); | ||
src.pipe(dest); | ||
setImmediate(() => { | ||
assert.strictEqual(src._readableState.pipesCount, 1); | ||
}); | ||
} | ||
|
||
{ | ||
const dest = new NullWriteable(); | ||
const src = new NeverEndReadable(); | ||
dest.on('pipe', common.mustCall(noop)); | ||
dest.on('unpipe', common.mustCall(noop)); | ||
src.pipe(dest); | ||
src.unpipe(dest); | ||
setImmediate(() => { | ||
assert.strictEqual(src._readableState.pipesCount, 0); | ||
}); | ||
} | ||
|
||
{ | ||
const dest = new NullWriteable(); | ||
const src = new QuickEndReadable(); | ||
dest.on('pipe', common.mustCall(noop)); | ||
dest.on('unpipe', common.mustCall(noop)); | ||
src.pipe(dest, {end: false}); | ||
setImmediate(() => { | ||
assert.strictEqual(src._readableState.pipesCount, 0); | ||
}); | ||
} | ||
|
||
{ | ||
const dest = new NullWriteable(); | ||
const src = new NeverEndReadable(); | ||
dest.on('pipe', common.mustCall(noop)); | ||
dest.on('unpipe', common.mustNotCall('unpipe should not have been emitted')); | ||
src.pipe(dest, {end: false}); | ||
setImmediate(() => { | ||
assert.strictEqual(src._readableState.pipesCount, 1); | ||
}); | ||
} | ||
|
||
{ | ||
const dest = new NullWriteable(); | ||
const src = new NeverEndReadable(); | ||
dest.on('pipe', common.mustCall(noop)); | ||
dest.on('unpipe', common.mustCall(noop)); | ||
src.pipe(dest, {end: false}); | ||
src.unpipe(dest); | ||
setImmediate(() => { | ||
assert.strictEqual(src._readableState.pipesCount, 0); | ||
}); | ||
} |