23 lines
529 B
TypeScript
23 lines
529 B
TypeScript
const PEER_ID_KEY = "p2p-poll-peer-id";
|
|
|
|
/** Generate a UUID v4 */
|
|
function generateUUID(): string {
|
|
return crypto.randomUUID();
|
|
}
|
|
|
|
/**
|
|
* Get or create a persistent peer ID.
|
|
* Stored in localStorage so each browser tab/device gets a stable identity.
|
|
*/
|
|
export function getPeerId(): string {
|
|
if (typeof globalThis.localStorage === "undefined") {
|
|
return generateUUID();
|
|
}
|
|
let id = localStorage.getItem(PEER_ID_KEY);
|
|
if (!id) {
|
|
id = generateUUID();
|
|
localStorage.setItem(PEER_ID_KEY, id);
|
|
}
|
|
return id;
|
|
}
|