Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: remove _closed #6574

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ function Zlib(opts, mode) {
this._handle.onerror = function(message, errno) {
// there is no way to cleanly recover.
// continuing only obscures problems.
self._handle = null;
_close(self);
self._hadError = true;

var error = new Error(message);
Expand All @@ -387,11 +387,16 @@ function Zlib(opts, mode) {

this._buffer = Buffer.allocUnsafe(this._chunkSize);
this._offset = 0;
this._closed = false;
this._level = level;
this._strategy = strategy;

this.once('end', this.close);

Object.defineProperty(this, '_closed', {
get: () => { return !this._handle; },
configurable: true,
enumerable: true
});
}

util.inherits(Zlib, Transform);
Expand All @@ -412,7 +417,7 @@ Zlib.prototype.params = function(level, strategy, callback) {
if (this._level !== level || this._strategy !== strategy) {
var self = this;
this.flush(binding.Z_SYNC_FLUSH, function() {
assert(!self._closed, 'zlib binding closed');
assert(self._handle, 'zlib binding closed');
self._handle.params(level, strategy);
if (!self._hadError) {
self._level = level;
Expand All @@ -426,7 +431,7 @@ Zlib.prototype.params = function(level, strategy, callback) {
};

Zlib.prototype.reset = function() {
assert(!this._closed, 'zlib binding closed');
assert(this._handle, 'zlib binding closed');
return this._handle.reset();
};

Expand Down Expand Up @@ -469,15 +474,12 @@ function _close(engine, callback) {
if (callback)
process.nextTick(callback);

if (engine._closed)
// Caller may invoke .close after a zlib error (which will null _handle).
if (!engine._handle)
return;

engine._closed = true;

// Caller may invoke .close after a zlib error (which will null _handle).
if (engine._handle) {
engine._handle.close();
}
engine._handle.close();
engine._handle = null;
}

function emitCloseNT(self) {
Expand All @@ -493,7 +495,7 @@ Zlib.prototype._transform = function(chunk, encoding, cb) {
if (chunk !== null && !(chunk instanceof Buffer))
return cb(new Error('invalid input'));

if (this._closed)
if (!this._handle)
return cb(new Error('zlib binding closed'));

// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag
Expand Down Expand Up @@ -533,7 +535,7 @@ Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
error = er;
});

assert(!this._closed, 'zlib binding closed');
assert(this._handle, 'zlib binding closed');
do {
var res = this._handle.writeSync(flushFlag,
chunk, // in
Expand All @@ -559,7 +561,7 @@ Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
return buf;
}

assert(!this._closed, 'zlib binding closed');
assert(this._handle, 'zlib binding closed');
var req = this._handle.write(flushFlag,
chunk, // in
inOff, // in_off
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-zlib-close-after-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const zlib = require('zlib');
const decompress = zlib.createGunzip(15);

decompress.on('error', common.mustCall((err) => {
assert.strictEqual(decompress._closed, true);
assert.doesNotThrow(() => decompress.close());
}));

assert.strictEqual(decompress._closed, false);
decompress.write('something invalid');