GeezerVison is showing github.com — util: add internal sleep() function · nodejs/node@5aecbcf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,8 @@ const { | 15 | 15 | getHiddenValue, | 16 | 16 | setHiddenValue, | 17 | 17 | arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex, | 18 | | - decorated_private_symbol: kDecoratedPrivateSymbolIndex | | 18 | + decorated_private_symbol: kDecoratedPrivateSymbolIndex, | | 19 | + sleep: _sleep | 19 | 20 | } = internalBinding('util'); | 20 | 21 | const { isNativeError } = internalBinding('types'); | 21 | 22 |
| @@ -374,6 +375,17 @@ function once(callback) { | 374 | 375 | }; | 375 | 376 | } | 376 | 377 |
| | 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 | + | 377 | 389 | module.exports = { | 378 | 390 | assertCrypto, | 379 | 391 | cachedResult, | @@ -391,6 +403,7 @@ module.exports = { | 391 | 403 | normalizeEncoding, | 392 | 404 | once, | 393 | 405 | promisify, | | 406 | + sleep, | 394 | 407 | spliceOne, | 395 | 408 | removeColors, | 396 | 409 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,6 +161,12 @@ static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) { | 161 | 161 | args.GetReturnValue().Set(maybe_value.FromJust()); | 162 | 162 | } | 163 | 163 |
| | 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 | + | 164 | 170 | void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) { | 165 | 171 | CHECK(args[0]->IsArrayBufferView()); | 166 | 172 | args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer()); | @@ -282,6 +288,7 @@ void Initialize(Local<Object> target, | 282 | 288 | env->SetMethodNoSideEffect(target, "getOwnNonIndexProperties", | 283 | 289 | GetOwnNonIndexProperties); | 284 | 290 | env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName); | | 291 | + env->SetMethod(target, "sleep", Sleep); | 285 | 292 |
| 286 | 293 | env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer); | 287 | 294 | Local<Object> constants = Object::New(env->isolate()); | |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +}); |
|