Skip to content

Commit 82e50cd

Browse files
committed
feat: add node.js support
1 parent 47227f7 commit 82e50cd

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/generate-random-bytes.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import { BYTE_LENGTH } from './utils';
22

33
export default function generateRandomBytes(): string {
4-
const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
5-
const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
4+
if (typeof window !== 'undefined') {
5+
const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
6+
const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
7+
8+
return bytes;
9+
}
10+
11+
const buffer = require('crypto').randomBytes(BYTE_LENGTH);
12+
const bytes = buffer.toString('base64');
613

714
return bytes;
815
}

0 commit comments

Comments
 (0)