Skip to content
Prev Previous commit
Next Next commit
crypto: use nullish assignment when possible
Co-authored-by: James M Snell <[email protected]>
  • Loading branch information
rangoo94 and jasnell committed Feb 24, 2021
commit 321ad2368efac4a7faadb892e189c95740b8c4e8
18 changes: 7 additions & 11 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,21 @@ function serializeUUID(buf, offset = 0) {
}

function getBufferedUUID() {
if (uuidData === undefined) {
uuidData = secureBuffer(16 * kBatchSize);
if (uuidData === undefined)
throw new ERR_OPERATION_FAILED('Out of memory');
}
uuidData ??= secureBuffer(16 * kBatchSize);
if (uuidData === undefined)
throw new ERR_OPERATION_FAILED('Out of memory');

if (uuidBatch === 0) randomFillSync(uuidData);
uuidBatch = (uuidBatch + 1) % kBatchSize;
return serializeUUID(uuidData, uuidBatch * 16);
}

function getUnbufferedUUID() {
let uuidBuf = uuidNotBuffered;
if (uuidBuf === undefined)
uuidBuf = uuidNotBuffered = secureBuffer(16);
if (uuidBuf === undefined)
uuidNotBuffered ??= secureBuffer(16);
if (uuidNotBuffered === undefined)
throw new ERR_OPERATION_FAILED('Out of memory');
randomFillSync(uuidBuf);
return serializeUUID(uuidBuf);
randomFillSync(uuidNotBuffered);
return serializeUUID(uuidNotBuffered);
}

function randomUUID(options) {
Expand Down