-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathbinding.cc
73 lines (55 loc) · 2.36 KB
/
binding.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* node-rdkafka - Node.js wrapper for RdKafka C/C++ library
*
* Copyright (c) 2016 Blizzard Entertainment
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE.txt file for details.
*/
#include "src/binding.h"
using NodeKafka::Producer;
using NodeKafka::KafkaConsumer;
using NodeKafka::AdminClient;
using NodeKafka::Topic;
using RdKafka::ErrorCode;
NAN_METHOD(NodeRdKafkaErr2Str) {
int points = Nan::To<int>(info[0]).FromJust();
// Cast to error code
RdKafka::ErrorCode err = static_cast<RdKafka::ErrorCode>(points);
std::string errstr = RdKafka::err2str(err);
info.GetReturnValue().Set(Nan::New<v8::String>(errstr).ToLocalChecked());
}
NAN_METHOD(NodeRdKafkaBuildInFeatures) {
RdKafka::Conf * config = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
std::string features;
if (RdKafka::Conf::CONF_OK == config->get("builtin.features", features)) {
info.GetReturnValue().Set(Nan::New<v8::String>(features).ToLocalChecked());
} else {
info.GetReturnValue().Set(Nan::Undefined());
}
delete config;
}
void ConstantsInit(v8::Local<v8::Object> exports) {
v8::Local<v8::Object> topicConstants = Nan::New<v8::Object>();
// RdKafka Error Code definitions
NODE_DEFINE_CONSTANT(topicConstants, RdKafka::Topic::PARTITION_UA);
NODE_DEFINE_CONSTANT(topicConstants, RdKafka::Topic::OFFSET_BEGINNING);
NODE_DEFINE_CONSTANT(topicConstants, RdKafka::Topic::OFFSET_END);
NODE_DEFINE_CONSTANT(topicConstants, RdKafka::Topic::OFFSET_STORED);
NODE_DEFINE_CONSTANT(topicConstants, RdKafka::Topic::OFFSET_INVALID);
Nan::Set(exports, Nan::New("topic").ToLocalChecked(), topicConstants);
Nan::Set(exports, Nan::New("err2str").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(NodeRdKafkaErr2Str)).ToLocalChecked()); // NOLINT
Nan::Set(exports, Nan::New("features").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(NodeRdKafkaBuildInFeatures)).ToLocalChecked()); // NOLINT
}
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Value> m_, void* v_) {
KafkaConsumer::Init(exports);
Producer::Init(exports);
AdminClient::Init(exports);
Topic::Init(exports);
ConstantsInit(exports);
Nan::Set(exports, Nan::New("librdkafkaVersion").ToLocalChecked(),
Nan::New(RdKafka::version_str().c_str()).ToLocalChecked());
}
NODE_MODULE_CONTEXT_AWARE(kafka, Init)