@grafana/faro-web-sdk 1.2.3 → 1.2.4
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.
- package/dist/bundle/faro-web-sdk.iife.js +1 -1
- package/dist/bundle/types/index.d.ts +1 -1
- package/dist/bundle/types/instrumentations/console/instrumentation.d.ts +1 -1
- package/dist/bundle/types/instrumentations/errors/instrumentation.d.ts +1 -1
- package/dist/bundle/types/instrumentations/session/index.d.ts +1 -1
- package/dist/bundle/types/instrumentations/session/instrumentation.d.ts +2 -2
- package/dist/bundle/types/instrumentations/session/sessionManager/PersistentSessionsManager.d.ts +12 -0
- package/dist/bundle/types/instrumentations/session/sessionManager/VolatileSessionManager.d.ts +12 -0
- package/dist/bundle/types/instrumentations/session/sessionManager/index.d.ts +3 -0
- package/dist/bundle/types/instrumentations/session/sessionManager/sessionManagerUtils.d.ts +16 -0
- package/dist/bundle/types/instrumentations/session/sessionManager/types.d.ts +7 -0
- package/dist/bundle/types/instrumentations/view/instrumentation.d.ts +1 -1
- package/dist/bundle/types/instrumentations/webVitals/instrumentation.d.ts +1 -1
- package/dist/bundle/types/metas/index.d.ts +1 -0
- package/dist/bundle/types/metas/sdk/index.d.ts +1 -0
- package/dist/bundle/types/metas/sdk/meta.d.ts +2 -0
- package/dist/bundle/types/transports/console/transport.d.ts +1 -1
- package/dist/bundle/types/transports/fetch/transport.d.ts +1 -1
- package/dist/bundle/types/utils/webStorage.d.ts +30 -4
- package/dist/cjs/config/makeCoreConfig.js +22 -7
- package/dist/cjs/config/makeCoreConfig.js.map +1 -1
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/instrumentations/session/index.js +1 -1
- package/dist/cjs/instrumentations/session/index.js.map +1 -1
- package/dist/cjs/instrumentations/session/instrumentation.js +20 -9
- package/dist/cjs/instrumentations/session/instrumentation.js.map +1 -1
- package/dist/cjs/instrumentations/session/sessionManager/PersistentSessionsManager.js +66 -0
- package/dist/cjs/instrumentations/session/sessionManager/PersistentSessionsManager.js.map +1 -0
- package/dist/cjs/instrumentations/session/sessionManager/VolatileSessionManager.js +55 -0
- package/dist/cjs/instrumentations/session/sessionManager/VolatileSessionManager.js.map +1 -0
- package/dist/cjs/instrumentations/session/sessionManager/index.js +8 -0
- package/dist/cjs/instrumentations/session/sessionManager/index.js.map +1 -0
- package/dist/cjs/instrumentations/session/sessionManager/sessionManagerUtils.js +77 -0
- package/dist/cjs/instrumentations/session/sessionManager/sessionManagerUtils.js.map +1 -0
- package/dist/cjs/instrumentations/session/sessionManager/types.js +3 -0
- package/dist/cjs/instrumentations/session/sessionManager/types.js.map +1 -0
- package/dist/cjs/metas/index.js +3 -1
- package/dist/cjs/metas/index.js.map +1 -1
- package/dist/cjs/metas/sdk/index.js +6 -0
- package/dist/cjs/metas/sdk/index.js.map +1 -0
- package/dist/cjs/metas/sdk/meta.js +16 -0
- package/dist/cjs/metas/sdk/meta.js.map +1 -0
- package/dist/cjs/utils/webStorage.js +47 -11
- package/dist/cjs/utils/webStorage.js.map +1 -1
- package/dist/esm/config/makeCoreConfig.js +23 -8
- package/dist/esm/config/makeCoreConfig.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/instrumentations/session/index.js +1 -1
- package/dist/esm/instrumentations/session/index.js.map +1 -1
- package/dist/esm/instrumentations/session/instrumentation.js +20 -8
- package/dist/esm/instrumentations/session/instrumentation.js.map +1 -1
- package/dist/esm/instrumentations/session/sessionManager/PersistentSessionsManager.js +59 -0
- package/dist/esm/instrumentations/session/sessionManager/PersistentSessionsManager.js.map +1 -0
- package/dist/esm/instrumentations/session/sessionManager/VolatileSessionManager.js +48 -0
- package/dist/esm/instrumentations/session/sessionManager/VolatileSessionManager.js.map +1 -0
- package/dist/esm/instrumentations/session/sessionManager/index.js +3 -0
- package/dist/esm/instrumentations/session/sessionManager/index.js.map +1 -0
- package/dist/esm/instrumentations/session/sessionManager/sessionManagerUtils.js +58 -0
- package/dist/esm/instrumentations/session/sessionManager/sessionManagerUtils.js.map +1 -0
- package/dist/esm/instrumentations/session/sessionManager/types.js +2 -0
- package/dist/esm/instrumentations/session/sessionManager/types.js.map +1 -0
- package/dist/esm/metas/index.js +1 -0
- package/dist/esm/metas/index.js.map +1 -1
- package/dist/esm/metas/sdk/index.js +2 -0
- package/dist/esm/metas/sdk/index.js.map +1 -0
- package/dist/esm/metas/sdk/meta.js +9 -0
- package/dist/esm/metas/sdk/meta.js.map +1 -0
- package/dist/esm/utils/webStorage.js +46 -10
- package/dist/esm/utils/webStorage.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/instrumentations/console/instrumentation.d.ts +1 -1
- package/dist/types/instrumentations/errors/instrumentation.d.ts +1 -1
- package/dist/types/instrumentations/session/index.d.ts +1 -1
- package/dist/types/instrumentations/session/instrumentation.d.ts +2 -2
- package/dist/types/instrumentations/session/sessionManager/PersistentSessionsManager.d.ts +12 -0
- package/dist/types/instrumentations/session/sessionManager/VolatileSessionManager.d.ts +12 -0
- package/dist/types/instrumentations/session/sessionManager/index.d.ts +3 -0
- package/dist/types/instrumentations/session/sessionManager/sessionManagerUtils.d.ts +16 -0
- package/dist/types/instrumentations/session/sessionManager/types.d.ts +7 -0
- package/dist/types/instrumentations/view/instrumentation.d.ts +1 -1
- package/dist/types/instrumentations/webVitals/instrumentation.d.ts +1 -1
- package/dist/types/metas/index.d.ts +1 -0
- package/dist/types/metas/sdk/index.d.ts +1 -0
- package/dist/types/metas/sdk/meta.d.ts +2 -0
- package/dist/types/transports/console/transport.d.ts +1 -1
- package/dist/types/transports/fetch/transport.d.ts +1 -1
- package/dist/types/utils/webStorage.d.ts +30 -4
- package/package.json +3 -3
- package/dist/bundle/types/instrumentations/session/sessionHandler.d.ts +0 -22
- package/dist/cjs/instrumentations/session/sessionHandler.js +0 -170
- package/dist/cjs/instrumentations/session/sessionHandler.js.map +0 -1
- package/dist/esm/instrumentations/session/sessionHandler.js +0 -148
- package/dist/esm/instrumentations/session/sessionHandler.js.map +0 -1
- package/dist/types/instrumentations/session/sessionHandler.d.ts +0 -22
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BaseInstrumentation } from '@grafana/faro-core';
|
|
2
2
|
export declare class SessionInstrumentation extends BaseInstrumentation {
|
|
3
3
|
readonly name = "@grafana/faro-web-sdk:instrumentation-session";
|
|
4
|
-
readonly version = "1.2.
|
|
4
|
+
readonly version = "1.2.4";
|
|
5
5
|
private notifiedSession;
|
|
6
|
-
private sessionUpdater;
|
|
7
6
|
private sendSessionStartEvent;
|
|
7
|
+
private getSessionManagerInstanceByConfiguredStrategy;
|
|
8
8
|
initialize(): void;
|
|
9
9
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FaroUserSession } from './types';
|
|
2
|
+
export declare class PersistentSessionsManager {
|
|
3
|
+
private initialSessionId?;
|
|
4
|
+
private static storageTypeLocal;
|
|
5
|
+
private updateUserSession;
|
|
6
|
+
constructor(initialSessionId?: string | undefined);
|
|
7
|
+
static removeUserSession(): void;
|
|
8
|
+
static storeUserSession(session: FaroUserSession): void;
|
|
9
|
+
static fetchUserSession(): FaroUserSession | null;
|
|
10
|
+
updateSession: () => void;
|
|
11
|
+
private init;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FaroUserSession } from './types';
|
|
2
|
+
export declare class VolatileSessionsManager {
|
|
3
|
+
private initialSessionId?;
|
|
4
|
+
private static storageTypeSession;
|
|
5
|
+
private updateUserSession;
|
|
6
|
+
constructor(initialSessionId?: string | undefined);
|
|
7
|
+
static removeUserSession(): void;
|
|
8
|
+
static storeUserSession(session: FaroUserSession): void;
|
|
9
|
+
static fetchUserSession(): FaroUserSession | null;
|
|
10
|
+
updateSession: () => void;
|
|
11
|
+
private init;
|
|
12
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FaroUserSession } from './types';
|
|
2
|
+
export declare const STORAGE_KEY = "__FARO_SESSION__";
|
|
3
|
+
export declare const SESSION_EXPIRATION_TIME: number;
|
|
4
|
+
export declare const SESSION_INACTIVITY_TIME: number;
|
|
5
|
+
export declare const STORAGE_UPDATE_DELAY: number;
|
|
6
|
+
export declare const MAX_SESSION_PERSISTENCE_TIME_BUFFER: number;
|
|
7
|
+
export declare const MAX_SESSION_PERSISTENCE_TIME: number;
|
|
8
|
+
export declare function createUserSessionObject(sessionId?: string): FaroUserSession;
|
|
9
|
+
export declare function isUserSessionValid(session: FaroUserSession | null): boolean;
|
|
10
|
+
type GetUserSessionUpdaterParams = {
|
|
11
|
+
storeUserSession: (session: FaroUserSession) => void;
|
|
12
|
+
fetchUserSession: () => FaroUserSession | null;
|
|
13
|
+
};
|
|
14
|
+
export declare function getUserSessionUpdater({ fetchUserSession, storeUserSession }: GetUserSessionUpdaterParams): () => void;
|
|
15
|
+
export declare function addSessionMetadataToNextSession(newSession: FaroUserSession, previousSession: FaroUserSession | null): Required<FaroUserSession>;
|
|
16
|
+
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseInstrumentation } from '@grafana/faro-core';
|
|
2
2
|
export declare class ViewInstrumentation extends BaseInstrumentation {
|
|
3
3
|
readonly name = "@grafana/faro-web-sdk:instrumentation-view";
|
|
4
|
-
readonly version = "1.2.
|
|
4
|
+
readonly version = "1.2.4";
|
|
5
5
|
private notifiedView;
|
|
6
6
|
private sendViewChangedEvent;
|
|
7
7
|
initialize(): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseInstrumentation } from '@grafana/faro-core';
|
|
2
2
|
export declare class WebVitalsInstrumentation extends BaseInstrumentation {
|
|
3
3
|
readonly name = "@grafana/faro-web-sdk:instrumentation-web-vitals";
|
|
4
|
-
readonly version = "1.2.
|
|
4
|
+
readonly version = "1.2.4";
|
|
5
5
|
static mapping: {
|
|
6
6
|
cls: (onReport: import("web-vitals").CLSReportCallback, opts?: import("web-vitals").ReportOpts | undefined) => void;
|
|
7
7
|
fcp: (onReport: import("web-vitals").FCPReportCallback, opts?: import("web-vitals").ReportOpts | undefined) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { sdkMeta } from './meta';
|
|
@@ -4,7 +4,7 @@ import type { ConsoleTransportOptions } from './types';
|
|
|
4
4
|
export declare class ConsoleTransport extends BaseTransport {
|
|
5
5
|
private options;
|
|
6
6
|
readonly name = "@grafana/faro-web-sdk:transport-console";
|
|
7
|
-
readonly version = "1.2.
|
|
7
|
+
readonly version = "1.2.4";
|
|
8
8
|
constructor(options?: ConsoleTransportOptions);
|
|
9
9
|
send(item: TransportItem): void;
|
|
10
10
|
}
|
|
@@ -4,7 +4,7 @@ import type { FetchTransportOptions } from './types';
|
|
|
4
4
|
export declare class FetchTransport extends BaseTransport {
|
|
5
5
|
private options;
|
|
6
6
|
readonly name = "@grafana/faro-web-sdk:transport-fetch";
|
|
7
|
-
readonly version = "1.2.
|
|
7
|
+
readonly version = "1.2.4";
|
|
8
8
|
promiseBuffer: PromiseBuffer<Response | void>;
|
|
9
9
|
private readonly rateLimitBackoffMs;
|
|
10
10
|
private readonly getNow;
|
|
@@ -1,7 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
export declare const webStorageType: {
|
|
2
|
+
readonly session: "sessionStorage";
|
|
3
|
+
readonly local: "localStorage";
|
|
4
|
+
};
|
|
5
|
+
type StorageMechanism = (typeof webStorageType)[keyof typeof webStorageType];
|
|
6
|
+
/**
|
|
7
|
+
* Check if selected web storage mechanism is available.
|
|
8
|
+
* @param type storage mechanism to test availability for.
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
2
11
|
export declare function isWebStorageAvailable(type: StorageMechanism): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Get item from SessionStorage or LocalStorage.
|
|
14
|
+
* @param key: the item key.
|
|
15
|
+
* @param webStorageMechanism: wether the item shall be received form local storage or session storage. Defaults to local storage.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getItem(key: string, webStorageMechanism: StorageMechanism): string | null;
|
|
18
|
+
/**
|
|
19
|
+
* Store item in SessionStorage or LocalStorage.
|
|
20
|
+
* @param key: the item key.
|
|
21
|
+
* @param value: the item data.
|
|
22
|
+
* @param webStorageMechanism: wether the item shall be received form local storage or session storage. Defaults to local storage.
|
|
23
|
+
*/
|
|
24
|
+
export declare function setItem(key: string, value: string, webStorageMechanism: StorageMechanism): void;
|
|
25
|
+
/**
|
|
26
|
+
* Remove item from SessionStorage or LocalStorage.
|
|
27
|
+
* @param key: the item key.
|
|
28
|
+
* @param webStorageMechanism: wether the item shall be received form local storage or session storage. Defaults to local storage.
|
|
29
|
+
*/
|
|
30
|
+
export declare function removeItem(key: string, webStorageMechanism: StorageMechanism): void;
|
|
3
31
|
export declare const isLocalStorageAvailable: boolean;
|
|
4
|
-
export declare
|
|
5
|
-
export declare function setItem(key: string, value: string): void;
|
|
6
|
-
export declare function removeItem(key: string): void;
|
|
32
|
+
export declare const isSessionStorageAvailable: boolean;
|
|
7
33
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grafana/faro-web-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Faro instrumentations, metas, transports for web.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"observability",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"quality:circular-deps": "madge --circular ."
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@grafana/faro-core": "^1.2.
|
|
55
|
+
"@grafana/faro-core": "^1.2.4",
|
|
56
56
|
"ua-parser-js": "^1.0.32",
|
|
57
57
|
"web-vitals": "^3.1.1"
|
|
58
58
|
},
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"publishConfig": {
|
|
64
64
|
"access": "public"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "72cda534dc953ddaa37a0192ef956ac6ea505962"
|
|
67
67
|
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { MetaSession } from '@grafana/faro-core';
|
|
2
|
-
export interface FaroUserSession {
|
|
3
|
-
sessionId: string;
|
|
4
|
-
lastActivity: number;
|
|
5
|
-
started: number;
|
|
6
|
-
sessionMeta?: MetaSession;
|
|
7
|
-
}
|
|
8
|
-
export interface UserSessionUpdater {
|
|
9
|
-
handleUpdate: () => void;
|
|
10
|
-
init: () => void;
|
|
11
|
-
}
|
|
12
|
-
export declare const SESSION_EXPIRATION_TIME: number;
|
|
13
|
-
export declare const SESSION_INACTIVITY_TIME: number;
|
|
14
|
-
export declare const STORAGE_KEY = "__FARO_SESSION__";
|
|
15
|
-
export declare function createUserSessionObject(sessionId?: string): FaroUserSession;
|
|
16
|
-
export declare function removeUserSession(): void;
|
|
17
|
-
export declare function storeUserSession(session: FaroUserSession): void;
|
|
18
|
-
export declare function fetchUserSession(): FaroUserSession | null;
|
|
19
|
-
export declare function isUserSessionValid(session: FaroUserSession | null): boolean;
|
|
20
|
-
export declare function persistentUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater;
|
|
21
|
-
export declare function inMemoryUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater;
|
|
22
|
-
export declare function getSessionUpdater(initialSessionId?: string): UserSessionUpdater;
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.getSessionUpdater = exports.inMemoryUserSessionsUpdater = exports.persistentUserSessionsUpdater = exports.isUserSessionValid = exports.fetchUserSession = exports.storeUserSession = exports.removeUserSession = exports.createUserSessionObject = exports.STORAGE_KEY = exports.SESSION_INACTIVITY_TIME = exports.SESSION_EXPIRATION_TIME = void 0;
|
|
15
|
-
var faro_core_1 = require("@grafana/faro-core");
|
|
16
|
-
var utils_1 = require("../../utils");
|
|
17
|
-
var webStorage_1 = require("../../utils/webStorage");
|
|
18
|
-
// TODO: make this configurable from the outside
|
|
19
|
-
exports.SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // n hrs
|
|
20
|
-
exports.SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // n minutes
|
|
21
|
-
var STORAGE_UPDATE_DELAY = 1 * 1000; // n seconds
|
|
22
|
-
exports.STORAGE_KEY = '__FARO_SESSION__';
|
|
23
|
-
function createUserSessionObject(sessionId) {
|
|
24
|
-
var now = (0, faro_core_1.dateNow)();
|
|
25
|
-
return {
|
|
26
|
-
sessionId: sessionId !== null && sessionId !== void 0 ? sessionId : (0, faro_core_1.genShortID)(),
|
|
27
|
-
lastActivity: now,
|
|
28
|
-
started: now,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
exports.createUserSessionObject = createUserSessionObject;
|
|
32
|
-
function removeUserSession() {
|
|
33
|
-
(0, webStorage_1.removeItem)(exports.STORAGE_KEY);
|
|
34
|
-
}
|
|
35
|
-
exports.removeUserSession = removeUserSession;
|
|
36
|
-
function storeUserSession(session) {
|
|
37
|
-
(0, webStorage_1.setItem)(exports.STORAGE_KEY, JSON.stringify(session));
|
|
38
|
-
}
|
|
39
|
-
exports.storeUserSession = storeUserSession;
|
|
40
|
-
function fetchUserSession() {
|
|
41
|
-
var storedSession = (0, webStorage_1.getItem)(exports.STORAGE_KEY);
|
|
42
|
-
if (storedSession) {
|
|
43
|
-
return JSON.parse(storedSession);
|
|
44
|
-
}
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
exports.fetchUserSession = fetchUserSession;
|
|
48
|
-
function isUserSessionValid(session) {
|
|
49
|
-
if (session == null) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
var now = (0, faro_core_1.dateNow)();
|
|
53
|
-
var lifetimeValid = now - session.started < exports.SESSION_EXPIRATION_TIME;
|
|
54
|
-
if (!lifetimeValid) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
var inactivityPeriodValid = now - session.lastActivity < exports.SESSION_INACTIVITY_TIME;
|
|
58
|
-
return inactivityPeriodValid;
|
|
59
|
-
}
|
|
60
|
-
exports.isUserSessionValid = isUserSessionValid;
|
|
61
|
-
function persistentUserSessionsUpdater(initialSessionId) {
|
|
62
|
-
var throttledSessionUpdate = (0, utils_1.throttle)(function () {
|
|
63
|
-
var _a, _b, _c, _d, _e, _f;
|
|
64
|
-
var sessionFromLocalStorage = fetchUserSession();
|
|
65
|
-
if (isUserSessionValid(sessionFromLocalStorage)) {
|
|
66
|
-
storeUserSession(__assign(__assign({}, sessionFromLocalStorage), { lastActivity: (0, faro_core_1.dateNow)() }));
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
var newSession = createUserSessionObject(); // create local srorage session
|
|
70
|
-
newSession = __assign(__assign({}, newSession), { sessionMeta: {
|
|
71
|
-
id: newSession.sessionId,
|
|
72
|
-
attributes: __assign(__assign({}, ((_b = (_a = faro_core_1.faro.metas.value.session) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), (sessionFromLocalStorage != null ? { previousSession: sessionFromLocalStorage.sessionId } : {})),
|
|
73
|
-
} });
|
|
74
|
-
storeUserSession(newSession);
|
|
75
|
-
(_c = faro_core_1.faro.api) === null || _c === void 0 ? void 0 : _c.setSession(newSession.sessionMeta);
|
|
76
|
-
(_e = (_d = faro_core_1.faro.config.experimentalSessions) === null || _d === void 0 ? void 0 : _d.onSessionChange) === null || _e === void 0 ? void 0 : _e.call(_d, (_f = sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionMeta) !== null && _f !== void 0 ? _f : null, newSession.sessionMeta);
|
|
77
|
-
}
|
|
78
|
-
}, STORAGE_UPDATE_DELAY);
|
|
79
|
-
function init() {
|
|
80
|
-
storeUserSession(createUserSessionObject(initialSessionId));
|
|
81
|
-
document.addEventListener('visibilitychange', function () {
|
|
82
|
-
if (document.visibilityState === 'visible') {
|
|
83
|
-
throttledSessionUpdate();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
window.addEventListener('storage', function (event) {
|
|
87
|
-
var _a, _b, _c;
|
|
88
|
-
if (event.key !== exports.STORAGE_KEY) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
var newSession = JSON.parse((_a = event.newValue) !== null && _a !== void 0 ? _a : '');
|
|
92
|
-
var previousSession = JSON.parse((_b = event.oldValue) !== null && _b !== void 0 ? _b : '');
|
|
93
|
-
if (newSession.sessionId !== previousSession.sessionId) {
|
|
94
|
-
(_c = faro_core_1.faro.api) === null || _c === void 0 ? void 0 : _c.setSession(newSession.sessionMeta);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
// Users can call the setSession() method, so we need to sync this with the local storage session
|
|
98
|
-
faro_core_1.faro.metas.addListener(function syncSessionIfChangedExternally(meta) {
|
|
99
|
-
var _a, _b;
|
|
100
|
-
var session = meta.session;
|
|
101
|
-
var sessionFromLocalStorage = fetchUserSession();
|
|
102
|
-
if (session && session.id !== (sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionId)) {
|
|
103
|
-
var userSession = __assign(__assign({}, createUserSessionObject(session.id)), { sessionMeta: {
|
|
104
|
-
id: session.id,
|
|
105
|
-
attributes: __assign(__assign(__assign({}, ((_b = (_a = sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionMeta) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), session.attributes), (sessionFromLocalStorage ? { previousSession: sessionFromLocalStorage.sessionId } : {})),
|
|
106
|
-
} });
|
|
107
|
-
storeUserSession(userSession);
|
|
108
|
-
faro_core_1.faro.api.setSession(userSession.sessionMeta);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
return {
|
|
113
|
-
handleUpdate: throttledSessionUpdate,
|
|
114
|
-
init: init,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
exports.persistentUserSessionsUpdater = persistentUserSessionsUpdater;
|
|
118
|
-
function inMemoryUserSessionsUpdater(initialSessionId) {
|
|
119
|
-
var inMemoryUserSession = createUserSessionObject(initialSessionId);
|
|
120
|
-
function handleUpdate() {
|
|
121
|
-
var _a, _b, _c, _d, _e;
|
|
122
|
-
if (isUserSessionValid(inMemoryUserSession)) {
|
|
123
|
-
inMemoryUserSession.lastActivity = (0, faro_core_1.dateNow)();
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
inMemoryUserSession = createUserSessionObject();
|
|
127
|
-
var previousSessionMeta = faro_core_1.faro.metas.value.session;
|
|
128
|
-
var newSessionMeta = {
|
|
129
|
-
id: inMemoryUserSession.sessionId,
|
|
130
|
-
attributes: __assign(__assign({}, ((_b = (_a = faro_core_1.faro.metas.value.session) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), ((previousSessionMeta === null || previousSessionMeta === void 0 ? void 0 : previousSessionMeta.id) ? { previousSession: previousSessionMeta.id } : {})),
|
|
131
|
-
};
|
|
132
|
-
(_d = (_c = faro_core_1.faro.config.experimentalSessions) === null || _c === void 0 ? void 0 : _c.onSessionChange) === null || _d === void 0 ? void 0 : _d.call(_c, previousSessionMeta !== null && previousSessionMeta !== void 0 ? previousSessionMeta : null, newSessionMeta);
|
|
133
|
-
(_e = faro_core_1.faro.api) === null || _e === void 0 ? void 0 : _e.setSession(newSessionMeta);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
function init() {
|
|
137
|
-
document.addEventListener('visibilitychange', function () {
|
|
138
|
-
if (document.visibilityState === 'visible') {
|
|
139
|
-
handleUpdate();
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
// Users can call the setSession() method, so we need to sync this with the inMemoryUserSession
|
|
143
|
-
faro_core_1.faro.metas.addListener(function syncSessionIfChangedExternally(meta) {
|
|
144
|
-
var _a;
|
|
145
|
-
var session = meta.session;
|
|
146
|
-
if (session && session.id !== (inMemoryUserSession === null || inMemoryUserSession === void 0 ? void 0 : inMemoryUserSession.sessionId)) {
|
|
147
|
-
var previousSessionId = inMemoryUserSession.sessionId;
|
|
148
|
-
inMemoryUserSession = createUserSessionObject(session.id);
|
|
149
|
-
faro_core_1.faro.api.setSession({
|
|
150
|
-
id: inMemoryUserSession.sessionId,
|
|
151
|
-
attributes: __assign(__assign({}, ((_a = session.attributes) !== null && _a !== void 0 ? _a : {})), { previousSession: previousSessionId }),
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
return {
|
|
157
|
-
handleUpdate: handleUpdate,
|
|
158
|
-
init: init,
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
exports.inMemoryUserSessionsUpdater = inMemoryUserSessionsUpdater;
|
|
162
|
-
function getSessionUpdater(initialSessionId) {
|
|
163
|
-
var _a;
|
|
164
|
-
if (((_a = faro_core_1.faro.config.experimentalSessions) === null || _a === void 0 ? void 0 : _a.persistent) && webStorage_1.isLocalStorageAvailable) {
|
|
165
|
-
return persistentUserSessionsUpdater(initialSessionId);
|
|
166
|
-
}
|
|
167
|
-
return inMemoryUserSessionsUpdater(initialSessionId);
|
|
168
|
-
}
|
|
169
|
-
exports.getSessionUpdater = getSessionUpdater;
|
|
170
|
-
//# sourceMappingURL=sessionHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sessionHandler.js","s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QAC3E,OAAO,6BAA6B,CAAC,gBAAgB,CAAC,CAAC;KACxD;IAED,OAAO,2BAA2B,CAAC,gBAAgB,CAAC,CAAC;AACvD,CAAC;AAND,8CAMC","sourcesContent":["import { dateNow, faro, genShortID, Meta, MetaSession } from '@grafana/faro-core';\n\nimport { throttle } from '../../utils';\nimport { getItem, isLocalStorageAvailable, removeItem, setItem } from '../../utils/webStorage';\n\nexport interface FaroUserSession {\n sessionId: string;\n lastActivity: number;\n started: number;\n sessionMeta?: MetaSession;\n}\nexport interface UserSessionUpdater {\n handleUpdate: () => void;\n init: () => void;\n}\n\n// TODO: make this configurable from the outside\nexport const SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // n hrs\nexport const SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // n minutes\nconst STORAGE_UPDATE_DELAY = 1 * 1000; // n seconds\n\nexport const STORAGE_KEY = '__FARO_SESSION__';\n\nexport function createUserSessionObject(sessionId?: string): FaroUserSession {\n const now = dateNow();\n\n return {\n sessionId: sessionId ?? genShortID(),\n lastActivity: now,\n started: now,\n };\n}\n\nexport function removeUserSession() {\n removeItem(STORAGE_KEY);\n}\n\nexport function storeUserSession(session: FaroUserSession): void {\n setItem(STORAGE_KEY, JSON.stringify(session));\n}\n\nexport function fetchUserSession(): FaroUserSession | null {\n const storedSession = getItem(STORAGE_KEY);\n\n if (storedSession) {\n return JSON.parse(storedSession) as FaroUserSession;\n }\n\n return null;\n}\n\nexport function isUserSessionValid(session: FaroUserSession | null): boolean {\n if (session == null) {\n return false;\n }\n\n const now = dateNow();\n const lifetimeValid = now - session.started < SESSION_EXPIRATION_TIME;\n\n if (!lifetimeValid) {\n return false;\n }\n\n const inactivityPeriodValid = now - session.lastActivity < SESSION_INACTIVITY_TIME;\n return inactivityPeriodValid;\n}\n\nexport function persistentUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater {\n const throttledSessionUpdate = throttle(() => {\n const sessionFromLocalStorage = fetchUserSession();\n\n if (isUserSessionValid(sessionFromLocalStorage)) {\n storeUserSession({ ...sessionFromLocalStorage!, lastActivity: dateNow() });\n } else {\n let newSession = createUserSessionObject(); // create local srorage session\n newSession = {\n ...newSession,\n sessionMeta: {\n id: newSession.sessionId,\n attributes: {\n ...(faro.metas.value.session?.attributes ?? {}),\n ...(sessionFromLocalStorage != null ? { previousSession: sessionFromLocalStorage.sessionId } : {}),\n },\n },\n };\n\n storeUserSession(newSession);\n\n faro.api?.setSession(newSession.sessionMeta);\n faro.config.experimentalSessions?.onSessionChange?.(\n sessionFromLocalStorage?.sessionMeta ?? null,\n newSession.sessionMeta!\n );\n }\n }, STORAGE_UPDATE_DELAY);\n\n function init() {\n storeUserSession(createUserSessionObject(initialSessionId));\n\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n throttledSessionUpdate();\n }\n });\n\n window.addEventListener('storage', (event: StorageEvent) => {\n if (event.key !== STORAGE_KEY) {\n return;\n }\n\n const newSession: FaroUserSession = JSON.parse(event.newValue ?? '');\n const previousSession: FaroUserSession = JSON.parse(event.oldValue ?? '');\n\n if (newSession.sessionId !== previousSession.sessionId) {\n faro.api?.setSession(newSession.sessionMeta);\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the local storage session\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n const sessionFromLocalStorage = fetchUserSession();\n\n if (session && session.id !== sessionFromLocalStorage?.sessionId) {\n const userSession = {\n ...createUserSessionObject(session.id),\n sessionMeta: {\n id: session.id,\n attributes: {\n ...(sessionFromLocalStorage?.sessionMeta?.attributes ?? {}),\n ...session.attributes,\n ...(sessionFromLocalStorage ? { previousSession: sessionFromLocalStorage.sessionId } : {}),\n },\n },\n };\n\n storeUserSession(userSession);\n faro.api.setSession(userSession.sessionMeta);\n }\n });\n }\n\n return {\n handleUpdate: throttledSessionUpdate,\n init,\n };\n}\n\nexport function inMemoryUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater {\n let inMemoryUserSession: FaroUserSession = createUserSessionObject(initialSessionId);\n\n function handleUpdate() {\n if (isUserSessionValid(inMemoryUserSession)) {\n inMemoryUserSession.lastActivity = dateNow();\n } else {\n inMemoryUserSession = createUserSessionObject();\n\n const previousSessionMeta = faro.metas.value.session;\n const newSessionMeta = {\n id: inMemoryUserSession.sessionId,\n attributes: {\n ...(faro.metas.value.session?.attributes ?? {}),\n ...(previousSessionMeta?.id ? { previousSession: previousSessionMeta.id } : {}),\n },\n };\n\n faro.config.experimentalSessions?.onSessionChange?.(previousSessionMeta ?? null, newSessionMeta);\n\n faro.api?.setSession(newSessionMeta);\n }\n }\n\n function init() {\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n handleUpdate();\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the inMemoryUserSession\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n\n if (session && session.id !== inMemoryUserSession?.sessionId) {\n const previousSessionId = inMemoryUserSession.sessionId;\n inMemoryUserSession = createUserSessionObject(session.id);\n\n faro.api.setSession({\n id: inMemoryUserSession.sessionId,\n attributes: {\n ...(session.attributes ?? {}),\n previousSession: previousSessionId,\n },\n });\n }\n });\n }\n\n return {\n handleUpdate,\n init,\n };\n}\n\nexport function getSessionUpdater(initialSessionId?: string): UserSessionUpdater {\n if (faro.config.experimentalSessions?.persistent && isLocalStorageAvailable) {\n return persistentUserSessionsUpdater(initialSessionId);\n }\n\n return inMemoryUserSessionsUpdater(initialSessionId);\n}\n"]}
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import { dateNow, faro, genShortID } from '@grafana/faro-core';
|
|
2
|
-
import { throttle } from '../../utils';
|
|
3
|
-
import { getItem, isLocalStorageAvailable, removeItem, setItem } from '../../utils/webStorage';
|
|
4
|
-
// TODO: make this configurable from the outside
|
|
5
|
-
export const SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // n hrs
|
|
6
|
-
export const SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // n minutes
|
|
7
|
-
const STORAGE_UPDATE_DELAY = 1 * 1000; // n seconds
|
|
8
|
-
export const STORAGE_KEY = '__FARO_SESSION__';
|
|
9
|
-
export function createUserSessionObject(sessionId) {
|
|
10
|
-
const now = dateNow();
|
|
11
|
-
return {
|
|
12
|
-
sessionId: sessionId !== null && sessionId !== void 0 ? sessionId : genShortID(),
|
|
13
|
-
lastActivity: now,
|
|
14
|
-
started: now,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export function removeUserSession() {
|
|
18
|
-
removeItem(STORAGE_KEY);
|
|
19
|
-
}
|
|
20
|
-
export function storeUserSession(session) {
|
|
21
|
-
setItem(STORAGE_KEY, JSON.stringify(session));
|
|
22
|
-
}
|
|
23
|
-
export function fetchUserSession() {
|
|
24
|
-
const storedSession = getItem(STORAGE_KEY);
|
|
25
|
-
if (storedSession) {
|
|
26
|
-
return JSON.parse(storedSession);
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
export function isUserSessionValid(session) {
|
|
31
|
-
if (session == null) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
const now = dateNow();
|
|
35
|
-
const lifetimeValid = now - session.started < SESSION_EXPIRATION_TIME;
|
|
36
|
-
if (!lifetimeValid) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
const inactivityPeriodValid = now - session.lastActivity < SESSION_INACTIVITY_TIME;
|
|
40
|
-
return inactivityPeriodValid;
|
|
41
|
-
}
|
|
42
|
-
export function persistentUserSessionsUpdater(initialSessionId) {
|
|
43
|
-
const throttledSessionUpdate = throttle(() => {
|
|
44
|
-
var _a, _b, _c, _d, _e, _f;
|
|
45
|
-
const sessionFromLocalStorage = fetchUserSession();
|
|
46
|
-
if (isUserSessionValid(sessionFromLocalStorage)) {
|
|
47
|
-
storeUserSession(Object.assign(Object.assign({}, sessionFromLocalStorage), { lastActivity: dateNow() }));
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
let newSession = createUserSessionObject(); // create local srorage session
|
|
51
|
-
newSession = Object.assign(Object.assign({}, newSession), { sessionMeta: {
|
|
52
|
-
id: newSession.sessionId,
|
|
53
|
-
attributes: Object.assign(Object.assign({}, ((_b = (_a = faro.metas.value.session) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), (sessionFromLocalStorage != null ? { previousSession: sessionFromLocalStorage.sessionId } : {})),
|
|
54
|
-
} });
|
|
55
|
-
storeUserSession(newSession);
|
|
56
|
-
(_c = faro.api) === null || _c === void 0 ? void 0 : _c.setSession(newSession.sessionMeta);
|
|
57
|
-
(_e = (_d = faro.config.experimentalSessions) === null || _d === void 0 ? void 0 : _d.onSessionChange) === null || _e === void 0 ? void 0 : _e.call(_d, (_f = sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionMeta) !== null && _f !== void 0 ? _f : null, newSession.sessionMeta);
|
|
58
|
-
}
|
|
59
|
-
}, STORAGE_UPDATE_DELAY);
|
|
60
|
-
function init() {
|
|
61
|
-
storeUserSession(createUserSessionObject(initialSessionId));
|
|
62
|
-
document.addEventListener('visibilitychange', () => {
|
|
63
|
-
if (document.visibilityState === 'visible') {
|
|
64
|
-
throttledSessionUpdate();
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
window.addEventListener('storage', (event) => {
|
|
68
|
-
var _a, _b, _c;
|
|
69
|
-
if (event.key !== STORAGE_KEY) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const newSession = JSON.parse((_a = event.newValue) !== null && _a !== void 0 ? _a : '');
|
|
73
|
-
const previousSession = JSON.parse((_b = event.oldValue) !== null && _b !== void 0 ? _b : '');
|
|
74
|
-
if (newSession.sessionId !== previousSession.sessionId) {
|
|
75
|
-
(_c = faro.api) === null || _c === void 0 ? void 0 : _c.setSession(newSession.sessionMeta);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
// Users can call the setSession() method, so we need to sync this with the local storage session
|
|
79
|
-
faro.metas.addListener(function syncSessionIfChangedExternally(meta) {
|
|
80
|
-
var _a, _b;
|
|
81
|
-
const session = meta.session;
|
|
82
|
-
const sessionFromLocalStorage = fetchUserSession();
|
|
83
|
-
if (session && session.id !== (sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionId)) {
|
|
84
|
-
const userSession = Object.assign(Object.assign({}, createUserSessionObject(session.id)), { sessionMeta: {
|
|
85
|
-
id: session.id,
|
|
86
|
-
attributes: Object.assign(Object.assign(Object.assign({}, ((_b = (_a = sessionFromLocalStorage === null || sessionFromLocalStorage === void 0 ? void 0 : sessionFromLocalStorage.sessionMeta) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), session.attributes), (sessionFromLocalStorage ? { previousSession: sessionFromLocalStorage.sessionId } : {})),
|
|
87
|
-
} });
|
|
88
|
-
storeUserSession(userSession);
|
|
89
|
-
faro.api.setSession(userSession.sessionMeta);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
handleUpdate: throttledSessionUpdate,
|
|
95
|
-
init,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
export function inMemoryUserSessionsUpdater(initialSessionId) {
|
|
99
|
-
let inMemoryUserSession = createUserSessionObject(initialSessionId);
|
|
100
|
-
function handleUpdate() {
|
|
101
|
-
var _a, _b, _c, _d, _e;
|
|
102
|
-
if (isUserSessionValid(inMemoryUserSession)) {
|
|
103
|
-
inMemoryUserSession.lastActivity = dateNow();
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
inMemoryUserSession = createUserSessionObject();
|
|
107
|
-
const previousSessionMeta = faro.metas.value.session;
|
|
108
|
-
const newSessionMeta = {
|
|
109
|
-
id: inMemoryUserSession.sessionId,
|
|
110
|
-
attributes: Object.assign(Object.assign({}, ((_b = (_a = faro.metas.value.session) === null || _a === void 0 ? void 0 : _a.attributes) !== null && _b !== void 0 ? _b : {})), ((previousSessionMeta === null || previousSessionMeta === void 0 ? void 0 : previousSessionMeta.id) ? { previousSession: previousSessionMeta.id } : {})),
|
|
111
|
-
};
|
|
112
|
-
(_d = (_c = faro.config.experimentalSessions) === null || _c === void 0 ? void 0 : _c.onSessionChange) === null || _d === void 0 ? void 0 : _d.call(_c, previousSessionMeta !== null && previousSessionMeta !== void 0 ? previousSessionMeta : null, newSessionMeta);
|
|
113
|
-
(_e = faro.api) === null || _e === void 0 ? void 0 : _e.setSession(newSessionMeta);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function init() {
|
|
117
|
-
document.addEventListener('visibilitychange', () => {
|
|
118
|
-
if (document.visibilityState === 'visible') {
|
|
119
|
-
handleUpdate();
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
// Users can call the setSession() method, so we need to sync this with the inMemoryUserSession
|
|
123
|
-
faro.metas.addListener(function syncSessionIfChangedExternally(meta) {
|
|
124
|
-
var _a;
|
|
125
|
-
const session = meta.session;
|
|
126
|
-
if (session && session.id !== (inMemoryUserSession === null || inMemoryUserSession === void 0 ? void 0 : inMemoryUserSession.sessionId)) {
|
|
127
|
-
const previousSessionId = inMemoryUserSession.sessionId;
|
|
128
|
-
inMemoryUserSession = createUserSessionObject(session.id);
|
|
129
|
-
faro.api.setSession({
|
|
130
|
-
id: inMemoryUserSession.sessionId,
|
|
131
|
-
attributes: Object.assign(Object.assign({}, ((_a = session.attributes) !== null && _a !== void 0 ? _a : {})), { previousSession: previousSessionId }),
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
return {
|
|
137
|
-
handleUpdate,
|
|
138
|
-
init,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
export function getSessionUpdater(initialSessionId) {
|
|
142
|
-
var _a;
|
|
143
|
-
if (((_a = faro.config.experimentalSessions) === null || _a === void 0 ? void 0 : _a.persistent) && isLocalStorageAvailable) {
|
|
144
|
-
return persistentUserSessionsUpdater(initialSessionId);
|
|
145
|
-
}
|
|
146
|
-
return inMemoryUserSessionsUpdater(initialSessionId);
|
|
147
|
-
}
|
|
148
|
-
//# sourceMappingURL=sessionHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sessionHandler.js","sourceRoot":"","sources":["../../../../src/instrumentations/session/sessionHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAqB,MAAM,oBAAoB,CAAC;AAElF,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAa/F,gDAAgD;AAChD,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,QAAQ;AACnE,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,YAAY;AACnE,MAAM,oBAAoB,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,YAAY;AAEnD,MAAM,CAAC,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAE9C,MAAM,UAAU,uBAAuB,CAAC,SAAkB;IACxD,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;IAEtB,OAAO;QACL,SAAS,EAAE,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,UAAU,EAAE;QACpC,YAAY,EAAE,GAAG;QACjB,OAAO,EAAE,GAAG;KACb,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB;IAC/B,UAAU,CAAC,WAAW,CAAC,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,OAAwB;IACvD,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC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MAAA,OAAO,CAAC,UAAU,mCAAI,EAAE,CAAC,KAC7B,eAAe,EAAE,iBAAiB,GACnC;iBACF,CAAC,CAAC;aACJ;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO;QACL,YAAY;QACZ,IAAI;KACL,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,gBAAyB;;IACzD,IAAI,CAAA,MAAA,IAAI,CAAC,MAAM,CAAC,oBAAoB,0CAAE,UAAU,KAAI,uBAAuB,EAAE;QAC3E,OAAO,6BAA6B,CAAC,gBAAgB,CAAC,CAAC;KACxD;IAED,OAAO,2BAA2B,CAAC,gBAAgB,CAAC,CAAC;AACvD,CAAC","sourcesContent":["import { dateNow, faro, genShortID, Meta, MetaSession } from '@grafana/faro-core';\n\nimport { throttle } from '../../utils';\nimport { getItem, isLocalStorageAvailable, removeItem, setItem } from '../../utils/webStorage';\n\nexport interface FaroUserSession {\n sessionId: string;\n lastActivity: number;\n started: number;\n sessionMeta?: MetaSession;\n}\nexport interface UserSessionUpdater {\n handleUpdate: () => void;\n init: () => void;\n}\n\n// TODO: make this configurable from the outside\nexport const SESSION_EXPIRATION_TIME = 4 * 60 * 60 * 1000; // n hrs\nexport const SESSION_INACTIVITY_TIME = 15 * 60 * 1000; // n minutes\nconst STORAGE_UPDATE_DELAY = 1 * 1000; // n seconds\n\nexport const STORAGE_KEY = '__FARO_SESSION__';\n\nexport function createUserSessionObject(sessionId?: string): FaroUserSession {\n const now = dateNow();\n\n return {\n sessionId: sessionId ?? genShortID(),\n lastActivity: now,\n started: now,\n };\n}\n\nexport function removeUserSession() {\n removeItem(STORAGE_KEY);\n}\n\nexport function storeUserSession(session: FaroUserSession): void {\n setItem(STORAGE_KEY, JSON.stringify(session));\n}\n\nexport function fetchUserSession(): FaroUserSession | null {\n const storedSession = getItem(STORAGE_KEY);\n\n if (storedSession) {\n return JSON.parse(storedSession) as FaroUserSession;\n }\n\n return null;\n}\n\nexport function isUserSessionValid(session: FaroUserSession | null): boolean {\n if (session == null) {\n return false;\n }\n\n const now = dateNow();\n const lifetimeValid = now - session.started < SESSION_EXPIRATION_TIME;\n\n if (!lifetimeValid) {\n return false;\n }\n\n const inactivityPeriodValid = now - session.lastActivity < SESSION_INACTIVITY_TIME;\n return inactivityPeriodValid;\n}\n\nexport function persistentUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater {\n const throttledSessionUpdate = throttle(() => {\n const sessionFromLocalStorage = fetchUserSession();\n\n if (isUserSessionValid(sessionFromLocalStorage)) {\n storeUserSession({ ...sessionFromLocalStorage!, lastActivity: dateNow() });\n } else {\n let newSession = createUserSessionObject(); // create local srorage session\n newSession = {\n ...newSession,\n sessionMeta: {\n id: newSession.sessionId,\n attributes: {\n ...(faro.metas.value.session?.attributes ?? {}),\n ...(sessionFromLocalStorage != null ? { previousSession: sessionFromLocalStorage.sessionId } : {}),\n },\n },\n };\n\n storeUserSession(newSession);\n\n faro.api?.setSession(newSession.sessionMeta);\n faro.config.experimentalSessions?.onSessionChange?.(\n sessionFromLocalStorage?.sessionMeta ?? null,\n newSession.sessionMeta!\n );\n }\n }, STORAGE_UPDATE_DELAY);\n\n function init() {\n storeUserSession(createUserSessionObject(initialSessionId));\n\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n throttledSessionUpdate();\n }\n });\n\n window.addEventListener('storage', (event: StorageEvent) => {\n if (event.key !== STORAGE_KEY) {\n return;\n }\n\n const newSession: FaroUserSession = JSON.parse(event.newValue ?? '');\n const previousSession: FaroUserSession = JSON.parse(event.oldValue ?? '');\n\n if (newSession.sessionId !== previousSession.sessionId) {\n faro.api?.setSession(newSession.sessionMeta);\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the local storage session\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n const sessionFromLocalStorage = fetchUserSession();\n\n if (session && session.id !== sessionFromLocalStorage?.sessionId) {\n const userSession = {\n ...createUserSessionObject(session.id),\n sessionMeta: {\n id: session.id,\n attributes: {\n ...(sessionFromLocalStorage?.sessionMeta?.attributes ?? {}),\n ...session.attributes,\n ...(sessionFromLocalStorage ? { previousSession: sessionFromLocalStorage.sessionId } : {}),\n },\n },\n };\n\n storeUserSession(userSession);\n faro.api.setSession(userSession.sessionMeta);\n }\n });\n }\n\n return {\n handleUpdate: throttledSessionUpdate,\n init,\n };\n}\n\nexport function inMemoryUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater {\n let inMemoryUserSession: FaroUserSession = createUserSessionObject(initialSessionId);\n\n function handleUpdate() {\n if (isUserSessionValid(inMemoryUserSession)) {\n inMemoryUserSession.lastActivity = dateNow();\n } else {\n inMemoryUserSession = createUserSessionObject();\n\n const previousSessionMeta = faro.metas.value.session;\n const newSessionMeta = {\n id: inMemoryUserSession.sessionId,\n attributes: {\n ...(faro.metas.value.session?.attributes ?? {}),\n ...(previousSessionMeta?.id ? { previousSession: previousSessionMeta.id } : {}),\n },\n };\n\n faro.config.experimentalSessions?.onSessionChange?.(previousSessionMeta ?? null, newSessionMeta);\n\n faro.api?.setSession(newSessionMeta);\n }\n }\n\n function init() {\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'visible') {\n handleUpdate();\n }\n });\n\n // Users can call the setSession() method, so we need to sync this with the inMemoryUserSession\n faro.metas.addListener(function syncSessionIfChangedExternally(meta: Meta) {\n const session = meta.session;\n\n if (session && session.id !== inMemoryUserSession?.sessionId) {\n const previousSessionId = inMemoryUserSession.sessionId;\n inMemoryUserSession = createUserSessionObject(session.id);\n\n faro.api.setSession({\n id: inMemoryUserSession.sessionId,\n attributes: {\n ...(session.attributes ?? {}),\n previousSession: previousSessionId,\n },\n });\n }\n });\n }\n\n return {\n handleUpdate,\n init,\n };\n}\n\nexport function getSessionUpdater(initialSessionId?: string): UserSessionUpdater {\n if (faro.config.experimentalSessions?.persistent && isLocalStorageAvailable) {\n return persistentUserSessionsUpdater(initialSessionId);\n }\n\n return inMemoryUserSessionsUpdater(initialSessionId);\n}\n"]}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { MetaSession } from '@grafana/faro-core';
|
|
2
|
-
export interface FaroUserSession {
|
|
3
|
-
sessionId: string;
|
|
4
|
-
lastActivity: number;
|
|
5
|
-
started: number;
|
|
6
|
-
sessionMeta?: MetaSession;
|
|
7
|
-
}
|
|
8
|
-
export interface UserSessionUpdater {
|
|
9
|
-
handleUpdate: () => void;
|
|
10
|
-
init: () => void;
|
|
11
|
-
}
|
|
12
|
-
export declare const SESSION_EXPIRATION_TIME: number;
|
|
13
|
-
export declare const SESSION_INACTIVITY_TIME: number;
|
|
14
|
-
export declare const STORAGE_KEY = "__FARO_SESSION__";
|
|
15
|
-
export declare function createUserSessionObject(sessionId?: string): FaroUserSession;
|
|
16
|
-
export declare function removeUserSession(): void;
|
|
17
|
-
export declare function storeUserSession(session: FaroUserSession): void;
|
|
18
|
-
export declare function fetchUserSession(): FaroUserSession | null;
|
|
19
|
-
export declare function isUserSessionValid(session: FaroUserSession | null): boolean;
|
|
20
|
-
export declare function persistentUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater;
|
|
21
|
-
export declare function inMemoryUserSessionsUpdater(initialSessionId?: string): UserSessionUpdater;
|
|
22
|
-
export declare function getSessionUpdater(initialSessionId?: string): UserSessionUpdater;
|