@dash0/sdk-web
Version:
Dash0's Web SDK to collect telemetry from end-users' web browsers
19 lines (18 loc) • 698 B
JavaScript
import * as localStorage from "./local-storage";
import { generateUniqueId, SESSION_ID_BYTES } from "./id";
const SESSION_FLAGS = {
EPHEMERAL_SESSION: 0x01, // 00000001 - if the browser does NOT support local storage we count the session as ephemeral because we cannot persist it
};
function encodeSessionFlags(flags) {
let byte = 0;
if (flags.ephemeralSession) {
byte |= SESSION_FLAGS.EPHEMERAL_SESSION;
}
return byte.toString(16).padStart(2, "0");
}
export function generateSessionId() {
const sessionFlags = {
ephemeralSession: !localStorage.isSupported,
};
return `${encodeSessionFlags(sessionFlags)}${generateUniqueId(SESSION_ID_BYTES - 1)}`;
}