1- let crypto = require ( 'crypto' )
1+ import { randomFillSync } from 'crypto'
22
3- let { urlAlphabet } = require ( './url-alphabet' )
3+ import { urlAlphabet } from './url-alphabet/index.js'
4+
5+ export { urlAlphabet }
46
57// It is best to make fewer, larger requests to the crypto module to
68// avoid system call overhead. So, random numbers are generated in a
@@ -13,22 +15,22 @@ let pool, poolOffset
1315let fillPool = bytes => {
1416 if ( ! pool || pool . length < bytes ) {
1517 pool = Buffer . allocUnsafe ( bytes * POOL_SIZE_MULTIPLIER )
16- crypto . randomFillSync ( pool )
18+ randomFillSync ( pool )
1719 poolOffset = 0
1820 } else if ( poolOffset + bytes > pool . length ) {
19- crypto . randomFillSync ( pool )
21+ randomFillSync ( pool )
2022 poolOffset = 0
2123 }
2224 poolOffset += bytes
2325}
2426
25- let random = bytes => {
27+ export let random = bytes => {
2628 // `-=` convert `bytes` to number to prevent `valueOf` abusing
2729 fillPool ( ( bytes -= 0 ) )
2830 return pool . subarray ( poolOffset - bytes , poolOffset )
2931}
3032
31- let customRandom = ( alphabet , defaultSize , getRandom ) => {
33+ export let customRandom = ( alphabet , defaultSize , getRandom ) => {
3234 // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
3335 // values closer to the alphabet size. The bitmask calculates the closest
3436 // `2^31 - 1` number, which exceeds the alphabet size.
@@ -63,10 +65,10 @@ let customRandom = (alphabet, defaultSize, getRandom) => {
6365 }
6466}
6567
66- let customAlphabet = ( alphabet , size = 21 ) =>
68+ export let customAlphabet = ( alphabet , size = 21 ) =>
6769 customRandom ( alphabet , size , random )
6870
69- let nanoid = ( size = 21 ) => {
71+ export let nanoid = ( size = 21 ) => {
7072 // `-=` convert `size` to number to prevent `valueOf` abusing
7173 fillPool ( ( size -= 0 ) )
7274 let id = ''
@@ -81,5 +83,3 @@ let nanoid = (size = 21) => {
8183 }
8284 return id
8385}
84-
85- module . exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
0 commit comments