posthog-node 4.2.1 → 4.2.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,5 +1,9 @@
1
1
  # Next
2
2
 
3
+ # 4.2.2 - 2024-11-18
4
+
5
+ 1. fix: Shutdown will now respect the timeout and forcefully return rather than returning after the next fetch.
6
+
3
7
  # 4.2.1 - 2024-10-14
4
8
 
5
9
  1. fix: only log messages if debug is enabled
package/lib/index.cjs.js CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var rusha = require('rusha');
6
6
 
7
- var version = "4.2.1";
7
+ var version = "4.2.2";
8
8
 
9
9
  var PostHogPersistedProperty;
10
10
  (function (PostHogPersistedProperty) {
@@ -1035,6 +1035,9 @@ class PostHogCoreStateless {
1035
1035
  get isDebug() {
1036
1036
  return !!this.removeDebugCallback;
1037
1037
  }
1038
+ get isDisabled() {
1039
+ return this.disabled;
1040
+ }
1038
1041
  buildPayload(payload) {
1039
1042
  return {
1040
1043
  distinct_id: payload.distinct_id,
@@ -1363,33 +1366,45 @@ class PostHogCoreStateless {
1363
1366
  }, { ...this._retryOptions, ...retryOptions });
1364
1367
  }
1365
1368
  async shutdown(shutdownTimeoutMs = 30000) {
1369
+ // A little tricky - we want to have a max shutdown time and enforce it, even if that means we have some
1370
+ // dangling promises. We'll keep track of the timeout and resolve/reject based on that.
1366
1371
  await this._initPromise;
1372
+ let hasTimedOut = false;
1367
1373
  this.clearFlushTimer();
1368
- try {
1369
- await Promise.all(Object.values(this.pendingPromises));
1370
- const startTimeWithDelay = Date.now() + shutdownTimeoutMs;
1371
- while (true) {
1372
- const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1373
- if (queue.length === 0) {
1374
- break;
1375
- }
1376
- // flush again to make sure we send all events, some of which might've been added
1377
- // while we were waiting for the pending promises to resolve
1378
- // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1379
- await this.flush();
1380
- // If we've been waiting for more than the shutdownTimeoutMs, stop it
1381
- const now = Date.now();
1382
- if (startTimeWithDelay < now) {
1383
- break;
1374
+ const doShutdown = async () => {
1375
+ try {
1376
+ await Promise.all(Object.values(this.pendingPromises));
1377
+ while (true) {
1378
+ const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1379
+ if (queue.length === 0) {
1380
+ break;
1381
+ }
1382
+ // flush again to make sure we send all events, some of which might've been added
1383
+ // while we were waiting for the pending promises to resolve
1384
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1385
+ await this.flush();
1386
+ if (hasTimedOut) {
1387
+ break;
1388
+ }
1384
1389
  }
1385
1390
  }
1386
- }
1387
- catch (e) {
1388
- if (!isPostHogFetchError(e)) {
1389
- throw e;
1391
+ catch (e) {
1392
+ if (!isPostHogFetchError(e)) {
1393
+ throw e;
1394
+ }
1395
+ this.logMsgIfDebug(() => console.error('Error while shutting down PostHog', e));
1390
1396
  }
1391
- this.logMsgIfDebug(() => console.error('Error while shutting down PostHog', e));
1392
- }
1397
+ };
1398
+ return Promise.race([
1399
+ new Promise((_, reject) => {
1400
+ safeSetTimeout(() => {
1401
+ this.logMsgIfDebug(() => console.error('Timed out while shutting down PostHog'));
1402
+ hasTimedOut = true;
1403
+ reject('Timeout while shutting down PostHog. Some events may not have been sent.');
1404
+ }, shutdownTimeoutMs);
1405
+ }),
1406
+ doShutdown(),
1407
+ ]);
1393
1408
  }
1394
1409
  }
1395
1410