@posthog/core 1.69.9 → 1.71.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.
- package/capture-v1/package.json +6 -0
- package/dist/capture-v1/compression.d.ts +18 -0
- package/dist/capture-v1/compression.d.ts.map +1 -0
- package/dist/capture-v1/compression.js +70 -0
- package/dist/capture-v1/compression.mjs +26 -0
- package/dist/capture-v1/errors.d.ts +48 -0
- package/dist/capture-v1/errors.d.ts.map +1 -0
- package/dist/capture-v1/errors.js +82 -0
- package/dist/capture-v1/errors.mjs +38 -0
- package/dist/capture-v1/index.d.ts +15 -0
- package/dist/capture-v1/index.d.ts.map +1 -0
- package/dist/capture-v1/index.js +88 -0
- package/dist/capture-v1/index.mjs +7 -0
- package/dist/capture-v1/sender.d.ts +150 -0
- package/dist/capture-v1/sender.d.ts.map +1 -0
- package/dist/capture-v1/sender.js +554 -0
- package/dist/capture-v1/sender.mjs +507 -0
- package/dist/capture-v1/serialize.d.ts +30 -0
- package/dist/capture-v1/serialize.d.ts.map +1 -0
- package/dist/capture-v1/serialize.js +106 -0
- package/dist/capture-v1/serialize.mjs +59 -0
- package/dist/capture-v1/transform.d.ts +19 -0
- package/dist/capture-v1/transform.d.ts.map +1 -0
- package/dist/capture-v1/transform.js +118 -0
- package/dist/capture-v1/transform.mjs +77 -0
- package/dist/capture-v1/types.d.ts +52 -0
- package/dist/capture-v1/types.d.ts.map +1 -0
- package/dist/capture-v1/types.js +44 -0
- package/dist/capture-v1/types.mjs +3 -0
- package/dist/capture-v1/uuid.d.ts +9 -0
- package/dist/capture-v1/uuid.d.ts.map +1 -0
- package/dist/capture-v1/uuid.js +75 -0
- package/dist/capture-v1/uuid.mjs +34 -0
- package/dist/featureFlagLocalEvaluation.d.ts +2 -0
- package/dist/featureFlagLocalEvaluation.d.ts.map +1 -1
- package/dist/featureFlagLocalEvaluation.js +38 -0
- package/dist/featureFlagLocalEvaluation.mjs +38 -0
- package/dist/featureFlagUtils.d.ts +7 -5
- package/dist/featureFlagUtils.d.ts.map +1 -1
- package/dist/featureFlagUtils.js +17 -11
- package/dist/featureFlagUtils.mjs +14 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -16
- package/dist/index.mjs +1 -1
- package/dist/posthog-core-stateless.d.ts +44 -7
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +103 -24
- package/dist/posthog-core-stateless.mjs +103 -24
- package/dist/posthog-core.d.ts +138 -53
- package/dist/posthog-core.d.ts.map +1 -1
- package/dist/posthog-core.js +341 -115
- package/dist/posthog-core.mjs +338 -115
- package/dist/types.d.ts +33 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +25 -12
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/json-utils.d.ts +11 -1
- package/dist/utils/json-utils.d.ts.map +1 -1
- package/dist/utils/json-utils.js +50 -2
- package/dist/utils/json-utils.mjs +46 -1
- package/package.json +13 -3
- package/src/capture-v1/compression.ts +50 -0
- package/src/capture-v1/errors.ts +91 -0
- package/src/capture-v1/index.ts +34 -0
- package/src/capture-v1/sender.ts +779 -0
- package/src/capture-v1/serialize.ts +113 -0
- package/src/capture-v1/transform.ts +135 -0
- package/src/capture-v1/types.ts +59 -0
- package/src/capture-v1/uuid.ts +53 -0
- package/src/featureFlagLocalEvaluation.ts +51 -0
- package/src/featureFlagUtils.ts +22 -10
- package/src/index.ts +1 -0
- package/src/posthog-core-stateless.ts +190 -35
- package/src/posthog-core.ts +615 -178
- package/src/types.ts +31 -1
- package/src/utils/index.ts +1 -1
- package/src/utils/json-utils.ts +64 -1
- package/src/error-tracking/chunk-ids.spec.ts +0 -77
- package/src/error-tracking/coercers/error-event-coercer.spec.ts +0 -142
- package/src/error-tracking/coercers/promise-rejection-event.spec.ts +0 -77
- package/src/error-tracking/coercers/string-coercer.spec.ts +0 -26
- package/src/error-tracking/error-properties-builder.aggregate-errors.spec.ts +0 -412
- package/src/error-tracking/error-properties-builder.coerce.spec.ts +0 -337
- package/src/error-tracking/error-properties-builder.metadata.spec.ts +0 -318
- package/src/error-tracking/error-properties-builder.parse.spec.ts +0 -37
- package/src/error-tracking/exception-steps.spec.ts +0 -115
- package/src/error-tracking/parsers/base.spec.ts +0 -156
- package/src/error-tracking/parsers/index.spec.ts +0 -270
- package/src/error-tracking/parsers/node.spec.ts +0 -34
- package/src/error-tracking/release.spec.ts +0 -34
- package/src/logs/index.spec.ts +0 -2421
- package/src/logs/logs-utils.spec.ts +0 -266
- package/src/metrics/index.spec.ts +0 -1249
- package/src/surveys/activation.spec.ts +0 -61
- package/src/surveys/events.spec.ts +0 -183
- package/src/surveys/keys.spec.ts +0 -29
- package/src/surveys/property-matching.spec.ts +0 -539
- package/src/surveys/shuffling.spec.ts +0 -51
- package/src/surveys/translations.spec.ts +0 -274
- package/src/surveys/validation.spec.ts +0 -168
- package/src/traces/config.spec.ts +0 -271
- package/src/traces/ids.spec.ts +0 -115
- package/src/traces/index.spec.ts +0 -3287
- package/src/traces/live-spans.spec.ts +0 -69
- package/src/traces/otlp.spec.ts +0 -368
- package/src/traces/span.spec.ts +0 -1137
- package/src/traces/traceparent.spec.ts +0 -184
- package/src/utils/bot-detection.spec.ts +0 -107
- package/src/utils/bucketed-rate-limiter.spec.ts +0 -327
- package/src/utils/json-utils.spec.ts +0 -115
- package/src/utils/named-error.spec.ts +0 -36
- package/src/utils/number-utils.spec.ts +0 -212
- package/src/utils/otlp-any-value.spec.ts +0 -376
- package/src/utils/otlp-resource.spec.ts +0 -171
- package/src/utils/promise-queue.spec.ts +0 -55
- package/src/utils/string-utils.spec.ts +0 -158
- package/src/utils/type-utils.spec.ts +0 -38
- package/src/utils/webview-app-utils.spec.ts +0 -118
- package/src/vendor/uuidv7.spec.ts +0 -26
|
@@ -37,6 +37,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
37
37
|
maybeAdd: ()=>maybeAdd
|
|
38
38
|
});
|
|
39
39
|
const external_eventemitter_js_namespaceObject = require("./eventemitter.js");
|
|
40
|
+
const external_event_options_js_namespaceObject = require("./event-options.js");
|
|
40
41
|
const external_featureFlagUtils_js_namespaceObject = require("./featureFlagUtils.js");
|
|
41
42
|
const external_gzip_js_namespaceObject = require("./gzip.js");
|
|
42
43
|
const external_types_js_namespaceObject = require("./types.js");
|
|
@@ -109,6 +110,7 @@ class PostHogFetchNetworkError extends Error {
|
|
|
109
110
|
} : {}), this.error = error, this.name = 'PostHogFetchNetworkError';
|
|
110
111
|
}
|
|
111
112
|
}
|
|
113
|
+
const FLAG_CALLED_OPTION_PROPERTIES = Object.values(external_event_options_js_namespaceObject.EVENT_OPTION_LEGACY_PROPERTIES);
|
|
112
114
|
const maybeAdd = (key, value)=>void 0 !== value ? {
|
|
113
115
|
[key]: value
|
|
114
116
|
} : {};
|
|
@@ -232,7 +234,7 @@ class PostHogCoreStateless {
|
|
|
232
234
|
this._isInitialized = true;
|
|
233
235
|
this.evaluationContexts = options?.evaluationContexts ?? options?.evaluationEnvironments;
|
|
234
236
|
if (options?.evaluationEnvironments && !options?.evaluationContexts) this._logger.warn('evaluationEnvironments is deprecated. Use evaluationContexts instead. This property will be removed in a future version.');
|
|
235
|
-
this.disableCompression = !
|
|
237
|
+
this.disableCompression = !this.isCompressionAvailable() || (options?.disableCompression ?? false);
|
|
236
238
|
}
|
|
237
239
|
logMsgIfDebug(fn) {
|
|
238
240
|
if (this.isDebug) fn();
|
|
@@ -287,7 +289,7 @@ class PostHogCoreStateless {
|
|
|
287
289
|
const userProperties = payload.properties || {};
|
|
288
290
|
let properties = this.mergeCommonEventProperties(userProperties);
|
|
289
291
|
applyCallerFeatureFlagOverrides(properties, userProperties);
|
|
290
|
-
if ('$feature_flag_called' === payload.event && false === properties.$feature_flag_has_experiment && this.isMinimalFlagCalledEventsEnabled()) properties = (0, external_featureFlagUtils_js_namespaceObject.minimizeFlagCalledEventProperties)(properties);
|
|
292
|
+
if ('$feature_flag_called' === payload.event && false === properties.$feature_flag_has_experiment && this.isMinimalFlagCalledEventsEnabled()) properties = (0, external_featureFlagUtils_js_namespaceObject.minimizeFlagCalledEventProperties)(properties, FLAG_CALLED_OPTION_PROPERTIES);
|
|
291
293
|
return {
|
|
292
294
|
distinct_id: payload.distinct_id,
|
|
293
295
|
event: payload.event,
|
|
@@ -527,13 +529,13 @@ class PostHogCoreStateless {
|
|
|
527
529
|
const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate)).payloads;
|
|
528
530
|
return payloads;
|
|
529
531
|
}
|
|
530
|
-
async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
532
|
+
async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys) {
|
|
531
533
|
await this._initPromise;
|
|
532
|
-
return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
|
|
534
|
+
return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys);
|
|
533
535
|
}
|
|
534
|
-
async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
536
|
+
async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys) {
|
|
535
537
|
await this._initPromise;
|
|
536
|
-
const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
|
|
538
|
+
const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys);
|
|
537
539
|
if (!featureFlagDetails) return {
|
|
538
540
|
flags: void 0,
|
|
539
541
|
payloads: void 0,
|
|
@@ -545,12 +547,23 @@ class PostHogCoreStateless {
|
|
|
545
547
|
requestId: featureFlagDetails.requestId
|
|
546
548
|
};
|
|
547
549
|
}
|
|
548
|
-
async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
550
|
+
async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys) {
|
|
549
551
|
await this._initPromise;
|
|
550
552
|
const extraPayload = {
|
|
551
553
|
geoip_disable: disableGeoip ?? this.disableGeoip
|
|
552
554
|
};
|
|
553
555
|
if (flagKeysToEvaluate) extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
|
|
556
|
+
const personKeysKnownAbsent = knownAbsentPropertyKeys?.personPropertyKeysKnownAbsent;
|
|
557
|
+
if (personKeysKnownAbsent && personKeysKnownAbsent.length > 0) extraPayload['person_property_keys_known_absent'] = [
|
|
558
|
+
...personKeysKnownAbsent
|
|
559
|
+
];
|
|
560
|
+
const groupKeysKnownAbsent = Object.fromEntries(Object.entries(knownAbsentPropertyKeys?.groupPropertyKeysKnownAbsent ?? {}).filter(([, keys])=>keys && keys.length > 0).map(([groupType, keys])=>[
|
|
561
|
+
groupType,
|
|
562
|
+
[
|
|
563
|
+
...keys
|
|
564
|
+
]
|
|
565
|
+
]));
|
|
566
|
+
if (Object.keys(groupKeysKnownAbsent).length > 0) extraPayload['group_property_keys_known_absent'] = groupKeysKnownAbsent;
|
|
554
567
|
const result = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
555
568
|
if (!result.success) return;
|
|
556
569
|
const flagsResponse = result.response;
|
|
@@ -680,6 +693,23 @@ class PostHogCoreStateless {
|
|
|
680
693
|
this._events.emit('error', err);
|
|
681
694
|
}
|
|
682
695
|
}
|
|
696
|
+
batchByteTarget() {}
|
|
697
|
+
batchEventBytes(_message) {
|
|
698
|
+
return 0;
|
|
699
|
+
}
|
|
700
|
+
resendBatchSize(_messages) {}
|
|
701
|
+
countWithinBatchByteTarget(messages) {
|
|
702
|
+
const target = this.batchByteTarget();
|
|
703
|
+
if (void 0 === target) return messages.length;
|
|
704
|
+
let total = 0;
|
|
705
|
+
for(let index = 0; index < messages.length; index++){
|
|
706
|
+
const message = messages[index];
|
|
707
|
+
const bytes = void 0 === message ? 0 : this.batchEventBytes(message);
|
|
708
|
+
if (index > 0 && total + bytes > target) return index;
|
|
709
|
+
total += bytes;
|
|
710
|
+
}
|
|
711
|
+
return messages.length;
|
|
712
|
+
}
|
|
683
713
|
normalizeMessage(message) {
|
|
684
714
|
const { type: _type, library, library_version, ...sanitizedMessage } = message;
|
|
685
715
|
let properties = isPostHogEventProperties(sanitizedMessage.properties) ? sanitizedMessage.properties : void 0;
|
|
@@ -692,7 +722,7 @@ class PostHogCoreStateless {
|
|
|
692
722
|
$lib_version: library_version
|
|
693
723
|
};
|
|
694
724
|
if (properties) sanitizedMessage.properties = properties;
|
|
695
|
-
sanitizedMessage.uuid = (0, index_js_namespaceObject.getEventUuid)(sanitizedMessage.uuid,
|
|
725
|
+
sanitizedMessage.uuid = (0, index_js_namespaceObject.getEventUuid)(sanitizedMessage.uuid, ()=>this.generateEventUuid());
|
|
696
726
|
return sanitizedMessage;
|
|
697
727
|
}
|
|
698
728
|
normalizeTimestampForWire(timestamp) {
|
|
@@ -702,11 +732,14 @@ class PostHogCoreStateless {
|
|
|
702
732
|
const fractionalSeconds = 'string' == typeof timestamp ? timestamp.match(/\.(\d+)(?:Z|[+-]\d{2}:?\d{2})?$/i)?.[1] : void 0;
|
|
703
733
|
return fractionalSeconds && fractionalSeconds.length > 3 ? normalized.replace(/\.\d{3}Z$/, `.${fractionalSeconds}Z`) : normalized;
|
|
704
734
|
}
|
|
735
|
+
generateEventUuid() {
|
|
736
|
+
return (0, uuidv7_js_namespaceObject.uuidv7)();
|
|
737
|
+
}
|
|
705
738
|
prepareMessage(_message, options) {
|
|
706
739
|
const message = {
|
|
707
740
|
..._message,
|
|
708
741
|
timestamp: options?.timestamp ? options?.timestamp : (0, index_js_namespaceObject.currentISOTime)(),
|
|
709
|
-
uuid: (0, index_js_namespaceObject.getEventUuid)(options?.uuid,
|
|
742
|
+
uuid: (0, index_js_namespaceObject.getEventUuid)(options?.uuid, ()=>this.generateEventUuid())
|
|
710
743
|
};
|
|
711
744
|
const addGeoipDisableProperty = options?.disableGeoip ?? this.disableGeoip;
|
|
712
745
|
if (addGeoipDisableProperty) {
|
|
@@ -734,6 +767,9 @@ class PostHogCoreStateless {
|
|
|
734
767
|
flushAutomatic() {
|
|
735
768
|
return this.flush();
|
|
736
769
|
}
|
|
770
|
+
flushHoldMs() {
|
|
771
|
+
return 0;
|
|
772
|
+
}
|
|
737
773
|
async waitForPendingPromises(maxPromiseId, ignoredPromises = []) {
|
|
738
774
|
const ignoredPendingPromises = ignoredPromises.filter((promise)=>!!promise);
|
|
739
775
|
let iteration = 0;
|
|
@@ -768,6 +804,7 @@ class PostHogCoreStateless {
|
|
|
768
804
|
previousFlushPromise
|
|
769
805
|
])).then(()=>{
|
|
770
806
|
if (this.pendingFlushPromise === nextFlushPromise) this.pendingFlushPromise = null;
|
|
807
|
+
if (this.flushHoldMs() > 0) return;
|
|
771
808
|
return this._flush();
|
|
772
809
|
});
|
|
773
810
|
this.pendingFlushPromise = nextFlushPromise;
|
|
@@ -789,6 +826,9 @@ class PostHogCoreStateless {
|
|
|
789
826
|
if (customUserAgent && '' !== customUserAgent) headers['User-Agent'] = customUserAgent;
|
|
790
827
|
return headers;
|
|
791
828
|
}
|
|
829
|
+
isCompressionAvailable() {
|
|
830
|
+
return (0, external_gzip_js_namespaceObject.isGzipSupported)();
|
|
831
|
+
}
|
|
792
832
|
compressPayload(payload) {
|
|
793
833
|
return (0, external_gzip_js_namespaceObject.gzipCompress)(payload, this.isDebug);
|
|
794
834
|
}
|
|
@@ -861,23 +901,27 @@ class PostHogCoreStateless {
|
|
|
861
901
|
const originalQueueLength = queue.length;
|
|
862
902
|
let sentFromRoute = 0;
|
|
863
903
|
while(queue.length > 0 && sentFromRoute < originalQueueLength){
|
|
864
|
-
const
|
|
865
|
-
const
|
|
866
|
-
const
|
|
904
|
+
const countItems = queue.slice(0, this.maxBatchSize);
|
|
905
|
+
const countMessages = countItems.map((item)=>void 0 === item.message ? item.message : this.normalizeMessage(item.message));
|
|
906
|
+
const batchSize = Math.min(this.countWithinBatchByteTarget(countMessages), this.resendBatchSize(countMessages) ?? countMessages.length);
|
|
907
|
+
const batchItems = countItems.slice(0, batchSize);
|
|
908
|
+
const batchMessages = countMessages.slice(0, batchSize);
|
|
909
|
+
const persistQueueChange = async (keepIndices)=>{
|
|
910
|
+
const removedBatchItems = keepIndices ? batchItems.filter((_, index)=>!keepIndices.has(index)) : batchItems;
|
|
911
|
+
if (0 === removedBatchItems.length) return void await this.flushStorage();
|
|
867
912
|
const refreshedQueue = this.getPersistedProperty(queueKey) || [];
|
|
868
|
-
const
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
|
|
874
|
-
if (-1
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
});
|
|
913
|
+
const removedQueueIndices = new Set();
|
|
914
|
+
const findQueueIndex = (matches)=>refreshedQueue.findIndex((item, index)=>!removedQueueIndices.has(index) && matches(item));
|
|
915
|
+
for (const batchItem of removedBatchItems){
|
|
916
|
+
let queueIndex = findQueueIndex((item)=>item === batchItem);
|
|
917
|
+
const uuid = batchItem.message?.uuid;
|
|
918
|
+
if (-1 === queueIndex && 'string' == typeof uuid && uuid.length > 0) queueIndex = findQueueIndex((item)=>item.message?.uuid === uuid);
|
|
919
|
+
if (-1 !== queueIndex) removedQueueIndices.add(queueIndex);
|
|
920
|
+
}
|
|
921
|
+
const newQueue = refreshedQueue.filter((_, index)=>!removedQueueIndices.has(index));
|
|
878
922
|
this.setPersistedProperty(queueKey, newQueue);
|
|
879
923
|
queue = newQueue;
|
|
880
|
-
this._dequeuedMessagesCount +=
|
|
924
|
+
this._dequeuedMessagesCount += removedBatchItems.length;
|
|
881
925
|
await this.flushStorage();
|
|
882
926
|
};
|
|
883
927
|
const retryOptions = {
|
|
@@ -886,11 +930,13 @@ class PostHogCoreStateless {
|
|
|
886
930
|
return isPostHogFetchRetryableError(err);
|
|
887
931
|
}
|
|
888
932
|
};
|
|
933
|
+
let outcome;
|
|
889
934
|
try {
|
|
890
|
-
await this.sendBatch(batchMessages, retryOptions, route);
|
|
935
|
+
outcome = await this.sendBatch(batchMessages, retryOptions, route);
|
|
891
936
|
} catch (err) {
|
|
892
937
|
if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
|
|
893
938
|
this.maxBatchSize = Math.max(1, Math.floor(batchMessages.length / 2));
|
|
939
|
+
this.batchSizeBeforeTimeout = void 0;
|
|
894
940
|
this._logger.warn(`Received 413 when sending batch of size ${batchMessages.length}, reducing batch size to ${this.maxBatchSize}`);
|
|
895
941
|
continue;
|
|
896
942
|
}
|
|
@@ -898,9 +944,31 @@ class PostHogCoreStateless {
|
|
|
898
944
|
this._events.emit('error', err);
|
|
899
945
|
throw err;
|
|
900
946
|
}
|
|
947
|
+
const keepIndices = outcome ? new Set(outcome.keepIndices) : void 0;
|
|
948
|
+
if (outcome?.tooLarge && keepIndices && keepIndices.size > 1) {
|
|
949
|
+
if (keepIndices.size < batchMessages.length) await persistQueueChange(keepIndices);
|
|
950
|
+
this.maxBatchSize = Math.max(1, Math.floor(keepIndices.size / 2));
|
|
951
|
+
this.batchSizeBeforeTimeout = void 0;
|
|
952
|
+
this._logger.warn(`Received 413 when sending batch of size ${keepIndices.size}, reducing batch size to ${this.maxBatchSize}`);
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
if (keepIndices && keepIndices.size > 0) {
|
|
956
|
+
if (outcome?.timedOut && keepIndices.size > 1) {
|
|
957
|
+
this.batchSizeBeforeTimeout ??= this.maxBatchSize;
|
|
958
|
+
this.maxBatchSize = Math.max(1, Math.floor(keepIndices.size / 2));
|
|
959
|
+
this._logger.warn(`Request timed out when sending batch of size ${keepIndices.size}, reducing batch size to ${this.maxBatchSize}`);
|
|
960
|
+
}
|
|
961
|
+
await persistQueueChange(keepIndices);
|
|
962
|
+
sentMessages.push(...batchMessages.filter((_, index)=>!keepIndices.has(index)));
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
901
965
|
await persistQueueChange();
|
|
902
966
|
sentMessages.push(...batchMessages);
|
|
903
967
|
sentFromRoute += batchMessages.length;
|
|
968
|
+
if (void 0 !== this.batchSizeBeforeTimeout) {
|
|
969
|
+
this.maxBatchSize = Math.min(this.batchSizeBeforeTimeout, 2 * this.maxBatchSize);
|
|
970
|
+
if (this.maxBatchSize >= this.batchSizeBeforeTimeout) this.batchSizeBeforeTimeout = void 0;
|
|
971
|
+
}
|
|
904
972
|
}
|
|
905
973
|
}
|
|
906
974
|
async _sendOtlpBatch({ path, auth, payload }) {
|
|
@@ -1085,6 +1153,7 @@ class PostHogCoreStateless {
|
|
|
1085
1153
|
async _shutdown(shutdownTimeoutMs = 30000) {
|
|
1086
1154
|
await this._initPromise;
|
|
1087
1155
|
let hasTimedOut = false;
|
|
1156
|
+
const deadline = Date.now() + shutdownTimeoutMs;
|
|
1088
1157
|
this.clearFlushTimer();
|
|
1089
1158
|
if (this.disabled) return;
|
|
1090
1159
|
const doShutdown = async ()=>{
|
|
@@ -1093,6 +1162,16 @@ class PostHogCoreStateless {
|
|
|
1093
1162
|
while(true){
|
|
1094
1163
|
const hasQueuedEvents = this.getActiveQueueRoutes().some((route)=>this.getRouteQueue(route).length > 0);
|
|
1095
1164
|
if (!hasQueuedEvents) break;
|
|
1165
|
+
const holdMs = this.flushHoldMs();
|
|
1166
|
+
if (holdMs > 0) {
|
|
1167
|
+
if (Date.now() + holdMs >= deadline) {
|
|
1168
|
+
this._logger.warn('Shutdown stopped draining: the endpoint asked to wait past the shutdown timeout.');
|
|
1169
|
+
break;
|
|
1170
|
+
}
|
|
1171
|
+
await new Promise((resolve)=>(0, index_js_namespaceObject.safeSetTimeout)(resolve, holdMs));
|
|
1172
|
+
if (hasTimedOut) break;
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1096
1175
|
const dequeuedBeforeFlush = this._dequeuedMessagesCount;
|
|
1097
1176
|
await this.flush();
|
|
1098
1177
|
if (hasTimedOut) break;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SimpleEventEmitter } from "./eventemitter.mjs";
|
|
2
|
+
import { EVENT_OPTION_LEGACY_PROPERTIES } from "./event-options.mjs";
|
|
2
3
|
import { getFeatureFlagValue, minimizeFlagCalledEventProperties, normalizeFlagsResponse } from "./featureFlagUtils.mjs";
|
|
3
4
|
import { gzipCompress, isGzipSupported } from "./gzip.mjs";
|
|
4
5
|
import { PostHogPersistedProperty } from "./types.mjs";
|
|
@@ -71,6 +72,7 @@ class PostHogFetchNetworkError extends Error {
|
|
|
71
72
|
} : {}), this.error = error, this.name = 'PostHogFetchNetworkError';
|
|
72
73
|
}
|
|
73
74
|
}
|
|
75
|
+
const FLAG_CALLED_OPTION_PROPERTIES = Object.values(EVENT_OPTION_LEGACY_PROPERTIES);
|
|
74
76
|
const maybeAdd = (key, value)=>void 0 !== value ? {
|
|
75
77
|
[key]: value
|
|
76
78
|
} : {};
|
|
@@ -194,7 +196,7 @@ class PostHogCoreStateless {
|
|
|
194
196
|
this._isInitialized = true;
|
|
195
197
|
this.evaluationContexts = options?.evaluationContexts ?? options?.evaluationEnvironments;
|
|
196
198
|
if (options?.evaluationEnvironments && !options?.evaluationContexts) this._logger.warn('evaluationEnvironments is deprecated. Use evaluationContexts instead. This property will be removed in a future version.');
|
|
197
|
-
this.disableCompression = !
|
|
199
|
+
this.disableCompression = !this.isCompressionAvailable() || (options?.disableCompression ?? false);
|
|
198
200
|
}
|
|
199
201
|
logMsgIfDebug(fn) {
|
|
200
202
|
if (this.isDebug) fn();
|
|
@@ -249,7 +251,7 @@ class PostHogCoreStateless {
|
|
|
249
251
|
const userProperties = payload.properties || {};
|
|
250
252
|
let properties = this.mergeCommonEventProperties(userProperties);
|
|
251
253
|
applyCallerFeatureFlagOverrides(properties, userProperties);
|
|
252
|
-
if ('$feature_flag_called' === payload.event && false === properties.$feature_flag_has_experiment && this.isMinimalFlagCalledEventsEnabled()) properties = minimizeFlagCalledEventProperties(properties);
|
|
254
|
+
if ('$feature_flag_called' === payload.event && false === properties.$feature_flag_has_experiment && this.isMinimalFlagCalledEventsEnabled()) properties = minimizeFlagCalledEventProperties(properties, FLAG_CALLED_OPTION_PROPERTIES);
|
|
253
255
|
return {
|
|
254
256
|
distinct_id: payload.distinct_id,
|
|
255
257
|
event: payload.event,
|
|
@@ -489,13 +491,13 @@ class PostHogCoreStateless {
|
|
|
489
491
|
const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate)).payloads;
|
|
490
492
|
return payloads;
|
|
491
493
|
}
|
|
492
|
-
async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
494
|
+
async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys) {
|
|
493
495
|
await this._initPromise;
|
|
494
|
-
return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
|
|
496
|
+
return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys);
|
|
495
497
|
}
|
|
496
|
-
async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
498
|
+
async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys) {
|
|
497
499
|
await this._initPromise;
|
|
498
|
-
const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
|
|
500
|
+
const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys);
|
|
499
501
|
if (!featureFlagDetails) return {
|
|
500
502
|
flags: void 0,
|
|
501
503
|
payloads: void 0,
|
|
@@ -507,12 +509,23 @@ class PostHogCoreStateless {
|
|
|
507
509
|
requestId: featureFlagDetails.requestId
|
|
508
510
|
};
|
|
509
511
|
}
|
|
510
|
-
async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
512
|
+
async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate, knownAbsentPropertyKeys) {
|
|
511
513
|
await this._initPromise;
|
|
512
514
|
const extraPayload = {
|
|
513
515
|
geoip_disable: disableGeoip ?? this.disableGeoip
|
|
514
516
|
};
|
|
515
517
|
if (flagKeysToEvaluate) extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
|
|
518
|
+
const personKeysKnownAbsent = knownAbsentPropertyKeys?.personPropertyKeysKnownAbsent;
|
|
519
|
+
if (personKeysKnownAbsent && personKeysKnownAbsent.length > 0) extraPayload['person_property_keys_known_absent'] = [
|
|
520
|
+
...personKeysKnownAbsent
|
|
521
|
+
];
|
|
522
|
+
const groupKeysKnownAbsent = Object.fromEntries(Object.entries(knownAbsentPropertyKeys?.groupPropertyKeysKnownAbsent ?? {}).filter(([, keys])=>keys && keys.length > 0).map(([groupType, keys])=>[
|
|
523
|
+
groupType,
|
|
524
|
+
[
|
|
525
|
+
...keys
|
|
526
|
+
]
|
|
527
|
+
]));
|
|
528
|
+
if (Object.keys(groupKeysKnownAbsent).length > 0) extraPayload['group_property_keys_known_absent'] = groupKeysKnownAbsent;
|
|
516
529
|
const result = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
517
530
|
if (!result.success) return;
|
|
518
531
|
const flagsResponse = result.response;
|
|
@@ -642,6 +655,23 @@ class PostHogCoreStateless {
|
|
|
642
655
|
this._events.emit('error', err);
|
|
643
656
|
}
|
|
644
657
|
}
|
|
658
|
+
batchByteTarget() {}
|
|
659
|
+
batchEventBytes(_message) {
|
|
660
|
+
return 0;
|
|
661
|
+
}
|
|
662
|
+
resendBatchSize(_messages) {}
|
|
663
|
+
countWithinBatchByteTarget(messages) {
|
|
664
|
+
const target = this.batchByteTarget();
|
|
665
|
+
if (void 0 === target) return messages.length;
|
|
666
|
+
let total = 0;
|
|
667
|
+
for(let index = 0; index < messages.length; index++){
|
|
668
|
+
const message = messages[index];
|
|
669
|
+
const bytes = void 0 === message ? 0 : this.batchEventBytes(message);
|
|
670
|
+
if (index > 0 && total + bytes > target) return index;
|
|
671
|
+
total += bytes;
|
|
672
|
+
}
|
|
673
|
+
return messages.length;
|
|
674
|
+
}
|
|
645
675
|
normalizeMessage(message) {
|
|
646
676
|
const { type: _type, library, library_version, ...sanitizedMessage } = message;
|
|
647
677
|
let properties = isPostHogEventProperties(sanitizedMessage.properties) ? sanitizedMessage.properties : void 0;
|
|
@@ -654,7 +684,7 @@ class PostHogCoreStateless {
|
|
|
654
684
|
$lib_version: library_version
|
|
655
685
|
};
|
|
656
686
|
if (properties) sanitizedMessage.properties = properties;
|
|
657
|
-
sanitizedMessage.uuid = getEventUuid(sanitizedMessage.uuid,
|
|
687
|
+
sanitizedMessage.uuid = getEventUuid(sanitizedMessage.uuid, ()=>this.generateEventUuid());
|
|
658
688
|
return sanitizedMessage;
|
|
659
689
|
}
|
|
660
690
|
normalizeTimestampForWire(timestamp) {
|
|
@@ -664,11 +694,14 @@ class PostHogCoreStateless {
|
|
|
664
694
|
const fractionalSeconds = 'string' == typeof timestamp ? timestamp.match(/\.(\d+)(?:Z|[+-]\d{2}:?\d{2})?$/i)?.[1] : void 0;
|
|
665
695
|
return fractionalSeconds && fractionalSeconds.length > 3 ? normalized.replace(/\.\d{3}Z$/, `.${fractionalSeconds}Z`) : normalized;
|
|
666
696
|
}
|
|
697
|
+
generateEventUuid() {
|
|
698
|
+
return uuidv7();
|
|
699
|
+
}
|
|
667
700
|
prepareMessage(_message, options) {
|
|
668
701
|
const message = {
|
|
669
702
|
..._message,
|
|
670
703
|
timestamp: options?.timestamp ? options?.timestamp : currentISOTime(),
|
|
671
|
-
uuid: getEventUuid(options?.uuid,
|
|
704
|
+
uuid: getEventUuid(options?.uuid, ()=>this.generateEventUuid())
|
|
672
705
|
};
|
|
673
706
|
const addGeoipDisableProperty = options?.disableGeoip ?? this.disableGeoip;
|
|
674
707
|
if (addGeoipDisableProperty) {
|
|
@@ -696,6 +729,9 @@ class PostHogCoreStateless {
|
|
|
696
729
|
flushAutomatic() {
|
|
697
730
|
return this.flush();
|
|
698
731
|
}
|
|
732
|
+
flushHoldMs() {
|
|
733
|
+
return 0;
|
|
734
|
+
}
|
|
699
735
|
async waitForPendingPromises(maxPromiseId, ignoredPromises = []) {
|
|
700
736
|
const ignoredPendingPromises = ignoredPromises.filter((promise)=>!!promise);
|
|
701
737
|
let iteration = 0;
|
|
@@ -730,6 +766,7 @@ class PostHogCoreStateless {
|
|
|
730
766
|
previousFlushPromise
|
|
731
767
|
])).then(()=>{
|
|
732
768
|
if (this.pendingFlushPromise === nextFlushPromise) this.pendingFlushPromise = null;
|
|
769
|
+
if (this.flushHoldMs() > 0) return;
|
|
733
770
|
return this._flush();
|
|
734
771
|
});
|
|
735
772
|
this.pendingFlushPromise = nextFlushPromise;
|
|
@@ -751,6 +788,9 @@ class PostHogCoreStateless {
|
|
|
751
788
|
if (customUserAgent && '' !== customUserAgent) headers['User-Agent'] = customUserAgent;
|
|
752
789
|
return headers;
|
|
753
790
|
}
|
|
791
|
+
isCompressionAvailable() {
|
|
792
|
+
return isGzipSupported();
|
|
793
|
+
}
|
|
754
794
|
compressPayload(payload) {
|
|
755
795
|
return gzipCompress(payload, this.isDebug);
|
|
756
796
|
}
|
|
@@ -823,23 +863,27 @@ class PostHogCoreStateless {
|
|
|
823
863
|
const originalQueueLength = queue.length;
|
|
824
864
|
let sentFromRoute = 0;
|
|
825
865
|
while(queue.length > 0 && sentFromRoute < originalQueueLength){
|
|
826
|
-
const
|
|
827
|
-
const
|
|
828
|
-
const
|
|
866
|
+
const countItems = queue.slice(0, this.maxBatchSize);
|
|
867
|
+
const countMessages = countItems.map((item)=>void 0 === item.message ? item.message : this.normalizeMessage(item.message));
|
|
868
|
+
const batchSize = Math.min(this.countWithinBatchByteTarget(countMessages), this.resendBatchSize(countMessages) ?? countMessages.length);
|
|
869
|
+
const batchItems = countItems.slice(0, batchSize);
|
|
870
|
+
const batchMessages = countMessages.slice(0, batchSize);
|
|
871
|
+
const persistQueueChange = async (keepIndices)=>{
|
|
872
|
+
const removedBatchItems = keepIndices ? batchItems.filter((_, index)=>!keepIndices.has(index)) : batchItems;
|
|
873
|
+
if (0 === removedBatchItems.length) return void await this.flushStorage();
|
|
829
874
|
const refreshedQueue = this.getPersistedProperty(queueKey) || [];
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
const
|
|
835
|
-
|
|
836
|
-
if (-1
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
});
|
|
875
|
+
const removedQueueIndices = new Set();
|
|
876
|
+
const findQueueIndex = (matches)=>refreshedQueue.findIndex((item, index)=>!removedQueueIndices.has(index) && matches(item));
|
|
877
|
+
for (const batchItem of removedBatchItems){
|
|
878
|
+
let queueIndex = findQueueIndex((item)=>item === batchItem);
|
|
879
|
+
const uuid = batchItem.message?.uuid;
|
|
880
|
+
if (-1 === queueIndex && 'string' == typeof uuid && uuid.length > 0) queueIndex = findQueueIndex((item)=>item.message?.uuid === uuid);
|
|
881
|
+
if (-1 !== queueIndex) removedQueueIndices.add(queueIndex);
|
|
882
|
+
}
|
|
883
|
+
const newQueue = refreshedQueue.filter((_, index)=>!removedQueueIndices.has(index));
|
|
840
884
|
this.setPersistedProperty(queueKey, newQueue);
|
|
841
885
|
queue = newQueue;
|
|
842
|
-
this._dequeuedMessagesCount +=
|
|
886
|
+
this._dequeuedMessagesCount += removedBatchItems.length;
|
|
843
887
|
await this.flushStorage();
|
|
844
888
|
};
|
|
845
889
|
const retryOptions = {
|
|
@@ -848,11 +892,13 @@ class PostHogCoreStateless {
|
|
|
848
892
|
return isPostHogFetchRetryableError(err);
|
|
849
893
|
}
|
|
850
894
|
};
|
|
895
|
+
let outcome;
|
|
851
896
|
try {
|
|
852
|
-
await this.sendBatch(batchMessages, retryOptions, route);
|
|
897
|
+
outcome = await this.sendBatch(batchMessages, retryOptions, route);
|
|
853
898
|
} catch (err) {
|
|
854
899
|
if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
|
|
855
900
|
this.maxBatchSize = Math.max(1, Math.floor(batchMessages.length / 2));
|
|
901
|
+
this.batchSizeBeforeTimeout = void 0;
|
|
856
902
|
this._logger.warn(`Received 413 when sending batch of size ${batchMessages.length}, reducing batch size to ${this.maxBatchSize}`);
|
|
857
903
|
continue;
|
|
858
904
|
}
|
|
@@ -860,9 +906,31 @@ class PostHogCoreStateless {
|
|
|
860
906
|
this._events.emit('error', err);
|
|
861
907
|
throw err;
|
|
862
908
|
}
|
|
909
|
+
const keepIndices = outcome ? new Set(outcome.keepIndices) : void 0;
|
|
910
|
+
if (outcome?.tooLarge && keepIndices && keepIndices.size > 1) {
|
|
911
|
+
if (keepIndices.size < batchMessages.length) await persistQueueChange(keepIndices);
|
|
912
|
+
this.maxBatchSize = Math.max(1, Math.floor(keepIndices.size / 2));
|
|
913
|
+
this.batchSizeBeforeTimeout = void 0;
|
|
914
|
+
this._logger.warn(`Received 413 when sending batch of size ${keepIndices.size}, reducing batch size to ${this.maxBatchSize}`);
|
|
915
|
+
continue;
|
|
916
|
+
}
|
|
917
|
+
if (keepIndices && keepIndices.size > 0) {
|
|
918
|
+
if (outcome?.timedOut && keepIndices.size > 1) {
|
|
919
|
+
this.batchSizeBeforeTimeout ??= this.maxBatchSize;
|
|
920
|
+
this.maxBatchSize = Math.max(1, Math.floor(keepIndices.size / 2));
|
|
921
|
+
this._logger.warn(`Request timed out when sending batch of size ${keepIndices.size}, reducing batch size to ${this.maxBatchSize}`);
|
|
922
|
+
}
|
|
923
|
+
await persistQueueChange(keepIndices);
|
|
924
|
+
sentMessages.push(...batchMessages.filter((_, index)=>!keepIndices.has(index)));
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
863
927
|
await persistQueueChange();
|
|
864
928
|
sentMessages.push(...batchMessages);
|
|
865
929
|
sentFromRoute += batchMessages.length;
|
|
930
|
+
if (void 0 !== this.batchSizeBeforeTimeout) {
|
|
931
|
+
this.maxBatchSize = Math.min(this.batchSizeBeforeTimeout, 2 * this.maxBatchSize);
|
|
932
|
+
if (this.maxBatchSize >= this.batchSizeBeforeTimeout) this.batchSizeBeforeTimeout = void 0;
|
|
933
|
+
}
|
|
866
934
|
}
|
|
867
935
|
}
|
|
868
936
|
async _sendOtlpBatch({ path, auth, payload }) {
|
|
@@ -1047,6 +1115,7 @@ class PostHogCoreStateless {
|
|
|
1047
1115
|
async _shutdown(shutdownTimeoutMs = 30000) {
|
|
1048
1116
|
await this._initPromise;
|
|
1049
1117
|
let hasTimedOut = false;
|
|
1118
|
+
const deadline = Date.now() + shutdownTimeoutMs;
|
|
1050
1119
|
this.clearFlushTimer();
|
|
1051
1120
|
if (this.disabled) return;
|
|
1052
1121
|
const doShutdown = async ()=>{
|
|
@@ -1055,6 +1124,16 @@ class PostHogCoreStateless {
|
|
|
1055
1124
|
while(true){
|
|
1056
1125
|
const hasQueuedEvents = this.getActiveQueueRoutes().some((route)=>this.getRouteQueue(route).length > 0);
|
|
1057
1126
|
if (!hasQueuedEvents) break;
|
|
1127
|
+
const holdMs = this.flushHoldMs();
|
|
1128
|
+
if (holdMs > 0) {
|
|
1129
|
+
if (Date.now() + holdMs >= deadline) {
|
|
1130
|
+
this._logger.warn('Shutdown stopped draining: the endpoint asked to wait past the shutdown timeout.');
|
|
1131
|
+
break;
|
|
1132
|
+
}
|
|
1133
|
+
await new Promise((resolve)=>safeSetTimeout(resolve, holdMs));
|
|
1134
|
+
if (hasTimedOut) break;
|
|
1135
|
+
continue;
|
|
1136
|
+
}
|
|
1058
1137
|
const dequeuedBeforeFlush = this._dequeuedMessagesCount;
|
|
1059
1138
|
await this.flush();
|
|
1060
1139
|
if (hasTimedOut) break;
|