posthog-node 3.1.3 → 3.2.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,12 @@
1
+ # 3.2.1 - 2023-12-15
2
+
3
+ 1. Fixes issue where a background refresh of feature flags could throw an unhandled error. It now emits to be detected by `.on('error', ...)`
4
+
5
+ # 3.2.0 - 2023-12-05
6
+
7
+ 1. Fixes issues with Axios imports for non-node environments like Cloudflare workers
8
+ 2. Uses the globally defined `fetch` if available, otherwise imports and uses axios as a polyfill
9
+
1
10
  # 3.1.3 - 2023-10-27
2
11
 
3
12
  1. Updates axios dependency
package/lib/index.cjs.js CHANGED
@@ -3,11 +3,6 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var rusha = require('rusha');
6
- var axios = require('axios');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
11
6
 
12
7
  /******************************************************************************
13
8
  Copyright (c) Microsoft Corporation.
@@ -163,7 +158,7 @@ function __spreadArray(to, from, pack) {
163
158
  return to.concat(ar || Array.prototype.slice.call(from));
164
159
  }
165
160
 
166
- var version = "3.1.3";
161
+ var version = "3.2.1";
167
162
 
168
163
  var PostHogPersistedProperty;
169
164
  (function (PostHogPersistedProperty) {
@@ -1009,7 +1004,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1009
1004
  var _this = this;
1010
1005
  var _a;
1011
1006
  if (this.optedOut) {
1012
- this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
1007
+ this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
1013
1008
  return;
1014
1009
  }
1015
1010
  var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime() });
@@ -1380,13 +1375,20 @@ var PostHogCoreStateless = /** @class */ (function () {
1380
1375
  /***
1381
1376
  * PROPERTIES
1382
1377
  ***/
1383
- PostHogCore.prototype.personProperties = function (properties) {
1378
+ PostHogCore.prototype.setPersonPropertiesForFlags = function (properties) {
1384
1379
  // Get persisted person properties
1385
1380
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1386
1381
  this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
1387
1382
  return this;
1388
1383
  };
1389
- PostHogCore.prototype.groupProperties = function (properties) {
1384
+ PostHogCore.prototype.resetPersonPropertiesForFlags = function () {
1385
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {});
1386
+ };
1387
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
1388
+ PostHogCore.prototype.personProperties = function (properties) {
1389
+ return this.setPersonPropertiesForFlags(properties);
1390
+ };
1391
+ PostHogCore.prototype.setGroupPropertiesForFlags = function (properties) {
1390
1392
  // Get persisted group properties
1391
1393
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
1392
1394
  if (Object.keys(existingProperties).length !== 0) {
@@ -1398,6 +1400,13 @@ var PostHogCoreStateless = /** @class */ (function () {
1398
1400
  this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
1399
1401
  return this;
1400
1402
  };
1403
+ PostHogCore.prototype.resetGroupPropertiesForFlags = function () {
1404
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {});
1405
+ };
1406
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
1407
+ PostHogCore.prototype.groupProperties = function (properties) {
1408
+ return this.setGroupPropertiesForFlags(properties);
1409
+ };
1401
1410
  /***
1402
1411
  *** FEATURE FLAGS
1403
1412
  ***/
@@ -1602,57 +1611,76 @@ var PostHogMemoryStorage = /** @class */ (function () {
1602
1611
  return PostHogMemoryStorage;
1603
1612
  }());
1604
1613
 
1605
- // So that alternative implementations can be used if desired
1606
-
1607
- var fetch = function (url, options) {
1608
- return __awaiter(void 0, void 0, void 0, function () {
1609
- var res;
1610
- return __generator(this, function (_a) {
1611
- switch (_a.label) {
1612
- case 0:
1613
- return [4
1614
- /*yield*/
1615
- , axios__default["default"].request({
1616
- url: url,
1617
- headers: options.headers,
1618
- method: options.method.toLowerCase(),
1619
- data: options.body,
1620
- signal: options.signal,
1621
- // fetch only throws on network errors, not on HTTP errors
1622
- validateStatus: function () {
1623
- return true;
1624
- }
1625
- })];
1626
-
1627
- case 1:
1628
- res = _a.sent();
1629
- return [2
1630
- /*return*/
1631
- , {
1632
- status: res.status,
1633
- text: function () {
1634
- return __awaiter(void 0, void 0, void 0, function () {
1635
- return __generator(this, function (_a) {
1636
- return [2
1637
- /*return*/
1638
- , res.data];
1614
+ /**
1615
+ * Fetch wrapper
1616
+ *
1617
+ * We want to polyfill fetch when not available with axios but use it when it is.
1618
+ * NOTE: The current version of Axios has an issue when in non-node environments like Clouflare Workers.
1619
+ * This is currently solved by using the global fetch if available instead.
1620
+ * See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
1621
+ */
1622
+
1623
+ var _fetch = // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
1624
+ // @ts-ignore
1625
+ typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
1626
+
1627
+ if (!_fetch) {
1628
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
1629
+ var axios_1 = require('axios');
1630
+
1631
+ _fetch = function (url, options) {
1632
+ return __awaiter(void 0, void 0, void 0, function () {
1633
+ var res;
1634
+ return __generator(this, function (_a) {
1635
+ switch (_a.label) {
1636
+ case 0:
1637
+ return [4
1638
+ /*yield*/
1639
+ , axios_1.request({
1640
+ url: url,
1641
+ headers: options.headers,
1642
+ method: options.method.toLowerCase(),
1643
+ data: options.body,
1644
+ signal: options.signal,
1645
+ // fetch only throws on network errors, not on HTTP errors
1646
+ validateStatus: function () {
1647
+ return true;
1648
+ }
1649
+ })];
1650
+
1651
+ case 1:
1652
+ res = _a.sent();
1653
+ return [2
1654
+ /*return*/
1655
+ , {
1656
+ status: res.status,
1657
+ text: function () {
1658
+ return __awaiter(void 0, void 0, void 0, function () {
1659
+ return __generator(this, function (_a) {
1660
+ return [2
1661
+ /*return*/
1662
+ , res.data];
1663
+ });
1639
1664
  });
1640
- });
1641
- },
1642
- json: function () {
1643
- return __awaiter(void 0, void 0, void 0, function () {
1644
- return __generator(this, function (_a) {
1645
- return [2
1646
- /*return*/
1647
- , res.data];
1665
+ },
1666
+ json: function () {
1667
+ return __awaiter(void 0, void 0, void 0, function () {
1668
+ return __generator(this, function (_a) {
1669
+ return [2
1670
+ /*return*/
1671
+ , res.data];
1672
+ });
1648
1673
  });
1649
- });
1650
- }
1651
- }];
1652
- }
1674
+ }
1675
+ }];
1676
+ }
1677
+ });
1653
1678
  });
1654
- });
1655
- };
1679
+ };
1680
+ } // NOTE: We have to export this as default, even though we prefer named exports as we are relying on detecting "fetch" in the global scope
1681
+
1682
+
1683
+ var fetch$1 = _fetch;
1656
1684
 
1657
1685
  var LONG_SCALE = 0xfffffffffffffff;
1658
1686
 
@@ -1718,7 +1746,8 @@ function () {
1718
1746
  this.host = host;
1719
1747
  this.poller = undefined; // NOTE: as any is required here as the AbortSignal typing is slightly misaligned but works just fine
1720
1748
 
1721
- this.fetch = options.fetch || fetch;
1749
+ this.fetch = options.fetch || fetch$1;
1750
+ this.onError = options.onError;
1722
1751
  void this.loadFeatureFlags();
1723
1752
  }
1724
1753
 
@@ -1878,10 +1907,13 @@ function () {
1878
1907
  this.featureFlags.map(function (flag) {
1879
1908
  return __awaiter(_this, void 0, void 0, function () {
1880
1909
  var matchValue, matchPayload, e_1;
1881
- return __generator(this, function (_a) {
1882
- switch (_a.label) {
1910
+
1911
+ var _a;
1912
+
1913
+ return __generator(this, function (_b) {
1914
+ switch (_b.label) {
1883
1915
  case 0:
1884
- _a.trys.push([0, 2,, 3]);
1916
+ _b.trys.push([0, 2,, 3]);
1885
1917
 
1886
1918
  matchValue = this.computeFlagLocally(flag, distinctId, groups, personProperties, groupProperties);
1887
1919
  response[flag.key] = matchValue;
@@ -1890,7 +1922,7 @@ function () {
1890
1922
  , this.computeFeatureFlagPayloadLocally(flag.key, matchValue)];
1891
1923
 
1892
1924
  case 1:
1893
- matchPayload = _a.sent();
1925
+ matchPayload = _b.sent();
1894
1926
 
1895
1927
  if (matchPayload) {
1896
1928
  payloads[flag.key] = matchPayload;
@@ -1901,10 +1933,10 @@ function () {
1901
1933
  , 3];
1902
1934
 
1903
1935
  case 2:
1904
- e_1 = _a.sent();
1936
+ e_1 = _b.sent();
1905
1937
 
1906
1938
  if (e_1 instanceof InconclusiveMatchError) ; else if (e_1 instanceof Error) {
1907
- console.error("Error computing flag locally: ".concat(flag.key, ": ").concat(e_1));
1939
+ (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, new Error("Error computing flag locally: ".concat(flag.key, ": ").concat(e_1)));
1908
1940
  }
1909
1941
 
1910
1942
  fallbackToDecide = true;
@@ -2142,13 +2174,15 @@ function () {
2142
2174
  };
2143
2175
 
2144
2176
  FeatureFlagsPoller.prototype._loadFeatureFlags = function () {
2177
+ var _a;
2178
+
2145
2179
  return __awaiter(this, void 0, void 0, function () {
2146
2180
  var res, responseJson, err_1;
2147
2181
 
2148
2182
  var _this = this;
2149
2183
 
2150
- return __generator(this, function (_a) {
2151
- switch (_a.label) {
2184
+ return __generator(this, function (_b) {
2185
+ switch (_b.label) {
2152
2186
  case 0:
2153
2187
  if (this.poller) {
2154
2188
  clearTimeout(this.poller);
@@ -2158,17 +2192,17 @@ function () {
2158
2192
  this.poller = setTimeout(function () {
2159
2193
  return _this._loadFeatureFlags();
2160
2194
  }, this.pollingInterval);
2161
- _a.label = 1;
2195
+ _b.label = 1;
2162
2196
 
2163
2197
  case 1:
2164
- _a.trys.push([1, 4,, 5]);
2198
+ _b.trys.push([1, 4,, 5]);
2165
2199
 
2166
2200
  return [4
2167
2201
  /*yield*/
2168
2202
  , this._requestFeatureFlagDefinitions()];
2169
2203
 
2170
2204
  case 2:
2171
- res = _a.sent();
2205
+ res = _b.sent();
2172
2206
 
2173
2207
  if (res && res.status === 401) {
2174
2208
  throw new ClientError("Your personalApiKey is invalid. Are you sure you're not using your Project API key? More information: https://posthog.com/docs/api/overview");
@@ -2187,7 +2221,7 @@ function () {
2187
2221
  , res.json()];
2188
2222
 
2189
2223
  case 3:
2190
- responseJson = _a.sent();
2224
+ responseJson = _b.sent();
2191
2225
 
2192
2226
  if (!('flags' in responseJson)) {
2193
2227
  console.error("Invalid response when getting feature flags: ".concat(JSON.stringify(responseJson)));
@@ -2205,11 +2239,11 @@ function () {
2205
2239
  , 5];
2206
2240
 
2207
2241
  case 4:
2208
- err_1 = _a.sent(); // if an error that is not an instance of ClientError is thrown
2242
+ err_1 = _b.sent(); // if an error that is not an instance of ClientError is thrown
2209
2243
  // we silently ignore the error when reloading feature flags
2210
2244
 
2211
2245
  if (err_1 instanceof ClientError) {
2212
- throw err_1;
2246
+ (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, err_1);
2213
2247
  }
2214
2248
 
2215
2249
  return [3
@@ -2534,7 +2568,10 @@ function (_super) {
2534
2568
  projectApiKey: apiKey,
2535
2569
  timeout: (_a = options.requestTimeout) !== null && _a !== void 0 ? _a : 10000,
2536
2570
  host: _this.host,
2537
- fetch: options.fetch
2571
+ fetch: options.fetch,
2572
+ onError: function (err) {
2573
+ _this._events.emit('error', err);
2574
+ }
2538
2575
  });
2539
2576
  }
2540
2577
 
@@ -2552,7 +2589,7 @@ function (_super) {
2552
2589
  };
2553
2590
 
2554
2591
  PostHog.prototype.fetch = function (url, options) {
2555
- return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
2592
+ return this.options.fetch ? this.options.fetch(url, options) : fetch$1(url, options);
2556
2593
  };
2557
2594
 
2558
2595
  PostHog.prototype.getLibraryId = function () {