browser-id
Version:
Unique ID for current browser (client).
22 lines (21 loc) • 672 B
JavaScript
import { v4 as uuid } from 'uuid';
import { Storage } from 'versioned-storage';
var STORAGE_NAME = 'browser_id';
var STORAGE_VERSION = 1;
var storage = new Storage(STORAGE_NAME, STORAGE_VERSION);
/**
* A function that returns a consistent unique identifier for the browser (or the Node/Deno environment).
* The same browser will always return the same identifier.
* @returns {string} The unique identifier.
*/
export function browserId() {
var existingID = storage.read();
if (existingID === null || existingID === undefined) {
var newID = uuid();
storage.write(newID);
return newID;
}
else {
return existingID;
}
}