Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty string value/key shouldn't be treated as null #773

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions e2e/both.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/producer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Object> 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;
Expand All @@ -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<v8::Object> 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<v8::String> val = Nan::To<v8::String>(info[3]).ToLocalChecked();
Expand Down