Skip to content

Commit

Permalink
fix: build warning on web
Browse files Browse the repository at this point in the history
  • Loading branch information
dcangulo committed Dec 4, 2023
1 parent b37544f commit c71e65c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/generate-random-bytes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BYTE_LENGTH } from './utils';

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

return bytes;
}

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

return bytes;
}
8 changes: 8 additions & 0 deletions src/generate-random-bytes.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BYTE_LENGTH } from './utils';

export default function generateRandomBytes(): string {
const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));

return bytes;
}

0 comments on commit c71e65c

Please sign in to comment.