cmux/node_modules/uid-promise/lib/index.js
2026-01-29 17:36:26 -08:00

23 lines
525 B
JavaScript

// Packages
const crypto = require('crypto')
// Utilities
const UIDCHARS = require('./chars')
module.exports = len => new Promise((resolve, reject) => {
if (typeof len !== 'number') {
reject(new TypeError('You must supply a length integer to `uid-promise`.'))
return
}
crypto.randomBytes(len, (err, buf) => {
if (err) {
return reject(err)
}
const str = []
for (let i = 0; i < buf.length; i++) {
str.push(UIDCHARS[buf[i] % UIDCHARS.length])
}
resolve(str.join(''))
})
})