GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: add internal sleep() function · nodejs/node@5aecbcf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • util.js
  • src
    • node_util.cc
  • test/parallel
    • test-util-sleep.js
Expand file treeCollapse file tree
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)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.