rclnodejs 1.5.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.
Files changed (59) hide show
  1. package/index.js +79 -3
  2. package/lib/action/client.js +55 -9
  3. package/lib/action/deferred.js +8 -2
  4. package/lib/action/server.js +10 -1
  5. package/lib/action/uuid.js +4 -1
  6. package/lib/client.js +152 -3
  7. package/lib/clock.js +4 -1
  8. package/lib/context.js +12 -2
  9. package/lib/duration.js +37 -12
  10. package/lib/errors.js +571 -0
  11. package/lib/event_handler.js +21 -4
  12. package/lib/interface_loader.js +52 -12
  13. package/lib/lifecycle.js +8 -2
  14. package/lib/logging.js +12 -3
  15. package/lib/message_serialization.js +179 -0
  16. package/lib/native_loader.js +9 -4
  17. package/lib/node.js +283 -47
  18. package/lib/parameter.js +176 -45
  19. package/lib/parameter_client.js +506 -0
  20. package/lib/parameter_watcher.js +309 -0
  21. package/lib/qos.js +22 -5
  22. package/lib/rate.js +6 -1
  23. package/lib/serialization.js +7 -2
  24. package/lib/subscription.js +16 -1
  25. package/lib/time.js +136 -21
  26. package/lib/time_source.js +13 -4
  27. package/lib/utils.js +313 -0
  28. package/lib/validator.js +11 -12
  29. package/package.json +2 -7
  30. package/prebuilds/linux-arm64/humble-jammy-arm64-rclnodejs.node +0 -0
  31. package/prebuilds/linux-arm64/jazzy-noble-arm64-rclnodejs.node +0 -0
  32. package/prebuilds/linux-arm64/kilted-noble-arm64-rclnodejs.node +0 -0
  33. package/prebuilds/linux-x64/humble-jammy-x64-rclnodejs.node +0 -0
  34. package/prebuilds/linux-x64/jazzy-noble-x64-rclnodejs.node +0 -0
  35. package/prebuilds/linux-x64/kilted-noble-x64-rclnodejs.node +0 -0
  36. package/rosidl_convertor/idl_convertor.js +3 -2
  37. package/rosidl_gen/generate_worker.js +1 -1
  38. package/rosidl_gen/idl_generator.js +11 -24
  39. package/rosidl_gen/index.js +1 -1
  40. package/rosidl_gen/templates/action-template.js +68 -0
  41. package/rosidl_gen/templates/message-template.js +1113 -0
  42. package/rosidl_gen/templates/service-event-template.js +31 -0
  43. package/rosidl_gen/templates/service-template.js +44 -0
  44. package/rosidl_parser/rosidl_parser.js +2 -2
  45. package/third_party/ref-napi/lib/ref.js +0 -45
  46. package/types/base.d.ts +3 -0
  47. package/types/client.d.ts +36 -0
  48. package/types/errors.d.ts +447 -0
  49. package/types/index.d.ts +17 -0
  50. package/types/interfaces.d.ts +1910 -1
  51. package/types/node.d.ts +56 -1
  52. package/types/parameter_client.d.ts +252 -0
  53. package/types/parameter_watcher.d.ts +104 -0
  54. package/rosidl_gen/templates/CMakeLists.dot +0 -40
  55. package/rosidl_gen/templates/action.dot +0 -50
  56. package/rosidl_gen/templates/message.dot +0 -851
  57. package/rosidl_gen/templates/package.dot +0 -16
  58. package/rosidl_gen/templates/service.dot +0 -26
  59. package/rosidl_gen/templates/service_event.dot +0 -10
@@ -0,0 +1,309 @@
1
+ // Copyright (c) 2025 Mahmoud Alghalayini. All rights reserved.
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
+ 'use strict';
16
+
17
+ const EventEmitter = require('events');
18
+ const { TypeValidationError, OperationError } = require('./errors');
19
+ const { normalizeNodeName } = require('./utils');
20
+ const debug = require('debug')('rclnodejs:parameter_watcher');
21
+
22
+ /**
23
+ * @class ParameterWatcher - Watches parameter changes on a remote node
24
+ *
25
+ * Subscribes to /parameter_events and emits 'change' events when
26
+ * watched parameters on the target node are modified.
27
+ *
28
+ * @extends EventEmitter
29
+ */
30
+ class ParameterWatcher extends EventEmitter {
31
+ #node;
32
+ #paramClient;
33
+ #subscription;
34
+ #watchedParams;
35
+ #remoteNodeName;
36
+ #destroyed;
37
+
38
+ /**
39
+ * Create a ParameterWatcher instance.
40
+ * Note: Use node.createParameterWatcher() instead of calling this directly.
41
+ *
42
+ * @param {object} node - The local rclnodejs Node instance
43
+ * @param {string} remoteNodeName - Name of the remote node to watch
44
+ * @param {string[]} parameterNames - Array of parameter names to watch
45
+ * @param {object} [options] - Options for the parameter client
46
+ * @param {number} [options.timeout=5000] - Default timeout for parameter operations
47
+ * @hideconstructor
48
+ */
49
+ constructor(node, remoteNodeName, parameterNames, options = {}) {
50
+ super();
51
+
52
+ if (!node || typeof node.createParameterClient !== 'function') {
53
+ throw new TypeValidationError('node', node, 'Node instance', {
54
+ entityType: 'parameter watcher',
55
+ });
56
+ }
57
+
58
+ if (typeof remoteNodeName !== 'string' || remoteNodeName.trim() === '') {
59
+ throw new TypeValidationError(
60
+ 'remoteNodeName',
61
+ remoteNodeName,
62
+ 'non-empty string',
63
+ {
64
+ entityType: 'parameter watcher',
65
+ }
66
+ );
67
+ }
68
+
69
+ if (!Array.isArray(parameterNames) || parameterNames.length === 0) {
70
+ throw new TypeValidationError(
71
+ 'parameterNames',
72
+ parameterNames,
73
+ 'non-empty array',
74
+ {
75
+ entityType: 'parameter watcher',
76
+ }
77
+ );
78
+ }
79
+
80
+ this.#node = node;
81
+ this.#watchedParams = new Set(parameterNames);
82
+ this.#paramClient = node.createParameterClient(remoteNodeName, options);
83
+ // Cache the remote node name for error messages (in case paramClient is destroyed)
84
+ this.#remoteNodeName = this.#paramClient.remoteNodeName;
85
+ this.#subscription = null;
86
+ this.#destroyed = false;
87
+
88
+ debug(
89
+ 'Created ParameterWatcher for node=%s, params=%o',
90
+ remoteNodeName,
91
+ parameterNames
92
+ );
93
+ }
94
+
95
+ /**
96
+ * Get the remote node name being watched.
97
+ * @type {string}
98
+ * @readonly
99
+ */
100
+ get remoteNodeName() {
101
+ return this.#remoteNodeName;
102
+ }
103
+
104
+ /**
105
+ * Get the list of watched parameter names.
106
+ * @type {string[]}
107
+ * @readonly
108
+ */
109
+ get watchedParameters() {
110
+ return Array.from(this.#watchedParams);
111
+ }
112
+
113
+ /**
114
+ * Start watching for parameter changes.
115
+ * Waits for the remote node's parameter services and subscribes to parameter events.
116
+ *
117
+ * @param {number} [timeout=5000] - Timeout in milliseconds to wait for services
118
+ * @returns {Promise<boolean>} Resolves to true when watching has started
119
+ * @throws {Error} If the watcher has been destroyed
120
+ */
121
+ async start(timeout = 5000) {
122
+ this.#checkNotDestroyed();
123
+
124
+ debug('Starting ParameterWatcher for node=%s', this.remoteNodeName);
125
+
126
+ const available = await this.#paramClient.waitForService(timeout);
127
+
128
+ if (!available) {
129
+ debug(
130
+ 'Parameter services not available for node=%s',
131
+ this.remoteNodeName
132
+ );
133
+ return false;
134
+ }
135
+
136
+ if (!this.#subscription) {
137
+ this.#subscription = this.#node.createSubscription(
138
+ 'rcl_interfaces/msg/ParameterEvent',
139
+ '/parameter_events',
140
+ (event) => this.#handleParameterEvent(event)
141
+ );
142
+
143
+ debug('Subscribed to /parameter_events');
144
+ }
145
+
146
+ return true;
147
+ }
148
+
149
+ /**
150
+ * Get current values of all watched parameters.
151
+ *
152
+ * @param {object} [options] - Options for the parameter client
153
+ * @param {number} [options.timeout] - Timeout in milliseconds
154
+ * @param {AbortSignal} [options.signal] - AbortSignal for cancellation
155
+ * @returns {Promise<Parameter[]>} Array of Parameter objects
156
+ * @throws {Error} If the watcher has been destroyed
157
+ */
158
+ async getCurrentValues(options) {
159
+ this.#checkNotDestroyed();
160
+ return await this.#paramClient.getParameters(
161
+ Array.from(this.#watchedParams),
162
+ options
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Add a parameter name to the watch list.
168
+ *
169
+ * @param {string} name - Parameter name to watch
170
+ * @throws {TypeError} If name is not a string
171
+ * @throws {Error} If the watcher has been destroyed
172
+ */
173
+ addParameter(name) {
174
+ this.#checkNotDestroyed();
175
+
176
+ if (typeof name !== 'string' || name.trim() === '') {
177
+ throw new TypeValidationError('name', name, 'non-empty string', {
178
+ entityType: 'parameter watcher',
179
+ entityName: this.remoteNodeName,
180
+ });
181
+ }
182
+
183
+ const wasAdded = !this.#watchedParams.has(name);
184
+ this.#watchedParams.add(name);
185
+
186
+ if (wasAdded) {
187
+ debug('Added parameter to watch list: %s', name);
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Remove a parameter name from the watch list.
193
+ *
194
+ * @param {string} name - Parameter name to stop watching
195
+ * @returns {boolean} True if the parameter was in the watch list
196
+ * @throws {Error} If the watcher has been destroyed
197
+ */
198
+ removeParameter(name) {
199
+ this.#checkNotDestroyed();
200
+
201
+ const wasRemoved = this.#watchedParams.delete(name);
202
+
203
+ if (wasRemoved) {
204
+ debug('Removed parameter from watch list: %s', name);
205
+ }
206
+
207
+ return wasRemoved;
208
+ }
209
+
210
+ /**
211
+ * Check if the watcher has been destroyed.
212
+ *
213
+ * @returns {boolean} True if destroyed
214
+ */
215
+ isDestroyed() {
216
+ return this.#destroyed;
217
+ }
218
+
219
+ /**
220
+ * Destroy the watcher and clean up resources.
221
+ * Unsubscribes from parameter events and destroys the parameter client.
222
+ */
223
+ destroy() {
224
+ if (this.#destroyed) {
225
+ return;
226
+ }
227
+
228
+ debug('Destroying ParameterWatcher for node=%s', this.remoteNodeName);
229
+
230
+ if (this.#subscription) {
231
+ try {
232
+ this.#node.destroySubscription(this.#subscription);
233
+ } catch (error) {
234
+ debug('Error destroying subscription: %s', error.message);
235
+ }
236
+ this.#subscription = null;
237
+ }
238
+
239
+ if (this.#paramClient) {
240
+ try {
241
+ this.#node.destroyParameterClient(this.#paramClient);
242
+ } catch (error) {
243
+ debug('Error destroying parameter client: %s', error.message);
244
+ }
245
+ this.#paramClient = null;
246
+ }
247
+
248
+ this.removeAllListeners();
249
+
250
+ this.#destroyed = true;
251
+ }
252
+
253
+ /**
254
+ * Handle parameter event from /parameter_events topic.
255
+ * @private
256
+ */
257
+ #handleParameterEvent(event) {
258
+ if (normalizeNodeName(event.node) !== this.remoteNodeName) {
259
+ return;
260
+ }
261
+
262
+ const relevantChanges = [];
263
+
264
+ if (event.new_parameters) {
265
+ const newParams = event.new_parameters.filter((p) =>
266
+ this.#watchedParams.has(p.name)
267
+ );
268
+ relevantChanges.push(...newParams);
269
+ }
270
+
271
+ if (event.changed_parameters) {
272
+ const changedParams = event.changed_parameters.filter((p) =>
273
+ this.#watchedParams.has(p.name)
274
+ );
275
+ relevantChanges.push(...changedParams);
276
+ }
277
+
278
+ if (event.deleted_parameters) {
279
+ const deletedParams = event.deleted_parameters.filter((p) =>
280
+ this.#watchedParams.has(p.name)
281
+ );
282
+ relevantChanges.push(...deletedParams);
283
+ }
284
+
285
+ if (relevantChanges.length > 0) {
286
+ debug(
287
+ 'Parameter change detected: %o',
288
+ relevantChanges.map((p) => p.name)
289
+ );
290
+ this.emit('change', relevantChanges);
291
+ }
292
+ }
293
+
294
+ /**
295
+ * Check if the watcher has been destroyed and throw if so.
296
+ * @private
297
+ */
298
+ #checkNotDestroyed() {
299
+ if (this.#destroyed) {
300
+ throw new OperationError('ParameterWatcher has been destroyed', {
301
+ code: 'WATCHER_DESTROYED',
302
+ entityType: 'parameter watcher',
303
+ entityName: this.remoteNodeName,
304
+ });
305
+ }
306
+ }
307
+ }
308
+
309
+ module.exports = ParameterWatcher;
package/lib/qos.js CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  'use strict';
16
16
 
17
+ const { TypeValidationError } = require('./errors.js');
18
+
17
19
  /**
18
20
  * Enum for HistoryPolicy
19
21
  * @readonly
@@ -129,7 +131,9 @@ class QoS {
129
131
  */
130
132
  set history(history) {
131
133
  if (typeof history !== 'number') {
132
- throw new TypeError('Invalid argument');
134
+ throw new TypeValidationError('history', history, 'number', {
135
+ entityType: 'qos',
136
+ });
133
137
  }
134
138
 
135
139
  this._history = history;
@@ -154,7 +158,9 @@ class QoS {
154
158
  */
155
159
  set depth(depth) {
156
160
  if (typeof depth !== 'number') {
157
- throw new TypeError('Invalid argument');
161
+ throw new TypeValidationError('depth', depth, 'number', {
162
+ entityType: 'qos',
163
+ });
158
164
  }
159
165
 
160
166
  this._depth = depth;
@@ -179,7 +185,9 @@ class QoS {
179
185
  */
180
186
  set reliability(reliability) {
181
187
  if (typeof reliability !== 'number') {
182
- throw new TypeError('Invalid argument');
188
+ throw new TypeValidationError('reliability', reliability, 'number', {
189
+ entityType: 'qos',
190
+ });
183
191
  }
184
192
 
185
193
  this._reliability = reliability;
@@ -204,7 +212,9 @@ class QoS {
204
212
  */
205
213
  set durability(durability) {
206
214
  if (typeof durability !== 'number') {
207
- throw new TypeError('Invalid argument');
215
+ throw new TypeValidationError('durability', durability, 'number', {
216
+ entityType: 'qos',
217
+ });
208
218
  }
209
219
 
210
220
  this._durability = durability;
@@ -229,7 +239,14 @@ class QoS {
229
239
  */
230
240
  set avoidRosNameSpaceConventions(avoidRosNameSpaceConventions) {
231
241
  if (typeof avoidRosNameSpaceConventions !== 'boolean') {
232
- throw new TypeError('Invalid argument');
242
+ throw new TypeValidationError(
243
+ 'avoidRosNameSpaceConventions',
244
+ avoidRosNameSpaceConventions,
245
+ 'boolean',
246
+ {
247
+ entityType: 'qos',
248
+ }
249
+ );
233
250
  }
234
251
 
235
252
  this._avoidRosNameSpaceConventions = avoidRosNameSpaceConventions;
package/lib/rate.js CHANGED
@@ -15,6 +15,7 @@
15
15
  const rclnodejs = require('../index.js');
16
16
  const Context = require('./context.js');
17
17
  const NodeOptions = require('./node_options.js');
18
+ const { OperationError } = require('./errors.js');
18
19
 
19
20
  const NOP_FN = () => {};
20
21
 
@@ -86,7 +87,11 @@ class Rate {
86
87
  */
87
88
  async sleep() {
88
89
  if (this.isCanceled()) {
89
- throw new Error('Rate has been cancelled.');
90
+ throw new OperationError('Rate has been cancelled', {
91
+ code: 'RATE_CANCELLED',
92
+ entityType: 'rate',
93
+ details: { frequency: this._hz },
94
+ });
90
95
  }
91
96
 
92
97
  return new Promise((resolve) => {
@@ -15,6 +15,7 @@
15
15
  'use strict';
16
16
 
17
17
  const rclnodejs = require('./native_loader.js');
18
+ const { TypeValidationError } = require('./errors.js');
18
19
 
19
20
  class Serialization {
20
21
  /**
@@ -25,7 +26,9 @@ class Serialization {
25
26
  */
26
27
  static serializeMessage(message, typeClass) {
27
28
  if (!(message instanceof typeClass)) {
28
- throw new TypeError('Message must be a valid ros2 message type');
29
+ throw new TypeValidationError('message', message, typeClass.name, {
30
+ entityType: 'serializer',
31
+ });
29
32
  }
30
33
  return rclnodejs.serialize(
31
34
  typeClass.type().pkgName,
@@ -43,7 +46,9 @@ class Serialization {
43
46
  */
44
47
  static deserializeMessage(buffer, typeClass) {
45
48
  if (!(buffer instanceof Buffer)) {
46
- throw new TypeError('Buffer is required for deserialization');
49
+ throw new TypeValidationError('buffer', buffer, 'Buffer', {
50
+ entityType: 'serializer',
51
+ });
47
52
  }
48
53
  const rosMsg = new typeClass();
49
54
  rclnodejs.deserialize(
@@ -16,6 +16,7 @@
16
16
 
17
17
  const rclnodejs = require('./native_loader.js');
18
18
  const Entity = require('./entity.js');
19
+ const { applySerializationMode } = require('./message_serialization.js');
19
20
  const debug = require('debug')('rclnodejs:subscription');
20
21
 
21
22
  /**
@@ -42,6 +43,7 @@ class Subscription extends Entity {
42
43
  this._topic = topic;
43
44
  this._callback = callback;
44
45
  this._isRaw = options.isRaw || false;
46
+ this._serializationMode = options.serializationMode || 'default';
45
47
  this._node = node;
46
48
 
47
49
  if (node && eventCallbacks) {
@@ -55,7 +57,13 @@ class Subscription extends Entity {
55
57
  if (this._isRaw) {
56
58
  this._callback(msg);
57
59
  } else {
58
- this._callback(msg.toPlainObject(this.typedArrayEnabled));
60
+ let message = msg.toPlainObject(this.typedArrayEnabled);
61
+
62
+ if (this._serializationMode !== 'default') {
63
+ message = applySerializationMode(message, this._serializationMode);
64
+ }
65
+
66
+ this._callback(message);
59
67
  }
60
68
  }
61
69
 
@@ -109,6 +117,13 @@ class Subscription extends Entity {
109
117
  return this._isRaw;
110
118
  }
111
119
 
120
+ /**
121
+ * @type {string}
122
+ */
123
+ get serializationMode() {
124
+ return this._serializationMode;
125
+ }
126
+
112
127
  /**
113
128
  * Test if the RMW supports content-filtered topics and that this subscription
114
129
  * has an active wellformed content-filter.