posthog-node 5.8.8 → 5.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/dist/{index.d.ts → client.d.ts} +7 -378
  2. package/dist/client.d.ts.map +1 -0
  3. package/dist/client.js +480 -0
  4. package/dist/client.mjs +436 -0
  5. package/dist/entrypoints/index.edge.d.ts +6 -0
  6. package/dist/entrypoints/index.edge.d.ts.map +1 -0
  7. package/dist/entrypoints/index.edge.js +96 -0
  8. package/dist/entrypoints/index.edge.mjs +19 -0
  9. package/dist/entrypoints/index.node.d.ts +6 -0
  10. package/dist/entrypoints/index.node.d.ts.map +1 -0
  11. package/dist/entrypoints/index.node.js +107 -0
  12. package/dist/entrypoints/index.node.mjs +24 -0
  13. package/dist/exports.d.ts +4 -0
  14. package/dist/exports.d.ts.map +1 -0
  15. package/dist/exports.js +78 -0
  16. package/dist/exports.mjs +3 -0
  17. package/dist/extensions/error-tracking/autocapture.d.ts +4 -0
  18. package/dist/extensions/error-tracking/autocapture.d.ts.map +1 -0
  19. package/dist/extensions/error-tracking/autocapture.js +68 -0
  20. package/dist/extensions/error-tracking/autocapture.mjs +31 -0
  21. package/dist/extensions/error-tracking/index.d.ts +19 -0
  22. package/dist/extensions/error-tracking/index.d.ts.map +1 -0
  23. package/dist/extensions/error-tracking/index.js +97 -0
  24. package/dist/extensions/error-tracking/index.mjs +63 -0
  25. package/dist/extensions/error-tracking/modifiers/context-lines.node.d.ts +5 -0
  26. package/dist/extensions/error-tracking/modifiers/context-lines.node.d.ts.map +1 -0
  27. package/dist/extensions/error-tracking/modifiers/context-lines.node.js +227 -0
  28. package/dist/extensions/error-tracking/modifiers/context-lines.node.mjs +187 -0
  29. package/dist/extensions/error-tracking/modifiers/module.node.d.ts +3 -0
  30. package/dist/extensions/error-tracking/modifiers/module.node.d.ts.map +1 -0
  31. package/dist/extensions/error-tracking/modifiers/module.node.js +64 -0
  32. package/dist/extensions/error-tracking/modifiers/module.node.mjs +30 -0
  33. package/dist/extensions/express.d.ts +17 -0
  34. package/dist/extensions/express.d.ts.map +1 -0
  35. package/dist/extensions/express.js +61 -0
  36. package/dist/extensions/express.mjs +17 -0
  37. package/dist/extensions/feature-flags/crypto-helpers.d.ts +3 -0
  38. package/dist/extensions/feature-flags/crypto-helpers.d.ts.map +1 -0
  39. package/dist/extensions/feature-flags/crypto-helpers.js +77 -0
  40. package/dist/extensions/feature-flags/crypto-helpers.mjs +22 -0
  41. package/dist/extensions/feature-flags/crypto.d.ts +2 -0
  42. package/dist/extensions/feature-flags/crypto.d.ts.map +1 -0
  43. package/dist/extensions/feature-flags/crypto.js +47 -0
  44. package/dist/extensions/feature-flags/crypto.mjs +13 -0
  45. package/dist/extensions/feature-flags/feature-flags.d.ts +89 -0
  46. package/dist/extensions/feature-flags/feature-flags.d.ts.map +1 -0
  47. package/dist/extensions/feature-flags/feature-flags.js +529 -0
  48. package/dist/extensions/feature-flags/feature-flags.mjs +483 -0
  49. package/dist/extensions/feature-flags/lazy.d.ts +24 -0
  50. package/dist/extensions/feature-flags/lazy.d.ts.map +1 -0
  51. package/dist/extensions/feature-flags/lazy.js +60 -0
  52. package/dist/extensions/feature-flags/lazy.mjs +26 -0
  53. package/dist/extensions/sentry-integration.d.ts +54 -0
  54. package/dist/extensions/sentry-integration.d.ts.map +1 -0
  55. package/dist/extensions/sentry-integration.js +113 -0
  56. package/dist/extensions/sentry-integration.mjs +73 -0
  57. package/dist/storage-memory.d.ts +7 -0
  58. package/dist/storage-memory.d.ts.map +1 -0
  59. package/dist/storage-memory.js +46 -0
  60. package/dist/storage-memory.mjs +12 -0
  61. package/dist/types.d.ts +253 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/dist/types.js +18 -0
  64. package/dist/types.mjs +0 -0
  65. package/dist/utils/logger.d.ts +3 -0
  66. package/dist/utils/logger.d.ts.map +1 -0
  67. package/dist/utils/logger.js +63 -0
  68. package/dist/utils/logger.mjs +29 -0
  69. package/dist/version.d.ts +2 -0
  70. package/dist/version.d.ts.map +1 -0
  71. package/dist/version.js +36 -0
  72. package/dist/version.mjs +2 -0
  73. package/package.json +32 -31
  74. package/src/client.ts +1532 -0
  75. package/src/entrypoints/index.edge.ts +22 -0
  76. package/src/entrypoints/index.node.ts +26 -0
  77. package/src/exports.ts +3 -0
  78. package/src/extensions/error-tracking/autocapture.ts +67 -0
  79. package/src/extensions/error-tracking/index.ts +104 -0
  80. package/src/extensions/error-tracking/modifiers/context-lines.node.ts +404 -0
  81. package/src/extensions/error-tracking/modifiers/module.node.ts +68 -0
  82. package/src/extensions/express.ts +40 -0
  83. package/src/extensions/feature-flags/crypto-helpers.ts +36 -0
  84. package/src/extensions/feature-flags/crypto.ts +22 -0
  85. package/src/extensions/feature-flags/feature-flags.ts +1003 -0
  86. package/src/extensions/feature-flags/lazy.ts +55 -0
  87. package/src/extensions/sentry-integration.ts +216 -0
  88. package/src/storage-memory.ts +13 -0
  89. package/src/types.ts +294 -0
  90. package/src/utils/logger.ts +39 -0
  91. package/src/version.ts +1 -0
  92. package/dist/edge/index.cjs +0 -3150
  93. package/dist/edge/index.cjs.map +0 -1
  94. package/dist/edge/index.mjs +0 -3144
  95. package/dist/edge/index.mjs.map +0 -1
  96. package/dist/node/index.cjs +0 -3556
  97. package/dist/node/index.cjs.map +0 -1
  98. package/dist/node/index.mjs +0 -3550
  99. package/dist/node/index.mjs.map +0 -1
package/dist/client.js ADDED
@@ -0,0 +1,480 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ PostHogBackendClient: ()=>PostHogBackendClient
37
+ });
38
+ const external_version_js_namespaceObject = require("./version.js");
39
+ const core_namespaceObject = require("@posthog/core");
40
+ const feature_flags_js_namespaceObject = require("./extensions/feature-flags/feature-flags.js");
41
+ const index_js_namespaceObject = require("./extensions/error-tracking/index.js");
42
+ var index_js_default = /*#__PURE__*/ __webpack_require__.n(index_js_namespaceObject);
43
+ const external_storage_memory_js_namespaceObject = require("./storage-memory.js");
44
+ const logger_js_namespaceObject = require("./utils/logger.js");
45
+ const MINIMUM_POLLING_INTERVAL = 100;
46
+ const THIRTY_SECONDS = 30000;
47
+ const MAX_CACHE_SIZE = 50000;
48
+ class PostHogBackendClient extends core_namespaceObject.PostHogCoreStateless {
49
+ constructor(apiKey, options = {}){
50
+ super(apiKey, options), this._memoryStorage = new external_storage_memory_js_namespaceObject.PostHogMemoryStorage();
51
+ this.options = options;
52
+ this.logger = (0, logger_js_namespaceObject.createLogger)(this.logMsgIfDebug.bind(this));
53
+ this.options.featureFlagsPollingInterval = 'number' == typeof options.featureFlagsPollingInterval ? Math.max(options.featureFlagsPollingInterval, MINIMUM_POLLING_INTERVAL) : THIRTY_SECONDS;
54
+ if (options.personalApiKey) {
55
+ if (options.personalApiKey.includes('phc_')) throw new Error('Your Personal API key is invalid. These keys are prefixed with "phx_" and can be created in PostHog project settings.');
56
+ const shouldEnableLocalEvaluation = false !== options.enableLocalEvaluation;
57
+ if (shouldEnableLocalEvaluation) this.featureFlagsPoller = new feature_flags_js_namespaceObject.FeatureFlagsPoller({
58
+ pollingInterval: this.options.featureFlagsPollingInterval,
59
+ personalApiKey: options.personalApiKey,
60
+ projectApiKey: apiKey,
61
+ timeout: options.requestTimeout ?? 10000,
62
+ host: this.host,
63
+ fetch: options.fetch,
64
+ onError: (err)=>{
65
+ this._events.emit('error', err);
66
+ },
67
+ onLoad: (count)=>{
68
+ this._events.emit('localEvaluationFlagsLoaded', count);
69
+ },
70
+ customHeaders: this.getCustomHeaders()
71
+ });
72
+ }
73
+ this.errorTracking = new (index_js_default())(this, options, this.logger);
74
+ this.distinctIdHasSentFlagCalls = {};
75
+ this.maxCacheSize = options.maxCacheSize || MAX_CACHE_SIZE;
76
+ }
77
+ getPersistedProperty(key) {
78
+ return this._memoryStorage.getProperty(key);
79
+ }
80
+ setPersistedProperty(key, value) {
81
+ return this._memoryStorage.setProperty(key, value);
82
+ }
83
+ fetch(url, options) {
84
+ return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
85
+ }
86
+ getLibraryVersion() {
87
+ return external_version_js_namespaceObject.version;
88
+ }
89
+ getCustomUserAgent() {
90
+ return `${this.getLibraryId()}/${this.getLibraryVersion()}`;
91
+ }
92
+ enable() {
93
+ return super.optIn();
94
+ }
95
+ disable() {
96
+ return super.optOut();
97
+ }
98
+ debug(enabled = true) {
99
+ super.debug(enabled);
100
+ this.featureFlagsPoller?.debug(enabled);
101
+ }
102
+ capture(props) {
103
+ if ('string' == typeof props) this.logMsgIfDebug(()=>console.warn('Called capture() with a string as the first argument when an object was expected.'));
104
+ this.addPendingPromise(this.prepareEventMessage(props).then(({ distinctId, event, properties, options })=>super.captureStateless(distinctId, event, properties, {
105
+ timestamp: options.timestamp,
106
+ disableGeoip: options.disableGeoip,
107
+ uuid: options.uuid
108
+ })).catch((err)=>{
109
+ if (err) console.error(err);
110
+ }));
111
+ }
112
+ async captureImmediate(props) {
113
+ if ('string' == typeof props) this.logMsgIfDebug(()=>console.warn('Called captureImmediate() with a string as the first argument when an object was expected.'));
114
+ return this.addPendingPromise(this.prepareEventMessage(props).then(({ distinctId, event, properties, options })=>super.captureStatelessImmediate(distinctId, event, properties, {
115
+ timestamp: options.timestamp,
116
+ disableGeoip: options.disableGeoip,
117
+ uuid: options.uuid
118
+ })).catch((err)=>{
119
+ if (err) console.error(err);
120
+ }));
121
+ }
122
+ identify({ distinctId, properties, disableGeoip }) {
123
+ const userPropsOnce = properties?.$set_once;
124
+ delete properties?.$set_once;
125
+ const userProps = properties?.$set || properties;
126
+ super.identifyStateless(distinctId, {
127
+ $set: userProps,
128
+ $set_once: userPropsOnce
129
+ }, {
130
+ disableGeoip
131
+ });
132
+ }
133
+ async identifyImmediate({ distinctId, properties, disableGeoip }) {
134
+ const userPropsOnce = properties?.$set_once;
135
+ delete properties?.$set_once;
136
+ const userProps = properties?.$set || properties;
137
+ await super.identifyStatelessImmediate(distinctId, {
138
+ $set: userProps,
139
+ $set_once: userPropsOnce
140
+ }, {
141
+ disableGeoip
142
+ });
143
+ }
144
+ alias(data) {
145
+ super.aliasStateless(data.alias, data.distinctId, void 0, {
146
+ disableGeoip: data.disableGeoip
147
+ });
148
+ }
149
+ async aliasImmediate(data) {
150
+ await super.aliasStatelessImmediate(data.alias, data.distinctId, void 0, {
151
+ disableGeoip: data.disableGeoip
152
+ });
153
+ }
154
+ isLocalEvaluationReady() {
155
+ return this.featureFlagsPoller?.isLocalEvaluationReady() ?? false;
156
+ }
157
+ async waitForLocalEvaluationReady(timeoutMs = THIRTY_SECONDS) {
158
+ if (this.isLocalEvaluationReady()) return true;
159
+ if (void 0 === this.featureFlagsPoller) return false;
160
+ return new Promise((resolve)=>{
161
+ const timeout = setTimeout(()=>{
162
+ cleanup();
163
+ resolve(false);
164
+ }, timeoutMs);
165
+ const cleanup = this._events.on('localEvaluationFlagsLoaded', (count)=>{
166
+ clearTimeout(timeout);
167
+ cleanup();
168
+ resolve(count > 0);
169
+ });
170
+ });
171
+ }
172
+ async getFeatureFlag(key, distinctId, options) {
173
+ const { groups, disableGeoip } = options || {};
174
+ let { onlyEvaluateLocally, sendFeatureFlagEvents, personProperties, groupProperties } = options || {};
175
+ const adjustedProperties = this.addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties);
176
+ personProperties = adjustedProperties.allPersonProperties;
177
+ groupProperties = adjustedProperties.allGroupProperties;
178
+ if (void 0 == onlyEvaluateLocally) onlyEvaluateLocally = false;
179
+ if (void 0 == sendFeatureFlagEvents) sendFeatureFlagEvents = this.options.sendFeatureFlagEvent ?? true;
180
+ let response = await this.featureFlagsPoller?.getFeatureFlag(key, distinctId, groups, personProperties, groupProperties);
181
+ const flagWasLocallyEvaluated = void 0 !== response;
182
+ let requestId;
183
+ let flagDetail;
184
+ if (!flagWasLocallyEvaluated && !onlyEvaluateLocally) {
185
+ const remoteResponse = await super.getFeatureFlagDetailStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
186
+ if (void 0 === remoteResponse) return;
187
+ flagDetail = remoteResponse.response;
188
+ response = (0, core_namespaceObject.getFeatureFlagValue)(flagDetail);
189
+ requestId = remoteResponse?.requestId;
190
+ }
191
+ const featureFlagReportedKey = `${key}_${response}`;
192
+ if (sendFeatureFlagEvents && (!(distinctId in this.distinctIdHasSentFlagCalls) || !this.distinctIdHasSentFlagCalls[distinctId].includes(featureFlagReportedKey))) {
193
+ if (Object.keys(this.distinctIdHasSentFlagCalls).length >= this.maxCacheSize) this.distinctIdHasSentFlagCalls = {};
194
+ if (Array.isArray(this.distinctIdHasSentFlagCalls[distinctId])) this.distinctIdHasSentFlagCalls[distinctId].push(featureFlagReportedKey);
195
+ else this.distinctIdHasSentFlagCalls[distinctId] = [
196
+ featureFlagReportedKey
197
+ ];
198
+ this.capture({
199
+ distinctId,
200
+ event: '$feature_flag_called',
201
+ properties: {
202
+ $feature_flag: key,
203
+ $feature_flag_response: response,
204
+ $feature_flag_id: flagDetail?.metadata?.id,
205
+ $feature_flag_version: flagDetail?.metadata?.version,
206
+ $feature_flag_reason: flagDetail?.reason?.description ?? flagDetail?.reason?.code,
207
+ locally_evaluated: flagWasLocallyEvaluated,
208
+ [`$feature/${key}`]: response,
209
+ $feature_flag_request_id: requestId
210
+ },
211
+ groups,
212
+ disableGeoip
213
+ });
214
+ }
215
+ return response;
216
+ }
217
+ async getFeatureFlagPayload(key, distinctId, matchValue, options) {
218
+ const { groups, disableGeoip } = options || {};
219
+ let { onlyEvaluateLocally, personProperties, groupProperties } = options || {};
220
+ const adjustedProperties = this.addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties);
221
+ personProperties = adjustedProperties.allPersonProperties;
222
+ groupProperties = adjustedProperties.allGroupProperties;
223
+ let response;
224
+ const localEvaluationEnabled = void 0 !== this.featureFlagsPoller;
225
+ if (localEvaluationEnabled) {
226
+ await this.featureFlagsPoller?.loadFeatureFlags();
227
+ const flag = this.featureFlagsPoller?.featureFlagsByKey[key];
228
+ if (flag) {
229
+ const result = await this.featureFlagsPoller?.computeFlagAndPayloadLocally(flag, distinctId, groups, personProperties, groupProperties, matchValue);
230
+ if (result) {
231
+ matchValue = result.value;
232
+ response = result.payload;
233
+ }
234
+ }
235
+ }
236
+ if (void 0 == onlyEvaluateLocally) onlyEvaluateLocally = false;
237
+ const payloadWasLocallyEvaluated = void 0 !== response;
238
+ if (!payloadWasLocallyEvaluated && !onlyEvaluateLocally) response = await super.getFeatureFlagPayloadStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
239
+ return response;
240
+ }
241
+ async getRemoteConfigPayload(flagKey) {
242
+ if (!this.options.personalApiKey) throw new Error('Personal API key is required for remote config payload decryption');
243
+ const response = await this._requestRemoteConfigPayload(flagKey);
244
+ if (!response) return;
245
+ const parsed = await response.json();
246
+ if ('string' == typeof parsed) try {
247
+ return JSON.parse(parsed);
248
+ } catch (e) {}
249
+ return parsed;
250
+ }
251
+ async isFeatureEnabled(key, distinctId, options) {
252
+ const feat = await this.getFeatureFlag(key, distinctId, options);
253
+ if (void 0 === feat) return;
254
+ return !!feat || false;
255
+ }
256
+ async getAllFlags(distinctId, options) {
257
+ const response = await this.getAllFlagsAndPayloads(distinctId, options);
258
+ return response.featureFlags || {};
259
+ }
260
+ async getAllFlagsAndPayloads(distinctId, options) {
261
+ const { groups, disableGeoip, flagKeys } = options || {};
262
+ let { onlyEvaluateLocally, personProperties, groupProperties } = options || {};
263
+ const adjustedProperties = this.addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties);
264
+ personProperties = adjustedProperties.allPersonProperties;
265
+ groupProperties = adjustedProperties.allGroupProperties;
266
+ if (void 0 == onlyEvaluateLocally) onlyEvaluateLocally = false;
267
+ const localEvaluationResult = await this.featureFlagsPoller?.getAllFlagsAndPayloads(distinctId, groups, personProperties, groupProperties, flagKeys);
268
+ let featureFlags = {};
269
+ let featureFlagPayloads = {};
270
+ let fallbackToFlags = true;
271
+ if (localEvaluationResult) {
272
+ featureFlags = localEvaluationResult.response;
273
+ featureFlagPayloads = localEvaluationResult.payloads;
274
+ fallbackToFlags = localEvaluationResult.fallbackToFlags;
275
+ }
276
+ if (fallbackToFlags && !onlyEvaluateLocally) {
277
+ const remoteEvaluationResult = await super.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeys);
278
+ featureFlags = {
279
+ ...featureFlags,
280
+ ...remoteEvaluationResult.flags || {}
281
+ };
282
+ featureFlagPayloads = {
283
+ ...featureFlagPayloads,
284
+ ...remoteEvaluationResult.payloads || {}
285
+ };
286
+ }
287
+ return {
288
+ featureFlags,
289
+ featureFlagPayloads
290
+ };
291
+ }
292
+ groupIdentify({ groupType, groupKey, properties, distinctId, disableGeoip }) {
293
+ super.groupIdentifyStateless(groupType, groupKey, properties, {
294
+ disableGeoip
295
+ }, distinctId);
296
+ }
297
+ async reloadFeatureFlags() {
298
+ await this.featureFlagsPoller?.loadFeatureFlags(true);
299
+ }
300
+ async _shutdown(shutdownTimeoutMs) {
301
+ this.featureFlagsPoller?.stopPoller();
302
+ this.errorTracking.shutdown();
303
+ return super._shutdown(shutdownTimeoutMs);
304
+ }
305
+ async _requestRemoteConfigPayload(flagKey) {
306
+ if (!this.options.personalApiKey) return;
307
+ const url = `${this.host}/api/projects/@current/feature_flags/${flagKey}/remote_config?token=${encodeURIComponent(this.apiKey)}`;
308
+ const options = {
309
+ method: 'GET',
310
+ headers: {
311
+ ...this.getCustomHeaders(),
312
+ 'Content-Type': 'application/json',
313
+ Authorization: `Bearer ${this.options.personalApiKey}`
314
+ }
315
+ };
316
+ let abortTimeout = null;
317
+ if (this.options.requestTimeout && 'number' == typeof this.options.requestTimeout) {
318
+ const controller = new AbortController();
319
+ abortTimeout = (0, core_namespaceObject.safeSetTimeout)(()=>{
320
+ controller.abort();
321
+ }, this.options.requestTimeout);
322
+ options.signal = controller.signal;
323
+ }
324
+ try {
325
+ return await this.fetch(url, options);
326
+ } catch (error) {
327
+ this._events.emit('error', error);
328
+ return;
329
+ } finally{
330
+ if (abortTimeout) clearTimeout(abortTimeout);
331
+ }
332
+ }
333
+ extractPropertiesFromEvent(eventProperties, groups) {
334
+ if (!eventProperties) return {
335
+ personProperties: {},
336
+ groupProperties: {}
337
+ };
338
+ const personProperties = {};
339
+ const groupProperties = {};
340
+ for (const [key, value] of Object.entries(eventProperties))if ((0, core_namespaceObject.isPlainObject)(value) && groups && key in groups) {
341
+ const groupProps = {};
342
+ for (const [groupKey, groupValue] of Object.entries(value))groupProps[String(groupKey)] = String(groupValue);
343
+ groupProperties[String(key)] = groupProps;
344
+ } else personProperties[String(key)] = String(value);
345
+ return {
346
+ personProperties,
347
+ groupProperties
348
+ };
349
+ }
350
+ async getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions) {
351
+ const finalPersonProperties = sendFeatureFlagsOptions?.personProperties || {};
352
+ const finalGroupProperties = sendFeatureFlagsOptions?.groupProperties || {};
353
+ const flagKeys = sendFeatureFlagsOptions?.flagKeys;
354
+ const onlyEvaluateLocally = sendFeatureFlagsOptions?.onlyEvaluateLocally ?? false;
355
+ if (onlyEvaluateLocally) if (!((this.featureFlagsPoller?.featureFlags?.length || 0) > 0)) return {};
356
+ else {
357
+ const groupsWithStringValues = {};
358
+ for (const [key, value] of Object.entries(groups || {}))groupsWithStringValues[key] = String(value);
359
+ return await this.getAllFlags(distinctId, {
360
+ groups: groupsWithStringValues,
361
+ personProperties: finalPersonProperties,
362
+ groupProperties: finalGroupProperties,
363
+ disableGeoip,
364
+ onlyEvaluateLocally: true,
365
+ flagKeys
366
+ });
367
+ }
368
+ if ((this.featureFlagsPoller?.featureFlags?.length || 0) > 0) {
369
+ const groupsWithStringValues = {};
370
+ for (const [key, value] of Object.entries(groups || {}))groupsWithStringValues[key] = String(value);
371
+ return await this.getAllFlags(distinctId, {
372
+ groups: groupsWithStringValues,
373
+ personProperties: finalPersonProperties,
374
+ groupProperties: finalGroupProperties,
375
+ disableGeoip,
376
+ onlyEvaluateLocally: true,
377
+ flagKeys
378
+ });
379
+ }
380
+ return (await super.getFeatureFlagsStateless(distinctId, groups, finalPersonProperties, finalGroupProperties, disableGeoip)).flags;
381
+ }
382
+ addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties) {
383
+ const allPersonProperties = {
384
+ distinct_id: distinctId,
385
+ ...personProperties || {}
386
+ };
387
+ const allGroupProperties = {};
388
+ if (groups) for (const groupName of Object.keys(groups))allGroupProperties[groupName] = {
389
+ $group_key: groups[groupName],
390
+ ...groupProperties?.[groupName] || {}
391
+ };
392
+ return {
393
+ allPersonProperties,
394
+ allGroupProperties
395
+ };
396
+ }
397
+ captureException(error, distinctId, additionalProperties) {
398
+ const syntheticException = new Error('PostHog syntheticException');
399
+ this.addPendingPromise(index_js_default().buildEventMessage(error, {
400
+ syntheticException
401
+ }, distinctId, additionalProperties).then((msg)=>this.capture(msg)));
402
+ }
403
+ async captureExceptionImmediate(error, distinctId, additionalProperties) {
404
+ const syntheticException = new Error('PostHog syntheticException');
405
+ this.addPendingPromise(index_js_default().buildEventMessage(error, {
406
+ syntheticException
407
+ }, distinctId, additionalProperties).then((msg)=>this.captureImmediate(msg)));
408
+ }
409
+ async prepareEventMessage(props) {
410
+ const { distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip, uuid } = props;
411
+ const eventMessage = this._runBeforeSend({
412
+ distinctId,
413
+ event,
414
+ properties,
415
+ groups,
416
+ sendFeatureFlags,
417
+ timestamp,
418
+ disableGeoip,
419
+ uuid
420
+ });
421
+ if (!eventMessage) return Promise.reject(null);
422
+ const eventProperties = await Promise.resolve().then(async ()=>{
423
+ if (sendFeatureFlags) {
424
+ const sendFeatureFlagsOptions = 'object' == typeof sendFeatureFlags ? sendFeatureFlags : void 0;
425
+ return await this.getFeatureFlagsForEvent(distinctId, groups, disableGeoip, sendFeatureFlagsOptions);
426
+ }
427
+ return {};
428
+ }).then((flags)=>{
429
+ const additionalProperties = {};
430
+ if (flags) for (const [feature, variant] of Object.entries(flags))additionalProperties[`$feature/${feature}`] = variant;
431
+ const activeFlags = Object.keys(flags || {}).filter((flag)=>flags?.[flag] !== false).sort();
432
+ if (activeFlags.length > 0) additionalProperties['$active_feature_flags'] = activeFlags;
433
+ return additionalProperties;
434
+ }).catch(()=>({})).then((additionalProperties)=>{
435
+ const props = {
436
+ ...additionalProperties,
437
+ ...eventMessage.properties || {},
438
+ $groups: eventMessage.groups || groups
439
+ };
440
+ return props;
441
+ });
442
+ return {
443
+ distinctId: eventMessage.distinctId,
444
+ event: eventMessage.event,
445
+ properties: eventProperties,
446
+ options: {
447
+ timestamp: eventMessage.timestamp,
448
+ disableGeoip: eventMessage.disableGeoip,
449
+ uuid: eventMessage.uuid
450
+ }
451
+ };
452
+ }
453
+ _runBeforeSend(eventMessage) {
454
+ const beforeSend = this.options.before_send;
455
+ if (!beforeSend) return eventMessage;
456
+ const fns = Array.isArray(beforeSend) ? beforeSend : [
457
+ beforeSend
458
+ ];
459
+ let result = eventMessage;
460
+ for (const fn of fns){
461
+ result = fn(result);
462
+ if (!result) {
463
+ this.logMsgIfDebug(()=>console.info(`Event '${eventMessage.event}' was rejected in beforeSend function`));
464
+ return null;
465
+ }
466
+ if (!result.properties || 0 === Object.keys(result.properties).length) {
467
+ const message = `Event '${result.event}' has no properties after beforeSend function, this is likely an error.`;
468
+ this.logMsgIfDebug(()=>console.warn(message));
469
+ }
470
+ }
471
+ return result;
472
+ }
473
+ }
474
+ exports.PostHogBackendClient = __webpack_exports__.PostHogBackendClient;
475
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
476
+ "PostHogBackendClient"
477
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
478
+ Object.defineProperty(exports, '__esModule', {
479
+ value: true
480
+ });