Skip to content

Commit 57cbcdb

Browse files
committed
fix UUID generation on non secure context
1 parent f8d85f9 commit 57cbcdb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/comms.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
ExpandTableResultResponse,
1616
SetTabTitleResponse,
1717
} from "./responseTypes";
18+
import { generateUUID } from "./utils";
1819

1920
// Define a custom import.meta interface for TypeScript
2021
declare global {
@@ -100,7 +101,7 @@ export async function request(name: unknown, args?: unknown): Promise<unknown> {
100101

101102
return new Promise<any>((resolve, reject) => {
102103
try {
103-
const id = crypto.randomUUID();
104+
const id = generateUUID();
104105
const data = { id, name, args };
105106
pendingRequests.set(id, { name: name as string, resolve, reject });
106107
window.parent.postMessage(data, "*");

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function generateUUID() {
2+
const buf = new Uint8Array(16);
3+
crypto.getRandomValues(buf);
4+
5+
buf[6] = (buf[6] & 0x0f) | 0x40; // version 4
6+
buf[8] = (buf[8] & 0x3f) | 0x80; // variant
7+
8+
const hex = Array.from(buf, (b) => b.toString(16).padStart(2, "0")).join("");
9+
10+
return [
11+
hex.substring(0, 8),
12+
hex.substring(8, 12),
13+
hex.substring(12, 16),
14+
hex.substring(16, 20),
15+
hex.substring(20),
16+
].join("-");
17+
}

0 commit comments

Comments
 (0)