Skip to content

Commit

Permalink
PerMessageDeflate#cleanup - properly cleanup on close
Browse files Browse the repository at this point in the history
  • Loading branch information
yosiat committed Aug 11, 2019
1 parent fa99173 commit 60991a2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/permessage-deflate.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ class PerMessageDeflate {
}

if (this._deflate) {
if (this._deflate[kCallback]) {
this._deflate[kCallback]();
}

this._deflate.close();
this._deflate = null;
}
Expand Down Expand Up @@ -312,7 +316,9 @@ class PerMessageDeflate {
zlibLimiter.push((done) => {
this._compress(data, fin, (err, result) => {
done();
callback(err, result);
if (err || result) {
callback(err, result);
}
});
});
}
Expand Down Expand Up @@ -419,6 +425,8 @@ class PerMessageDeflate {
this._deflate.on('data', deflateOnData);
}

this._deflate[kCallback] = callback;

this._deflate.write(data);
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
if (!this._deflate) {
Expand All @@ -438,6 +446,11 @@ class PerMessageDeflate {

if (fin) data = data.slice(0, data.length - 4);

// Set callback to null so we won't call it twice if socket is closed
// (see PerMessageDeflate#cleanup)
// ideally, we would set it to `null` before calling, but we might set it to null
this._deflate[kCallback] = null;

if (fin && this.params[`${endpoint}_no_context_takeover`]) {
this._deflate.close();
this._deflate = null;
Expand Down

0 comments on commit 60991a2

Please sign in to comment.