summaryrefslogtreecommitdiff
path: root/lib/_stream_transform.js
diff options
context:
space:
mode:
authorRobert Nagy <ronagy@icloud.com>2020-07-11 23:50:09 +0200
committerRobert Nagy <ronagy@icloud.com>2020-07-15 21:23:10 +0200
commita65218f5e82eabdeb4018d80f64e6456f47f9f34 (patch)
treeaaa29d2ed1dc71a919ba8c5a87a8fd2ce9ea6eb8 /lib/_stream_transform.js
parente44855d3173b791682d00a2ac001dd79ebabc95f (diff)
downloadnode-new-a65218f5e82eabdeb4018d80f64e6456f47f9f34.tar.gz
stream: try to wait for flush to complete before 'finish'
Due to compat reasons Transform streams don't always wait for flush to complete before finishing the stream. Try to wait when possible, i.e. when the user does not override _final. Fixes: https://github.com/nodejs/node/issues/34274 PR-URL: https://github.com/nodejs/node/pull/34314 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Zeyu Yang <himself65@outlook.com>
Diffstat (limited to 'lib/_stream_transform.js')
-rw-r--r--lib/_stream_transform.js22
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js
index a27b09887b..564cdf0e82 100644
--- a/lib/_stream_transform.js
+++ b/lib/_stream_transform.js
@@ -106,11 +106,15 @@ function Transform(options) {
this.on('prefinish', prefinish);
}
-function prefinish() {
+function final(cb) {
if (typeof this._flush === 'function' && !this.destroyed) {
this._flush((er, data) => {
if (er) {
- this.destroy(er);
+ if (cb) {
+ cb(er);
+ } else {
+ this.destroy(er);
+ }
return;
}
@@ -118,12 +122,26 @@ function prefinish() {
this.push(data);
}
this.push(null);
+ if (cb) {
+ cb();
+ }
});
} else {
this.push(null);
+ if (cb) {
+ cb();
+ }
+ }
+}
+
+function prefinish() {
+ if (this._final !== final) {
+ final.call(this);
}
}
+Transform.prototype._final = final;
+
Transform.prototype._transform = function(chunk, encoding, callback) {
throw new ERR_METHOD_NOT_IMPLEMENTED('_transform()');
};