@flopflip/cache 14.0.0 → 14.0.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.
@@ -1,6 +1,7 @@
1
1
  import { type TAdapter, type TAdapterIdentifiers, type TCacheIdentifiers, type TFlags } from '@flopflip/types';
2
2
  declare function getCachePrefix(adapterIdentifiers: TAdapterIdentifiers): string;
3
- declare function getCache(cacheIdentifier: TCacheIdentifiers, adapterIdentifiers: TAdapterIdentifiers, cacheKey?: string): Promise<{
3
+ export declare function encodeCacheContext(cacheContext: any): string;
4
+ declare function getCache(cacheIdentifier: TCacheIdentifiers, adapterIdentifiers: TAdapterIdentifiers, cacheContext?: any): Promise<{
4
5
  set(flags: TFlags): any;
5
6
  get(): any;
6
7
  unset(): any;
@@ -1,3 +1,3 @@
1
1
  declare const version = "__@FLOPFLIP/VERSION_OF_RELEASE__";
2
2
  export { version };
3
- export { getAllCachedFlags, getCache, getCachedFlags } from "./cache.js";
3
+ export { encodeCacheContext, getAllCachedFlags, getCache, getCachedFlags, } from "./cache.js";
@@ -1,3 +1,3 @@
1
1
  declare const version = "__@FLOPFLIP/VERSION_OF_RELEASE__";
2
- export { getAllCachedFlags, getCache, getCachedFlags } from "./cache/index.js";
2
+ export { encodeCacheContext, getAllCachedFlags, getCache, getCachedFlags, } from "./cache/index.js";
3
3
  export { version };
@@ -78,7 +78,15 @@ function _objectSpread2(e) {
78
78
  const FLAGS_CACHE_KEY = 'flags';
79
79
  const FLAGS_REFERENCE_CACHE_KEY = 'flags-reference';
80
80
  function getCachePrefix(adapterIdentifiers) {
81
- return "@flopflip/".concat(adapterIdentifiers, "-adapter");
81
+ return `@flopflip/${adapterIdentifiers}-adapter`;
82
+ }
83
+ function encodeCacheContext(cacheContext) {
84
+ const encodedAsJson = JSON.stringify(cacheContext);
85
+ const hashCode = [...encodedAsJson].reduce(
86
+ // eslint-disable-next-line no-bitwise
87
+ (hash, c) => Math.imul(31, hash) + c.charCodeAt(0) | 0, 0);
88
+ const encodedCacheContext = Math.abs(hashCode).toString();
89
+ return encodedCacheContext;
82
90
  }
83
91
  async function importCache(cacheIdentifier) {
84
92
  let cacheModule;
@@ -96,11 +104,15 @@ async function importCache(cacheIdentifier) {
96
104
  }
97
105
  return cacheModule;
98
106
  }
99
- async function getCache(cacheIdentifier, adapterIdentifiers, cacheKey) {
107
+ async function getCache(cacheIdentifier, adapterIdentifiers, cacheContext) {
100
108
  const cacheModule = await importCache(cacheIdentifier);
101
109
  const CACHE_PREFIX = getCachePrefix(adapterIdentifiers);
102
110
  const createCache = cacheModule.default;
103
- const flagsCachePrefix = [CACHE_PREFIX, cacheKey].filter(Boolean).join('/');
111
+ let encodedCacheContext = '';
112
+ try {
113
+ encodedCacheContext = encodeCacheContext(cacheContext);
114
+ } catch (error) {}
115
+ const flagsCachePrefix = [CACHE_PREFIX, encodedCacheContext].filter(Boolean).join('/');
104
116
  const flagsCache = createCache({
105
117
  prefix: flagsCachePrefix
106
118
  });
@@ -137,7 +149,7 @@ function getCachedFlags(cacheIdentifier, adapterIdentifiers) {
137
149
  return JSON.parse(cachedFlags);
138
150
  }
139
151
  } catch (error) {
140
- console.warn("@flopflip/cache: Failed to parse cached flags from ".concat(cacheIdentifier, "."));
152
+ console.warn(`@flopflip/cache: Failed to parse cached flags from ${cacheIdentifier}.`);
141
153
  }
142
154
  }
143
155
  return {};
@@ -152,8 +164,9 @@ function getAllCachedFlags(adapter, cacheIdentifier) {
152
164
  return getCachedFlags(cacheIdentifier, adapter.id);
153
165
  }
154
166
 
155
- const version = "14.0.0";
167
+ const version = "14.0.2";
156
168
 
169
+ exports.encodeCacheContext = encodeCacheContext;
157
170
  exports.getAllCachedFlags = getAllCachedFlags;
158
171
  exports.getCache = getCache;
159
172
  exports.getCachedFlags = getCachedFlags;
@@ -78,7 +78,15 @@ function _objectSpread2(e) {
78
78
  const FLAGS_CACHE_KEY = 'flags';
79
79
  const FLAGS_REFERENCE_CACHE_KEY = 'flags-reference';
80
80
  function getCachePrefix(adapterIdentifiers) {
81
- return "@flopflip/".concat(adapterIdentifiers, "-adapter");
81
+ return `@flopflip/${adapterIdentifiers}-adapter`;
82
+ }
83
+ function encodeCacheContext(cacheContext) {
84
+ const encodedAsJson = JSON.stringify(cacheContext);
85
+ const hashCode = [...encodedAsJson].reduce(
86
+ // eslint-disable-next-line no-bitwise
87
+ (hash, c) => Math.imul(31, hash) + c.charCodeAt(0) | 0, 0);
88
+ const encodedCacheContext = Math.abs(hashCode).toString();
89
+ return encodedCacheContext;
82
90
  }
83
91
  async function importCache(cacheIdentifier) {
84
92
  let cacheModule;
@@ -96,11 +104,15 @@ async function importCache(cacheIdentifier) {
96
104
  }
97
105
  return cacheModule;
98
106
  }
99
- async function getCache(cacheIdentifier, adapterIdentifiers, cacheKey) {
107
+ async function getCache(cacheIdentifier, adapterIdentifiers, cacheContext) {
100
108
  const cacheModule = await importCache(cacheIdentifier);
101
109
  const CACHE_PREFIX = getCachePrefix(adapterIdentifiers);
102
110
  const createCache = cacheModule.default;
103
- const flagsCachePrefix = [CACHE_PREFIX, cacheKey].filter(Boolean).join('/');
111
+ let encodedCacheContext = '';
112
+ try {
113
+ encodedCacheContext = encodeCacheContext(cacheContext);
114
+ } catch (error) {}
115
+ const flagsCachePrefix = [CACHE_PREFIX, encodedCacheContext].filter(Boolean).join('/');
104
116
  const flagsCache = createCache({
105
117
  prefix: flagsCachePrefix
106
118
  });
@@ -137,7 +149,7 @@ function getCachedFlags(cacheIdentifier, adapterIdentifiers) {
137
149
  return JSON.parse(cachedFlags);
138
150
  }
139
151
  } catch (error) {
140
- console.warn("@flopflip/cache: Failed to parse cached flags from ".concat(cacheIdentifier, "."));
152
+ console.warn(`@flopflip/cache: Failed to parse cached flags from ${cacheIdentifier}.`);
141
153
  }
142
154
  }
143
155
  return {};
@@ -152,8 +164,9 @@ function getAllCachedFlags(adapter, cacheIdentifier) {
152
164
  return getCachedFlags(cacheIdentifier, adapter.id);
153
165
  }
154
166
 
155
- const version = "14.0.0";
167
+ const version = "14.0.2";
156
168
 
169
+ exports.encodeCacheContext = encodeCacheContext;
157
170
  exports.getAllCachedFlags = getAllCachedFlags;
158
171
  exports.getCache = getCache;
159
172
  exports.getCachedFlags = getCachedFlags;
@@ -56,7 +56,15 @@ function _objectSpread2(e) {
56
56
  const FLAGS_CACHE_KEY = 'flags';
57
57
  const FLAGS_REFERENCE_CACHE_KEY = 'flags-reference';
58
58
  function getCachePrefix(adapterIdentifiers) {
59
- return "@flopflip/".concat(adapterIdentifiers, "-adapter");
59
+ return `@flopflip/${adapterIdentifiers}-adapter`;
60
+ }
61
+ function encodeCacheContext(cacheContext) {
62
+ const encodedAsJson = JSON.stringify(cacheContext);
63
+ const hashCode = [...encodedAsJson].reduce(
64
+ // eslint-disable-next-line no-bitwise
65
+ (hash, c) => Math.imul(31, hash) + c.charCodeAt(0) | 0, 0);
66
+ const encodedCacheContext = Math.abs(hashCode).toString();
67
+ return encodedCacheContext;
60
68
  }
61
69
  async function importCache(cacheIdentifier) {
62
70
  let cacheModule;
@@ -74,11 +82,15 @@ async function importCache(cacheIdentifier) {
74
82
  }
75
83
  return cacheModule;
76
84
  }
77
- async function getCache(cacheIdentifier, adapterIdentifiers, cacheKey) {
85
+ async function getCache(cacheIdentifier, adapterIdentifiers, cacheContext) {
78
86
  const cacheModule = await importCache(cacheIdentifier);
79
87
  const CACHE_PREFIX = getCachePrefix(adapterIdentifiers);
80
88
  const createCache = cacheModule.default;
81
- const flagsCachePrefix = [CACHE_PREFIX, cacheKey].filter(Boolean).join('/');
89
+ let encodedCacheContext = '';
90
+ try {
91
+ encodedCacheContext = encodeCacheContext(cacheContext);
92
+ } catch (error) {}
93
+ const flagsCachePrefix = [CACHE_PREFIX, encodedCacheContext].filter(Boolean).join('/');
82
94
  const flagsCache = createCache({
83
95
  prefix: flagsCachePrefix
84
96
  });
@@ -115,7 +127,7 @@ function getCachedFlags(cacheIdentifier, adapterIdentifiers) {
115
127
  return JSON.parse(cachedFlags);
116
128
  }
117
129
  } catch (error) {
118
- console.warn("@flopflip/cache: Failed to parse cached flags from ".concat(cacheIdentifier, "."));
130
+ console.warn(`@flopflip/cache: Failed to parse cached flags from ${cacheIdentifier}.`);
119
131
  }
120
132
  }
121
133
  return {};
@@ -130,6 +142,6 @@ function getAllCachedFlags(adapter, cacheIdentifier) {
130
142
  return getCachedFlags(cacheIdentifier, adapter.id);
131
143
  }
132
144
 
133
- const version = "14.0.0";
145
+ const version = "14.0.2";
134
146
 
135
- export { getAllCachedFlags, getCache, getCachedFlags, version };
147
+ export { encodeCacheContext, getAllCachedFlags, getCache, getCachedFlags, version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flopflip/cache",
3
- "version": "14.0.0",
3
+ "version": "14.0.2",
4
4
  "description": "Caching for flipflop adapters",
5
5
  "main": "dist/flopflip-cache.cjs.js",
6
6
  "module": "dist/flopflip-cache.esm.js",
@@ -29,8 +29,8 @@
29
29
  "client"
30
30
  ],
31
31
  "dependencies": {
32
- "@flopflip/localstorage-cache": "14.0.0",
33
- "@flopflip/sessionstorage-cache": "14.0.0",
34
- "@flopflip/types": "14.0.0"
32
+ "@flopflip/localstorage-cache": "14.0.2",
33
+ "@flopflip/sessionstorage-cache": "14.0.2",
34
+ "@flopflip/types": "14.0.2"
35
35
  }
36
36
  }