question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should be possible to use `crypto.randomBytes`'s async API

See original GitHub issue

Running my program with --trace-sync-io, I see:

WARNING: Detected use of sync API
at _rng (/home/blong/trello-idea-powerup/node_modules/node-uuid/uuid.js:60:53)
at v4 (/home/blong/trello-idea-powerup/node_modules/node-uuid/uuid.js:224:55)
// etc.

The problem is that you’re using crypto.randomBytes synchronously, and don’t seem to provide a way to pass a callback and use the async API.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
janazcommented, Nov 6, 2018

It’s possible to use the library in async mode by providing the random option item obtained in an async fashion:

const crypto = require('crypto');
const uuid = require('uuid/v4');

const uuidPromise = (u) => new Promise((resolve, reject) => {
  crypto.randomBytes(16, (err, random) => {
    if (err) {
      return reject(err);
    }
    resolve(u({random}));
  });
});

// Usage
uuidPromise(uuid)
 .then(u => console.log("UUID", u))
 .catch(e => console.error(e));
5reactions
jstewmoncommented, Oct 30, 2020

👋 Hi, stumbled across this issue today, read through the comments and wanted to mention that I think the case for using the async version of randomBytes is to avoid blocking the event loop because the operation may block waiting for entropy, which will almost never happen outside of a virtualized or containerized environment.

But, it is not a hypothetical problem for workloads running in virtualized or containerized environments. A quick search for random entropy block virtualized containerized will turn up plenty of material on the subject.

Personally, I’ve seen this problem manifest a few times in automated test suites that run in a containerized environment running on a virtualized environment - tests would time out waiting on randomBytes.

Anyway, I haven’t noticed this issue with this library specifically (great library by the way!), and as was mentioned, it is possible to BYO random bytes, which can be procured asynchronously, so there doesn’t seem to be change required. I just wanted to try to add some context regarding why using the async version would be preferential for some use cases. ✌️

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use async/await using crypto.randomBytes in nodejs?
Can anyone help me with this? All I'm trying to do is to get randomBytes async. using the async./await block but ensure that...
Read more >
Crypto | Node.js v19.3.0 Documentation
It is possible for Node.js to be built without including support for the node:crypto module. In such cases, attempting to import from crypto...
Read more >
Node.js crypto.randomBytes() Method - GeeksforGeeks
The crypto.randomBytes() method is used to generate a cryptographically well-built artificial random data and the number of bytes to be ...
Read more >
Random - Expo Documentation
expo-random provides a native interface for creating strong random bytes. With Random you can create values equivalent to Node.js core crypto.randomBytes API.
Read more >
Crypto.getRandomValues() - Web APIs | MDN
User agents are instead urged to provide the best entropy they can when generating random numbers, using a well-defined, efficient pseudorandom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found