Skip to content

Commit 5f4ee7f

Browse files
committed
lib: make AbortSignal.timeout() parameter handling spec-compliant
Signed-off-by: Renegade334 <[email protected]>
1 parent 0a9b67e commit 5f4ee7f

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/internal/abort_controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ const {
4444
} = require('internal/errors');
4545
const {
4646
converters,
47+
convertToInt,
4748
createInterfaceConverter,
4849
createSequenceConverter,
4950
} = require('internal/webidl');
5051

5152
const {
5253
validateObject,
53-
validateUint32,
5454
kValidateObjectAllowObjects,
5555
} = require('internal/validators');
5656

@@ -295,7 +295,7 @@ class AbortSignal extends EventTarget {
295295
* @returns {AbortSignal}
296296
*/
297297
static timeout(delay) {
298-
validateUint32(delay, 'delay', false);
298+
delay = convertToInt('delay', delay, 64, { __proto__: null, enforceRange: true });
299299
const signal = new AbortSignal(kDontThrowSymbol);
300300
signal[kTimeout] = true;
301301
clearTimeoutRegistry.register(

test/parallel/test-abortcontroller.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ test('AbortSignal with a timeout is not collected while there is an active liste
225225
assert.strictEqual(ref.deref(), undefined);
226226
});
227227

228+
test('AbortSignal.timeout() correctly validates delay argument', () => {
229+
for (const value of [0, -0, -0.9, Number.MAX_SAFE_INTEGER]) {
230+
AbortSignal.timeout(value);
231+
}
232+
233+
for (const value of [-1, Number.MAX_SAFE_INTEGER + 1, Number.NaN]) {
234+
assert.throws(() => AbortSignal.timeout(value), { code: 'ERR_INVALID_ARG_VALUE' });
235+
}
236+
});
237+
228238
test('Setting a long timeout should not keep the process open', () => {
229239
AbortSignal.timeout(1_200_000);
230240
});

0 commit comments

Comments
 (0)