Skip to content

Commit

Permalink
crypto: align parameter names with documentation
Browse files Browse the repository at this point in the history
Change _cb_ to _callback_ to align with documentation. This is so that
stack traces and error messages align with the documentation. If the
documentation says "callback", then the stack traces and error messages
should indicate that "callback" needs to be function or whatever, rather
than "cb".

PR-URL: #35054
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Trott authored and addaleax committed Sep 22, 2020
1 parent 53c9975 commit 921129c
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions lib/internal/crypto/random.js
Expand Up @@ -49,19 +49,19 @@ function assertSize(size, elementSize, offset, length) {
return size >>> 0; // Convert to uint32.
}

function randomBytes(size, cb) {
function randomBytes(size, callback) {
size = assertSize(size, 1, 0, Infinity);
if (cb !== undefined && typeof cb !== 'function')
throw new ERR_INVALID_CALLBACK(cb);
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK(callback);

const buf = new FastBuffer(size);

if (!cb) return handleError(_randomBytes(buf, 0, size), buf);
if (!callback) return handleError(_randomBytes(buf, 0, size), buf);

const wrap = new AsyncWrap(Providers.RANDOMBYTESREQUEST);
wrap.ondone = (ex) => { // Retains buf while request is in flight.
if (ex) return cb.call(wrap, ex);
cb.call(wrap, null, buf);
if (ex) return callback.call(wrap, ex);
callback.call(wrap, null, buf);
};

_randomBytes(buf, 0, size, wrap);
Expand All @@ -85,22 +85,22 @@ function randomFillSync(buf, offset = 0, size) {
return handleError(_randomBytes(buf, offset, size), buf);
}

function randomFill(buf, offset, size, cb) {
function randomFill(buf, offset, size, callback) {
if (!isArrayBufferView(buf)) {
throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView', buf);
}

const elementSize = buf.BYTES_PER_ELEMENT || 1;

if (typeof offset === 'function') {
cb = offset;
callback = offset;
offset = 0;
size = buf.bytesLength;
} else if (typeof size === 'function') {
cb = size;
callback = size;
size = buf.byteLength - offset;
} else if (typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK(cb);
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}

offset = assertOffset(offset, elementSize, buf.byteLength);
Expand All @@ -113,8 +113,8 @@ function randomFill(buf, offset, size, cb) {

const wrap = new AsyncWrap(Providers.RANDOMBYTESREQUEST);
wrap.ondone = (ex) => { // Retains buf while request is in flight.
if (ex) return cb.call(wrap, ex);
cb.call(wrap, null, buf);
if (ex) return callback.call(wrap, ex);
callback.call(wrap, null, buf);
};

_randomBytes(buf, offset, size, wrap);
Expand All @@ -126,22 +126,22 @@ const RAND_MAX = 0xFFFF_FFFF_FFFF;

// Generates an integer in [min, max) range where min is inclusive and max is
// exclusive.
function randomInt(min, max, cb) {
function randomInt(min, max, callback) {
// Detect optional min syntax
// randomInt(max)
// randomInt(max, cb)
// randomInt(max, callback)
const minNotSpecified = typeof max === 'undefined' ||
typeof max === 'function';

if (minNotSpecified) {
cb = max;
callback = max;
max = min;
min = 0;
}

const isSync = typeof cb === 'undefined';
if (!isSync && typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK(cb);
const isSync = typeof callback === 'undefined';
if (!isSync && typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK(callback);
}
if (!NumberIsSafeInteger(min)) {
throw new ERR_INVALID_ARG_TYPE('min', 'safe integer', min);
Expand Down Expand Up @@ -180,15 +180,15 @@ function randomInt(min, max, cb) {
// Async API
const pickAttempt = () => {
randomBytes(6, (err, bytes) => {
if (err) return cb(err);
if (err) return callback(err);
const x = bytes.readUIntBE(0, 6);
// If x > (maxVal - (maxVal % range)), we will get "modulo bias"
if (x > randLimit) {
// Try again
return pickAttempt();
}
const n = (x % range) + min;
cb(null, n);
callback(null, n);
});
};

Expand Down

0 comments on commit 921129c

Please sign in to comment.