-
-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add expo-random support to nanoid/async
- Loading branch information
Showing
8 changed files
with
70 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
let random = bytes => | ||
Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes))) | ||
|
||
module.exports = { random } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Generate an array of random bytes collected from hardware noise. | ||
* | ||
* ```js | ||
* import { customRandom, random } from 'nanoid' | ||
* const nanoid = customRandom("abcdef", 5, random) | ||
* ``` | ||
* | ||
* @param bytes Size of the array. | ||
* @returns An array of random bytes. | ||
*/ | ||
export function random (bytes: number): Promise<Uint8Array> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let crypto = require('crypto') | ||
|
||
// `crypto.randomFill()` is a little faster than `crypto.randomBytes()`, | ||
// because it is possible to use in combination with `Buffer.allocUnsafe()`. | ||
let random = bytes => | ||
new Promise((resolve, reject) => { | ||
// `Buffer.allocUnsafe()` is faster because it doesn’t flush the memory. | ||
// Memory flushing is unnecessary since the buffer allocation itself resets | ||
// the memory with the new bytes. | ||
crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(buf) | ||
} | ||
}) | ||
}) | ||
|
||
module.exports = { random } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let { getRandomBytesAsync } = require('expo-random') | ||
|
||
let random = getRandomBytesAsync | ||
|
||
module.exports = { random } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters