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/lib/index.d.ts CHANGED
@@ -143,7 +143,7 @@ declare abstract class PostHogCoreStateless {
143
143
  private removeDebugCallback?;
144
144
  private disableGeoip;
145
145
  private historicalMigration;
146
- disabled: boolean;
146
+ protected disabled: boolean;
147
147
  private defaultOptIn;
148
148
  private pendingPromises;
149
149
  protected _events: SimpleEventEmitter;
@@ -167,6 +167,7 @@ declare abstract class PostHogCoreStateless {
167
167
  on(event: string, cb: (...args: any[]) => void): () => void;
168
168
  debug(enabled?: boolean): void;
169
169
  get isDebug(): boolean;
170
+ get isDisabled(): boolean;
170
171
  private buildPayload;
171
172
  protected addPendingPromise<T>(promise: Promise<T>): Promise<T>;
172
173
  /***
package/lib/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'rusha';
2
2
 
3
- var version = "4.2.1";
3
+ var version = "4.2.2";
4
4
 
5
5
  var PostHogPersistedProperty;
6
6
  (function (PostHogPersistedProperty) {
@@ -1031,6 +1031,9 @@ class PostHogCoreStateless {
1031
1031
  get isDebug() {
1032
1032
  return !!this.removeDebugCallback;
1033
1033
  }
1034
+ get isDisabled() {
1035
+ return this.disabled;
1036
+ }
1034
1037
  buildPayload(payload) {
1035
1038
  return {
1036
1039
  distinct_id: payload.distinct_id,
@@ -1359,33 +1362,45 @@ class PostHogCoreStateless {
1359
1362
  }, { ...this._retryOptions, ...retryOptions });
1360
1363
  }
1361
1364
  async shutdown(shutdownTimeoutMs = 30000) {
1365
+ // A little tricky - we want to have a max shutdown time and enforce it, even if that means we have some
1366
+ // dangling promises. We'll keep track of the timeout and resolve/reject based on that.
1362
1367
  await this._initPromise;
1368
+ let hasTimedOut = false;
1363
1369
  this.clearFlushTimer();
1364
- try {
1365
- await Promise.all(Object.values(this.pendingPromises));
1366
- const startTimeWithDelay = Date.now() + shutdownTimeoutMs;
1367
- while (true) {
1368
- const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1369
- if (queue.length === 0) {
1370
- break;
1371
- }
1372
- // flush again to make sure we send all events, some of which might've been added
1373
- // while we were waiting for the pending promises to resolve
1374
- // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1375
- await this.flush();
1376
- // If we've been waiting for more than the shutdownTimeoutMs, stop it
1377
- const now = Date.now();
1378
- if (startTimeWithDelay < now) {
1379
- break;
1370
+ const doShutdown = async () => {
1371
+ try {
1372
+ await Promise.all(Object.values(this.pendingPromises));
1373
+ while (true) {
1374
+ const queue = this.getPersistedProperty(PostHogPersistedProperty.Queue) || [];
1375
+ if (queue.length === 0) {
1376
+ break;
1377
+ }
1378
+ // flush again to make sure we send all events, some of which might've been added
1379
+ // while we were waiting for the pending promises to resolve
1380
+ // For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture
1381
+ await this.flush();
1382
+ if (hasTimedOut) {
1383
+ break;
1384
+ }
1380
1385
  }
1381
1386
  }
1382
- }
1383
- catch (e) {
1384
- if (!isPostHogFetchError(e)) {
1385
- throw e;
1387
+ catch (e) {
1388
+ if (!isPostHogFetchError(e)) {
1389
+ throw e;
1390
+ }
1391
+ this.logMsgIfDebug(() => console.error('Error while shutting down PostHog', e));
1386
1392
  }
1387
- this.logMsgIfDebug(() => console.error('Error while shutting down PostHog', e));
1388
- }
1393
+ };
1394
+ return Promise.race([
1395
+ new Promise((_, reject) => {
1396
+ safeSetTimeout(() => {
1397
+ this.logMsgIfDebug(() => console.error('Timed out while shutting down PostHog'));
1398
+ hasTimedOut = true;
1399
+ reject('Timeout while shutting down PostHog. Some events may not have been sent.');
1400
+ }, shutdownTimeoutMs);
1401
+ }),
1402
+ doShutdown(),
1403
+ ]);
1389
1404
  }
1390
1405
  }
1391
1406