Skip to content

Commit

Permalink
crypto: move _randomBytes call out of handleError funct
Browse files Browse the repository at this point in the history
This commit moves the _randomBytes function call out of the handleError
function, which now it takes in an error and a buf object as its
parameters.

PR-URL: #28318
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
danbev authored and targos committed Jul 2, 2019
1 parent def96ae commit 47b230a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function randomBytes(size, cb) {

const buf = Buffer.alloc(size);

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

const wrap = new AsyncWrap(Providers.RANDOMBYTESREQUEST);
wrap.ondone = (ex) => { // Retains buf while request is in flight.
Expand All @@ -77,7 +77,7 @@ function randomFillSync(buf, offset = 0, size) {
size = assertSize(size, elementSize, offset, buf.byteLength);
}

return handleError(buf, offset, size);
return handleError(_randomBytes(buf, offset, size), buf);
}

function randomFill(buf, offset, size, cb) {
Expand Down Expand Up @@ -115,8 +115,7 @@ function randomFill(buf, offset, size, cb) {
_randomBytes(buf, offset, size, wrap);
}

function handleError(buf, offset, size) {
const ex = _randomBytes(buf, offset, size);
function handleError(ex, buf) {
if (ex) throw ex;
return buf;
}
Expand Down

0 comments on commit 47b230a

Please sign in to comment.