Permalink
Browse files

zlib: s/clear/close/ and match other close() semantics

  • Loading branch information...
1 parent cfbfaaa commit 07d3b21f4352e58928b6b6c2c3c0bfc59153bea9 @isaacs isaacs committed Oct 30, 2012
Showing with 21 additions and 10 deletions.
  1. +15 −4 lib/zlib.js
  2. +6 −6 src/node_zlib.cc
View
@@ -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);
@@ -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);
@@ -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() {
View
@@ -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) {
@@ -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());
}
@@ -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"));

0 comments on commit 07d3b21

Please sign in to comment.