Skip to content

Commit

Permalink
stream: 'data' argument on callback of Transform._flush()
Browse files Browse the repository at this point in the history
Add a `data` argument on Transform._flush() callback to be API
consistent with Transform._transform().

Fixes: #3707
PR-URL: #3708
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
piranna authored and mcollina committed Jun 9, 2016
1 parent 779091f commit 0cd0118
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/api/stream.md
Expand Up @@ -1697,7 +1697,7 @@ after all data has been output, which occurs after the callback in
#### transform.\_flush(callback)

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

*Note*: **This function MUST NOT be called by application code directly.** It
should be implemented by child classes, and called only by the internal Readable
Expand Down
9 changes: 6 additions & 3 deletions lib/_stream_transform.js
Expand Up @@ -115,8 +115,8 @@ function Transform(options) {

this.once('prefinish', function() {
if (typeof this._flush === 'function')
this._flush(function(er) {
done(stream, er);
this._flush(function(er, data) {
done(stream, er, data);
});
else
done(stream);
Expand Down Expand Up @@ -173,10 +173,13 @@ Transform.prototype._read = function(n) {
};


function done(stream, er) {
function done(stream, er, data) {
if (er)
return stream.emit('error', er);

if (data !== null && data !== undefined)
stream.push(data);

// if there's nothing in the write buffer, then that means
// that nothing more will ever be provided
var ws = stream._writableState;
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-stream-transform-flush-data.js
@@ -0,0 +1,27 @@
'use strict';

require('../common');

const assert = require('assert');
const Transform = require('stream').Transform;


const expected = 'asdf';


function _transform(d, e, n) {
n();
}
function _flush(n) {
n(null, expected);
}

var t = new Transform({
transform: _transform,
flush: _flush
});

t.end(Buffer.from('blerg'));
t.on('data', (data) => {
assert.strictEqual(data.toString(), expected);
});

0 comments on commit 0cd0118

Please sign in to comment.