posthog-js 1.76.0 → 1.77.1
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/array.full.js +2 -2
- package/dist/array.full.js.map +1 -1
- package/dist/array.js +2 -2
- package/dist/array.js.map +1 -1
- package/dist/es.js +2 -2
- package/dist/es.js.map +1 -1
- package/dist/module.d.ts +38 -6
- package/dist/module.js +2 -2
- package/dist/module.js.map +1 -1
- package/lib/package.json +8 -6
- package/lib/src/autocapture-utils.d.ts +15 -0
- package/lib/src/autocapture-utils.js +303 -0
- package/lib/src/autocapture-utils.js.map +1 -0
- package/lib/src/autocapture.d.ts +27 -0
- package/lib/src/autocapture.js +290 -0
- package/lib/src/autocapture.js.map +1 -0
- package/lib/src/base-request-queue.d.ts +12 -0
- package/lib/src/base-request-queue.js +33 -0
- package/lib/src/base-request-queue.js.map +1 -0
- package/lib/src/compression.d.ts +3 -0
- package/lib/src/compression.js +35 -0
- package/lib/src/compression.js.map +1 -0
- package/lib/src/config.d.ts +5 -0
- package/lib/src/config.js +9 -0
- package/lib/src/config.js.map +1 -0
- package/lib/src/constants.d.ts +19 -0
- package/lib/src/constants.js +41 -0
- package/lib/src/constants.js.map +1 -0
- package/lib/src/decide.d.ts +8 -0
- package/lib/src/decide.js +118 -0
- package/lib/src/decide.js.map +1 -0
- package/lib/src/extensions/cloud.d.ts +1 -0
- package/lib/src/extensions/cloud.js +2 -0
- package/lib/src/extensions/cloud.js.map +1 -0
- package/lib/src/extensions/exceptions/error-conversion.d.ts +26 -0
- package/lib/src/extensions/exceptions/error-conversion.js +204 -0
- package/lib/src/extensions/exceptions/error-conversion.js.map +1 -0
- package/lib/src/extensions/exceptions/exception-autocapture.d.ts +24 -0
- package/lib/src/extensions/exceptions/exception-autocapture.js +164 -0
- package/lib/src/extensions/exceptions/exception-autocapture.js.map +1 -0
- package/lib/src/extensions/exceptions/stack-trace.d.ts +31 -0
- package/lib/src/extensions/exceptions/stack-trace.js +259 -0
- package/lib/src/extensions/exceptions/stack-trace.js.map +1 -0
- package/lib/src/extensions/exceptions/type-checking.d.ts +10 -0
- package/lib/src/extensions/exceptions/type-checking.js +43 -0
- package/lib/src/extensions/exceptions/type-checking.js.map +1 -0
- package/lib/src/extensions/rageclick.d.ts +10 -0
- package/lib/src/extensions/rageclick.js +33 -0
- package/lib/src/extensions/rageclick.js.map +1 -0
- package/lib/src/extensions/segment-integration.d.ts +44 -0
- package/lib/src/extensions/segment-integration.js +34 -0
- package/lib/src/extensions/segment-integration.js.map +1 -0
- package/lib/src/extensions/sentry-integration.d.ts +30 -0
- package/lib/src/extensions/sentry-integration.js +63 -0
- package/lib/src/extensions/sentry-integration.js.map +1 -0
- package/lib/src/extensions/sessionrecording-utils.d.ts +67 -0
- package/lib/src/extensions/sessionrecording-utils.js +192 -0
- package/lib/src/extensions/sessionrecording-utils.js.map +1 -0
- package/lib/src/extensions/sessionrecording.d.ts +45 -0
- package/lib/src/extensions/sessionrecording.js +430 -0
- package/lib/src/extensions/sessionrecording.js.map +1 -0
- package/lib/src/extensions/toolbar.d.ts +18 -0
- package/lib/src/extensions/toolbar.js +151 -0
- package/lib/src/extensions/toolbar.js.map +1 -0
- package/lib/src/extensions/web-performance.d.ts +20 -0
- package/lib/src/extensions/web-performance.js +245 -0
- package/lib/src/extensions/web-performance.js.map +1 -0
- package/lib/src/gdpr-utils.d.ts +80 -0
- package/lib/src/gdpr-utils.js +236 -0
- package/lib/src/gdpr-utils.js.map +1 -0
- package/lib/src/loader-globals-full.d.ts +1 -0
- package/lib/src/loader-globals-full.js +5 -0
- package/lib/src/loader-globals-full.js.map +1 -0
- package/lib/src/loader-globals.d.ts +1 -0
- package/lib/src/loader-globals.js +3 -0
- package/lib/src/loader-globals.js.map +1 -0
- package/lib/src/loader-module.d.ts +4 -0
- package/lib/src/loader-module.js +6 -0
- package/lib/src/loader-module.js.map +1 -0
- package/lib/src/loader-recorder-v2.d.ts +2 -0
- package/lib/src/loader-recorder-v2.js +15 -0
- package/lib/src/loader-recorder-v2.js.map +1 -0
- package/lib/src/loader-recorder.d.ts +2 -0
- package/lib/src/loader-recorder.js +15 -0
- package/lib/src/loader-recorder.js.map +1 -0
- package/lib/src/page-view.d.ts +38 -0
- package/lib/src/page-view.js +127 -0
- package/lib/src/page-view.js.map +1 -0
- package/lib/src/posthog-core.d.ts +701 -0
- package/lib/src/posthog-core.js +1916 -0
- package/lib/src/posthog-core.js.map +1 -0
- package/lib/src/posthog-featureflags.d.ts +70 -0
- package/lib/src/posthog-featureflags.js +438 -0
- package/lib/src/posthog-featureflags.js.map +1 -0
- package/lib/src/posthog-persistence.d.ts +57 -0
- package/lib/src/posthog-persistence.js +256 -0
- package/lib/src/posthog-persistence.js.map +1 -0
- package/lib/src/posthog-surveys.d.ts +57 -0
- package/lib/src/posthog-surveys.js +76 -0
- package/lib/src/posthog-surveys.js.map +1 -0
- package/lib/src/rate-limiter.d.ts +5 -0
- package/lib/src/rate-limiter.js +66 -0
- package/lib/src/rate-limiter.js.map +1 -0
- package/lib/src/request-queue.d.ts +10 -0
- package/lib/src/request-queue.js +146 -0
- package/lib/src/request-queue.js.map +1 -0
- package/lib/src/retry-queue.d.ts +28 -0
- package/lib/src/retry-queue.js +198 -0
- package/lib/src/retry-queue.js.map +1 -0
- package/lib/src/send-request.d.ts +6 -0
- package/lib/src/send-request.js +125 -0
- package/lib/src/send-request.js.map +1 -0
- package/lib/src/sessionid.d.ts +28 -0
- package/lib/src/sessionid.js +205 -0
- package/lib/src/sessionid.js.map +1 -0
- package/lib/src/storage.d.ts +7 -0
- package/lib/src/storage.js +291 -0
- package/lib/src/storage.js.map +1 -0
- package/lib/src/types.d.ts +294 -0
- package/lib/src/types.js +6 -0
- package/lib/src/types.js.map +1 -0
- package/lib/src/utils.d.ts +89 -0
- package/lib/src/utils.js +868 -0
- package/lib/src/utils.js.map +1 -0
- package/lib/src/uuidv7.d.ts +42 -0
- package/lib/src/uuidv7.js +228 -0
- package/lib/src/uuidv7.js.map +1 -0
- package/package.json +8 -6
- package/CHANGELOG.md +0 -1145
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
+
if (!m) return o;
|
|
4
|
+
var i = m.call(o), r, ar = [], e;
|
|
5
|
+
try {
|
|
6
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
+
}
|
|
8
|
+
catch (error) { e = { error: error }; }
|
|
9
|
+
finally {
|
|
10
|
+
try {
|
|
11
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
+
}
|
|
13
|
+
finally { if (e) throw e.error; }
|
|
14
|
+
}
|
|
15
|
+
return ar;
|
|
16
|
+
};
|
|
17
|
+
import { SESSION_ID } from './constants';
|
|
18
|
+
import { sessionStore } from './storage';
|
|
19
|
+
import { uuidv7 } from './uuidv7';
|
|
20
|
+
var MAX_SESSION_IDLE_TIMEOUT = 30 * 60; // 30 mins
|
|
21
|
+
var MIN_SESSION_IDLE_TIMEOUT = 60; // 1 mins
|
|
22
|
+
var SESSION_LENGTH_LIMIT = 24 * 3600 * 1000; // 24 hours
|
|
23
|
+
var SessionIdManager = /** @class */ (function () {
|
|
24
|
+
function SessionIdManager(config, persistence) {
|
|
25
|
+
this._sessionIdChangedHandlers = [];
|
|
26
|
+
this.config = config;
|
|
27
|
+
this.persistence = persistence;
|
|
28
|
+
this._windowId = undefined;
|
|
29
|
+
this._sessionId = undefined;
|
|
30
|
+
this._sessionStartTimestamp = null;
|
|
31
|
+
this._sessionActivityTimestamp = null;
|
|
32
|
+
var persistenceName = config['persistence_name'] || config['token'];
|
|
33
|
+
var desiredTimeout = config['session_idle_timeout_seconds'] || MAX_SESSION_IDLE_TIMEOUT;
|
|
34
|
+
if (typeof desiredTimeout !== 'number') {
|
|
35
|
+
console.warn('[PostHog] session_idle_timeout_seconds must be a number. Defaulting to 30 minutes.');
|
|
36
|
+
desiredTimeout = MAX_SESSION_IDLE_TIMEOUT;
|
|
37
|
+
}
|
|
38
|
+
else if (desiredTimeout > MAX_SESSION_IDLE_TIMEOUT) {
|
|
39
|
+
console.warn('[PostHog] session_idle_timeout_seconds cannot be greater than 30 minutes. Using 30 minutes instead.');
|
|
40
|
+
}
|
|
41
|
+
else if (desiredTimeout < MIN_SESSION_IDLE_TIMEOUT) {
|
|
42
|
+
console.warn('[PostHog] session_idle_timeout_seconds cannot be less than 60 seconds. Using 60 seconds instead.');
|
|
43
|
+
}
|
|
44
|
+
this._sessionTimeoutMs =
|
|
45
|
+
Math.min(Math.max(desiredTimeout, MIN_SESSION_IDLE_TIMEOUT), MAX_SESSION_IDLE_TIMEOUT) * 1000;
|
|
46
|
+
this._window_id_storage_key = 'ph_' + persistenceName + '_window_id';
|
|
47
|
+
this._primary_window_exists_storage_key = 'ph_' + persistenceName + '_primary_window_exists';
|
|
48
|
+
// primary_window_exists is set when the DOM has been loaded and is cleared on unload
|
|
49
|
+
// if it exists here it means there was no unload which suggests this window is opened as a tab duplication, window.open, etc.
|
|
50
|
+
if (this._canUseSessionStorage()) {
|
|
51
|
+
var lastWindowId = sessionStore.parse(this._window_id_storage_key);
|
|
52
|
+
var primaryWindowExists = sessionStore.parse(this._primary_window_exists_storage_key);
|
|
53
|
+
if (lastWindowId && !primaryWindowExists) {
|
|
54
|
+
// Persist window from previous storage state
|
|
55
|
+
this._windowId = lastWindowId;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// Wipe any reference to previous window id
|
|
59
|
+
sessionStore.remove(this._window_id_storage_key);
|
|
60
|
+
}
|
|
61
|
+
// Flag this session as having a primary window
|
|
62
|
+
sessionStore.set(this._primary_window_exists_storage_key, true);
|
|
63
|
+
}
|
|
64
|
+
this._listenToReloadWindow();
|
|
65
|
+
}
|
|
66
|
+
SessionIdManager.prototype.onSessionId = function (callback) {
|
|
67
|
+
var _this = this;
|
|
68
|
+
// KLUDGE: when running in tests the handlers array was always undefined
|
|
69
|
+
// it's yucky but safe to set it here so that it's always definitely available
|
|
70
|
+
if (this._sessionIdChangedHandlers === undefined) {
|
|
71
|
+
this._sessionIdChangedHandlers = [];
|
|
72
|
+
}
|
|
73
|
+
this._sessionIdChangedHandlers.push(callback);
|
|
74
|
+
if (this._sessionId) {
|
|
75
|
+
callback(this._sessionId, this._windowId);
|
|
76
|
+
}
|
|
77
|
+
return function () {
|
|
78
|
+
_this._sessionIdChangedHandlers = _this._sessionIdChangedHandlers.filter(function (h) { return h !== callback; });
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
SessionIdManager.prototype._canUseSessionStorage = function () {
|
|
82
|
+
// We only want to use sessionStorage if persistence is enabled and not memory storage
|
|
83
|
+
return this.config.persistence !== 'memory' && !this.persistence.disabled && sessionStore.is_supported();
|
|
84
|
+
};
|
|
85
|
+
// Note: this tries to store the windowId in sessionStorage. SessionStorage is unique to the current window/tab,
|
|
86
|
+
// and persists page loads/reloads. So it's uniquely suited for storing the windowId. This function also respects
|
|
87
|
+
// when persistence is disabled (by user config) and when sessionStorage is not supported (it *should* be supported on all browsers),
|
|
88
|
+
// and in that case, it falls back to memory (which sadly, won't persist page loads)
|
|
89
|
+
SessionIdManager.prototype._setWindowId = function (windowId) {
|
|
90
|
+
if (windowId !== this._windowId) {
|
|
91
|
+
this._windowId = windowId;
|
|
92
|
+
if (this._canUseSessionStorage()) {
|
|
93
|
+
sessionStore.set(this._window_id_storage_key, windowId);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
SessionIdManager.prototype._getWindowId = function () {
|
|
98
|
+
if (this._windowId) {
|
|
99
|
+
return this._windowId;
|
|
100
|
+
}
|
|
101
|
+
if (this._canUseSessionStorage()) {
|
|
102
|
+
return sessionStore.parse(this._window_id_storage_key);
|
|
103
|
+
}
|
|
104
|
+
// New window id will be generated
|
|
105
|
+
return null;
|
|
106
|
+
};
|
|
107
|
+
// Note: 'this.persistence.register' can be disabled in the config.
|
|
108
|
+
// In that case, this works by storing sessionId and the timestamp in memory.
|
|
109
|
+
SessionIdManager.prototype._setSessionId = function (sessionId, sessionActivityTimestamp, sessionStartTimestamp) {
|
|
110
|
+
var _a;
|
|
111
|
+
if (sessionId !== this._sessionId ||
|
|
112
|
+
sessionActivityTimestamp !== this._sessionActivityTimestamp ||
|
|
113
|
+
sessionStartTimestamp !== this._sessionStartTimestamp) {
|
|
114
|
+
this._sessionStartTimestamp = sessionStartTimestamp;
|
|
115
|
+
this._sessionActivityTimestamp = sessionActivityTimestamp;
|
|
116
|
+
this._sessionId = sessionId;
|
|
117
|
+
this.persistence.register((_a = {},
|
|
118
|
+
_a[SESSION_ID] = [sessionActivityTimestamp, sessionId, sessionStartTimestamp],
|
|
119
|
+
_a));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
SessionIdManager.prototype._getSessionId = function () {
|
|
123
|
+
if (this._sessionId && this._sessionActivityTimestamp && this._sessionStartTimestamp) {
|
|
124
|
+
return [this._sessionActivityTimestamp, this._sessionId, this._sessionStartTimestamp];
|
|
125
|
+
}
|
|
126
|
+
var sessionId = this.persistence.props[SESSION_ID];
|
|
127
|
+
if (Array.isArray(sessionId) && sessionId.length === 2) {
|
|
128
|
+
// Storage does not yet have a session start time. Add the last activity timestamp as the start time
|
|
129
|
+
sessionId.push(sessionId[0]);
|
|
130
|
+
}
|
|
131
|
+
return sessionId || [0, null, 0];
|
|
132
|
+
};
|
|
133
|
+
// Resets the session id by setting it to null. On the subsequent call to checkAndGetSessionAndWindowId,
|
|
134
|
+
// new ids will be generated.
|
|
135
|
+
SessionIdManager.prototype.resetSessionId = function () {
|
|
136
|
+
this._setSessionId(null, null, null);
|
|
137
|
+
};
|
|
138
|
+
/*
|
|
139
|
+
* Listens to window unloads and removes the primaryWindowExists key from sessionStorage.
|
|
140
|
+
* Reloaded or fresh tabs created after a DOM unloads (reloading the same tab) WILL NOT have this primaryWindowExists flag in session storage.
|
|
141
|
+
* Cloned sessions (new tab, tab duplication, window.open(), ...) WILL have this primaryWindowExists flag in their copied session storage.
|
|
142
|
+
* We conditionally check the primaryWindowExists value in the constructor to decide if the window id in the last session storage should be carried over.
|
|
143
|
+
*/
|
|
144
|
+
SessionIdManager.prototype._listenToReloadWindow = function () {
|
|
145
|
+
var _this = this;
|
|
146
|
+
window.addEventListener('beforeunload', function () {
|
|
147
|
+
if (_this._canUseSessionStorage()) {
|
|
148
|
+
sessionStore.remove(_this._primary_window_exists_storage_key);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
/*
|
|
153
|
+
* This function returns the current sessionId and windowId. It should be used to
|
|
154
|
+
* access these values over directly calling `._sessionId` or `._windowId`. In addition
|
|
155
|
+
* to returning the sessionId and windowId, this function also manages cycling the
|
|
156
|
+
* sessionId and windowId when appropriate by doing the following:
|
|
157
|
+
*
|
|
158
|
+
* 1. If the sessionId or windowId is not set, it will generate a new one and store it.
|
|
159
|
+
* 2. If the readOnly param is set to false, it will:
|
|
160
|
+
* a. Check if it has been > SESSION_CHANGE_THRESHOLD since the last call with this flag set.
|
|
161
|
+
* If so, it will generate a new sessionId and store it.
|
|
162
|
+
* b. Update the timestamp stored with the sessionId to ensure the current session is extended
|
|
163
|
+
* for the appropriate amount of time.
|
|
164
|
+
*
|
|
165
|
+
* @param {boolean} readOnly (optional) Defaults to False. Should be set to True when the call to the function should not extend or cycle the session (e.g. being called for non-user generated events)
|
|
166
|
+
* @param {Number} timestamp (optional) Defaults to the current time. The timestamp to be stored with the sessionId (used when determining if a new sessionId should be generated)
|
|
167
|
+
*/
|
|
168
|
+
SessionIdManager.prototype.checkAndGetSessionAndWindowId = function (readOnly, _timestamp) {
|
|
169
|
+
if (readOnly === void 0) { readOnly = false; }
|
|
170
|
+
if (_timestamp === void 0) { _timestamp = null; }
|
|
171
|
+
var timestamp = _timestamp || new Date().getTime();
|
|
172
|
+
// eslint-disable-next-line prefer-const
|
|
173
|
+
var _a = __read(this._getSessionId(), 3), lastTimestamp = _a[0], sessionId = _a[1], startTimestamp = _a[2];
|
|
174
|
+
var windowId = this._getWindowId();
|
|
175
|
+
var sessionPastMaximumLength = startTimestamp && startTimestamp > 0 && Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT;
|
|
176
|
+
var valuesChanged = false;
|
|
177
|
+
if (!sessionId ||
|
|
178
|
+
(!readOnly && Math.abs(timestamp - lastTimestamp) > this._sessionTimeoutMs) ||
|
|
179
|
+
sessionPastMaximumLength) {
|
|
180
|
+
sessionId = uuidv7();
|
|
181
|
+
windowId = uuidv7();
|
|
182
|
+
startTimestamp = timestamp;
|
|
183
|
+
valuesChanged = true;
|
|
184
|
+
}
|
|
185
|
+
else if (!windowId) {
|
|
186
|
+
windowId = uuidv7();
|
|
187
|
+
valuesChanged = true;
|
|
188
|
+
}
|
|
189
|
+
var newTimestamp = lastTimestamp === 0 || !readOnly || sessionPastMaximumLength ? timestamp : lastTimestamp;
|
|
190
|
+
var sessionStartTimestamp = startTimestamp === 0 ? new Date().getTime() : startTimestamp;
|
|
191
|
+
this._setWindowId(windowId);
|
|
192
|
+
this._setSessionId(sessionId, newTimestamp, sessionStartTimestamp);
|
|
193
|
+
if (valuesChanged) {
|
|
194
|
+
this._sessionIdChangedHandlers.forEach(function (handler) { return handler(sessionId, windowId); });
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
sessionId: sessionId,
|
|
198
|
+
windowId: windowId,
|
|
199
|
+
sessionStartTimestamp: sessionStartTimestamp,
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
return SessionIdManager;
|
|
203
|
+
}());
|
|
204
|
+
export { SessionIdManager };
|
|
205
|
+
//# sourceMappingURL=sessionid.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessionid.js","sourceRoot":"","sources":["../../src/sessionid.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,OAAO,EAAE,UAAU,EAAE,MAAM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AAO,IAAK,OAAA,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,EAA5B,CAA4B,CAAC,CAAA;SACpF;QAED,OAAO;YACH,SAAS,WAAA;YACT,QAAQ,UAAA;YACR,qBAAqB,uBAAA;SACxB,CAAA;IACL,CAAC;IACL,uBAAC;AAAD,CAAC,AA1ND,IA0NC","sourcesContent":["import { PostHogPersistence } from './posthog-persistence'\nimport { SESSION_ID } from './constants'\nimport { sessionStore } from './storage'\nimport { PostHogConfig, SessionIdChangedCallback } from './types'\nimport { uuidv7 } from './uuidv7'\n\nconst MAX_SESSION_IDLE_TIMEOUT = 30 * 60 // 30 mins\nconst MIN_SESSION_IDLE_TIMEOUT = 60 // 1 mins\nconst SESSION_LENGTH_LIMIT = 24 * 3600 * 1000 // 24 hours\n\nexport class SessionIdManager {\n private config: Partial<PostHogConfig>\n private persistence: PostHogPersistence\n private _windowId: string | null | undefined\n private _sessionId: string | null | undefined\n private _window_id_storage_key: string\n private _primary_window_exists_storage_key: string\n private _sessionStartTimestamp: number | null\n private _sessionActivityTimestamp: number | null\n private _sessionTimeoutMs: number\n private _sessionIdChangedHandlers: SessionIdChangedCallback[] = []\n\n constructor(config: Partial<PostHogConfig>, persistence: PostHogPersistence) {\n this.config = config\n this.persistence = persistence\n this._windowId = undefined\n this._sessionId = undefined\n this._sessionStartTimestamp = null\n this._sessionActivityTimestamp = null\n\n const persistenceName = config['persistence_name'] || config['token']\n let desiredTimeout = config['session_idle_timeout_seconds'] || MAX_SESSION_IDLE_TIMEOUT\n\n if (typeof desiredTimeout !== 'number') {\n console.warn('[PostHog] session_idle_timeout_seconds must be a number. Defaulting to 30 minutes.')\n desiredTimeout = MAX_SESSION_IDLE_TIMEOUT\n } else if (desiredTimeout > MAX_SESSION_IDLE_TIMEOUT) {\n console.warn(\n '[PostHog] session_idle_timeout_seconds cannot be greater than 30 minutes. Using 30 minutes instead.'\n )\n } else if (desiredTimeout < MIN_SESSION_IDLE_TIMEOUT) {\n console.warn(\n '[PostHog] session_idle_timeout_seconds cannot be less than 60 seconds. Using 60 seconds instead.'\n )\n }\n\n this._sessionTimeoutMs =\n Math.min(Math.max(desiredTimeout, MIN_SESSION_IDLE_TIMEOUT), MAX_SESSION_IDLE_TIMEOUT) * 1000\n this._window_id_storage_key = 'ph_' + persistenceName + '_window_id'\n this._primary_window_exists_storage_key = 'ph_' + persistenceName + '_primary_window_exists'\n\n // primary_window_exists is set when the DOM has been loaded and is cleared on unload\n // if it exists here it means there was no unload which suggests this window is opened as a tab duplication, window.open, etc.\n if (this._canUseSessionStorage()) {\n const lastWindowId = sessionStore.parse(this._window_id_storage_key)\n\n const primaryWindowExists = sessionStore.parse(this._primary_window_exists_storage_key)\n if (lastWindowId && !primaryWindowExists) {\n // Persist window from previous storage state\n this._windowId = lastWindowId\n } else {\n // Wipe any reference to previous window id\n sessionStore.remove(this._window_id_storage_key)\n }\n // Flag this session as having a primary window\n sessionStore.set(this._primary_window_exists_storage_key, true)\n }\n\n this._listenToReloadWindow()\n }\n\n onSessionId(callback: SessionIdChangedCallback): () => void {\n // KLUDGE: when running in tests the handlers array was always undefined\n // it's yucky but safe to set it here so that it's always definitely available\n if (this._sessionIdChangedHandlers === undefined) {\n this._sessionIdChangedHandlers = []\n }\n\n this._sessionIdChangedHandlers.push(callback)\n if (this._sessionId) {\n callback(this._sessionId, this._windowId)\n }\n return () => {\n this._sessionIdChangedHandlers = this._sessionIdChangedHandlers.filter((h) => h !== callback)\n }\n }\n\n private _canUseSessionStorage(): boolean {\n // We only want to use sessionStorage if persistence is enabled and not memory storage\n return this.config.persistence !== 'memory' && !this.persistence.disabled && sessionStore.is_supported()\n }\n\n // Note: this tries to store the windowId in sessionStorage. SessionStorage is unique to the current window/tab,\n // and persists page loads/reloads. So it's uniquely suited for storing the windowId. This function also respects\n // when persistence is disabled (by user config) and when sessionStorage is not supported (it *should* be supported on all browsers),\n // and in that case, it falls back to memory (which sadly, won't persist page loads)\n private _setWindowId(windowId: string): void {\n if (windowId !== this._windowId) {\n this._windowId = windowId\n if (this._canUseSessionStorage()) {\n sessionStore.set(this._window_id_storage_key, windowId)\n }\n }\n }\n\n private _getWindowId(): string | null {\n if (this._windowId) {\n return this._windowId\n }\n if (this._canUseSessionStorage()) {\n return sessionStore.parse(this._window_id_storage_key)\n }\n // New window id will be generated\n return null\n }\n\n // Note: 'this.persistence.register' can be disabled in the config.\n // In that case, this works by storing sessionId and the timestamp in memory.\n private _setSessionId(\n sessionId: string | null,\n sessionActivityTimestamp: number | null,\n sessionStartTimestamp: number | null\n ): void {\n if (\n sessionId !== this._sessionId ||\n sessionActivityTimestamp !== this._sessionActivityTimestamp ||\n sessionStartTimestamp !== this._sessionStartTimestamp\n ) {\n this._sessionStartTimestamp = sessionStartTimestamp\n this._sessionActivityTimestamp = sessionActivityTimestamp\n this._sessionId = sessionId\n this.persistence.register({\n [SESSION_ID]: [sessionActivityTimestamp, sessionId, sessionStartTimestamp],\n })\n }\n }\n\n private _getSessionId(): [number, string, number] {\n if (this._sessionId && this._sessionActivityTimestamp && this._sessionStartTimestamp) {\n return [this._sessionActivityTimestamp, this._sessionId, this._sessionStartTimestamp]\n }\n const sessionId = this.persistence.props[SESSION_ID]\n\n if (Array.isArray(sessionId) && sessionId.length === 2) {\n // Storage does not yet have a session start time. Add the last activity timestamp as the start time\n sessionId.push(sessionId[0])\n }\n\n return sessionId || [0, null, 0]\n }\n\n // Resets the session id by setting it to null. On the subsequent call to checkAndGetSessionAndWindowId,\n // new ids will be generated.\n resetSessionId(): void {\n this._setSessionId(null, null, null)\n }\n\n /*\n * Listens to window unloads and removes the primaryWindowExists key from sessionStorage.\n * Reloaded or fresh tabs created after a DOM unloads (reloading the same tab) WILL NOT have this primaryWindowExists flag in session storage.\n * Cloned sessions (new tab, tab duplication, window.open(), ...) WILL have this primaryWindowExists flag in their copied session storage.\n * We conditionally check the primaryWindowExists value in the constructor to decide if the window id in the last session storage should be carried over.\n */\n private _listenToReloadWindow(): void {\n window.addEventListener('beforeunload', () => {\n if (this._canUseSessionStorage()) {\n sessionStore.remove(this._primary_window_exists_storage_key)\n }\n })\n }\n\n /*\n * This function returns the current sessionId and windowId. It should be used to\n * access these values over directly calling `._sessionId` or `._windowId`. In addition\n * to returning the sessionId and windowId, this function also manages cycling the\n * sessionId and windowId when appropriate by doing the following:\n *\n * 1. If the sessionId or windowId is not set, it will generate a new one and store it.\n * 2. If the readOnly param is set to false, it will:\n * a. Check if it has been > SESSION_CHANGE_THRESHOLD since the last call with this flag set.\n * If so, it will generate a new sessionId and store it.\n * b. Update the timestamp stored with the sessionId to ensure the current session is extended\n * for the appropriate amount of time.\n *\n * @param {boolean} readOnly (optional) Defaults to False. Should be set to True when the call to the function should not extend or cycle the session (e.g. being called for non-user generated events)\n * @param {Number} timestamp (optional) Defaults to the current time. The timestamp to be stored with the sessionId (used when determining if a new sessionId should be generated)\n */\n checkAndGetSessionAndWindowId(readOnly = false, _timestamp: number | null = null) {\n const timestamp = _timestamp || new Date().getTime()\n\n // eslint-disable-next-line prefer-const\n let [lastTimestamp, sessionId, startTimestamp] = this._getSessionId()\n let windowId = this._getWindowId()\n\n const sessionPastMaximumLength =\n startTimestamp && startTimestamp > 0 && Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT\n\n let valuesChanged = false\n if (\n !sessionId ||\n (!readOnly && Math.abs(timestamp - lastTimestamp) > this._sessionTimeoutMs) ||\n sessionPastMaximumLength\n ) {\n sessionId = uuidv7()\n windowId = uuidv7()\n startTimestamp = timestamp\n valuesChanged = true\n } else if (!windowId) {\n windowId = uuidv7()\n valuesChanged = true\n }\n\n const newTimestamp = lastTimestamp === 0 || !readOnly || sessionPastMaximumLength ? timestamp : lastTimestamp\n const sessionStartTimestamp = startTimestamp === 0 ? new Date().getTime() : startTimestamp\n\n this._setWindowId(windowId)\n this._setSessionId(sessionId, newTimestamp, sessionStartTimestamp)\n\n if (valuesChanged) {\n this._sessionIdChangedHandlers.forEach((handler) => handler(sessionId, windowId))\n }\n\n return {\n sessionId,\n windowId,\n sessionStartTimestamp,\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PersistentStore } from './types';
|
|
2
|
+
export declare const cookieStore: PersistentStore;
|
|
3
|
+
export declare const localStore: PersistentStore;
|
|
4
|
+
export declare const localPlusCookieStore: PersistentStore;
|
|
5
|
+
export declare const memoryStore: PersistentStore;
|
|
6
|
+
export declare const resetSessionStorageSupported: () => void;
|
|
7
|
+
export declare const sessionStore: PersistentStore;
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import { _extend, logger } from './utils';
|
|
13
|
+
import Config from './config';
|
|
14
|
+
import { DISTINCT_ID, SESSION_ID } from './constants';
|
|
15
|
+
var DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\.[a-z.]{2,6}$/i;
|
|
16
|
+
// Methods partially borrowed from quirksmode.org/js/cookies.html
|
|
17
|
+
export var cookieStore = {
|
|
18
|
+
is_supported: function () { return true; },
|
|
19
|
+
error: function (msg) {
|
|
20
|
+
logger.error('cookieStore error: ' + msg);
|
|
21
|
+
},
|
|
22
|
+
get: function (name) {
|
|
23
|
+
try {
|
|
24
|
+
var nameEQ = name + '=';
|
|
25
|
+
var ca = document.cookie.split(';').filter(function (x) { return x.length; });
|
|
26
|
+
for (var i = 0; i < ca.length; i++) {
|
|
27
|
+
var c = ca[i];
|
|
28
|
+
while (c.charAt(0) == ' ') {
|
|
29
|
+
c = c.substring(1, c.length);
|
|
30
|
+
}
|
|
31
|
+
if (c.indexOf(nameEQ) === 0) {
|
|
32
|
+
return decodeURIComponent(c.substring(nameEQ.length, c.length));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (err) { }
|
|
37
|
+
return null;
|
|
38
|
+
},
|
|
39
|
+
parse: function (name) {
|
|
40
|
+
var cookie;
|
|
41
|
+
try {
|
|
42
|
+
cookie = JSON.parse(cookieStore.get(name)) || {};
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
// noop
|
|
46
|
+
}
|
|
47
|
+
return cookie;
|
|
48
|
+
},
|
|
49
|
+
set: function (name, value, days, cross_subdomain, is_secure) {
|
|
50
|
+
try {
|
|
51
|
+
var cdomain = '', expires = '', secure = '';
|
|
52
|
+
if (cross_subdomain) {
|
|
53
|
+
// NOTE: Could we use this for cross domain tracking?
|
|
54
|
+
var matches = document.location.hostname.match(DOMAIN_MATCH_REGEX), domain = matches ? matches[0] : '';
|
|
55
|
+
cdomain = domain ? '; domain=.' + domain : '';
|
|
56
|
+
}
|
|
57
|
+
if (days) {
|
|
58
|
+
var date = new Date();
|
|
59
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
60
|
+
expires = '; expires=' + date.toUTCString();
|
|
61
|
+
}
|
|
62
|
+
if (is_secure) {
|
|
63
|
+
secure = '; secure';
|
|
64
|
+
}
|
|
65
|
+
var new_cookie_val = name +
|
|
66
|
+
'=' +
|
|
67
|
+
encodeURIComponent(JSON.stringify(value)) +
|
|
68
|
+
expires +
|
|
69
|
+
'; SameSite=Lax; path=/' +
|
|
70
|
+
cdomain +
|
|
71
|
+
secure;
|
|
72
|
+
document.cookie = new_cookie_val;
|
|
73
|
+
return new_cookie_val;
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
remove: function (name, cross_subdomain) {
|
|
80
|
+
try {
|
|
81
|
+
cookieStore.set(name, '', -1, cross_subdomain);
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
var _localStorage_supported = null;
|
|
89
|
+
export var localStore = {
|
|
90
|
+
is_supported: function () {
|
|
91
|
+
if (_localStorage_supported !== null) {
|
|
92
|
+
return _localStorage_supported;
|
|
93
|
+
}
|
|
94
|
+
var supported = true;
|
|
95
|
+
if (typeof window !== 'undefined') {
|
|
96
|
+
try {
|
|
97
|
+
var key = '__mplssupport__', val = 'xyz';
|
|
98
|
+
localStore.set(key, val);
|
|
99
|
+
if (localStore.get(key) !== '"xyz"') {
|
|
100
|
+
supported = false;
|
|
101
|
+
}
|
|
102
|
+
localStore.remove(key);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
supported = false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
supported = false;
|
|
110
|
+
}
|
|
111
|
+
if (!supported) {
|
|
112
|
+
logger.error('localStorage unsupported; falling back to cookie store');
|
|
113
|
+
}
|
|
114
|
+
_localStorage_supported = supported;
|
|
115
|
+
return supported;
|
|
116
|
+
},
|
|
117
|
+
error: function (msg) {
|
|
118
|
+
logger.error('localStorage error: ' + msg);
|
|
119
|
+
},
|
|
120
|
+
get: function (name) {
|
|
121
|
+
try {
|
|
122
|
+
return window.localStorage.getItem(name);
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
localStore.error(err);
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
},
|
|
129
|
+
parse: function (name) {
|
|
130
|
+
try {
|
|
131
|
+
return JSON.parse(localStore.get(name)) || {};
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
// noop
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
},
|
|
138
|
+
set: function (name, value) {
|
|
139
|
+
try {
|
|
140
|
+
window.localStorage.setItem(name, JSON.stringify(value));
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
localStore.error(err);
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
remove: function (name) {
|
|
147
|
+
try {
|
|
148
|
+
window.localStorage.removeItem(name);
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
localStore.error(err);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
// Use localstorage for most data but still use cookie for COOKIE_PERSISTED_PROPERTIES
|
|
156
|
+
// This solves issues with cookies having too much data in them causing headers too large
|
|
157
|
+
// Also makes sure we don't have to send a ton of data to the server
|
|
158
|
+
var COOKIE_PERSISTED_PROPERTIES = [DISTINCT_ID, SESSION_ID];
|
|
159
|
+
export var localPlusCookieStore = __assign(__assign({}, localStore), { parse: function (name) {
|
|
160
|
+
try {
|
|
161
|
+
var extend = {};
|
|
162
|
+
try {
|
|
163
|
+
// See if there's a cookie stored with data.
|
|
164
|
+
extend = cookieStore.parse(name) || {};
|
|
165
|
+
}
|
|
166
|
+
catch (err) { }
|
|
167
|
+
var value = _extend(extend, JSON.parse(localStore.get(name) || '{}'));
|
|
168
|
+
localStore.set(name, value);
|
|
169
|
+
return value;
|
|
170
|
+
}
|
|
171
|
+
catch (err) {
|
|
172
|
+
// noop
|
|
173
|
+
}
|
|
174
|
+
return null;
|
|
175
|
+
}, set: function (name, value, days, cross_subdomain, is_secure) {
|
|
176
|
+
try {
|
|
177
|
+
localStore.set(name, value);
|
|
178
|
+
var cookiePersistedProperties_1 = {};
|
|
179
|
+
COOKIE_PERSISTED_PROPERTIES.forEach(function (key) {
|
|
180
|
+
if (value[key]) {
|
|
181
|
+
cookiePersistedProperties_1[key] = value[key];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
if (Object.keys(cookiePersistedProperties_1).length) {
|
|
185
|
+
cookieStore.set(name, cookiePersistedProperties_1, days, cross_subdomain, is_secure);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
localStore.error(err);
|
|
190
|
+
}
|
|
191
|
+
}, remove: function (name, cross_subdomain) {
|
|
192
|
+
try {
|
|
193
|
+
window.localStorage.removeItem(name);
|
|
194
|
+
cookieStore.remove(name, cross_subdomain);
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
localStore.error(err);
|
|
198
|
+
}
|
|
199
|
+
} });
|
|
200
|
+
var memoryStorage = {};
|
|
201
|
+
// Storage that only lasts the length of the pageview if we don't want to use cookies
|
|
202
|
+
export var memoryStore = {
|
|
203
|
+
is_supported: function () {
|
|
204
|
+
return true;
|
|
205
|
+
},
|
|
206
|
+
error: function (msg) {
|
|
207
|
+
logger.error('memoryStorage error: ' + msg);
|
|
208
|
+
},
|
|
209
|
+
get: function (name) {
|
|
210
|
+
return memoryStorage[name] || null;
|
|
211
|
+
},
|
|
212
|
+
parse: function (name) {
|
|
213
|
+
return memoryStorage[name] || null;
|
|
214
|
+
},
|
|
215
|
+
set: function (name, value) {
|
|
216
|
+
memoryStorage[name] = value;
|
|
217
|
+
},
|
|
218
|
+
remove: function (name) {
|
|
219
|
+
delete memoryStorage[name];
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
var sessionStorageSupported = null;
|
|
223
|
+
export var resetSessionStorageSupported = function () {
|
|
224
|
+
sessionStorageSupported = null;
|
|
225
|
+
};
|
|
226
|
+
// Storage that only lasts the length of a tab/window. Survives page refreshes
|
|
227
|
+
export var sessionStore = {
|
|
228
|
+
is_supported: function () {
|
|
229
|
+
if (sessionStorageSupported !== null) {
|
|
230
|
+
return sessionStorageSupported;
|
|
231
|
+
}
|
|
232
|
+
sessionStorageSupported = true;
|
|
233
|
+
if (typeof window !== 'undefined') {
|
|
234
|
+
try {
|
|
235
|
+
var key = '__support__', val = 'xyz';
|
|
236
|
+
sessionStore.set(key, val);
|
|
237
|
+
if (sessionStore.get(key) !== '"xyz"') {
|
|
238
|
+
sessionStorageSupported = false;
|
|
239
|
+
}
|
|
240
|
+
sessionStore.remove(key);
|
|
241
|
+
}
|
|
242
|
+
catch (err) {
|
|
243
|
+
sessionStorageSupported = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
sessionStorageSupported = false;
|
|
248
|
+
}
|
|
249
|
+
return sessionStorageSupported;
|
|
250
|
+
},
|
|
251
|
+
error: function (msg) {
|
|
252
|
+
if (Config.DEBUG) {
|
|
253
|
+
logger.error('sessionStorage error: ', msg);
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
get: function (name) {
|
|
257
|
+
try {
|
|
258
|
+
return window.sessionStorage.getItem(name);
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
sessionStore.error(err);
|
|
262
|
+
}
|
|
263
|
+
return null;
|
|
264
|
+
},
|
|
265
|
+
parse: function (name) {
|
|
266
|
+
try {
|
|
267
|
+
return JSON.parse(sessionStore.get(name)) || null;
|
|
268
|
+
}
|
|
269
|
+
catch (err) {
|
|
270
|
+
// noop
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
},
|
|
274
|
+
set: function (name, value) {
|
|
275
|
+
try {
|
|
276
|
+
window.sessionStorage.setItem(name, JSON.stringify(value));
|
|
277
|
+
}
|
|
278
|
+
catch (err) {
|
|
279
|
+
sessionStore.error(err);
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
remove: function (name) {
|
|
283
|
+
try {
|
|
284
|
+
window.sessionStorage.removeItem(name);
|
|
285
|
+
}
|
|
286
|
+
catch (err) {
|
|
287
|
+
sessionStore.error(err);
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM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'\nimport { PersistentStore, Properties } from './types'\nimport Config from './config'\nimport { DISTINCT_ID, SESSION_ID } from './constants'\n\nconst DOMAIN_MATCH_REGEX = /[a-z0-9][a-z0-9-]+\\.[a-z.]{2,6}$/i\n\n// Methods partially borrowed from quirksmode.org/js/cookies.html\nexport const cookieStore: PersistentStore = {\n is_supported: () => true,\n\n error: function (msg) {\n logger.error('cookieStore error: ' + msg)\n },\n\n get: function (name) {\n try {\n const nameEQ = name + '='\n const ca = document.cookie.split(';').filter((x) => x.length)\n for (let i = 0; i < ca.length; i++) {\n let c = ca[i]\n while (c.charAt(0) == ' ') {\n c = c.substring(1, c.length)\n }\n if (c.indexOf(nameEQ) === 0) {\n return decodeURIComponent(c.substring(nameEQ.length, c.length))\n }\n }\n } catch (err) {}\n return null\n },\n\n parse: function (name) {\n let cookie\n try {\n cookie = JSON.parse(cookieStore.get(name)) || {}\n } catch (err) {\n // noop\n }\n return cookie\n },\n\n set: function (name, value, days, cross_subdomain, is_secure) {\n try {\n let cdomain = '',\n expires = '',\n secure = ''\n\n if (cross_subdomain) {\n // NOTE: Could we use this for cross domain tracking?\n const matches = document.location.hostname.match(DOMAIN_MATCH_REGEX),\n domain = matches ? matches[0] : ''\n\n cdomain = domain ? '; domain=.' + domain : ''\n }\n\n if (days) {\n const date = new Date()\n date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)\n expires = '; expires=' + date.toUTCString()\n }\n\n if (is_secure) {\n secure = '; secure'\n }\n\n const new_cookie_val =\n name +\n '=' +\n encodeURIComponent(JSON.stringify(value)) +\n expires +\n '; SameSite=Lax; path=/' +\n cdomain +\n secure\n document.cookie = new_cookie_val\n return new_cookie_val\n } catch (err) {\n return\n }\n },\n\n remove: function (name, cross_subdomain) {\n try {\n cookieStore.set(name, '', -1, cross_subdomain)\n } catch (err) {\n return\n }\n },\n}\n\nlet _localStorage_supported: boolean | null = null\n\nexport const localStore: PersistentStore = {\n is_supported: function () {\n if (_localStorage_supported !== null) {\n return _localStorage_supported\n }\n\n let supported = true\n if (typeof window !== 'undefined') {\n try {\n const key = '__mplssupport__',\n val = 'xyz'\n localStore.set(key, val)\n if (localStore.get(key) !== '\"xyz\"') {\n supported = false\n }\n localStore.remove(key)\n } catch (err) {\n supported = false\n }\n } else {\n supported = false\n }\n if (!supported) {\n logger.error('localStorage unsupported; falling back to cookie store')\n }\n\n _localStorage_supported = supported\n return supported\n },\n\n error: function (msg) {\n logger.error('localStorage error: ' + msg)\n },\n\n get: function (name) {\n try {\n return window.localStorage.getItem(name)\n } catch (err) {\n localStore.error(err)\n }\n return null\n },\n\n parse: function (name) {\n try {\n return JSON.parse(localStore.get(name)) || {}\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value) {\n try {\n window.localStorage.setItem(name, JSON.stringify(value))\n } catch (err) {\n localStore.error(err)\n }\n },\n\n remove: function (name) {\n try {\n window.localStorage.removeItem(name)\n } catch (err) {\n localStore.error(err)\n }\n },\n}\n\n// Use localstorage for most data but still use cookie for COOKIE_PERSISTED_PROPERTIES\n// This solves issues with cookies having too much data in them causing headers too large\n// Also makes sure we don't have to send a ton of data to the server\nconst COOKIE_PERSISTED_PROPERTIES = [DISTINCT_ID, SESSION_ID]\n\nexport const localPlusCookieStore: PersistentStore = {\n ...localStore,\n parse: function (name) {\n try {\n let extend: Properties = {}\n try {\n // See if there's a cookie stored with data.\n extend = cookieStore.parse(name) || {}\n } catch (err) {}\n const value = _extend(extend, JSON.parse(localStore.get(name) || '{}'))\n localStore.set(name, value)\n return value\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value, days, cross_subdomain, is_secure) {\n try {\n localStore.set(name, value)\n const cookiePersistedProperties: Record<string, any> = {}\n COOKIE_PERSISTED_PROPERTIES.forEach((key) => {\n if (value[key]) {\n cookiePersistedProperties[key] = value[key]\n }\n })\n\n if (Object.keys(cookiePersistedProperties).length) {\n cookieStore.set(name, cookiePersistedProperties, days, cross_subdomain, is_secure)\n }\n } catch (err) {\n localStore.error(err)\n }\n },\n\n remove: function (name, cross_subdomain) {\n try {\n window.localStorage.removeItem(name)\n cookieStore.remove(name, cross_subdomain)\n } catch (err) {\n localStore.error(err)\n }\n },\n}\n\nconst memoryStorage: Properties = {}\n\n// Storage that only lasts the length of the pageview if we don't want to use cookies\nexport const memoryStore: PersistentStore = {\n is_supported: function () {\n return true\n },\n\n error: function (msg) {\n logger.error('memoryStorage error: ' + msg)\n },\n\n get: function (name) {\n return memoryStorage[name] || null\n },\n\n parse: function (name) {\n return memoryStorage[name] || null\n },\n\n set: function (name, value) {\n memoryStorage[name] = value\n },\n\n remove: function (name) {\n delete memoryStorage[name]\n },\n}\n\nlet sessionStorageSupported: boolean | null = null\nexport const resetSessionStorageSupported = () => {\n sessionStorageSupported = null\n}\n// Storage that only lasts the length of a tab/window. Survives page refreshes\nexport const sessionStore: PersistentStore = {\n is_supported: function () {\n if (sessionStorageSupported !== null) {\n return sessionStorageSupported\n }\n sessionStorageSupported = true\n if (typeof window !== 'undefined') {\n try {\n const key = '__support__',\n val = 'xyz'\n sessionStore.set(key, val)\n if (sessionStore.get(key) !== '\"xyz\"') {\n sessionStorageSupported = false\n }\n sessionStore.remove(key)\n } catch (err) {\n sessionStorageSupported = false\n }\n } else {\n sessionStorageSupported = false\n }\n return sessionStorageSupported\n },\n\n error: function (msg) {\n if (Config.DEBUG) {\n logger.error('sessionStorage error: ', msg)\n }\n },\n\n get: function (name) {\n try {\n return window.sessionStorage.getItem(name)\n } catch (err) {\n sessionStore.error(err)\n }\n return null\n },\n\n parse: function (name) {\n try {\n return JSON.parse(sessionStore.get(name)) || null\n } catch (err) {\n // noop\n }\n return null\n },\n\n set: function (name, value) {\n try {\n window.sessionStorage.setItem(name, JSON.stringify(value))\n } catch (err) {\n sessionStore.error(err)\n }\n },\n\n remove: function (name) {\n try {\n window.sessionStorage.removeItem(name)\n } catch (err) {\n sessionStore.error(err)\n }\n },\n}\n"]}
|