Add expo-random support to nanoid/async
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent ea3eecb · Browse files · 8 files·+70 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+70 added−22 removed
- tests:+20 added−1 removed
- other:+50 added−21 removed
Provenance
- Commit
3ac74c1- Tree
b9a3893- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
ea3eecb
8 files changed+70 added−22 removed
added:async/random/index.browser.js+4 added
1234let random = bytes => Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes)))
module.exports = { random }added:async/random/index.d.ts+12 added
123456789101112/** * 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>added:async/random/index.js+19 added
12345678910111213141516171819let 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 }added:async/random/index.native.js+5 added
12345let { getRandomBytesAsync } = require('expo-random')
let random = getRandomBytesAsync
module.exports = { random }modified:async/index.browser.js+3 added-1 removed
12345659 unmodified lines666768696870let { random } = require('./random')
let customAlphabet = (alphabet, size) => { // First, a bitmask is necessary to generate the ID. The bitmask makes bytes // values closer to the alphabet size. The bitmask calculates the closest // `2^31 - 1` number, which exceeds the alphabet size.59 unmodified lines } return Promise.resolve(id)}
module.exports = { nanoid, customAlphabet }module.exports = { nanoid, customAlphabet, random }modified:async/index.js+2 added-19 removed
12123567891011121314151617181920456743 unmodified lines515253547255let crypto = require('crypto')
let { urlAlphabet } = require('..')let { random } = require('./random')
// `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) } }) })
let customAlphabet = (alphabet, size) => { // First, a bitmask is necessary to generate the ID. The bitmask makes bytes // values closer to the alphabet size. The bitmask calculates the closest // `2^31 - 1` number, which exceeds the alphabet size.43 unmodified lines } return id })
module.exports = { nanoid, customAlphabet }module.exports = { nanoid, customAlphabet, random }modified:test/async.test.js+20 added-1 removed
21 unmodified lines22 }23 24 for (let type of ['node', 'browser']) {25 describe(`${type}`, () => {26- let { nanoid, customAlphabet } = type === 'node' ? node : browser26+ let { nanoid, customAlphabet, random } = type === 'node' ? node : browser27 28 describe('nanoid', () => {29 function mock (callback) {30 crypto.randomFill = callback75 unmodified lines106 })107 }108 })109 110+ describe('random', () => {111+ it('generates small random buffers', async () => {112+ expect(await random(10)).toHaveLength(10)113+ })114+115+ it('generates random buffers', async () => {116+ let numbers = {}117+ let bytes = await random(10000)118+ expect(bytes).toHaveLength(10000)119+ for (let byte of bytes) {120+ if (!numbers[byte]) numbers[byte] = 0121+ numbers[byte] += 1122+ expect(typeof byte).toEqual('number')123+ expect(byte).toBeLessThanOrEqual(255)124+ expect(byte).toBeGreaterThanOrEqual(0)125+ }126+ })127+ })128+129 describe('customAlphabet', () => {130 it('has options', async () => {131 let nanoidA = customAlphabet('a', 5)132 let id = await nanoidA()33 unmodified linesmodified:package.json+5 added-1 removed
23 unmodified lines24 "browser": {25 "./index.js": "./index.browser.js",26 "./async/index.js": "./async/index.browser.js"27 },28+ "react-native": {29+ "./async/random/index.js": "./async/random/index.native.js",30+ "./async/index.js": "./async/index.js"31+ },32 "bin": "./bin/nanoid.cjs",33 "sideEffects": false,34 "devDependencies": {35 "@logux/eslint-config": "^38.0.0",86 unmodified lines122 "yoda": "off"123 },124 "overrides": [125 {122- "files": "*.rn.js",126+ "files": "*.native.js",127 "rules": {128 "node/no-missing-require": "off",129 "global-require": "off"130 }71 unmodified lines