posthog-node 2.5.4 → 2.6.0

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.esm.js CHANGED
@@ -155,7 +155,7 @@ function __spreadArray(to, from, pack) {
155
155
  return to.concat(ar || Array.prototype.slice.call(from));
156
156
  }
157
157
 
158
- var version = "2.5.4";
158
+ var version = "2.6.0";
159
159
 
160
160
  var PostHogPersistedProperty;
161
161
  (function (PostHogPersistedProperty) {
@@ -1598,6 +1598,7 @@ function () {
1598
1598
  this.featureFlags = [];
1599
1599
  this.featureFlagsByKey = {};
1600
1600
  this.groupTypeMapping = {};
1601
+ this.cohorts = {};
1601
1602
  this.loadedSuccessfullyOnce = false;
1602
1603
  this.timeout = timeout;
1603
1604
  this.projectApiKey = projectApiKey;
@@ -1935,14 +1936,23 @@ function () {
1935
1936
  var rolloutPercentage = condition.rollout_percentage;
1936
1937
 
1937
1938
  if ((condition.properties || []).length > 0) {
1938
- var matchAll = condition.properties.every(function (property) {
1939
- return matchProperty(property, properties);
1940
- });
1939
+ for (var _i = 0, _a = condition.properties; _i < _a.length; _i++) {
1940
+ var prop = _a[_i];
1941
+ var propertyType = prop.type;
1942
+ var matches = false;
1941
1943
 
1942
- if (!matchAll) {
1943
- return false;
1944
- } else if (rolloutPercentage == undefined) {
1945
- // == to include `null` as a match, not just `undefined`
1944
+ if (propertyType === 'cohort') {
1945
+ matches = matchCohort(prop, properties, this.cohorts);
1946
+ } else {
1947
+ matches = matchProperty(prop, properties);
1948
+ }
1949
+
1950
+ if (!matches) {
1951
+ return false;
1952
+ }
1953
+ }
1954
+
1955
+ if (rolloutPercentage == undefined) {
1946
1956
  return true;
1947
1957
  }
1948
1958
  }
@@ -2075,6 +2085,7 @@ function () {
2075
2085
  return acc[curr.key] = curr, acc;
2076
2086
  }, {});
2077
2087
  this.groupTypeMapping = responseJson.group_type_mapping || {};
2088
+ this.cohorts = responseJson.cohorts || [];
2078
2089
  this.loadedSuccessfullyOnce = true;
2079
2090
  return [3
2080
2091
  /*break*/
@@ -2107,7 +2118,7 @@ function () {
2107
2118
  return __generator(this, function (_a) {
2108
2119
  switch (_a.label) {
2109
2120
  case 0:
2110
- url = "".concat(this.host, "/api/feature_flag/local_evaluation?token=").concat(this.projectApiKey);
2121
+ url = "".concat(this.host, "/api/feature_flag/local_evaluation?token=").concat(this.projectApiKey, "&send_cohorts");
2111
2122
  options = {
2112
2123
  method: 'GET',
2113
2124
  headers: {
@@ -2244,6 +2255,119 @@ function matchProperty(property, propertyValues) {
2244
2255
  }
2245
2256
  }
2246
2257
 
2258
+ function matchCohort(property, propertyValues, cohortProperties) {
2259
+ var cohortId = String(property.value);
2260
+
2261
+ if (!(cohortId in cohortProperties)) {
2262
+ throw new InconclusiveMatchError("can't match cohort without a given cohort property value");
2263
+ }
2264
+
2265
+ var propertyGroup = cohortProperties[cohortId];
2266
+ return matchPropertyGroup(propertyGroup, propertyValues, cohortProperties);
2267
+ }
2268
+
2269
+ function matchPropertyGroup(propertyGroup, propertyValues, cohortProperties) {
2270
+ if (!propertyGroup) {
2271
+ return true;
2272
+ }
2273
+
2274
+ var propertyGroupType = propertyGroup.type;
2275
+ var properties = propertyGroup.values;
2276
+
2277
+ if (!properties || properties.length === 0) {
2278
+ // empty groups are no-ops, always match
2279
+ return true;
2280
+ }
2281
+
2282
+ var errorMatchingLocally = false;
2283
+
2284
+ if ('values' in properties[0]) {
2285
+ // a nested property group
2286
+ for (var _i = 0, _a = properties; _i < _a.length; _i++) {
2287
+ var prop = _a[_i];
2288
+
2289
+ try {
2290
+ var matches = matchPropertyGroup(prop, propertyValues, cohortProperties);
2291
+
2292
+ if (propertyGroupType === 'AND') {
2293
+ if (!matches) {
2294
+ return false;
2295
+ }
2296
+ } else {
2297
+ // OR group
2298
+ if (matches) {
2299
+ return true;
2300
+ }
2301
+ }
2302
+ } catch (err) {
2303
+ if (err instanceof InconclusiveMatchError) {
2304
+ console.debug("Failed to compute property ".concat(prop, " locally: ").concat(err));
2305
+ errorMatchingLocally = true;
2306
+ } else {
2307
+ throw err;
2308
+ }
2309
+ }
2310
+ }
2311
+
2312
+ if (errorMatchingLocally) {
2313
+ throw new InconclusiveMatchError("Can't match cohort without a given cohort property value");
2314
+ } // if we get here, all matched in AND case, or none matched in OR case
2315
+
2316
+
2317
+ return propertyGroupType === 'AND';
2318
+ } else {
2319
+ for (var _b = 0, _c = properties; _b < _c.length; _b++) {
2320
+ var prop = _c[_b];
2321
+
2322
+ try {
2323
+ var matches = void 0;
2324
+
2325
+ if (prop.type === 'cohort') {
2326
+ matches = matchCohort(prop, propertyValues, cohortProperties);
2327
+ } else {
2328
+ matches = matchProperty(prop, propertyValues);
2329
+ }
2330
+
2331
+ var negation = prop.negation || false;
2332
+
2333
+ if (propertyGroupType === 'AND') {
2334
+ // if negated property, do the inverse
2335
+ if (!matches && !negation) {
2336
+ return false;
2337
+ }
2338
+
2339
+ if (matches && negation) {
2340
+ return false;
2341
+ }
2342
+ } else {
2343
+ // OR group
2344
+ if (matches && !negation) {
2345
+ return true;
2346
+ }
2347
+
2348
+ if (!matches && negation) {
2349
+ return true;
2350
+ }
2351
+ }
2352
+ } catch (err) {
2353
+ if (err instanceof InconclusiveMatchError) {
2354
+ console.debug("Failed to compute property ".concat(prop, " locally: ").concat(err));
2355
+ errorMatchingLocally = true;
2356
+ } else {
2357
+ throw err;
2358
+ }
2359
+ }
2360
+ }
2361
+
2362
+ if (errorMatchingLocally) {
2363
+ throw new InconclusiveMatchError("can't match cohort without a given cohort property value");
2364
+ } // if we get here, all matched in AND case, or none matched in OR case
2365
+
2366
+
2367
+ return propertyGroupType === 'AND';
2368
+ }
2369
+ }
2370
+
2247
2371
  function isValidRegex(regex) {
2248
2372
  try {
2249
2373
  new RegExp(regex);