posthog-js-lite 3.2.0 → 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,5 +1,11 @@
1
1
  # Next
2
2
 
3
+ # 3.2.1 - 2025-01-17
4
+
5
+ ## Fixed
6
+
7
+ 1. fix: check if window and fetch are available before using on web env
8
+
3
9
  # 3.2.0 - 2024-12-12
4
10
 
5
11
  ## Changed
package/lib/index.cjs.js CHANGED
@@ -65,6 +65,9 @@ function safeSetTimeout(fn, timeout) {
65
65
  // We unref if available to prevent Node.js hanging on exit
66
66
  t?.unref && t?.unref();
67
67
  return t;
68
+ }
69
+ function getFetch() {
70
+ return typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
68
71
  }
69
72
 
70
73
  // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
@@ -1917,15 +1920,18 @@ class PostHogCore extends PostHogCoreStateless {
1917
1920
  }
1918
1921
  }
1919
1922
 
1920
- var version = "3.2.0";
1923
+ var version = "3.2.1";
1921
1924
 
1922
1925
  function getContext(window) {
1923
1926
  let context = {};
1924
- if (window.navigator) {
1927
+ if (window?.navigator) {
1925
1928
  const userAgent = window.navigator.userAgent;
1929
+ const osValue = os(window);
1926
1930
  context = {
1927
1931
  ...context,
1928
- $os: os(window),
1932
+ ...(osValue !== undefined && {
1933
+ $os: osValue
1934
+ }),
1929
1935
  $browser: browser(userAgent, window.navigator.vendor, !!window.opera),
1930
1936
  $referrer: window.document.referrer,
1931
1937
  $referring_domain: referringDomain(window.document.referrer),
@@ -2027,6 +2033,9 @@ function browserVersion(userAgent, vendor, opera) {
2027
2033
  return parseFloat(matches[matches.length - 2]);
2028
2034
  }
2029
2035
  function os(window) {
2036
+ if (!window?.navigator) {
2037
+ return undefined;
2038
+ }
2030
2039
  const a = window.navigator.userAgent;
2031
2040
  if (/Windows/i.test(a)) {
2032
2041
  if (/Phone/.test(a) || /WPDesktop/.test(a)) {
@@ -2046,7 +2055,7 @@ function os(window) {
2046
2055
  } else if (/CrOS/.test(a)) {
2047
2056
  return 'Chrome OS';
2048
2057
  } else {
2049
- return '';
2058
+ return undefined;
2050
2059
  }
2051
2060
  }
2052
2061
  function device(userAgent) {
@@ -2150,9 +2159,6 @@ const createStorageLike = store => {
2150
2159
  };
2151
2160
  };
2152
2161
  const checkStoreIsSupported = (storage, key = '__mplssupport__') => {
2153
- if (!window) {
2154
- return false;
2155
- }
2156
2162
  try {
2157
2163
  const val = 'xyz';
2158
2164
  storage.setItem(key, val);
@@ -2224,12 +2230,15 @@ class PostHog extends PostHogCore {
2224
2230
  super(apiKey, options);
2225
2231
  // posthog-js stores options in one object on
2226
2232
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
2227
- this._storage = getStorage(options?.persistence || 'localStorage', window);
2233
+ this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow());
2228
2234
  this.setupBootstrap(options);
2229
2235
  if (options?.preloadFeatureFlags !== false) {
2230
2236
  this.reloadFeatureFlags();
2231
2237
  }
2232
2238
  }
2239
+ getWindow() {
2240
+ return typeof window !== 'undefined' ? window : undefined;
2241
+ }
2233
2242
  getPersistedProperty(key) {
2234
2243
  if (!this._storageCache) {
2235
2244
  this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
@@ -2248,7 +2257,12 @@ class PostHog extends PostHogCore {
2248
2257
  this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
2249
2258
  }
2250
2259
  fetch(url, options) {
2251
- return window.fetch(url, options);
2260
+ const fetchFn = getFetch();
2261
+ if (!fetchFn) {
2262
+ // error will be handled by the caller (fetchWithRetry)
2263
+ return Promise.reject(new Error('Fetch API is not available in this environment.'));
2264
+ }
2265
+ return fetchFn(url, options);
2252
2266
  }
2253
2267
  getLibraryId() {
2254
2268
  return 'posthog-js-lite';
@@ -2262,7 +2276,7 @@ class PostHog extends PostHogCore {
2262
2276
  getCommonEventProperties() {
2263
2277
  return {
2264
2278
  ...super.getCommonEventProperties(),
2265
- ...getContext(window)
2279
+ ...getContext(this.getWindow())
2266
2280
  };
2267
2281
  }
2268
2282
  }