| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 76b8803 commit 0a03e35
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | const errors = require('internal/errors'); | |
| 4 | 4 | const { isArrayBufferView } = require('internal/util/types'); | |
| 5 | 5 | const { | |
| 6 | - randomBytes, | ||
| 6 | + randomBytes: _randomBytes, | ||
| 7 | 7 | randomFill: _randomFill | |
| 8 | 8 | } = process.binding('crypto'); | |
| 9 | 9 | ||
@@ -24,7 +24,7 @@ function assertOffset(offset, length) { | |||
| 24 | 24 | } | |
| 25 | 25 | } | |
| 26 | 26 | ||
| 27 | - function assertSize(size, offset, length) { | ||
| 27 | + function assertSize(size, offset = 0, length = Infinity) { | ||
| 28 | 28 | if (typeof size !== 'number' || size !== size) { | |
| 29 | 29 | throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number'); | |
| 30 | 30 | } | |
@@ -38,6 +38,13 @@ function assertSize(size, offset, length) { | |||
| 38 | 38 | } | |
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | + function randomBytes(size, cb) { | ||
| 42 | + assertSize(size); | ||
| 43 | + if (cb !== undefined && typeof cb !== 'function') | ||
| 44 | + throw new errors.TypeError('ERR_INVALID_CALLBACK'); | ||
| 45 | + return _randomBytes(size, cb); | ||
| 46 | + } | ||
| 47 | + | ||
| 41 | 48 | function randomFillSync(buf, offset = 0, size) { | |
| 42 | 49 | if (!isArrayBufferView(buf)) { | |
| 43 | 50 | throw new errors.TypeError('ERR_INVALID_ARG_TYPE', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5526,13 +5526,8 @@ void RandomBytesProcessSync(Environment* env, | |||
| 5526 | 5526 | void RandomBytes(const FunctionCallbackInfo<Value>& args) { | |
| 5527 | 5527 | Environment* env = Environment::GetCurrent(args); | |
| 5528 | 5528 | ||
| 5529 | - if (!args[0]->IsNumber() || args[0].As<v8::Number>()->Value() < 0) { | ||
| 5530 | - return env->ThrowTypeError("size must be a number >= 0"); | ||
| 5531 | - } | ||
| 5532 | - | ||
| 5533 | 5529 | const int64_t size = args[0]->IntegerValue(); | |
| 5534 | - if (size > Buffer::kMaxLength) | ||
| 5535 | - return env->ThrowTypeError("size must be a uint32"); | ||
| 5530 | + CHECK(size <= Buffer::kMaxLength); | ||
| 5536 | 5531 | ||
| 5537 | 5532 | Local<Object> obj = env->randombytes_constructor_template()-> | |
| 5538 | 5533 | NewInstance(env->context()).ToLocalChecked(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,12 +33,26 @@ crypto.DEFAULT_ENCODING = 'buffer'; | |||
| 33 | 33 | // bump, we register a lot of exit listeners | |
| 34 | 34 | process.setMaxListeners(256); | |
| 35 | 35 | ||
| 36 | - const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/; | ||
| 37 | 36 | [crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) { | |
| 38 | 37 | [-1, undefined, null, false, true, {}, []].forEach(function(value) { | |
| 39 | - assert.throws(function() { f(value); }, expectedErrorRegexp); | ||
| 40 | - assert.throws(function() { f(value, common.mustNotCall()); }, | ||
| 41 | - expectedErrorRegexp); | ||
| 38 | + | ||
| 39 | + common.expectsError( | ||
| 40 | + () => f(value), | ||
| 41 | + { | ||
| 42 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 43 | + type: TypeError, | ||
| 44 | + message: /^The "size" argument must be of type (number|uint32)$/ | ||
| 45 | + } | ||
| 46 | + ); | ||
| 47 | + | ||
| 48 | + common.expectsError( | ||
| 49 | + () => f(value, common.mustNotCall()), | ||
| 50 | + { | ||
| 51 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 52 | + type: TypeError, | ||
| 53 | + message: /^The "size" argument must be of type (number|uint32)$/ | ||
| 54 | + } | ||
| 55 | + ); | ||
| 42 | 56 | }); | |
| 43 | 57 | ||
| 44 | 58 | [0, 1, 2, 4, 16, 256, 1024, 101.2].forEach(function(len) { | |
@@ -464,9 +478,14 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/; | |||
| 464 | 478 | ||
| 465 | 479 | // #5126, "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData() | |
| 466 | 480 | // length exceeds max acceptable value" | |
| 467 | - assert.throws(function() { | ||
| 468 | - crypto.randomBytes((-1 >>> 0) + 1); | ||
| 469 | - }, /^TypeError: size must be a uint32$/); | ||
| 481 | + common.expectsError( | ||
| 482 | + () => crypto.randomBytes((-1 >>> 0) + 1), | ||
| 483 | + { | ||
| 484 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 485 | + type: TypeError, | ||
| 486 | + message: 'The "size" argument must be of type uint32' | ||
| 487 | + } | ||
| 488 | + ); | ||
| 470 | 489 | ||
| 471 | 490 | [1, true, NaN, null, undefined, {}, []].forEach((i) => { | |
| 472 | 491 | common.expectsError( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments