Skip to content

Commit e0bfb56

Browse files
committed
feat: enable Draft API support
1 parent b817aac commit e0bfb56

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ macro(set_option_from_env OPTION_NAME)
2828
message(STATUS "${OPTION_NAME}: ${${OPTION_NAME}}")
2929
endmacro()
3030

31-
option(ZMQ_DRAFT "Build and install draft APIs" OFF)
31+
option(ZMQ_DRAFT "Build and install draft APIs (e.g. `server-client`, `radio-dish`, `scatter-gather`)" ON)
3232
set_option_from_env(ZMQ_DRAFT)
3333

3434
option(ZMQ_CURVE "Enable CURVE security" ON)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Fully usable with TypeScript (3+).
1313
- Compatible with Zeromq 4/5 via "zeromq/v5-compat"
1414
- Secure Curve protocol with Libsodium
15+
- Zeromq Draft API support
1516

1617
## Useful links
1718

@@ -142,6 +143,8 @@ etc.
142143

143144
#### Draft support
144145

146+
(Enabled by default)
147+
145148
By default `libzmq` is built with support for `Draft` patterns (e.g.
146149
`server-client`, `radio-dish`, `scatter-gather`). If you want to build `libzmq`
147150
without support for `Draft`, you can specify the following in `.npmrc`:

src/module.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ struct Terminator {
3232
});
3333

3434
using namespace std::chrono_literals;
35-
if (terminate.wait_for(500ms) == std::future_status::timeout) {
35+
const auto timeout = 500ms;
36+
if (terminate.wait_for(timeout) == std::future_status::timeout) {
3637
/* We can't use process.emitWarning, because the Node.js runtime
3738
has already shut down. So we mimic it instead. */
3839
(void)fprintf(stderr,

src/outgoing_msg.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,18 @@ bool OutgoingMsg::Parts::SetGroup(Napi::Value value) {
119119
auto group = [&]() {
120120
if (value.IsString()) {
121121
return std::string(value.As<Napi::String>());
122-
} else if (value.IsBuffer()) {
123-
Napi::Object buf = value.As<Napi::Object>();
122+
}
123+
if (value.IsBuffer()) {
124+
auto buf = value.As<Napi::Object>();
124125
auto length = buf.As<Napi::Buffer<char>>().Length();
125-
auto value = buf.As<Napi::Buffer<char>>().Data();
126+
auto* value = buf.As<Napi::Buffer<char>>().Data();
126127
return std::string(value, length);
127-
} else {
128-
return std::string();
129128
}
129+
return std::string();
130130
}();
131131

132132
for (auto& part : parts) {
133-
if (zmq_msg_set_group(part, group.c_str()) < 0) {
133+
if (zmq_msg_set_group(part.get(), group.c_str()) < 0) {
134134
ErrnoException(value.Env(), zmq_errno()).ThrowAsJavaScriptException();
135135
return false;
136136
}
@@ -141,14 +141,15 @@ bool OutgoingMsg::Parts::SetGroup(Napi::Value value) {
141141

142142
bool OutgoingMsg::Parts::SetRoutingId(Napi::Value value) {
143143
if (value.IsUndefined()) {
144+
// https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-length.html
144145
ErrnoException(value.Env(), EINVAL).ThrowAsJavaScriptException();
145146
return false;
146147
}
147148

148-
auto id = value.As<Napi::Number>().Uint32Value();
149+
auto routing_id = value.As<Napi::Number>().Uint32Value();
149150

150151
for (auto& part : parts) {
151-
if (zmq_msg_set_routing_id(part, id) < 0) {
152+
if (zmq_msg_set_routing_id(part.get(), routing_id) < 0) {
152153
ErrnoException(value.Env(), zmq_errno()).ThrowAsJavaScriptException();
153154
return false;
154155
}

src/socket.cc

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ Socket::Socket(const Napi::CallbackInfo& info)
103103
}
104104

105105
uv_os_sock_t file_descriptor = 0;
106-
std::function<void()> const finalize = nullptr;
107106

108107
const auto error = [this]() {
109108
[[maybe_unused]] auto err = zmq_close(socket);
@@ -125,20 +124,22 @@ Socket::Socket(const Napi::CallbackInfo& info)
125124
}
126125
#endif
127126

127+
std::function<void()> finalize = nullptr;
128+
128129
/* Currently only some DRAFT sockets are threadsafe. */
129130
if (thread_safe) {
130131
#ifdef ZMQ_HAS_POLLABLE_THREAD_SAFE
131132
/* Threadsafe sockets do not expose an FD we can integrate into the
132133
event loop, so we have to construct one by creating a zmq_poller. */
133-
auto poll = zmq_poller_new();
134+
auto* poll = zmq_poller_new();
134135
if (poll == nullptr) {
135136
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
136137
error();
137138
}
138139

139140
/* Callback to free the underlying poller. Move the poller to transfer
140141
ownership after the constructor has completed. */
141-
finalize = [=]() mutable {
142+
finalize = [&]() {
142143
[[maybe_unused]] auto err = zmq_poller_destroy(&poll);
143144
assert(err == 0);
144145
};
@@ -149,7 +150,7 @@ Socket::Socket(const Napi::CallbackInfo& info)
149150
error();
150151
}
151152

152-
if (zmq_poller_fd(poll, &fd) < 0) {
153+
if (zmq_poller_fd(poll, &file_descriptor) < 0) {
153154
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
154155
finalize();
155156
error();
@@ -327,17 +328,17 @@ void Socket::Receive(const Napi::Promise::Deferred& res) {
327328
switch (type) {
328329
case ZMQ_SERVER: {
329330
auto meta = Napi::Object::New(Env());
330-
meta.Set("routingId", zmq_msg_routing_id(part));
331-
list[i++] = meta;
331+
meta.Set("routingId", zmq_msg_routing_id(part.get()));
332+
list[i_part++] = meta;
332333
break;
333334
}
334335

335336
case ZMQ_DISH: {
336337
auto meta = Napi::Object::New(Env());
337-
auto data = zmq_msg_group(part);
338+
const auto* data = zmq_msg_group(part.get());
338339
auto length = strnlen(data, ZMQ_GROUP_MAX_LENGTH);
339340
meta.Set("group", Napi::Buffer<char>::Copy(Env(), data, length));
340-
list[i++] = meta;
341+
list[i_part++] = meta;
341342
break;
342343
}
343344
}
@@ -610,7 +611,9 @@ Napi::Value Socket::Send(const Napi::CallbackInfo& info) {
610611
Arg::Required<Arg::Object>("Options must be an object"),
611612
};
612613

613-
if (args.ThrowIfInvalid(info)) return Env().Undefined();
614+
if (args.ThrowIfInvalid(info)) {
615+
return Env().Undefined();
616+
}
614617

615618
break;
616619
}
@@ -752,19 +755,22 @@ void Socket::Join([[maybe_unused]] const Napi::CallbackInfo& info) {
752755
Arg::Required<Arg::String, Arg::Buffer>("Group must be a string or buffer"),
753756
};
754757

755-
if (args.ThrowIfInvalid(info)) return;
758+
if (args.ThrowIfInvalid(info)) {
759+
return;
760+
}
756761

757-
if (!ValidateOpen()) return;
762+
if (!ValidateOpen()) {
763+
return;
764+
}
758765

759766
auto str = [&]() {
760767
if (info[0].IsString()) {
761768
return std::string(info[0].As<Napi::String>());
762-
} else {
763-
Napi::Object buf = info[0].As<Napi::Object>();
764-
auto length = buf.As<Napi::Buffer<char>>().Length();
765-
auto value = buf.As<Napi::Buffer<char>>().Data();
766-
return std::string(value, length);
767769
}
770+
auto buf = info[0].As<Napi::Object>();
771+
auto length = buf.As<Napi::Buffer<char>>().Length();
772+
auto* value = buf.As<Napi::Buffer<char>>().Data();
773+
return std::string(value, length);
768774
}();
769775

770776
if (zmq_join(socket, str.c_str()) < 0) {
@@ -780,19 +786,22 @@ void Socket::Leave([[maybe_unused]] const Napi::CallbackInfo& info) {
780786
Arg::Required<Arg::String, Arg::Buffer>("Group must be a string or buffer"),
781787
};
782788

783-
if (args.ThrowIfInvalid(info)) return;
789+
if (args.ThrowIfInvalid(info)) {
790+
return;
791+
}
784792

785-
if (!ValidateOpen()) return;
793+
if (!ValidateOpen()) {
794+
return;
795+
}
786796

787797
auto str = [&]() {
788798
if (info[0].IsString()) {
789799
return std::string(info[0].As<Napi::String>());
790-
} else {
791-
Napi::Object buf = info[0].As<Napi::Object>();
792-
auto length = buf.As<Napi::Buffer<char>>().Length();
793-
auto value = buf.As<Napi::Buffer<char>>().Data();
794-
return std::string(value, length);
795800
}
801+
auto buf = info[0].As<Napi::Object>();
802+
auto length = buf.As<Napi::Buffer<char>>().Length();
803+
auto* value = buf.As<Napi::Buffer<char>>().Data();
804+
return std::string(value, length);
796805
}();
797806

798807
if (zmq_leave(socket, str.c_str()) < 0) {

0 commit comments

Comments
 (0)