Skip to content

7.13.2 has unhandled promise rejection Wu.getRandomValues is undefined #2858

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

Closed
pdugan20 opened this issue Apr 2, 2020 · 9 comments
Closed
Assignees

Comments

@pdugan20
Copy link

pdugan20 commented Apr 2, 2020

Describe your environment

  • Operating System version: react native 0.61
  • Browser version: n/a
  • Firebase SDK version: 7.13.2
  • Firebase Product: firestore

Describe the problem

Getting the following error when trying to initialize within react native:

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'Wu.getRandomValues')]

Not sure what Wu is, is that referring to Kai Wu?

Steps to reproduce:

Try and initialize firebase within a react native environment.

Relevant Code:

import Constants from 'expo-constants';
import { decode, encode } from 'base-64';
import firebase from 'firebase';
import '@firebase/firestore';

global.btoa = encode;
global.atob = decode;
global.crypto = {};

global.crypto.getRandomValues = (byteArray) => {
    for (let i = 0; i < byteArray.length; i += 1) {
        byteArray[i] = Math.floor(256 * Math.random());
    }
};

class Fire {
    constructor() {
        if (!firebase.apps.length) {
            firebase.initializeApp(Constants.manifest.extra.firebaseConfig);
        }
    }
}

Fire.shared = new Fire();

export const db = firebase.firestore();
export const storage = firebase.storage();
export const auth = firebase.auth();

export default Fire;
@schmidt-sebastian
Copy link
Contributor

schmidt-sebastian commented Apr 3, 2020

@pdugan20 Thanks for filing this issue.

@dconeybe The following line is likely the culprit:

const crypto = window.crypto || (window as any).msCrypto;

msCrypto is not in any of the .d.ts files we include here: https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/externs.json Hence, it gets mangled.

Since we likely don't want to include an externs file just for this, your best bet may be to use object-literal access: window["msCrypto"] (please verify that this does work - the best build for these kind of verifications is the 'dist/index.esm2017.js' build.

If I interpet this issue correctly. "crypto" is also not available in ReactNative. Otherwise, msCrypto should never get evaluated. Do we need to provide a fallback to Math.random()?

@RWOverdijk
Copy link

I get this on @firebase/[email protected].

@IjzerenHein
Copy link

I get this on @firebase/[email protected].

@RWOverdijk Yes at the moment the latest working version for react-native seems to be 7.9.0

expo/expo#7507 (comment)

@IjzerenHein
Copy link

If I interpet this issue correctly. "crypto" is also not available in ReactNative. Otherwise, msCrypto should never get evaluated. Do we need to provide a fallback to Math.random()?

@schmidt-sebastian Yes this does seem to be the case. At the moment this is breaking the react-native support so a fallback to Math.random would be great on the platforms that don't support crypto.

@dconeybe
Copy link
Contributor

dconeybe commented Apr 6, 2020

I've reverted this "crypto" change for now. Our next release will therefore fix this error.

@dconeybe dconeybe closed this as completed Apr 6, 2020
@IjzerenHein
Copy link

I've reverted this "crypto" change for now. Our next release will therefore fix this error.

Awesome great to hear @dconeybe !

There is currently another issue which breaks the react-native supported due to the absence of atob in the pure js environment. Is this something you also plan on addressing?

@dconeybe
Copy link
Contributor

dconeybe commented Apr 6, 2020

There are no immediate plans to fix that issue at the moment unfortunately. We are, however, aware of the issue. For now, you will just need to continue to polyfill atob and btoa.

@pdugan20
Copy link
Author

pdugan20 commented Apr 6, 2020

@dconeybe thanks for the quick fix! appreciate it!

@IjzerenHein
Copy link

Alright thanks for the info @dconeybe !

@firebase firebase locked and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants