posthog-node 4.17.0 → 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.
Files changed (46) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/lib/edge/{index.cjs.js → index.cjs} +43 -11
  3. package/lib/edge/index.cjs.map +1 -0
  4. package/lib/edge/{index.esm.js → index.mjs} +43 -11
  5. package/lib/edge/index.mjs.map +1 -0
  6. package/lib/index.d.ts +69 -57
  7. package/lib/node/{index.cjs.js → index.cjs} +43 -11
  8. package/lib/node/index.cjs.map +1 -0
  9. package/lib/node/{index.esm.js → index.mjs} +43 -11
  10. package/lib/node/index.mjs.map +1 -0
  11. package/package.json +17 -17
  12. package/lib/edge/index.cjs.js.map +0 -1
  13. package/lib/edge/index.esm.js.map +0 -1
  14. package/lib/node/index.cjs.js.map +0 -1
  15. package/lib/node/index.esm.js.map +0 -1
  16. package/src/client.ts +0 -650
  17. package/src/entrypoints/index.edge.ts +0 -15
  18. package/src/entrypoints/index.node.ts +0 -17
  19. package/src/exports.ts +0 -3
  20. package/src/extensions/error-tracking/autocapture.ts +0 -65
  21. package/src/extensions/error-tracking/context-lines.node.ts +0 -392
  22. package/src/extensions/error-tracking/error-conversion.ts +0 -279
  23. package/src/extensions/error-tracking/get-module.node.ts +0 -57
  24. package/src/extensions/error-tracking/index.ts +0 -69
  25. package/src/extensions/error-tracking/reduceable-cache.ts +0 -39
  26. package/src/extensions/error-tracking/stack-parser.ts +0 -212
  27. package/src/extensions/error-tracking/type-checking.ts +0 -40
  28. package/src/extensions/error-tracking/types.ts +0 -69
  29. package/src/extensions/express.ts +0 -37
  30. package/src/extensions/feature-flags/crypto-helpers.ts +0 -36
  31. package/src/extensions/feature-flags/crypto.ts +0 -22
  32. package/src/extensions/feature-flags/feature-flags.ts +0 -865
  33. package/src/extensions/feature-flags/lazy.ts +0 -55
  34. package/src/extensions/sentry-integration.ts +0 -204
  35. package/src/fetch.ts +0 -39
  36. package/src/storage-memory.ts +0 -13
  37. package/src/types.ts +0 -275
  38. package/test/crypto.spec.ts +0 -36
  39. package/test/extensions/error-conversion.spec.ts +0 -44
  40. package/test/extensions/sentry-integration.spec.ts +0 -163
  41. package/test/feature-flags.decide.spec.ts +0 -381
  42. package/test/feature-flags.spec.ts +0 -4686
  43. package/test/lazy.spec.ts +0 -71
  44. package/test/posthog-node.spec.ts +0 -1341
  45. package/test/test-utils.ts +0 -111
  46. package/tsconfig.json +0 -8
@@ -863,7 +863,7 @@ function setupExpressErrorHandler(_posthog, app) {
863
863
  });
864
864
  }
865
865
 
866
- var version = "4.17.0";
866
+ var version = "4.17.2";
867
867
 
868
868
  var PostHogPersistedProperty;
869
869
  (function (PostHogPersistedProperty) {
@@ -1194,6 +1194,9 @@ function isTokenInRollout(token, percentage = 0, excludedHashes) {
1194
1194
  const hashInt = parseInt(tokenHash, 16);
1195
1195
  const hashFloat = hashInt / 0xffffffff;
1196
1196
  return hashFloat < percentage;
1197
+ }
1198
+ function allSettled(promises) {
1199
+ return Promise.all(promises.map((p) => (p ?? Promise.resolve()).then((value) => ({ status: 'fulfilled', value }), (reason) => ({ status: 'rejected', reason }))));
1197
1200
  }
1198
1201
 
1199
1202
  // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
@@ -1938,6 +1941,7 @@ class PostHogCoreStateless {
1938
1941
  ...extraPayload,
1939
1942
  }),
1940
1943
  };
1944
+ this.logMsgIfDebug(() => console.log('PostHog Debug', 'Decide URL', url));
1941
1945
  // Don't retry /decide API calls
1942
1946
  return this.fetchWithRetry(url, fetchOptions, { retryCount: 0 }, this.featureFlagsRequestTimeoutMs)
1943
1947
  .then((response) => response.json())
@@ -2055,7 +2059,7 @@ class PostHogCoreStateless {
2055
2059
  async getSurveysStateless() {
2056
2060
  await this._initPromise;
2057
2061
  if (this.disableSurveys === true) {
2058
- this.logMsgIfDebug(() => console.log('Loading surveys is disabled.'));
2062
+ this.logMsgIfDebug(() => console.log('PostHog Debug', 'Loading surveys is disabled.'));
2059
2063
  return [];
2060
2064
  }
2061
2065
  const url = `${this.host}/api/surveys/?token=${this.apiKey}`;
@@ -2178,7 +2182,6 @@ class PostHogCoreStateless {
2178
2182
  }
2179
2183
  catch (err) {
2180
2184
  this._events.emit('error', err);
2181
- throw err;
2182
2185
  }
2183
2186
  }
2184
2187
  prepareMessage(type, _message, options) {
@@ -2218,15 +2221,38 @@ class PostHogCoreStateless {
2218
2221
  await logFlushError(err);
2219
2222
  });
2220
2223
  }
2224
+ /**
2225
+ * Flushes the queue
2226
+ *
2227
+ * This function will return a promise that will resolve when the flush is complete,
2228
+ * or reject if there was an error (for example if the server or network is down).
2229
+ *
2230
+ * If there is already a flush in progress, this function will wait for that flush to complete.
2231
+ *
2232
+ * It's recommended to do error handling in the callback of the promise.
2233
+ *
2234
+ * @example
2235
+ * posthog.flush().then(() => {
2236
+ * console.log('Flush complete')
2237
+ * }).catch((err) => {
2238
+ * console.error('Flush failed', err)
2239
+ * })
2240
+ *
2241
+ *
2242
+ * @throws PostHogFetchHttpError
2243
+ * @throws PostHogFetchNetworkError
2244
+ * @throws Error
2245
+ */
2221
2246
  async flush() {
2222
2247
  // Wait for the current flush operation to finish (regardless of success or failure), then try to flush again.
2223
2248
  // Use allSettled instead of finally to be defensive around flush throwing errors immediately rather than rejecting.
2224
- const nextFlushPromise = Promise.allSettled([this.flushPromise]).then(() => {
2249
+ // Use a custom allSettled implementation to avoid issues with patching Promise on RN
2250
+ const nextFlushPromise = allSettled([this.flushPromise]).then(() => {
2225
2251
  return this._flush();
2226
2252
  });
2227
2253
  this.flushPromise = nextFlushPromise;
2228
2254
  void this.addPendingPromise(nextFlushPromise);
2229
- Promise.allSettled([nextFlushPromise]).then(() => {
2255
+ allSettled([nextFlushPromise]).then(() => {
2230
2256
  // If there are no others waiting to flush, clear the promise.
2231
2257
  // We don't strictly need to do this, but it could make debugging easier
2232
2258
  if (this.flushPromise === nextFlushPromise) {
@@ -2252,7 +2278,7 @@ class PostHogCoreStateless {
2252
2278
  await this._initPromise;
2253
2279
  let queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
2254
2280
  if (!queue.length) {
2255
- return [];
2281
+ return;
2256
2282
  }
2257
2283
  const sentMessages = [];
2258
2284
  const originalQueueLength = queue.length;
@@ -2323,7 +2349,6 @@ class PostHogCoreStateless {
2323
2349
  sentMessages.push(...batchMessages);
2324
2350
  }
2325
2351
  this._events.emit('flush', sentMessages);
2326
- return sentMessages;
2327
2352
  }
2328
2353
  async fetchWithRetry(url, options, retryOptions, requestTimeout) {
2329
2354
  var _a;
@@ -2333,7 +2358,14 @@ class PostHogCoreStateless {
2333
2358
  return ctrl.signal;
2334
2359
  });
2335
2360
  const body = options.body ? options.body : '';
2336
- const reqByteLength = Buffer.byteLength(body, STRING_FORMAT);
2361
+ let reqByteLength = -1;
2362
+ try {
2363
+ reqByteLength = Buffer.byteLength(body, STRING_FORMAT);
2364
+ }
2365
+ catch {
2366
+ const encoded = new TextEncoder().encode(body);
2367
+ reqByteLength = encoded.length;
2368
+ }
2337
2369
  return await retriable(async () => {
2338
2370
  let res = null;
2339
2371
  try {
@@ -2879,12 +2911,12 @@ class FeatureFlagsPoller {
2879
2911
  case 200:
2880
2912
  {
2881
2913
  // Process successful response
2882
- const responseJson = await res.json();
2914
+ const responseJson = (await res.json()) ?? {};
2883
2915
  if (!('flags' in responseJson)) {
2884
2916
  this.onError?.(new Error(`Invalid response when getting feature flags: ${JSON.stringify(responseJson)}`));
2885
2917
  return;
2886
2918
  }
2887
- this.featureFlags = responseJson.flags || [];
2919
+ this.featureFlags = responseJson.flags ?? [];
2888
2920
  this.featureFlagsByKey = this.featureFlags.reduce((acc, curr) => (acc[curr.key] = curr, acc), {});
2889
2921
  this.groupTypeMapping = responseJson.group_type_mapping || {};
2890
2922
  this.cohorts = responseJson.cohorts || {};
@@ -3890,4 +3922,4 @@ class PostHog extends PostHogBackendClient {
3890
3922
  }
3891
3923
 
3892
3924
  export { PostHog, PostHogSentryIntegration, createEventProcessor, sentryIntegration, setupExpressErrorHandler };
3893
- //# sourceMappingURL=index.esm.js.map
3925
+ //# sourceMappingURL=index.mjs.map