node-zookeeper-thrift
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "node-zookeeper-thrift", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Zookeeper/Thrift implementation", | ||
@@ -14,6 +14,6 @@ "main": "zkt.js", | ||
"dependencies": { | ||
"zookeeper": "3.4.1-4", | ||
"thrift": "0.7.0", | ||
"async": "0.1.22", | ||
"underscore": "1.4.2" | ||
"underscore": "1.4.2", | ||
"zookeeper": "3.4.1-4" | ||
}, | ||
@@ -20,0 +20,0 @@ "devDependencies": { |
@@ -77,3 +77,28 @@ var zkt = require('../zkt'); | ||
it('should fetch schema paths from server and format', function() { | ||
// create formatter function | ||
var formatterFn = function(data) { | ||
if (data && data.serviceEndpoint) { | ||
return data.serviceEndpoint; | ||
} | ||
return data; | ||
} | ||
// associate thrift client and formatter with path | ||
var anotherSchema = { | ||
"/path/to/service/one": {thriftClient: thriftClient, formatter:formatterFn} | ||
} | ||
zkt.fetch(server, anotherSchema, function(err, results) { | ||
if (err) { | ||
throw err; | ||
} | ||
_.isEqual( _.keys(results), [ '/path/to/service/one' ]).should.be.true | ||
var child1 = results['/path/to/service/one']['child1']; | ||
child1.host.should.equal("10.100.145.81"); | ||
child1.port.should.equal(4567); | ||
}); | ||
}); | ||
}); | ||
46
zkt.js
@@ -28,3 +28,28 @@ var zk = require('./lib/zk'); | ||
/** | ||
Return handle to a function that will read the zookeeper node for given path, | ||
deserialize it if thriftClient available, and futher format the result if formatter | ||
function | ||
*/ | ||
function readFn(zkInstance, fullPath, thriftClient, formatter) { | ||
var fn = function(cb) { | ||
zk.readNode(zkInstance, fullPath, function(err, value) { | ||
if (err) return cb(err); | ||
if (!thriftClient) return cb(null, value); | ||
// if thrift client available, parse data | ||
thrift.parseThrift(thriftClient, value, function(err, data) { | ||
// if formatter, available, additionally format the data | ||
if (formatter && !err && data) { | ||
data = formatter(data); | ||
} | ||
return cb(err, data); | ||
}); | ||
}); | ||
} | ||
return fn; | ||
} | ||
/** | ||
Read each child node. Deserialize if thrift client available. | ||
@@ -42,16 +67,8 @@ */ | ||
schema[path].thriftClient : null; | ||
formatter = (schema && schema[path] && schema[path].formatter) ? | ||
schema[path].formatter : null; | ||
// for each child node, create function that will read it | ||
batched[fullPath] = function(cb) { | ||
zk.readNode(zkInstance, fullPath, function(err, value) { | ||
if (err) { | ||
return cb(err); | ||
} else if (thriftClient) { | ||
// if thrift client available, parse data | ||
thrift.parseThrift(thriftClient, value, cb); | ||
} else { | ||
cb(null, value); | ||
} | ||
}); | ||
} | ||
batched[fullPath] = readFn(zkInstance, fullPath, thriftClient, formatter); | ||
}) | ||
@@ -86,3 +103,6 @@ }); | ||
- schema: { | ||
'/path/to/zookeeperService/one': {thriftClient: <point-to-generated-thrift-client-class>}, | ||
'/path/to/zookeeperService/one': { | ||
thriftClient: <point-to-generated-thrift-client-class>, | ||
formatter: function(data) { return(formatted-data)} | ||
}, | ||
'/path/to/zookeeperService/two': {thriftClient: <point-to-generated-thrift-client-class>} | ||
@@ -89,0 +109,0 @@ } |
23328
699