From 1f238f6befc02a7e795732951dc1e5570cc54d5c 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 --- .../readable-stream/doc/stream.markdown | 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/deps/npm/node_modules/npm-registry-client/node_modules/concat-stream/node_modules/readable-stream/doc/stream.markdown b/deps/npm/node_modules/npm-registry-client/node_modules/concat-stream/node_modules/readable-stream/doc/stream.markdown index 0bc3819e63b025..6922c8cb89faa1 100644 --- a/deps/npm/node_modules/npm-registry-client/node_modules/concat-stream/node_modules/readable-stream/doc/stream.markdown +++ b/deps/npm/node_modules/npm-registry-client/node_modules/concat-stream/node_modules/readable-stream/doc/stream.markdown @@ -1146,7 +1146,7 @@ has been called. #### transform.\_flush(callback) * `callback` {Function} Call this function (optionally with an error - argument) when you are done flushing any remaining data. + argument and data) when you are done flushing any remaining data. Note: **This function MUST NOT be called directly.** It MAY be implemented by child classes, and if so, will be called by the internal Transform 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); +});