Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Snyk Badge](https://snyk.io/test/github/parse-community/Parse-SDK-JS/badge.svg)](https://snyk.io/test/github/parse-community/Parse-SDK-JS)
[![Coverage](https://codecov.io/gh/parse-community/Parse-SDK-JS/branch/alpha/graph/badge.svg)](https://codecov.io/gh/parse-community/Parse-SDK-JS)

[![Node Version](https://img.shields.io/badge/nodejs-18,_20,_22,_24-green.svg?logo=node.js&style=flat)](https://nodejs.org/)
[![Node Version](https://img.shields.io/badge/nodejs-20,_22,_24-green.svg?logo=node.js&style=flat)](https://nodejs.org/)
[![auto-release](https://img.shields.io/badge/%F0%9F%9A%80-auto--release-9e34eb.svg)](https://github.com/parse-community/parse-dashboard/releases)

[![npm latest version](https://img.shields.io/npm/v/parse/latest.svg)](https://www.npmjs.com/package/parse)
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,17 @@ 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', () => {
const tmp = global.crypto;
delete global.crypto;
try {
const uuidv4 = require('../uuid').default;
uuidv4();
expect(true).toBe(false);
} catch (e) {
expect(e.message).toBe('crypto.randomUUID is not available. For React Native, import "react-native-random-uuid"');
}
global.crypto = tmp;
});
});
32 changes: 9 additions & 23 deletions src/uuid.ts
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 = () => {
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();
};

export default uuid;
2 changes: 1 addition & 1 deletion types/AnonymousUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ declare const AnonymousUtils: {
getAuthType(): string;
getAuthData(): {
authData: {
id: string;
id: `${string}-${string}-${string}-${string}-${string}`;
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion types/Parse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ declare const Parse: {
getAuthType(): string;
getAuthData(): {
authData: {
id: string;
id: `${string}-${string}-${string}-${string}-${string}`;
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion types/uuid.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare let uuid: () => string;
declare const uuid: () => `${string}-${string}-${string}-${string}-${string}`;
export default uuid;