Skip to content

Commit dc69cbe

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 d4aa656 commit dc69cbe

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
@@ -26,7 +26,8 @@ const {
2626
getHiddenValue,
2727
setHiddenValue,
2828
arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex,
29-
decorated_private_symbol: kDecoratedPrivateSymbolIndex
29+
decorated_private_symbol: kDecoratedPrivateSymbolIndex,
30+
sleep: _sleep
3031
} = internalBinding('util');
3132
const { isNativeError } = internalBinding('types');
3233

@@ -385,6 +386,17 @@ function once(callback) {
385386
};
386387
}
387388

389+
let validateUint32;
390+
391+
function sleep(msec) {
392+
// Lazy-load to avoid a circular dependency.
393+
if (validateUint32 === undefined)
394+
({ validateUint32 } = require('internal/validators'));
395+
396+
validateUint32(msec, 'msec');
397+
_sleep(msec);
398+
}
399+
388400
module.exports = {
389401
assertCrypto,
390402
cachedResult,
@@ -402,6 +414,7 @@ module.exports = {
402414
normalizeEncoding,
403415
once,
404416
promisify,
417+
sleep,
405418
spliceOne,
406419
removeColors,
407420

src/node_util.cc

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

172+
static void Sleep(const FunctionCallbackInfo<Value>& args) {
173+
CHECK(args[0]->IsUint32());
174+
uint32_t msec = args[0].As<Uint32>()->Value();
175+
uv_sleep(msec);
176+
}
177+
172178
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
173179
CHECK(args[0]->IsArrayBufferView());
174180
args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
@@ -290,6 +296,7 @@ void Initialize(Local<Object> target,
290296
env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties",
291297
GetOwnNonIndexProperties);
292298
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
299+
env->SetMethod(target, "sleep", Sleep);
293300

294301
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
295302
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)