-
-
Notifications
You must be signed in to change notification settings - Fork 600
fix: crypto.randomUUID causes React-Native build failures
#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
base: alpha
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,25 +1,11 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let uuid: () => string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (process.env.PARSE_BUILD === 'weapp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uuid = function () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const s: string[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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((Number(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 if (process.env.PARSE_BUILD === 'browser') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Use native crypto.randomUUID() from the Web Crypto API in browsers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uuid = () => globalThis.crypto.randomUUID(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Use Node.js crypto.randomUUID() for Node and React Native builds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uuid = require('crypto').randomUUID; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const uuid = (): string => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof crypto === 'undefined' || typeof crypto.randomUUID !== 'function') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'crypto.randomUUID is not available. ' + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'For React Native, import "react-native-random-uuid"' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+4
to
+5
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'crypto.randomUUID is not available. ' + | |
| 'For React Native, import "react-native-random-uuid"' | |
| 'crypto.randomUUID is not available in this environment. ' + | |
| 'Use a UUID polyfill or environment-specific implementation (for example, in React Native you can import "react-native-random-uuid").' |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new uuid implementation relies on the global crypto object being available. However, the WeChat mini-program (weapp) environment may not have the crypto object or crypto.randomUUID available. The previous implementation had a custom fallback UUID generation for weapp. This change could break the existing weapp test that expects uuid to work in that environment (see src/tests/weapp-test.js line 58-62). Consider adding a fallback implementation for environments where crypto.randomUUID is not available, or verify that weapp does indeed support crypto.randomUUID.
| if (typeof crypto === 'undefined' || typeof crypto.randomUUID !== 'function') { | |
| throw new Error( | |
| 'crypto.randomUUID is not available. ' + | |
| 'For React Native, import "react-native-random-uuid"' | |
| ); | |
| } | |
| return crypto.randomUUID(); | |
| // Prefer native crypto.randomUUID when available | |
| if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { | |
| return crypto.randomUUID(); | |
| } | |
| // Fallback implementation for environments without crypto.randomUUID | |
| // (e.g., WeChat mini-program, older browsers, some React Native setups) | |
| let d = Date.now(); | |
| let d2 = | |
| typeof performance !== 'undefined' && typeof performance.now === 'function' | |
| ? performance.now() * 1000 | |
| : 0; | |
| return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { | |
| let r = Math.random() * 16; | |
| if (d > 0) { | |
| r = (d + r) % 16 | 0; | |
| d = Math.floor(d / 16); | |
| } else { | |
| r = (d2 + r) % 16 | 0; | |
| d2 = Math.floor(d2 / 16); | |
| } | |
| const v = c === 'x' ? r : (r & 0x3) | 0x8; | |
| return v.toString(16); | |
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| declare let uuid: () => string; | ||
| declare const uuid: () => string; | ||
| export default uuid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test doesn't have proper cleanup if the test fails before global.crypto is restored. If the uuidv4() call doesn't throw an error as expected, global.crypto will remain deleted, potentially affecting other tests. Consider using a finally block or Jest's afterEach/beforeEach hooks to ensure proper cleanup.