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.
@@ -855,7 +855,7 @@ function applyChunkIds(frames, parser) {
855
855
 
856
856
  const SHUTDOWN_TIMEOUT = 2000;
857
857
  class ErrorTracking {
858
- static async captureException(client, error, hint, distinctId, additionalProperties) {
858
+ static async buildEventMessage(error, hint, distinctId, additionalProperties) {
859
859
  const properties = {
860
860
  ...additionalProperties
861
861
  };
@@ -865,14 +865,14 @@ class ErrorTracking {
865
865
  properties.$process_person_profile = false;
866
866
  }
867
867
  const exceptionProperties = await propertiesFromUnknownInput(this.stackParser, this.frameModifiers, error, hint);
868
- client.capture({
868
+ return {
869
869
  event: '$exception',
870
870
  distinctId: distinctId || uuidv7(),
871
871
  properties: {
872
872
  ...exceptionProperties,
873
873
  ...properties
874
874
  }
875
- });
875
+ };
876
876
  }
877
877
  constructor(client, options) {
878
878
  this.client = client;
@@ -886,7 +886,9 @@ class ErrorTracking {
886
886
  }
887
887
  }
888
888
  onException(exception, hint) {
889
- ErrorTracking.captureException(this.client, exception, hint);
889
+ void ErrorTracking.buildEventMessage(exception, hint).then(msg => {
890
+ this.client.capture(msg);
891
+ });
890
892
  }
891
893
  async onFatalError() {
892
894
  await this.client.shutdown(SHUTDOWN_TIMEOUT);
@@ -906,14 +908,14 @@ function setupExpressErrorHandler(_posthog, app) {
906
908
  };
907
909
  // Given stateless nature of Node SDK we capture exceptions using personless processing
908
910
  // when no user can be determined e.g. in the case of exception autocapture
909
- ErrorTracking.captureException(_posthog, error, hint, uuidv7(), {
911
+ ErrorTracking.buildEventMessage(error, hint, uuidv7(), {
910
912
  $process_person_profile: false
911
- });
913
+ }).then(msg => _posthog.capture(msg));
912
914
  next(error);
913
915
  });
914
916
  }
915
917
 
916
- var version = "5.1.1";
918
+ var version = "5.3.1";
917
919
 
918
920
  var PostHogPersistedProperty;
919
921
  (function (PostHogPersistedProperty) {
@@ -951,6 +953,12 @@ var Compression;
951
953
  })(Compression || (Compression = {}));
952
954
  var SurveyPosition;
953
955
  (function (SurveyPosition) {
956
+ SurveyPosition["TopLeft"] = "top_left";
957
+ SurveyPosition["TopCenter"] = "top_center";
958
+ SurveyPosition["TopRight"] = "top_right";
959
+ SurveyPosition["MiddleLeft"] = "middle_left";
960
+ SurveyPosition["MiddleCenter"] = "middle_center";
961
+ SurveyPosition["MiddleRight"] = "middle_right";
954
962
  SurveyPosition["Left"] = "left";
955
963
  SurveyPosition["Right"] = "right";
956
964
  SurveyPosition["Center"] = "center";
@@ -2471,23 +2479,6 @@ class FeatureFlagsPoller {
2471
2479
  stopPoller() {
2472
2480
  clearTimeout(this.poller);
2473
2481
  }
2474
- _requestRemoteConfigPayload(flagKey) {
2475
- const url = `${this.host}/api/projects/@current/feature_flags/${flagKey}/remote_config/`;
2476
- const options = this.getPersonalApiKeyRequestOptions();
2477
- let abortTimeout = null;
2478
- if (this.timeout && typeof this.timeout === 'number') {
2479
- const controller = new AbortController();
2480
- abortTimeout = safeSetTimeout(() => {
2481
- controller.abort();
2482
- }, this.timeout);
2483
- options.signal = controller.signal;
2484
- }
2485
- try {
2486
- return this.fetch(url, options);
2487
- } finally {
2488
- clearTimeout(abortTimeout);
2489
- }
2490
- }
2491
2482
  }
2492
2483
  // # This function takes a distinct_id and a feature flag key and returns a float between 0 and 1.
2493
2484
  // # Given the same distinct_id and key, it'll always return the same float. These floats are
@@ -2772,21 +2763,25 @@ class PostHogBackendClient extends PostHogCoreStateless {
2772
2763
  if (options.personalApiKey.includes('phc_')) {
2773
2764
  throw new Error('Your Personal API key is invalid. These keys are prefixed with "phx_" and can be created in PostHog project settings.');
2774
2765
  }
2775
- this.featureFlagsPoller = new FeatureFlagsPoller({
2776
- pollingInterval: this.options.featureFlagsPollingInterval,
2777
- personalApiKey: options.personalApiKey,
2778
- projectApiKey: apiKey,
2779
- timeout: options.requestTimeout ?? 10000,
2780
- host: this.host,
2781
- fetch: options.fetch,
2782
- onError: err => {
2783
- this._events.emit('error', err);
2784
- },
2785
- onLoad: count => {
2786
- this._events.emit('localEvaluationFlagsLoaded', count);
2787
- },
2788
- customHeaders: this.getCustomHeaders()
2789
- });
2766
+ // Only start the poller if local evaluation is enabled (defaults to true for backward compatibility)
2767
+ const shouldEnableLocalEvaluation = options.enableLocalEvaluation !== false;
2768
+ if (shouldEnableLocalEvaluation) {
2769
+ this.featureFlagsPoller = new FeatureFlagsPoller({
2770
+ pollingInterval: this.options.featureFlagsPollingInterval,
2771
+ personalApiKey: options.personalApiKey,
2772
+ projectApiKey: apiKey,
2773
+ timeout: options.requestTimeout ?? 10000,
2774
+ host: this.host,
2775
+ fetch: options.fetch,
2776
+ onError: err => {
2777
+ this._events.emit('error', err);
2778
+ },
2779
+ onLoad: count => {
2780
+ this._events.emit('localEvaluationFlagsLoaded', count);
2781
+ },
2782
+ customHeaders: this.getCustomHeaders()
2783
+ });
2784
+ }
2790
2785
  }
2791
2786
  this.errorTracking = new ErrorTracking(this, options);
2792
2787
  this.distinctIdHasSentFlagCalls = {};
@@ -3142,7 +3137,10 @@ class PostHogBackendClient extends PostHogCoreStateless {
3142
3137
  return response;
3143
3138
  }
3144
3139
  async getRemoteConfigPayload(flagKey) {
3145
- const response = await this.featureFlagsPoller?._requestRemoteConfigPayload(flagKey);
3140
+ if (!this.options.personalApiKey) {
3141
+ throw new Error('Personal API key is required for remote config payload decryption');
3142
+ }
3143
+ const response = await this._requestRemoteConfigPayload(flagKey);
3146
3144
  if (!response) {
3147
3145
  return undefined;
3148
3146
  }
@@ -3236,6 +3234,38 @@ class PostHogBackendClient extends PostHogCoreStateless {
3236
3234
  this.featureFlagsPoller?.stopPoller();
3237
3235
  return super._shutdown(shutdownTimeoutMs);
3238
3236
  }
3237
+ async _requestRemoteConfigPayload(flagKey) {
3238
+ if (!this.options.personalApiKey) {
3239
+ return undefined;
3240
+ }
3241
+ const url = `${this.host}/api/projects/@current/feature_flags/${flagKey}/remote_config/`;
3242
+ const options = {
3243
+ method: 'GET',
3244
+ headers: {
3245
+ ...this.getCustomHeaders(),
3246
+ 'Content-Type': 'application/json',
3247
+ Authorization: `Bearer ${this.options.personalApiKey}`
3248
+ }
3249
+ };
3250
+ let abortTimeout = null;
3251
+ if (this.options.requestTimeout && typeof this.options.requestTimeout === 'number') {
3252
+ const controller = new AbortController();
3253
+ abortTimeout = safeSetTimeout(() => {
3254
+ controller.abort();
3255
+ }, this.options.requestTimeout);
3256
+ options.signal = controller.signal;
3257
+ }
3258
+ try {
3259
+ return await this.fetch(url, options);
3260
+ } catch (error) {
3261
+ this._events.emit('error', error);
3262
+ return undefined;
3263
+ } finally {
3264
+ if (abortTimeout) {
3265
+ clearTimeout(abortTimeout);
3266
+ }
3267
+ }
3268
+ }
3239
3269
  addLocalPersonAndGroupProperties(distinctId, groups, personProperties, groupProperties) {
3240
3270
  const allPersonProperties = {
3241
3271
  distinct_id: distinctId,
@@ -3257,9 +3287,18 @@ class PostHogBackendClient extends PostHogCoreStateless {
3257
3287
  }
3258
3288
  captureException(error, distinctId, additionalProperties) {
3259
3289
  const syntheticException = new Error('PostHog syntheticException');
3260
- ErrorTracking.captureException(this, error, {
3290
+ ErrorTracking.buildEventMessage(error, {
3291
+ syntheticException
3292
+ }, distinctId, additionalProperties).then(msg => {
3293
+ this.capture(msg);
3294
+ });
3295
+ }
3296
+ async captureExceptionImmediate(error, distinctId, additionalProperties) {
3297
+ const syntheticException = new Error('PostHog syntheticException');
3298
+ const evtMsg = await ErrorTracking.buildEventMessage(error, {
3261
3299
  syntheticException
3262
3300
  }, distinctId, additionalProperties);
3301
+ return await this.captureImmediate(evtMsg);
3263
3302
  }
3264
3303
  }
3265
3304