Repository: parse-community/Parse-SDK-JS
Pull request #1356 (2021-05-06 10:40:46)

[bug] Fix weapp uuid error.
In weapp, crypto is not provided.
So, I fake it with random string for installation id.

davimacedo: It makes sense to me. What do you think @dplewis ?
dplewis: The uuid module is used in other places besides the InstallationController. I think a separate `uuid.js` file would suffice.
onerain88: I wrapped the uuid.js instead of uuid/v4
codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) Report
> Merging [#1356](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (e69107c) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/66878f268912d58cb0033ea725e991293923af3c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) (66878f2) will **decrease** coverage by `0.13%`.
> The diff coverage is `52.94%`.

> :exclamation: Current head e69107c differs from pull request most recent head 49d0eb4. Consider uploading reports for the commit 49d0eb4 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356/graphs/tree.svg?width=650&height=150&src=pr&token=8nCENM0QHj&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community)

```diff
@@            Coverage Diff             @@
##           master    #1356      +/-   ##
==========================================
- Coverage   99.23%   99.10%   -0.14%     
==========================================
  Files          60       61       +1     
  Lines        5920     5933      +13     
  Branches     1346     1347       +1     
==========================================
+ Hits         5875     5880       +5     
- Misses         44       52       +8     
  Partials        1        1              
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community) | Coverage Δ | |
|---|---|---|
| [src/uuid.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL3V1aWQuanM=) | `38.46% <38.46%> (ø)` | |
| [src/AnonymousUtils.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL0Fub255bW91c1V0aWxzLmpz) | `100.00% <100.00%> (ø)` | |
| [src/InstallationController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL0luc3RhbGxhdGlvbkNvbnRyb2xsZXIuanM=) | `100.00% <100.00%> (ø)` | |
| [src/ParseObject.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL1BhcnNlT2JqZWN0Lmpz) | `99.89% <100.00%> (ø)` | |
| [src/RESTController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community#diff-c3JjL1JFU1RDb250cm9sbGVyLmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Last update [66878f2...49d0eb4](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1356?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=parse-community).


Patch:
diff --git a/src/AnonymousUtils.js b/src/AnonymousUtils.js
index 872f612..f9de0f0 100644
--- a/src/AnonymousUtils.js
+++ b/src/AnonymousUtils.js
@@ -11,7 +11,7 @@
 
 import ParseUser from './ParseUser';
 import type { RequestOptions } from './RESTController';
-const uuidv4 = require('uuid/v4');
+const uuidv4 = require('./uuid');
 
 let registered = false;
 
diff --git a/src/InstallationController.js b/src/InstallationController.js
index c99697e..08008c1 100644
--- a/src/InstallationController.js
+++ b/src/InstallationController.js
@@ -10,7 +10,7 @@
  */
 
 import Storage from './Storage';
-const uuidv4 = require('uuid/v4');
+const uuidv4 = require('./uuid');
 
 let iidCache = null;
 
diff --git a/src/ParseObject.js b/src/ParseObject.js
index 6cb2143..8f4198c 100644
--- a/src/ParseObject.js
+++ b/src/ParseObject.js
@@ -43,7 +43,7 @@ import unsavedChildren from './unsavedChildren';
 import type { AttributeMap, OpsMap } from './ObjectStateMutations';
 import type { RequestOptions, FullOptions } from './RESTController';
 
-const uuidv4 = require('uuid/v4');
+const uuidv4 = require('./uuid');
 
 export type Pointer = {
   __type: string,
diff --git a/src/RESTController.js b/src/RESTController.js
index 85e18fc..4e06532 100644
--- a/src/RESTController.js
+++ b/src/RESTController.js
@@ -9,7 +9,7 @@
  * @flow
  */
 /* global XMLHttpRequest, XDomainRequest */
-const uuidv4 = require('uuid/v4');
+const uuidv4 = require('./uuid');
 
 import CoreManager from './CoreManager';
 import ParseError from './ParseError';
diff --git a/src/uuid.js b/src/uuid.js
new file mode 100644
index 0000000..c8bfdc4
--- /dev/null
+++ b/src/uuid.js
@@ -0,0 +1,22 @@
+let uuid = null;
+
+if (process.env.PARSE_BUILD === 'weapp') {
+  uuid = function () {
+    const s = [];
+    const hexDigits = '0123456789abcdef';
+
+    for (let i = 0; i < 36; i++) {
+      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
+    }
+
+    s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
+    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
+    s[8] = s[13] = s[18] = s[23] = '-';
+
+    return s.join('');
+  };
+} else {
+  uuid = require('uuid/v4');
+}
+
+module.exports = uuid;


Test patch:
diff --git a/src/__tests__/InstallationController-test.js b/src/__tests__/InstallationController-test.js
index 7d22f24..9c3ff8c 100644
--- a/src/__tests__/InstallationController-test.js
+++ b/src/__tests__/InstallationController-test.js
@@ -11,7 +11,7 @@ jest.dontMock('../CoreManager');
 jest.dontMock('../InstallationController');
 jest.dontMock('../Storage');
 jest.dontMock('../StorageController.default');
-jest.mock('uuid/v4', () => {
+jest.mock('../uuid', () => {
   let value = 0;
   return () => value++ + '';
 });
diff --git a/src/__tests__/ParseObject-test.js b/src/__tests__/ParseObject-test.js
index 9e222e4..e8294ae 100644
--- a/src/__tests__/ParseObject-test.js
+++ b/src/__tests__/ParseObject-test.js
@@ -33,7 +33,7 @@ jest.dontMock('../unsavedChildren');
 jest.dontMock('../ParseACL');
 jest.dontMock('../LocalDatastore');
 
-jest.mock('uuid/v4', () => {
+jest.mock('../uuid', () => {
   let value = 0;
   return () => value++;
 });
diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js
index 26c555b..89150dc 100644
--- a/src/__tests__/ParseQuery-test.js
+++ b/src/__tests__/ParseQuery-test.js
@@ -21,7 +21,7 @@ jest.dontMock('../LocalDatastore');
 jest.dontMock('../OfflineQuery');
 jest.dontMock('../LiveQuerySubscription');
 
-jest.mock('uuid/v4', () => {
+jest.mock('../uuid', () => {
   let value = 0;
   return () => value++;
 });
diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js
index 6c0fa59..8691513 100644
--- a/src/__tests__/ParseUser-test.js
+++ b/src/__tests__/ParseUser-test.js
@@ -31,7 +31,7 @@ jest.dontMock('../UniqueInstanceStateController');
 jest.dontMock('crypto-js/aes');
 jest.dontMock('crypto-js/enc-utf8');
 
-jest.mock('uuid/v4', () => {
+jest.mock('../uuid', () => {
   let value = 0;
   return () => value++;
 });
diff --git a/src/__tests__/RESTController-test.js b/src/__tests__/RESTController-test.js
index 950605f..f2074c4 100644
--- a/src/__tests__/RESTController-test.js
+++ b/src/__tests__/RESTController-test.js
@@ -9,7 +9,7 @@
 
 jest.autoMockOff();
 jest.useFakeTimers();
-jest.mock('uuid/v4', () => {
+jest.mock('../uuid', () => {
   let value = 1000;
   return () => (value++).toString();
 });