pulsar-client 1.6.2 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -7
- package/binding.gyp +23 -5
- package/docker-tests.sh +4 -3
- package/index.d.ts +5 -4
- package/package.json +4 -10
- package/pulsar-test-service-start.sh +1 -1
- package/pulsar-version.txt +1 -1
- package/run-unit-tests.sh +1 -1
- package/src/Client.cc +84 -58
- package/src/Client.h +6 -4
- package/src/Consumer.cc +260 -231
- package/src/Consumer.h +9 -9
- package/src/ConsumerConfig.cc +33 -32
- package/src/ConsumerConfig.h +5 -6
- package/src/Message.cc +26 -29
- package/src/Message.h +4 -4
- package/src/MessageId.cc +19 -22
- package/src/MessageId.h +5 -6
- package/src/MessageListener.h +3 -8
- package/src/Producer.cc +116 -133
- package/src/Producer.h +3 -3
- package/src/ProducerConfig.cc +33 -22
- package/src/ProducerConfig.h +2 -2
- package/src/Reader.cc +98 -129
- package/src/Reader.h +3 -3
- package/src/ReaderConfig.cc +14 -20
- package/src/ReaderConfig.h +5 -6
- package/src/ReaderListener.h +2 -7
- package/src/SchemaInfo.cc +78 -0
- package/src/SchemaInfo.h +41 -0
- package/src/ThreadSafeDeferred.cc +98 -0
- package/src/ThreadSafeDeferred.h +85 -0
- package/tests/consumer.test.js +2 -2
- package/tests/end_to_end.test.js +2 -2
- package/tests/producer.test.js +2 -2
package/src/ConsumerConfig.cc
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
#include "ConsumerConfig.h"
|
|
21
21
|
#include "Consumer.h"
|
|
22
|
+
#include "SchemaInfo.h"
|
|
22
23
|
#include "Message.h"
|
|
23
24
|
#include <pulsar/c/consumer_configuration.h>
|
|
24
25
|
#include <pulsar/c/consumer.h>
|
|
@@ -38,6 +39,7 @@ static const std::string CFG_CONSUMER_NAME = "consumerName";
|
|
|
38
39
|
static const std::string CFG_PROPS = "properties";
|
|
39
40
|
static const std::string CFG_LISTENER = "listener";
|
|
40
41
|
static const std::string CFG_READ_COMPACTED = "readCompacted";
|
|
42
|
+
static const std::string CFG_SCHEMA = "schema";
|
|
41
43
|
static const std::string CFG_PRIVATE_KEY_PATH = "privateKeyPath";
|
|
42
44
|
static const std::string CFG_CRYPTO_FAILURE_ACTION = "cryptoFailureAction";
|
|
43
45
|
|
|
@@ -56,18 +58,17 @@ static const std::map<std::string, pulsar_consumer_crypto_failure_action> CONSUM
|
|
|
56
58
|
{"CONSUME", pulsar_ConsumerConsume},
|
|
57
59
|
};
|
|
58
60
|
|
|
59
|
-
void FinalizeListenerCallback(Napi::Env env,
|
|
61
|
+
void FinalizeListenerCallback(Napi::Env env, MessageListenerCallback *cb, void *) { delete cb; }
|
|
60
62
|
|
|
61
|
-
ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
62
|
-
std::shared_ptr<CConsumerWrapper> consumerWrapper,
|
|
63
|
-
pulsar_message_listener messageListener)
|
|
63
|
+
ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig, pulsar_message_listener messageListener)
|
|
64
64
|
: topic(""),
|
|
65
65
|
topicsPattern(""),
|
|
66
66
|
subscription(""),
|
|
67
67
|
ackTimeoutMs(0),
|
|
68
68
|
nAckRedeliverTimeoutMs(60000),
|
|
69
69
|
listener(nullptr) {
|
|
70
|
-
this->cConsumerConfig =
|
|
70
|
+
this->cConsumerConfig = std::shared_ptr<pulsar_consumer_configuration_t>(
|
|
71
|
+
pulsar_consumer_configuration_create(), pulsar_consumer_configuration_free);
|
|
71
72
|
|
|
72
73
|
if (consumerConfig.Has(CFG_TOPIC) && consumerConfig.Get(CFG_TOPIC).IsString()) {
|
|
73
74
|
this->topic = consumerConfig.Get(CFG_TOPIC).ToString().Utf8Value();
|
|
@@ -93,7 +94,7 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
|
93
94
|
if (consumerConfig.Has(CFG_SUBSCRIPTION_TYPE) && consumerConfig.Get(CFG_SUBSCRIPTION_TYPE).IsString()) {
|
|
94
95
|
std::string subscriptionType = consumerConfig.Get(CFG_SUBSCRIPTION_TYPE).ToString().Utf8Value();
|
|
95
96
|
if (SUBSCRIPTION_TYPE.count(subscriptionType)) {
|
|
96
|
-
pulsar_consumer_configuration_set_consumer_type(this->cConsumerConfig,
|
|
97
|
+
pulsar_consumer_configuration_set_consumer_type(this->cConsumerConfig.get(),
|
|
97
98
|
SUBSCRIPTION_TYPE.at(subscriptionType));
|
|
98
99
|
}
|
|
99
100
|
}
|
|
@@ -101,20 +102,21 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
|
101
102
|
if (consumerConfig.Has(CFG_INIT_POSITION) && consumerConfig.Get(CFG_INIT_POSITION).IsString()) {
|
|
102
103
|
std::string initPosition = consumerConfig.Get(CFG_INIT_POSITION).ToString().Utf8Value();
|
|
103
104
|
if (INIT_POSITION.count(initPosition)) {
|
|
104
|
-
pulsar_consumer_set_subscription_initial_position(this->cConsumerConfig,
|
|
105
|
+
pulsar_consumer_set_subscription_initial_position(this->cConsumerConfig.get(),
|
|
105
106
|
INIT_POSITION.at(initPosition));
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
|
|
109
110
|
if (consumerConfig.Has(CFG_CONSUMER_NAME) && consumerConfig.Get(CFG_CONSUMER_NAME).IsString()) {
|
|
110
111
|
std::string consumerName = consumerConfig.Get(CFG_CONSUMER_NAME).ToString().Utf8Value();
|
|
111
|
-
if (!consumerName.empty())
|
|
112
|
+
if (!consumerName.empty())
|
|
113
|
+
pulsar_consumer_set_consumer_name(this->cConsumerConfig.get(), consumerName.c_str());
|
|
112
114
|
}
|
|
113
115
|
|
|
114
116
|
if (consumerConfig.Has(CFG_ACK_TIMEOUT) && consumerConfig.Get(CFG_ACK_TIMEOUT).IsNumber()) {
|
|
115
117
|
this->ackTimeoutMs = consumerConfig.Get(CFG_ACK_TIMEOUT).ToNumber().Int64Value();
|
|
116
118
|
if (this->ackTimeoutMs == 0 || this->ackTimeoutMs >= MIN_ACK_TIMEOUT_MILLIS) {
|
|
117
|
-
pulsar_consumer_set_unacked_messages_timeout_ms(this->cConsumerConfig, this->ackTimeoutMs);
|
|
119
|
+
pulsar_consumer_set_unacked_messages_timeout_ms(this->cConsumerConfig.get(), this->ackTimeoutMs);
|
|
118
120
|
}
|
|
119
121
|
}
|
|
120
122
|
|
|
@@ -122,7 +124,7 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
|
122
124
|
consumerConfig.Get(CFG_NACK_REDELIVER_TIMEOUT).IsNumber()) {
|
|
123
125
|
this->nAckRedeliverTimeoutMs = consumerConfig.Get(CFG_NACK_REDELIVER_TIMEOUT).ToNumber().Int64Value();
|
|
124
126
|
if (this->nAckRedeliverTimeoutMs >= 0) {
|
|
125
|
-
pulsar_configure_set_negative_ack_redelivery_delay_ms(this->cConsumerConfig,
|
|
127
|
+
pulsar_configure_set_negative_ack_redelivery_delay_ms(this->cConsumerConfig.get(),
|
|
126
128
|
this->nAckRedeliverTimeoutMs);
|
|
127
129
|
}
|
|
128
130
|
}
|
|
@@ -130,7 +132,7 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
|
130
132
|
if (consumerConfig.Has(CFG_RECV_QUEUE) && consumerConfig.Get(CFG_RECV_QUEUE).IsNumber()) {
|
|
131
133
|
int32_t receiverQueueSize = consumerConfig.Get(CFG_RECV_QUEUE).ToNumber().Int32Value();
|
|
132
134
|
if (receiverQueueSize >= 0) {
|
|
133
|
-
pulsar_consumer_configuration_set_receiver_queue_size(this->cConsumerConfig, receiverQueueSize);
|
|
135
|
+
pulsar_consumer_configuration_set_receiver_queue_size(this->cConsumerConfig.get(), receiverQueueSize);
|
|
134
136
|
}
|
|
135
137
|
}
|
|
136
138
|
|
|
@@ -139,11 +141,17 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
|
139
141
|
int32_t receiverQueueSizeAcrossPartitions =
|
|
140
142
|
consumerConfig.Get(CFG_RECV_QUEUE_ACROSS_PARTITIONS).ToNumber().Int32Value();
|
|
141
143
|
if (receiverQueueSizeAcrossPartitions >= 0) {
|
|
142
|
-
pulsar_consumer_set_max_total_receiver_queue_size_across_partitions(this->cConsumerConfig,
|
|
144
|
+
pulsar_consumer_set_max_total_receiver_queue_size_across_partitions(this->cConsumerConfig.get(),
|
|
143
145
|
receiverQueueSizeAcrossPartitions);
|
|
144
146
|
}
|
|
145
147
|
}
|
|
146
148
|
|
|
149
|
+
if (consumerConfig.Has(CFG_SCHEMA) && consumerConfig.Get(CFG_SCHEMA).IsObject()) {
|
|
150
|
+
SchemaInfo *schemaInfo = new SchemaInfo(consumerConfig.Get(CFG_SCHEMA).ToObject());
|
|
151
|
+
schemaInfo->SetConsumerSchema(this->cConsumerConfig);
|
|
152
|
+
delete schemaInfo;
|
|
153
|
+
}
|
|
154
|
+
|
|
147
155
|
if (consumerConfig.Has(CFG_PROPS) && consumerConfig.Get(CFG_PROPS).IsObject()) {
|
|
148
156
|
Napi::Object propObj = consumerConfig.Get(CFG_PROPS).ToObject();
|
|
149
157
|
Napi::Array arr = propObj.GetPropertyNames();
|
|
@@ -151,69 +159,62 @@ ConsumerConfig::ConsumerConfig(const Napi::Object &consumerConfig,
|
|
|
151
159
|
for (int i = 0; i < size; i++) {
|
|
152
160
|
std::string key = arr.Get(i).ToString().Utf8Value();
|
|
153
161
|
std::string value = propObj.Get(key).ToString().Utf8Value();
|
|
154
|
-
pulsar_consumer_configuration_set_property(this->cConsumerConfig, key.c_str(), value.c_str());
|
|
162
|
+
pulsar_consumer_configuration_set_property(this->cConsumerConfig.get(), key.c_str(), value.c_str());
|
|
155
163
|
}
|
|
156
164
|
}
|
|
157
165
|
|
|
158
166
|
if (consumerConfig.Has(CFG_LISTENER) && consumerConfig.Get(CFG_LISTENER).IsFunction()) {
|
|
159
|
-
this->listener = new
|
|
167
|
+
this->listener = new MessageListenerCallback();
|
|
160
168
|
Napi::ThreadSafeFunction callback = Napi::ThreadSafeFunction::New(
|
|
161
169
|
consumerConfig.Env(), consumerConfig.Get(CFG_LISTENER).As<Napi::Function>(), "Listener Callback", 1,
|
|
162
170
|
1, (void *)NULL, FinalizeListenerCallback, listener);
|
|
163
171
|
this->listener->callback = std::move(callback);
|
|
164
|
-
pulsar_consumer_configuration_set_message_listener(this->cConsumerConfig, messageListener,
|
|
172
|
+
pulsar_consumer_configuration_set_message_listener(this->cConsumerConfig.get(), messageListener,
|
|
165
173
|
this->listener);
|
|
166
174
|
}
|
|
167
175
|
|
|
168
176
|
if (consumerConfig.Has(CFG_READ_COMPACTED) && consumerConfig.Get(CFG_READ_COMPACTED).IsBoolean()) {
|
|
169
177
|
bool readCompacted = consumerConfig.Get(CFG_READ_COMPACTED).ToBoolean();
|
|
170
178
|
if (readCompacted) {
|
|
171
|
-
pulsar_consumer_set_read_compacted(this->cConsumerConfig, 1);
|
|
179
|
+
pulsar_consumer_set_read_compacted(this->cConsumerConfig.get(), 1);
|
|
172
180
|
}
|
|
173
181
|
}
|
|
174
182
|
|
|
175
183
|
if (consumerConfig.Has(CFG_PRIVATE_KEY_PATH) && consumerConfig.Get(CFG_PRIVATE_KEY_PATH).IsString()) {
|
|
176
184
|
std::string publicKeyPath = "";
|
|
177
185
|
std::string privateKeyPath = consumerConfig.Get(CFG_PRIVATE_KEY_PATH).ToString().Utf8Value();
|
|
178
|
-
pulsar_consumer_configuration_set_default_crypto_key_reader(
|
|
179
|
-
|
|
186
|
+
pulsar_consumer_configuration_set_default_crypto_key_reader(
|
|
187
|
+
this->cConsumerConfig.get(), publicKeyPath.c_str(), privateKeyPath.c_str());
|
|
180
188
|
if (consumerConfig.Has(CFG_CRYPTO_FAILURE_ACTION) &&
|
|
181
189
|
consumerConfig.Get(CFG_CRYPTO_FAILURE_ACTION).IsString()) {
|
|
182
190
|
std::string cryptoFailureAction = consumerConfig.Get(CFG_CRYPTO_FAILURE_ACTION).ToString().Utf8Value();
|
|
183
191
|
if (CONSUMER_CRYPTO_FAILURE_ACTION.count(cryptoFailureAction)) {
|
|
184
192
|
pulsar_consumer_configuration_set_crypto_failure_action(
|
|
185
|
-
this->cConsumerConfig, CONSUMER_CRYPTO_FAILURE_ACTION.at(cryptoFailureAction));
|
|
193
|
+
this->cConsumerConfig.get(), CONSUMER_CRYPTO_FAILURE_ACTION.at(cryptoFailureAction));
|
|
186
194
|
}
|
|
187
195
|
}
|
|
188
196
|
}
|
|
189
197
|
}
|
|
190
198
|
|
|
191
199
|
ConsumerConfig::~ConsumerConfig() {
|
|
192
|
-
|
|
193
|
-
if (this->listener) {
|
|
200
|
+
if (this->listener != nullptr) {
|
|
194
201
|
this->listener->callback.Release();
|
|
195
202
|
}
|
|
196
203
|
}
|
|
197
204
|
|
|
198
|
-
pulsar_consumer_configuration_t
|
|
205
|
+
std::shared_ptr<pulsar_consumer_configuration_t> ConsumerConfig::GetCConsumerConfig() {
|
|
206
|
+
return this->cConsumerConfig;
|
|
207
|
+
}
|
|
199
208
|
|
|
200
209
|
std::string ConsumerConfig::GetTopic() { return this->topic; }
|
|
201
210
|
std::vector<std::string> ConsumerConfig::GetTopics() { return this->topics; }
|
|
202
211
|
std::string ConsumerConfig::GetTopicsPattern() { return this->topicsPattern; }
|
|
203
212
|
std::string ConsumerConfig::GetSubscription() { return this->subscription; }
|
|
204
|
-
|
|
205
|
-
|
|
213
|
+
MessageListenerCallback *ConsumerConfig::GetListenerCallback() {
|
|
214
|
+
MessageListenerCallback *cb = this->listener;
|
|
206
215
|
this->listener = nullptr;
|
|
207
216
|
return cb;
|
|
208
217
|
}
|
|
209
218
|
|
|
210
219
|
int64_t ConsumerConfig::GetAckTimeoutMs() { return this->ackTimeoutMs; }
|
|
211
220
|
int64_t ConsumerConfig::GetNAckRedeliverTimeoutMs() { return this->nAckRedeliverTimeoutMs; }
|
|
212
|
-
|
|
213
|
-
CConsumerWrapper::CConsumerWrapper() : cConsumer(nullptr) {}
|
|
214
|
-
|
|
215
|
-
CConsumerWrapper::~CConsumerWrapper() {
|
|
216
|
-
if (this->cConsumer) {
|
|
217
|
-
pulsar_consumer_free(this->cConsumer);
|
|
218
|
-
}
|
|
219
|
-
}
|
package/src/ConsumerConfig.h
CHANGED
|
@@ -27,10 +27,9 @@
|
|
|
27
27
|
|
|
28
28
|
class ConsumerConfig {
|
|
29
29
|
public:
|
|
30
|
-
ConsumerConfig(const Napi::Object &consumerConfig,
|
|
31
|
-
pulsar_message_listener messageListener);
|
|
30
|
+
ConsumerConfig(const Napi::Object &consumerConfig, pulsar_message_listener messageListener);
|
|
32
31
|
~ConsumerConfig();
|
|
33
|
-
pulsar_consumer_configuration_t
|
|
32
|
+
std::shared_ptr<pulsar_consumer_configuration_t> GetCConsumerConfig();
|
|
34
33
|
std::string GetTopic();
|
|
35
34
|
std::vector<std::string> GetTopics();
|
|
36
35
|
std::string GetTopicsPattern();
|
|
@@ -38,17 +37,17 @@ class ConsumerConfig {
|
|
|
38
37
|
int64_t GetAckTimeoutMs();
|
|
39
38
|
int64_t GetNAckRedeliverTimeoutMs();
|
|
40
39
|
|
|
41
|
-
|
|
40
|
+
MessageListenerCallback *GetListenerCallback();
|
|
42
41
|
|
|
43
42
|
private:
|
|
44
|
-
pulsar_consumer_configuration_t
|
|
43
|
+
std::shared_ptr<pulsar_consumer_configuration_t> cConsumerConfig;
|
|
45
44
|
std::string topic;
|
|
46
45
|
std::vector<std::string> topics;
|
|
47
46
|
std::string topicsPattern;
|
|
48
47
|
std::string subscription;
|
|
49
48
|
int64_t ackTimeoutMs;
|
|
50
49
|
int64_t nAckRedeliverTimeoutMs;
|
|
51
|
-
|
|
50
|
+
MessageListenerCallback *listener;
|
|
52
51
|
};
|
|
53
52
|
|
|
54
53
|
#endif
|
package/src/Message.cc
CHANGED
|
@@ -54,7 +54,7 @@ Napi::Object Message::Init(Napi::Env env, Napi::Object exports) {
|
|
|
54
54
|
return exports;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
Napi::Object Message::NewInstance(Napi::Value arg, pulsar_message_t
|
|
57
|
+
Napi::Object Message::NewInstance(Napi::Value arg, std::shared_ptr<pulsar_message_t> cMessage) {
|
|
58
58
|
Napi::Object obj = constructor.New({});
|
|
59
59
|
Message *msg = Unwrap(obj);
|
|
60
60
|
msg->cMessage = cMessage;
|
|
@@ -63,14 +63,14 @@ Napi::Object Message::NewInstance(Napi::Value arg, pulsar_message_t *cMessage) {
|
|
|
63
63
|
|
|
64
64
|
Message::Message(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Message>(info), cMessage(nullptr) {}
|
|
65
65
|
|
|
66
|
-
pulsar_message_t
|
|
66
|
+
std::shared_ptr<pulsar_message_t> Message::GetCMessage() { return this->cMessage; }
|
|
67
67
|
|
|
68
68
|
Napi::Value Message::GetTopicName(const Napi::CallbackInfo &info) {
|
|
69
69
|
Napi::Env env = info.Env();
|
|
70
70
|
if (!ValidateCMessage(env)) {
|
|
71
71
|
return env.Null();
|
|
72
72
|
}
|
|
73
|
-
return Napi::String::New(env, pulsar_message_get_topic_name(this->cMessage));
|
|
73
|
+
return Napi::String::New(env, pulsar_message_get_topic_name(this->cMessage.get()));
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
Napi::Value Message::GetRedeliveryCount(const Napi::CallbackInfo &info) {
|
|
@@ -78,7 +78,7 @@ Napi::Value Message::GetRedeliveryCount(const Napi::CallbackInfo &info) {
|
|
|
78
78
|
if (!ValidateCMessage(env)) {
|
|
79
79
|
return env.Null();
|
|
80
80
|
}
|
|
81
|
-
return Napi::Number::New(env, pulsar_message_get_redelivery_count(this->cMessage));
|
|
81
|
+
return Napi::Number::New(env, pulsar_message_get_redelivery_count(this->cMessage.get()));
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
Napi::Value Message::GetProperties(const Napi::CallbackInfo &info) {
|
|
@@ -87,11 +87,12 @@ Napi::Value Message::GetProperties(const Napi::CallbackInfo &info) {
|
|
|
87
87
|
return env.Null();
|
|
88
88
|
}
|
|
89
89
|
Napi::Array arr = Napi::Array::New(env);
|
|
90
|
-
pulsar_string_map_t *cProperties = pulsar_message_get_properties(this->cMessage);
|
|
90
|
+
pulsar_string_map_t *cProperties = pulsar_message_get_properties(this->cMessage.get());
|
|
91
91
|
int size = pulsar_string_map_size(cProperties);
|
|
92
92
|
for (int i = 0; i < size; i++) {
|
|
93
93
|
arr.Set(pulsar_string_map_get_key(cProperties, i), pulsar_string_map_get_value(cProperties, i));
|
|
94
94
|
}
|
|
95
|
+
pulsar_string_map_free(cProperties);
|
|
95
96
|
return arr;
|
|
96
97
|
}
|
|
97
98
|
|
|
@@ -100,8 +101,8 @@ Napi::Value Message::GetData(const Napi::CallbackInfo &info) {
|
|
|
100
101
|
if (!ValidateCMessage(env)) {
|
|
101
102
|
return env.Null();
|
|
102
103
|
}
|
|
103
|
-
void *data = const_cast<void *>(pulsar_message_get_data(this->cMessage));
|
|
104
|
-
size_t size = (size_t)pulsar_message_get_length(this->cMessage);
|
|
104
|
+
void *data = const_cast<void *>(pulsar_message_get_data(this->cMessage.get()));
|
|
105
|
+
size_t size = (size_t)pulsar_message_get_length(this->cMessage.get());
|
|
105
106
|
return Napi::Buffer<char>::Copy(env, (char *)data, size);
|
|
106
107
|
}
|
|
107
108
|
|
|
@@ -118,7 +119,7 @@ Napi::Value Message::GetEventTimestamp(const Napi::CallbackInfo &info) {
|
|
|
118
119
|
if (!ValidateCMessage(env)) {
|
|
119
120
|
return env.Null();
|
|
120
121
|
}
|
|
121
|
-
return Napi::Number::New(env, pulsar_message_get_event_timestamp(this->cMessage));
|
|
122
|
+
return Napi::Number::New(env, pulsar_message_get_event_timestamp(this->cMessage.get()));
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
Napi::Value Message::GetPublishTimestamp(const Napi::CallbackInfo &info) {
|
|
@@ -126,7 +127,7 @@ Napi::Value Message::GetPublishTimestamp(const Napi::CallbackInfo &info) {
|
|
|
126
127
|
if (!ValidateCMessage(env)) {
|
|
127
128
|
return env.Null();
|
|
128
129
|
}
|
|
129
|
-
return Napi::Number::New(env, pulsar_message_get_publish_timestamp(this->cMessage));
|
|
130
|
+
return Napi::Number::New(env, pulsar_message_get_publish_timestamp(this->cMessage.get()));
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
Napi::Value Message::GetPartitionKey(const Napi::CallbackInfo &info) {
|
|
@@ -134,11 +135,11 @@ Napi::Value Message::GetPartitionKey(const Napi::CallbackInfo &info) {
|
|
|
134
135
|
if (!ValidateCMessage(env)) {
|
|
135
136
|
return env.Null();
|
|
136
137
|
}
|
|
137
|
-
return Napi::String::New(env, pulsar_message_get_partitionKey(this->cMessage));
|
|
138
|
+
return Napi::String::New(env, pulsar_message_get_partitionKey(this->cMessage.get()));
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
bool Message::ValidateCMessage(Napi::Env env) {
|
|
141
|
-
if (this->cMessage) {
|
|
142
|
+
if (this->cMessage.get()) {
|
|
142
143
|
return true;
|
|
143
144
|
} else {
|
|
144
145
|
Napi::Error::New(env, "Message has not been built").ThrowAsJavaScriptException();
|
|
@@ -146,13 +147,13 @@ bool Message::ValidateCMessage(Napi::Env env) {
|
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
149
|
|
|
149
|
-
pulsar_message_t
|
|
150
|
-
pulsar_message_t
|
|
150
|
+
std::shared_ptr<pulsar_message_t> Message::BuildMessage(Napi::Object conf) {
|
|
151
|
+
std::shared_ptr<pulsar_message_t> cMessage(pulsar_message_create(), pulsar_message_free);
|
|
151
152
|
|
|
152
153
|
if (conf.Has(CFG_DATA) && conf.Get(CFG_DATA).IsBuffer()) {
|
|
153
154
|
Napi::Buffer<char> buf = conf.Get(CFG_DATA).As<Napi::Buffer<char>>();
|
|
154
155
|
char *data = buf.Data();
|
|
155
|
-
pulsar_message_set_content(cMessage, data, buf.Length());
|
|
156
|
+
pulsar_message_set_content(cMessage.get(), data, buf.Length());
|
|
156
157
|
}
|
|
157
158
|
|
|
158
159
|
if (conf.Has(CFG_PROPS) && conf.Get(CFG_PROPS).IsObject()) {
|
|
@@ -162,25 +163,25 @@ pulsar_message_t *Message::BuildMessage(Napi::Object conf) {
|
|
|
162
163
|
for (int i = 0; i < size; i++) {
|
|
163
164
|
Napi::String key = arr.Get(i).ToString();
|
|
164
165
|
Napi::String value = propObj.Get(key).ToString();
|
|
165
|
-
pulsar_message_set_property(cMessage, key.Utf8Value().c_str(), value.Utf8Value().c_str());
|
|
166
|
+
pulsar_message_set_property(cMessage.get(), key.Utf8Value().c_str(), value.Utf8Value().c_str());
|
|
166
167
|
}
|
|
167
168
|
}
|
|
168
169
|
|
|
169
170
|
if (conf.Has(CFG_EVENT_TIME) && conf.Get(CFG_EVENT_TIME).IsNumber()) {
|
|
170
171
|
int64_t eventTimestamp = conf.Get(CFG_EVENT_TIME).ToNumber().Int64Value();
|
|
171
172
|
if (eventTimestamp >= 0) {
|
|
172
|
-
pulsar_message_set_event_timestamp(cMessage, eventTimestamp);
|
|
173
|
+
pulsar_message_set_event_timestamp(cMessage.get(), eventTimestamp);
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
|
|
176
177
|
if (conf.Has(CFG_SEQUENCE_ID) && conf.Get(CFG_SEQUENCE_ID).IsNumber()) {
|
|
177
178
|
Napi::Number sequenceId = conf.Get(CFG_SEQUENCE_ID).ToNumber();
|
|
178
|
-
pulsar_message_set_sequence_id(cMessage, sequenceId.Int64Value());
|
|
179
|
+
pulsar_message_set_sequence_id(cMessage.get(), sequenceId.Int64Value());
|
|
179
180
|
}
|
|
180
181
|
|
|
181
182
|
if (conf.Has(CFG_PARTITION_KEY) && conf.Get(CFG_PARTITION_KEY).IsString()) {
|
|
182
183
|
Napi::String partitionKey = conf.Get(CFG_PARTITION_KEY).ToString();
|
|
183
|
-
pulsar_message_set_partition_key(cMessage, partitionKey.Utf8Value().c_str());
|
|
184
|
+
pulsar_message_set_partition_key(cMessage.get(), partitionKey.Utf8Value().c_str());
|
|
184
185
|
}
|
|
185
186
|
|
|
186
187
|
if (conf.Has(CFG_REPL_CLUSTERS) && conf.Get(CFG_REPL_CLUSTERS).IsArray()) {
|
|
@@ -188,44 +189,40 @@ pulsar_message_t *Message::BuildMessage(Napi::Object conf) {
|
|
|
188
189
|
// Empty list means to disable replication
|
|
189
190
|
int length = clusters.Length();
|
|
190
191
|
if (length == 0) {
|
|
191
|
-
pulsar_message_disable_replication(cMessage, 1);
|
|
192
|
+
pulsar_message_disable_replication(cMessage.get(), 1);
|
|
192
193
|
} else {
|
|
193
194
|
char **arr = NewStringArray(length);
|
|
194
195
|
for (int i = 0; i < length; i++) {
|
|
195
196
|
SetString(arr, clusters.Get(i).ToString().Utf8Value().c_str(), i);
|
|
196
197
|
}
|
|
197
|
-
pulsar_message_set_replication_clusters(cMessage, (const char **)arr, length);
|
|
198
|
+
pulsar_message_set_replication_clusters(cMessage.get(), (const char **)arr, length);
|
|
198
199
|
FreeStringArray(arr, length);
|
|
199
200
|
}
|
|
200
201
|
}
|
|
201
202
|
|
|
202
203
|
if (conf.Has(CFG_DELIVER_AFTER) && conf.Get(CFG_DELIVER_AFTER).IsNumber()) {
|
|
203
204
|
Napi::Number deliverAfter = conf.Get(CFG_DELIVER_AFTER).ToNumber();
|
|
204
|
-
pulsar_message_set_deliver_after(cMessage, deliverAfter.Int64Value());
|
|
205
|
+
pulsar_message_set_deliver_after(cMessage.get(), deliverAfter.Int64Value());
|
|
205
206
|
}
|
|
206
207
|
|
|
207
208
|
if (conf.Has(CFG_DELIVER_AT) && conf.Get(CFG_DELIVER_AT).IsNumber()) {
|
|
208
209
|
Napi::Number deliverAt = conf.Get(CFG_DELIVER_AT).ToNumber();
|
|
209
|
-
pulsar_message_set_deliver_at(cMessage, deliverAt.Int64Value());
|
|
210
|
+
pulsar_message_set_deliver_at(cMessage.get(), deliverAt.Int64Value());
|
|
210
211
|
}
|
|
211
212
|
|
|
212
213
|
if (conf.Has(CFG_DISABLE_REPLICATION) && conf.Get(CFG_DISABLE_REPLICATION).IsBoolean()) {
|
|
213
214
|
Napi::Boolean disableReplication = conf.Get(CFG_DISABLE_REPLICATION).ToBoolean();
|
|
214
215
|
if (disableReplication.Value()) {
|
|
215
|
-
pulsar_message_disable_replication(cMessage, 1);
|
|
216
|
+
pulsar_message_disable_replication(cMessage.get(), 1);
|
|
216
217
|
}
|
|
217
218
|
}
|
|
218
219
|
|
|
219
220
|
if (conf.Has(CFG_ORDERING_KEY) && conf.Get(CFG_ORDERING_KEY).IsString()) {
|
|
220
221
|
Napi::String orderingKey = conf.Get(CFG_ORDERING_KEY).ToString();
|
|
221
|
-
pulsar_message_set_ordering_key(cMessage, orderingKey.Utf8Value().c_str());
|
|
222
|
+
pulsar_message_set_ordering_key(cMessage.get(), orderingKey.Utf8Value().c_str());
|
|
222
223
|
}
|
|
223
224
|
|
|
224
225
|
return cMessage;
|
|
225
226
|
}
|
|
226
227
|
|
|
227
|
-
Message::~Message() {
|
|
228
|
-
if (this->cMessage != nullptr) {
|
|
229
|
-
pulsar_message_free(this->cMessage);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
228
|
+
Message::~Message() {}
|
package/src/Message.h
CHANGED
|
@@ -26,16 +26,16 @@
|
|
|
26
26
|
class Message : public Napi::ObjectWrap<Message> {
|
|
27
27
|
public:
|
|
28
28
|
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
|
29
|
-
static Napi::Object NewInstance(Napi::Value arg, pulsar_message_t
|
|
30
|
-
static pulsar_message_t
|
|
29
|
+
static Napi::Object NewInstance(Napi::Value arg, std::shared_ptr<pulsar_message_t> cMessage);
|
|
30
|
+
static std::shared_ptr<pulsar_message_t> BuildMessage(Napi::Object conf);
|
|
31
31
|
Message(const Napi::CallbackInfo &info);
|
|
32
32
|
~Message();
|
|
33
|
-
pulsar_message_t
|
|
33
|
+
std::shared_ptr<pulsar_message_t> GetCMessage();
|
|
34
34
|
|
|
35
35
|
private:
|
|
36
36
|
static Napi::FunctionReference constructor;
|
|
37
37
|
|
|
38
|
-
pulsar_message_t
|
|
38
|
+
std::shared_ptr<pulsar_message_t> cMessage;
|
|
39
39
|
|
|
40
40
|
Napi::Value GetTopicName(const Napi::CallbackInfo &info);
|
|
41
41
|
Napi::Value GetProperties(const Napi::CallbackInfo &info);
|
package/src/MessageId.cc
CHANGED
|
@@ -31,7 +31,6 @@ Napi::Object MessageId::Init(Napi::Env env, Napi::Object exports) {
|
|
|
31
31
|
Napi::Function func = DefineClass(env, "MessageId",
|
|
32
32
|
{StaticMethod("earliest", &MessageId::Earliest, napi_static),
|
|
33
33
|
StaticMethod("latest", &MessageId::Latest, napi_static),
|
|
34
|
-
StaticMethod("finalize", &MessageId::Free, napi_static),
|
|
35
34
|
InstanceMethod("serialize", &MessageId::Serialize),
|
|
36
35
|
StaticMethod("deserialize", &MessageId::Deserialize, napi_static),
|
|
37
36
|
InstanceMethod("toString", &MessageId::ToString)});
|
|
@@ -48,10 +47,13 @@ MessageId::MessageId(const Napi::CallbackInfo &info) : Napi::ObjectWrap<MessageI
|
|
|
48
47
|
Napi::HandleScope scope(env);
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
Napi::Object MessageId::NewInstanceFromMessage(const Napi::CallbackInfo &info,
|
|
50
|
+
Napi::Object MessageId::NewInstanceFromMessage(const Napi::CallbackInfo &info,
|
|
51
|
+
std::shared_ptr<pulsar_message_t> cMessage) {
|
|
52
52
|
Napi::Object obj = NewInstance(info[0]);
|
|
53
53
|
MessageId *msgId = Unwrap(obj);
|
|
54
|
-
|
|
54
|
+
std::shared_ptr<pulsar_message_id_t> cMessageId(pulsar_message_get_message_id(cMessage.get()),
|
|
55
|
+
pulsar_message_id_free);
|
|
56
|
+
msgId->cMessageId = cMessageId;
|
|
55
57
|
return obj;
|
|
56
58
|
}
|
|
57
59
|
|
|
@@ -60,32 +62,29 @@ Napi::Object MessageId::NewInstance(Napi::Value arg) {
|
|
|
60
62
|
return obj;
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
Napi::Object MessageId::NewInstance(pulsar_message_id_t
|
|
65
|
+
Napi::Object MessageId::NewInstance(std::shared_ptr<pulsar_message_id_t> cMessageId) {
|
|
64
66
|
Napi::Object obj = constructor.New({});
|
|
65
67
|
MessageId *msgId = Unwrap(obj);
|
|
66
68
|
msgId->cMessageId = cMessageId;
|
|
67
69
|
return obj;
|
|
68
70
|
}
|
|
69
71
|
|
|
70
|
-
void
|
|
71
|
-
Napi::Object obj = info[0].As<Napi::Object>();
|
|
72
|
-
MessageId *msgId = Unwrap(obj);
|
|
73
|
-
pulsar_message_id_free(msgId->cMessageId);
|
|
74
|
-
}
|
|
72
|
+
void nofree(void *__ptr) {}
|
|
75
73
|
|
|
76
74
|
Napi::Value MessageId::Earliest(const Napi::CallbackInfo &info) {
|
|
77
75
|
Napi::Object obj = NewInstance(info[0]);
|
|
78
76
|
MessageId *msgId = Unwrap(obj);
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
std::shared_ptr<pulsar_message_id_t> cMessageId((pulsar_message_id_t *)pulsar_message_id_earliest(),
|
|
78
|
+
nofree);
|
|
79
|
+
msgId->cMessageId = cMessageId;
|
|
81
80
|
return obj;
|
|
82
81
|
}
|
|
83
82
|
|
|
84
83
|
Napi::Value MessageId::Latest(const Napi::CallbackInfo &info) {
|
|
85
84
|
Napi::Object obj = NewInstance(info[0]);
|
|
86
85
|
MessageId *msgId = Unwrap(obj);
|
|
87
|
-
|
|
88
|
-
msgId->
|
|
86
|
+
std::shared_ptr<pulsar_message_id_t> cMessageId((pulsar_message_id_t *)pulsar_message_id_latest(), nofree);
|
|
87
|
+
msgId->cMessageId = cMessageId;
|
|
89
88
|
return obj;
|
|
90
89
|
}
|
|
91
90
|
|
|
@@ -95,7 +94,7 @@ Napi::Value MessageId::Serialize(const Napi::CallbackInfo &info) {
|
|
|
95
94
|
Napi::Env env = info.Env();
|
|
96
95
|
|
|
97
96
|
int len;
|
|
98
|
-
void *ptr = pulsar_message_id_serialize(GetCMessageId(), &len);
|
|
97
|
+
void *ptr = pulsar_message_id_serialize(GetCMessageId().get(), &len);
|
|
99
98
|
|
|
100
99
|
return Napi::Buffer<char>::New(env, (char *)ptr, len, serializeFinalizeCallback);
|
|
101
100
|
}
|
|
@@ -113,22 +112,20 @@ Napi::Value MessageId::Deserialize(const Napi::CallbackInfo &info) {
|
|
|
113
112
|
|
|
114
113
|
Napi::Buffer<char> buf = info[0].As<Napi::Buffer<char>>();
|
|
115
114
|
char *data = buf.Data();
|
|
116
|
-
|
|
115
|
+
std::shared_ptr<pulsar_message_id_t> cMessageId(pulsar_message_id_deserialize(data, buf.Length()),
|
|
116
|
+
pulsar_message_id_free);
|
|
117
|
+
msgId->cMessageId = cMessageId;
|
|
117
118
|
|
|
118
119
|
return obj;
|
|
119
120
|
}
|
|
120
121
|
|
|
121
|
-
pulsar_message_id_t
|
|
122
|
+
std::shared_ptr<pulsar_message_id_t> MessageId::GetCMessageId() { return this->cMessageId; }
|
|
122
123
|
|
|
123
124
|
Napi::Value MessageId::ToString(const Napi::CallbackInfo &info) {
|
|
124
|
-
char *cStr = pulsar_message_id_str(this->cMessageId);
|
|
125
|
+
char *cStr = pulsar_message_id_str(this->cMessageId.get());
|
|
125
126
|
std::string s(cStr);
|
|
126
127
|
free(cStr);
|
|
127
128
|
return Napi::String::New(info.Env(), s);
|
|
128
129
|
}
|
|
129
130
|
|
|
130
|
-
MessageId::~MessageId() {
|
|
131
|
-
if (!this->skipCMessageIdFree) {
|
|
132
|
-
pulsar_message_id_free(this->cMessageId);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
131
|
+
MessageId::~MessageId() {}
|
package/src/MessageId.h
CHANGED
|
@@ -28,21 +28,20 @@ class MessageId : public Napi::ObjectWrap<MessageId> {
|
|
|
28
28
|
public:
|
|
29
29
|
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
|
30
30
|
static Napi::Object NewInstance(Napi::Value arg);
|
|
31
|
-
static Napi::Object NewInstance(pulsar_message_id_t
|
|
32
|
-
static Napi::Object NewInstanceFromMessage(const Napi::CallbackInfo &info,
|
|
31
|
+
static Napi::Object NewInstance(std::shared_ptr<pulsar_message_id_t> cMessageId);
|
|
32
|
+
static Napi::Object NewInstanceFromMessage(const Napi::CallbackInfo &info,
|
|
33
|
+
std::shared_ptr<pulsar_message_t> cMessage);
|
|
33
34
|
static Napi::Value Earliest(const Napi::CallbackInfo &info);
|
|
34
35
|
static Napi::Value Latest(const Napi::CallbackInfo &info);
|
|
35
36
|
Napi::Value Serialize(const Napi::CallbackInfo &info);
|
|
36
37
|
static Napi::Value Deserialize(const Napi::CallbackInfo &info);
|
|
37
|
-
static void Free(const Napi::CallbackInfo &info);
|
|
38
38
|
MessageId(const Napi::CallbackInfo &info);
|
|
39
39
|
~MessageId();
|
|
40
|
-
pulsar_message_id_t
|
|
40
|
+
std::shared_ptr<pulsar_message_id_t> GetCMessageId();
|
|
41
41
|
|
|
42
42
|
private:
|
|
43
43
|
static Napi::FunctionReference constructor;
|
|
44
|
-
pulsar_message_id_t
|
|
45
|
-
bool skipCMessageIdFree = false;
|
|
44
|
+
std::shared_ptr<pulsar_message_id_t> cMessageId;
|
|
46
45
|
|
|
47
46
|
Napi::Value ToString(const Napi::CallbackInfo &info);
|
|
48
47
|
};
|
package/src/MessageListener.h
CHANGED
|
@@ -21,19 +21,14 @@
|
|
|
21
21
|
#define MESSAGELISTENER_H
|
|
22
22
|
|
|
23
23
|
#include <napi.h>
|
|
24
|
-
#include <pulsar/c/client.h>
|
|
25
24
|
|
|
26
|
-
struct
|
|
27
|
-
pulsar_consumer_t *cConsumer;
|
|
28
|
-
CConsumerWrapper();
|
|
29
|
-
~CConsumerWrapper();
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
struct ListenerCallback {
|
|
25
|
+
struct MessageListenerCallback {
|
|
33
26
|
Napi::ThreadSafeFunction callback;
|
|
34
27
|
|
|
35
28
|
// Using consumer as void* since the ListenerCallback is shared between Config and Consumer.
|
|
36
29
|
void *consumer;
|
|
30
|
+
|
|
31
|
+
MessageListenerCallback() : consumer(nullptr) {}
|
|
37
32
|
};
|
|
38
33
|
|
|
39
34
|
#endif
|