Skip to content

Commit 0cd0118

Browse files
pirannamcollina
authored andcommitted
stream: 'data' argument on callback of Transform._flush()
Add a `data` argument on Transform._flush() callback to be API consistent with Transform._transform(). Fixes: #3707 PR-URL: #3708 Reviewed-By: Matteo Collina <[email protected]>
1 parent 779091f commit 0cd0118

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

doc/api/stream.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ after all data has been output, which occurs after the callback in
16971697
#### transform.\_flush(callback)
16981698

16991699
* `callback` {Function} A callback function (optionally with an error
1700-
argument) to be called when remaining data has been flushed.
1700+
argument and data) to be called when remaining data has been flushed.
17011701

17021702
*Note*: **This function MUST NOT be called by application code directly.** It
17031703
should be implemented by child classes, and called only by the internal Readable

lib/_stream_transform.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ function Transform(options) {
115115

116116
this.once('prefinish', function() {
117117
if (typeof this._flush === 'function')
118-
this._flush(function(er) {
119-
done(stream, er);
118+
this._flush(function(er, data) {
119+
done(stream, er, data);
120120
});
121121
else
122122
done(stream);
@@ -173,10 +173,13 @@ Transform.prototype._read = function(n) {
173173
};
174174

175175

176-
function done(stream, er) {
176+
function done(stream, er, data) {
177177
if (er)
178178
return stream.emit('error', er);
179179

180+
if (data !== null && data !== undefined)
181+
stream.push(data);
182+
180183
// if there's nothing in the write buffer, then that means
181184
// that nothing more will ever be provided
182185
var ws = stream._writableState;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const Transform = require('stream').Transform;
7+
8+
9+
const expected = 'asdf';
10+
11+
12+
function _transform(d, e, n) {
13+
n();
14+
}
15+
function _flush(n) {
16+
n(null, expected);
17+
}
18+
19+
var t = new Transform({
20+
transform: _transform,
21+
flush: _flush
22+
});
23+
24+
t.end(Buffer.from('blerg'));
25+
t.on('data', (data) => {
26+
assert.strictEqual(data.toString(), expected);
27+
});

0 commit comments

Comments
 (0)