Skip to content

Commit 07d3b21

Browse files
committed
zlib: s/clear/close/ and match other close() semantics
1 parent cfbfaaa commit 07d3b21

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

lib/zlib.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ function zlibBuffer(engine, buffer, callback) {
153153
var buf = Buffer.concat(buffers, nread);
154154
buffers = [];
155155
callback(null, buf);
156-
engine._clear();
157156
}
158157

159158
engine.on('error', onError);
@@ -298,7 +297,9 @@ function Zlib(opts, mode) {
298297
this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
299298
this._buffer = new Buffer(this._chunkSize);
300299
this._offset = 0;
301-
var self = this;
300+
this._closed = false;
301+
302+
this.once('end', this.close);
302303
}
303304

304305
util.inherits(Zlib, Stream);
@@ -356,8 +357,18 @@ Zlib.prototype.end = function end(chunk, cb) {
356357
return ret;
357358
};
358359

359-
Zlib.prototype._clear = function() {
360-
return this._binding.clear();
360+
Zlib.prototype.close = function(callback) {
361+
if (callback)
362+
process.nextTick(callback);
363+
364+
if (this._closed)
365+
return;
366+
367+
this._closed = true;
368+
369+
this._binding.close();
370+
371+
process.nextTick(this.emit.bind(this, 'close'));
361372
};
362373

363374
Zlib.prototype._process = function() {

src/node_zlib.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class ZCtx : public ObjectWrap {
6363

6464

6565
~ZCtx() {
66-
Clear();
66+
Close();
6767
}
6868

6969

70-
void Clear() {
70+
void Close() {
7171
assert(!write_in_progress_ && "write in progress");
72-
assert(init_done_ && "clear before init");
72+
assert(init_done_ && "close before init");
7373
assert(mode_ <= UNZIP);
7474

7575
if (mode_ == DEFLATE || mode_ == GZIP || mode_ == DEFLATERAW) {
@@ -89,10 +89,10 @@ class ZCtx : public ObjectWrap {
8989
}
9090

9191

92-
static Handle<Value> Clear(const Arguments& args) {
92+
static Handle<Value> Close(const Arguments& args) {
9393
HandleScope scope;
9494
ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
95-
ctx->Clear();
95+
ctx->Close();
9696
return scope.Close(Undefined());
9797
}
9898

@@ -472,7 +472,7 @@ void InitZlib(Handle<Object> target) {
472472

473473
NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write);
474474
NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx::Init);
475-
NODE_SET_PROTOTYPE_METHOD(z, "clear", ZCtx::Clear);
475+
NODE_SET_PROTOTYPE_METHOD(z, "close", ZCtx::Close);
476476
NODE_SET_PROTOTYPE_METHOD(z, "reset", ZCtx::Reset);
477477

478478
z->SetClassName(String::NewSymbol("Zlib"));

0 commit comments

Comments
 (0)