From cacdbd471158ecc554ee2de8750fd5528312b753 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Mon, 24 Oct 2016 17:26:44 +0530 Subject: [PATCH 01/10] Kafka topics should not contain special characters This is my observation while playing with createTopics function. I later realised that topic names which I am creating contains special characters such as :,// etc which is causing the issue. I tried without them and it works. It does work with other number and string combinations so no test required for that. Hope this PR is help. --- lib/client.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/client.js b/lib/client.js index 3c83e80d..8b970056 100644 --- a/lib/client.js +++ b/lib/client.js @@ -349,12 +349,26 @@ Client.prototype.loadMetadataForTopics = function (topics, cb) { }; Client.prototype.createTopics = function (topics, isAsync, cb) { + function isValid(str){ + return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); + } topics = typeof topics === 'string' ? [topics] : topics; if (typeof isAsync === 'function' && typeof cb === 'undefined') { cb = isAsync; isAsync = true; } + + var validTopics = true; + Object.keys(topics).map(function(elem){ + if(!isValid(topics[elem])) { + validTopics = false; + } + }); + + if(!validTopics) { + return cb(null,"Some topics contains special characters, please remove them"); + } var self = this; // first, load metadata to create topics From e1fe46c1ea3bb8b9e54ad0b4dd79b1349705b8f1 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Mon, 24 Oct 2016 17:34:05 +0530 Subject: [PATCH 02/10] Update client.js Fixes for build failure #1. --- lib/client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 8b970056..24093253 100644 --- a/lib/client.js +++ b/lib/client.js @@ -349,7 +349,7 @@ Client.prototype.loadMetadataForTopics = function (topics, cb) { }; Client.prototype.createTopics = function (topics, isAsync, cb) { - function isValid(str){ + function isValid (str) { return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); } topics = typeof topics === 'string' ? [topics] : topics; @@ -360,8 +360,8 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { } var validTopics = true; - Object.keys(topics).map(function(elem){ - if(!isValid(topics[elem])) { + Object.keys(topics).map(function(elem) { + if (!isValid(topics[elem])) { validTopics = false; } }); From bcdc3ef453d304c23edac3fb2089de29ef7c611b Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Mon, 24 Oct 2016 17:40:52 +0530 Subject: [PATCH 03/10] Update client.js Fixed for build #2. --- lib/client.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index 24093253..0205da4b 100644 --- a/lib/client.js +++ b/lib/client.js @@ -357,17 +357,16 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { if (typeof isAsync === 'function' && typeof cb === 'undefined') { cb = isAsync; isAsync = true; - } - + } var validTopics = true; - Object.keys(topics).map(function(elem) { + Object.keys(topics).map(function (elem) { if (!isValid(topics[elem])) { validTopics = false; } }); if(!validTopics) { - return cb(null,"Some topics contains special characters, please remove them"); + return cb(null, 'Some topics contains special characters, please remove them'); } var self = this; From 33028c1505c5f2b9ee799ce71ecc1b0294e9d971 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Mon, 24 Oct 2016 17:45:33 +0530 Subject: [PATCH 04/10] Update client.js Fixes for build #3. --- lib/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/client.js b/lib/client.js index 0205da4b..229245f9 100644 --- a/lib/client.js +++ b/lib/client.js @@ -357,7 +357,7 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { if (typeof isAsync === 'function' && typeof cb === 'undefined') { cb = isAsync; isAsync = true; - } + } var validTopics = true; Object.keys(topics).map(function (elem) { if (!isValid(topics[elem])) { @@ -365,7 +365,7 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { } }); - if(!validTopics) { + if (!validTopics) { return cb(null, 'Some topics contains special characters, please remove them'); } var self = this; From 18151c5ef7407beb11bf3a0f8073aae9f5100daa Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Tue, 25 Oct 2016 16:00:40 +0530 Subject: [PATCH 05/10] Validating the topics using util function --- lib/client.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/client.js b/lib/client.js index 229245f9..619d692a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -20,6 +20,7 @@ var Zookeeper = zk.Zookeeper; var url = require('url'); var debug = require('debug')('kafka-node:Client'); var validateConfig = require('./utils').validateConfig; +var validateKafkaTopics = require('./utils').validateCreateTopics; /** * Communicates with kafka brokers @@ -349,25 +350,15 @@ Client.prototype.loadMetadataForTopics = function (topics, cb) { }; Client.prototype.createTopics = function (topics, isAsync, cb) { - function isValid (str) { - return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); - } topics = typeof topics === 'string' ? [topics] : topics; if (typeof isAsync === 'function' && typeof cb === 'undefined') { cb = isAsync; isAsync = true; } - var validTopics = true; - Object.keys(topics).map(function (elem) { - if (!isValid(topics[elem])) { - validTopics = false; - } - }); - - if (!validTopics) { - return cb(null, 'Some topics contains special characters, please remove them'); - } + + validateKafkaTopics(topics); + var self = this; // first, load metadata to create topics From 76b8524186d163eb764c9c651c3a8f24d2845b37 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Tue, 25 Oct 2016 16:04:11 +0530 Subject: [PATCH 06/10] Build fix --- lib/client.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 619d692a..efd0fdc4 100644 --- a/lib/client.js +++ b/lib/client.js @@ -356,11 +356,8 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { cb = isAsync; isAsync = true; } - validateKafkaTopics(topics); - var self = this; - // first, load metadata to create topics this.loadMetadataForTopics(topics, function (err, resp) { if (err) return cb(err); From 5adc88ccdd2b467bd98ce17cdf5b321987a682ec Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Tue, 25 Oct 2016 19:33:19 +0530 Subject: [PATCH 07/10] Update client.js Updating the name of function as per changes in pull req no 17. --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index efd0fdc4..4bec1b04 100644 --- a/lib/client.js +++ b/lib/client.js @@ -20,7 +20,7 @@ var Zookeeper = zk.Zookeeper; var url = require('url'); var debug = require('debug')('kafka-node:Client'); var validateConfig = require('./utils').validateConfig; -var validateKafkaTopics = require('./utils').validateCreateTopics; +var validateKafkaTopics = require('./utils').validateTopicNames; /** * Communicates with kafka brokers From aed806929a18bd246df6e5b2f60b94dae3d8e6e2 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Wed, 26 Oct 2016 12:14:36 +0530 Subject: [PATCH 08/10] Handling async nature of validation. Build fix. --- lib/client.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 4bec1b04..27506715 100644 --- a/lib/client.js +++ b/lib/client.js @@ -356,7 +356,12 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { cb = isAsync; isAsync = true; } - validateKafkaTopics(topics); + try { + validateKafkaTopics(topics); + } catch (e) { + if (isAsync) return cb(e); + throw new e; + } var self = this; // first, load metadata to create topics this.loadMetadataForTopics(topics, function (err, resp) { From 357bb218de878d2bda8a5633194ed19341b07c84 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Wed, 26 Oct 2016 12:30:06 +0530 Subject: [PATCH 09/10] Update client.js Build fix. --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 27506715..55f1b4df 100644 --- a/lib/client.js +++ b/lib/client.js @@ -360,7 +360,7 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { validateKafkaTopics(topics); } catch (e) { if (isAsync) return cb(e); - throw new e; + throw new Error(e); } var self = this; // first, load metadata to create topics From 696bb5abc08772c5810645e80159c2af1aa077c4 Mon Sep 17 00:00:00 2001 From: Shahid Shaikh Date: Thu, 27 Oct 2016 00:39:30 +0530 Subject: [PATCH 10/10] Update client.js For build fix. --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 55f1b4df..deabc945 100644 --- a/lib/client.js +++ b/lib/client.js @@ -360,7 +360,7 @@ Client.prototype.createTopics = function (topics, isAsync, cb) { validateKafkaTopics(topics); } catch (e) { if (isAsync) return cb(e); - throw new Error(e); + throw e; } var self = this; // first, load metadata to create topics