@dash0/sdk-web 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { generateSessionId } from "./session-id";
3
+ import * as localStorage from "./local-storage";
4
+
5
+ describe("generateSessionId", () => {
6
+ it("returns a session ID of the expected length", () => {
7
+ const sessionId = generateSessionId();
8
+ expect(sessionId).toHaveLength(16);
9
+ });
10
+
11
+ it("returns a session ID with proper flags when storage is supported", () => {
12
+ vi.spyOn(localStorage, "isSupported", "get").mockReturnValue(true);
13
+
14
+ const sessionId = generateSessionId();
15
+ console.log(sessionId);
16
+ expect(sessionId.startsWith("00")).toBe(true);
17
+ });
18
+
19
+ it("returns a session ID with proper flags when storage is NOT supported", () => {
20
+ vi.spyOn(localStorage, "isSupported", "get").mockReturnValue(false);
21
+
22
+ const sessionId = generateSessionId();
23
+ console.log(sessionId);
24
+ expect(sessionId.startsWith("01")).toBe(true);
25
+ });
26
+
27
+ it("returns a unique session ID on each call", () => {
28
+ const sessionId1 = generateSessionId();
29
+ const sessionId2 = generateSessionId();
30
+
31
+ expect(sessionId1).not.toBe(sessionId2);
32
+ });
33
+ });
@@ -0,0 +1,27 @@
1
+ import { generateUniqueId, SESSION_ID_BYTES, TRACE_ID_BYTES } from "./id";
2
+
3
+ type TraceFlags = {
4
+ withoutSession: boolean;
5
+ };
6
+
7
+ const TRACE_ID_PREFIX = "d042";
8
+
9
+ const TRACE_FLAGS = {
10
+ WITHOUT_SESSION: 0x01, // 00000001 - in some edge cases we do not have a session ID, so we need to mark the trace as such
11
+ } as const;
12
+
13
+ function encodeTraceFlags(flags: TraceFlags): string {
14
+ let byte = 0;
15
+ if (flags.withoutSession) {
16
+ byte |= TRACE_FLAGS.WITHOUT_SESSION;
17
+ }
18
+
19
+ return byte.toString(16).padStart(2, "0");
20
+ }
21
+
22
+ export function generateTraceId(sessionId: string | null): string {
23
+ if (!sessionId) {
24
+ return `${TRACE_ID_PREFIX}${encodeTraceFlags({ withoutSession: true })}${generateUniqueId(TRACE_ID_BYTES - 3)}`;
25
+ }
26
+ return `${TRACE_ID_PREFIX}${encodeTraceFlags({ withoutSession: false })}${sessionId}${generateUniqueId(TRACE_ID_BYTES - SESSION_ID_BYTES - 3)}`;
27
+ }
@@ -0,0 +1,42 @@
1
+ import { expect, describe, it } from "vitest";
2
+ import { generateTraceId } from "./trace-id";
3
+
4
+ describe("generateTraceId", () => {
5
+ it("returns a trace ID of the expected length", () => {
6
+ const traceId = generateTraceId(null);
7
+
8
+ expect(traceId).toHaveLength(32);
9
+ });
10
+
11
+ it("returns a trace ID with correct prefix when a session id is set", () => {
12
+ const traceId = generateTraceId("abcdef1234567890");
13
+
14
+ expect(traceId.substring(0, 6)).toEqual("d04200");
15
+ });
16
+
17
+ it("returns a trace ID with correct prefix when NOT session is set", () => {
18
+ const traceId = generateTraceId(null);
19
+
20
+ expect(traceId.substring(0, 6)).toEqual("d04201");
21
+ });
22
+
23
+ it("returns a unique trace ID on each call", () => {
24
+ const sessiondId = "abcdef1234567890";
25
+ const traceId1 = generateTraceId("abcdef1234567890");
26
+ const traceId2 = generateTraceId("abcdef1234567890");
27
+
28
+ expect(traceId1).not.toBe(traceId2);
29
+ expect(traceId1.substring(6, 22)).toBe(sessiondId);
30
+ expect(traceId2.substring(6, 22)).toBe(sessiondId);
31
+ });
32
+
33
+ it("returns a unique trace ID when session id is not set", () => {
34
+ const traceId1 = generateTraceId(null);
35
+ const sessionId1 = traceId1.substring(6, 22);
36
+ const traceId2 = generateTraceId(null);
37
+ const sessionId2 = traceId2.substring(6, 22);
38
+
39
+ expect(traceId1).not.toBe(traceId2);
40
+ expect(sessionId1).not.toBe(sessionId2);
41
+ });
42
+ });