MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / getDeviceId

Function getDeviceId

apps/api/src/utils/ids.ts:10–59  ·  view source on GitHub ↗
({
  projectId,
  ip,
  ua,
  salts,
  overrideDeviceId,
  eventTimeMs,
}: {
  projectId: string;
  ip: string;
  ua: string | undefined;
  salts: { current: string; previous: string };
  overrideDeviceId?: string;
  /** Event timestamp (ms). Used to decide whether an existing session is
   *  still within its idle window. Defaults to `Date.now()`. */
  eventTimeMs?: number;
})

Source from the content-addressed store, hash-verified

8import type { IClickhouseSession } from '@openpanel/db';
9
10export async function getDeviceId({
11 projectId,
12 ip,
13 ua,
14 salts,
15 overrideDeviceId,
16 eventTimeMs,
17}: {
18 projectId: string;
19 ip: string;
20 ua: string | undefined;
21 salts: { current: string; previous: string };
22 overrideDeviceId?: string;
23 /** Event timestamp (ms). Used to decide whether an existing session is
24 * still within its idle window. Defaults to `Date.now()`. */
25 eventTimeMs?: number;
26}) {
27 if (overrideDeviceId) {
28 // A caller-supplied device id is stable (no salt rotation), so it's the only
29 // candidate — resolve it through the same path as internal ids.
30 return await getInfoFromSession({
31 projectId,
32 deviceIds: [overrideDeviceId],
33 eventTimeMs: eventTimeMs ?? Date.now(),
34 });
35 }
36
37 if (!ua) {
38 return { deviceId: '', sessionId: '' };
39 }
40
41 const currentDeviceId = generateDeviceId({
42 salt: salts.current,
43 origin: projectId,
44 ip,
45 ua,
46 });
47 const previousDeviceId = generateDeviceId({
48 salt: salts.previous,
49 origin: projectId,
50 ip,
51 ua,
52 });
53
54 return await getInfoFromSession({
55 projectId,
56 deviceIds: [currentDeviceId, previousDeviceId],
57 eventTimeMs: eventTimeMs ?? Date.now(),
58 });
59}
60
61interface DeviceIdResult {
62 deviceId: string;

Callers 3

buildContextFunction · 0.90
postEventFunction · 0.90
ids.test.tsFile · 0.90

Calls 2

generateDeviceIdFunction · 0.90
getInfoFromSessionFunction · 0.85

Tested by

no test coverage detected