Skip to content

Commit c71e65c

Browse files
committed
fix: build warning on web
1 parent b37544f commit c71e65c

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/generate-random-bytes.ts

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

33
export default function generateRandomBytes(): string {
4-
if (typeof window !== 'undefined') {
5-
const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
6-
const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
4+
if (typeof window === 'undefined') {
5+
const buffer = require('crypto').randomBytes(BYTE_LENGTH);
6+
const bytes = buffer.toString('base64');
77

88
return bytes;
99
}
1010

11-
const buffer = require('crypto').randomBytes(BYTE_LENGTH);
12-
const bytes = buffer.toString('base64');
11+
const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
12+
const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
1313

1414
return bytes;
1515
}

src/generate-random-bytes.web.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BYTE_LENGTH } from './utils';
2+
3+
export default function generateRandomBytes(): string {
4+
const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
5+
const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
6+
7+
return bytes;
8+
}

0 commit comments

Comments
 (0)