@posthog/core 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/error-tracking/chunk-ids.js +1 -1
- package/dist/error-tracking/chunk-ids.mjs +1 -1
- package/dist/error-tracking/coercers/error-event-coercer.js +4 -5
- package/dist/error-tracking/coercers/error-event-coercer.mjs +4 -5
- package/dist/error-tracking/coercers/event-coercer.js +1 -2
- package/dist/error-tracking/coercers/event-coercer.mjs +1 -2
- package/dist/error-tracking/coercers/object-coercer.js +1 -2
- package/dist/error-tracking/coercers/object-coercer.mjs +1 -2
- package/dist/error-tracking/coercers/primitive-coercer.js +1 -2
- package/dist/error-tracking/coercers/primitive-coercer.mjs +1 -2
- package/dist/error-tracking/coercers/promise-rejection-event.js +4 -5
- package/dist/error-tracking/coercers/promise-rejection-event.mjs +4 -5
- package/dist/error-tracking/coercers/string-coercer.js +3 -4
- package/dist/error-tracking/coercers/string-coercer.mjs +3 -4
- package/dist/error-tracking/coercers/utils.js +2 -4
- package/dist/error-tracking/coercers/utils.mjs +2 -4
- package/dist/error-tracking/error-properties-builder.d.ts +6 -6
- package/dist/error-tracking/error-properties-builder.d.ts.map +1 -1
- package/dist/error-tracking/error-properties-builder.js +17 -27
- package/dist/error-tracking/error-properties-builder.mjs +16 -26
- package/dist/error-tracking/parsers/index.js +2 -4
- package/dist/error-tracking/parsers/index.mjs +2 -4
- package/dist/error-tracking/parsers/node.js +3 -5
- package/dist/error-tracking/parsers/node.mjs +3 -5
- package/dist/error-tracking/utils.js +4 -4
- package/dist/error-tracking/utils.mjs +4 -4
- package/dist/eventemitter.js +4 -4
- package/dist/eventemitter.mjs +4 -4
- package/dist/featureFlagUtils.js +20 -45
- package/dist/featureFlagUtils.mjs +20 -45
- package/dist/gzip.js +1 -2
- package/dist/gzip.mjs +1 -2
- package/dist/index.d.ts +4 -366
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +54 -1225
- package/dist/index.mjs +5 -1190
- package/dist/posthog-core-stateless.d.ts +204 -0
- package/dist/posthog-core-stateless.d.ts.map +1 -0
- package/dist/posthog-core-stateless.js +675 -0
- package/dist/posthog-core-stateless.mjs +632 -0
- package/dist/posthog-core.d.ts +171 -0
- package/dist/posthog-core.d.ts.map +1 -0
- package/dist/posthog-core.js +554 -0
- package/dist/posthog-core.mjs +520 -0
- package/dist/testing/PostHogCoreTestClient.d.ts +2 -1
- package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -1
- package/dist/testing/PostHogCoreTestClient.js +9 -11
- package/dist/testing/PostHogCoreTestClient.mjs +8 -10
- package/dist/testing/test-utils.js +1 -1
- package/dist/testing/test-utils.mjs +1 -1
- package/dist/utils/bucketed-rate-limiter.js +8 -12
- package/dist/utils/bucketed-rate-limiter.mjs +8 -12
- package/dist/utils/index.js +3 -3
- package/dist/utils/index.mjs +3 -3
- package/dist/utils/type-utils.js +1 -1
- package/dist/utils/type-utils.mjs +1 -1
- package/dist/vendor/uuidv7.js +12 -16
- package/dist/vendor/uuidv7.mjs +12 -16
- package/package.json +3 -2
- package/src/__tests__/featureFlagUtils.spec.ts +427 -0
- package/src/__tests__/gzip.spec.ts +69 -0
- package/src/__tests__/posthog.ai.spec.ts +110 -0
- package/src/__tests__/posthog.capture.spec.ts +91 -0
- package/src/__tests__/posthog.core.spec.ts +135 -0
- package/src/__tests__/posthog.debug.spec.ts +36 -0
- package/src/__tests__/posthog.enqueue.spec.ts +93 -0
- package/src/__tests__/posthog.featureflags.spec.ts +1106 -0
- package/src/__tests__/posthog.featureflags.v1.spec.ts +922 -0
- package/src/__tests__/posthog.flush.spec.ts +237 -0
- package/src/__tests__/posthog.gdpr.spec.ts +50 -0
- package/src/__tests__/posthog.groups.spec.ts +96 -0
- package/src/__tests__/posthog.identify.spec.ts +194 -0
- package/src/__tests__/posthog.init.spec.ts +110 -0
- package/src/__tests__/posthog.listeners.spec.ts +51 -0
- package/src/__tests__/posthog.register.spec.ts +47 -0
- package/src/__tests__/posthog.reset.spec.ts +76 -0
- package/src/__tests__/posthog.sessions.spec.ts +63 -0
- package/src/__tests__/posthog.setProperties.spec.ts +102 -0
- package/src/__tests__/posthog.shutdown.spec.ts +88 -0
- package/src/__tests__/utils.spec.ts +36 -0
- package/src/error-tracking/chunk-ids.ts +58 -0
- package/src/error-tracking/coercers/dom-exception-coercer.ts +38 -0
- package/src/error-tracking/coercers/error-coercer.ts +36 -0
- package/src/error-tracking/coercers/error-event-coercer.ts +24 -0
- package/src/error-tracking/coercers/event-coercer.ts +19 -0
- package/src/error-tracking/coercers/index.ts +8 -0
- package/src/error-tracking/coercers/object-coercer.ts +76 -0
- package/src/error-tracking/coercers/primitive-coercer.ts +19 -0
- package/src/error-tracking/coercers/promise-rejection-event.spec.ts +77 -0
- package/src/error-tracking/coercers/promise-rejection-event.ts +53 -0
- package/src/error-tracking/coercers/string-coercer.spec.ts +26 -0
- package/src/error-tracking/coercers/string-coercer.ts +31 -0
- package/src/error-tracking/coercers/utils.ts +33 -0
- package/src/error-tracking/error-properties-builder.coerce.spec.ts +202 -0
- package/src/error-tracking/error-properties-builder.parse.spec.ts +30 -0
- package/src/error-tracking/error-properties-builder.ts +167 -0
- package/src/error-tracking/index.ts +5 -0
- package/src/error-tracking/parsers/base.ts +29 -0
- package/src/error-tracking/parsers/chrome.ts +53 -0
- package/src/error-tracking/parsers/gecko.ts +38 -0
- package/src/error-tracking/parsers/index.ts +104 -0
- package/src/error-tracking/parsers/node.ts +111 -0
- package/src/error-tracking/parsers/opera.ts +18 -0
- package/src/error-tracking/parsers/react-native.ts +0 -0
- package/src/error-tracking/parsers/safari.ts +33 -0
- package/src/error-tracking/parsers/winjs.ts +12 -0
- package/src/error-tracking/types.ts +107 -0
- package/src/error-tracking/utils.ts +39 -0
- package/src/eventemitter.ts +27 -0
- package/src/featureFlagUtils.ts +192 -0
- package/src/gzip.ts +29 -0
- package/src/index.ts +8 -0
- package/src/posthog-core-stateless.ts +1226 -0
- package/src/posthog-core.ts +958 -0
- package/src/testing/PostHogCoreTestClient.ts +91 -0
- package/src/testing/index.ts +2 -0
- package/src/testing/test-utils.ts +47 -0
- package/src/types.ts +544 -0
- package/src/utils/bucketed-rate-limiter.spec.ts +33 -0
- package/src/utils/bucketed-rate-limiter.ts +85 -0
- package/src/utils/index.ts +98 -0
- package/src/utils/number-utils.spec.ts +89 -0
- package/src/utils/number-utils.ts +30 -0
- package/src/utils/promise-queue.spec.ts +55 -0
- package/src/utils/promise-queue.ts +30 -0
- package/src/utils/string-utils.ts +23 -0
- package/src/utils/type-utils.ts +134 -0
- package/src/vendor/uuidv7.ts +479 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { SimpleEventEmitter } from "./eventemitter.mjs";
|
|
2
|
+
import { getFeatureFlagValue, normalizeFlagsResponse } from "./featureFlagUtils.mjs";
|
|
3
|
+
import { gzipCompress, isGzipSupported } from "./gzip.mjs";
|
|
4
|
+
import { PostHogPersistedProperty } from "./types.mjs";
|
|
5
|
+
import { PromiseQueue, STRING_FORMAT, allSettled, assert, currentISOTime, removeTrailingSlash, retriable, safeSetTimeout } from "./utils/index.mjs";
|
|
6
|
+
import { uuidv7 } from "./vendor/uuidv7.mjs";
|
|
7
|
+
class PostHogFetchHttpError extends Error {
|
|
8
|
+
constructor(response, reqByteLength){
|
|
9
|
+
super('HTTP error while fetching PostHog: status=' + response.status + ', reqByteLength=' + reqByteLength), this.response = response, this.reqByteLength = reqByteLength, this.name = 'PostHogFetchHttpError';
|
|
10
|
+
}
|
|
11
|
+
get status() {
|
|
12
|
+
return this.response.status;
|
|
13
|
+
}
|
|
14
|
+
get text() {
|
|
15
|
+
return this.response.text();
|
|
16
|
+
}
|
|
17
|
+
get json() {
|
|
18
|
+
return this.response.json();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
class PostHogFetchNetworkError extends Error {
|
|
22
|
+
constructor(error){
|
|
23
|
+
super('Network error while fetching PostHog', error instanceof Error ? {
|
|
24
|
+
cause: error
|
|
25
|
+
} : {}), this.error = error, this.name = 'PostHogFetchNetworkError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const maybeAdd = (key, value)=>void 0 !== value ? {
|
|
29
|
+
[key]: value
|
|
30
|
+
} : {};
|
|
31
|
+
async function logFlushError(err) {
|
|
32
|
+
if (err instanceof PostHogFetchHttpError) {
|
|
33
|
+
let text = '';
|
|
34
|
+
try {
|
|
35
|
+
text = await err.text;
|
|
36
|
+
} catch {}
|
|
37
|
+
console.error(`Error while flushing PostHog: message=${err.message}, response body=${text}`, err);
|
|
38
|
+
} else console.error('Error while flushing PostHog', err);
|
|
39
|
+
return Promise.resolve();
|
|
40
|
+
}
|
|
41
|
+
function isPostHogFetchError(err) {
|
|
42
|
+
return 'object' == typeof err && (err instanceof PostHogFetchHttpError || err instanceof PostHogFetchNetworkError);
|
|
43
|
+
}
|
|
44
|
+
function isPostHogFetchContentTooLargeError(err) {
|
|
45
|
+
return 'object' == typeof err && err instanceof PostHogFetchHttpError && 413 === err.status;
|
|
46
|
+
}
|
|
47
|
+
var posthog_core_stateless_QuotaLimitedFeature = /*#__PURE__*/ function(QuotaLimitedFeature) {
|
|
48
|
+
QuotaLimitedFeature["FeatureFlags"] = "feature_flags";
|
|
49
|
+
QuotaLimitedFeature["Recordings"] = "recordings";
|
|
50
|
+
return QuotaLimitedFeature;
|
|
51
|
+
}({});
|
|
52
|
+
class PostHogCoreStateless {
|
|
53
|
+
constructor(apiKey, options){
|
|
54
|
+
this.flushPromise = null;
|
|
55
|
+
this.shutdownPromise = null;
|
|
56
|
+
this.promiseQueue = new PromiseQueue();
|
|
57
|
+
this._events = new SimpleEventEmitter();
|
|
58
|
+
this._isInitialized = false;
|
|
59
|
+
assert(apiKey, "You must pass your PostHog project's api key.");
|
|
60
|
+
this.apiKey = apiKey;
|
|
61
|
+
this.host = removeTrailingSlash(options?.host || 'https://us.i.posthog.com');
|
|
62
|
+
this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 20;
|
|
63
|
+
this.maxBatchSize = Math.max(this.flushAt, options?.maxBatchSize ?? 100);
|
|
64
|
+
this.maxQueueSize = Math.max(this.flushAt, options?.maxQueueSize ?? 1000);
|
|
65
|
+
this.flushInterval = options?.flushInterval ?? 10000;
|
|
66
|
+
this.preloadFeatureFlags = options?.preloadFeatureFlags ?? true;
|
|
67
|
+
this.defaultOptIn = options?.defaultOptIn ?? true;
|
|
68
|
+
this.disableSurveys = options?.disableSurveys ?? false;
|
|
69
|
+
this._retryOptions = {
|
|
70
|
+
retryCount: options?.fetchRetryCount ?? 3,
|
|
71
|
+
retryDelay: options?.fetchRetryDelay ?? 3000,
|
|
72
|
+
retryCheck: isPostHogFetchError
|
|
73
|
+
};
|
|
74
|
+
this.requestTimeout = options?.requestTimeout ?? 10000;
|
|
75
|
+
this.featureFlagsRequestTimeoutMs = options?.featureFlagsRequestTimeoutMs ?? 3000;
|
|
76
|
+
this.remoteConfigRequestTimeoutMs = options?.remoteConfigRequestTimeoutMs ?? 3000;
|
|
77
|
+
this.disableGeoip = options?.disableGeoip ?? true;
|
|
78
|
+
this.disabled = options?.disabled ?? false;
|
|
79
|
+
this.historicalMigration = options?.historicalMigration ?? false;
|
|
80
|
+
this._initPromise = Promise.resolve();
|
|
81
|
+
this._isInitialized = true;
|
|
82
|
+
this.disableCompression = !isGzipSupported() || (options?.disableCompression ?? false);
|
|
83
|
+
}
|
|
84
|
+
logMsgIfDebug(fn) {
|
|
85
|
+
if (this.isDebug) fn();
|
|
86
|
+
}
|
|
87
|
+
wrap(fn) {
|
|
88
|
+
if (this.disabled) return void this.logMsgIfDebug(()=>console.warn('[PostHog] The client is disabled'));
|
|
89
|
+
if (this._isInitialized) return fn();
|
|
90
|
+
this._initPromise.then(()=>fn());
|
|
91
|
+
}
|
|
92
|
+
getCommonEventProperties() {
|
|
93
|
+
return {
|
|
94
|
+
$lib: this.getLibraryId(),
|
|
95
|
+
$lib_version: this.getLibraryVersion()
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
get optedOut() {
|
|
99
|
+
return this.getPersistedProperty(PostHogPersistedProperty.OptedOut) ?? !this.defaultOptIn;
|
|
100
|
+
}
|
|
101
|
+
async optIn() {
|
|
102
|
+
this.wrap(()=>{
|
|
103
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, false);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async optOut() {
|
|
107
|
+
this.wrap(()=>{
|
|
108
|
+
this.setPersistedProperty(PostHogPersistedProperty.OptedOut, true);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
on(event, cb) {
|
|
112
|
+
return this._events.on(event, cb);
|
|
113
|
+
}
|
|
114
|
+
debug(enabled = true) {
|
|
115
|
+
this.removeDebugCallback?.();
|
|
116
|
+
if (enabled) {
|
|
117
|
+
const removeDebugCallback = this.on('*', (event, payload)=>console.log('PostHog Debug', event, payload));
|
|
118
|
+
this.removeDebugCallback = ()=>{
|
|
119
|
+
removeDebugCallback();
|
|
120
|
+
this.removeDebugCallback = void 0;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
get isDebug() {
|
|
125
|
+
return !!this.removeDebugCallback;
|
|
126
|
+
}
|
|
127
|
+
get isDisabled() {
|
|
128
|
+
return this.disabled;
|
|
129
|
+
}
|
|
130
|
+
buildPayload(payload) {
|
|
131
|
+
return {
|
|
132
|
+
distinct_id: payload.distinct_id,
|
|
133
|
+
event: payload.event,
|
|
134
|
+
properties: {
|
|
135
|
+
...payload.properties || {},
|
|
136
|
+
...this.getCommonEventProperties()
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
addPendingPromise(promise) {
|
|
141
|
+
return this.promiseQueue.add(promise);
|
|
142
|
+
}
|
|
143
|
+
identifyStateless(distinctId, properties, options) {
|
|
144
|
+
this.wrap(()=>{
|
|
145
|
+
const payload = {
|
|
146
|
+
...this.buildPayload({
|
|
147
|
+
distinct_id: distinctId,
|
|
148
|
+
event: '$identify',
|
|
149
|
+
properties
|
|
150
|
+
})
|
|
151
|
+
};
|
|
152
|
+
this.enqueue('identify', payload, options);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async identifyStatelessImmediate(distinctId, properties, options) {
|
|
156
|
+
const payload = {
|
|
157
|
+
...this.buildPayload({
|
|
158
|
+
distinct_id: distinctId,
|
|
159
|
+
event: '$identify',
|
|
160
|
+
properties
|
|
161
|
+
})
|
|
162
|
+
};
|
|
163
|
+
await this.sendImmediate('identify', payload, options);
|
|
164
|
+
}
|
|
165
|
+
captureStateless(distinctId, event, properties, options) {
|
|
166
|
+
this.wrap(()=>{
|
|
167
|
+
const payload = this.buildPayload({
|
|
168
|
+
distinct_id: distinctId,
|
|
169
|
+
event,
|
|
170
|
+
properties
|
|
171
|
+
});
|
|
172
|
+
this.enqueue('capture', payload, options);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
async captureStatelessImmediate(distinctId, event, properties, options) {
|
|
176
|
+
const payload = this.buildPayload({
|
|
177
|
+
distinct_id: distinctId,
|
|
178
|
+
event,
|
|
179
|
+
properties
|
|
180
|
+
});
|
|
181
|
+
await this.sendImmediate('capture', payload, options);
|
|
182
|
+
}
|
|
183
|
+
aliasStateless(alias, distinctId, properties, options) {
|
|
184
|
+
this.wrap(()=>{
|
|
185
|
+
const payload = this.buildPayload({
|
|
186
|
+
event: '$create_alias',
|
|
187
|
+
distinct_id: distinctId,
|
|
188
|
+
properties: {
|
|
189
|
+
...properties || {},
|
|
190
|
+
distinct_id: distinctId,
|
|
191
|
+
alias
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
this.enqueue('alias', payload, options);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
async aliasStatelessImmediate(alias, distinctId, properties, options) {
|
|
198
|
+
const payload = this.buildPayload({
|
|
199
|
+
event: '$create_alias',
|
|
200
|
+
distinct_id: distinctId,
|
|
201
|
+
properties: {
|
|
202
|
+
...properties || {},
|
|
203
|
+
distinct_id: distinctId,
|
|
204
|
+
alias
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
await this.sendImmediate('alias', payload, options);
|
|
208
|
+
}
|
|
209
|
+
groupIdentifyStateless(groupType, groupKey, groupProperties, options, distinctId, eventProperties) {
|
|
210
|
+
this.wrap(()=>{
|
|
211
|
+
const payload = this.buildPayload({
|
|
212
|
+
distinct_id: distinctId || `$${groupType}_${groupKey}`,
|
|
213
|
+
event: '$groupidentify',
|
|
214
|
+
properties: {
|
|
215
|
+
$group_type: groupType,
|
|
216
|
+
$group_key: groupKey,
|
|
217
|
+
$group_set: groupProperties || {},
|
|
218
|
+
...eventProperties || {}
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
this.enqueue('capture', payload, options);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
async getRemoteConfig() {
|
|
225
|
+
await this._initPromise;
|
|
226
|
+
let host = this.host;
|
|
227
|
+
if ('https://us.i.posthog.com' === host) host = 'https://us-assets.i.posthog.com';
|
|
228
|
+
else if ('https://eu.i.posthog.com' === host) host = 'https://eu-assets.i.posthog.com';
|
|
229
|
+
const url = `${host}/array/${this.apiKey}/config`;
|
|
230
|
+
const fetchOptions = {
|
|
231
|
+
method: 'GET',
|
|
232
|
+
headers: {
|
|
233
|
+
...this.getCustomHeaders(),
|
|
234
|
+
'Content-Type': 'application/json'
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
return this.fetchWithRetry(url, fetchOptions, {
|
|
238
|
+
retryCount: 0
|
|
239
|
+
}, this.remoteConfigRequestTimeoutMs).then((response)=>response.json()).catch((error)=>{
|
|
240
|
+
this.logMsgIfDebug(()=>console.error('Remote config could not be loaded', error));
|
|
241
|
+
this._events.emit('error', error);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
async getFlags(distinctId, groups = {}, personProperties = {}, groupProperties = {}, extraPayload = {}) {
|
|
245
|
+
await this._initPromise;
|
|
246
|
+
const url = `${this.host}/flags/?v=2&config=true`;
|
|
247
|
+
const fetchOptions = {
|
|
248
|
+
method: 'POST',
|
|
249
|
+
headers: {
|
|
250
|
+
...this.getCustomHeaders(),
|
|
251
|
+
'Content-Type': 'application/json'
|
|
252
|
+
},
|
|
253
|
+
body: JSON.stringify({
|
|
254
|
+
token: this.apiKey,
|
|
255
|
+
distinct_id: distinctId,
|
|
256
|
+
groups,
|
|
257
|
+
person_properties: personProperties,
|
|
258
|
+
group_properties: groupProperties,
|
|
259
|
+
...extraPayload
|
|
260
|
+
})
|
|
261
|
+
};
|
|
262
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Flags URL', url));
|
|
263
|
+
return this.fetchWithRetry(url, fetchOptions, {
|
|
264
|
+
retryCount: 0
|
|
265
|
+
}, this.featureFlagsRequestTimeoutMs).then((response)=>response.json()).then((response)=>normalizeFlagsResponse(response)).catch((error)=>{
|
|
266
|
+
this._events.emit('error', error);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
async getFeatureFlagStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
|
|
270
|
+
await this._initPromise;
|
|
271
|
+
const flagDetailResponse = await this.getFeatureFlagDetailStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
|
|
272
|
+
if (void 0 === flagDetailResponse) return {
|
|
273
|
+
response: void 0,
|
|
274
|
+
requestId: void 0
|
|
275
|
+
};
|
|
276
|
+
let response = getFeatureFlagValue(flagDetailResponse.response);
|
|
277
|
+
if (void 0 === response) response = false;
|
|
278
|
+
return {
|
|
279
|
+
response,
|
|
280
|
+
requestId: flagDetailResponse.requestId
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
async getFeatureFlagDetailStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
|
|
284
|
+
await this._initPromise;
|
|
285
|
+
const flagsResponse = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, [
|
|
286
|
+
key
|
|
287
|
+
]);
|
|
288
|
+
if (void 0 === flagsResponse) return;
|
|
289
|
+
const featureFlags = flagsResponse.flags;
|
|
290
|
+
const flagDetail = featureFlags[key];
|
|
291
|
+
return {
|
|
292
|
+
response: flagDetail,
|
|
293
|
+
requestId: flagsResponse.requestId
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
async getFeatureFlagPayloadStateless(key, distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
|
|
297
|
+
await this._initPromise;
|
|
298
|
+
const payloads = await this.getFeatureFlagPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, [
|
|
299
|
+
key
|
|
300
|
+
]);
|
|
301
|
+
if (!payloads) return;
|
|
302
|
+
const response = payloads[key];
|
|
303
|
+
if (void 0 === response) return null;
|
|
304
|
+
return response;
|
|
305
|
+
}
|
|
306
|
+
async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
307
|
+
await this._initPromise;
|
|
308
|
+
const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate)).payloads;
|
|
309
|
+
return payloads;
|
|
310
|
+
}
|
|
311
|
+
async getFeatureFlagsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
312
|
+
await this._initPromise;
|
|
313
|
+
return await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
|
|
314
|
+
}
|
|
315
|
+
async getFeatureFlagsAndPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
316
|
+
await this._initPromise;
|
|
317
|
+
const featureFlagDetails = await this.getFeatureFlagDetailsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip, flagKeysToEvaluate);
|
|
318
|
+
if (!featureFlagDetails) return {
|
|
319
|
+
flags: void 0,
|
|
320
|
+
payloads: void 0,
|
|
321
|
+
requestId: void 0
|
|
322
|
+
};
|
|
323
|
+
return {
|
|
324
|
+
flags: featureFlagDetails.featureFlags,
|
|
325
|
+
payloads: featureFlagDetails.featureFlagPayloads,
|
|
326
|
+
requestId: featureFlagDetails.requestId
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
async getFeatureFlagDetailsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip, flagKeysToEvaluate) {
|
|
330
|
+
await this._initPromise;
|
|
331
|
+
const extraPayload = {};
|
|
332
|
+
if (disableGeoip ?? this.disableGeoip) extraPayload['geoip_disable'] = true;
|
|
333
|
+
if (flagKeysToEvaluate) extraPayload['flag_keys_to_evaluate'] = flagKeysToEvaluate;
|
|
334
|
+
const flagsResponse = await this.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload);
|
|
335
|
+
if (void 0 === flagsResponse) return;
|
|
336
|
+
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');
|
|
337
|
+
if (flagsResponse.quotaLimited?.includes("feature_flags")) {
|
|
338
|
+
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');
|
|
339
|
+
return {
|
|
340
|
+
flags: {},
|
|
341
|
+
featureFlags: {},
|
|
342
|
+
featureFlagPayloads: {},
|
|
343
|
+
requestId: flagsResponse?.requestId
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return flagsResponse;
|
|
347
|
+
}
|
|
348
|
+
async getSurveysStateless() {
|
|
349
|
+
await this._initPromise;
|
|
350
|
+
if (true === this.disableSurveys) {
|
|
351
|
+
this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Loading surveys is disabled.'));
|
|
352
|
+
return [];
|
|
353
|
+
}
|
|
354
|
+
const url = `${this.host}/api/surveys/?token=${this.apiKey}`;
|
|
355
|
+
const fetchOptions = {
|
|
356
|
+
method: 'GET',
|
|
357
|
+
headers: {
|
|
358
|
+
...this.getCustomHeaders(),
|
|
359
|
+
'Content-Type': 'application/json'
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
const response = await this.fetchWithRetry(url, fetchOptions).then((response)=>{
|
|
363
|
+
if (200 !== response.status || !response.json) {
|
|
364
|
+
const msg = `Surveys API could not be loaded: ${response.status}`;
|
|
365
|
+
const error = new Error(msg);
|
|
366
|
+
this.logMsgIfDebug(()=>console.error(error));
|
|
367
|
+
this._events.emit('error', new Error(msg));
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
return response.json();
|
|
371
|
+
}).catch((error)=>{
|
|
372
|
+
this.logMsgIfDebug(()=>console.error('Surveys API could not be loaded', error));
|
|
373
|
+
this._events.emit('error', error);
|
|
374
|
+
});
|
|
375
|
+
const newSurveys = response?.surveys;
|
|
376
|
+
if (newSurveys) this.logMsgIfDebug(()=>console.log('PostHog Debug', 'Surveys fetched from API: ', JSON.stringify(newSurveys)));
|
|
377
|
+
return newSurveys ?? [];
|
|
378
|
+
}
|
|
379
|
+
get props() {
|
|
380
|
+
if (!this._props) this._props = this.getPersistedProperty(PostHogPersistedProperty.Props);
|
|
381
|
+
return this._props || {};
|
|
382
|
+
}
|
|
383
|
+
set props(val) {
|
|
384
|
+
this._props = val;
|
|
385
|
+
}
|
|
386
|
+
async register(properties) {
|
|
387
|
+
this.wrap(()=>{
|
|
388
|
+
this.props = {
|
|
389
|
+
...this.props,
|
|
390
|
+
...properties
|
|
391
|
+
};
|
|
392
|
+
this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
async unregister(property) {
|
|
396
|
+
this.wrap(()=>{
|
|
397
|
+
delete this.props[property];
|
|
398
|
+
this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
enqueue(type, _message, options) {
|
|
402
|
+
this.wrap(()=>{
|
|
403
|
+
if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
|
|
404
|
+
const message = this.prepareMessage(type, _message, options);
|
|
405
|
+
const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
406
|
+
if (queue.length >= this.maxQueueSize) {
|
|
407
|
+
queue.shift();
|
|
408
|
+
this.logMsgIfDebug(()=>console.info('Queue is full, the oldest event is dropped.'));
|
|
409
|
+
}
|
|
410
|
+
queue.push({
|
|
411
|
+
message
|
|
412
|
+
});
|
|
413
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, queue);
|
|
414
|
+
this._events.emit(type, message);
|
|
415
|
+
if (queue.length >= this.flushAt) this.flushBackground();
|
|
416
|
+
if (this.flushInterval && !this._flushTimer) this._flushTimer = safeSetTimeout(()=>this.flushBackground(), this.flushInterval);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
async sendImmediate(type, _message, options) {
|
|
420
|
+
if (this.disabled) return void this.logMsgIfDebug(()=>console.warn('[PostHog] The client is disabled'));
|
|
421
|
+
if (!this._isInitialized) await this._initPromise;
|
|
422
|
+
if (this.optedOut) return void this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
|
|
423
|
+
const data = {
|
|
424
|
+
api_key: this.apiKey,
|
|
425
|
+
batch: [
|
|
426
|
+
this.prepareMessage(type, _message, options)
|
|
427
|
+
],
|
|
428
|
+
sent_at: currentISOTime()
|
|
429
|
+
};
|
|
430
|
+
if (this.historicalMigration) data.historical_migration = true;
|
|
431
|
+
const payload = JSON.stringify(data);
|
|
432
|
+
const url = `${this.host}/batch/`;
|
|
433
|
+
const gzippedPayload = this.disableCompression ? null : await gzipCompress(payload, this.isDebug);
|
|
434
|
+
const fetchOptions = {
|
|
435
|
+
method: 'POST',
|
|
436
|
+
headers: {
|
|
437
|
+
...this.getCustomHeaders(),
|
|
438
|
+
'Content-Type': 'application/json',
|
|
439
|
+
...null !== gzippedPayload && {
|
|
440
|
+
'Content-Encoding': 'gzip'
|
|
441
|
+
}
|
|
442
|
+
},
|
|
443
|
+
body: gzippedPayload || payload
|
|
444
|
+
};
|
|
445
|
+
try {
|
|
446
|
+
await this.fetchWithRetry(url, fetchOptions);
|
|
447
|
+
} catch (err) {
|
|
448
|
+
this._events.emit('error', err);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
prepareMessage(type, _message, options) {
|
|
452
|
+
const message = {
|
|
453
|
+
..._message,
|
|
454
|
+
type: type,
|
|
455
|
+
library: this.getLibraryId(),
|
|
456
|
+
library_version: this.getLibraryVersion(),
|
|
457
|
+
timestamp: options?.timestamp ? options?.timestamp : currentISOTime(),
|
|
458
|
+
uuid: options?.uuid ? options.uuid : uuidv7()
|
|
459
|
+
};
|
|
460
|
+
const addGeoipDisableProperty = options?.disableGeoip ?? this.disableGeoip;
|
|
461
|
+
if (addGeoipDisableProperty) {
|
|
462
|
+
if (!message.properties) message.properties = {};
|
|
463
|
+
message['properties']['$geoip_disable'] = true;
|
|
464
|
+
}
|
|
465
|
+
if (message.distinctId) {
|
|
466
|
+
message.distinct_id = message.distinctId;
|
|
467
|
+
delete message.distinctId;
|
|
468
|
+
}
|
|
469
|
+
return message;
|
|
470
|
+
}
|
|
471
|
+
clearFlushTimer() {
|
|
472
|
+
if (this._flushTimer) {
|
|
473
|
+
clearTimeout(this._flushTimer);
|
|
474
|
+
this._flushTimer = void 0;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
flushBackground() {
|
|
478
|
+
this.flush().catch(async (err)=>{
|
|
479
|
+
await logFlushError(err);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
async flush() {
|
|
483
|
+
const nextFlushPromise = allSettled([
|
|
484
|
+
this.flushPromise
|
|
485
|
+
]).then(()=>this._flush());
|
|
486
|
+
this.flushPromise = nextFlushPromise;
|
|
487
|
+
this.addPendingPromise(nextFlushPromise);
|
|
488
|
+
allSettled([
|
|
489
|
+
nextFlushPromise
|
|
490
|
+
]).then(()=>{
|
|
491
|
+
if (this.flushPromise === nextFlushPromise) this.flushPromise = null;
|
|
492
|
+
});
|
|
493
|
+
return nextFlushPromise;
|
|
494
|
+
}
|
|
495
|
+
getCustomHeaders() {
|
|
496
|
+
const customUserAgent = this.getCustomUserAgent();
|
|
497
|
+
const headers = {};
|
|
498
|
+
if (customUserAgent && '' !== customUserAgent) headers['User-Agent'] = customUserAgent;
|
|
499
|
+
return headers;
|
|
500
|
+
}
|
|
501
|
+
async _flush() {
|
|
502
|
+
this.clearFlushTimer();
|
|
503
|
+
await this._initPromise;
|
|
504
|
+
let queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
505
|
+
if (!queue.length) return;
|
|
506
|
+
const sentMessages = [];
|
|
507
|
+
const originalQueueLength = queue.length;
|
|
508
|
+
while(queue.length > 0 && sentMessages.length < originalQueueLength){
|
|
509
|
+
const batchItems = queue.slice(0, this.maxBatchSize);
|
|
510
|
+
const batchMessages = batchItems.map((item)=>item.message);
|
|
511
|
+
const persistQueueChange = ()=>{
|
|
512
|
+
const refreshedQueue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
513
|
+
const newQueue = refreshedQueue.slice(batchItems.length);
|
|
514
|
+
this.setPersistedProperty(PostHogPersistedProperty.Queue, newQueue);
|
|
515
|
+
queue = newQueue;
|
|
516
|
+
};
|
|
517
|
+
const data = {
|
|
518
|
+
api_key: this.apiKey,
|
|
519
|
+
batch: batchMessages,
|
|
520
|
+
sent_at: currentISOTime()
|
|
521
|
+
};
|
|
522
|
+
if (this.historicalMigration) data.historical_migration = true;
|
|
523
|
+
const payload = JSON.stringify(data);
|
|
524
|
+
const url = `${this.host}/batch/`;
|
|
525
|
+
const gzippedPayload = this.disableCompression ? null : await gzipCompress(payload, this.isDebug);
|
|
526
|
+
const fetchOptions = {
|
|
527
|
+
method: 'POST',
|
|
528
|
+
headers: {
|
|
529
|
+
...this.getCustomHeaders(),
|
|
530
|
+
'Content-Type': 'application/json',
|
|
531
|
+
...null !== gzippedPayload && {
|
|
532
|
+
'Content-Encoding': 'gzip'
|
|
533
|
+
}
|
|
534
|
+
},
|
|
535
|
+
body: gzippedPayload || payload
|
|
536
|
+
};
|
|
537
|
+
const retryOptions = {
|
|
538
|
+
retryCheck: (err)=>{
|
|
539
|
+
if (isPostHogFetchContentTooLargeError(err)) return false;
|
|
540
|
+
return isPostHogFetchError(err);
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
try {
|
|
544
|
+
await this.fetchWithRetry(url, fetchOptions, retryOptions);
|
|
545
|
+
} catch (err) {
|
|
546
|
+
if (isPostHogFetchContentTooLargeError(err) && batchMessages.length > 1) {
|
|
547
|
+
this.maxBatchSize = Math.max(1, Math.floor(batchMessages.length / 2));
|
|
548
|
+
this.logMsgIfDebug(()=>console.warn(`Received 413 when sending batch of size ${batchMessages.length}, reducing batch size to ${this.maxBatchSize}`));
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
if (!(err instanceof PostHogFetchNetworkError)) persistQueueChange();
|
|
552
|
+
this._events.emit('error', err);
|
|
553
|
+
throw err;
|
|
554
|
+
}
|
|
555
|
+
persistQueueChange();
|
|
556
|
+
sentMessages.push(...batchMessages);
|
|
557
|
+
}
|
|
558
|
+
this._events.emit('flush', sentMessages);
|
|
559
|
+
}
|
|
560
|
+
async fetchWithRetry(url, options, retryOptions, requestTimeout) {
|
|
561
|
+
AbortSignal.timeout ??= function(ms) {
|
|
562
|
+
const ctrl = new AbortController();
|
|
563
|
+
setTimeout(()=>ctrl.abort(), ms);
|
|
564
|
+
return ctrl.signal;
|
|
565
|
+
};
|
|
566
|
+
const body = options.body ? options.body : '';
|
|
567
|
+
let reqByteLength = -1;
|
|
568
|
+
try {
|
|
569
|
+
reqByteLength = body instanceof Blob ? body.size : Buffer.byteLength(body, STRING_FORMAT);
|
|
570
|
+
} catch {
|
|
571
|
+
if (body instanceof Blob) reqByteLength = body.size;
|
|
572
|
+
else {
|
|
573
|
+
const encoded = new TextEncoder().encode(body);
|
|
574
|
+
reqByteLength = encoded.length;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
return await retriable(async ()=>{
|
|
578
|
+
let res = null;
|
|
579
|
+
try {
|
|
580
|
+
res = await this.fetch(url, {
|
|
581
|
+
signal: AbortSignal.timeout(requestTimeout ?? this.requestTimeout),
|
|
582
|
+
...options
|
|
583
|
+
});
|
|
584
|
+
} catch (e) {
|
|
585
|
+
throw new PostHogFetchNetworkError(e);
|
|
586
|
+
}
|
|
587
|
+
const isNoCors = 'no-cors' === options.mode;
|
|
588
|
+
if (!isNoCors && (res.status < 200 || res.status >= 400)) throw new PostHogFetchHttpError(res, reqByteLength);
|
|
589
|
+
return res;
|
|
590
|
+
}, {
|
|
591
|
+
...this._retryOptions,
|
|
592
|
+
...retryOptions
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
async _shutdown(shutdownTimeoutMs = 30000) {
|
|
596
|
+
await this._initPromise;
|
|
597
|
+
let hasTimedOut = false;
|
|
598
|
+
this.clearFlushTimer();
|
|
599
|
+
const doShutdown = async ()=>{
|
|
600
|
+
try {
|
|
601
|
+
await this.promiseQueue.join();
|
|
602
|
+
while(true){
|
|
603
|
+
const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
|
|
604
|
+
if (0 === queue.length) break;
|
|
605
|
+
await this.flush();
|
|
606
|
+
if (hasTimedOut) break;
|
|
607
|
+
}
|
|
608
|
+
} catch (e) {
|
|
609
|
+
if (!isPostHogFetchError(e)) throw e;
|
|
610
|
+
await logFlushError(e);
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
return Promise.race([
|
|
614
|
+
new Promise((_, reject)=>{
|
|
615
|
+
safeSetTimeout(()=>{
|
|
616
|
+
this.logMsgIfDebug(()=>console.error('Timed out while shutting down PostHog'));
|
|
617
|
+
hasTimedOut = true;
|
|
618
|
+
reject('Timeout while shutting down PostHog. Some events may not have been sent.');
|
|
619
|
+
}, shutdownTimeoutMs);
|
|
620
|
+
}),
|
|
621
|
+
doShutdown()
|
|
622
|
+
]);
|
|
623
|
+
}
|
|
624
|
+
async shutdown(shutdownTimeoutMs = 30000) {
|
|
625
|
+
if (this.shutdownPromise) this.logMsgIfDebug(()=>console.warn('shutdown() called while already shutting down. shutdown() is meant to be called once before process exit - use flush() for per-request cleanup'));
|
|
626
|
+
else this.shutdownPromise = this._shutdown(shutdownTimeoutMs).finally(()=>{
|
|
627
|
+
this.shutdownPromise = null;
|
|
628
|
+
});
|
|
629
|
+
return this.shutdownPromise;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
export { PostHogCoreStateless, posthog_core_stateless_QuotaLimitedFeature as QuotaLimitedFeature, logFlushError, maybeAdd };
|