@leanbase.com/js 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.
@@ -0,0 +1,1789 @@
1
+ var leanbase = (function () {
2
+ 'use strict';
3
+
4
+ const normalizeFlagsResponse = (flagsResponse)=>{
5
+ if ('flags' in flagsResponse) {
6
+ const featureFlags = getFlagValuesFromFlags(flagsResponse.flags);
7
+ const featureFlagPayloads = getPayloadsFromFlags(flagsResponse.flags);
8
+ return {
9
+ ...flagsResponse,
10
+ featureFlags,
11
+ featureFlagPayloads
12
+ };
13
+ }
14
+ {
15
+ const featureFlags = flagsResponse.featureFlags ?? {};
16
+ const featureFlagPayloads = Object.fromEntries(Object.entries(flagsResponse.featureFlagPayloads || {}).map(([k, v])=>[
17
+ k,
18
+ parsePayload(v)
19
+ ]));
20
+ const flags = Object.fromEntries(Object.entries(featureFlags).map(([key, value])=>[
21
+ key,
22
+ getFlagDetailFromFlagAndPayload(key, value, featureFlagPayloads[key])
23
+ ]));
24
+ return {
25
+ ...flagsResponse,
26
+ featureFlags,
27
+ featureFlagPayloads,
28
+ flags
29
+ };
30
+ }
31
+ };
32
+ function getFlagDetailFromFlagAndPayload(key, value, payload) {
33
+ return {
34
+ key: key,
35
+ enabled: 'string' == typeof value ? true : value,
36
+ variant: 'string' == typeof value ? value : void 0,
37
+ reason: void 0,
38
+ metadata: {
39
+ id: void 0,
40
+ version: void 0,
41
+ payload: payload ? JSON.stringify(payload) : void 0,
42
+ description: void 0
43
+ }
44
+ };
45
+ }
46
+ const getFlagValuesFromFlags = (flags)=>Object.fromEntries(Object.entries(flags ?? {}).map(([key, detail])=>[
47
+ key,
48
+ getFeatureFlagValue(detail)
49
+ ]).filter(([, value])=>void 0 !== value));
50
+ const getPayloadsFromFlags = (flags)=>{
51
+ const safeFlags = flags ?? {};
52
+ return Object.fromEntries(Object.keys(safeFlags).filter((flag)=>{
53
+ const details = safeFlags[flag];
54
+ return details.enabled && details.metadata && void 0 !== details.metadata.payload;
55
+ }).map((flag)=>{
56
+ const payload = safeFlags[flag].metadata?.payload;
57
+ return [
58
+ flag,
59
+ payload ? parsePayload(payload) : void 0
60
+ ];
61
+ }));
62
+ };
63
+ const getFeatureFlagValue = (detail)=>void 0 === detail ? void 0 : detail.variant ?? detail.enabled;
64
+ const parsePayload = (response)=>{
65
+ if ('string' != typeof response) return response;
66
+ try {
67
+ return JSON.parse(response);
68
+ } catch {
69
+ return response;
70
+ }
71
+ };
72
+ const createFlagsResponseFromFlagsAndPayloads = (featureFlags, featureFlagPayloads)=>{
73
+ const allKeys = [
74
+ ...new Set([
75
+ ...Object.keys(featureFlags ?? {}),
76
+ ...Object.keys(featureFlagPayloads ?? {})
77
+ ])
78
+ ];
79
+ const enabledFlags = allKeys.filter((flag)=>!!featureFlags[flag] || !!featureFlagPayloads[flag]).reduce((res, key)=>(res[key] = featureFlags[key] ?? true, res), {});
80
+ const flagDetails = {
81
+ featureFlags: enabledFlags,
82
+ featureFlagPayloads: featureFlagPayloads ?? {}
83
+ };
84
+ return normalizeFlagsResponse(flagDetails);
85
+ };
86
+ const updateFlagValue = (flag, value)=>({
87
+ ...flag,
88
+ enabled: getEnabledFromValue(value),
89
+ variant: getVariantFromValue(value)
90
+ });
91
+ function getEnabledFromValue(value) {
92
+ return 'string' == typeof value ? true : value;
93
+ }
94
+ function getVariantFromValue(value) {
95
+ return 'string' == typeof value ? value : void 0;
96
+ }
97
+
98
+ /*! For license information please see uuidv7.mjs.LICENSE.txt */
99
+ /**
100
+ * uuidv7: An experimental implementation of the proposed UUID Version 7
101
+ *
102
+ * @license Apache-2.0
103
+ * @copyright 2021-2023 LiosK
104
+ * @packageDocumentation
105
+ */ const DIGITS = "0123456789abcdef";
106
+ class UUID {
107
+ constructor(bytes){
108
+ this.bytes = bytes;
109
+ }
110
+ static ofInner(bytes) {
111
+ if (16 === bytes.length) return new UUID(bytes);
112
+ throw new TypeError("not 128-bit length");
113
+ }
114
+ static fromFieldsV7(unixTsMs, randA, randBHi, randBLo) {
115
+ if (!Number.isInteger(unixTsMs) || !Number.isInteger(randA) || !Number.isInteger(randBHi) || !Number.isInteger(randBLo) || unixTsMs < 0 || randA < 0 || randBHi < 0 || randBLo < 0 || unixTsMs > 0xffffffffffff || randA > 0xfff || randBHi > 0x3fffffff || randBLo > 0xffffffff) throw new RangeError("invalid field value");
116
+ const bytes = new Uint8Array(16);
117
+ bytes[0] = unixTsMs / 2 ** 40;
118
+ bytes[1] = unixTsMs / 2 ** 32;
119
+ bytes[2] = unixTsMs / 2 ** 24;
120
+ bytes[3] = unixTsMs / 2 ** 16;
121
+ bytes[4] = unixTsMs / 256;
122
+ bytes[5] = unixTsMs;
123
+ bytes[6] = 0x70 | randA >>> 8;
124
+ bytes[7] = randA;
125
+ bytes[8] = 0x80 | randBHi >>> 24;
126
+ bytes[9] = randBHi >>> 16;
127
+ bytes[10] = randBHi >>> 8;
128
+ bytes[11] = randBHi;
129
+ bytes[12] = randBLo >>> 24;
130
+ bytes[13] = randBLo >>> 16;
131
+ bytes[14] = randBLo >>> 8;
132
+ bytes[15] = randBLo;
133
+ return new UUID(bytes);
134
+ }
135
+ static parse(uuid) {
136
+ let hex;
137
+ switch(uuid.length){
138
+ case 32:
139
+ hex = /^[0-9a-f]{32}$/i.exec(uuid)?.[0];
140
+ break;
141
+ case 36:
142
+ hex = /^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)?.slice(1, 6).join("");
143
+ break;
144
+ case 38:
145
+ hex = /^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i.exec(uuid)?.slice(1, 6).join("");
146
+ break;
147
+ case 45:
148
+ hex = /^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i.exec(uuid)?.slice(1, 6).join("");
149
+ break;
150
+ }
151
+ if (hex) {
152
+ const inner = new Uint8Array(16);
153
+ for(let i = 0; i < 16; i += 4){
154
+ const n = parseInt(hex.substring(2 * i, 2 * i + 8), 16);
155
+ inner[i + 0] = n >>> 24;
156
+ inner[i + 1] = n >>> 16;
157
+ inner[i + 2] = n >>> 8;
158
+ inner[i + 3] = n;
159
+ }
160
+ return new UUID(inner);
161
+ }
162
+ throw new SyntaxError("could not parse UUID string");
163
+ }
164
+ toString() {
165
+ let text = "";
166
+ for(let i = 0; i < this.bytes.length; i++){
167
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
168
+ text += DIGITS.charAt(0xf & this.bytes[i]);
169
+ if (3 === i || 5 === i || 7 === i || 9 === i) text += "-";
170
+ }
171
+ return text;
172
+ }
173
+ toHex() {
174
+ let text = "";
175
+ for(let i = 0; i < this.bytes.length; i++){
176
+ text += DIGITS.charAt(this.bytes[i] >>> 4);
177
+ text += DIGITS.charAt(0xf & this.bytes[i]);
178
+ }
179
+ return text;
180
+ }
181
+ toJSON() {
182
+ return this.toString();
183
+ }
184
+ getVariant() {
185
+ const n = this.bytes[8] >>> 4;
186
+ if (n < 0) throw new Error("unreachable");
187
+ if (n <= 7) return this.bytes.every((e)=>0 === e) ? "NIL" : "VAR_0";
188
+ if (n <= 11) return "VAR_10";
189
+ if (n <= 13) return "VAR_110";
190
+ if (n <= 15) return this.bytes.every((e)=>0xff === e) ? "MAX" : "VAR_RESERVED";
191
+ else throw new Error("unreachable");
192
+ }
193
+ getVersion() {
194
+ return "VAR_10" === this.getVariant() ? this.bytes[6] >>> 4 : void 0;
195
+ }
196
+ clone() {
197
+ return new UUID(this.bytes.slice(0));
198
+ }
199
+ equals(other) {
200
+ return 0 === this.compareTo(other);
201
+ }
202
+ compareTo(other) {
203
+ for(let i = 0; i < 16; i++){
204
+ const diff = this.bytes[i] - other.bytes[i];
205
+ if (0 !== diff) return Math.sign(diff);
206
+ }
207
+ return 0;
208
+ }
209
+ }
210
+ class V7Generator {
211
+ constructor(randomNumberGenerator){
212
+ this.timestamp = 0;
213
+ this.counter = 0;
214
+ this.random = randomNumberGenerator ?? getDefaultRandom();
215
+ }
216
+ generate() {
217
+ return this.generateOrResetCore(Date.now(), 10000);
218
+ }
219
+ generateOrAbort() {
220
+ return this.generateOrAbortCore(Date.now(), 10000);
221
+ }
222
+ generateOrResetCore(unixTsMs, rollbackAllowance) {
223
+ let value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
224
+ if (void 0 === value) {
225
+ this.timestamp = 0;
226
+ value = this.generateOrAbortCore(unixTsMs, rollbackAllowance);
227
+ }
228
+ return value;
229
+ }
230
+ generateOrAbortCore(unixTsMs, rollbackAllowance) {
231
+ const MAX_COUNTER = 0x3ffffffffff;
232
+ if (!Number.isInteger(unixTsMs) || unixTsMs < 1 || unixTsMs > 0xffffffffffff) throw new RangeError("`unixTsMs` must be a 48-bit positive integer");
233
+ if (rollbackAllowance < 0 || rollbackAllowance > 0xffffffffffff) throw new RangeError("`rollbackAllowance` out of reasonable range");
234
+ if (unixTsMs > this.timestamp) {
235
+ this.timestamp = unixTsMs;
236
+ this.resetCounter();
237
+ } else {
238
+ if (!(unixTsMs + rollbackAllowance >= this.timestamp)) return;
239
+ this.counter++;
240
+ if (this.counter > MAX_COUNTER) {
241
+ this.timestamp++;
242
+ this.resetCounter();
243
+ }
244
+ }
245
+ return UUID.fromFieldsV7(this.timestamp, Math.trunc(this.counter / 2 ** 30), this.counter & 2 ** 30 - 1, this.random.nextUint32());
246
+ }
247
+ resetCounter() {
248
+ this.counter = 0x400 * this.random.nextUint32() + (0x3ff & this.random.nextUint32());
249
+ }
250
+ generateV4() {
251
+ const bytes = new Uint8Array(Uint32Array.of(this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32(), this.random.nextUint32()).buffer);
252
+ bytes[6] = 0x40 | bytes[6] >>> 4;
253
+ bytes[8] = 0x80 | bytes[8] >>> 2;
254
+ return UUID.ofInner(bytes);
255
+ }
256
+ }
257
+ const getDefaultRandom = ()=>({
258
+ nextUint32: ()=>0x10000 * Math.trunc(0x10000 * Math.random()) + Math.trunc(0x10000 * Math.random())
259
+ });
260
+ let defaultGenerator;
261
+ const uuidv7 = ()=>uuidv7obj().toString();
262
+ const uuidv7obj = ()=>(defaultGenerator || (defaultGenerator = new V7Generator())).generate();
263
+
264
+ var types_PostHogPersistedProperty = /*#__PURE__*/ function(PostHogPersistedProperty) {
265
+ PostHogPersistedProperty["AnonymousId"] = "anonymous_id";
266
+ PostHogPersistedProperty["DistinctId"] = "distinct_id";
267
+ PostHogPersistedProperty["Props"] = "props";
268
+ PostHogPersistedProperty["FeatureFlagDetails"] = "feature_flag_details";
269
+ PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
270
+ PostHogPersistedProperty["FeatureFlagPayloads"] = "feature_flag_payloads";
271
+ PostHogPersistedProperty["BootstrapFeatureFlagDetails"] = "bootstrap_feature_flag_details";
272
+ PostHogPersistedProperty["BootstrapFeatureFlags"] = "bootstrap_feature_flags";
273
+ PostHogPersistedProperty["BootstrapFeatureFlagPayloads"] = "bootstrap_feature_flag_payloads";
274
+ PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
275
+ PostHogPersistedProperty["Queue"] = "queue";
276
+ PostHogPersistedProperty["OptedOut"] = "opted_out";
277
+ PostHogPersistedProperty["SessionId"] = "session_id";
278
+ PostHogPersistedProperty["SessionStartTimestamp"] = "session_start_timestamp";
279
+ PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
280
+ PostHogPersistedProperty["PersonProperties"] = "person_properties";
281
+ PostHogPersistedProperty["GroupProperties"] = "group_properties";
282
+ PostHogPersistedProperty["InstalledAppBuild"] = "installed_app_build";
283
+ PostHogPersistedProperty["InstalledAppVersion"] = "installed_app_version";
284
+ PostHogPersistedProperty["SessionReplay"] = "session_replay";
285
+ PostHogPersistedProperty["SurveyLastSeenDate"] = "survey_last_seen_date";
286
+ PostHogPersistedProperty["SurveysSeen"] = "surveys_seen";
287
+ PostHogPersistedProperty["Surveys"] = "surveys";
288
+ PostHogPersistedProperty["RemoteConfig"] = "remote_config";
289
+ PostHogPersistedProperty["FlagsEndpointWasHit"] = "flags_endpoint_was_hit";
290
+ return PostHogPersistedProperty;
291
+ }({});
292
+ var types_Compression = /*#__PURE__*/ function(Compression) {
293
+ Compression["GZipJS"] = "gzip-js";
294
+ Compression["Base64"] = "base64";
295
+ return Compression;
296
+ }({});
297
+
298
+ const nativeIsArray = Array.isArray;
299
+ const ObjProto = Object.prototype;
300
+ const type_utils_toString = ObjProto.toString;
301
+ const isArray = nativeIsArray || function(obj) {
302
+ return '[object Array]' === type_utils_toString.call(obj);
303
+ };
304
+ const isFunction = (x)=>'function' == typeof x;
305
+ const isNull = (x)=>null === x;
306
+ const isPlainError = (x)=>x instanceof Error;
307
+
308
+ class PromiseQueue {
309
+ add(promise) {
310
+ const promiseUUID = uuidv7();
311
+ this.promiseByIds[promiseUUID] = promise;
312
+ promise.catch(()=>{}).finally(()=>{
313
+ delete this.promiseByIds[promiseUUID];
314
+ });
315
+ return promise;
316
+ }
317
+ async join() {
318
+ let promises = Object.values(this.promiseByIds);
319
+ let length = promises.length;
320
+ while(length > 0){
321
+ await Promise.all(promises);
322
+ promises = Object.values(this.promiseByIds);
323
+ length = promises.length;
324
+ }
325
+ }
326
+ get length() {
327
+ return Object.keys(this.promiseByIds).length;
328
+ }
329
+ constructor(){
330
+ this.promiseByIds = {};
331
+ }
332
+ }
333
+
334
+ const STRING_FORMAT = 'utf8';
335
+ function assert(truthyValue, message) {
336
+ if (!truthyValue || 'string' != typeof truthyValue || isEmpty(truthyValue)) throw new Error(message);
337
+ }
338
+ function isEmpty(truthyValue) {
339
+ if (0 === truthyValue.trim().length) return true;
340
+ return false;
341
+ }
342
+ function removeTrailingSlash(url) {
343
+ return url?.replace(/\/+$/, '');
344
+ }
345
+ async function retriable(fn, props) {
346
+ let lastError = null;
347
+ for(let i = 0; i < props.retryCount + 1; i++){
348
+ if (i > 0) await new Promise((r)=>setTimeout(r, props.retryDelay));
349
+ try {
350
+ const res = await fn();
351
+ return res;
352
+ } catch (e) {
353
+ lastError = e;
354
+ if (!props.retryCheck(e)) throw e;
355
+ }
356
+ }
357
+ throw lastError;
358
+ }
359
+ function currentISOTime() {
360
+ return new Date().toISOString();
361
+ }
362
+ function safeSetTimeout(fn, timeout) {
363
+ const t = setTimeout(fn, timeout);
364
+ t?.unref && t?.unref();
365
+ return t;
366
+ }
367
+ function getFetch() {
368
+ return 'undefined' != typeof fetch ? fetch : void 0 !== globalThis.fetch ? globalThis.fetch : void 0;
369
+ }
370
+ function allSettled(promises) {
371
+ return Promise.all(promises.map((p)=>(p ?? Promise.resolve()).then((value)=>({
372
+ status: 'fulfilled',
373
+ value
374
+ }), (reason)=>({
375
+ status: 'rejected',
376
+ reason
377
+ }))));
378
+ }
379
+
380
+ class SimpleEventEmitter {
381
+ constructor(){
382
+ this.events = {};
383
+ this.events = {};
384
+ }
385
+ on(event, listener) {
386
+ if (!this.events[event]) this.events[event] = [];
387
+ this.events[event].push(listener);
388
+ return ()=>{
389
+ this.events[event] = this.events[event].filter((x)=>x !== listener);
390
+ };
391
+ }
392
+ emit(event, payload) {
393
+ for (const listener of this.events[event] || [])listener(payload);
394
+ for (const listener of this.events['*'] || [])listener(event, payload);
395
+ }
396
+ }
397
+
398
+ function isGzipSupported() {
399
+ return 'CompressionStream' in globalThis;
400
+ }
401
+ async function gzipCompress(input, isDebug = true) {
402
+ try {
403
+ const dataStream = new Blob([
404
+ input
405
+ ], {
406
+ type: 'text/plain'
407
+ }).stream();
408
+ const compressedStream = dataStream.pipeThrough(new CompressionStream('gzip'));
409
+ return await new Response(compressedStream).blob();
410
+ } catch (error) {
411
+ if (isDebug) console.error('Failed to gzip compress data', error);
412
+ return null;
413
+ }
414
+ }
415
+
416
+ function createConsole(consoleLike = console) {
417
+ const lockedMethods = {
418
+ log: consoleLike.log.bind(consoleLike),
419
+ warn: consoleLike.warn.bind(consoleLike),
420
+ error: consoleLike.error.bind(consoleLike),
421
+ debug: consoleLike.debug.bind(consoleLike)
422
+ };
423
+ return lockedMethods;
424
+ }
425
+ const _createLogger = (prefix, maybeCall, consoleLike)=>{
426
+ function _log(level, ...args) {
427
+ maybeCall(()=>{
428
+ const consoleMethod = consoleLike[level];
429
+ consoleMethod(prefix, ...args);
430
+ });
431
+ }
432
+ const logger = {
433
+ info: (...args)=>{
434
+ _log('log', ...args);
435
+ },
436
+ warn: (...args)=>{
437
+ _log('warn', ...args);
438
+ },
439
+ error: (...args)=>{
440
+ _log('error', ...args);
441
+ },
442
+ critical: (...args)=>{
443
+ consoleLike['error'](prefix, ...args);
444
+ },
445
+ createLogger: (additionalPrefix)=>_createLogger(`${prefix} ${additionalPrefix}`, maybeCall, consoleLike)
446
+ };
447
+ return logger;
448
+ };
449
+ function createLogger(prefix, maybeCall) {
450
+ return _createLogger(prefix, maybeCall, createConsole());
451
+ }
452
+
453
+ class PostHogFetchHttpError extends Error {
454
+ constructor(response, reqByteLength){
455
+ super('HTTP error while fetching PostHog: status=' + response.status + ', reqByteLength=' + reqByteLength), this.response = response, this.reqByteLength = reqByteLength, this.name = 'PostHogFetchHttpError';
456
+ }
457
+ get status() {
458
+ return this.response.status;
459
+ }
460
+ get text() {
461
+ return this.response.text();
462
+ }
463
+ get json() {
464
+ return this.response.json();
465
+ }
466
+ }
467
+ class PostHogFetchNetworkError extends Error {
468
+ constructor(error){
469
+ super('Network error while fetching PostHog', error instanceof Error ? {
470
+ cause: error
471
+ } : {}), this.error = error, this.name = 'PostHogFetchNetworkError';
472
+ }
473
+ }
474
+ const maybeAdd = (key, value)=>void 0 !== value ? {
475
+ [key]: value
476
+ } : {};
477
+ async function logFlushError(err) {
478
+ if (err instanceof PostHogFetchHttpError) {
479
+ let text = '';
480
+ try {
481
+ text = await err.text;
482
+ } catch {}
483
+ console.error(`Error while flushing PostHog: message=${err.message}, response body=${text}`, err);
484
+ } else console.error('Error while flushing PostHog', err);
485
+ return Promise.resolve();
486
+ }
487
+ function isPostHogFetchError(err) {
488
+ return 'object' == typeof err && (err instanceof PostHogFetchHttpError || err instanceof PostHogFetchNetworkError);
489
+ }
490
+ function isPostHogFetchContentTooLargeError(err) {
491
+ return 'object' == typeof err && err instanceof PostHogFetchHttpError && 413 === err.status;
492
+ }
493
+ var posthog_core_stateless_QuotaLimitedFeature = /*#__PURE__*/ function(QuotaLimitedFeature) {
494
+ QuotaLimitedFeature["FeatureFlags"] = "feature_flags";
495
+ QuotaLimitedFeature["Recordings"] = "recordings";
496
+ return QuotaLimitedFeature;
497
+ }({});
498
+ class PostHogCoreStateless {
499
+ constructor(apiKey, options = {}){
500
+ this.flushPromise = null;
501
+ this.shutdownPromise = null;
502
+ this.promiseQueue = new PromiseQueue();
503
+ this._events = new SimpleEventEmitter();
504
+ this._isInitialized = false;
505
+ assert(apiKey, "You must pass your PostHog project's api key.");
506
+ this.apiKey = apiKey;
507
+ this.host = removeTrailingSlash(options.host || 'https://us.i.posthog.com');
508
+ this.flushAt = options.flushAt ? Math.max(options.flushAt, 1) : 20;
509
+ this.maxBatchSize = Math.max(this.flushAt, options.maxBatchSize ?? 100);
510
+ this.maxQueueSize = Math.max(this.flushAt, options.maxQueueSize ?? 1000);
511
+ this.flushInterval = options.flushInterval ?? 10000;
512
+ this.preloadFeatureFlags = options.preloadFeatureFlags ?? true;
513
+ this.defaultOptIn = options.defaultOptIn ?? true;
514
+ this.disableSurveys = options.disableSurveys ?? false;
515
+ this._retryOptions = {
516
+ retryCount: options.fetchRetryCount ?? 3,
517
+ retryDelay: options.fetchRetryDelay ?? 3000,
518
+ retryCheck: isPostHogFetchError
519
+ };
520
+ this.requestTimeout = options.requestTimeout ?? 10000;
521
+ this.featureFlagsRequestTimeoutMs = options.featureFlagsRequestTimeoutMs ?? 3000;
522
+ this.remoteConfigRequestTimeoutMs = options.remoteConfigRequestTimeoutMs ?? 3000;
523
+ this.disableGeoip = options.disableGeoip ?? true;
524
+ this.disabled = options.disabled ?? false;
525
+ this.historicalMigration = options?.historicalMigration ?? false;
526
+ this.evaluationEnvironments = options?.evaluationEnvironments;
527
+ this._initPromise = Promise.resolve();
528
+ this._isInitialized = true;
529
+ this._logger = createLogger('[PostHog]', this.logMsgIfDebug.bind(this));
530
+ this.disableCompression = !isGzipSupported() || (options?.disableCompression ?? false);
531
+ }
532
+ logMsgIfDebug(fn) {
533
+ if (this.isDebug) fn();
534
+ }
535
+ wrap(fn) {
536
+ if (this.disabled) return void this._logger.warn('The client is disabled');
537
+ if (this._isInitialized) return fn();
538
+ this._initPromise.then(()=>fn());
539
+ }
540
+ getCommonEventProperties() {
541
+ return {
542
+ $lib: this.getLibraryId(),
543
+ $lib_version: this.getLibraryVersion()
544
+ };
545
+ }
546
+ get optedOut() {
547
+ return this.getPersistedProperty(types_PostHogPersistedProperty.OptedOut) ?? !this.defaultOptIn;
548
+ }
549
+ async optIn() {
550
+ this.wrap(()=>{
551
+ this.setPersistedProperty(types_PostHogPersistedProperty.OptedOut, false);
552
+ });
553
+ }
554
+ async optOut() {
555
+ this.wrap(()=>{
556
+ this.setPersistedProperty(types_PostHogPersistedProperty.OptedOut, true);
557
+ });
558
+ }
559
+ on(event, cb) {
560
+ return this._events.on(event, cb);
561
+ }
562
+ debug(enabled = true) {
563
+ this.removeDebugCallback?.();
564
+ if (enabled) {
565
+ const removeDebugCallback = this.on('*', (event, payload)=>this._logger.info(event, payload));
566
+ this.removeDebugCallback = ()=>{
567
+ removeDebugCallback();
568
+ this.removeDebugCallback = void 0;
569
+ };
570
+ }
571
+ }
572
+ get isDebug() {
573
+ return !!this.removeDebugCallback;
574
+ }
575
+ get isDisabled() {
576
+ return this.disabled;
577
+ }
578
+ buildPayload(payload) {
579
+ return {
580
+ distinct_id: payload.distinct_id,
581
+ event: payload.event,
582
+ properties: {
583
+ ...payload.properties || {},
584
+ ...this.getCommonEventProperties()
585
+ }
586
+ };
587
+ }
588
+ addPendingPromise(promise) {
589
+ return this.promiseQueue.add(promise);
590
+ }
591
+ identifyStateless(distinctId, properties, options) {
592
+ this.wrap(()=>{
593
+ const payload = {
594
+ ...this.buildPayload({
595
+ distinct_id: distinctId,
596
+ event: '$identify',
597
+ properties
598
+ })
599
+ };
600
+ this.enqueue('identify', payload, options);
601
+ });
602
+ }
603
+ async identifyStatelessImmediate(distinctId, properties, options) {
604
+ const payload = {
605
+ ...this.buildPayload({
606
+ distinct_id: distinctId,
607
+ event: '$identify',
608
+ properties
609
+ })
610
+ };
611
+ await this.sendImmediate('identify', payload, options);
612
+ }
613
+ captureStateless(distinctId, event, properties, options) {
614
+ this.wrap(()=>{
615
+ const payload = this.buildPayload({
616
+ distinct_id: distinctId,
617
+ event,
618
+ properties
619
+ });
620
+ this.enqueue('capture', payload, options);
621
+ });
622
+ }
623
+ async captureStatelessImmediate(distinctId, event, properties, options) {
624
+ const payload = this.buildPayload({
625
+ distinct_id: distinctId,
626
+ event,
627
+ properties
628
+ });
629
+ await this.sendImmediate('capture', payload, options);
630
+ }
631
+ aliasStateless(alias, distinctId, properties, options) {
632
+ this.wrap(()=>{
633
+ const payload = this.buildPayload({
634
+ event: '$create_alias',
635
+ distinct_id: distinctId,
636
+ properties: {
637
+ ...properties || {},
638
+ distinct_id: distinctId,
639
+ alias
640
+ }
641
+ });
642
+ this.enqueue('alias', payload, options);
643
+ });
644
+ }
645
+ async aliasStatelessImmediate(alias, distinctId, properties, options) {
646
+ const payload = this.buildPayload({
647
+ event: '$create_alias',
648
+ distinct_id: distinctId,
649
+ properties: {
650
+ ...properties || {},
651
+ distinct_id: distinctId,
652
+ alias
653
+ }
654
+ });
655
+ await this.sendImmediate('alias', payload, options);
656
+ }
657
+ groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties) {
658
+ this.wrap(()=>{
659
+ const payload = this.buildPayload({
660
+ distinct_id: distinctId || `$${groupType}_${groupKey}`,
661
+ event: '$groupidentify',
662
+ properties: {
663
+ $group_type: groupType,
664
+ $group_key: groupKey,
665
+ $group_set: groupProperties || {},
666
+ ...eventProperties || {}
667
+ }
668
+ });
669
+ this.enqueue('capture', payload, options);
670
+ });
671
+ }
672
+ async getRemoteConfig() {
673
+ await this._initPromise;
674
+ let host = this.host;
675
+ if ('https://us.i.posthog.com' === host) host = 'https://us-assets.i.posthog.com';
676
+ else if ('https://eu.i.posthog.com' === host) host = 'https://eu-assets.i.posthog.com';
677
+ const url = `${host}/array/${this.apiKey}/config`;
678
+ const fetchOptions = {
679
+ method: 'GET',
680
+ headers: {
681
+ ...this.getCustomHeaders(),
682
+ 'Content-Type': 'application/json'
683
+ }
684
+ };
685
+ return this.fetchWithRetry(url, fetchOptions, {
686
+ retryCount: 0
687
+ }, this.remoteConfigRequestTimeoutMs).then((response)=>response.json()).catch((error)=>{
688
+ this._logger.error('Remote config could not be loaded', error);
689
+ this._events.emit('error', error);
690
+ });
691
+ }
692
+ async getFlags(distinctId, groups = {}, personProperties = {}, groupProperties = {}, extraPayload = {}, fetchConfig = true) {
693
+ await this._initPromise;
694
+ const configParam = fetchConfig ? '&config=true' : '';
695
+ const url = `${this.host}/flags/?v=2${configParam}`;
696
+ const requestData = {
697
+ token: this.apiKey,
698
+ distinct_id: distinctId,
699
+ groups,
700
+ person_properties: personProperties,
701
+ group_properties: groupProperties,
702
+ ...extraPayload
703
+ };
704
+ if (this.evaluationEnvironments && this.evaluationEnvironments.length > 0) requestData.evaluation_environments = this.evaluationEnvironments;
705
+ const fetchOptions = {
706
+ method: 'POST',
707
+ headers: {
708
+ ...this.getCustomHeaders(),
709
+ 'Content-Type': 'application/json'
710
+ },
711
+ body: JSON.stringify(requestData)
712
+ };
713
+ this._logger.info('Flags URL', url);
714
+ return this.fetchWithRetry(url, fetchOptions, {
715
+ retryCount: 0
716
+ }, this.featureFlagsRequestTimeoutMs).then((response)=>response.json()).then((response)=>normalizeFlagsResponse(response)).catch((error)=>{
717
+ this._events.emit('error', error);
718
+ });
719
+ }
720
+ async getFeatureFlagStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
721
+ await this._initPromise;
722
+ const flagDetailResponse = await this.getFeatureFlagDetailStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
723
+ if (void 0 === flagDetailResponse) return {
724
+ response: void 0,
725
+ requestId: void 0
726
+ };
727
+ let response = getFeatureFlagValue(flagDetailResponse.response);
728
+ if (void 0 === response) response = false;
729
+ return {
730
+ response,
731
+ requestId: flagDetailResponse.requestId
732
+ };
733
+ }
734
+ async getFeatureFlagDetailStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
735
+ await this._initPromise;
736
+ const flagsResponse = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, [
737
+ key
738
+ ]);
739
+ if (void 0 === flagsResponse) return;
740
+ const featureFlags = flagsResponse.flags;
741
+ const flagDetail = featureFlags[key];
742
+ return {
743
+ response: flagDetail,
744
+ requestId: flagsResponse.requestId
745
+ };
746
+ }
747
+ async getFeatureFlagPayloadStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
748
+ await this._initPromise;
749
+ const payloads = await this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, [
750
+ key
751
+ ]);
752
+ if (!payloads) return;
753
+ const response = payloads[key];
754
+ if (void 0 === response) return null;
755
+ return response;
756
+ }
757
+ async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
758
+ await this._initPromise;
759
+ const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate)).payloads;
760
+ return payloads;
761
+ }
762
+ async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
763
+ await this._initPromise;
764
+ return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
765
+ }
766
+ async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
767
+ await this._initPromise;
768
+ const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
769
+ if (!featureFlagDetails) return {
770
+ flags: void 0,
771
+ payloads: void 0,
772
+ requestId: void 0
773
+ };
774
+ return {
775
+ flags: featureFlagDetails.featureFlags,
776
+ payloads: featureFlagDetails.featureFlagPayloads,
777
+ requestId: featureFlagDetails.requestId
778
+ };
779
+ }
780
+ async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
781
+ await this._initPromise;
782
+ const extraPayload = {};
783
+ if (disableGeoip ?? this.disableGeoip) extraPayload['geoip_disable'] = true;
784
+ if (flagKeysToEvaluate) extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
785
+ const flagsResponse = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
786
+ if (void 0 === flagsResponse) return;
787
+ if (flagsResponse.errorsWhileComputingFlags) console.error('[FEATURE FLAGS] Error while computing feature flags, some flags may be missing or incorrect. Learn more at https://posthog.com/docs/feature-flags/best-practices');
788
+ if (flagsResponse.quotaLimited?.includes("feature_flags")) {
789
+ console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - feature flags unavailable. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
790
+ return {
791
+ flags: {},
792
+ featureFlags: {},
793
+ featureFlagPayloads: {},
794
+ requestId: flagsResponse?.requestId
795
+ };
796
+ }
797
+ return flagsResponse;
798
+ }
799
+ async getSurveysStateless() {
800
+ await this._initPromise;
801
+ if (true === this.disableSurveys) {
802
+ this._logger.info('Loading surveys is disabled.');
803
+ return [];
804
+ }
805
+ const url = `${this.host}/api/surveys/?token=${this.apiKey}`;
806
+ const fetchOptions = {
807
+ method: 'GET',
808
+ headers: {
809
+ ...this.getCustomHeaders(),
810
+ 'Content-Type': 'application/json'
811
+ }
812
+ };
813
+ const response = await this.fetchWithRetry(url, fetchOptions).then((response)=>{
814
+ if (200 !== response.status || !response.json) {
815
+ const msg = `Surveys API could not be loaded: ${response.status}`;
816
+ const error = new Error(msg);
817
+ this._logger.error(error);
818
+ this._events.emit('error', new Error(msg));
819
+ return;
820
+ }
821
+ return response.json();
822
+ }).catch((error)=>{
823
+ this._logger.error('Surveys API could not be loaded', error);
824
+ this._events.emit('error', error);
825
+ });
826
+ const newSurveys = response?.surveys;
827
+ if (newSurveys) this._logger.info('Surveys fetched from API: ', JSON.stringify(newSurveys));
828
+ return newSurveys ?? [];
829
+ }
830
+ get props() {
831
+ if (!this._props) this._props = this.getPersistedProperty(types_PostHogPersistedProperty.Props);
832
+ return this._props || {};
833
+ }
834
+ set props(val) {
835
+ this._props = val;
836
+ }
837
+ async register(properties) {
838
+ this.wrap(()=>{
839
+ this.props = {
840
+ ...this.props,
841
+ ...properties
842
+ };
843
+ this.setPersistedProperty(types_PostHogPersistedProperty.Props, this.props);
844
+ });
845
+ }
846
+ async unregister(property) {
847
+ this.wrap(()=>{
848
+ delete this.props[property];
849
+ this.setPersistedProperty(types_PostHogPersistedProperty.Props, this.props);
850
+ });
851
+ }
852
+ enqueue(type, _message, options) {
853
+ this.wrap(()=>{
854
+ if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
855
+ const message = this.prepareMessage(type, _message, options);
856
+ const queue = this.getPersistedProperty(types_PostHogPersistedProperty.Queue) || [];
857
+ if (queue.length >= this.maxQueueSize) {
858
+ queue.shift();
859
+ this._logger.info('Queue is full, the oldest event is dropped.');
860
+ }
861
+ queue.push({
862
+ message
863
+ });
864
+ this.setPersistedProperty(types_PostHogPersistedProperty.Queue, queue);
865
+ this._events.emit(type, message);
866
+ if (queue.length >= this.flushAt) this.flushBackground();
867
+ if (this.flushInterval && !this._flushTimer) this._flushTimer = safeSetTimeout(()=>this.flushBackground(), this.flushInterval);
868
+ });
869
+ }
870
+ async sendImmediate(type, _message, options) {
871
+ if (this.disabled) return void this._logger.warn('The client is disabled');
872
+ if (!this._isInitialized) await this._initPromise;
873
+ if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
874
+ const data = {
875
+ api_key: this.apiKey,
876
+ batch: [
877
+ this.prepareMessage(type, _message, options)
878
+ ],
879
+ sent_at: currentISOTime()
880
+ };
881
+ if (this.historicalMigration) data.historical_migration = true;
882
+ const payload = JSON.stringify(data);
883
+ const url = `${this.host}/batch/`;
884
+ const gzippedPayload = this.disableCompression ? null : await gzipCompress(payload, this.isDebug);
885
+ const fetchOptions = {
886
+ method: 'POST',
887
+ headers: {
888
+ ...this.getCustomHeaders(),
889
+ 'Content-Type': 'application/json',
890
+ ...null !== gzippedPayload && {
891
+ 'Content-Encoding': 'gzip'
892
+ }
893
+ },
894
+ body: gzippedPayload || payload
895
+ };
896
+ try {
897
+ await this.fetchWithRetry(url, fetchOptions);
898
+ } catch (err) {
899
+ this._events.emit('error', err);
900
+ }
901
+ }
902
+ prepareMessage(type, _message, options) {
903
+ const message = {
904
+ ..._message,
905
+ type: type,
906
+ library: this.getLibraryId(),
907
+ library_version: this.getLibraryVersion(),
908
+ timestamp: options?.timestamp ? options?.timestamp : currentISOTime(),
909
+ uuid: options?.uuid ? options.uuid : uuidv7()
910
+ };
911
+ const addGeoipDisableProperty = options?.disableGeoip ?? this.disableGeoip;
912
+ if (addGeoipDisableProperty) {
913
+ if (!message.properties) message.properties = {};
914
+ message['properties']['$geoip_disable'] = true;
915
+ }
916
+ if (message.distinctId) {
917
+ message.distinct_id = message.distinctId;
918
+ delete message.distinctId;
919
+ }
920
+ return message;
921
+ }
922
+ clearFlushTimer() {
923
+ if (this._flushTimer) {
924
+ clearTimeout(this._flushTimer);
925
+ this._flushTimer = void 0;
926
+ }
927
+ }
928
+ flushBackground() {
929
+ this.flush().catch(async (err)=>{
930
+ await logFlushError(err);
931
+ });
932
+ }
933
+ async flush() {
934
+ const nextFlushPromise = allSettled([
935
+ this.flushPromise
936
+ ]).then(()=>this._flush());
937
+ this.flushPromise = nextFlushPromise;
938
+ this.addPendingPromise(nextFlushPromise);
939
+ allSettled([
940
+ nextFlushPromise
941
+ ]).then(()=>{
942
+ if (this.flushPromise === nextFlushPromise) this.flushPromise = null;
943
+ });
944
+ return nextFlushPromise;
945
+ }
946
+ getCustomHeaders() {
947
+ const customUserAgent = this.getCustomUserAgent();
948
+ const headers = {};
949
+ if (customUserAgent && '' !== customUserAgent) headers['User-Agent'] = customUserAgent;
950
+ return headers;
951
+ }
952
+ async _flush() {
953
+ this.clearFlushTimer();
954
+ await this._initPromise;
955
+ let queue = this.getPersistedProperty(types_PostHogPersistedProperty.Queue) || [];
956
+ if (!queue.length) return;
957
+ const sentMessages = [];
958
+ const originalQueueLength = queue.length;
959
+ while(queue.length > 0 && sentMessages.length < originalQueueLength){
960
+ const batchItems = queue.slice(0, this.maxBatchSize);
961
+ const batchMessages = batchItems.map((item)=>item.message);
962
+ const persistQueueChange = ()=>{
963
+ const refreshedQueue = this.getPersistedProperty(types_PostHogPersistedProperty.Queue) || [];
964
+ const newQueue = refreshedQueue.slice(batchItems.length);
965
+ this.setPersistedProperty(types_PostHogPersistedProperty.Queue, newQueue);
966
+ queue = newQueue;
967
+ };
968
+ const data = {
969
+ api_key: this.apiKey,
970
+ batch: batchMessages,
971
+ sent_at: currentISOTime()
972
+ };
973
+ if (this.historicalMigration) data.historical_migration = true;
974
+ const payload = JSON.stringify(data);
975
+ const url = `${this.host}/batch/`;
976
+ const gzippedPayload = this.disableCompression ? null : await gzipCompress(payload, this.isDebug);
977
+ const fetchOptions = {
978
+ method: 'POST',
979
+ headers: {
980
+ ...this.getCustomHeaders(),
981
+ 'Content-Type': 'application/json',
982
+ ...null !== gzippedPayload && {
983
+ 'Content-Encoding': 'gzip'
984
+ }
985
+ },
986
+ body: gzippedPayload || payload
987
+ };
988
+ const retryOptions = {
989
+ retryCheck: (err)=>{
990
+ if (isPostHogFetchContentTooLargeError(err)) return false;
991
+ return isPostHogFetchError(err);
992
+ }
993
+ };
994
+ try {
995
+ await this.fetchWithRetry(url, fetchOptions, retryOptions);
996
+ } catch (err) {
997
+ if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
998
+ this.maxBatchSize = Math.max(1, Math.floor(batchMessages.length / 2));
999
+ this._logger.warn(`Received 413 when sending batch of size ${batchMessages.length}, reducing batch size to ${this.maxBatchSize}`);
1000
+ continue;
1001
+ }
1002
+ if (!(err instanceof PostHogFetchNetworkError)) persistQueueChange();
1003
+ this._events.emit('error', err);
1004
+ throw err;
1005
+ }
1006
+ persistQueueChange();
1007
+ sentMessages.push(...batchMessages);
1008
+ }
1009
+ this._events.emit('flush', sentMessages);
1010
+ }
1011
+ async fetchWithRetry(url, options, retryOptions, requestTimeout) {
1012
+ AbortSignal.timeout ??= function(ms) {
1013
+ const ctrl = new AbortController();
1014
+ setTimeout(()=>ctrl.abort(), ms);
1015
+ return ctrl.signal;
1016
+ };
1017
+ const body = options.body ? options.body : '';
1018
+ let reqByteLength = -1;
1019
+ try {
1020
+ reqByteLength = body instanceof Blob ? body.size : Buffer.byteLength(body, STRING_FORMAT);
1021
+ } catch {
1022
+ if (body instanceof Blob) reqByteLength = body.size;
1023
+ else {
1024
+ const encoded = new TextEncoder().encode(body);
1025
+ reqByteLength = encoded.length;
1026
+ }
1027
+ }
1028
+ return await retriable(async ()=>{
1029
+ let res = null;
1030
+ try {
1031
+ res = await this.fetch(url, {
1032
+ signal: AbortSignal.timeout(requestTimeout ?? this.requestTimeout),
1033
+ ...options
1034
+ });
1035
+ } catch (e) {
1036
+ throw new PostHogFetchNetworkError(e);
1037
+ }
1038
+ const isNoCors = 'no-cors' === options.mode;
1039
+ if (!isNoCors && (res.status < 200 || res.status >= 400)) throw new PostHogFetchHttpError(res, reqByteLength);
1040
+ return res;
1041
+ }, {
1042
+ ...this._retryOptions,
1043
+ ...retryOptions
1044
+ });
1045
+ }
1046
+ async _shutdown(shutdownTimeoutMs = 30000) {
1047
+ await this._initPromise;
1048
+ let hasTimedOut = false;
1049
+ this.clearFlushTimer();
1050
+ const doShutdown = async ()=>{
1051
+ try {
1052
+ await this.promiseQueue.join();
1053
+ while(true){
1054
+ const queue = this.getPersistedProperty(types_PostHogPersistedProperty.Queue) || [];
1055
+ if (0 === queue.length) break;
1056
+ await this.flush();
1057
+ if (hasTimedOut) break;
1058
+ }
1059
+ } catch (e) {
1060
+ if (!isPostHogFetchError(e)) throw e;
1061
+ await logFlushError(e);
1062
+ }
1063
+ };
1064
+ return Promise.race([
1065
+ new Promise((_, reject)=>{
1066
+ safeSetTimeout(()=>{
1067
+ this._logger.error('Timed out while shutting down PostHog');
1068
+ hasTimedOut = true;
1069
+ reject('Timeout while shutting down PostHog. Some events may not have been sent.');
1070
+ }, shutdownTimeoutMs);
1071
+ }),
1072
+ doShutdown()
1073
+ ]);
1074
+ }
1075
+ async shutdown(shutdownTimeoutMs = 30000) {
1076
+ if (this.shutdownPromise) this._logger.warn('shutdown() called while already shutting down. shutdown() is meant to be called once before process exit - use flush() for per-request cleanup');
1077
+ else this.shutdownPromise = this._shutdown(shutdownTimeoutMs).finally(()=>{
1078
+ this.shutdownPromise = null;
1079
+ });
1080
+ return this.shutdownPromise;
1081
+ }
1082
+ }
1083
+
1084
+ class PostHogCore extends PostHogCoreStateless {
1085
+ constructor(apiKey, options){
1086
+ const disableGeoipOption = options?.disableGeoip ?? false;
1087
+ const featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 10000;
1088
+ super(apiKey, {
1089
+ ...options,
1090
+ disableGeoip: disableGeoipOption,
1091
+ featureFlagsRequestTimeoutMs
1092
+ }), this.flagCallReported = {}, this._sessionMaxLengthSeconds = 86400, this.sessionProps = {};
1093
+ this.sendFeatureFlagEvent = options?.sendFeatureFlagEvent ?? true;
1094
+ this._sessionExpirationTimeSeconds = options?.sessionExpirationTimeSeconds ?? 1800;
1095
+ }
1096
+ setupBootstrap(options) {
1097
+ const bootstrap = options?.bootstrap;
1098
+ if (!bootstrap) return;
1099
+ if (bootstrap.distinctId) if (bootstrap.isIdentifiedId) {
1100
+ const distinctId = this.getPersistedProperty(types_PostHogPersistedProperty.DistinctId);
1101
+ if (!distinctId) this.setPersistedProperty(types_PostHogPersistedProperty.DistinctId, bootstrap.distinctId);
1102
+ } else {
1103
+ const anonymousId = this.getPersistedProperty(types_PostHogPersistedProperty.AnonymousId);
1104
+ if (!anonymousId) this.setPersistedProperty(types_PostHogPersistedProperty.AnonymousId, bootstrap.distinctId);
1105
+ }
1106
+ const bootstrapFeatureFlags = bootstrap.featureFlags;
1107
+ const bootstrapFeatureFlagPayloads = bootstrap.featureFlagPayloads ?? {};
1108
+ if (bootstrapFeatureFlags && Object.keys(bootstrapFeatureFlags).length) {
1109
+ const normalizedBootstrapFeatureFlagDetails = createFlagsResponseFromFlagsAndPayloads(bootstrapFeatureFlags, bootstrapFeatureFlagPayloads);
1110
+ if (Object.keys(normalizedBootstrapFeatureFlagDetails.flags).length > 0) {
1111
+ this.setBootstrappedFeatureFlagDetails(normalizedBootstrapFeatureFlagDetails);
1112
+ const currentFeatureFlagDetails = this.getKnownFeatureFlagDetails() || {
1113
+ flags: {}};
1114
+ const newFeatureFlagDetails = {
1115
+ flags: {
1116
+ ...normalizedBootstrapFeatureFlagDetails.flags,
1117
+ ...currentFeatureFlagDetails.flags
1118
+ },
1119
+ requestId: normalizedBootstrapFeatureFlagDetails.requestId
1120
+ };
1121
+ this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
1122
+ }
1123
+ }
1124
+ }
1125
+ clearProps() {
1126
+ this.props = void 0;
1127
+ this.sessionProps = {};
1128
+ this.flagCallReported = {};
1129
+ }
1130
+ on(event, cb) {
1131
+ return this._events.on(event, cb);
1132
+ }
1133
+ reset(propertiesToKeep) {
1134
+ this.wrap(()=>{
1135
+ const allPropertiesToKeep = [
1136
+ types_PostHogPersistedProperty.Queue,
1137
+ ...propertiesToKeep || []
1138
+ ];
1139
+ this.clearProps();
1140
+ for (const key of Object.keys(types_PostHogPersistedProperty))if (!allPropertiesToKeep.includes(types_PostHogPersistedProperty[key])) this.setPersistedProperty(types_PostHogPersistedProperty[key], null);
1141
+ this.reloadFeatureFlags();
1142
+ });
1143
+ }
1144
+ getCommonEventProperties() {
1145
+ const featureFlags = this.getFeatureFlags();
1146
+ const featureVariantProperties = {};
1147
+ if (featureFlags) for (const [feature, variant] of Object.entries(featureFlags))featureVariantProperties[`$feature/${feature}`] = variant;
1148
+ return {
1149
+ ...maybeAdd('$active_feature_flags', featureFlags ? Object.keys(featureFlags) : void 0),
1150
+ ...featureVariantProperties,
1151
+ ...super.getCommonEventProperties()
1152
+ };
1153
+ }
1154
+ enrichProperties(properties) {
1155
+ return {
1156
+ ...this.props,
1157
+ ...this.sessionProps,
1158
+ ...properties || {},
1159
+ ...this.getCommonEventProperties(),
1160
+ $session_id: this.getSessionId()
1161
+ };
1162
+ }
1163
+ getSessionId() {
1164
+ if (!this._isInitialized) return '';
1165
+ let sessionId = this.getPersistedProperty(types_PostHogPersistedProperty.SessionId);
1166
+ const sessionLastTimestamp = this.getPersistedProperty(types_PostHogPersistedProperty.SessionLastTimestamp) || 0;
1167
+ const sessionStartTimestamp = this.getPersistedProperty(types_PostHogPersistedProperty.SessionStartTimestamp) || 0;
1168
+ const now = Date.now();
1169
+ const sessionLastDif = now - sessionLastTimestamp;
1170
+ const sessionStartDif = now - sessionStartTimestamp;
1171
+ if (!sessionId || sessionLastDif > 1000 * this._sessionExpirationTimeSeconds || sessionStartDif > 1000 * this._sessionMaxLengthSeconds) {
1172
+ sessionId = uuidv7();
1173
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionId, sessionId);
1174
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionStartTimestamp, now);
1175
+ }
1176
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionLastTimestamp, now);
1177
+ return sessionId;
1178
+ }
1179
+ resetSessionId() {
1180
+ this.wrap(()=>{
1181
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionId, null);
1182
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionLastTimestamp, null);
1183
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionStartTimestamp, null);
1184
+ });
1185
+ }
1186
+ getAnonymousId() {
1187
+ if (!this._isInitialized) return '';
1188
+ let anonId = this.getPersistedProperty(types_PostHogPersistedProperty.AnonymousId);
1189
+ if (!anonId) {
1190
+ anonId = uuidv7();
1191
+ this.setPersistedProperty(types_PostHogPersistedProperty.AnonymousId, anonId);
1192
+ }
1193
+ return anonId;
1194
+ }
1195
+ getDistinctId() {
1196
+ if (!this._isInitialized) return '';
1197
+ return this.getPersistedProperty(types_PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1198
+ }
1199
+ registerForSession(properties) {
1200
+ this.sessionProps = {
1201
+ ...this.sessionProps,
1202
+ ...properties
1203
+ };
1204
+ }
1205
+ unregisterForSession(property) {
1206
+ delete this.sessionProps[property];
1207
+ }
1208
+ identify(distinctId, properties, options) {
1209
+ this.wrap(()=>{
1210
+ const previousDistinctId = this.getDistinctId();
1211
+ distinctId = distinctId || previousDistinctId;
1212
+ if (properties?.$groups) this.groups(properties.$groups);
1213
+ const userPropsOnce = properties?.$set_once;
1214
+ delete properties?.$set_once;
1215
+ const userProps = properties?.$set || properties;
1216
+ const allProperties = this.enrichProperties({
1217
+ $anon_distinct_id: this.getAnonymousId(),
1218
+ ...maybeAdd('$set', userProps),
1219
+ ...maybeAdd('$set_once', userPropsOnce)
1220
+ });
1221
+ if (distinctId !== previousDistinctId) {
1222
+ this.setPersistedProperty(types_PostHogPersistedProperty.AnonymousId, previousDistinctId);
1223
+ this.setPersistedProperty(types_PostHogPersistedProperty.DistinctId, distinctId);
1224
+ this.reloadFeatureFlags();
1225
+ }
1226
+ super.identifyStateless(distinctId, allProperties, options);
1227
+ });
1228
+ }
1229
+ capture(event, properties, options) {
1230
+ this.wrap(()=>{
1231
+ const distinctId = this.getDistinctId();
1232
+ if (properties?.$groups) this.groups(properties.$groups);
1233
+ const allProperties = this.enrichProperties(properties);
1234
+ super.captureStateless(distinctId, event, allProperties, options);
1235
+ });
1236
+ }
1237
+ alias(alias) {
1238
+ this.wrap(()=>{
1239
+ const distinctId = this.getDistinctId();
1240
+ const allProperties = this.enrichProperties({});
1241
+ super.aliasStateless(alias, distinctId, allProperties);
1242
+ });
1243
+ }
1244
+ autocapture(eventType, elements, properties = {}, options) {
1245
+ this.wrap(()=>{
1246
+ const distinctId = this.getDistinctId();
1247
+ const payload = {
1248
+ distinct_id: distinctId,
1249
+ event: '$autocapture',
1250
+ properties: {
1251
+ ...this.enrichProperties(properties),
1252
+ $event_type: eventType,
1253
+ $elements: elements
1254
+ }
1255
+ };
1256
+ this.enqueue('autocapture', payload, options);
1257
+ });
1258
+ }
1259
+ groups(groups) {
1260
+ this.wrap(()=>{
1261
+ const existingGroups = this.props.$groups || {};
1262
+ this.register({
1263
+ $groups: {
1264
+ ...existingGroups,
1265
+ ...groups
1266
+ }
1267
+ });
1268
+ if (Object.keys(groups).find((type)=>existingGroups[type] !== groups[type])) this.reloadFeatureFlags();
1269
+ });
1270
+ }
1271
+ group(groupType, groupKey, groupProperties, options) {
1272
+ this.wrap(()=>{
1273
+ this.groups({
1274
+ [groupType]: groupKey
1275
+ });
1276
+ if (groupProperties) this.groupIdentify(groupType, groupKey, groupProperties, options);
1277
+ });
1278
+ }
1279
+ groupIdentify(groupType, groupKey, groupProperties, options) {
1280
+ this.wrap(()=>{
1281
+ const distinctId = this.getDistinctId();
1282
+ const eventProperties = this.enrichProperties({});
1283
+ super.groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties);
1284
+ });
1285
+ }
1286
+ setPersonPropertiesForFlags(properties) {
1287
+ this.wrap(()=>{
1288
+ const existingProperties = this.getPersistedProperty(types_PostHogPersistedProperty.PersonProperties) || {};
1289
+ this.setPersistedProperty(types_PostHogPersistedProperty.PersonProperties, {
1290
+ ...existingProperties,
1291
+ ...properties
1292
+ });
1293
+ });
1294
+ }
1295
+ resetPersonPropertiesForFlags() {
1296
+ this.wrap(()=>{
1297
+ this.setPersistedProperty(types_PostHogPersistedProperty.PersonProperties, null);
1298
+ });
1299
+ }
1300
+ setGroupPropertiesForFlags(properties) {
1301
+ this.wrap(()=>{
1302
+ const existingProperties = this.getPersistedProperty(types_PostHogPersistedProperty.GroupProperties) || {};
1303
+ if (0 !== Object.keys(existingProperties).length) Object.keys(existingProperties).forEach((groupType)=>{
1304
+ existingProperties[groupType] = {
1305
+ ...existingProperties[groupType],
1306
+ ...properties[groupType]
1307
+ };
1308
+ delete properties[groupType];
1309
+ });
1310
+ this.setPersistedProperty(types_PostHogPersistedProperty.GroupProperties, {
1311
+ ...existingProperties,
1312
+ ...properties
1313
+ });
1314
+ });
1315
+ }
1316
+ resetGroupPropertiesForFlags() {
1317
+ this.wrap(()=>{
1318
+ this.setPersistedProperty(types_PostHogPersistedProperty.GroupProperties, null);
1319
+ });
1320
+ }
1321
+ async remoteConfigAsync() {
1322
+ await this._initPromise;
1323
+ if (this._remoteConfigResponsePromise) return this._remoteConfigResponsePromise;
1324
+ return this._remoteConfigAsync();
1325
+ }
1326
+ async flagsAsync(sendAnonDistinctId = true, fetchConfig = true) {
1327
+ await this._initPromise;
1328
+ if (this._flagsResponsePromise) return this._flagsResponsePromise;
1329
+ return this._flagsAsync(sendAnonDistinctId, fetchConfig);
1330
+ }
1331
+ cacheSessionReplay(source, response) {
1332
+ const sessionReplay = response?.sessionRecording;
1333
+ if (sessionReplay) {
1334
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionReplay, sessionReplay);
1335
+ this._logger.info(`Session replay config from ${source}: `, JSON.stringify(sessionReplay));
1336
+ } else if ('boolean' == typeof sessionReplay && false === sessionReplay) {
1337
+ this._logger.info(`Session replay config from ${source} disabled.`);
1338
+ this.setPersistedProperty(types_PostHogPersistedProperty.SessionReplay, null);
1339
+ }
1340
+ }
1341
+ async _remoteConfigAsync() {
1342
+ this._remoteConfigResponsePromise = this._initPromise.then(()=>{
1343
+ let remoteConfig = this.getPersistedProperty(types_PostHogPersistedProperty.RemoteConfig);
1344
+ this._logger.info('Cached remote config: ', JSON.stringify(remoteConfig));
1345
+ return super.getRemoteConfig().then((response)=>{
1346
+ if (response) {
1347
+ const remoteConfigWithoutSurveys = {
1348
+ ...response
1349
+ };
1350
+ delete remoteConfigWithoutSurveys.surveys;
1351
+ this._logger.info('Fetched remote config: ', JSON.stringify(remoteConfigWithoutSurveys));
1352
+ if (false === this.disableSurveys) {
1353
+ const surveys = response.surveys;
1354
+ let hasSurveys = true;
1355
+ if (Array.isArray(surveys)) this._logger.info('Surveys fetched from remote config: ', JSON.stringify(surveys));
1356
+ else {
1357
+ this._logger.info('There are no surveys.');
1358
+ hasSurveys = false;
1359
+ }
1360
+ if (hasSurveys) this.setPersistedProperty(types_PostHogPersistedProperty.Surveys, surveys);
1361
+ else this.setPersistedProperty(types_PostHogPersistedProperty.Surveys, null);
1362
+ } else this.setPersistedProperty(types_PostHogPersistedProperty.Surveys, null);
1363
+ this.setPersistedProperty(types_PostHogPersistedProperty.RemoteConfig, remoteConfigWithoutSurveys);
1364
+ this.cacheSessionReplay('remote config', response);
1365
+ if (false === response.hasFeatureFlags) {
1366
+ this.setKnownFeatureFlagDetails({
1367
+ flags: {}
1368
+ });
1369
+ this._logger.warn('Remote config has no feature flags, will not load feature flags.');
1370
+ } else if (false !== this.preloadFeatureFlags) this.reloadFeatureFlags();
1371
+ if (!response.supportedCompression?.includes(types_Compression.GZipJS)) this.disableCompression = true;
1372
+ remoteConfig = response;
1373
+ }
1374
+ return remoteConfig;
1375
+ });
1376
+ }).finally(()=>{
1377
+ this._remoteConfigResponsePromise = void 0;
1378
+ });
1379
+ return this._remoteConfigResponsePromise;
1380
+ }
1381
+ async _flagsAsync(sendAnonDistinctId = true, fetchConfig = true) {
1382
+ this._flagsResponsePromise = this._initPromise.then(async ()=>{
1383
+ const distinctId = this.getDistinctId();
1384
+ const groups = this.props.$groups || {};
1385
+ const personProperties = this.getPersistedProperty(types_PostHogPersistedProperty.PersonProperties) || {};
1386
+ const groupProperties = this.getPersistedProperty(types_PostHogPersistedProperty.GroupProperties) || {};
1387
+ const extraProperties = {
1388
+ $anon_distinct_id: sendAnonDistinctId ? this.getAnonymousId() : void 0
1389
+ };
1390
+ const res = await super.getFlags(distinctId, groups, personProperties, groupProperties, extraProperties, fetchConfig);
1391
+ if (res?.quotaLimited?.includes(posthog_core_stateless_QuotaLimitedFeature.FeatureFlags)) {
1392
+ this.setKnownFeatureFlagDetails(null);
1393
+ console.warn('[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all flags. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts');
1394
+ return res;
1395
+ }
1396
+ if (res?.featureFlags) {
1397
+ if (this.sendFeatureFlagEvent) this.flagCallReported = {};
1398
+ let newFeatureFlagDetails = res;
1399
+ if (res.errorsWhileComputingFlags) {
1400
+ const currentFlagDetails = this.getKnownFeatureFlagDetails();
1401
+ this._logger.info('Cached feature flags: ', JSON.stringify(currentFlagDetails));
1402
+ newFeatureFlagDetails = {
1403
+ ...res,
1404
+ flags: {
1405
+ ...currentFlagDetails?.flags,
1406
+ ...res.flags
1407
+ }
1408
+ };
1409
+ }
1410
+ this.setKnownFeatureFlagDetails(newFeatureFlagDetails);
1411
+ this.setPersistedProperty(types_PostHogPersistedProperty.FlagsEndpointWasHit, true);
1412
+ this.cacheSessionReplay('flags', res);
1413
+ }
1414
+ return res;
1415
+ }).finally(()=>{
1416
+ this._flagsResponsePromise = void 0;
1417
+ });
1418
+ return this._flagsResponsePromise;
1419
+ }
1420
+ setKnownFeatureFlagDetails(flagsResponse) {
1421
+ this.wrap(()=>{
1422
+ this.setPersistedProperty(types_PostHogPersistedProperty.FeatureFlagDetails, flagsResponse);
1423
+ this._events.emit('featureflags', getFlagValuesFromFlags(flagsResponse?.flags ?? {}));
1424
+ });
1425
+ }
1426
+ getKnownFeatureFlagDetails() {
1427
+ const storedDetails = this.getPersistedProperty(types_PostHogPersistedProperty.FeatureFlagDetails);
1428
+ if (!storedDetails) {
1429
+ const featureFlags = this.getPersistedProperty(types_PostHogPersistedProperty.FeatureFlags);
1430
+ const featureFlagPayloads = this.getPersistedProperty(types_PostHogPersistedProperty.FeatureFlagPayloads);
1431
+ if (void 0 === featureFlags && void 0 === featureFlagPayloads) return;
1432
+ return createFlagsResponseFromFlagsAndPayloads(featureFlags ?? {}, featureFlagPayloads ?? {});
1433
+ }
1434
+ return normalizeFlagsResponse(storedDetails);
1435
+ }
1436
+ getKnownFeatureFlags() {
1437
+ const featureFlagDetails = this.getKnownFeatureFlagDetails();
1438
+ if (!featureFlagDetails) return;
1439
+ return getFlagValuesFromFlags(featureFlagDetails.flags);
1440
+ }
1441
+ getKnownFeatureFlagPayloads() {
1442
+ const featureFlagDetails = this.getKnownFeatureFlagDetails();
1443
+ if (!featureFlagDetails) return;
1444
+ return getPayloadsFromFlags(featureFlagDetails.flags);
1445
+ }
1446
+ getBootstrappedFeatureFlagDetails() {
1447
+ const details = this.getPersistedProperty(types_PostHogPersistedProperty.BootstrapFeatureFlagDetails);
1448
+ if (!details) return;
1449
+ return details;
1450
+ }
1451
+ setBootstrappedFeatureFlagDetails(details) {
1452
+ this.setPersistedProperty(types_PostHogPersistedProperty.BootstrapFeatureFlagDetails, details);
1453
+ }
1454
+ getBootstrappedFeatureFlags() {
1455
+ const details = this.getBootstrappedFeatureFlagDetails();
1456
+ if (!details) return;
1457
+ return getFlagValuesFromFlags(details.flags);
1458
+ }
1459
+ getBootstrappedFeatureFlagPayloads() {
1460
+ const details = this.getBootstrappedFeatureFlagDetails();
1461
+ if (!details) return;
1462
+ return getPayloadsFromFlags(details.flags);
1463
+ }
1464
+ getFeatureFlag(key) {
1465
+ const details = this.getFeatureFlagDetails();
1466
+ if (!details) return;
1467
+ const featureFlag = details.flags[key];
1468
+ let response = getFeatureFlagValue(featureFlag);
1469
+ if (void 0 === response) response = false;
1470
+ if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
1471
+ const bootstrappedResponse = this.getBootstrappedFeatureFlags()?.[key];
1472
+ const bootstrappedPayload = this.getBootstrappedFeatureFlagPayloads()?.[key];
1473
+ this.flagCallReported[key] = true;
1474
+ this.capture('$feature_flag_called', {
1475
+ $feature_flag: key,
1476
+ $feature_flag_response: response,
1477
+ ...maybeAdd('$feature_flag_id', featureFlag?.metadata?.id),
1478
+ ...maybeAdd('$feature_flag_version', featureFlag?.metadata?.version),
1479
+ ...maybeAdd('$feature_flag_reason', featureFlag?.reason?.description ?? featureFlag?.reason?.code),
1480
+ ...maybeAdd('$feature_flag_bootstrapped_response', bootstrappedResponse),
1481
+ ...maybeAdd('$feature_flag_bootstrapped_payload', bootstrappedPayload),
1482
+ $used_bootstrap_value: !this.getPersistedProperty(types_PostHogPersistedProperty.FlagsEndpointWasHit),
1483
+ ...maybeAdd('$feature_flag_request_id', details.requestId)
1484
+ });
1485
+ }
1486
+ return response;
1487
+ }
1488
+ getFeatureFlagPayload(key) {
1489
+ const payloads = this.getFeatureFlagPayloads();
1490
+ if (!payloads) return;
1491
+ const response = payloads[key];
1492
+ if (void 0 === response) return null;
1493
+ return response;
1494
+ }
1495
+ getFeatureFlagPayloads() {
1496
+ return this.getFeatureFlagDetails()?.featureFlagPayloads;
1497
+ }
1498
+ getFeatureFlags() {
1499
+ return this.getFeatureFlagDetails()?.featureFlags;
1500
+ }
1501
+ getFeatureFlagDetails() {
1502
+ let details = this.getKnownFeatureFlagDetails();
1503
+ const overriddenFlags = this.getPersistedProperty(types_PostHogPersistedProperty.OverrideFeatureFlags);
1504
+ if (!overriddenFlags) return details;
1505
+ details = details ?? {
1506
+ featureFlags: {},
1507
+ featureFlagPayloads: {},
1508
+ flags: {}
1509
+ };
1510
+ const flags = details.flags ?? {};
1511
+ for(const key in overriddenFlags)if (overriddenFlags[key]) flags[key] = updateFlagValue(flags[key], overriddenFlags[key]);
1512
+ else delete flags[key];
1513
+ const result = {
1514
+ ...details,
1515
+ flags
1516
+ };
1517
+ return normalizeFlagsResponse(result);
1518
+ }
1519
+ getFeatureFlagsAndPayloads() {
1520
+ const flags = this.getFeatureFlags();
1521
+ const payloads = this.getFeatureFlagPayloads();
1522
+ return {
1523
+ flags,
1524
+ payloads
1525
+ };
1526
+ }
1527
+ isFeatureEnabled(key) {
1528
+ const response = this.getFeatureFlag(key);
1529
+ if (void 0 === response) return;
1530
+ return !!response;
1531
+ }
1532
+ reloadFeatureFlags(options) {
1533
+ this.flagsAsync(true).then((res)=>{
1534
+ options?.cb?.(void 0, res?.featureFlags);
1535
+ }).catch((e)=>{
1536
+ options?.cb?.(e, void 0);
1537
+ if (!options?.cb) this._logger.info('Error reloading feature flags', e);
1538
+ });
1539
+ }
1540
+ async reloadRemoteConfigAsync() {
1541
+ return await this.remoteConfigAsync();
1542
+ }
1543
+ async reloadFeatureFlagsAsync(sendAnonDistinctId) {
1544
+ return (await this.flagsAsync(sendAnonDistinctId ?? true))?.featureFlags;
1545
+ }
1546
+ onFeatureFlags(cb) {
1547
+ return this.on('featureflags', async ()=>{
1548
+ const flags = this.getFeatureFlags();
1549
+ if (flags) cb(flags);
1550
+ });
1551
+ }
1552
+ onFeatureFlag(key, cb) {
1553
+ return this.on('featureflags', async ()=>{
1554
+ const flagResponse = this.getFeatureFlag(key);
1555
+ if (void 0 !== flagResponse) cb(flagResponse);
1556
+ });
1557
+ }
1558
+ async overrideFeatureFlag(flags) {
1559
+ this.wrap(()=>{
1560
+ if (null === flags) return this.setPersistedProperty(types_PostHogPersistedProperty.OverrideFeatureFlags, null);
1561
+ return this.setPersistedProperty(types_PostHogPersistedProperty.OverrideFeatureFlags, flags);
1562
+ });
1563
+ }
1564
+ captureException(error, additionalProperties) {
1565
+ const properties = {
1566
+ $exception_level: 'error',
1567
+ $exception_list: [
1568
+ {
1569
+ type: isPlainError(error) ? error.name : 'Error',
1570
+ value: isPlainError(error) ? error.message : error,
1571
+ mechanism: {
1572
+ handled: true,
1573
+ synthetic: false
1574
+ }
1575
+ }
1576
+ ],
1577
+ ...additionalProperties
1578
+ };
1579
+ this.capture('$exception', properties);
1580
+ }
1581
+ captureTraceFeedback(traceId, userFeedback) {
1582
+ this.capture('$ai_feedback', {
1583
+ $ai_feedback_text: userFeedback,
1584
+ $ai_trace_id: String(traceId)
1585
+ });
1586
+ }
1587
+ captureTraceMetric(traceId, metricName, metricValue) {
1588
+ this.capture('$ai_metric', {
1589
+ $ai_metric_name: metricName,
1590
+ $ai_metric_value: String(metricValue),
1591
+ $ai_trace_id: String(traceId)
1592
+ });
1593
+ }
1594
+ }
1595
+
1596
+ const version = '0.1.0';
1597
+
1598
+ // Simple logger with [Leanbase] prefix
1599
+ const PREFIX = '[Leanbase]';
1600
+ const logger = {
1601
+ info: (...args) => {
1602
+ if (typeof console !== 'undefined') {
1603
+ // eslint-disable-next-line no-console
1604
+ console.log(PREFIX, ...args);
1605
+ }
1606
+ },
1607
+ warn: (...args) => {
1608
+ if (typeof console !== 'undefined') {
1609
+ // eslint-disable-next-line no-console
1610
+ console.warn(PREFIX, ...args);
1611
+ }
1612
+ },
1613
+ error: (...args) => {
1614
+ if (typeof console !== 'undefined') {
1615
+ // eslint-disable-next-line no-console
1616
+ console.error(PREFIX, ...args);
1617
+ }
1618
+ }
1619
+ };
1620
+
1621
+ class Leanbase extends PostHogCore {
1622
+ constructor(apiKey, options) {
1623
+ // Leanbase defaults
1624
+ const leanbaseOptions = {
1625
+ host: 'https://i.leanbase.co',
1626
+ ...options
1627
+ };
1628
+ super(apiKey, leanbaseOptions);
1629
+ this._storage = new Map();
1630
+ this._storageKey = `leanbase_${apiKey}`;
1631
+ // Load from localStorage if available
1632
+ if (typeof window !== 'undefined' && window.localStorage) {
1633
+ try {
1634
+ const stored = window.localStorage.getItem(this._storageKey);
1635
+ if (stored) {
1636
+ const parsed = JSON.parse(stored);
1637
+ Object.entries(parsed).forEach(([key, value]) => {
1638
+ this._storage.set(key, value);
1639
+ });
1640
+ }
1641
+ } catch (err) {
1642
+ logger.warn('Failed to load persisted data', err);
1643
+ }
1644
+ }
1645
+ logger.info('Leanbase initialized', {
1646
+ apiKey,
1647
+ host: leanbaseOptions.host
1648
+ });
1649
+ // Preload feature flags if not explicitly disabled
1650
+ if (options?.preloadFeatureFlags !== false) {
1651
+ this.reloadFeatureFlags();
1652
+ }
1653
+ }
1654
+ // PostHogCore abstract methods
1655
+ fetch(url, options) {
1656
+ const fetchFn = getFetch();
1657
+ if (!fetchFn) {
1658
+ return Promise.reject(new Error('Fetch API is not available in this environment.'));
1659
+ }
1660
+ return fetchFn(url, options);
1661
+ }
1662
+ getLibraryId() {
1663
+ return 'leanbase';
1664
+ }
1665
+ getLibraryVersion() {
1666
+ return version;
1667
+ }
1668
+ getCustomUserAgent() {
1669
+ return;
1670
+ }
1671
+ getPersistedProperty(key) {
1672
+ return this._storage.get(key);
1673
+ }
1674
+ setPersistedProperty(key, value) {
1675
+ if (isNull(value)) {
1676
+ this._storage.delete(key);
1677
+ } else {
1678
+ this._storage.set(key, value);
1679
+ }
1680
+ // Persist to localStorage if available
1681
+ if (typeof window !== 'undefined' && window.localStorage) {
1682
+ try {
1683
+ const obj = {};
1684
+ this._storage.forEach((v, k) => {
1685
+ obj[k] = v;
1686
+ });
1687
+ window.localStorage.setItem(this._storageKey, JSON.stringify(obj));
1688
+ } catch (err) {
1689
+ logger.warn('Failed to persist data', err);
1690
+ }
1691
+ }
1692
+ }
1693
+ // Public API: leanbase.capture()
1694
+ capture(event, properties, options) {
1695
+ super.capture(event, properties, options);
1696
+ }
1697
+ // Public API: leanbase.identify()
1698
+ identify(distinctId, properties, options) {
1699
+ super.identify(distinctId, properties, options);
1700
+ }
1701
+ // Cleanup
1702
+ destroy() {
1703
+ // Future: cleanup autocapture and session recording
1704
+ this._storage.clear();
1705
+ }
1706
+ }
1707
+
1708
+ const api = {
1709
+ _instance: null,
1710
+ _queue: [],
1711
+ init(apiKey, options) {
1712
+ this._instance = new Leanbase(apiKey, options);
1713
+ // Flush queued calls
1714
+ const q = this._queue;
1715
+ this._queue = [];
1716
+ for (const {
1717
+ fn,
1718
+ args
1719
+ } of q) {
1720
+ // @ts-expect-error dynamic dispatch to API methods
1721
+ this[fn](...args);
1722
+ }
1723
+ },
1724
+ capture(...args) {
1725
+ if (this._instance) {
1726
+ this._instance.capture(...args);
1727
+ } else {
1728
+ this._queue.push({
1729
+ fn: 'capture',
1730
+ args
1731
+ });
1732
+ }
1733
+ },
1734
+ identify(...args) {
1735
+ if (this._instance) {
1736
+ this._instance.identify(...args);
1737
+ } else {
1738
+ this._queue.push({
1739
+ fn: 'identify',
1740
+ args
1741
+ });
1742
+ }
1743
+ },
1744
+ group(...args) {
1745
+ if (this._instance && isFunction(this._instance.group)) {
1746
+ this._instance.group(...args);
1747
+ } else {
1748
+ this._queue.push({
1749
+ fn: 'group',
1750
+ args
1751
+ });
1752
+ }
1753
+ },
1754
+ alias(...args) {
1755
+ if (this._instance && isFunction(this._instance.alias)) {
1756
+ this._instance.alias(...args);
1757
+ } else {
1758
+ this._queue.push({
1759
+ fn: 'alias',
1760
+ args
1761
+ });
1762
+ }
1763
+ },
1764
+ reset() {
1765
+ this._instance?.reset();
1766
+ this._instance = null;
1767
+ },
1768
+ getInstance() {
1769
+ return this._instance;
1770
+ }
1771
+ };
1772
+ (function attachToGlobal(g) {
1773
+ // Prefer not to overwrite if a stub already exists (e.g., user queued calls before script loaded)
1774
+ const existing = g.leanbase;
1775
+ if (existing && typeof existing === 'object') {
1776
+ // If there is a pre-existing queue-compatible stub, try to drain it into our API
1777
+ if (isArray(existing._queue)) {
1778
+ api._queue = existing._queue;
1779
+ }
1780
+ }
1781
+ g.leanbase = api;
1782
+ // Also expose PascalCase alias for familiarity
1783
+ g.Leanbase = g.leanbase;
1784
+ })(globalThis);
1785
+
1786
+ return api;
1787
+
1788
+ })();
1789
+ //# sourceMappingURL=leanbase.iife.js.map