diff --git a/packages/core/package.json b/packages/core/package.json index 538609d..696a2e7 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -57,6 +57,10 @@ "require": "./lib/api/query.cjs", "import": "./lib/api/query.mjs" }, + "./lib/browser-storage/config": { + "require": "./lib/browser-storage/config.cjs", + "import": "./lib/browser-storage/config.mjs" + }, "./lib/browser-storage/functions": { "require": "./lib/browser-storage/functions.cjs", "import": "./lib/browser-storage/functions.mjs" diff --git a/packages/core/src/browser-storage/config.ts b/packages/core/src/browser-storage/config.ts new file mode 100644 index 0000000..de42d24 --- /dev/null +++ b/packages/core/src/browser-storage/config.ts @@ -0,0 +1,7 @@ +// Cache version. Bump when structure changes +export const browserCacheVersion = 'iconify2'; + +// Cache keys +export const browserCachePrefix = 'iconify'; +export const browserCacheCountKey = browserCachePrefix + '-count'; +export const browserCacheVersionKey = browserCachePrefix + '-version'; diff --git a/packages/core/src/browser-storage/index.ts b/packages/core/src/browser-storage/index.ts index 62850a3..9b6a001 100644 --- a/packages/core/src/browser-storage/index.ts +++ b/packages/core/src/browser-storage/index.ts @@ -1,6 +1,12 @@ import type { IconifyJSON } from '@iconify/types'; import type { CacheIcons, LoadIconsCache } from '../cache'; import { getStorage, addIconSet } from '../storage/storage'; +import { + browserCacheCountKey, + browserCachePrefix, + browserCacheVersion, + browserCacheVersionKey, +} from './config'; interface StorageType { local: T; @@ -17,16 +23,6 @@ export interface StoredItem { data: IconifyJSON; } -// After changing configuration change it in tests/*/fake_cache.ts - -// Cache version. Bump when structure changes -const cacheVersion = 'iconify2'; - -// Cache keys -const cachePrefix = 'iconify'; -const countKey = cachePrefix + '-count'; -const versionKey = cachePrefix + '-version'; - /** * Cache expiration */ @@ -107,7 +103,7 @@ function setCount( value: number ): boolean { try { - storage.setItem(countKey, value.toString()); + storage.setItem(browserCacheCountKey, value.toString()); count[key] = value; return true; } catch (err) { @@ -122,7 +118,7 @@ function setCount( * @param storage */ function getCount(storage: typeof localStorage): number { - const count = storage.getItem(countKey); + const count = storage.getItem(browserCacheCountKey); if (count) { const total = parseInt(count); return total ? total : 0; @@ -141,7 +137,7 @@ function initCache( key: keyof StorageConfig ): void { try { - storage.setItem(versionKey, cacheVersion); + storage.setItem(browserCacheVersionKey, browserCacheVersion); } catch (err) { // } @@ -157,7 +153,7 @@ function destroyCache(storage: typeof localStorage): void { try { const total = getCount(storage); for (let i = 0; i < total; i++) { - storage.removeItem(cachePrefix + i.toString()); + storage.removeItem(browserCachePrefix + i.toString()); } } catch (err) { // @@ -185,7 +181,7 @@ export const loadCache: LoadIconsCache = (): void => { // Get one item from storage const getItem = (index: number): boolean => { - const name = cachePrefix + index.toString(); + const name = browserCachePrefix + index.toString(); const item = func.getItem(name); if (typeof item !== 'string') { @@ -226,8 +222,8 @@ export const loadCache: LoadIconsCache = (): void => { try { // Get version - const version = func.getItem(versionKey); - if (version !== cacheVersion) { + const version = func.getItem(browserCacheVersionKey); + if (version !== browserCacheVersion) { if (version) { // Version is set, but invalid - remove old entries destroyCache(func); @@ -302,7 +298,10 @@ export const storeCache: CacheIcons = ( provider, data, }; - func.setItem(cachePrefix + index.toString(), JSON.stringify(item)); + func.setItem( + browserCachePrefix + index.toString(), + JSON.stringify(item) + ); } catch (err) { return false; } diff --git a/packages/core/src/browser-storage/mock.ts b/packages/core/src/browser-storage/mock.ts index 7c8c9b1..5c550e2 100644 --- a/packages/core/src/browser-storage/mock.ts +++ b/packages/core/src/browser-storage/mock.ts @@ -8,14 +8,6 @@ export function nextPrefix(): string { return 'fake-storage-' + (prefixCounter++).toString(); } -// Cache version. Bump when structure changes -export const cacheVersion = 'iconify2'; - -// Cache keys -export const cachePrefix = 'iconify'; -export const countKey = cachePrefix + '-count'; -export const versionKey = cachePrefix + '-version'; - /** * Cache expiration */ diff --git a/packages/core/tests/cache/basic-test.ts b/packages/core/tests/cache/basic-test.ts index e3ee627..ab757f5 100644 --- a/packages/core/tests/cache/basic-test.ts +++ b/packages/core/tests/cache/basic-test.ts @@ -1,13 +1,11 @@ import { count, config, loadCache } from '../../lib/browser-storage'; import { - nextPrefix, - createCache, - reset, - cachePrefix, - cacheVersion, - versionKey, - countKey, -} from '../../lib/browser-storage/mock'; + browserCacheCountKey, + browserCachePrefix, + browserCacheVersion, + browserCacheVersionKey, +} from '../../lib/browser-storage/config'; +import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock'; describe('Testing mocked localStorage', () => { const provider = ''; @@ -77,10 +75,10 @@ describe('Testing mocked localStorage', () => { const cache = createCache(); // Add one item - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '1'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '1'); cache.setItem( - cachePrefix + '0', + browserCachePrefix + '0', JSON.stringify({ cached: Date.now(), provider, @@ -136,10 +134,10 @@ describe('Testing mocked localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '1'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '1'); cache.setItem( - cachePrefix + '0', + browserCachePrefix + '0', JSON.stringify({ cached: Date.now(), provider, diff --git a/packages/core/tests/cache/loading-test.ts b/packages/core/tests/cache/loading-test.ts index bff7449..6952d59 100644 --- a/packages/core/tests/cache/loading-test.ts +++ b/packages/core/tests/cache/loading-test.ts @@ -6,13 +6,15 @@ import { nextPrefix, createCache, reset, - cachePrefix, - cacheVersion, - versionKey, - countKey, hour, cacheExpiration, } from '../../lib/browser-storage/mock'; +import { + browserCacheCountKey, + browserCachePrefix, + browserCacheVersion, + browserCacheVersionKey, +} from '../../lib/browser-storage/config'; describe('Testing loading from localStorage', () => { const provider = ''; @@ -22,8 +24,8 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '1'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '1'); const item: StoredItem = { cached: Math.floor(Date.now() / hour), @@ -37,7 +39,7 @@ describe('Testing loading from localStorage', () => { }, }, }; - cache.setItem(cachePrefix + '0', JSON.stringify(item)); + cache.setItem(browserCachePrefix + '0', JSON.stringify(item)); // Set cache reset({ @@ -75,8 +77,8 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '1'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '1'); const item: StoredItem = { cached: Math.floor(Date.now() / hour), @@ -90,7 +92,7 @@ describe('Testing loading from localStorage', () => { }, }, }; - cache.setItem(cachePrefix + '0', JSON.stringify(item)); + cache.setItem(browserCachePrefix + '0', JSON.stringify(item)); // Set cache reset({ @@ -132,8 +134,8 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '1'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '1'); const item: StoredItem = { // Expiration date @@ -148,7 +150,7 @@ describe('Testing loading from localStorage', () => { }, }, }; - cache.setItem(cachePrefix + '0', JSON.stringify(item)); + cache.setItem(browserCachePrefix + '0', JSON.stringify(item)); // Set cache reset({ @@ -185,10 +187,10 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '1'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '1'); cache.setItem( - cachePrefix + '0', + browserCachePrefix + '0', JSON.stringify({ cached: Math.floor(Date.now() / hour), provider, @@ -239,8 +241,8 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '0'); // Should be at least "1" + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '0'); // Should be at least "1" const item: StoredItem = { cached: Math.floor(Date.now() / hour), @@ -254,7 +256,7 @@ describe('Testing loading from localStorage', () => { }, }, }; - cache.setItem(cachePrefix + '0', JSON.stringify(item)); + cache.setItem(browserCachePrefix + '0', JSON.stringify(item)); // Set cache reset({ @@ -291,8 +293,8 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add one icon set - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '5'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '5'); const item: StoredItem = { cached: Math.floor(Date.now() / hour), @@ -306,7 +308,7 @@ describe('Testing loading from localStorage', () => { }, }, }; - cache.setItem(cachePrefix + '0', JSON.stringify(item)); + cache.setItem(browserCachePrefix + '0', JSON.stringify(item)); // Set cache reset({ @@ -343,8 +345,8 @@ describe('Testing loading from localStorage', () => { const cache = createCache(); // Add two icon sets - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '5'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '5'); // Missing: 0, 2, 3 const item1: StoredItem = { @@ -372,8 +374,8 @@ describe('Testing loading from localStorage', () => { }, }; - cache.setItem(cachePrefix + '1', JSON.stringify(item1)); - cache.setItem(cachePrefix + '4', JSON.stringify(item4)); + cache.setItem(browserCachePrefix + '1', JSON.stringify(item1)); + cache.setItem(browserCachePrefix + '4', JSON.stringify(item4)); // Set cache reset({ @@ -413,11 +415,11 @@ describe('Testing loading from localStorage', () => { const cache2 = createCache(); // Add few icon sets - cache1.setItem(versionKey, cacheVersion); - cache2.setItem(versionKey, cacheVersion); + cache1.setItem(browserCacheVersionKey, browserCacheVersion); + cache2.setItem(browserCacheVersionKey, browserCacheVersion); - cache1.setItem(countKey, '6'); - cache2.setItem(countKey, '3'); + cache1.setItem(browserCacheCountKey, '6'); + cache2.setItem(browserCacheCountKey, '3'); // Create 5 items const icons: IconifyJSON[] = []; @@ -444,7 +446,7 @@ describe('Testing loading from localStorage', () => { // Add items 1,3,5 to localStorage [1, 3, 5].forEach((index) => { cache1.setItem( - cachePrefix + index.toString(), + browserCachePrefix + index.toString(), JSON.stringify(items[index]) ); }); @@ -452,7 +454,7 @@ describe('Testing loading from localStorage', () => { // Add items 0 and 2 to sessionStorage [0, 2].forEach((index) => { cache2.setItem( - cachePrefix + index.toString(), + browserCachePrefix + index.toString(), JSON.stringify(items[index]) ); }); diff --git a/packages/core/tests/cache/saving-test.ts b/packages/core/tests/cache/saving-test.ts index abff911..fcdef12 100644 --- a/packages/core/tests/cache/saving-test.ts +++ b/packages/core/tests/cache/saving-test.ts @@ -12,13 +12,15 @@ import { nextPrefix, createCache, reset, - cachePrefix, - cacheVersion, - versionKey, - countKey, hour, cacheExpiration, } from '../../lib/browser-storage/mock'; +import { + browserCacheCountKey, + browserCachePrefix, + browserCacheVersion, + browserCacheVersionKey, +} from '../../lib/browser-storage/config'; describe('Testing saving to localStorage', () => { const provider = ''; @@ -73,9 +75,11 @@ describe('Testing saving to localStorage', () => { }); // Check cache - expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item)); - expect(cache.getItem(countKey)).toBe('1'); - expect(cache.getItem(versionKey)).toBe(cacheVersion); + expect(cache.getItem(browserCachePrefix + '0')).toBe( + JSON.stringify(item) + ); + expect(cache.getItem(browserCacheCountKey)).toBe('1'); + expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion); }); it('Multiple icon sets', () => { @@ -135,10 +139,14 @@ describe('Testing saving to localStorage', () => { }); // Check cache - expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item0)); - expect(cache.getItem(cachePrefix + '1')).toBe(JSON.stringify(item1)); - expect(cache.getItem(countKey)).toBe('2'); - expect(cache.getItem(versionKey)).toBe(cacheVersion); + expect(cache.getItem(browserCachePrefix + '0')).toBe( + JSON.stringify(item0) + ); + expect(cache.getItem(browserCachePrefix + '1')).toBe( + JSON.stringify(item1) + ); + expect(cache.getItem(browserCacheCountKey)).toBe('2'); + expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion); }); it('Adding icon set on unused spot', () => { @@ -174,9 +182,9 @@ describe('Testing saving to localStorage', () => { }; // Add item - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '2'); - cache.setItem(cachePrefix + '1', JSON.stringify(item1)); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '2'); + cache.setItem(browserCachePrefix + '1', JSON.stringify(item1)); // Set cache reset({ @@ -214,10 +222,14 @@ describe('Testing saving to localStorage', () => { }); // Check cache - expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item0)); - expect(cache.getItem(cachePrefix + '1')).toBe(JSON.stringify(item1)); - expect(cache.getItem(countKey)).toBe('2'); - expect(cache.getItem(versionKey)).toBe(cacheVersion); + expect(cache.getItem(browserCachePrefix + '0')).toBe( + JSON.stringify(item0) + ); + expect(cache.getItem(browserCachePrefix + '1')).toBe( + JSON.stringify(item1) + ); + expect(cache.getItem(browserCacheCountKey)).toBe('2'); + expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion); }); it('Adding multiple icon sets to existing data', () => { @@ -257,12 +269,15 @@ describe('Testing saving to localStorage', () => { // Skip items 1, 5, 9+ if (i !== 1 && i !== 5 && i < 9) { - cache.setItem(cachePrefix + i.toString(), JSON.stringify(item)); + cache.setItem( + browserCachePrefix + i.toString(), + JSON.stringify(item) + ); } } - cache.setItem(versionKey, cacheVersion); - cache.setItem(countKey, '10'); + cache.setItem(browserCacheVersionKey, browserCacheVersion); + cache.setItem(browserCacheCountKey, '10'); // Set cache reset({ @@ -287,11 +302,11 @@ describe('Testing saving to localStorage', () => { // reverse order, 9 should not be there because it is last item session: [5, 4, 2, 1], }); - expect(cache.getItem(countKey)).toBe('9'); + expect(cache.getItem(browserCacheCountKey)).toBe('9'); // Check cached items [0, 3, 6, 7, 8].forEach((index) => { - expect(cache.getItem(cachePrefix + index.toString())).toBe( + expect(cache.getItem(browserCachePrefix + index.toString())).toBe( JSON.stringify(items[index]) ); }); @@ -299,7 +314,9 @@ describe('Testing saving to localStorage', () => { // Check expired items - should have been deleted // Also check items that weren't supposed to be added [2, 4, 1, 5, 9, 10, 11, 12, 13].forEach((index) => { - expect(cache.getItem(cachePrefix + index.toString())).toBeNull(); + expect( + cache.getItem(browserCachePrefix + index.toString()) + ).toBeNull(); }); // Add item 5 @@ -312,7 +329,7 @@ describe('Testing saving to localStorage', () => { local: [], session: [4, 2, 1], }); - expect(cache.getItem(countKey)).toBe('9'); + expect(cache.getItem(browserCacheCountKey)).toBe('9'); // Add items 4, 2, 1 const list = [4, 2, 1]; @@ -327,7 +344,7 @@ describe('Testing saving to localStorage', () => { local: [], session: list, }); - expect(cache.getItem(countKey)).toBe('9'); + expect(cache.getItem(browserCacheCountKey)).toBe('9'); }); // Add item 10 @@ -340,7 +357,7 @@ describe('Testing saving to localStorage', () => { local: [], session: [], }); - expect(cache.getItem(countKey)).toBe('10'); + expect(cache.getItem(browserCacheCountKey)).toBe('10'); // Add item 11 storeCache(provider, icons[11]); @@ -352,7 +369,7 @@ describe('Testing saving to localStorage', () => { local: [], session: [], }); - expect(cache.getItem(countKey)).toBe('11'); + expect(cache.getItem(browserCacheCountKey)).toBe('11'); }); it('Overwrite outdated data', () => { @@ -360,11 +377,11 @@ describe('Testing saving to localStorage', () => { const cache = createCache(); // Add data in old format - cache.setItem(versionKey, '1.0.6'); - cache.setItem(countKey, '3'); + cache.setItem(browserCacheVersionKey, '1.0.6'); + cache.setItem(browserCacheCountKey, '3'); for (let i = 0; i < 3; i++) { cache.setItem( - cachePrefix + i.toString(), + browserCachePrefix + i.toString(), JSON.stringify({ prefix: prefix, icons: { @@ -438,9 +455,11 @@ describe('Testing saving to localStorage', () => { }); // Check cache - expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item)); - expect(cache.getItem(countKey)).toBe('1'); - expect(cache.getItem(versionKey)).toBe(cacheVersion); + expect(cache.getItem(browserCachePrefix + '0')).toBe( + JSON.stringify(item) + ); + expect(cache.getItem(browserCacheCountKey)).toBe('1'); + expect(cache.getItem(browserCacheVersionKey)).toBe(browserCacheVersion); }); it('Using both storage options', () => { @@ -449,8 +468,8 @@ describe('Testing saving to localStorage', () => { const cache2 = createCache(); // Add icon sets to localStorage - cache1.setItem(versionKey, cacheVersion); - cache1.setItem(countKey, '3'); + cache1.setItem(browserCacheVersionKey, browserCacheVersion); + cache1.setItem(browserCacheCountKey, '3'); [0, 1, 2].forEach((index) => { const icon: IconifyJSON = { prefix: prefix, @@ -466,14 +485,14 @@ describe('Testing saving to localStorage', () => { data: icon, }; cache1.setItem( - cachePrefix + index.toString(), + browserCachePrefix + index.toString(), JSON.stringify(item) ); }); // Add icon sets to sessionStorage - cache2.setItem(versionKey, cacheVersion); - cache2.setItem(countKey, '4'); + cache2.setItem(browserCacheVersionKey, browserCacheVersion); + cache2.setItem(browserCacheCountKey, '4'); [0, 1, 2, 3].forEach((index) => { const icon: IconifyJSON = { prefix: prefix, @@ -489,7 +508,7 @@ describe('Testing saving to localStorage', () => { data: icon, }; cache2.setItem( - cachePrefix + index.toString(), + browserCachePrefix + index.toString(), JSON.stringify(item) ); }); @@ -553,7 +572,9 @@ describe('Testing saving to localStorage', () => { }); // Check cache - expect(cache1.getItem(cachePrefix + '3')).toBe(JSON.stringify(item)); + expect(cache1.getItem(browserCachePrefix + '3')).toBe( + JSON.stringify(item) + ); }); it('Using both storage options, but localStorage is read only', () => { @@ -562,8 +583,8 @@ describe('Testing saving to localStorage', () => { const cache2 = createCache(); // Add icon sets to localStorage - cache1.setItem(versionKey, cacheVersion); - cache1.setItem(countKey, '3'); + cache1.setItem(browserCacheVersionKey, browserCacheVersion); + cache1.setItem(browserCacheCountKey, '3'); [0, 1, 2].forEach((index) => { const icon: IconifyJSON = { prefix: prefix, @@ -579,14 +600,14 @@ describe('Testing saving to localStorage', () => { data: icon, }; cache1.setItem( - cachePrefix + index.toString(), + browserCachePrefix + index.toString(), JSON.stringify(item) ); }); // Add icon sets to sessionStorage - cache2.setItem(versionKey, cacheVersion); - cache2.setItem(countKey, '4'); + cache2.setItem(browserCacheVersionKey, browserCacheVersion); + cache2.setItem(browserCacheCountKey, '4'); [0, 1, 2, 3].forEach((index) => { const icon: IconifyJSON = { prefix: prefix, @@ -602,7 +623,7 @@ describe('Testing saving to localStorage', () => { data: icon, }; cache2.setItem( - cachePrefix + index.toString(), + browserCachePrefix + index.toString(), JSON.stringify(item) ); }); @@ -669,6 +690,8 @@ describe('Testing saving to localStorage', () => { }); // Check cache - expect(cache2.getItem(cachePrefix + '4')).toBe(JSON.stringify(item)); + expect(cache2.getItem(browserCachePrefix + '4')).toBe( + JSON.stringify(item) + ); }); });