Skip to content

Commit 5aecbcf

Browse files
cjihrigtargos
authored andcommitted
util: add internal sleep() function
PR-URL: #30787 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent fff1167 commit 5aecbcf

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/internal/util.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const {
1515
getHiddenValue,
1616
setHiddenValue,
1717
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
18-
decorated_private_symbol: kDecoratedPrivateSymbolIndex
18+
decorated_private_symbol: kDecoratedPrivateSymbolIndex,
19+
sleep: _sleep
1920
} = internalBinding('util');
2021
const { isNativeError } = internalBinding('types');
2122

@@ -374,6 +375,17 @@ function once(callback) {
374375
};
375376
}
376377

378+
let validateUint32;
379+
380+
function sleep(msec) {
381+
// Lazy-load to avoid a circular dependency.
382+
if (validateUint32 === undefined)
383+
({ validateUint32 } = require('internal/validators'));
384+
385+
validateUint32(msec, 'msec');
386+
_sleep(msec);
387+
}
388+
377389
module.exports = {
378390
assertCrypto,
379391
cachedResult,
@@ -391,6 +403,7 @@ module.exports = {
391403
normalizeEncoding,
392404
once,
393405
promisify,
406+
sleep,
394407
spliceOne,
395408
removeColors,
396409

src/node_util.cc

+7
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
161161
args.GetReturnValue().Set(maybe_value.FromJust());
162162
}
163163

164+
static void Sleep(const FunctionCallbackInfo<Value>& args) {
165+
CHECK(args[0]->IsUint32());
166+
uint32_t msec = args[0].As<Uint32>()->Value();
167+
uv_sleep(msec);
168+
}
169+
164170
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
165171
CHECK(args[0]->IsArrayBufferView());
166172
args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
@@ -282,6 +288,7 @@ void Initialize(Local<Object> target,
282288
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
283289
GetOwnNonIndexProperties);
284290
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
291+
env->SetMethod(target, "sleep", Sleep);
285292

286293
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
287294
Local<Object> constants = Object::New(env->isolate());

test/parallel/test-util-sleep.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('assert');
5+
const { sleep } = require('internal/util');
6+
7+
[undefined, null, '', {}, true, false].forEach((value) => {
8+
assert.throws(
9+
() => { sleep(value); },
10+
/The "msec" argument must be of type number/
11+
);
12+
});
13+
14+
[-1, 3.14, NaN, 4294967296].forEach((value) => {
15+
assert.throws(
16+
() => { sleep(value); },
17+
/The value of "msec" is out of range/
18+
);
19+
});

0 commit comments

Comments
 (0)