Skip to content

Commit f8d7aae

Browse files
authored
Fallback for randomUUID when not available (non secure context) (#472)
1 parent 7cf78e1 commit f8d7aae

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

.changeset/khaki-items-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'playroom': patch
3+
---
4+
5+
Fallback for randomUUID when not available in development (i.e. non secure context)

cypress/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"types": ["cypress", "@testing-library/cypress"],
4+
"types": ["cypress", "@testing-library/cypress", "@types/node"],
55
"isolatedModules": false
66
},
77
"include": [

src/contexts/StoreContext.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from '../configModules/themes';
2323
import availableWidths, { type Widths } from '../configModules/widths';
2424
import { isValidLocation } from '../utils/cursor';
25+
import { fallbackUuid } from '../utils/fallbackUuid';
2526
import { formatForInsertion, formatAndInsert } from '../utils/formatting';
2627
import {
2728
getDataParam,
@@ -189,7 +190,10 @@ const sortStoredPlayrooms = (storedPlayrooms: State['storedPlayrooms']) =>
189190
)
190191
);
191192

192-
const createPlayroomId = () => self.crypto.randomUUID();
193+
const createPlayroomId = () =>
194+
!self.crypto.randomUUID && process.env.NODE_ENV === 'development'
195+
? fallbackUuid()
196+
: self.crypto.randomUUID();
193197

194198
const reducer = (state: State, action: Action): State => {
195199
switch (action.type) {

src/utils/fallbackUuid.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** Generate a UUID: https://stackoverflow.com/questions/105034/how-do-i-create-a-guid-uuid/2117523#2117523 */
2+
export const fallbackUuid = () =>
3+
'10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) =>
4+
(
5+
Number(c) ^
6+
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (Number(c) / 4)))
7+
).toString(16)
8+
);

0 commit comments

Comments
 (0)