posthog-node 4.17.1 → 4.17.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # Next
2
+
3
+ # 4.17.2 - 2025-05-22
4
+
5
+ 1. chore: improve event prop types
6
+ 2. fix: no throw in sendImmediate
7
+
1
8
  # 4.17.1 - 2025-05-02
2
9
 
3
10
  1. fix: fix imports for old node.js version
@@ -885,7 +885,7 @@ function setupExpressErrorHandler(_posthog, app) {
885
885
  });
886
886
  }
887
887
 
888
- var version = "4.17.1";
888
+ var version = "4.17.2";
889
889
 
890
890
  var PostHogPersistedProperty;
891
891
  (function (PostHogPersistedProperty) {
@@ -1216,6 +1216,9 @@ function isTokenInRollout(token, percentage = 0, excludedHashes) {
1216
1216
  const hashInt = parseInt(tokenHash, 16);
1217
1217
  const hashFloat = hashInt / 0xffffffff;
1218
1218
  return hashFloat < percentage;
1219
+ }
1220
+ function allSettled(promises) {
1221
+ return Promise.all(promises.map((p) => (p ?? Promise.resolve()).then((value) => ({ status: 'fulfilled', value }), (reason) => ({ status: 'rejected', reason }))));
1219
1222
  }
1220
1223
 
1221
1224
  // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
@@ -1960,6 +1963,7 @@ class PostHogCoreStateless {
1960
1963
  ...extraPayload,
1961
1964
  }),
1962
1965
  };
1966
+ this.logMsgIfDebug(() => console.log('PostHog Debug', 'Decide URL', url));
1963
1967
  // Don't retry /decide API calls
1964
1968
  return this.fetchWithRetry(url, fetchOptions, { retryCount: 0 }, this.featureFlagsRequestTimeoutMs)
1965
1969
  .then((response) => response.json())
@@ -2077,7 +2081,7 @@ class PostHogCoreStateless {
2077
2081
  async getSurveysStateless() {
2078
2082
  await this._initPromise;
2079
2083
  if (this.disableSurveys === true) {
2080
- this.logMsgIfDebug(() => console.log('Loading surveys is disabled.'));
2084
+ this.logMsgIfDebug(() => console.log('PostHog Debug', 'Loading surveys is disabled.'));
2081
2085
  return [];
2082
2086
  }
2083
2087
  const url = `${this.host}/api/surveys/?token=${this.apiKey}`;
@@ -2200,7 +2204,6 @@ class PostHogCoreStateless {
2200
2204
  }
2201
2205
  catch (err) {
2202
2206
  this._events.emit('error', err);
2203
- throw err;
2204
2207
  }
2205
2208
  }
2206
2209
  prepareMessage(type, _message, options) {
@@ -2240,15 +2243,38 @@ class PostHogCoreStateless {
2240
2243
  await logFlushError(err);
2241
2244
  });
2242
2245
  }
2246
+ /**
2247
+ * Flushes the queue
2248
+ *
2249
+ * This function will return a promise that will resolve when the flush is complete,
2250
+ * or reject if there was an error (for example if the server or network is down).
2251
+ *
2252
+ * If there is already a flush in progress, this function will wait for that flush to complete.
2253
+ *
2254
+ * It's recommended to do error handling in the callback of the promise.
2255
+ *
2256
+ * @example
2257
+ * posthog.flush().then(() => {
2258
+ * console.log('Flush complete')
2259
+ * }).catch((err) => {
2260
+ * console.error('Flush failed', err)
2261
+ * })
2262
+ *
2263
+ *
2264
+ * @throws PostHogFetchHttpError
2265
+ * @throws PostHogFetchNetworkError
2266
+ * @throws Error
2267
+ */
2243
2268
  async flush() {
2244
2269
  // Wait for the current flush operation to finish (regardless of success or failure), then try to flush again.
2245
2270
  // Use allSettled instead of finally to be defensive around flush throwing errors immediately rather than rejecting.
2246
- const nextFlushPromise = Promise.allSettled([this.flushPromise]).then(() => {
2271
+ // Use a custom allSettled implementation to avoid issues with patching Promise on RN
2272
+ const nextFlushPromise = allSettled([this.flushPromise]).then(() => {
2247
2273
  return this._flush();
2248
2274
  });
2249
2275
  this.flushPromise = nextFlushPromise;
2250
2276
  void this.addPendingPromise(nextFlushPromise);
2251
- Promise.allSettled([nextFlushPromise]).then(() => {
2277
+ allSettled([nextFlushPromise]).then(() => {
2252
2278
  // If there are no others waiting to flush, clear the promise.
2253
2279
  // We don't strictly need to do this, but it could make debugging easier
2254
2280
  if (this.flushPromise === nextFlushPromise) {
@@ -2274,7 +2300,7 @@ class PostHogCoreStateless {
2274
2300
  await this._initPromise;
2275
2301
  let queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
2276
2302
  if (!queue.length) {
2277
- return [];
2303
+ return;
2278
2304
  }
2279
2305
  const sentMessages = [];
2280
2306
  const originalQueueLength = queue.length;
@@ -2345,7 +2371,6 @@ class PostHogCoreStateless {
2345
2371
  sentMessages.push(...batchMessages);
2346
2372
  }
2347
2373
  this._events.emit('flush', sentMessages);
2348
- return sentMessages;
2349
2374
  }
2350
2375
  async fetchWithRetry(url, options, retryOptions, requestTimeout) {
2351
2376
  var _a;
@@ -2355,7 +2380,14 @@ class PostHogCoreStateless {
2355
2380
  return ctrl.signal;
2356
2381
  });
2357
2382
  const body = options.body ? options.body : '';
2358
- const reqByteLength = Buffer.byteLength(body, STRING_FORMAT);
2383
+ let reqByteLength = -1;
2384
+ try {
2385
+ reqByteLength = Buffer.byteLength(body, STRING_FORMAT);
2386
+ }
2387
+ catch {
2388
+ const encoded = new TextEncoder().encode(body);
2389
+ reqByteLength = encoded.length;
2390
+ }
2359
2391
  return await retriable(async () => {
2360
2392
  let res = null;
2361
2393
  try {
@@ -2901,12 +2933,12 @@ class FeatureFlagsPoller {
2901
2933
  case 200:
2902
2934
  {
2903
2935
  // Process successful response
2904
- const responseJson = await res.json();
2936
+ const responseJson = (await res.json()) ?? {};
2905
2937
  if (!('flags' in responseJson)) {
2906
2938
  this.onError?.(new Error(`Invalid response when getting feature flags: ${JSON.stringify(responseJson)}`));
2907
2939
  return;
2908
2940
  }
2909
- this.featureFlags = responseJson.flags || [];
2941
+ this.featureFlags = responseJson.flags ?? [];
2910
2942
  this.featureFlagsByKey = this.featureFlags.reduce((acc, curr) => (acc[curr.key] = curr, acc), {});
2911
2943
  this.groupTypeMapping = responseJson.group_type_mapping || {};
2912
2944
  this.cohorts = responseJson.cohorts || {};