-
-
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 16 commits
a79469c
1b8aa51
7ba5b9b
fe8a508
6e4fb95
0855c31
7a51152
2865cd0
9b16ba2
e14ed7f
734dca1
488fa4f
829b785
cdbe45b
4cac018
d42660a
b3c41db
effdd62
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 |
|---|---|---|
|
|
@@ -161,4 +161,15 @@ describe('Browser', () => { | |
| expect(uuid2).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i); | ||
| expect(uuid1).not.toEqual(uuid2); | ||
| }); | ||
|
|
||
| it('throw error if randomUUID is not available', () => { | ||
| jest.resetModules(); | ||
| const tmp = global.crypto; | ||
| delete global.crypto; | ||
| const uuidv4 = require('../uuid').default; | ||
| expect(() => uuidv4()).toThrow( | ||
| '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").' | ||
| ); | ||
| global.crypto = tmp; | ||
| }); | ||
|
Comment on lines
+165
to
+174
|
||
| }); | ||
| 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 in this environment. ' + | ||
| 'Use a UUID polyfill or environment-specific implementation (for example, in React Native you can import "react-native-random-uuid").' | ||
| ); | ||
| } | ||
| return crypto.randomUUID(); | ||
| }; | ||
|
Comment on lines
+1
to
+9
|
||
|
|
||
| export default uuid; | ||
| 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; |
Uh oh!
There was an error while loading. Please reload this page.