posthog-node 5.1.1 → 5.3.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.
@@ -859,7 +859,7 @@ function applyChunkIds(frames, parser) {
859
859
 
860
860
  const SHUTDOWN_TIMEOUT = 2000;
861
861
  class ErrorTracking {
862
- static async captureException(client, error, hint, distinctId, additionalProperties) {
862
+ static async buildEventMessage(error, hint, distinctId, additionalProperties) {
863
863
  const properties = {
864
864
  ...additionalProperties
865
865
  };
@@ -869,14 +869,14 @@ class ErrorTracking {
869
869
  properties.$process_person_profile = false;
870
870
  }
871
871
  const exceptionProperties = await propertiesFromUnknownInput(this.stackParser, this.frameModifiers, error, hint);
872
- client.capture({
872
+ return {
873
873
  event: '$exception',
874
874
  distinctId: distinctId || uuidv7(),
875
875
  properties: {
876
876
  ...exceptionProperties,
877
877
  ...properties
878
878
  }
879
- });
879
+ };
880
880
  }
881
881
  constructor(client, options) {
882
882
  this.client = client;
@@ -890,7 +890,9 @@ class ErrorTracking {
890
890
  }
891
891
  }
892
892
  onException(exception, hint) {
893
- ErrorTracking.captureException(this.client, exception, hint);
893
+ void ErrorTracking.buildEventMessage(exception, hint).then(msg => {
894
+ this.client.capture(msg);
895
+ });
894
896
  }
895
897
  async onFatalError() {
896
898
  await this.client.shutdown(SHUTDOWN_TIMEOUT);
@@ -910,9 +912,9 @@ function setupExpressErrorHandler(_posthog, app) {
910
912
  };
911
913
  // Given stateless nature of Node SDK we capture exceptions using personless processing
912
914
  // when no user can be determined e.g. in the case of exception autocapture
913
- ErrorTracking.captureException(_posthog, error, hint, uuidv7(), {
915
+ ErrorTracking.buildEventMessage(error, hint, uuidv7(), {
914
916
  $process_person_profile: false
915
- });
917
+ }).then(msg => _posthog.capture(msg));
916
918
  next(error);
917
919
  });
918
920
  }
@@ -1318,7 +1320,7 @@ function snipLine(line, colno) {
1318
1320
  return newLine;
1319
1321
  }
1320
1322
 
1321
- var version = "5.1.1";
1323
+ var version = "5.3.1";
1322
1324
 
1323
1325
  var PostHogPersistedProperty;
1324
1326
  (function (PostHogPersistedProperty) {
@@ -1356,6 +1358,12 @@ var Compression;
1356
1358
  })(Compression || (Compression = {}));
1357
1359
  var SurveyPosition;
1358
1360
  (function (SurveyPosition) {
1361
+ SurveyPosition["TopLeft"] = "top_left";
1362
+ SurveyPosition["TopCenter"] = "top_center";
1363
+ SurveyPosition["TopRight"] = "top_right";
1364
+ SurveyPosition["MiddleLeft"] = "middle_left";
1365
+ SurveyPosition["MiddleCenter"] = "middle_center";
1366
+ SurveyPosition["MiddleRight"] = "middle_right";
1359
1367
  SurveyPosition["Left"] = "left";
1360
1368
  SurveyPosition["Right"] = "right";
1361
1369
  SurveyPosition["Center"] = "center";
@@ -2876,23 +2884,6 @@ class FeatureFlagsPoller {
2876
2884
  stopPoller() {
2877
2885
  clearTimeout(this.poller);
2878
2886
  }
2879
- _requestRemoteConfigPayload(flagKey) {
2880
- const url = `${this.host}/api/projects/@current/feature_flags/${flagKey}/remote_config/`;
2881
- const options = this.getPersonalApiKeyRequestOptions();
2882
- let abortTimeout = null;
2883
- if (this.timeout && typeof this.timeout === 'number') {
2884
- const controller = new AbortController();
2885
- abortTimeout = safeSetTimeout(() => {
2886
- controller.abort();
2887
- }, this.timeout);
2888
- options.signal = controller.signal;
2889
- }
2890
- try {
2891
- return this.fetch(url, options);
2892
- } finally {
2893
- clearTimeout(abortTimeout);
2894
- }
2895
- }
2896
2887
  }
2897
2888
  // # This function takes a distinct_id and a feature flag key and returns a float between 0 and 1.
2898
2889
  // # Given the same distinct_id and key, it'll always return the same float. These floats are
@@ -3177,21 +3168,25 @@ class PostHogBackendClient extends PostHogCoreStateless {
3177
3168
  if (options.personalApiKey.includes('phc_')) {
3178
3169
  throw new Error('Your Personal API key is invalid. These keys are prefixed with "phx_" and can be created in PostHog project settings.');
3179
3170
  }
3180
- this.featureFlagsPoller = new FeatureFlagsPoller({
3181
- pollingInterval: this.options.featureFlagsPollingInterval,
3182
- personalApiKey: options.personalApiKey,
3183
- projectApiKey: apiKey,
3184
- timeout: options.requestTimeout ?? 10000,
3185
- host: this.host,
3186
- fetch: options.fetch,
3187
- onError: err => {
3188
- this._events.emit('error', err);
3189
- },
3190
- onLoad: count => {
3191
- this._events.emit('localEvaluationFlagsLoaded', count);
3192
- },
3193
- customHeaders: this.getCustomHeaders()
3194
- });
3171
+ // Only start the poller if local evaluation is enabled (defaults to true for backward compatibility)
3172
+ const shouldEnableLocalEvaluation = options.enableLocalEvaluation !== false;
3173
+ if (shouldEnableLocalEvaluation) {
3174
+ this.featureFlagsPoller = new FeatureFlagsPoller({
3175
+ pollingInterval: this.options.featureFlagsPollingInterval,
3176
+ personalApiKey: options.personalApiKey,
3177
+ projectApiKey: apiKey,
3178
+ timeout: options.requestTimeout ?? 10000,
3179
+ host: this.host,
3180
+ fetch: options.fetch,
3181
+ onError: err => {
3182
+ this._events.emit('error', err);
3183
+ },
3184
+ onLoad: count => {
3185
+ this._events.emit('localEvaluationFlagsLoaded', count);
3186
+ },
3187
+ customHeaders: this.getCustomHeaders()
3188
+ });
3189
+ }
3195
3190
  }
3196
3191
  this.errorTracking = new ErrorTracking(this, options);
3197
3192
  this.distinctIdHasSentFlagCalls = {};
@@ -3547,7 +3542,10 @@ class PostHogBackendClient extends PostHogCoreStateless {
3547
3542
  return response;
3548
3543
  }
3549
3544
  async getRemoteConfigPayload(flagKey) {
3550
- const response = await this.featureFlagsPoller?._requestRemoteConfigPayload(flagKey);
3545
+ if (!this.options.personalApiKey) {
3546
+ throw new Error('Personal API key is required for remote config payload decryption');
3547
+ }
3548
+ const response = await this._requestRemoteConfigPayload(flagKey);
3551
3549
  if (!response) {
3552
3550
  return undefined;
3553
3551
  }
@@ -3641,6 +3639,38 @@ class PostHogBackendClient extends PostHogCoreStateless {
3641
3639
  this.featureFlagsPoller?.stopPoller();
3642
3640
  return super._shutdown(shutdownTimeoutMs);
3643
3641
  }
3642
+ async _requestRemoteConfigPayload(flagKey) {
3643
+ if (!this.options.personalApiKey) {
3644
+ return undefined;
3645
+ }
3646
+ const url = `${this.host}/api/projects/@current/feature_flags/${flagKey}/remote_config/`;
3647
+ const options = {
3648
+ method: 'GET',
3649
+ headers: {
3650
+ ...this.getCustomHeaders(),
3651
+ 'Content-Type': 'application/json',
3652
+ Authorization: `Bearer ${this.options.personalApiKey}`
3653
+ }
3654
+ };
3655
+ let abortTimeout = null;
3656
+ if (this.options.requestTimeout && typeof this.options.requestTimeout === 'number') {
3657
+ const controller = new AbortController();
3658
+ abortTimeout = safeSetTimeout(() => {
3659
+ controller.abort();
3660
+ }, this.options.requestTimeout);
3661
+ options.signal = controller.signal;
3662
+ }
3663
+ try {
3664
+ return await this.fetch(url, options);
3665
+ } catch (error) {
3666
+ this._events.emit('error', error);
3667
+ return undefined;
3668
+ } finally {
3669
+ if (abortTimeout) {
3670
+ clearTimeout(abortTimeout);
3671
+ }
3672
+ }
3673
+ }
3644
3674
  addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties) {
3645
3675
  const allPersonProperties = {
3646
3676
  distinct_id: distinctId,
@@ -3662,9 +3692,18 @@ class PostHogBackendClient extends PostHogCoreStateless {
3662
3692
  }
3663
3693
  captureException(error, distinctId, additionalProperties) {
3664
3694
  const syntheticException = new Error('PostHog syntheticException');
3665
- ErrorTracking.captureException(this, error, {
3695
+ ErrorTracking.buildEventMessage(error, {
3696
+ syntheticException
3697
+ }, distinctId, additionalProperties).then(msg => {
3698
+ this.capture(msg);
3699
+ });
3700
+ }
3701
+ async captureExceptionImmediate(error, distinctId, additionalProperties) {
3702
+ const syntheticException = new Error('PostHog syntheticException');
3703
+ const evtMsg = await ErrorTracking.buildEventMessage(error, {
3666
3704
  syntheticException
3667
3705
  }, distinctId, additionalProperties);
3706
+ return await this.captureImmediate(evtMsg);
3668
3707
  }
3669
3708
  }
3670
3709