Skip to content

Commit e06b8a7

Browse files
authored
perf: Replace uuid dependency with native crypto.randomUUID() API (#2810)
2 parents 687cd9a + 5f488cb commit e06b8a7

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

integration/test/ParseUserTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const assert = require('assert');
44
const Parse = require('../../node');
5-
const { randomUUID: uuidv4 } = require('crypto');
5+
const uuidv4 = require('../../lib/node/uuid').default;
66
const { twitterAuthData } = require('./helper');
77

88
class CustomUser extends Parse.User {

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"dependencies": {
3434
"@babel/runtime-corejs3": "7.28.4",
3535
"react-native-crypto-js": "1.0.0",
36-
"uuid": "13.0.0",
3736
"ws": "8.18.3"
3837
},
3938
"devDependencies": {
@@ -136,7 +135,6 @@
136135
".*": "./babel-jest.js"
137136
},
138137
"transformIgnorePatterns": [
139-
"/node_modules/(?!uuid)/",
140138
"package.json"
141139
],
142140
"testEnvironment": "jsdom"

src/__tests__/browser-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ jest.dontMock('../EventEmitter');
77
jest.dontMock('../Parse');
88
jest.dontMock('../RESTController');
99
jest.dontMock('../Storage');
10+
jest.dontMock('../uuid');
1011
jest.dontMock('crypto-js/aes');
1112
jest.setMock('../EventuallyQueue', { poll: jest.fn() });
1213

@@ -151,4 +152,13 @@ describe('Browser', () => {
151152
}
152153
expect(called).toBe(true);
153154
});
155+
156+
it('load uuid module', () => {
157+
const uuidv4 = require('../uuid').default;
158+
const uuid1 = uuidv4();
159+
const uuid2 = uuidv4();
160+
expect(uuid1).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
161+
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);
162+
expect(uuid1).not.toEqual(uuid2);
163+
});
154164
});

src/__tests__/weapp-test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ const mockWeChat = require('./test_helpers/mockWeChat');
2020

2121
global.wx = mockWeChat;
2222

23-
jest.mock('uuid', () => {
24-
return () => 0;
25-
});
26-
2723
describe('WeChat', () => {
2824
beforeEach(() => {
2925
process.env.PARSE_BUILD = 'weapp';

src/uuid.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ if (process.env.PARSE_BUILD === 'weapp') {
1414
s[8] = s[13] = s[18] = s[23] = '-';
1515
return s.join('');
1616
};
17-
} else if (process.env.PARSE_BUILD === 'node' || process.env.PARSE_BUILD === 'react-native') {
18-
// Use Node.js built-in crypto.randomUUID() for Node and React Native builds
19-
// React Native tests run in Node.js environment, and uuid v13 is ESM-only
20-
uuid = require('crypto').randomUUID;
17+
} else if (process.env.PARSE_BUILD === 'browser') {
18+
// Use native crypto.randomUUID() from the Web Crypto API in browsers
19+
uuid = () => globalThis.crypto.randomUUID();
2120
} else {
22-
// Use uuid package for browser builds
23-
uuid = require('uuid').v4;
21+
// Use Node.js crypto.randomUUID() for Node and React Native builds
22+
uuid = require('crypto').randomUUID;
2423
}
2524

2625
export default uuid;

0 commit comments

Comments
 (0)