posthog-node 4.0.0 → 4.0.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+
2
+ # 4.0.1 - 2024-04-25
3
+
4
+ 1. Prevent double JSON parsing of feature flag payloads, which would convert the payload [1] into 1.
5
+
1
6
  # 4.0.0 - 2024-03-18
2
7
 
3
8
  ## Added
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.0.0";
7
+ var version = "4.0.1";
8
8
 
9
9
  var PostHogPersistedProperty;
10
10
  (function (PostHogPersistedProperty) {
@@ -1157,14 +1157,11 @@ class PostHogCoreStateless {
1157
1157
  if (response === undefined) {
1158
1158
  return null;
1159
1159
  }
1160
- return this._parsePayload(response);
1160
+ return response;
1161
1161
  }
1162
1162
  async getFeatureFlagPayloadsStateless(distinctId, groups = {}, personProperties = {}, groupProperties = {}, disableGeoip) {
1163
1163
  await this._initPromise;
1164
1164
  const payloads = (await this.getFeatureFlagsAndPayloadsStateless(distinctId, groups, personProperties, groupProperties, disableGeoip)).payloads;
1165
- if (payloads) {
1166
- return Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1167
- }
1168
1165
  return payloads;
1169
1166
  }
1170
1167
  _parsePayload(response) {
@@ -1188,9 +1185,13 @@ class PostHogCoreStateless {
1188
1185
  const decideResponse = await this.getDecide(distinctId, groups, personProperties, groupProperties, extraPayload);
1189
1186
  const flags = decideResponse?.featureFlags;
1190
1187
  const payloads = decideResponse?.featureFlagPayloads;
1188
+ let parsedPayloads = payloads;
1189
+ if (payloads) {
1190
+ parsedPayloads = Object.fromEntries(Object.entries(payloads).map(([k, v]) => [k, this._parsePayload(v)]));
1191
+ }
1191
1192
  return {
1192
1193
  flags,
1193
- payloads,
1194
+ payloads: parsedPayloads,
1194
1195
  };
1195
1196
  }
1196
1197
  /***
@@ -1543,11 +1544,15 @@ class FeatureFlagsPoller {
1543
1544
  } // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
1544
1545
 
1545
1546
 
1546
- if (response === undefined) {
1547
+ if (response === undefined || response === null) {
1547
1548
  return null;
1548
1549
  }
1549
1550
 
1550
- return response;
1551
+ try {
1552
+ return JSON.parse(response);
1553
+ } catch {
1554
+ return response;
1555
+ }
1551
1556
  }
1552
1557
 
1553
1558
  async getAllFlagsAndPayloads(distinctId, groups = {}, personProperties = {}, groupProperties = {}) {
@@ -1681,7 +1686,7 @@ class FeatureFlagsPoller {
1681
1686
  let matches = false;
1682
1687
 
1683
1688
  if (propertyType === 'cohort') {
1684
- matches = matchCohort(prop, properties, this.cohorts);
1689
+ matches = matchCohort(prop, properties, this.cohorts, this.debugMode);
1685
1690
  } else {
1686
1691
  matches = matchProperty(prop, properties);
1687
1692
  }
@@ -1937,7 +1942,7 @@ function matchProperty(property, propertyValues) {
1937
1942
  }
1938
1943
  }
1939
1944
 
1940
- function matchCohort(property, propertyValues, cohortProperties) {
1945
+ function matchCohort(property, propertyValues, cohortProperties, debugMode = false) {
1941
1946
  const cohortId = String(property.value);
1942
1947
 
1943
1948
  if (!(cohortId in cohortProperties)) {
@@ -1945,10 +1950,10 @@ function matchCohort(property, propertyValues, cohortProperties) {
1945
1950
  }
1946
1951
 
1947
1952
  const propertyGroup = cohortProperties[cohortId];
1948
- return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties);
1953
+ return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode);
1949
1954
  }
1950
1955
 
1951
- function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1956
+ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties, debugMode = false) {
1952
1957
  if (!propertyGroup) {
1953
1958
  return true;
1954
1959
  }
@@ -1967,7 +1972,7 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1967
1972
  // a nested property group
1968
1973
  for (const prop of properties) {
1969
1974
  try {
1970
- const matches = matchPropertyGroup(prop, propertyValues, cohortProperties);
1975
+ const matches = matchPropertyGroup(prop, propertyValues, cohortProperties, debugMode);
1971
1976
 
1972
1977
  if (propertyGroupType === 'AND') {
1973
1978
  if (!matches) {
@@ -1981,7 +1986,10 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
1981
1986
  }
1982
1987
  } catch (err) {
1983
1988
  if (err instanceof InconclusiveMatchError) {
1984
- console.debug(`Failed to compute property ${prop} locally: ${err}`);
1989
+ if (debugMode) {
1990
+ console.debug(`Failed to compute property ${prop} locally: ${err}`);
1991
+ }
1992
+
1985
1993
  errorMatchingLocally = true;
1986
1994
  } else {
1987
1995
  throw err;
@@ -2001,7 +2009,7 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
2001
2009
  let matches;
2002
2010
 
2003
2011
  if (prop.type === 'cohort') {
2004
- matches = matchCohort(prop, propertyValues, cohortProperties);
2012
+ matches = matchCohort(prop, propertyValues, cohortProperties, debugMode);
2005
2013
  } else {
2006
2014
  matches = matchProperty(prop, propertyValues);
2007
2015
  }
@@ -2029,7 +2037,10 @@ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
2029
2037
  }
2030
2038
  } catch (err) {
2031
2039
  if (err instanceof InconclusiveMatchError) {
2032
- console.debug(`Failed to compute property ${prop} locally: ${err}`);
2040
+ if (debugMode) {
2041
+ console.debug(`Failed to compute property ${prop} locally: ${err}`);
2042
+ }
2043
+
2033
2044
  errorMatchingLocally = true;
2034
2045
  } else {
2035
2046
  throw err;
@@ -2378,11 +2389,7 @@ class PostHog extends PostHogCoreStateless {
2378
2389
  response = await super.getFeatureFlagPayloadStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
2379
2390
  }
2380
2391
 
2381
- try {
2382
- return JSON.parse(response);
2383
- } catch {
2384
- return response;
2385
- }
2392
+ return response;
2386
2393
  }
2387
2394
 
2388
2395
  async isFeatureEnabled(key, distinctId, options) {