From 0cd01183342d0a1675a261c0bb57e742cfd75d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Tue, 7 Jun 2016 22:54:51 +0200 Subject: [PATCH] stream: 'data' argument on callback of Transform._flush() Add a `data` argument on Transform._flush() callback to be API consistent with Transform._transform(). Fixes: https://github.com/nodejs/node/issues/3707 PR-URL: https://github.com/nodejs/node/pull/3708 Reviewed-By: Matteo Collina --- doc/api/stream.md | 2 +- lib/_stream_transform.js | 9 ++++--- .../test-stream-transform-flush-data.js | 27 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-stream-transform-flush-data.js diff --git a/doc/api/stream.md b/doc/api/stream.md index 48c1a44eb6a258..c7aa8f24170647 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -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 diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js index 50fc542b5abd91..5dc7fdd0a8d831 100644 --- a/lib/_stream_transform.js +++ b/lib/_stream_transform.js @@ -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); @@ -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; diff --git a/test/parallel/test-stream-transform-flush-data.js b/test/parallel/test-stream-transform-flush-data.js new file mode 100644 index 00000000000000..dede64ba10f9da --- /dev/null +++ b/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); +});