Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for crypto globally instead of window #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ function detectPrng() {
if (!root) {
root = typeof window !== "undefined" ? window : null;
}
var browserCrypto = root && (root.crypto || root.msCrypto);
if (browserCrypto) {
var webCrypto = root && (root.crypto || root.msCrypto) || (typeof crypto !== "undefined" ? crypto : null);
if (webCrypto) {
return function () {
var buffer = new Uint8Array(1);
browserCrypto.getRandomValues(buffer);
webCrypto.getRandomValues(buffer);
return buffer[0] / 0xff;
};
} else {
Expand Down
8 changes: 5 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ export function detectPrng(allowInsecure = false, root) {
if (!root) {
root = typeof window !== "undefined" ? window : null;
}
const browserCrypto = root && (root.crypto || root.msCrypto);
if (browserCrypto) {
const webCrypto = root &&
(root.crypto || root.msCrypto) ||
(typeof crypto !== "undefined" ? crypto : null);
if (webCrypto) {
return () => {
const buffer = new Uint8Array(1);
browserCrypto.getRandomValues(buffer);
webCrypto.getRandomValues(buffer);
return buffer[0] / 0xff;
};
}
Expand Down
6 changes: 3 additions & 3 deletions dist/index.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ function detectPrng() {
if (!root) {
root = typeof window !== "undefined" ? window : null;
}
var browserCrypto = root && (root.crypto || root.msCrypto);
if (browserCrypto) {
var webCrypto = root && (root.crypto || root.msCrypto) || (typeof crypto !== "undefined" ? crypto : null);
if (webCrypto) {
return function () {
var buffer = new Uint8Array(1);
browserCrypto.getRandomValues(buffer);
webCrypto.getRandomValues(buffer);
return buffer[0] / 0xff;
};
} else {
Expand Down
8 changes: 5 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ export function detectPrng(allowInsecure: boolean = false, root?: any): PRNG {
root = typeof window !== "undefined" ? window : null
}

const browserCrypto = root && (root.crypto || root.msCrypto)
const webCrypto = root &&
(root.crypto || root.msCrypto) ||
(typeof crypto !== "undefined" ? crypto : null)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably just do typeof crypto and typeof msCrypto instead, but I didn't want to break backward compatibility with the root that can currently be passed into this function.


if (browserCrypto) {
if (webCrypto) {
return () => {
const buffer = new Uint8Array(1)
browserCrypto.getRandomValues(buffer)
webCrypto.getRandomValues(buffer)
return buffer[0] / 0xff
}
} else {
Expand Down