posthog-node 3.1.0 → 3.1.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.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createHash } from 'crypto';
1
+ import { createHash } from 'rusha';
2
2
  import axios from 'axios';
3
3
 
4
4
  /******************************************************************************
@@ -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 = "3.1.0";
158
+ var version = "3.1.2";
159
159
 
160
160
  var PostHogPersistedProperty;
161
161
  (function (PostHogPersistedProperty) {
@@ -1145,7 +1145,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1145
1145
  return [4 /*yield*/, this.flushAsync()];
1146
1146
  case 2:
1147
1147
  _a.sent();
1148
- return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
1148
+ return [4 /*yield*/, Promise.all(Object.values(this.pendingPromises).map(function (x) {
1149
+ return x.catch(function () {
1150
+ // ignore errors as we are shutting down and can't deal with them anyways.
1151
+ });
1152
+ }))];
1149
1153
  case 3:
1150
1154
  _a.sent();
1151
1155
  return [3 /*break*/, 5];
@@ -1175,14 +1179,9 @@ var PostHogCoreStateless = /** @class */ (function () {
1175
1179
  var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1176
1180
  _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1177
1181
  _this.flagCallReported = {};
1182
+ _this.sessionProps = {};
1178
1183
  _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1179
1184
  _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1180
- // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
1181
- if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1182
- safeSetTimeout(function () {
1183
- _this.reloadFeatureFlags();
1184
- }, 1);
1185
- }
1186
1185
  return _this;
1187
1186
  }
1188
1187
  PostHogCore.prototype.setupBootstrap = function (options) {
@@ -1222,6 +1221,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1222
1221
  });
1223
1222
  PostHogCore.prototype.clearProps = function () {
1224
1223
  this.props = undefined;
1224
+ this.sessionProps = {};
1225
1225
  };
1226
1226
  PostHogCore.prototype.on = function (event, cb) {
1227
1227
  return this._events.on(event, cb);
@@ -1249,7 +1249,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1249
1249
  return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
1250
1250
  };
1251
1251
  PostHogCore.prototype.enrichProperties = function (properties) {
1252
- return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1252
+ return __assign(__assign(__assign(__assign(__assign({}, this.props), this.sessionProps), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1253
1253
  };
1254
1254
  PostHogCore.prototype.getSessionId = function () {
1255
1255
  var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
@@ -1275,13 +1275,19 @@ var PostHogCoreStateless = /** @class */ (function () {
1275
1275
  PostHogCore.prototype.getDistinctId = function () {
1276
1276
  return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1277
1277
  };
1278
+ PostHogCore.prototype.unregister = function (property) {
1279
+ delete this.props[property];
1280
+ this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1281
+ };
1278
1282
  PostHogCore.prototype.register = function (properties) {
1279
1283
  this.props = __assign(__assign({}, this.props), properties);
1280
1284
  this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1281
1285
  };
1282
- PostHogCore.prototype.unregister = function (property) {
1283
- delete this.props[property];
1284
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1286
+ PostHogCore.prototype.registerForSession = function (properties) {
1287
+ this.sessionProps = __assign(__assign({}, this.sessionProps), properties);
1288
+ };
1289
+ PostHogCore.prototype.unregisterForSession = function (property) {
1290
+ delete this.sessionProps[property];
1285
1291
  };
1286
1292
  /***
1287
1293
  *** TRACKING
@@ -1297,9 +1303,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1297
1303
  // We keep the AnonymousId to be used by decide calls and identify to link the previousId
1298
1304
  this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
1299
1305
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
1300
- if (this.getFeatureFlags()) {
1301
- this.reloadFeatureFlags();
1302
- }
1306
+ this.reloadFeatureFlags();
1303
1307
  }
1304
1308
  _super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
1305
1309
  return this;
@@ -1339,7 +1343,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1339
1343
  this.register({
1340
1344
  $groups: __assign(__assign({}, existingGroups), groups),
1341
1345
  });
1342
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
1346
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; })) {
1343
1347
  this.reloadFeatureFlags();
1344
1348
  }
1345
1349
  return this;
@@ -2280,9 +2284,10 @@ function () {
2280
2284
  function _hash(key, distinctId, salt) {
2281
2285
  if (salt === void 0) {
2282
2286
  salt = '';
2283
- }
2287
+ } // rusha is a fast sha1 implementation in pure javascript
2284
2288
 
2285
- var sha1Hash = createHash('sha1');
2289
+
2290
+ var sha1Hash = createHash();
2286
2291
  sha1Hash.update("".concat(key, ".").concat(distinctId).concat(salt));
2287
2292
  return parseInt(sha1Hash.digest('hex').slice(0, 15), 16) / LONG_SCALE;
2288
2293
  }
@@ -2650,8 +2655,10 @@ function (_super) {
2650
2655
  return __awaiter(this, void 0, void 0, function () {
2651
2656
  var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
2652
2657
 
2653
- return __generator(this, function (_d) {
2654
- switch (_d.label) {
2658
+ var _d;
2659
+
2660
+ return __generator(this, function (_e) {
2661
+ switch (_e.label) {
2655
2662
  case 0:
2656
2663
  _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2657
2664
  _c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, sendFeatureFlagEvents = _c.sendFeatureFlagEvents; // set defaults
@@ -2669,7 +2676,7 @@ function (_super) {
2669
2676
  , (_a = this.featureFlagsPoller) === null || _a === void 0 ? void 0 : _a.getFeatureFlag(key, distinctId, groups, personProperties, groupProperties)];
2670
2677
 
2671
2678
  case 1:
2672
- response = _d.sent();
2679
+ response = _e.sent();
2673
2680
  flagWasLocallyEvaluated = response !== undefined;
2674
2681
  if (!(!flagWasLocallyEvaluated && !onlyEvaluateLocally)) return [3
2675
2682
  /*break*/
@@ -2679,8 +2686,8 @@ function (_super) {
2679
2686
  , _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2680
2687
 
2681
2688
  case 2:
2682
- response = _d.sent();
2683
- _d.label = 3;
2689
+ response = _e.sent();
2690
+ _e.label = 3;
2684
2691
 
2685
2692
  case 3:
2686
2693
  featureFlagReportedKey = "".concat(key, "_").concat(response);
@@ -2699,11 +2706,11 @@ function (_super) {
2699
2706
  this.capture({
2700
2707
  distinctId: distinctId,
2701
2708
  event: '$feature_flag_called',
2702
- properties: {
2709
+ properties: (_d = {
2703
2710
  $feature_flag: key,
2704
2711
  $feature_flag_response: response,
2705
2712
  locally_evaluated: flagWasLocallyEvaluated
2706
- },
2713
+ }, _d["$feature/".concat(key)] = response, _d),
2707
2714
  groups: groups,
2708
2715
  disableGeoip: disableGeoip
2709
2716
  });