From e584ac17001f52c420f3d542c7035e220f97d8dd Mon Sep 17 00:00:00 2001 From: laike9m Date: Tue, 27 Jan 2015 16:37:38 +0800 Subject: [PATCH 1/3] fix bug: error happens if no callback is set when calling client.close() --- lib/client.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 43ca0ff..b5f3f2c 100644 --- a/lib/client.js +++ b/lib/client.js @@ -195,7 +195,9 @@ ZKClient.prototype.connect = function connect(cb) { ZKClient.prototype.close = function close(cb) { assert.optionalFunc(cb, 'callback'); - cb = once(cb); + if (cb) { + cb = once(cb); + } var log = this.log; var zk = this.zk; From fe8669f0febbebc8d56dc9521370a7fbe6f63d09 Mon Sep 17 00:00:00 2001 From: laike9m Date: Thu, 26 Mar 2015 17:22:10 +0800 Subject: [PATCH 2/3] make zk-plus suppourt string and number as node value --- lib/client.js | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/client.js b/lib/client.js index b5f3f2c..97c84d8 100644 --- a/lib/client.js +++ b/lib/client.js @@ -196,9 +196,8 @@ ZKClient.prototype.close = function close(cb) { assert.optionalFunc(cb, 'callback'); if (cb) { - cb = once(cb); + cb = once(cb); } - var log = this.log; var zk = this.zk; @@ -227,7 +226,7 @@ ZKClient.prototype.close = function close(cb) { ZKClient.prototype.create = function creat(p, obj, opts, cb) { assert.string(p, 'path'); - assert.object(obj, 'object'); + if (typeof (opts) === 'function') { cb = opts; opts = {}; @@ -242,7 +241,13 @@ ZKClient.prototype.create = function creat(p, obj, opts, cb) { if (!this._connected(cb)) return; - var data = new Buffer(JSON.stringify(obj), 'utf8'); + var data; + if (typeof(obj) === "string") { + data = new Buffer(obj, 'utf-8'); + } else { + assert.object(obj, 'object'); + data = new Buffer(JSON.stringify(obj), 'utf8'); + } var f; var flags = opts.flags || []; var log = this.log.child({ @@ -311,14 +316,16 @@ ZKClient.prototype.get = function get(p, cb) { try { obj = JSON.parse(data.toString('utf8')); } catch (e) { - log.trace({ - err: e, - data: data - }, 'get: failed (parsing data)'); - cb(e); - return; + try { + obj = data.toString('utf8'); + } catch (e) { + log.trace({ + err: e, + data: data + }, 'get: failed (parsing data)'); + return; + } } - log.trace({data: obj}, 'get: done'); cb(null, obj); } @@ -375,7 +382,7 @@ ZKClient.prototype.mkdirp = function mkdirp(p, cb) { ZKClient.prototype.put = function put(p, obj, opts, cb) { assert.string(p, 'path'); - assert.object(obj, 'object'); +// assert.object(obj, 'object'); if (typeof (opts) === 'function') { cb = opts; opts = {}; @@ -389,7 +396,13 @@ ZKClient.prototype.put = function put(p, obj, opts, cb) { if (!this._connected(cb)) return; - var data = new Buffer(JSON.stringify(obj), 'utf8'); + var data; + if (typeof(obj) === "string" || typeof(obj) == "number") { + data = new Buffer(obj.toString(), 'utf-8'); + } else { + assert.object(obj, 'object'); + data = new Buffer(JSON.stringify(obj), 'utf8'); + } var log = this.log.child({path: p}, true); var ver = opts.version !== undefined ? opts.version : -1; var zk = this.zk; From 5e082c54dc79d4be35455a0e3311d978d5561cc7 Mon Sep 17 00:00:00 2001 From: laike9m Date: Tue, 31 Mar 2015 19:50:44 +0800 Subject: [PATCH 3/3] add boolean support --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 97c84d8..2f313b2 100644 --- a/lib/client.js +++ b/lib/client.js @@ -397,7 +397,7 @@ ZKClient.prototype.put = function put(p, obj, opts, cb) { return; var data; - if (typeof(obj) === "string" || typeof(obj) == "number") { + if (typeof(obj) === "string" || typeof(obj) == "number" || typeof(obj) == "boolean") { data = new Buffer(obj.toString(), 'utf-8'); } else { assert.object(obj, 'object');