@databuddy/sdk 2.3.21 → 2.3.22

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.
@@ -4,7 +4,7 @@ import { detectClientId } from '../core/index.mjs';
4
4
  export { clear, flush, getAnonymousId, getSessionId, getTracker, getTrackingIds, getTrackingParams, isTrackerAvailable, track, trackError } from '../core/index.mjs';
5
5
  import { i as isScriptInjected, c as createScript } from '../shared/@databuddy/sdk.C8vEu9Y4.mjs';
6
6
  import React, { useRef, useMemo, useEffect, useSyncExternalStore, createContext, useContext } from 'react';
7
- import { B as BrowserFlagStorage, C as CoreFlagsManager } from '../shared/@databuddy/sdk.D0dyEsAb.mjs';
7
+ import { B as BrowserFlagStorage, C as CoreFlagsManager } from '../shared/@databuddy/sdk.DBtQ5j2L.mjs';
8
8
  import { l as logger } from '../shared/@databuddy/sdk.DCKr2Zpd.mjs';
9
9
 
10
10
  function Databuddy(props) {
@@ -1,15 +1,25 @@
1
1
  import { l as logger, c as createCacheEntry, i as isCacheValid, b as buildQueryParams, R as RequestBatcher, a as isCacheStale, D as DEFAULT_RESULT, g as getCacheKey, f as fetchAllFlags } from './sdk.DCKr2Zpd.mjs';
2
2
 
3
+ const isBrowser = typeof window !== "undefined" && typeof localStorage !== "undefined";
3
4
  class BrowserFlagStorage {
4
5
  ttl = 24 * 60 * 60 * 1e3;
5
6
  // 24 hours in milliseconds
6
7
  get(key) {
8
+ if (!isBrowser) {
9
+ return null;
10
+ }
7
11
  return this.getFromLocalStorage(key);
8
12
  }
9
13
  set(key, value) {
14
+ if (!isBrowser) {
15
+ return;
16
+ }
10
17
  this.setToLocalStorage(key, value);
11
18
  }
12
19
  getAll() {
20
+ if (!isBrowser) {
21
+ return {};
22
+ }
13
23
  const result = {};
14
24
  const now = Date.now();
15
25
  const keys = Object.keys(localStorage).filter(
@@ -33,6 +43,9 @@ class BrowserFlagStorage {
33
43
  return result;
34
44
  }
35
45
  clear() {
46
+ if (!isBrowser) {
47
+ return;
48
+ }
36
49
  const keys = Object.keys(localStorage).filter(
37
50
  (key) => key.startsWith("db-flag-")
38
51
  );
@@ -77,14 +90,23 @@ class BrowserFlagStorage {
77
90
  return Date.now() > expiresAt;
78
91
  }
79
92
  delete(key) {
93
+ if (!isBrowser) {
94
+ return;
95
+ }
80
96
  localStorage.removeItem(`db-flag-${key}`);
81
97
  }
82
98
  deleteMultiple(keys) {
99
+ if (!isBrowser) {
100
+ return;
101
+ }
83
102
  for (const key of keys) {
84
103
  localStorage.removeItem(`db-flag-${key}`);
85
104
  }
86
105
  }
87
106
  setAll(flags) {
107
+ if (!isBrowser) {
108
+ return;
109
+ }
88
110
  const currentFlags = this.getAll();
89
111
  const currentKeys = Object.keys(currentFlags);
90
112
  const newKeys = Object.keys(flags);
@@ -97,6 +119,9 @@ class BrowserFlagStorage {
97
119
  }
98
120
  }
99
121
  cleanupExpired() {
122
+ if (!isBrowser) {
123
+ return;
124
+ }
100
125
  const now = Date.now();
101
126
  const keys = Object.keys(localStorage).filter(
102
127
  (key) => key.startsWith("db-flag-")
@@ -181,6 +206,16 @@ class CoreFlagsManager {
181
206
  document.removeEventListener("visibilitychange", handleVisibility);
182
207
  };
183
208
  }
209
+ removeStaleKeys(validKeys, user) {
210
+ const ctx = user ?? this.config.user;
211
+ const suffix = ctx?.userId || ctx?.email ? `:${ctx.userId ?? ""}:${ctx.email ?? ""}` : "";
212
+ for (const key of this.cache.keys()) {
213
+ const belongsToUser = suffix ? key.endsWith(suffix) : !key.includes(":");
214
+ if (belongsToUser && !validKeys.has(key)) {
215
+ this.cache.delete(key);
216
+ }
217
+ }
218
+ }
184
219
  async initialize() {
185
220
  if (!this.config.skipStorage && this.storage) {
186
221
  this.loadFromStorage();
@@ -334,13 +369,20 @@ class CoreFlagsManager {
334
369
  }
335
370
  const apiUrl = this.config.apiUrl ?? "https://api.databuddy.cc";
336
371
  const params = buildQueryParams(this.config, user);
372
+ const ttl = this.config.cacheTtl ?? 6e4;
373
+ const staleTime = this.config.staleTime ?? ttl / 2;
337
374
  try {
338
375
  const flags = await fetchAllFlags(apiUrl, params);
339
- const ttl = this.config.cacheTtl ?? 6e4;
340
- const staleTime = this.config.staleTime ?? ttl / 2;
341
- for (const [key, result] of Object.entries(flags)) {
342
- const cacheKey = getCacheKey(key, user ?? this.config.user);
343
- this.cache.set(cacheKey, createCacheEntry(result, ttl, staleTime));
376
+ const flagCacheEntries = Object.entries(flags).map(([key, result]) => ({
377
+ cacheKey: getCacheKey(key, user ?? this.config.user),
378
+ cacheEntry: createCacheEntry(result, ttl, staleTime)
379
+ }));
380
+ this.removeStaleKeys(
381
+ new Set(flagCacheEntries.map(({ cacheKey }) => cacheKey)),
382
+ user
383
+ );
384
+ for (const { cacheKey, cacheEntry } of flagCacheEntries) {
385
+ this.cache.set(cacheKey, cacheEntry);
344
386
  }
345
387
  this.ready = true;
346
388
  this.notifyUpdate();
@@ -1,6 +1,6 @@
1
1
  import { defineComponent, ref, onMounted, onUnmounted, watch, reactive, watchEffect, computed } from 'vue';
2
2
  import { i as isScriptInjected, c as createScript } from '../shared/@databuddy/sdk.C8vEu9Y4.mjs';
3
- import { B as BrowserFlagStorage, C as CoreFlagsManager } from '../shared/@databuddy/sdk.D0dyEsAb.mjs';
3
+ import { B as BrowserFlagStorage, C as CoreFlagsManager } from '../shared/@databuddy/sdk.DBtQ5j2L.mjs';
4
4
  import '../shared/@databuddy/sdk.DCKr2Zpd.mjs';
5
5
 
6
6
  const Databuddy = defineComponent({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@databuddy/sdk",
3
- "version": "2.3.21",
3
+ "version": "2.3.22",
4
4
  "description": "Official Databuddy Analytics SDK",
5
5
  "main": "./dist/core/index.mjs",
6
6
  "types": "./dist/core/index.d.ts",