-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add package.exports, add broswer implementation
- Loading branch information
Showing
6 changed files
with
56 additions
and
56 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 |
---|---|---|
@@ -1,45 +1,4 @@ | ||
// the file extension is needed for ESM | ||
import { UIDCHARS } from './chars.js'; | ||
import { randomBytes } from 'node:crypto'; | ||
import { generateUidFunction } from './uid.js'; | ||
|
||
export const uid = (len: number) => | ||
new Promise<string>(async (resolve, reject) => { | ||
if (!Number.isInteger(len)) { | ||
reject( | ||
new TypeError('You must supply a length integer to `uid-promise`.'), | ||
); | ||
return; | ||
} | ||
|
||
if (len <= 0) { | ||
reject(new Error('You must supply a length integer greater than zero')); | ||
return; | ||
} | ||
|
||
const isWebCryptoFuncDefined = | ||
typeof globalThis.crypto?.getRandomValues === 'function'; | ||
|
||
const randomBytes = isWebCryptoFuncDefined | ||
? // the file extensions are needed for ESM | ||
await import('./web-random-bytes.js').then((mod) => mod.randomBytes) | ||
: await import('./node-random-bytes.js').then((mod) => mod.randomBytes); | ||
|
||
randomBytes(len, (err, buf) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
const str = []; | ||
let rand; | ||
for (let i = 0; i < buf.length; i++) { | ||
rand = buf[i]; | ||
while (rand > 248) { | ||
try { | ||
rand = randomBytes(1)[0]; | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
str.push(UIDCHARS[rand % UIDCHARS.length]); | ||
} | ||
resolve(str.join('')); | ||
}); | ||
}); | ||
export const uid = generateUidFunction(randomBytes); |
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
// the file extension is needed for ESM | ||
import { UIDCHARS } from './chars.js'; | ||
import type { RandomBytes } from './types.js'; | ||
|
||
export const generateUidFunction = | ||
(randomBytes: RandomBytes) => (len: number) => | ||
new Promise<string>(async (resolve, reject) => { | ||
if (!Number.isInteger(len)) { | ||
reject( | ||
new TypeError('You must supply a length integer to `uid-promise`.'), | ||
); | ||
return; | ||
} | ||
|
||
if (len <= 0) { | ||
reject(new Error('You must supply a length integer greater than zero')); | ||
return; | ||
} | ||
|
||
randomBytes(len, (err, buf) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
const str = []; | ||
let rand; | ||
for (let i = 0; i < buf.length; i++) { | ||
rand = buf[i]; | ||
while (rand > 248) { | ||
try { | ||
rand = randomBytes(1)[0]; | ||
} catch (err) { | ||
reject(err); | ||
} | ||
} | ||
str.push(UIDCHARS[rand % UIDCHARS.length]); | ||
} | ||
resolve(str.join('')); | ||
}); | ||
}); |
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