Skip to content

Commit 1cf7f1f

Browse files
committed
Add polyfill for AbortSignal.any() for Node.js <= 18.20
1 parent 3ad842c commit 1cf7f1f

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.github/workflows/linux-arm64-build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
node-version: [24.X]
23+
node-version: [16.X]
2424
architecture: [arm64]
2525
ros_distribution:
2626
- humble

lib/client.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ const DistroUtils = require('./distro.js');
1919
const Entity = require('./entity.js');
2020
const debug = require('debug')('rclnodejs:client');
2121

22+
// Polyfill for AbortSignal.any() for Node.js <= 18.20
23+
// AbortSignal.any() was added in Node.js 20.3.0
24+
// See https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
25+
if (!AbortSignal.any) {
26+
AbortSignal.any = function (signals) {
27+
const controller = new AbortController();
28+
29+
for (const signal of signals) {
30+
if (signal.aborted) {
31+
controller.abort(signal.reason);
32+
break;
33+
}
34+
35+
signal.addEventListener(
36+
'abort',
37+
() => {
38+
controller.abort(signal.reason);
39+
},
40+
{ once: true }
41+
);
42+
}
43+
44+
return controller.signal;
45+
};
46+
}
47+
2248
/**
2349
* @class - Class representing a Client in ROS
2450
* @hideconstructor

test/test-async-client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ describe('Client async functionality', function () {
260260
});
261261

262262
it('should handle zero and negative timeouts', async function () {
263+
// Skip this test on Node.js < 18.20 where AbortSignal.timeout(0) throws RangeError
264+
const [major, minor] = process.versions.node.split('.').map(Number);
265+
if (major < 18 || (major === 18 && minor < 20)) {
266+
this.skip();
267+
}
268+
263269
const request = { a: BigInt(1), b: BigInt(1) };
264270

265271
try {

0 commit comments

Comments
 (0)