From 564d6279240ae1c9eb89526f9f7d0ebd131934f6 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 5 Apr 2020 17:48:08 -0400 Subject: [PATCH] Empty string value/key shouldn't be treated as null --- e2e/both.spec.js | 22 ++++++++++++++++++++++ src/producer.cc | 12 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/e2e/both.spec.js b/e2e/both.spec.js index 66d55890..948fbb51 100644 --- a/e2e/both.spec.js +++ b/e2e/both.spec.js @@ -306,6 +306,28 @@ describe('Consumer/Producer', function() { run_headers_test(done, headers); }); + it('should be able to produce and consume messages: empty buffer key and empty value', function(done) { + this.timeout(20000); + var emptyString = ''; + var key = Buffer.from(emptyString); + var value = Buffer.from(''); + + producer.setPollInterval(10); + + consumer.once('data', function(message) { + t.notEqual(message.value, null, 'message should not be null'); + t.equal(value.toString(), message.value.toString(), 'invalid message value'); + t.equal(emptyString, message.key, 'invalid message key'); + done(); + }); + + consumer.subscribe([topic]); + consumer.consume(); + + setTimeout(function() { + producer.produce(topic, null, value, key); + }, 2000); + }); it('should be able to produce and consume messages: empty key and empty value', function(done) { this.timeout(20000); diff --git a/src/producer.cc b/src/producer.cc index 385a7968..e86f1790 100644 --- a/src/producer.cc +++ b/src/producer.cc @@ -388,6 +388,12 @@ NAN_METHOD(Producer::NodeProduce) { message_buffer_length = node::Buffer::Length(message_buffer_object); message_buffer_data = node::Buffer::Data(message_buffer_object); + if (message_buffer_data == NULL) { + // empty string message buffer should not end up as null message + v8::Local message_buffer_object_emptystring = Nan::NewBuffer(new char[0], 0).ToLocalChecked(); + message_buffer_length = node::Buffer::Length(message_buffer_object_emptystring); + message_buffer_data = node::Buffer::Data(message_buffer_object_emptystring); + } } size_t key_buffer_length; @@ -412,6 +418,12 @@ NAN_METHOD(Producer::NodeProduce) { key_buffer_length = node::Buffer::Length(key_buffer_object); key_buffer_data = node::Buffer::Data(key_buffer_object); + if (key_buffer_data == NULL) { + // empty string key buffer should not end up as null key + v8::Local key_buffer_object_emptystring = Nan::NewBuffer(new char[0], 0).ToLocalChecked(); + key_buffer_length = node::Buffer::Length(key_buffer_object_emptystring); + key_buffer_data = node::Buffer::Data(key_buffer_object_emptystring); + } } else { // If it was a string just use the utf8 value. v8::Local val = Nan::To(info[3]).ToLocalChecked();