Skip to content

Commit

Permalink
zlib: s/clear/close/ and match other close() semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 31, 2012
1 parent cfbfaaa commit 07d3b21
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
19 changes: 15 additions & 4 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ function zlibBuffer(engine, buffer, callback) {
var buf = Buffer.concat(buffers, nread);
buffers = [];
callback(null, buf);
engine._clear();
}

engine.on('error', onError);
Expand Down Expand Up @@ -298,7 +297,9 @@ function Zlib(opts, mode) {
this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
this._buffer = new Buffer(this._chunkSize);
this._offset = 0;
var self = this;
this._closed = false;

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

util.inherits(Zlib, Stream);
Expand Down Expand Up @@ -356,8 +357,18 @@ Zlib.prototype.end = function end(chunk, cb) {
return ret;
};

Zlib.prototype._clear = function() {
return this._binding.clear();
Zlib.prototype.close = function(callback) {
if (callback)
process.nextTick(callback);

if (this._closed)
return;

this._closed = true;

this._binding.close();

process.nextTick(this.emit.bind(this, 'close'));
};

Zlib.prototype._process = function() {
Expand Down
12 changes: 6 additions & 6 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ class ZCtx : public ObjectWrap {


~ZCtx() {
Clear();
Close();
}


void Clear() {
void Close() {
assert(!write_in_progress_ && "write in progress");
assert(init_done_ && "clear before init");
assert(init_done_ && "close before init");
assert(mode_ <= UNZIP);

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


static Handle<Value> Clear(const Arguments& args) {
static Handle<Value> Close(const Arguments& args) {
HandleScope scope;
ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
ctx->Clear();
ctx->Close();
return scope.Close(Undefined());
}

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

NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write);
NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx::Init);
NODE_SET_PROTOTYPE_METHOD(z, "clear", ZCtx::Clear);
NODE_SET_PROTOTYPE_METHOD(z, "close", ZCtx::Close);
NODE_SET_PROTOTYPE_METHOD(z, "reset", ZCtx::Reset);

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

0 comments on commit 07d3b21

Please sign in to comment.