Skip to content

Commit

Permalink
Bugfix when Buffer is polyfilled by webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Oct 26, 2022
1 parent ad4bd9c commit b74f460
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions libs/dexie-cloud-common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ export function setByKeyPath(
}
}

export const randomString = typeof Buffer !== 'undefined' ? (bytes: number) => {
// Node
const buf = Buffer.alloc(bytes);
randomFillSync(buf);
return buf.toString("base64");
} : (bytes: number) => {
export const randomString = typeof self !== 'undefined' && typeof crypto !== 'undefined' ? (bytes: number) => {
// Web
const buf = new Uint8Array(bytes);
crypto.getRandomValues(buf);
return btoa(String.fromCharCode.apply(null, buf as any));
}

} : typeof Buffer !== 'undefined' ? (bytes: number) => {
// Node
const buf = Buffer.alloc(bytes);
randomFillSync(buf);
return buf.toString("base64");
} : ()=>{throw new Error("No implementation of randomString was found");}

0 comments on commit b74f460

Please sign in to comment.