rclnodejs 0.32.5 → 1.0.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.
Files changed (68) hide show
  1. package/README.md +3 -3
  2. package/binding.gyp +17 -2
  3. package/lib/client.js +2 -3
  4. package/lib/context.js +8 -0
  5. package/lib/logging.js +26 -9
  6. package/lib/node.js +68 -1
  7. package/lib/publisher.js +8 -0
  8. package/lib/service.js +9 -2
  9. package/lib/subscription.js +8 -0
  10. package/lib/timer.js +32 -0
  11. package/package.json +4 -4
  12. package/rostsd_gen/index.js +167 -67
  13. package/scripts/config.js +1 -0
  14. package/scripts/npmjs-readme.md +3 -3
  15. package/src/addon.cpp +54 -53
  16. package/src/executor.cpp +19 -10
  17. package/src/{executor.hpp → executor.h} +7 -5
  18. package/src/handle_manager.cpp +30 -56
  19. package/src/{handle_manager.hpp → handle_manager.h} +8 -7
  20. package/src/{macros.hpp → macros.h} +7 -5
  21. package/src/rcl_action_client_bindings.cpp +231 -0
  22. package/src/{rcl_action_bindings.hpp → rcl_action_client_bindings.h} +6 -11
  23. package/src/rcl_action_goal_bindings.cpp +117 -0
  24. package/src/rcl_action_goal_bindings.h +26 -0
  25. package/src/rcl_action_server_bindings.cpp +470 -0
  26. package/src/rcl_action_server_bindings.h +26 -0
  27. package/src/rcl_bindings.cpp +42 -1979
  28. package/src/{rcl_bindings.hpp → rcl_bindings.h} +5 -25
  29. package/src/rcl_client_bindings.cpp +183 -0
  30. package/src/rcl_client_bindings.h +26 -0
  31. package/src/rcl_context_bindings.cpp +156 -0
  32. package/src/rcl_context_bindings.h +26 -0
  33. package/src/rcl_graph_bindings.cpp +280 -0
  34. package/src/rcl_graph_bindings.h +26 -0
  35. package/src/rcl_guard_condition_bindings.cpp +75 -0
  36. package/src/rcl_guard_condition_bindings.h +26 -0
  37. package/src/rcl_handle.cpp +41 -57
  38. package/src/{rcl_handle.hpp → rcl_handle.h} +18 -17
  39. package/src/rcl_lifecycle_bindings.cpp +135 -114
  40. package/src/{rcl_lifecycle_bindings.hpp → rcl_lifecycle_bindings.h} +5 -7
  41. package/src/rcl_logging_bindings.cpp +96 -0
  42. package/src/rcl_logging_bindings.h +26 -0
  43. package/src/rcl_names_bindings.cpp +255 -0
  44. package/src/rcl_names_bindings.h +26 -0
  45. package/src/rcl_node_bindings.cpp +447 -0
  46. package/src/rcl_node_bindings.h +26 -0
  47. package/src/rcl_publisher_bindings.cpp +141 -0
  48. package/src/rcl_publisher_bindings.h +26 -0
  49. package/src/rcl_service_bindings.cpp +185 -0
  50. package/src/rcl_service_bindings.h +26 -0
  51. package/src/rcl_subscription_bindings.cpp +335 -0
  52. package/src/rcl_subscription_bindings.h +26 -0
  53. package/src/rcl_time_point_bindings.cpp +194 -0
  54. package/src/rcl_time_point_bindings.h +26 -0
  55. package/src/rcl_timer_bindings.cpp +237 -0
  56. package/src/rcl_timer_bindings.h +26 -0
  57. package/src/rcl_utilities.cpp +166 -1
  58. package/src/{rcl_utilities.hpp → rcl_utilities.h} +21 -3
  59. package/src/shadow_node.cpp +56 -75
  60. package/src/{shadow_node.hpp → shadow_node.h} +18 -17
  61. package/types/context.d.ts +6 -0
  62. package/types/interfaces.d.ts +4377 -0
  63. package/types/node.d.ts +53 -0
  64. package/types/publisher.d.ts +6 -0
  65. package/types/service.d.ts +6 -0
  66. package/types/subscription.d.ts +6 -0
  67. package/types/timer.d.ts +18 -0
  68. package/src/rcl_action_bindings.cpp +0 -826
@@ -0,0 +1,185 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "rcl_service_bindings.h"
16
+
17
+ #include <rcl/error_handling.h>
18
+ #include <rcl/rcl.h>
19
+ #if ROS_VERSION > 2205
20
+ #include <rcl/service_introspection.h>
21
+ #endif
22
+
23
+ #include <memory>
24
+ #include <string>
25
+
26
+ #include "macros.h"
27
+ #include "rcl_handle.h"
28
+ #include "rcl_utilities.h"
29
+
30
+ namespace rclnodejs {
31
+
32
+ Napi::Value CreateService(const Napi::CallbackInfo& info) {
33
+ Napi::Env env = info.Env();
34
+
35
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
36
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
37
+
38
+ std::string service_name = info[1].As<Napi::String>().Utf8Value();
39
+ std::string interface_name = info[2].As<Napi::String>().Utf8Value();
40
+ std::string package_name = info[3].As<Napi::String>().Utf8Value();
41
+
42
+ const rosidl_service_type_support_t* ts =
43
+ GetServiceTypeSupport(package_name, interface_name);
44
+
45
+ if (ts) {
46
+ rcl_service_t* service =
47
+ reinterpret_cast<rcl_service_t*>(malloc(sizeof(rcl_service_t)));
48
+ *service = rcl_get_zero_initialized_service();
49
+ rcl_service_options_t service_ops = rcl_service_get_default_options();
50
+ auto qos_profile = GetQoSProfile(info[4]);
51
+ if (qos_profile) {
52
+ service_ops.qos = *qos_profile;
53
+ }
54
+
55
+ THROW_ERROR_IF_NOT_EQUAL(
56
+ rcl_service_init(service, node, ts, service_name.c_str(), &service_ops),
57
+ RCL_RET_OK, rcl_get_error_string().str);
58
+ auto js_obj =
59
+ RclHandle::NewInstance(env, service, node_handle, [node](void* ptr) {
60
+ rcl_service_t* service = reinterpret_cast<rcl_service_t*>(ptr);
61
+ rcl_ret_t ret = rcl_service_fini(service, node);
62
+ free(ptr);
63
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, ret, rcl_get_error_string().str);
64
+ });
65
+
66
+ return js_obj;
67
+ } else {
68
+ Napi::Error::New(env, GetErrorMessageAndClear())
69
+ .ThrowAsJavaScriptException();
70
+ return env.Undefined();
71
+ }
72
+ }
73
+
74
+ Napi::Value RclTakeRequest(const Napi::CallbackInfo& info) {
75
+ Napi::Env env = info.Env();
76
+
77
+ rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
78
+ RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
79
+ rmw_request_id_t* header =
80
+ reinterpret_cast<rmw_request_id_t*>(malloc(sizeof(rmw_request_id_t)));
81
+
82
+ void* taken_request = info[2].As<Napi::Buffer<char>>().Data();
83
+ rcl_ret_t ret = rcl_take_request(service, header, taken_request);
84
+ if (ret != RCL_RET_SERVICE_TAKE_FAILED) {
85
+ auto js_obj = RclHandle::NewInstance(env, header, nullptr,
86
+ [](void* ptr) { free(ptr); });
87
+ return js_obj;
88
+ }
89
+
90
+ return env.Undefined();
91
+ }
92
+
93
+ Napi::Value SendResponse(const Napi::CallbackInfo& info) {
94
+ Napi::Env env = info.Env();
95
+
96
+ rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
97
+ RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
98
+ void* buffer = info[1].As<Napi::Buffer<char>>().Data();
99
+
100
+ rmw_request_id_t* header = reinterpret_cast<rmw_request_id_t*>(
101
+ RclHandle::Unwrap(info[2].As<Napi::Object>())->ptr());
102
+
103
+ THROW_ERROR_IF_NOT_EQUAL(rcl_send_response(service, header, buffer),
104
+ RCL_RET_OK, rcl_get_error_string().str);
105
+
106
+ return env.Undefined();
107
+ }
108
+
109
+ Napi::Value GetServiceServiceName(const Napi::CallbackInfo& info) {
110
+ Napi::Env env = info.Env();
111
+
112
+ rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
113
+ RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
114
+
115
+ const char* service_name = rcl_service_get_service_name(service);
116
+ return Napi::String::New(env, service_name);
117
+ }
118
+
119
+ #if ROS_VERSION > 2205 // 2205 == Humble
120
+ Napi::Value ConfigureServiceIntrospection(const Napi::CallbackInfo& info) {
121
+ Napi::Env env = info.Env();
122
+
123
+ RclHandle* service_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
124
+ rcl_service_t* service =
125
+ reinterpret_cast<rcl_service_t*>(service_handle->ptr());
126
+ RclHandle* node_handle = RclHandle::Unwrap(info[1].As<Napi::Object>());
127
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
128
+
129
+ rcl_clock_t* clock = reinterpret_cast<rcl_clock_t*>(
130
+ RclHandle::Unwrap(info[2].As<Napi::Object>())->ptr());
131
+
132
+ std::string interface_name = info[3].As<Napi::String>().Utf8Value();
133
+ std::string package_name = info[4].As<Napi::String>().Utf8Value();
134
+ const rosidl_service_type_support_t* ts =
135
+ GetServiceTypeSupport(package_name, interface_name);
136
+
137
+ if (ts) {
138
+ rcl_publisher_options_t publisher_ops = rcl_publisher_get_default_options();
139
+ auto qos_profile = GetQoSProfile(info[5]);
140
+ if (qos_profile) {
141
+ publisher_ops.qos = *qos_profile;
142
+ }
143
+ rcl_service_introspection_state_t state =
144
+ static_cast<rcl_service_introspection_state_t>(
145
+ info[6].As<Napi::Number>().Uint32Value());
146
+
147
+ THROW_ERROR_IF_NOT_EQUAL(
148
+ rcl_service_configure_service_introspection(service, node, clock, ts,
149
+ publisher_ops, state),
150
+ RCL_RET_OK, rcl_get_error_string().str);
151
+ } else {
152
+ Napi::Error::New(env, GetErrorMessageAndClear())
153
+ .ThrowAsJavaScriptException();
154
+ }
155
+ return env.Undefined();
156
+ }
157
+ #endif
158
+
159
+ Napi::Value GetOptions(const Napi::CallbackInfo& info) {
160
+ Napi::Env env = info.Env();
161
+
162
+ rcl_service_t* service = reinterpret_cast<rcl_service_t*>(
163
+ RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
164
+
165
+ const rcl_service_options_t* options = rcl_service_get_options(service);
166
+ auto qos_profile = ConvertToQoS(env, &options->qos);
167
+
168
+ return qos_profile;
169
+ }
170
+
171
+ Napi::Object InitServiceBindings(Napi::Env env, Napi::Object exports) {
172
+ exports.Set("createService", Napi::Function::New(env, CreateService));
173
+ exports.Set("rclTakeRequest", Napi::Function::New(env, RclTakeRequest));
174
+ exports.Set("sendResponse", Napi::Function::New(env, SendResponse));
175
+ exports.Set("getServiceServiceName",
176
+ Napi::Function::New(env, GetServiceServiceName));
177
+ #if ROS_VERSION > 2205 // 2205 == Humble
178
+ exports.Set("configureServiceIntrospection",
179
+ Napi::Function::New(env, ConfigureServiceIntrospection));
180
+ #endif
181
+ exports.Set("getOptions", Napi::Function::New(env, GetOptions));
182
+ return exports;
183
+ }
184
+
185
+ } // namespace rclnodejs
@@ -0,0 +1,26 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef SRC_RCL_SERVICE_BINDINGS_H_
16
+ #define SRC_RCL_SERVICE_BINDINGS_H_
17
+
18
+ #include <napi.h>
19
+
20
+ namespace rclnodejs {
21
+
22
+ Napi::Object InitServiceBindings(Napi::Env env, Napi::Object exports);
23
+
24
+ }
25
+
26
+ #endif // SRC_RCL_SERVICE_BINDINGS_H_
@@ -0,0 +1,335 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #include "rcl_subscription_bindings.h"
16
+
17
+ #include <rcl/error_handling.h>
18
+ #include <rcl/rcl.h>
19
+
20
+ #include <cstdio>
21
+ #include <memory>
22
+ #include <string>
23
+
24
+ #include "macros.h"
25
+ #include "rcl_handle.h"
26
+ #include "rcl_utilities.h"
27
+
28
+ namespace rclnodejs {
29
+
30
+ Napi::Value RclTake(const Napi::CallbackInfo& info) {
31
+ Napi::Env env = info.Env();
32
+
33
+ RclHandle* subscription_handle =
34
+ RclHandle::Unwrap(info[0].As<Napi::Object>());
35
+ rcl_subscription_t* subscription =
36
+ reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr());
37
+ void* msg_taken = info[1].As<Napi::Buffer<char>>().Data();
38
+ rcl_ret_t ret = rcl_take(subscription, msg_taken, nullptr, nullptr);
39
+
40
+ if (ret != RCL_RET_OK && ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) {
41
+ rcl_reset_error();
42
+ Napi::Error::New(env, rcl_get_error_string().str)
43
+ .ThrowAsJavaScriptException();
44
+ return Napi::Boolean::New(env, false);
45
+ }
46
+
47
+ if (ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) {
48
+ return Napi::Boolean::New(env, true);
49
+ }
50
+
51
+ return env.Undefined();
52
+ }
53
+
54
+ Napi::Value CreateSubscription(const Napi::CallbackInfo& info) {
55
+ Napi::Env env = info.Env();
56
+
57
+ RclHandle* node_handle = RclHandle::Unwrap(info[0].As<Napi::Object>());
58
+ rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
59
+
60
+ std::string package_name = info[1].As<Napi::String>().Utf8Value();
61
+ std::string message_sub_folder = info[2].As<Napi::String>().Utf8Value();
62
+ std::string message_name = info[3].As<Napi::String>().Utf8Value();
63
+ std::string topic = info[4].As<Napi::String>().Utf8Value();
64
+ Napi::Object options = info[5].As<Napi::Object>();
65
+
66
+ rcl_subscription_t* subscription =
67
+ reinterpret_cast<rcl_subscription_t*>(malloc(sizeof(rcl_subscription_t)));
68
+ *subscription = rcl_get_zero_initialized_subscription();
69
+
70
+ rcl_subscription_options_t subscription_ops =
71
+ rcl_subscription_get_default_options();
72
+
73
+ Napi::Value qos = options.Get("qos");
74
+ auto qos_profile = GetQoSProfile(qos);
75
+ if (qos_profile) {
76
+ subscription_ops.qos = *qos_profile;
77
+ }
78
+
79
+ #if ROS_VERSION >= 2205 // 2205 => Humble+
80
+ if (options.Has("contentFilter")) {
81
+ // configure content-filter
82
+ Napi::Value contentFilterVal = options.Get("contentFilter");
83
+
84
+ if (!contentFilterVal.IsUndefined()) {
85
+ Napi::Object contentFilter = contentFilterVal.As<Napi::Object>();
86
+
87
+ // expression property is required
88
+ std::string expression =
89
+ contentFilter.Get("expression").As<Napi::String>().Utf8Value();
90
+
91
+ // parameters property (string[]) is optional
92
+ int argc = 0;
93
+ char** argv = nullptr;
94
+
95
+ if (contentFilter.Has("parameters")) {
96
+ Napi::Array jsArgv = contentFilter.Get("parameters").As<Napi::Array>();
97
+ argc = jsArgv.Length();
98
+ if (argc > 0) {
99
+ argv = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
100
+ for (int i = 0; i < argc; i++) {
101
+ std::string arg = jsArgv.Get(i).As<Napi::String>().Utf8Value();
102
+ int len = arg.length() + 1;
103
+ argv[i] = reinterpret_cast<char*>(malloc(len * sizeof(char*)));
104
+ snprintf(argv[i], len, "%s", arg.c_str());
105
+ }
106
+ }
107
+ }
108
+
109
+ rcl_ret_t ret = rcl_subscription_options_set_content_filter_options(
110
+ expression.c_str(), argc, (const char**)argv, &subscription_ops);
111
+
112
+ if (ret != RCL_RET_OK) {
113
+ rcl_reset_error();
114
+ Napi::Error::New(env, rcl_get_error_string().str)
115
+ .ThrowAsJavaScriptException();
116
+ }
117
+
118
+ if (argc) {
119
+ for (int i = 0; i < argc; i++) {
120
+ free(argv[i]);
121
+ }
122
+ free(argv);
123
+ }
124
+ }
125
+ }
126
+ #endif
127
+
128
+ const rosidl_message_type_support_t* ts =
129
+ GetMessageTypeSupport(package_name, message_sub_folder, message_name);
130
+
131
+ if (ts) {
132
+ THROW_ERROR_IF_NOT_EQUAL(
133
+ RCL_RET_OK,
134
+ rcl_subscription_init(subscription, node, ts, topic.c_str(),
135
+ &subscription_ops),
136
+ rcl_get_error_string().str);
137
+
138
+ auto js_obj = RclHandle::NewInstance(
139
+ env, subscription, node_handle, [node](void* ptr) {
140
+ rcl_subscription_t* subscription =
141
+ reinterpret_cast<rcl_subscription_t*>(ptr);
142
+ rcl_ret_t ret = rcl_subscription_fini(subscription, node);
143
+ free(ptr);
144
+ THROW_ERROR_IF_NOT_EQUAL(RCL_RET_OK, ret, rcl_get_error_string().str);
145
+ });
146
+
147
+ return js_obj;
148
+ } else {
149
+ Napi::Error::New(env, GetErrorMessageAndClear())
150
+ .ThrowAsJavaScriptException();
151
+ return env.Undefined();
152
+ }
153
+ }
154
+
155
+ Napi::Value RclTakeRaw(const Napi::CallbackInfo& info) {
156
+ Napi::Env env = info.Env();
157
+
158
+ RclHandle* subscription_handle =
159
+ RclHandle::Unwrap(info[0].As<Napi::Object>());
160
+ rcl_subscription_t* subscription =
161
+ reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr());
162
+
163
+ rcl_serialized_message_t msg = rmw_get_zero_initialized_serialized_message();
164
+ rcutils_allocator_t allocator = rcutils_get_default_allocator();
165
+ rcl_ret_t ret = rmw_serialized_message_init(&msg, 0u, &allocator);
166
+ if (ret != RCL_RET_OK) {
167
+ THROW_ERROR_IF_NOT_EQUAL(rmw_serialized_message_fini(&msg), RCL_RET_OK,
168
+ "Failed to deallocate message buffer.");
169
+ return env.Undefined();
170
+ }
171
+ ret = rcl_take_serialized_message(subscription, &msg, nullptr, nullptr);
172
+ if (ret != RCL_RET_OK && ret != RCL_RET_SUBSCRIPTION_TAKE_FAILED) {
173
+ rcl_reset_error();
174
+ THROW_ERROR_IF_NOT_EQUAL(rmw_serialized_message_fini(&msg), RCL_RET_OK,
175
+ "Failed to deallocate message buffer.");
176
+ return env.Undefined();
177
+ }
178
+
179
+ if (ret == RCL_RET_SUBSCRIPTION_TAKE_FAILED) {
180
+ THROW_ERROR_IF_NOT_EQUAL(rmw_serialized_message_fini(&msg), RCL_RET_OK,
181
+ "Failed to deallocate message buffer.");
182
+ return env.Undefined();
183
+ }
184
+
185
+ Napi::Buffer<char> buffer = Napi::Buffer<char>::Copy(
186
+ env, reinterpret_cast<char*>(msg.buffer), msg.buffer_length);
187
+ THROW_ERROR_IF_NOT_EQUAL(rmw_serialized_message_fini(&msg), RCL_RET_OK,
188
+ "Failed to deallocate message buffer");
189
+ return buffer;
190
+ }
191
+
192
+ Napi::Value GetSubscriptionTopic(const Napi::CallbackInfo& info) {
193
+ Napi::Env env = info.Env();
194
+
195
+ rcl_subscription_t* subscription = reinterpret_cast<rcl_subscription_t*>(
196
+ RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
197
+
198
+ const char* topic = rcl_subscription_get_topic_name(subscription);
199
+ return Napi::String::New(env, topic);
200
+ }
201
+
202
+ Napi::Value HasContentFilter(const Napi::CallbackInfo& info) {
203
+ #if ROS_VERSION < 2205 // 2205 => Humble+
204
+ return Napi::Boolean::New(info.Env(), false);
205
+ #else
206
+ Napi::Env env = info.Env();
207
+
208
+ RclHandle* subscription_handle =
209
+ RclHandle::Unwrap(info[0].As<Napi::Object>());
210
+ rcl_subscription_t* subscription =
211
+ reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr());
212
+
213
+ bool is_valid = rcl_subscription_is_cft_enabled(subscription);
214
+ return Napi::Boolean::New(env, is_valid);
215
+ #endif
216
+ }
217
+
218
+ Napi::Value SetContentFilter(const Napi::CallbackInfo& info) {
219
+ #if ROS_VERSION < 2205 // 2205 => Humble+
220
+ return Napi::Boolean::New(info.Env(), false);
221
+ #else
222
+ Napi::Env env = info.Env();
223
+
224
+ RclHandle* subscription_handle =
225
+ RclHandle::Unwrap(info[0].As<Napi::Object>());
226
+ rcl_subscription_t* subscription =
227
+ reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr());
228
+
229
+ Napi::Object contentFilter = info[1].As<Napi::Object>();
230
+
231
+ std::string expression =
232
+ contentFilter.Get("expression").As<Napi::String>().Utf8Value();
233
+
234
+ // parameters property (string[]) is optional
235
+ int argc = 0;
236
+ char** argv = nullptr;
237
+
238
+ if (contentFilter.Has("parameters")) {
239
+ Napi::Array jsArgv = contentFilter.Get("parameters").As<Napi::Array>();
240
+ argc = jsArgv.Length();
241
+ if (argc > 0) {
242
+ argv = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
243
+ for (int i = 0; i < argc; i++) {
244
+ std::string arg = jsArgv.Get(i).As<Napi::String>().Utf8Value();
245
+ int len = arg.length() + 1;
246
+ argv[i] = reinterpret_cast<char*>(malloc(len * sizeof(char*)));
247
+ snprintf(argv[i], len, "%s", arg.c_str());
248
+ }
249
+ }
250
+ }
251
+
252
+ // create ctf options
253
+ rcl_subscription_content_filter_options_t options =
254
+ rcl_get_zero_initialized_subscription_content_filter_options();
255
+
256
+ THROW_ERROR_IF_NOT_EQUAL(
257
+ RCL_RET_OK,
258
+ rcl_subscription_content_filter_options_set(
259
+ subscription, expression.c_str(), argc, (const char**)argv, &options),
260
+ rcl_get_error_string().str);
261
+
262
+ THROW_ERROR_IF_NOT_EQUAL(
263
+ RCL_RET_OK, rcl_subscription_set_content_filter(subscription, &options),
264
+ rcl_get_error_string().str);
265
+
266
+ if (argc) {
267
+ for (int i = 0; i < argc; i++) {
268
+ free(argv[i]);
269
+ }
270
+ free(argv);
271
+ }
272
+
273
+ return Napi::Boolean::New(env, true);
274
+ #endif
275
+ }
276
+
277
+ Napi::Value ClearContentFilter(const Napi::CallbackInfo& info) {
278
+ #if ROS_VERSION < 2205 // 2205 => Humble+
279
+ return Napi::Boolean::New(info.Env(), false);
280
+ #else
281
+ Napi::Env env = info.Env();
282
+
283
+ RclHandle* subscription_handle =
284
+ RclHandle::Unwrap(info[0].As<Napi::Object>());
285
+ rcl_subscription_t* subscription =
286
+ reinterpret_cast<rcl_subscription_t*>(subscription_handle->ptr());
287
+
288
+ // create ctf options
289
+ rcl_subscription_content_filter_options_t options =
290
+ rcl_get_zero_initialized_subscription_content_filter_options();
291
+
292
+ THROW_ERROR_IF_NOT_EQUAL(
293
+ RCL_RET_OK,
294
+ rcl_subscription_content_filter_options_init(
295
+ subscription, "", 0, (const char**)nullptr, &options),
296
+ rcl_get_error_string().str);
297
+
298
+ THROW_ERROR_IF_NOT_EQUAL(
299
+ RCL_RET_OK, rcl_subscription_set_content_filter(subscription, &options),
300
+ rcl_get_error_string().str);
301
+
302
+ return Napi::Boolean::New(env, true);
303
+ #endif
304
+ }
305
+
306
+ Napi::Value GetPublisherCount(const Napi::CallbackInfo& info) {
307
+ Napi::Env env = info.Env();
308
+
309
+ rcl_subscription_t* subscription = reinterpret_cast<rcl_subscription_t*>(
310
+ RclHandle::Unwrap(info[0].As<Napi::Object>())->ptr());
311
+
312
+ size_t count = 0;
313
+ THROW_ERROR_IF_NOT_EQUAL(
314
+ rcl_subscription_get_publisher_count(subscription, &count), RCL_RET_OK,
315
+ rcl_get_error_string().str);
316
+
317
+ return Napi::Number::New(env, count);
318
+ }
319
+
320
+ Napi::Object InitSubscriptionBindings(Napi::Env env, Napi::Object exports) {
321
+ exports.Set("rclTake", Napi::Function::New(env, RclTake));
322
+ exports.Set("createSubscription",
323
+ Napi::Function::New(env, CreateSubscription));
324
+ exports.Set("rclTakeRaw", Napi::Function::New(env, RclTakeRaw));
325
+ exports.Set("getSubscriptionTopic",
326
+ Napi::Function::New(env, GetSubscriptionTopic));
327
+ exports.Set("hasContentFilter", Napi::Function::New(env, HasContentFilter));
328
+ exports.Set("setContentFilter", Napi::Function::New(env, SetContentFilter));
329
+ exports.Set("clearContentFilter",
330
+ Napi::Function::New(env, ClearContentFilter));
331
+ exports.Set("getPublisherCount", Napi::Function::New(env, GetPublisherCount));
332
+ return exports;
333
+ }
334
+
335
+ } // namespace rclnodejs
@@ -0,0 +1,26 @@
1
+ // Copyright (c) 2025, The Robot Web Tools Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifndef SRC_RCL_SUBSCRIPTION_BINDINGS_H_
16
+ #define SRC_RCL_SUBSCRIPTION_BINDINGS_H_
17
+
18
+ #include <napi.h>
19
+
20
+ namespace rclnodejs {
21
+
22
+ Napi::Object InitSubscriptionBindings(Napi::Env env, Napi::Object exports);
23
+
24
+ }
25
+
26
+ #endif // SRC_RCL_SUBSCRIPTION_BINDINGS_H_