@posthog/browser 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/ARCHITECTURE.md +559 -0
  2. package/LICENSE +417 -0
  3. package/README.md +302 -2
  4. package/dist/analytics-buffer.d.ts +4 -0
  5. package/dist/analytics-buffer.js +200 -0
  6. package/dist/analytics-buffer.mjs +162 -0
  7. package/dist/analytics-delivery.d.ts +3 -0
  8. package/dist/analytics-delivery.js +231 -0
  9. package/dist/analytics-delivery.mjs +193 -0
  10. package/dist/analytics-internal.d.ts +64 -0
  11. package/dist/analytics-internal.js +52 -0
  12. package/dist/analytics-internal.mjs +5 -0
  13. package/dist/analytics-options.d.ts +14 -0
  14. package/dist/analytics-options.js +18 -0
  15. package/dist/analytics-options.mjs +1 -0
  16. package/dist/analytics.d.ts +5 -0
  17. package/dist/analytics.js +42 -0
  18. package/dist/analytics.mjs +4 -0
  19. package/dist/autocapture-options.d.ts +32 -0
  20. package/dist/autocapture-options.js +49 -0
  21. package/dist/autocapture-options.mjs +11 -0
  22. package/dist/autocapture.d.ts +5 -0
  23. package/dist/autocapture.js +108 -0
  24. package/dist/autocapture.mjs +70 -0
  25. package/dist/automatic-analytics.d.ts +3 -0
  26. package/dist/automatic-analytics.js +44 -0
  27. package/dist/automatic-analytics.mjs +6 -0
  28. package/dist/automatic-surveys.d.ts +2 -0
  29. package/dist/automatic-surveys.js +41 -0
  30. package/dist/automatic-surveys.mjs +3 -0
  31. package/dist/bot-filter.d.ts +2 -0
  32. package/dist/bot-filter.js +87 -0
  33. package/dist/bot-filter.mjs +49 -0
  34. package/dist/browser-client.d.ts +30 -0
  35. package/dist/browser-client.js +18 -0
  36. package/dist/browser-client.mjs +1 -0
  37. package/dist/capture-summary.d.ts +3 -0
  38. package/dist/capture-summary.js +63 -0
  39. package/dist/capture-summary.mjs +22 -0
  40. package/dist/capture-v1.d.ts +105 -0
  41. package/dist/capture-v1.js +781 -0
  42. package/dist/capture-v1.mjs +722 -0
  43. package/dist/core.d.ts +7 -0
  44. package/dist/core.js +49 -0
  45. package/dist/core.mjs +5 -0
  46. package/dist/event-buffer.d.ts +41 -0
  47. package/dist/event-buffer.js +158 -0
  48. package/dist/event-buffer.mjs +120 -0
  49. package/dist/extensions/registry.d.ts +16 -0
  50. package/dist/extensions/registry.js +128 -0
  51. package/dist/extensions/registry.mjs +90 -0
  52. package/dist/flags-internal.d.ts +8 -0
  53. package/dist/flags-internal.js +18 -0
  54. package/dist/flags-internal.mjs +1 -0
  55. package/dist/flags-options.d.ts +22 -0
  56. package/dist/flags-options.js +18 -0
  57. package/dist/flags-options.mjs +1 -0
  58. package/dist/flags-token.d.ts +15 -0
  59. package/dist/flags-token.js +40 -0
  60. package/dist/flags-token.mjs +2 -0
  61. package/dist/flags.d.ts +6 -0
  62. package/dist/flags.js +163 -0
  63. package/dist/flags.mjs +122 -0
  64. package/dist/id.d.ts +1 -0
  65. package/dist/id.js +61 -0
  66. package/dist/id.mjs +23 -0
  67. package/dist/index.d.ts +11 -0
  68. package/dist/index.js +119 -0
  69. package/dist/index.mjs +75 -0
  70. package/dist/lane.d.ts +57 -0
  71. package/dist/lane.js +363 -0
  72. package/dist/lane.mjs +325 -0
  73. package/dist/logger.d.ts +4 -0
  74. package/dist/logger.js +57 -0
  75. package/dist/logger.mjs +19 -0
  76. package/dist/logs-internal.d.ts +7 -0
  77. package/dist/logs-internal.js +18 -0
  78. package/dist/logs-internal.mjs +1 -0
  79. package/dist/logs-options.d.ts +11 -0
  80. package/dist/logs-options.js +57 -0
  81. package/dist/logs-options.mjs +19 -0
  82. package/dist/logs.d.ts +6 -0
  83. package/dist/logs.js +140 -0
  84. package/dist/logs.mjs +102 -0
  85. package/dist/named-error.d.ts +7 -0
  86. package/dist/named-error.js +49 -0
  87. package/dist/named-error.mjs +11 -0
  88. package/dist/posthog.d.ts +3 -0
  89. package/dist/posthog.js +823 -0
  90. package/dist/posthog.mjs +785 -0
  91. package/dist/rate-limiter.d.ts +19 -0
  92. package/dist/rate-limiter.js +89 -0
  93. package/dist/rate-limiter.mjs +51 -0
  94. package/dist/remote-config.d.ts +3 -0
  95. package/dist/remote-config.js +47 -0
  96. package/dist/remote-config.mjs +9 -0
  97. package/dist/request.d.ts +14 -0
  98. package/dist/request.js +130 -0
  99. package/dist/request.mjs +89 -0
  100. package/dist/state.d.ts +82 -0
  101. package/dist/state.js +625 -0
  102. package/dist/state.mjs +581 -0
  103. package/dist/surveys-extension.d.ts +9 -0
  104. package/dist/surveys-extension.js +272 -0
  105. package/dist/surveys-extension.mjs +234 -0
  106. package/dist/surveys-internal.d.ts +12 -0
  107. package/dist/surveys-internal.js +18 -0
  108. package/dist/surveys-internal.mjs +1 -0
  109. package/dist/surveys-options.d.ts +18 -0
  110. package/dist/surveys-options.js +42 -0
  111. package/dist/surveys-options.mjs +4 -0
  112. package/dist/surveys.d.ts +7 -0
  113. package/dist/surveys.js +48 -0
  114. package/dist/surveys.mjs +7 -0
  115. package/dist/types.d.ts +114 -0
  116. package/dist/types.js +18 -0
  117. package/dist/types.mjs +1 -0
  118. package/dist/version.d.ts +1 -0
  119. package/dist/version.js +40 -0
  120. package/dist/version.mjs +2 -0
  121. package/package.json +88 -3
package/dist/state.js ADDED
@@ -0,0 +1,625 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, getters, values)=>{
5
+ var define = (defs, kind)=>{
6
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
7
+ enumerable: true,
8
+ [kind]: defs[key]
9
+ });
10
+ };
11
+ define(getters, "get");
12
+ define(values, "value");
13
+ };
14
+ })();
15
+ (()=>{
16
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
17
+ })();
18
+ (()=>{
19
+ __webpack_require__.r = (exports1)=>{
20
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
21
+ value: 'Module'
22
+ });
23
+ Object.defineProperty(exports1, '__esModule', {
24
+ value: true
25
+ });
26
+ };
27
+ })();
28
+ var __webpack_exports__ = {};
29
+ __webpack_require__.r(__webpack_exports__);
30
+ __webpack_require__.d(__webpack_exports__, {
31
+ BrowserState: ()=>BrowserState,
32
+ getDefaultSessionStorage: ()=>getDefaultSessionStorage,
33
+ getDefaultStorage: ()=>getDefaultStorage
34
+ });
35
+ const external_id_js_namespaceObject = require("./id.js");
36
+ const emptyRecord = ()=>Object.create(null);
37
+ const cloneJson = (value)=>{
38
+ const json = JSON.stringify(value);
39
+ if (void 0 === json) throw new TypeError('The value must be JSON-serializable');
40
+ return JSON.parse(json);
41
+ };
42
+ const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
43
+ const readGroups = (value)=>{
44
+ const groups = emptyRecord();
45
+ if (isRecord(value)) Object.entries(value).forEach(([key, group])=>{
46
+ if ('string' == typeof group) groups[key] = group;
47
+ });
48
+ return groups;
49
+ };
50
+ const isTimestamp = (value)=>'number' == typeof value && Number.isFinite(value) && value >= 0;
51
+ const readRevision = (value, fallback)=>{
52
+ if ('string' == typeof value && /^(0|[1-9]\d*)$/.test(value)) return value;
53
+ const number = void 0 === value ? fallback : value;
54
+ return isTimestamp(number) && Number.isSafeInteger(number) ? String(number) : void 0;
55
+ };
56
+ const compareRevisions = (left, right)=>{
57
+ if (left === right) return 0;
58
+ if (void 0 === left) return -1;
59
+ if (void 0 === right) return 1;
60
+ return left.length === right.length ? left > right ? 1 : -1 : left.length > right.length ? 1 : -1;
61
+ };
62
+ const incrementRevision = (revision)=>{
63
+ const digits = revision.split('');
64
+ for(let index = digits.length - 1; index >= 0; index--){
65
+ if ('9' !== digits[index]) {
66
+ digits[index] = String(Number(digits[index]) + 1);
67
+ return digits.join('');
68
+ }
69
+ digits[index] = '0';
70
+ }
71
+ return `1${digits.join('')}`;
72
+ };
73
+ const readSession = (value)=>{
74
+ if (!isRecord(value)) return;
75
+ const { sessionId, sessionStartTimestamp, lastActivityTimestamp } = value;
76
+ const revision = readRevision(value.revision, sessionStartTimestamp);
77
+ if ('string' != typeof sessionId || 0 === sessionId.length || !isTimestamp(sessionStartTimestamp) || !isTimestamp(lastActivityTimestamp) || !revision) return;
78
+ return {
79
+ sessionId,
80
+ sessionStartTimestamp,
81
+ lastActivityTimestamp,
82
+ revision
83
+ };
84
+ };
85
+ const readExtensionData = (value)=>{
86
+ const extensionData = emptyRecord();
87
+ if (isRecord(value)) Object.entries(value).forEach(([key, data])=>{
88
+ if (isRecord(data)) {
89
+ const namespaceData = emptyRecord();
90
+ Object.entries(data).forEach(([dataKey, dataValue])=>{
91
+ namespaceData[dataKey] = dataValue;
92
+ });
93
+ extensionData[key] = namespaceData;
94
+ }
95
+ });
96
+ return extensionData;
97
+ };
98
+ const createSession = (now, revision)=>({
99
+ sessionId: (0, external_id_js_namespaceObject.createId)(),
100
+ sessionStartTimestamp: now,
101
+ lastActivityTimestamp: now,
102
+ revision
103
+ });
104
+ const authorityRevision = (state)=>state?.session?.revision ?? state?.sessionReset;
105
+ const persistedAuthorityWins = (persisted, local)=>{
106
+ const persistedRevision = authorityRevision(persisted);
107
+ const localRevision = authorityRevision(local);
108
+ const comparison = compareRevisions(persistedRevision, localRevision);
109
+ if (0 !== comparison) return comparison > 0;
110
+ if (void 0 === persistedRevision) return false;
111
+ if (!persisted.session || !local.session) return !!persisted.session !== !!local.session;
112
+ return persisted.session.sessionId !== local.session.sessionId || persisted.session.lastActivityTimestamp > local.session.lastActivityTimestamp;
113
+ };
114
+ const copyAuthority = (target, source)=>{
115
+ delete target.session;
116
+ delete target.sessionReset;
117
+ if (source.session) target.session = source.session;
118
+ else if (void 0 !== source.sessionReset) target.sessionReset = source.sessionReset;
119
+ };
120
+ const nextRevision = (now, local, external)=>{
121
+ const localRevision = authorityRevision(local);
122
+ const externalRevision = authorityRevision(external);
123
+ const latest = compareRevisions(localRevision, externalRevision) >= 0 ? localRevision : externalRevision;
124
+ const incremented = latest ? incrementRevision(latest) : '0';
125
+ const clock = readRevision(now) ?? '0';
126
+ return compareRevisions(clock, incremented) > 0 ? clock : incremented;
127
+ };
128
+ const createInitialState = (deviceId = (0, external_id_js_namespaceObject.createId)(), anonymousId = deviceId)=>({
129
+ version: 1,
130
+ deviceId,
131
+ anonymousId,
132
+ distinctId: anonymousId,
133
+ isIdentified: false,
134
+ groups: emptyRecord(),
135
+ extensionData: emptyRecord()
136
+ });
137
+ const decodeConsent = (value)=>{
138
+ const normalized = 'string' == typeof value ? value.trim().toLowerCase() : value;
139
+ return true === normalized || 1 === normalized || '1' === normalized || 'true' === normalized || 'yes' === normalized ? 'granted' : false === normalized || 0 === normalized || '0' === normalized || 'false' === normalized || 'no' === normalized ? 'denied' : void 0;
140
+ };
141
+ const parseState = (value)=>{
142
+ if (!value) return;
143
+ try {
144
+ const parsed = JSON.parse(value);
145
+ if (!isRecord(parsed) || 1 !== parsed.version) return;
146
+ const anonymousId = parsed.anonymousId;
147
+ const distinctId = parsed.distinctId;
148
+ const deviceId = 'string' == typeof parsed.deviceId ? parsed.deviceId : anonymousId;
149
+ const hasSession = Object.prototype.hasOwnProperty.call(parsed, 'session');
150
+ const hasReset = Object.prototype.hasOwnProperty.call(parsed, 'sessionReset');
151
+ const session = readSession(parsed.session);
152
+ const sessionReset = readRevision(parsed.sessionReset);
153
+ if ('string' != typeof deviceId || 'string' != typeof anonymousId || 'string' != typeof distinctId || hasSession && !session || hasReset && !sessionReset || session && hasReset) return;
154
+ return {
155
+ version: 1,
156
+ deviceId,
157
+ anonymousId,
158
+ distinctId,
159
+ isIdentified: true === parsed.isIdentified || distinctId !== anonymousId,
160
+ groups: readGroups(parsed.groups),
161
+ ...session ? {
162
+ session
163
+ } : {},
164
+ ...sessionReset ? {
165
+ sessionReset
166
+ } : {},
167
+ extensionData: readExtensionData(parsed.extensionData)
168
+ };
169
+ } catch {
170
+ return;
171
+ }
172
+ };
173
+ const getDefaultStorage = ()=>{
174
+ try {
175
+ return globalThis.localStorage;
176
+ } catch {
177
+ return;
178
+ }
179
+ };
180
+ const getDefaultSessionStorage = ()=>{
181
+ try {
182
+ return globalThis.sessionStorage;
183
+ } catch {
184
+ return;
185
+ }
186
+ };
187
+ class BrowserState {
188
+ constructor(projectToken, storage, persistenceKey, consentPersistenceName, optOutByDefault, windowStorageFactory, onConsentChange){
189
+ this._windowId = '';
190
+ this._windowStorageInitialized = false;
191
+ this._windowListenerInstalled = false;
192
+ this._lastActivityWriteTimestamp = 0;
193
+ this._stateReadPending = false;
194
+ this._lastConsentValue = null;
195
+ this._hasIgnoredConsentValue = false;
196
+ this._beforeUnload = ()=>{
197
+ this._flushSession();
198
+ this._windowRemove(this._primaryWindowKey);
199
+ };
200
+ this._storage = storage;
201
+ this._windowStorageFactory = windowStorageFactory;
202
+ this._stateKey = persistenceKey ?? `ph_${projectToken}_posthog_browser_v2`;
203
+ const windowPrefix = persistenceKey ?? `ph_${projectToken}`;
204
+ this._windowKey = `${windowPrefix}_window_id`;
205
+ this._primaryWindowKey = `${windowPrefix}_primary_window_exists`;
206
+ this.consentKey = void 0 === consentPersistenceName ? `__ph_opt_in_out_${projectToken}` : consentPersistenceName;
207
+ this._defaultConsent = optOutByDefault ? 'denied' : 'implicit';
208
+ this._consent = this._defaultConsent;
209
+ this._onConsentChange = onConsentChange;
210
+ const [consentRead, storedConsent] = this._read(this.consentKey);
211
+ if (consentRead) {
212
+ this._lastConsentValue = storedConsent;
213
+ this._consent = decodeConsent(storedConsent) ?? this._defaultConsent;
214
+ }
215
+ const [stateRead, storedState] = this._read(this._stateKey);
216
+ this._state = parseState('string' == typeof storedState ? storedState : null) ?? createInitialState();
217
+ this._lastActivityWriteTimestamp = this._state.session?.lastActivityTimestamp ?? 0;
218
+ this._stateReadPending = !stateRead;
219
+ if (stateRead) this._save();
220
+ }
221
+ prepare() {
222
+ if (!this._stateReadPending) return true;
223
+ const [read, storedState] = this._read(this._stateKey);
224
+ if (!read) return false;
225
+ this._stateReadPending = false;
226
+ const persisted = parseState('string' == typeof storedState ? storedState : null);
227
+ if (persisted) {
228
+ this._state = persisted;
229
+ this._lastActivityWriteTimestamp = persisted.session?.lastActivityTimestamp ?? 0;
230
+ } else this._save(false);
231
+ return true;
232
+ }
233
+ get deviceId() {
234
+ return this._state.deviceId;
235
+ }
236
+ get anonymousId() {
237
+ return this._state.anonymousId;
238
+ }
239
+ get distinctId() {
240
+ return this._state.distinctId;
241
+ }
242
+ get isIdentified() {
243
+ return this._state.isIdentified;
244
+ }
245
+ get groups() {
246
+ return {
247
+ ...this._state.groups
248
+ };
249
+ }
250
+ get session() {
251
+ const session = this._state.session;
252
+ if (!session || !this._windowId) return;
253
+ return {
254
+ sessionId: session.sessionId,
255
+ windowId: this._windowId,
256
+ sessionStartTimestamp: session.sessionStartTimestamp,
257
+ lastActivityTimestamp: session.lastActivityTimestamp
258
+ };
259
+ }
260
+ get consent() {
261
+ return this.refreshConsent();
262
+ }
263
+ refreshConsent() {
264
+ const [read, value] = this._read(this.consentKey);
265
+ return read ? this.observeConsent(value) : this._consent;
266
+ }
267
+ observeConsent(value) {
268
+ if (this._hasIgnoredConsentValue && value === this._ignoredConsentValue) return this._consent;
269
+ this._hasIgnoredConsentValue = false;
270
+ this._lastConsentValue = value;
271
+ this._applyConsent(decodeConsent(value) ?? this._defaultConsent);
272
+ return this._consent;
273
+ }
274
+ identify(distinctId) {
275
+ this._state.distinctId = distinctId;
276
+ this._state.isIdentified = true;
277
+ this._save();
278
+ }
279
+ group(type, key) {
280
+ if (this._state.groups[type] === key) return false;
281
+ this._state.groups[type] = key;
282
+ this._save();
283
+ return true;
284
+ }
285
+ prepareSessionForEvent(now = Date.now()) {
286
+ const safeNow = isTimestamp(now) ? now : 0;
287
+ const [sharedRead, external] = this._readPersistedState();
288
+ let session = this._state.session;
289
+ let lastActivityWriteTimestamp = this._lastActivityWriteTimestamp;
290
+ let adoptedAfterReset = false;
291
+ if (sharedRead && external && persistedAuthorityWins(external, this._state)) {
292
+ session = external.session;
293
+ if (session) {
294
+ lastActivityWriteTimestamp = session.lastActivityTimestamp;
295
+ adoptedAfterReset = 'reset' === this._pendingSessionReason;
296
+ }
297
+ }
298
+ const pendingReason = this._pendingSessionReason;
299
+ const idle = session ? Math.abs(safeNow - session.lastActivityTimestamp) > 1800000 : false;
300
+ const maximum = session ? Math.abs(safeNow - session.sessionStartTimestamp) > 86400000 : false;
301
+ const rotated = !!(pendingReason || !session || idle || maximum);
302
+ const reason = adoptedAfterReset ? 'reset' : pendingReason ?? (idle ? 'idleTimeout' : maximum ? 'maxLength' : void 0);
303
+ const preparedSession = rotated ? createSession(safeNow, nextRevision(safeNow, this._state, external)) : {
304
+ ...session,
305
+ lastActivityTimestamp: Math.max(safeNow, session.lastActivityTimestamp)
306
+ };
307
+ const window = this._prepareWindow(rotated);
308
+ return {
309
+ context: {
310
+ sessionId: preparedSession.sessionId,
311
+ windowId: window.windowId,
312
+ sessionStartTimestamp: preparedSession.sessionStartTimestamp,
313
+ lastActivityTimestamp: preparedSession.lastActivityTimestamp
314
+ },
315
+ session: preparedSession,
316
+ reason,
317
+ rotated,
318
+ lastActivityWriteTimestamp,
319
+ windowStorage: window.storage,
320
+ windowStorageResolved: window.storageResolved,
321
+ previousWindowId: this._windowId,
322
+ previousWindowStorageInitialized: this._windowStorageInitialized,
323
+ previousPendingReason: this._pendingSessionReason
324
+ };
325
+ }
326
+ sessionAdmitted(prepared) {
327
+ const [, external] = this._readPersistedState();
328
+ const preparedState = {
329
+ ...this._state,
330
+ session: prepared.session
331
+ };
332
+ delete preparedState.sessionReset;
333
+ if (external && persistedAuthorityWins(external, preparedState)) return false;
334
+ this._state.session = prepared.session;
335
+ delete this._state.sessionReset;
336
+ this._pendingSessionReason = void 0;
337
+ this._windowId = prepared.context.windowId;
338
+ this._initializeWindowStorage(prepared.windowStorage, prepared.windowStorageResolved);
339
+ this._setWindowId(prepared.context.windowId);
340
+ if (prepared.rotated) {
341
+ this._lastActivityWriteTimestamp = prepared.session.lastActivityTimestamp;
342
+ this._save();
343
+ } else if (Math.abs(prepared.session.lastActivityTimestamp - prepared.lastActivityWriteTimestamp) >= 60000) {
344
+ this._lastActivityWriteTimestamp = prepared.session.lastActivityTimestamp;
345
+ this._save();
346
+ } else this._lastActivityWriteTimestamp = prepared.lastActivityWriteTimestamp;
347
+ const [, committed] = this._readPersistedState();
348
+ if (committed && persistedAuthorityWins(committed, preparedState)) {
349
+ copyAuthority(this._state, committed);
350
+ this._pendingSessionReason = prepared.previousPendingReason;
351
+ this._lastActivityWriteTimestamp = committed.session?.lastActivityTimestamp ?? 0;
352
+ if (prepared.previousWindowStorageInitialized) this._setWindowId(prepared.previousWindowId);
353
+ else this._clearWindow(true);
354
+ return false;
355
+ }
356
+ return true;
357
+ }
358
+ reset() {
359
+ const [, external] = this._readPersistedState();
360
+ const now = Date.now();
361
+ const safeNow = isTimestamp(now) ? now : 0;
362
+ const resetRevision = nextRevision(safeNow, this._state, external);
363
+ this._state = createInitialState(this._state.deviceId, (0, external_id_js_namespaceObject.createId)());
364
+ this._state.sessionReset = resetRevision;
365
+ this._lastActivityWriteTimestamp = 0;
366
+ this._pendingSessionReason = 'reset';
367
+ this._clearWindow(true);
368
+ this._save(false);
369
+ }
370
+ dispose() {
371
+ this._flushSession();
372
+ this._removeWindowListener();
373
+ this._windowRemove(this._primaryWindowKey);
374
+ }
375
+ optIn() {
376
+ this._applyConsent('granted');
377
+ const written = this._writeConsent('1');
378
+ this._save();
379
+ return written;
380
+ }
381
+ optOut() {
382
+ this._applyConsent('denied');
383
+ return this._writeConsent('0');
384
+ }
385
+ keyValueStore(namespace, canAccess = ()=>true) {
386
+ const values = ()=>this._state.extensionData[namespace] ??= emptyRecord();
387
+ const read = (key)=>{
388
+ if (!canAccess()) return;
389
+ const value = values()[key];
390
+ return void 0 === value ? void 0 : cloneJson(value);
391
+ };
392
+ return {
393
+ initialize () {},
394
+ get: (keyOrKeys)=>{
395
+ if ('string' == typeof keyOrKeys) return read(keyOrKeys);
396
+ const entries = emptyRecord();
397
+ for (const key of keyOrKeys){
398
+ const value = read(key);
399
+ if (void 0 !== value) entries[key] = value;
400
+ }
401
+ return entries;
402
+ },
403
+ set: (keyOrValues, value)=>{
404
+ if (!canAccess()) return;
405
+ const namespaceValues = values();
406
+ const entries = 'string' == typeof keyOrValues ? {
407
+ [keyOrValues]: value
408
+ } : keyOrValues;
409
+ for (const [key, entry] of Object.entries(entries))if (void 0 === entry) delete namespaceValues[key];
410
+ else namespaceValues[key] = cloneJson(entry);
411
+ this._save();
412
+ },
413
+ remove: (keyOrKeys)=>{
414
+ if (!canAccess()) return;
415
+ const namespaceValues = values();
416
+ for (const key of 'string' == typeof keyOrKeys ? [
417
+ keyOrKeys
418
+ ] : keyOrKeys)delete namespaceValues[key];
419
+ this._save();
420
+ }
421
+ };
422
+ }
423
+ _readPersistedState() {
424
+ if (!this._storage) return [
425
+ false
426
+ ];
427
+ const [read, value] = this._read(this._stateKey);
428
+ if (!read) return [
429
+ false
430
+ ];
431
+ if (null === value) return [
432
+ true
433
+ ];
434
+ if ('string' != typeof value) return [
435
+ false
436
+ ];
437
+ const state = parseState(value);
438
+ return state ? [
439
+ true,
440
+ state
441
+ ] : [
442
+ false
443
+ ];
444
+ }
445
+ _prepareWindow(rotated) {
446
+ if (rotated || this._windowId) return {
447
+ windowId: rotated ? (0, external_id_js_namespaceObject.createId)() : this._windowId,
448
+ storage: void 0,
449
+ storageResolved: false
450
+ };
451
+ let storage;
452
+ try {
453
+ storage = this._windowStorageFactory?.();
454
+ } catch {
455
+ storage = void 0;
456
+ }
457
+ let lastWindowId = null;
458
+ let primaryWindowExists = null;
459
+ try {
460
+ lastWindowId = storage?.getItem(this._windowKey) ?? null;
461
+ } catch {}
462
+ try {
463
+ primaryWindowExists = storage?.getItem(this._primaryWindowKey) ?? null;
464
+ } catch {}
465
+ const reusable = lastWindowId && (!primaryWindowExists || this._isReload()) ? lastWindowId : void 0;
466
+ return {
467
+ windowId: reusable ?? (0, external_id_js_namespaceObject.createId)(),
468
+ storage,
469
+ storageResolved: true
470
+ };
471
+ }
472
+ _initializeWindowStorage(preparedStorage, storageResolved = false) {
473
+ if (this._windowStorageInitialized) return;
474
+ this._windowStorageInitialized = true;
475
+ if (storageResolved) this._windowStorage = preparedStorage;
476
+ else try {
477
+ this._windowStorage = this._windowStorageFactory?.();
478
+ } catch {
479
+ this._windowStorage = void 0;
480
+ }
481
+ const lastWindowId = this._windowRead(this._windowKey);
482
+ const primaryWindowExists = this._windowRead(this._primaryWindowKey);
483
+ if (lastWindowId && (!primaryWindowExists || this._isReload())) this._windowId = lastWindowId;
484
+ else if (primaryWindowExists) this._windowRemove(this._windowKey);
485
+ if (!this._windowStorage) return;
486
+ try {
487
+ globalThis.addEventListener('beforeunload', this._beforeUnload);
488
+ this._windowListenerInstalled = true;
489
+ } catch {}
490
+ try {
491
+ globalThis.addEventListener('unload', this._beforeUnload);
492
+ this._windowListenerInstalled = true;
493
+ } catch {}
494
+ if (this._windowListenerInstalled) this._windowSet(this._primaryWindowKey, '1');
495
+ else {
496
+ this._windowRemove(this._windowKey);
497
+ this._windowRemove(this._primaryWindowKey);
498
+ this._windowStorage = void 0;
499
+ }
500
+ }
501
+ _isReload() {
502
+ try {
503
+ return globalThis.performance?.getEntriesByType('navigation')[0]?.type === 'reload';
504
+ } catch {
505
+ return false;
506
+ }
507
+ }
508
+ _setWindowId(windowId) {
509
+ this._windowId = windowId;
510
+ this._windowSet(this._windowKey, windowId);
511
+ }
512
+ _clearWindow(removeWindow) {
513
+ this._removeWindowListener();
514
+ if (removeWindow) this._windowRemove(this._windowKey);
515
+ this._windowRemove(this._primaryWindowKey);
516
+ this._windowStorage = void 0;
517
+ this._windowStorageInitialized = false;
518
+ this._windowId = '';
519
+ }
520
+ _removeWindowListener() {
521
+ if (!this._windowListenerInstalled) return;
522
+ for (const event of [
523
+ 'beforeunload',
524
+ 'unload'
525
+ ])try {
526
+ globalThis.removeEventListener(event, this._beforeUnload);
527
+ } catch {}
528
+ this._windowListenerInstalled = false;
529
+ }
530
+ _flushSession() {
531
+ const session = this._state.session;
532
+ if (!session || session.lastActivityTimestamp === this._lastActivityWriteTimestamp) return;
533
+ const [sharedRead, sharedState] = this._readPersistedState();
534
+ const shared = sharedState?.session;
535
+ if (sharedRead && (!shared || shared.revision !== session.revision || shared.sessionId !== session.sessionId || shared.sessionStartTimestamp !== session.sessionStartTimestamp || shared.lastActivityTimestamp > session.lastActivityTimestamp)) return;
536
+ this._lastActivityWriteTimestamp = session.lastActivityTimestamp;
537
+ this._save(false);
538
+ }
539
+ _windowRead(key) {
540
+ try {
541
+ return this._windowStorage?.getItem(key) ?? null;
542
+ } catch {
543
+ return null;
544
+ }
545
+ }
546
+ _windowSet(key, value) {
547
+ try {
548
+ this._windowStorage?.setItem(key, value);
549
+ } catch {}
550
+ }
551
+ _windowRemove(key) {
552
+ try {
553
+ this._windowStorage?.removeItem(key);
554
+ } catch {}
555
+ }
556
+ _applyConsent(consent) {
557
+ if (consent === this._consent) return;
558
+ const previousConsent = this._consent;
559
+ this._consent = consent;
560
+ try {
561
+ this._onConsentChange(consent, previousConsent);
562
+ } catch {}
563
+ }
564
+ _save(preserveSession = true) {
565
+ if (!this.prepare()) return;
566
+ let state = this._state;
567
+ if (preserveSession) {
568
+ const [read, persisted] = this._readPersistedState();
569
+ if (read && persisted && persistedAuthorityWins(persisted, state)) {
570
+ state = {
571
+ ...state
572
+ };
573
+ copyAuthority(state, persisted);
574
+ }
575
+ }
576
+ try {
577
+ this._storage?.setItem(this._stateKey, JSON.stringify(state));
578
+ } catch {}
579
+ }
580
+ _writeConsent(value) {
581
+ if (!this._storage) {
582
+ this._ignoredConsentValue = this._lastConsentValue;
583
+ this._hasIgnoredConsentValue = true;
584
+ return false;
585
+ }
586
+ try {
587
+ this._storage.setItem(this.consentKey, value);
588
+ this._lastConsentValue = value;
589
+ this._hasIgnoredConsentValue = false;
590
+ return true;
591
+ } catch {
592
+ this._ignoredConsentValue = this._lastConsentValue;
593
+ this._hasIgnoredConsentValue = true;
594
+ return false;
595
+ }
596
+ }
597
+ _read(key) {
598
+ if (!this._storage) return [
599
+ true,
600
+ null
601
+ ];
602
+ try {
603
+ return [
604
+ true,
605
+ this._storage.getItem(key)
606
+ ];
607
+ } catch {
608
+ return [
609
+ false,
610
+ null
611
+ ];
612
+ }
613
+ }
614
+ }
615
+ exports.BrowserState = __webpack_exports__.BrowserState;
616
+ exports.getDefaultSessionStorage = __webpack_exports__.getDefaultSessionStorage;
617
+ exports.getDefaultStorage = __webpack_exports__.getDefaultStorage;
618
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
619
+ "BrowserState",
620
+ "getDefaultSessionStorage",
621
+ "getDefaultStorage"
622
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
623
+ Object.defineProperty(exports, '__esModule', {
624
+ value: true
625
+ });