Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5aecbcf

Browse filesBrowse files
cjihrigtargos
authored andcommitted
util: add internal sleep() function
PR-URL: #30787 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
1 parent fff1167 commit 5aecbcf
Copy full SHA for 5aecbcf

File tree

Expand file treeCollapse file tree

3 files changed

+40
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+40
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+14-1Lines changed: 14 additions & 1 deletion
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

Collapse file

‎src/node_util.cc‎

Copy file name to clipboardExpand all lines: src/node_util.cc
+7Lines changed: 7 additions & 0 deletions
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());
Collapse file

‎test/parallel/test-util-sleep.js‎

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.