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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 3.1.2 - 2023-08-17
2
+
3
+ 1. Returns the current flag property with $feature_flag_called events, to make it easier to use in experiments
4
+
5
+ # 3.1.1 - 2023-04-26
6
+
7
+ 1. Replace crypto library with pure-js rusha library which makes posthog-node work with Cloudflare Workers in Next.js edge runtime.
8
+
1
9
  # 3.1.0 - 2023-04-19
2
10
 
3
11
  1. Some small fixes to incorrect types
package/lib/index.cjs.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var crypto = require('crypto');
5
+ var rusha = require('rusha');
6
6
  var axios = require('axios');
7
7
 
8
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -163,7 +163,7 @@ function __spreadArray(to, from, pack) {
163
163
  return to.concat(ar || Array.prototype.slice.call(from));
164
164
  }
165
165
 
166
- var version = "3.1.0";
166
+ var version = "3.1.2";
167
167
 
168
168
  var PostHogPersistedProperty;
169
169
  (function (PostHogPersistedProperty) {
@@ -1153,7 +1153,11 @@ var PostHogCoreStateless = /** @class */ (function () {
1153
1153
  return [4 /*yield*/, this.flushAsync()];
1154
1154
  case 2:
1155
1155
  _a.sent();
1156
- return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
1156
+ return [4 /*yield*/, Promise.all(Object.values(this.pendingPromises).map(function (x) {
1157
+ return x.catch(function () {
1158
+ // ignore errors as we are shutting down and can't deal with them anyways.
1159
+ });
1160
+ }))];
1157
1161
  case 3:
1158
1162
  _a.sent();
1159
1163
  return [3 /*break*/, 5];
@@ -1183,14 +1187,9 @@ var PostHogCoreStateless = /** @class */ (function () {
1183
1187
  var disableGeoipOption = (_a = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _a !== void 0 ? _a : false;
1184
1188
  _this = _super.call(this, apiKey, __assign(__assign({}, options), { disableGeoip: disableGeoipOption })) || this;
1185
1189
  _this.flagCallReported = {};
1190
+ _this.sessionProps = {};
1186
1191
  _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1187
1192
  _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1188
- // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
1189
- if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1190
- safeSetTimeout(function () {
1191
- _this.reloadFeatureFlags();
1192
- }, 1);
1193
- }
1194
1193
  return _this;
1195
1194
  }
1196
1195
  PostHogCore.prototype.setupBootstrap = function (options) {
@@ -1230,6 +1229,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1230
1229
  });
1231
1230
  PostHogCore.prototype.clearProps = function () {
1232
1231
  this.props = undefined;
1232
+ this.sessionProps = {};
1233
1233
  };
1234
1234
  PostHogCore.prototype.on = function (event, cb) {
1235
1235
  return this._events.on(event, cb);
@@ -1257,7 +1257,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1257
1257
  return __assign(__assign({ $active_feature_flags: featureFlags ? Object.keys(featureFlags) : undefined }, featureVariantProperties), _super.prototype.getCommonEventProperties.call(this));
1258
1258
  };
1259
1259
  PostHogCore.prototype.enrichProperties = function (properties) {
1260
- return __assign(__assign(__assign(__assign({}, this.props), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1260
+ return __assign(__assign(__assign(__assign(__assign({}, this.props), this.sessionProps), (properties || {})), this.getCommonEventProperties()), { $session_id: this.getSessionId() });
1261
1261
  };
1262
1262
  PostHogCore.prototype.getSessionId = function () {
1263
1263
  var sessionId = this.getPersistedProperty(PostHogPersistedProperty.SessionId);
@@ -1283,13 +1283,19 @@ var PostHogCoreStateless = /** @class */ (function () {
1283
1283
  PostHogCore.prototype.getDistinctId = function () {
1284
1284
  return this.getPersistedProperty(PostHogPersistedProperty.DistinctId) || this.getAnonymousId();
1285
1285
  };
1286
+ PostHogCore.prototype.unregister = function (property) {
1287
+ delete this.props[property];
1288
+ this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1289
+ };
1286
1290
  PostHogCore.prototype.register = function (properties) {
1287
1291
  this.props = __assign(__assign({}, this.props), properties);
1288
1292
  this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1289
1293
  };
1290
- PostHogCore.prototype.unregister = function (property) {
1291
- delete this.props[property];
1292
- this.setPersistedProperty(PostHogPersistedProperty.Props, this.props);
1294
+ PostHogCore.prototype.registerForSession = function (properties) {
1295
+ this.sessionProps = __assign(__assign({}, this.sessionProps), properties);
1296
+ };
1297
+ PostHogCore.prototype.unregisterForSession = function (property) {
1298
+ delete this.sessionProps[property];
1293
1299
  };
1294
1300
  /***
1295
1301
  *** TRACKING
@@ -1305,9 +1311,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1305
1311
  // We keep the AnonymousId to be used by decide calls and identify to link the previousId
1306
1312
  this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
1307
1313
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
1308
- if (this.getFeatureFlags()) {
1309
- this.reloadFeatureFlags();
1310
- }
1314
+ this.reloadFeatureFlags();
1311
1315
  }
1312
1316
  _super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
1313
1317
  return this;
@@ -1347,7 +1351,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1347
1351
  this.register({
1348
1352
  $groups: __assign(__assign({}, existingGroups), groups),
1349
1353
  });
1350
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
1354
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; })) {
1351
1355
  this.reloadFeatureFlags();
1352
1356
  }
1353
1357
  return this;
@@ -2288,9 +2292,10 @@ function () {
2288
2292
  function _hash(key, distinctId, salt) {
2289
2293
  if (salt === void 0) {
2290
2294
  salt = '';
2291
- }
2295
+ } // rusha is a fast sha1 implementation in pure javascript
2292
2296
 
2293
- var sha1Hash = crypto.createHash('sha1');
2297
+
2298
+ var sha1Hash = rusha.createHash();
2294
2299
  sha1Hash.update("".concat(key, ".").concat(distinctId).concat(salt));
2295
2300
  return parseInt(sha1Hash.digest('hex').slice(0, 15), 16) / LONG_SCALE;
2296
2301
  }
@@ -2658,8 +2663,10 @@ function (_super) {
2658
2663
  return __awaiter(this, void 0, void 0, function () {
2659
2664
  var _b, groups, personProperties, groupProperties, disableGeoip, _c, onlyEvaluateLocally, sendFeatureFlagEvents, response, flagWasLocallyEvaluated, featureFlagReportedKey;
2660
2665
 
2661
- return __generator(this, function (_d) {
2662
- switch (_d.label) {
2666
+ var _d;
2667
+
2668
+ return __generator(this, function (_e) {
2669
+ switch (_e.label) {
2663
2670
  case 0:
2664
2671
  _b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties, disableGeoip = _b.disableGeoip;
2665
2672
  _c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, sendFeatureFlagEvents = _c.sendFeatureFlagEvents; // set defaults
@@ -2677,7 +2684,7 @@ function (_super) {
2677
2684
  , (_a = this.featureFlagsPoller) === null || _a === void 0 ? void 0 : _a.getFeatureFlag(key, distinctId, groups, personProperties, groupProperties)];
2678
2685
 
2679
2686
  case 1:
2680
- response = _d.sent();
2687
+ response = _e.sent();
2681
2688
  flagWasLocallyEvaluated = response !== undefined;
2682
2689
  if (!(!flagWasLocallyEvaluated && !onlyEvaluateLocally)) return [3
2683
2690
  /*break*/
@@ -2687,8 +2694,8 @@ function (_super) {
2687
2694
  , _super.prototype.getFeatureFlagStateless.call(this, key, distinctId, groups, personProperties, groupProperties, disableGeoip)];
2688
2695
 
2689
2696
  case 2:
2690
- response = _d.sent();
2691
- _d.label = 3;
2697
+ response = _e.sent();
2698
+ _e.label = 3;
2692
2699
 
2693
2700
  case 3:
2694
2701
  featureFlagReportedKey = "".concat(key, "_").concat(response);
@@ -2707,11 +2714,11 @@ function (_super) {
2707
2714
  this.capture({
2708
2715
  distinctId: distinctId,
2709
2716
  event: '$feature_flag_called',
2710
- properties: {
2717
+ properties: (_d = {
2711
2718
  $feature_flag: key,
2712
2719
  $feature_flag_response: response,
2713
2720
  locally_evaluated: flagWasLocallyEvaluated
2714
- },
2721
+ }, _d["$feature/".concat(key)] = response, _d),
2715
2722
  groups: groups,
2716
2723
  disableGeoip: disableGeoip
2717
2724
  });