Skip to content

Commit

Permalink
fix: add package.exports, add broswer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
agadzik committed May 16, 2023
1 parent dd1d989 commit ea2c7d9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
"license": "MIT",
"description": "generates a cryptographically strong random uid",
"repository": "vercel/uid-promise",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
"types": "lib/cjs/index.d.ts",
"types": "lib/types/index.d.ts",
"packageManager": "pnpm@8.5.1",
"engines": {
"node": ">=18"
},
"files": [
"lib"
],
"exports": {
"import": "./lib/esm/index.browser.js",
"default": "./lib/cjs/index.js"
},
"scripts": {
"build": "pnpm build:cjs && pnpm build:esm",
"build:cjs": "tsc",
"build:esm": "tsc --module esnext --outDir ./lib/esm && echo '{ \"type\": \"module\" }' > ./lib/esm/package.json",
"test": "pnpm test:cjs && pnpm test:esm && pnpm test:webcrypto",
"test": "pnpm test:cjs && pnpm test:esm",
"test:cjs": "pnpm build:cjs && node --test test/test.js",
"test:esm": "pnpm build:esm && node --test test/test-esm.mjs",
"test:webcrypto": "pnpm build:cjs && node --test --experimental-global-webcrypto test/test.js"
"test:esm": "pnpm build:esm && node --test test/test-esm.mjs"
},
"devDependencies": {
"@types/node": "20.1.3",
Expand Down
8 changes: 6 additions & 2 deletions src/web-random-bytes.ts → src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { RandomBytes } from './types';
// the file extension is needed for ESM
import type { RandomBytes } from './types.js';
import { generateUidFunction } from './uid.js';

export const randomBytes: RandomBytes = (size, callback) => {
const randomBytes: RandomBytes = (size, callback) => {
if (size < 0 || size > 65536) {
throw new RangeError(
'The value of "size" is out of range. It must be >= 0 && <= 65536. Received ' +
Expand All @@ -20,3 +22,5 @@ export const randomBytes: RandomBytes = (size, callback) => {

return new Uint8Array();
};

export const uid = generateUidFunction(randomBytes);
47 changes: 3 additions & 44 deletions src/index.ts
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);
4 changes: 0 additions & 4 deletions src/node-random-bytes.ts

This file was deleted.

39 changes: 39 additions & 0 deletions src/uid.ts
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(''));
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./lib/types",
"esModuleInterop": true,
"lib": ["esnext", "DOM"],
"module": "commonjs",
Expand Down

0 comments on commit ea2c7d9

Please sign in to comment.