★ wanayoo — archive 1999 https://github.com/yahoo/serialize-javascript/issues/87Nouvelle recherche | Portail wanayoo
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

Version 3.1.0 is not working in React-Native when is not in debugging mode or when is generating release #87

Open
danilojpferreira opened this issue Jun 5, 2020 · 3 comments

Comments

@danilojpferreira
Copy link

@danilojpferreira danilojpferreira commented Jun 5, 2020

Hello, I'm using serialize-javascript in React-Native instead of JSON.stringify function. The version 3.1.0 is not working in React-Native when the debugger is off or in the release apk (because there is no debugger).

The issue is:
Error: Secure random number generation is not supported by this browser. Use Chrome, Firefox or Internet Explorer 11.

Reproduce:

  1. In React-Native project install with npm, npx or yarn the package serialize-javascript.
  2. Create a serialize function:
import serializeJs from 'serialize-javascript';
...

const clearArrayUndefinedValues = (array) => {
 try {
   if (!Array.isArray(array))
     throw new Error('Invalid argument. Must be an array.');

   const filteredArr = array.reduce((arr, current) => {
     if (typeof current === 'object' && current !== null) {
       if (Array.isArray(current))
         return [...arr, clearArrayUndefinedValues(current)];
       // eslint-disable-next-line no-use-before-define
       return [...arr, clearObjectUndefinedValues(current)];
     } else if (current !== undefined) return [...arr, current];

     return arr;
   }, []);

   return filteredArr;
 } catch (error) {
   console.log(
     '##\t Error log in clearArrayUndefinedValues on app/shared/utils.js ->',
     error
   );
   return null;
 }
};

const clearObjectUndefinedValues = (object) => {
 try {
   if (typeof object !== 'object')
     throw new Error('Invalid argument. Must be an object.');

   if (object === null) throw new Error('Invalid argument. Must not be null.');

   const filtered = Object.keys(object).reduce((obj, key) => {
     const { [key]: current } = object;

     if (typeof current === 'object' && current !== null) {
       if (Array.isArray(current))
         return { ...obj, [key]: clearArrayUndefinedValues(current) };

       return { ...obj, [key]: clearObjectUndefinedValues(current) };
     } else if (current !== undefined) return { ...obj, [key]: current };

     return obj;
   }, {});

   return filtered;
 } catch (error) {
   console.log(
     '##\t Error log in clearObjectUndefinedValues on app/shared/utils.js ->',
     error
   );
   return null;
 }
};

export const serialize = (object) => {
 try {
   if (typeof object !== 'object')
     throw new Error('Invalid argument. Must be an object.');

   if (object === null) throw new Error('Invalid argument. Must not be null.');

   if (Array.isArray(object))
     return serializeJs(clearArrayUndefinedValues(object));

   return serializeJs(clearObjectUndefinedValues(object));
 } catch (error) {
   console.log('##\t Error log in serialize on app/shared/utils.js ->', error);
   return null;
 }
};
  1. Use a serialize function in any place of your code:
import { serialize } from '../shared/utils';
...
const  JSONToObject = serialize(data);

My set is:

MacOs 10.15.5
i7 2.2Ghz
16GB

Running app in simulators:

iPhone 11 os 13.5
Pixel 3 os Android API 29.

Chrome version:

83.0.4103.61 64 bits

The previous version (3.0.0) working well. There are a prevision/way to fix this to work without debugger?

@okuryu
Copy link
Collaborator

@okuryu okuryu commented Jun 5, 2020

I'm not familiar with React Native, but as the error message says, you need crypto.randomBytes (for Node.js) or Crypto.getRandomValues (for browsers). If you can apply something like the crypto pollyfill libs for React Native it might work well.

@danilojpferreira
Copy link
Author

@danilojpferreira danilojpferreira commented Jun 12, 2020

I tried to find something like this but I've no success. Idk how to fix this.

@birdofpreyru
Copy link

@birdofpreyru birdofpreyru commented Jun 14, 2020

I also bumped into this issue.

It happens because of the following single line (and the dependency on randombytes library, introduced by it):

var bytes = randomBytes(UID_LENGTH);

This comes from this commit: f21a6fb

I don't really understand, why random UUIDs are necessary during JS serialization. Lazy to dig in too deep, but my guess is that original implementation just cut some corners, and used in-place UID instead of building a separate index of transformed entities, thus the correct fix of the problem should be not relying on a more randomized UUID, but re-writing the algo to not use UUIDs at all.

The working workaround for RN is to shim randombytes with https://www.npmjs.com/package/react-native-randombytes, but it requires some efforts to setup, and alias the randombytes for 3-rd party packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.