posthog-node 3.0.0 → 3.1.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/lib/index.d.ts CHANGED
@@ -85,7 +85,7 @@ declare type JsonType = string | number | boolean | null | {
85
85
  interface RetriableOptions {
86
86
  retryCount?: number;
87
87
  retryDelay?: number;
88
- retryCheck?: (err: any) => true;
88
+ retryCheck?: (err: any) => boolean;
89
89
  }
90
90
 
91
91
  declare class SimpleEventEmitter {
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.0.0";
158
+ var version = "3.1.1";
159
159
 
160
160
  var PostHogPersistedProperty;
161
161
  (function (PostHogPersistedProperty) {
@@ -193,29 +193,31 @@ function retriable(fn, props) {
193
193
  i = 0;
194
194
  _d.label = 1;
195
195
  case 1:
196
- if (!(i < retryCount + 1)) return [3 /*break*/, 8];
197
- _d.label = 2;
196
+ if (!(i < retryCount + 1)) return [3 /*break*/, 7];
197
+ if (!(i > 0)) return [3 /*break*/, 3];
198
+ // don't wait when it's the last try
199
+ return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
198
200
  case 2:
199
- _d.trys.push([2, 4, , 5]);
200
- return [4 /*yield*/, fn()];
201
+ // don't wait when it's the last try
202
+ _d.sent();
203
+ _d.label = 3;
201
204
  case 3:
205
+ _d.trys.push([3, 5, , 6]);
206
+ return [4 /*yield*/, fn()];
207
+ case 4:
202
208
  res = _d.sent();
203
209
  return [2 /*return*/, res];
204
- case 4:
210
+ case 5:
205
211
  e_1 = _d.sent();
206
212
  lastError = e_1;
207
213
  if (!retryCheck(e_1)) {
208
214
  throw e_1;
209
215
  }
210
- return [3 /*break*/, 5];
211
- case 5: return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, retryDelay); })];
216
+ return [3 /*break*/, 6];
212
217
  case 6:
213
- _d.sent();
214
- _d.label = 7;
215
- case 7:
216
218
  i++;
217
219
  return [3 /*break*/, 1];
218
- case 8: throw lastError;
220
+ case 7: throw lastError;
219
221
  }
220
222
  });
221
223
  });
@@ -714,6 +716,29 @@ var SimpleEventEmitter = /** @class */ (function () {
714
716
  return SimpleEventEmitter;
715
717
  }());
716
718
 
719
+ var PostHogFetchHttpError = /** @class */ (function (_super) {
720
+ __extends(PostHogFetchHttpError, _super);
721
+ function PostHogFetchHttpError(response) {
722
+ var _this = _super.call(this, 'HTTP error while fetching PostHog: ' + response.status) || this;
723
+ _this.response = response;
724
+ _this.name = 'PostHogFetchHttpError';
725
+ return _this;
726
+ }
727
+ return PostHogFetchHttpError;
728
+ }(Error));
729
+ var PostHogFetchNetworkError = /** @class */ (function (_super) {
730
+ __extends(PostHogFetchNetworkError, _super);
731
+ function PostHogFetchNetworkError(error) {
732
+ var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
733
+ _this.error = error;
734
+ _this.name = 'PostHogFetchNetworkError';
735
+ return _this;
736
+ }
737
+ return PostHogFetchNetworkError;
738
+ }(Error));
739
+ function isPostHogFetchError(err) {
740
+ return typeof err === 'object' && (err.name === 'PostHogFetchHttpError' || err.name === 'PostHogFetchNetworkError');
741
+ }
717
742
  var PostHogCoreStateless = /** @class */ (function () {
718
743
  function PostHogCoreStateless(apiKey, options) {
719
744
  var _a, _b, _c, _d, _e;
@@ -733,6 +758,7 @@ var PostHogCoreStateless = /** @class */ (function () {
733
758
  this._retryOptions = {
734
759
  retryCount: (_b = options === null || options === void 0 ? void 0 : options.fetchRetryCount) !== null && _b !== void 0 ? _b : 3,
735
760
  retryDelay: (_c = options === null || options === void 0 ? void 0 : options.fetchRetryDelay) !== null && _c !== void 0 ? _c : 3000,
761
+ retryCheck: isPostHogFetchError,
736
762
  };
737
763
  this.requestTimeout = (_d = options === null || options === void 0 ? void 0 : options.requestTimeout) !== null && _d !== void 0 ? _d : 10000; // 10 seconds
738
764
  this.disableGeoip = (_e = options === null || options === void 0 ? void 0 : options.disableGeoip) !== null && _e !== void 0 ? _e : true;
@@ -1025,6 +1051,9 @@ var PostHogCoreStateless = /** @class */ (function () {
1025
1051
  };
1026
1052
  var promiseUUID = generateUUID();
1027
1053
  var done = function (err) {
1054
+ if (err) {
1055
+ _this._events.emit('error', err);
1056
+ }
1028
1057
  callback === null || callback === void 0 ? void 0 : callback(err, messages);
1029
1058
  // remove promise from pendingPromises
1030
1059
  delete _this.pendingPromises[promiseUUID];
@@ -1057,10 +1086,6 @@ var PostHogCoreStateless = /** @class */ (function () {
1057
1086
  requestPromise
1058
1087
  .then(function () { return done(); })
1059
1088
  .catch(function (err) {
1060
- if (err.response) {
1061
- var error = new Error(err.response.statusText);
1062
- return done(error);
1063
- }
1064
1089
  done(err);
1065
1090
  });
1066
1091
  };
@@ -1070,30 +1095,72 @@ var PostHogCoreStateless = /** @class */ (function () {
1070
1095
  return __awaiter(this, void 0, void 0, function () {
1071
1096
  var _this = this;
1072
1097
  return __generator(this, function (_c) {
1073
- (_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
1074
- var ctrl = new AbortController();
1075
- setTimeout(function () { return ctrl.abort(); }, ms);
1076
- return ctrl.signal;
1077
- });
1078
- return [2 /*return*/, retriable(function () {
1079
- return _this.fetch(url, __assign({ signal: AbortSignal.timeout(_this.requestTimeout) }, options));
1080
- }, retryOptions || this._retryOptions)];
1098
+ switch (_c.label) {
1099
+ case 0:
1100
+ (_a = (_b = AbortSignal).timeout) !== null && _a !== void 0 ? _a : (_b.timeout = function timeout(ms) {
1101
+ var ctrl = new AbortController();
1102
+ setTimeout(function () { return ctrl.abort(); }, ms);
1103
+ return ctrl.signal;
1104
+ });
1105
+ return [4 /*yield*/, retriable(function () { return __awaiter(_this, void 0, void 0, function () {
1106
+ var res, e_1;
1107
+ return __generator(this, function (_a) {
1108
+ switch (_a.label) {
1109
+ case 0:
1110
+ res = null;
1111
+ _a.label = 1;
1112
+ case 1:
1113
+ _a.trys.push([1, 3, , 4]);
1114
+ return [4 /*yield*/, this.fetch(url, __assign({ signal: AbortSignal.timeout(this.requestTimeout) }, options))];
1115
+ case 2:
1116
+ res = _a.sent();
1117
+ return [3 /*break*/, 4];
1118
+ case 3:
1119
+ e_1 = _a.sent();
1120
+ // fetch will only throw on network errors or on timeouts
1121
+ throw new PostHogFetchNetworkError(e_1);
1122
+ case 4:
1123
+ if (res.status < 200 || res.status >= 400) {
1124
+ throw new PostHogFetchHttpError(res);
1125
+ }
1126
+ return [2 /*return*/, res];
1127
+ }
1128
+ });
1129
+ }); }, __assign(__assign({}, this._retryOptions), retryOptions))];
1130
+ case 1: return [2 /*return*/, _c.sent()];
1131
+ }
1081
1132
  });
1082
1133
  });
1083
1134
  };
1084
1135
  PostHogCoreStateless.prototype.shutdownAsync = function () {
1085
1136
  return __awaiter(this, void 0, void 0, function () {
1137
+ var e_2;
1086
1138
  return __generator(this, function (_a) {
1087
1139
  switch (_a.label) {
1088
1140
  case 0:
1089
1141
  clearTimeout(this._flushTimer);
1090
- return [4 /*yield*/, this.flushAsync()];
1142
+ _a.label = 1;
1091
1143
  case 1:
1092
- _a.sent();
1093
- return [4 /*yield*/, Promise.allSettled(Object.values(this.pendingPromises))];
1144
+ _a.trys.push([1, 4, , 5]);
1145
+ return [4 /*yield*/, this.flushAsync()];
1094
1146
  case 2:
1095
1147
  _a.sent();
1096
- return [2 /*return*/];
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
+ }))];
1153
+ case 3:
1154
+ _a.sent();
1155
+ return [3 /*break*/, 5];
1156
+ case 4:
1157
+ e_2 = _a.sent();
1158
+ if (!isPostHogFetchError(e_2)) {
1159
+ throw e_2;
1160
+ }
1161
+ console.error('Error while shutting down PostHog', e_2);
1162
+ return [3 /*break*/, 5];
1163
+ case 5: return [2 /*return*/];
1097
1164
  }
1098
1165
  });
1099
1166
  });
@@ -1114,12 +1181,6 @@ var PostHogCoreStateless = /** @class */ (function () {
1114
1181
  _this.flagCallReported = {};
1115
1182
  _this.sendFeatureFlagEvent = (_b = options === null || options === void 0 ? void 0 : options.sendFeatureFlagEvent) !== null && _b !== void 0 ? _b : true;
1116
1183
  _this._sessionExpirationTimeSeconds = (_c = options === null || options === void 0 ? void 0 : options.sessionExpirationTimeSeconds) !== null && _c !== void 0 ? _c : 1800; // 30 minutes
1117
- // NOTE: It is important we don't initiate anything in the constructor as some async IO may still be underway on the parent
1118
- if ((options === null || options === void 0 ? void 0 : options.preloadFeatureFlags) !== false) {
1119
- safeSetTimeout(function () {
1120
- _this.reloadFeatureFlags();
1121
- }, 1);
1122
- }
1123
1184
  return _this;
1124
1185
  }
1125
1186
  PostHogCore.prototype.setupBootstrap = function (options) {
@@ -1234,9 +1295,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1234
1295
  // We keep the AnonymousId to be used by decide calls and identify to link the previousId
1235
1296
  this.setPersistedProperty(PostHogPersistedProperty.AnonymousId, previousDistinctId);
1236
1297
  this.setPersistedProperty(PostHogPersistedProperty.DistinctId, distinctId);
1237
- if (this.getFeatureFlags()) {
1238
- this.reloadFeatureFlags();
1239
- }
1298
+ this.reloadFeatureFlags();
1240
1299
  }
1241
1300
  _super.prototype.identifyStateless.call(this, distinctId, allProperties, options);
1242
1301
  return this;
@@ -1276,7 +1335,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1276
1335
  this.register({
1277
1336
  $groups: __assign(__assign({}, existingGroups), groups),
1278
1337
  });
1279
- if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; }) && this.getFeatureFlags()) {
1338
+ if (Object.keys(groups).find(function (type) { return existingGroups[type] !== groups[type]; })) {
1280
1339
  this.reloadFeatureFlags();
1281
1340
  }
1282
1341
  return this;
@@ -1537,7 +1596,11 @@ var fetch = function (url, options) {
1537
1596
  headers: options.headers,
1538
1597
  method: options.method.toLowerCase(),
1539
1598
  data: options.body,
1540
- signal: options.signal
1599
+ signal: options.signal,
1600
+ // fetch only throws on network errors, not on HTTP errors
1601
+ validateStatus: function () {
1602
+ return true;
1603
+ }
1541
1604
  })];
1542
1605
 
1543
1606
  case 1:
@@ -1547,10 +1610,22 @@ var fetch = function (url, options) {
1547
1610
  , {
1548
1611
  status: res.status,
1549
1612
  text: function () {
1550
- return res.data;
1613
+ return __awaiter(void 0, void 0, void 0, function () {
1614
+ return __generator(this, function (_a) {
1615
+ return [2
1616
+ /*return*/
1617
+ , res.data];
1618
+ });
1619
+ });
1551
1620
  },
1552
1621
  json: function () {
1553
- return res.data;
1622
+ return __awaiter(void 0, void 0, void 0, function () {
1623
+ return __generator(this, function (_a) {
1624
+ return [2
1625
+ /*return*/
1626
+ , res.data];
1627
+ });
1628
+ });
1554
1629
  }
1555
1630
  }];
1556
1631
  }
@@ -2170,7 +2245,7 @@ function () {
2170
2245
 
2171
2246
  case 3:
2172
2247
  err_2 = _a.sent();
2173
- throw new Error("Request failed with error: ".concat(err_2));
2248
+ throw err_2;
2174
2249
 
2175
2250
  case 4:
2176
2251
  clearTimeout(abortTimeout);
@@ -2201,9 +2276,10 @@ function () {
2201
2276
  function _hash(key, distinctId, salt) {
2202
2277
  if (salt === void 0) {
2203
2278
  salt = '';
2204
- }
2279
+ } // rusha is a fast sha1 implementation in pure javascript
2280
+
2205
2281
 
2206
- var sha1Hash = createHash('sha1');
2282
+ var sha1Hash = createHash();
2207
2283
  sha1Hash.update("".concat(key, ".").concat(distinctId).concat(salt));
2208
2284
  return parseInt(sha1Hash.digest('hex').slice(0, 15), 16) / LONG_SCALE;
2209
2285
  }