| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,8 +31,13 @@ const { | |||
| 31 | 31 | kEmptyObject, | |
| 32 | 32 | } = require('internal/util'); | |
| 33 | 33 | const { isPromise } = require('internal/util/types'); | |
| 34 | - const { isUint32, validateAbortSignal } = require('internal/validators'); | ||
| 34 | + const { | ||
| 35 | + isUint32, | ||
| 36 | + validateAbortSignal, | ||
| 37 | + validateNumber, | ||
| 38 | + } = require('internal/validators'); | ||
| 35 | 39 | const { setTimeout } = require('timers/promises'); | |
| 40 | + const { TIMEOUT_MAX } = require('internal/timers'); | ||
| 36 | 41 | const { cpus } = require('os'); | |
| 37 | 42 | const { bigint: hrtime } = process.hrtime; | |
| 38 | 43 | const kCallbackAndPromisePresent = 'callbackAndPromisePresent'; | |
@@ -148,7 +153,8 @@ class Test extends AsyncResource { | |||
| 148 | 153 | this.concurrency = concurrency; | |
| 149 | 154 | } | |
| 150 | 155 | ||
| 151 | - if (isUint32(timeout)) { | ||
| 156 | + if (timeout != null && timeout !== Infinity) { | ||
| 157 | + validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX); | ||
| 152 | 158 | this.timeout = timeout; | |
| 153 | 159 | } | |
| 154 | 160 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,7 @@ const { | |||
| 6 | 6 | ArrayPrototypeJoin, | |
| 7 | 7 | ArrayPrototypeMap, | |
| 8 | 8 | NumberIsInteger, | |
| 9 | + NumberIsNaN, | ||
| 9 | 10 | NumberMAX_SAFE_INTEGER, | |
| 10 | 11 | NumberMIN_SAFE_INTEGER, | |
| 11 | 12 | NumberParseInt, | |
@@ -115,9 +116,17 @@ function validateString(value, name) { | |||
| 115 | 116 | throw new ERR_INVALID_ARG_TYPE(name, 'string', value); | |
| 116 | 117 | } | |
| 117 | 118 | ||
| 118 | - function validateNumber(value, name) { | ||
| 119 | + function validateNumber(value, name, min = undefined, max) { | ||
| 119 | 120 | if (typeof value !== 'number') | |
| 120 | 121 | throw new ERR_INVALID_ARG_TYPE(name, 'number', value); | |
| 122 | + | ||
| 123 | + if ((min != null && value < min) || (max != null && value > max) || | ||
| 124 | + ((min != null || max != null) && NumberIsNaN(value))) { | ||
| 125 | + throw new ERR_OUT_OF_RANGE( | ||
| 126 | + name, | ||
| 127 | + `${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`, | ||
| 128 | + value); | ||
| 129 | + } | ||
| 121 | 130 | } | |
| 122 | 131 | ||
| 123 | 132 | const validateOneOf = hideStackFrames((value, name, oneOf) => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const test = require('node:test'); | ||
| 5 | + | ||
| 6 | + [Symbol(), {}, [], () => {}, 1n, true, '1'].forEach((timeout) => { | ||
| 7 | + assert.throws(() => test({ timeout }), { code: 'ERR_INVALID_ARG_TYPE' }); | ||
| 8 | + }); | ||
| 9 | + [-1, -Infinity, NaN, 2 ** 33, Number.MAX_SAFE_INTEGER].forEach((timeout) => { | ||
| 10 | + assert.throws(() => test({ timeout }), { code: 'ERR_OUT_OF_RANGE' }); | ||
| 11 | + }); | ||
| 12 | + [null, undefined, Infinity, 0, 1, 1.1].forEach((timeout) => { | ||
| 13 | + // Valid values should not throw. | ||
| 14 | + test({ timeout }); | ||
| 15 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments