limited-cache 2.2.0 → 2.3.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 +12 -0
- package/README.md +4 -3
- package/dist/cjs/index.cjs +273 -380
- package/dist/cjs/index.d.cts +84 -76
- package/dist/cjs/index.d.cts.map +1 -0
- package/dist/esm/index.d.ts +99 -6
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +279 -6
- package/dist/esm/index.js.map +1 -1
- package/package.json +34 -53
- package/src/__tests__/LimitedCache.test.ts +2 -2
- package/src/__tests__/LimitedCacheObject.test.ts +30 -27
- package/src/__tests__/lowLevelFunctions.test.ts +10 -8
- package/src/__tests__/scenarios/keys.test.ts +2 -2
- package/src/__tests__/scenarios/maxCacheSize.test.ts +8 -5
- package/src/__tests__/scenarios/maxCacheTime.test.ts +58 -58
- package/src/__tests__/scenarios/values.test.ts +2 -2
- package/src/__tests__/typeChecks/LimitedCacheObjectTypes.test.ts +15 -15
- package/src/__tests__/typeChecks/LimitedCacheTypes.test.ts +1 -1
- package/src/__tests__/typeChecks/lowLevelFunctionsTypes.test.ts +2 -2
- package/src/core/LimitedCache.ts +20 -21
- package/src/core/LimitedCacheObject.ts +30 -19
- package/src/core/defaultOptions.ts +5 -4
- package/src/core/limitedCacheUtil.ts +15 -3
- package/src/core/lowLevelFunctions.ts +43 -32
- package/src/types.ts +4 -3
- package/dist/esm/core/LimitedCache.d.ts +0 -4
- package/dist/esm/core/LimitedCache.d.ts.map +0 -1
- package/dist/esm/core/LimitedCache.js +0 -29
- package/dist/esm/core/LimitedCache.js.map +0 -1
- package/dist/esm/core/LimitedCacheObject.d.ts +0 -5
- package/dist/esm/core/LimitedCacheObject.d.ts.map +0 -1
- package/dist/esm/core/LimitedCacheObject.js +0 -52
- package/dist/esm/core/LimitedCacheObject.js.map +0 -1
- package/dist/esm/core/builtIns.d.ts +0 -12
- package/dist/esm/core/builtIns.d.ts.map +0 -1
- package/dist/esm/core/builtIns.js +0 -5
- package/dist/esm/core/builtIns.js.map +0 -1
- package/dist/esm/core/defaultOptions.d.ts +0 -6
- package/dist/esm/core/defaultOptions.d.ts.map +0 -1
- package/dist/esm/core/defaultOptions.js +0 -14
- package/dist/esm/core/defaultOptions.js.map +0 -1
- package/dist/esm/core/limitedCacheUtil.d.ts +0 -13
- package/dist/esm/core/limitedCacheUtil.d.ts.map +0 -1
- package/dist/esm/core/limitedCacheUtil.js +0 -14
- package/dist/esm/core/limitedCacheUtil.js.map +0 -1
- package/dist/esm/core/lowLevelFunctions.d.ts +0 -14
- package/dist/esm/core/lowLevelFunctions.d.ts.map +0 -1
- package/dist/esm/core/lowLevelFunctions.js +0 -240
- package/dist/esm/core/lowLevelFunctions.js.map +0 -1
- package/dist/esm/types.d.ts +0 -58
- package/dist/esm/types.d.ts.map +0 -1
- package/dist/esm/types.js +0 -2
- package/dist/esm/types.js.map +0 -1
- package/legacy-types/ts3.5/dist/esm/core/LimitedCache.d.ts +0 -4
- package/legacy-types/ts3.5/dist/esm/core/LimitedCacheObject.d.ts +0 -5
- package/legacy-types/ts3.5/dist/esm/core/builtIns.d.ts +0 -12
- package/legacy-types/ts3.5/dist/esm/core/defaultOptions.d.ts +0 -6
- package/legacy-types/ts3.5/dist/esm/core/limitedCacheUtil.d.ts +0 -13
- package/legacy-types/ts3.5/dist/esm/core/lowLevelFunctions.d.ts +0 -14
- package/legacy-types/ts3.5/dist/esm/index.d.ts +0 -7
- package/legacy-types/ts3.5/dist/esm/types.d.ts +0 -61
- package/legacy-types/ts4.0/dist/esm/core/LimitedCache.d.ts +0 -4
- package/legacy-types/ts4.0/dist/esm/core/LimitedCacheObject.d.ts +0 -5
- package/legacy-types/ts4.0/dist/esm/core/builtIns.d.ts +0 -12
- package/legacy-types/ts4.0/dist/esm/core/defaultOptions.d.ts +0 -6
- package/legacy-types/ts4.0/dist/esm/core/limitedCacheUtil.d.ts +0 -13
- package/legacy-types/ts4.0/dist/esm/core/lowLevelFunctions.d.ts +0 -14
- package/legacy-types/ts4.0/dist/esm/index.d.ts +0 -7
- package/legacy-types/ts4.0/dist/esm/types.d.ts +0 -61
- package/legacy-types/ts4.5/dist/esm/core/LimitedCache.d.ts +0 -4
- package/legacy-types/ts4.5/dist/esm/core/LimitedCacheObject.d.ts +0 -5
- package/legacy-types/ts4.5/dist/esm/core/builtIns.d.ts +0 -12
- package/legacy-types/ts4.5/dist/esm/core/defaultOptions.d.ts +0 -6
- package/legacy-types/ts4.5/dist/esm/core/limitedCacheUtil.d.ts +0 -13
- package/legacy-types/ts4.5/dist/esm/core/lowLevelFunctions.d.ts +0 -14
- package/legacy-types/ts4.5/dist/esm/index.d.ts +0 -7
- package/legacy-types/ts4.5/dist/esm/types.d.ts +0 -61
- package/src/core/builtIns.ts +0 -9
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,4BAA4B,CAAC;AAE3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/core/defaultOptions.ts","../../src/core/lowLevelFunctions.ts","../../src/core/LimitedCache.ts","../../src/core/LimitedCacheObject.ts","../../src/core/limitedCacheUtil.ts"],"sourcesContent":["import type { LimitedCacheOptionsReadonly } from '../types.js';\n\nconst CURRENT_META_VERSION = 2 as const;\n// Default = 1 year\nconst MAXIMUM_CACHE_TIME: number = 365 * 86400 * 1000;\n\nconst defaultOptions: LimitedCacheOptionsReadonly = {\n // Public\n maxCacheSize: 100,\n maxCacheTime: 86400 * 1000,\n // Development-only\n warnIfItemPurgedBeforeTime: 5000,\n // Private\n opLimit: 200,\n scanLimit: 50,\n};\n\nexport { CURRENT_META_VERSION, defaultOptions, MAXIMUM_CACHE_TIME };\n","import type {\n DefaultItemType,\n LimitedCacheMeta,\n LimitedCacheOptions,\n LimitedCacheOptionsFull,\n LimitedCacheOptionsReadonly,\n} from '../types.js';\nimport { CURRENT_META_VERSION, defaultOptions, MAXIMUM_CACHE_TIME } from './defaultOptions.js';\n\n/* Initialization and options */\n\nconst positiveNumberOrZero = (value: number): number => Math.max(value, 0) || 0;\n\nconst normalizeOptions = (cacheMetaOptions: LimitedCacheOptionsFull): LimitedCacheOptionsFull => {\n Object.assign(cacheMetaOptions, {\n maxCacheSize: positiveNumberOrZero(cacheMetaOptions.maxCacheSize),\n maxCacheTime: positiveNumberOrZero(cacheMetaOptions.maxCacheTime),\n opLimit: positiveNumberOrZero(cacheMetaOptions.opLimit),\n });\n\n // @ts-expect-error `process.env.NODE_ENV` left intact and not added to global typings\n if (process.env.NODE_ENV !== 'production') {\n cacheMetaOptions.warnIfItemPurgedBeforeTime = positiveNumberOrZero(\n cacheMetaOptions.warnIfItemPurgedBeforeTime,\n );\n }\n return cacheMetaOptions;\n};\n\nconst isCacheMeta = (cacheMeta: unknown): cacheMeta is LimitedCacheMeta => {\n // @ts-expect-error Duck-typing the unknown value\n return !!cacheMeta?.limitedCacheMetaVersion;\n};\n\nconst upgradeCacheMeta = (cacheMeta: LimitedCacheMeta): void => {\n if (!isCacheMeta(cacheMeta)) {\n throw new Error('Limited-cache metadata is missing: please check your usage');\n }\n if (cacheMeta.limitedCacheMetaVersion !== CURRENT_META_VERSION) {\n // Version is out of date! (Today the only prior version is 1)\n // Version 1: Cache meta cannot be migrated because timestamps and keys are incompatible\n // biome-ignore lint/suspicious/noConsole: Intentional warning\n console.warn('Limited-cache metadata is from an incompatible version (1). It must be reset.');\n cacheMeta.limitedCacheMetaVersion = CURRENT_META_VERSION;\n lowLevelReset(cacheMeta);\n }\n};\n\nconst lowLevelSetOptions = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n options: LimitedCacheOptions,\n): LimitedCacheOptionsReadonly => {\n upgradeCacheMeta(cacheMeta);\n return normalizeOptions(Object.assign(cacheMeta.options, options));\n};\n\nconst lowLevelInit = <ItemType = DefaultItemType>(\n optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType>,\n): LimitedCacheMeta<ItemType> => {\n if (isCacheMeta(optionsOrCacheMeta)) {\n const existingCacheMeta = optionsOrCacheMeta;\n upgradeCacheMeta(existingCacheMeta);\n return existingCacheMeta;\n }\n // Else: it's options\n const fullOptions = normalizeOptions({\n ...defaultOptions,\n ...optionsOrCacheMeta,\n });\n\n // The cacheMeta is created once, and persists per instance\n const newCacheMeta = lowLevelReset({\n limitedCacheMetaVersion: CURRENT_META_VERSION,\n options: fullOptions,\n } as LimitedCacheMeta<ItemType>);\n\n return newCacheMeta;\n};\n\n/* Internal cache manipulation */\n\nconst _getExpireTime = (cacheMeta: LimitedCacheMeta, cacheKey: string): number => {\n const {\n options: { maxCacheTime },\n keyInfo: { [cacheKey]: keyInfo },\n } = cacheMeta;\n if (!keyInfo) {\n // A missing record is always treated as expired\n return 0;\n }\n // If we have an exact expireTime then honor it. Otherwise it'll depend on the current maxCacheTime.\n const [setTime, expireTime] = keyInfo;\n return expireTime || setTime + (maxCacheTime || MAXIMUM_CACHE_TIME);\n};\n\nconst _cacheKeyHasExpired = (\n cacheMeta: LimitedCacheMeta,\n cacheKey: string,\n now: number,\n): boolean => {\n return _getExpireTime(cacheMeta, cacheKey) < now;\n};\n\nconst lowLevelDoMaintenance = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n const { cache, keyList, keyInfo } = cacheMeta;\n const now = Date.now();\n\n // Rebuild cache from keyList only, checking timestamps to auto-remove expired\n const [newCache, newKeyList, newKeyInfo] = keyList.reduce(\n (acc, cacheKey) => {\n const [accCache, accKeyList, accKeyInfo] = acc;\n if (!_cacheKeyHasExpired(cacheMeta, cacheKey, now)) {\n accCache[cacheKey] = cache[cacheKey];\n accKeyList.push(cacheKey);\n accKeyInfo[cacheKey] = keyInfo[cacheKey];\n }\n return acc;\n },\n [\n // This manual assertion is required because TypeScript doesn't know that the initial value is of the same type as the accumulator.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n {} as (typeof cacheMeta)['cache'],\n [] as (typeof cacheMeta)['keyList'],\n Object.create(null) as (typeof cacheMeta)['keyInfo'],\n ],\n );\n\n return Object.assign(cacheMeta, {\n cache: newCache,\n keyList: newKeyList,\n keyInfo: newKeyInfo,\n opsLeft: cacheMeta.options.opLimit,\n });\n};\n\nconst _removeFromIndex = (cacheMeta: LimitedCacheMeta, startIndex: number, now: number): void => {\n const { cache, keyList, keyInfo } = cacheMeta;\n\n // Always remove the item requested, and also remove any neighbors who have expired\n let nextIndex = startIndex;\n let nextCacheKey = keyList[startIndex] as string;\n const keyListLength = keyList.length;\n do {\n // Remove the 'next' item\n\n cache[nextCacheKey] = keyInfo[nextCacheKey] = undefined;\n\n // Now advance and decide whether to keep going\n nextIndex++;\n nextCacheKey = keyList[nextIndex] as string;\n } while (nextIndex < keyListLength && _cacheKeyHasExpired(cacheMeta, nextCacheKey, now));\n\n // Remove the index for everything from the startIndex until we stopped\n keyList.splice(startIndex, nextIndex - startIndex);\n};\n\nconst _removeItemsToMakeRoom = (cacheMeta: LimitedCacheMeta, now: number): void => {\n const {\n options: { scanLimit, warnIfItemPurgedBeforeTime },\n cache,\n keyList,\n keyInfo,\n } = cacheMeta;\n\n // These track the soonest-to-expire thing we've found. It may not actually be \"oldest\".\n // By default we'll remove the item at the head of the queue, unless we find something better.\n let oldestItemIndex = 0;\n let oldestExpireTime = _getExpireTime(cacheMeta, keyList[0] as string);\n\n if (oldestExpireTime > now) {\n // The head of the list hasn't yet expired: scan for a better candidate to remove\n let indexToCheck = 0;\n const maxIndexToCheck = Math.min(keyList.length, scanLimit);\n while (indexToCheck < maxIndexToCheck) {\n const cacheKeyForIndex = keyList[indexToCheck] as string;\n const expireTimeForIndex = _getExpireTime(cacheMeta, cacheKeyForIndex);\n\n // We only consider it if it's eligible for expiration: otherwise it can't be a better option\n // than the default head-of-queue\n if (expireTimeForIndex < now) {\n // We found an expired item! This wins automatically\n oldestItemIndex = indexToCheck;\n oldestExpireTime = 0;\n break;\n }\n if (expireTimeForIndex < oldestExpireTime) {\n // We have a new leader\n oldestItemIndex = indexToCheck;\n oldestExpireTime = expireTimeForIndex;\n }\n indexToCheck += 1;\n }\n }\n\n // Warn if the 'oldest' item is more recent than we'd like: this means it cycled into and out of\n // cache too quickly for the cache to be useful.\n if (\n // @ts-expect-error `process.env.NODE_ENV` left intact and not added to global typings\n process.env.NODE_ENV !== 'production' &&\n warnIfItemPurgedBeforeTime &&\n oldestExpireTime > now\n ) {\n const oldestItemKey = keyList[oldestItemIndex] as string;\n const [oldestItemSetTime, oldestItemExpireTime] = keyInfo[oldestItemKey] as [number, number];\n\n if (now - oldestItemSetTime < warnIfItemPurgedBeforeTime) {\n // biome-ignore lint/suspicious/noConsole: Dev-only code\n console.warn(\n 'Purged an item from cache while it was still fresh: you may want to increase maxCacheSize',\n {\n currentTime: now,\n key: oldestItemKey,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n item: cache[oldestItemKey],\n setTime: oldestItemSetTime,\n expireTime: oldestItemExpireTime,\n timeInCache: now - oldestItemSetTime,\n },\n );\n }\n }\n\n // Remove the oldest item we found, plus any expired neighbors\n _removeFromIndex(cacheMeta, oldestItemIndex, now);\n};\n\n/* Accessors */\n\nconst lowLevelHas = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n): boolean => {\n upgradeCacheMeta(cacheMeta);\n const { cache } = cacheMeta;\n // biome-ignore lint/suspicious/noPrototypeBuiltins: Keeping the legacy hasOwnProperty to avoid a breaking change\n if (Object.prototype.hasOwnProperty.call(cache, cacheKey) && cache[cacheKey] !== undefined) {\n if (!_cacheKeyHasExpired(cacheMeta, cacheKey, Date.now())) {\n return true;\n }\n // If it's expired, clear the value so that we can short-circuit future lookups\n cache[cacheKey] = undefined;\n }\n return false;\n};\n\nconst lowLevelGetOne = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n): ItemType | undefined => {\n upgradeCacheMeta(cacheMeta);\n if (lowLevelHas(cacheMeta, cacheKey)) {\n return cacheMeta.cache[cacheKey];\n }\n return;\n};\n\nconst lowLevelGetAll = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): Record<string, ItemType> => {\n upgradeCacheMeta(cacheMeta);\n // Remove all expired values, and return whatever's left\n lowLevelDoMaintenance(cacheMeta);\n // Retype because there won't be any `undefined` values after doMaintenance\n return cacheMeta.cache as Record<string, ItemType>;\n};\n\nconst lowLevelSet = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n item: ItemType,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n\n const {\n options: { maxCacheSize },\n keyList,\n keyInfo,\n } = cacheMeta;\n\n const now = Date.now();\n const isNew = !keyInfo[cacheKey];\n\n if (cacheMeta.cache[cacheKey] !== item) {\n // The cache itself is immutable (but the rest of cacheMeta is not)\n cacheMeta.cache = {\n ...cacheMeta.cache,\n [cacheKey]: item,\n };\n }\n // We've now set or updated it. Regardless of whether it's new, bump its set time\n // @TODO: expireTime override\n keyInfo[cacheKey] = [now, 0];\n\n if (isNew) {\n // It's a new key: grow the cache, then shrink it if we can\n keyList.push(cacheKey);\n\n cacheMeta.opsLeft--;\n if (cacheMeta.opsLeft <= 0) {\n // Time for an oil change\n lowLevelDoMaintenance(cacheMeta);\n }\n\n if (maxCacheSize && cacheMeta.keyList.length > maxCacheSize) {\n // We're still over the limit: drop at least one item\n _removeItemsToMakeRoom(cacheMeta, now);\n }\n }\n\n if (_cacheKeyHasExpired(cacheMeta, keyList[0] as string, now)) {\n // While we're here, if we need to expire the head of the queue then drop it\n _removeFromIndex(cacheMeta, 0, now);\n }\n\n return cacheMeta;\n};\n\nconst lowLevelRemove = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n cacheKey: string,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n const { cache, keyInfo } = cacheMeta;\n\n if (keyInfo[cacheKey]) {\n if (cache[cacheKey] !== undefined) {\n cacheMeta.cache = {\n ...cache,\n [cacheKey]: undefined,\n };\n }\n keyInfo[cacheKey] = undefined;\n }\n\n return cacheMeta;\n};\n\nconst lowLevelReset = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): LimitedCacheMeta<ItemType> => {\n upgradeCacheMeta(cacheMeta);\n return Object.assign(cacheMeta, {\n cache: {},\n keyList: [],\n keyInfo: Object.create(null) as Record<string, ItemType>,\n opsLeft: cacheMeta.options.opLimit,\n });\n};\n\nexport {\n isCacheMeta,\n lowLevelDoMaintenance,\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelReset,\n lowLevelSet,\n lowLevelSetOptions,\n upgradeCacheMeta,\n};\n","import type {\n DefaultItemType,\n LimitedCacheInstance,\n LimitedCacheMeta,\n LimitedCacheOptions,\n LimitedCacheOptionsReadonly,\n} from '../types.js';\nimport {\n lowLevelDoMaintenance,\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelReset,\n lowLevelSet,\n lowLevelSetOptions,\n} from './lowLevelFunctions.js';\n\n// Most public functions just call a low-level function directly, passing the cacheMeta.\n// Doing this via a helper function makes the typeChecks easier, and minifies better.\nconst bindFunctionToCacheMeta = <ItemType, OtherArgs extends unknown[], ReturnValue>(\n fn: (cacheMeta: LimitedCacheMeta<ItemType>, ...otherArgs: OtherArgs) => ReturnValue,\n cacheMeta: LimitedCacheMeta<ItemType>,\n): ((...otherArgs: OtherArgs) => ReturnValue) => fn.bind(null, cacheMeta);\n\nconst LimitedCache = <ItemType = DefaultItemType>(\n options?: LimitedCacheOptions,\n): LimitedCacheInstance<ItemType> => {\n const cacheMeta = lowLevelInit<ItemType>(options);\n\n return {\n get: bindFunctionToCacheMeta(lowLevelGetOne, cacheMeta),\n getAll: bindFunctionToCacheMeta(lowLevelGetAll, cacheMeta),\n has: bindFunctionToCacheMeta(lowLevelHas, cacheMeta),\n set: (cacheKey, item): ItemType => {\n lowLevelSet(cacheMeta, cacheKey, item);\n return item;\n },\n remove: (cacheKey): true => {\n lowLevelRemove(cacheMeta, cacheKey);\n return true;\n },\n reset: bindFunctionToCacheMeta(lowLevelReset, cacheMeta),\n getCacheMeta: (): LimitedCacheMeta<ItemType> => cacheMeta,\n getOptions: (): LimitedCacheOptionsReadonly => cacheMeta.options,\n setOptions: bindFunctionToCacheMeta(lowLevelSetOptions, cacheMeta),\n doMaintenance: bindFunctionToCacheMeta(lowLevelDoMaintenance, cacheMeta),\n };\n};\n\nexport { LimitedCache };\n","import type {\n DefaultItemType,\n LimitedCacheMeta,\n LimitedCacheObjectInstance,\n LimitedCacheOptions,\n} from '../types.js';\nimport {\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelSet,\n} from './lowLevelFunctions.js';\n\nconst proxyHandler: ProxyHandler<LimitedCacheMeta> = {\n get: (cacheMeta: LimitedCacheMeta, cacheKey: string) => {\n if (cacheKey === 'hasOwnProperty') {\n return Object.prototype.hasOwnProperty;\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return lowLevelGetOne(cacheMeta, cacheKey);\n },\n getOwnPropertyDescriptor: (cacheMeta: LimitedCacheMeta, cacheKey: string) => {\n const hasResult = lowLevelHas(cacheMeta, cacheKey);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n const getResult = lowLevelGetOne(cacheMeta, cacheKey);\n\n if (hasResult) {\n return {\n configurable: true,\n enumerable: hasResult,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n value: getResult,\n writable: true,\n };\n }\n return;\n },\n has: lowLevelHas,\n set: <T>(cacheMeta: LimitedCacheMeta, cacheKey: string, item: T): T => {\n lowLevelSet(cacheMeta, cacheKey, item);\n return item;\n },\n deleteProperty: (cacheMeta: LimitedCacheMeta, cacheKey: string): true => {\n lowLevelRemove(cacheMeta, cacheKey);\n return true;\n },\n ownKeys: (cacheMeta: LimitedCacheMeta) => Object.keys(lowLevelGetAll(cacheMeta)),\n};\n\n/**\n * TypeScript's Proxy type models the runtime target, but LimitedCacheObject intentionally returns\n * a facade with a different surface from the internal cache metadata target.\n */\nconst internal_createLimitedCacheObjectProxy = <ItemType = DefaultItemType>(\n cacheMeta: LimitedCacheMeta<ItemType>,\n): LimitedCacheObjectInstance<ItemType> => {\n return new Proxy(cacheMeta, proxyHandler) as unknown as LimitedCacheObjectInstance<ItemType>;\n};\n\n/**\n * So that we can retrieve the cacheMeta for a LimitedCacheObject, without polluting its properties, each proxy\n * is associated back to its internal cacheMeta here.\n */\nconst cacheMetasForProxies = new WeakMap();\n\nconst LimitedCacheObject = <ItemType = DefaultItemType>(\n options?: LimitedCacheOptions,\n): LimitedCacheObjectInstance<ItemType> => {\n const cacheMeta = lowLevelInit<ItemType>(options);\n const limitedCacheObject = internal_createLimitedCacheObjectProxy(cacheMeta);\n\n cacheMetasForProxies.set(limitedCacheObject, cacheMeta);\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return limitedCacheObject;\n};\n\nconst getCacheMetaFromObject = (instance: LimitedCacheObjectInstance): LimitedCacheMeta => {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return cacheMetasForProxies.get(instance);\n};\n\nexport { getCacheMetaFromObject, LimitedCacheObject };\n","import {\n lowLevelDoMaintenance,\n lowLevelGetAll,\n lowLevelGetOne,\n lowLevelHas,\n lowLevelInit,\n lowLevelRemove,\n lowLevelReset,\n lowLevelSet,\n lowLevelSetOptions,\n} from './lowLevelFunctions.js';\n\ntype LimitedCacheUtil = {\n init: typeof lowLevelInit;\n get: typeof lowLevelGetOne;\n getAll: typeof lowLevelGetAll;\n has: typeof lowLevelHas;\n set: typeof lowLevelSet;\n remove: typeof lowLevelRemove;\n reset: typeof lowLevelReset;\n doMaintenance: typeof lowLevelDoMaintenance;\n setOptions: typeof lowLevelSetOptions;\n};\n\nconst limitedCacheUtil: LimitedCacheUtil = {\n init: lowLevelInit,\n get: lowLevelGetOne,\n getAll: lowLevelGetAll,\n has: lowLevelHas,\n set: lowLevelSet,\n remove: lowLevelRemove,\n reset: lowLevelReset,\n doMaintenance: lowLevelDoMaintenance,\n setOptions: lowLevelSetOptions,\n};\n\nexport { limitedCacheUtil };\n"],"mappings":";AAEA,MAAM,uBAAuB;AAE7B,MAAM,qBAA6B,MAAM,QAAQ;AAEjD,MAAM,iBAA8C;CAElD,cAAc;CACd,cAAc,QAAQ;CAEtB,4BAA4B;CAE5B,SAAS;CACT,WAAW;AACb;;;ACJA,MAAM,wBAAwB,UAA0B,KAAK,IAAI,OAAO,CAAC,KAAK;AAE9E,MAAM,oBAAoB,qBAAuE;CAC/F,OAAO,OAAO,kBAAkB;EAC9B,cAAc,qBAAqB,iBAAiB,YAAY;EAChE,cAAc,qBAAqB,iBAAiB,YAAY;EAChE,SAAS,qBAAqB,iBAAiB,OAAO;CACxD,CAAC;CAGD,IAAI,QAAQ,IAAI,aAAa,cAC3B,iBAAiB,6BAA6B,qBAC5C,iBAAiB,0BACnB;CAEF,OAAO;AACT;AAEA,MAAM,eAAe,cAAsD;CAEzE,OAAO,CAAC,CAAC,WAAW;AACtB;AAEA,MAAM,oBAAoB,cAAsC;CAC9D,IAAI,CAAC,YAAY,SAAS,GACxB,MAAM,IAAI,MAAM,4DAA4D;CAE9E,IAAI,UAAU,4BAAA,GAAkD;EAI9D,QAAQ,KAAK,+EAA+E;EAC5F,UAAU,0BAAA;EACV,cAAc,SAAS;CACzB;AAC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AAI,QAAQ;AAEzC,MAAM,sBACJ,YACyC;CACzC,MAAM,YAAY,aAAuB,OAAO;CAChD,MAAM,qBAAqB,uCAAuC,SAAS;CAE3E,qBAAqB,IAAI,oBAAoB,SAAS;CAEtD,OAAO;AACT;AAEA,MAAM,0BAA0B,aAA2D;CAEzF,OAAO,qBAAqB,IAAI,QAAQ;AAC1C;;;ACzDA,MAAM,mBAAqC;CACzC,MAAM;CACN,KAAK;CACL,QAAQ;CACR,KAAK;CACL,KAAK;CACL,QAAQ;CACR,OAAO;CACP,eAAe;CACf,YAAY;AACd"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "limited-cache",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "A minimal JS cache: like using an object, except it won't grow forever",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"limited cache",
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"bugs": "https://github.com/spautz/limited-cache/issues",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/spautz/limited-cache.git",
|
|
21
|
+
"url": "git+https://github.com/spautz/limited-cache.git",
|
|
22
22
|
"directory": "packages/limited-cache"
|
|
23
23
|
},
|
|
24
24
|
"author": {
|
|
25
25
|
"name": "Steven Pautz",
|
|
26
|
-
"url": "https://
|
|
26
|
+
"url": "https://stevenpautz.com"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public",
|
|
@@ -33,7 +33,6 @@
|
|
|
33
33
|
"files": [
|
|
34
34
|
"dist/",
|
|
35
35
|
"docs/",
|
|
36
|
-
"legacy-types/",
|
|
37
36
|
"src/",
|
|
38
37
|
"LICENSE",
|
|
39
38
|
"*.md"
|
|
@@ -42,6 +41,7 @@
|
|
|
42
41
|
"type": "module",
|
|
43
42
|
"exports": {
|
|
44
43
|
".": {
|
|
44
|
+
"local-dev": "./src/index.ts",
|
|
45
45
|
"import": {
|
|
46
46
|
"types": "./dist/esm/index.d.ts",
|
|
47
47
|
"default": "./dist/esm/index.js"
|
|
@@ -51,16 +51,22 @@
|
|
|
51
51
|
"default": "./dist/cjs/index.cjs"
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
|
-
"./internal-raw-src": "./src/index.ts",
|
|
55
54
|
"./package.json": "./package.json"
|
|
56
55
|
},
|
|
57
56
|
"main": "./dist/cjs/index.cjs",
|
|
58
57
|
"module": "./dist/esm/index.js",
|
|
59
|
-
"jsnext:main": "./dist/esm/index.js",
|
|
60
58
|
"types": "./dist/esm/index.d.ts",
|
|
61
59
|
"sideEffects": false,
|
|
62
60
|
"dependencies": {},
|
|
63
|
-
"devDependencies": {
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@arethetypeswrong/cli": "0.18.3",
|
|
63
|
+
"@size-limit/esbuild": "12.1.0",
|
|
64
|
+
"@size-limit/esbuild-why": "12.1.0",
|
|
65
|
+
"@size-limit/file": "12.1.0",
|
|
66
|
+
"publint": "0.3.21",
|
|
67
|
+
"size-limit": "12.1.0",
|
|
68
|
+
"tsdown": "0.22.3"
|
|
69
|
+
},
|
|
64
70
|
"peerDependencies": {},
|
|
65
71
|
"size-limit": [
|
|
66
72
|
{
|
|
@@ -74,54 +80,29 @@
|
|
|
74
80
|
"limit": "1.2 kb"
|
|
75
81
|
}
|
|
76
82
|
],
|
|
77
|
-
"typesVersions": {
|
|
78
|
-
"<4.0": {
|
|
79
|
-
"*": [
|
|
80
|
-
"./legacy-types/ts3.5/*"
|
|
81
|
-
]
|
|
82
|
-
},
|
|
83
|
-
"<4.5": {
|
|
84
|
-
"*": [
|
|
85
|
-
"./legacy-types/ts4.0/*"
|
|
86
|
-
]
|
|
87
|
-
},
|
|
88
|
-
"<4.7": {
|
|
89
|
-
"*": [
|
|
90
|
-
"./legacy-types/ts4.5/*"
|
|
91
|
-
]
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
83
|
"scripts": {
|
|
95
84
|
"____ HOOKS _________________________________________________________": "",
|
|
96
|
-
"____
|
|
97
|
-
"
|
|
98
|
-
"all": "
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
105
|
-
"build:cjs": "tsup src/index.ts --format cjs --dts --outDir=./dist/cjs/ --tsconfig=./tsconfig.build-cjs.json",
|
|
106
|
-
"build:cjs:watch": "pnpm run build:cjs --watch",
|
|
107
|
-
"build:esm": "tsc -p tsconfig.build-esm.json",
|
|
108
|
-
"build:esm:watch": "pnpm run build:esm --watch",
|
|
109
|
-
"build:legacytypes": "pnpm run build:legacytypes:3.5 && pnpm run build:legacytypes:4.0 && pnpm run build:legacytypes:4.5",
|
|
110
|
-
"build:legacytypes:3.5": "downlevel-dts ./dist ./legacy-types/ts3.5/dist --to=3.5",
|
|
111
|
-
"build:legacytypes:4.0": "downlevel-dts ./dist ./legacy-types/ts4.0/dist --to=4.0",
|
|
112
|
-
"build:legacytypes:4.5": "downlevel-dts ./dist ./legacy-types/ts4.5/dist --to=4.5",
|
|
113
|
-
"build:verify": "publint --strict && size-limit",
|
|
114
|
-
"build:verifypack": "ls *.tgz | xargs -I {} attw {} --exclude-entrypoints internal-raw-src",
|
|
115
|
-
"lint": "eslint . --max-warnings 0",
|
|
116
|
-
"lint:fix": "eslint . --max-warnings 0 --fix",
|
|
117
|
-
"test": "pnpm run test:coverage",
|
|
85
|
+
"____ STANDARD PACKAGE TASKS ________________________________________": "",
|
|
86
|
+
"all": "turbo run pipeline-all",
|
|
87
|
+
"all:ci": "turbo run pipeline-ci --ui=stream",
|
|
88
|
+
"build": "pnpm run build:clean && pnpm run build:lib && pnpm run build:verify",
|
|
89
|
+
"clean": "pnpm run \"/^.*:clean$/\" && rimraf --glob ./node_modules/.cache ./.turbo ./*.log",
|
|
90
|
+
"dev": "pnpm run build:lib:watch",
|
|
91
|
+
"lint": "biome check --write .",
|
|
92
|
+
"lint:verify": "biome check .",
|
|
93
|
+
"publish:yalc": "pnpm run prepack && yalc publish --no-scripts --sig --push --replace",
|
|
118
94
|
"test:clean": "rimraf ./coverage",
|
|
119
|
-
"test
|
|
120
|
-
"test:
|
|
121
|
-
"test:
|
|
122
|
-
"test:
|
|
123
|
-
"
|
|
124
|
-
"
|
|
125
|
-
"
|
|
95
|
+
"test": "pnpm run test:clean && vitest run --coverage",
|
|
96
|
+
"test:ci": "pnpm run test:clean && vitest run --coverage --bail 1",
|
|
97
|
+
"test:ui": "pnpm run test:clean && vitest --ui",
|
|
98
|
+
"test:watch": "pnpm run test:clean && vitest watch --coverage",
|
|
99
|
+
"typecheck": "tsgo -p ./tsconfig.json --noEmit",
|
|
100
|
+
"____ BUILD & VALIDATION TASKS ______________________________________": "",
|
|
101
|
+
"build:clean": "rimraf --glob ./dist ./*.tgz",
|
|
102
|
+
"build:lib": "tsdown --config ./tsdown.config.ts",
|
|
103
|
+
"build:lib:watch": "tsdown --config ./tsdown.config.ts --watch",
|
|
104
|
+
"build:verify": "publint --strict && size-limit",
|
|
105
|
+
"build:verifypack": "node --experimental-strip-types ../../scripts/verify-packed-tarballs.ts",
|
|
106
|
+
"size-check": "size-limit --why"
|
|
126
107
|
}
|
|
127
108
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
2
|
import { defaultOptions } from '../core/defaultOptions.js';
|
|
3
3
|
import { LimitedCache } from '../core/LimitedCache.js';
|
|
4
|
-
import { LimitedCacheInstance } from '../types.js';
|
|
4
|
+
import type { LimitedCacheInstance } from '../types.js';
|
|
5
5
|
|
|
6
6
|
describe('LimitedCache', () => {
|
|
7
7
|
it('initializes without options', () => {
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
// biome-ignore-all lint/performance/noDelete: Delete is explicitly used to unset items for these tests
|
|
2
|
+
// biome-ignore-all lint/suspicious/noAssignInExpressions: Assignment expressions are explicitly tested
|
|
3
|
+
|
|
4
|
+
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
5
|
import { defaultOptions } from '../core/defaultOptions.js';
|
|
3
|
-
import {
|
|
4
|
-
import { LimitedCacheObjectInstance } from '../types.js';
|
|
6
|
+
import { getCacheMetaFromObject, LimitedCacheObject } from '../core/LimitedCacheObject.js';
|
|
7
|
+
import type { LimitedCacheObjectInstance } from '../types.js';
|
|
5
8
|
|
|
6
9
|
describe('LimitedCacheObject', () => {
|
|
7
10
|
it('initializes without options', () => {
|
|
@@ -29,13 +32,13 @@ describe('LimitedCacheObject', () => {
|
|
|
29
32
|
});
|
|
30
33
|
|
|
31
34
|
it('has: when missing, via prototype hasOwnProperty', () => {
|
|
32
|
-
const result = Object.
|
|
35
|
+
const result = Object.hasOwn(myCache, 'asdf');
|
|
33
36
|
expect(result).toEqual(false);
|
|
34
37
|
});
|
|
35
38
|
|
|
36
39
|
it('has: when missing, via local hasOwnProperty', () => {
|
|
37
40
|
// eslint-disable-next-line no-prototype-builtins
|
|
38
|
-
const result =
|
|
41
|
+
const result = Object.hasOwn(myCache, 'asdf');
|
|
39
42
|
expect(result).toEqual(false);
|
|
40
43
|
});
|
|
41
44
|
|
|
@@ -45,71 +48,71 @@ describe('LimitedCacheObject', () => {
|
|
|
45
48
|
});
|
|
46
49
|
|
|
47
50
|
it('has: when present, via prototype hasOwnProperty', () => {
|
|
48
|
-
myCache
|
|
51
|
+
myCache['abc'] = 123;
|
|
49
52
|
|
|
50
|
-
const result = Object.
|
|
53
|
+
const result = Object.hasOwn(myCache, 'abc');
|
|
51
54
|
expect(result).toEqual(true);
|
|
52
55
|
});
|
|
53
56
|
|
|
54
57
|
it('has: when present, via local hasOwnProperty', () => {
|
|
55
|
-
myCache
|
|
58
|
+
myCache['abc'] = 123;
|
|
56
59
|
|
|
57
60
|
// eslint-disable-next-line no-prototype-builtins
|
|
58
|
-
const result =
|
|
61
|
+
const result = Object.hasOwn(myCache, 'abc');
|
|
59
62
|
expect(result).toEqual(true);
|
|
60
63
|
});
|
|
61
64
|
|
|
62
65
|
it('has: when present, via "in"', () => {
|
|
63
|
-
myCache
|
|
66
|
+
myCache['abc'] = 123;
|
|
64
67
|
|
|
65
68
|
const result = 'abc' in myCache;
|
|
66
69
|
expect(result).toEqual(true);
|
|
67
70
|
});
|
|
68
71
|
|
|
69
72
|
it('get: when missing', () => {
|
|
70
|
-
const result = myCache
|
|
73
|
+
const result = myCache['asdf'];
|
|
71
74
|
expect(result).toBeUndefined();
|
|
72
75
|
});
|
|
73
76
|
|
|
74
77
|
it('get: when present', () => {
|
|
75
|
-
myCache
|
|
78
|
+
myCache['abc'] = 123;
|
|
76
79
|
|
|
77
|
-
const result = myCache
|
|
80
|
+
const result = myCache['abc'];
|
|
78
81
|
expect(result).toEqual(123);
|
|
79
82
|
});
|
|
80
83
|
|
|
81
84
|
it('set: when new', () => {
|
|
82
|
-
const result = (myCache
|
|
85
|
+
const result = (myCache['abc'] = 123);
|
|
83
86
|
expect(result).toEqual(123);
|
|
84
87
|
});
|
|
85
88
|
|
|
86
89
|
it('set: when already present', () => {
|
|
87
|
-
myCache
|
|
90
|
+
myCache['abc'] = 123;
|
|
88
91
|
|
|
89
|
-
const result = (myCache
|
|
92
|
+
const result = (myCache['abc'] = 456);
|
|
90
93
|
expect(result).toEqual(456);
|
|
91
94
|
});
|
|
92
95
|
|
|
93
96
|
it('delete and return value', () => {
|
|
94
|
-
myCache
|
|
97
|
+
myCache['abc'] = 123;
|
|
95
98
|
|
|
96
|
-
const result = delete myCache
|
|
99
|
+
const result = delete myCache['abc'];
|
|
97
100
|
expect(result).toEqual(true);
|
|
98
101
|
});
|
|
99
102
|
|
|
100
103
|
it('delete, then check', () => {
|
|
101
|
-
myCache
|
|
102
|
-
delete myCache
|
|
104
|
+
myCache['abc'] = 123;
|
|
105
|
+
delete myCache['abc'];
|
|
103
106
|
|
|
104
|
-
const result = Object.
|
|
107
|
+
const result = Object.hasOwn(myCache, 'abc');
|
|
105
108
|
expect(result).toEqual(false);
|
|
106
109
|
});
|
|
107
110
|
|
|
108
111
|
it('delete, then get', () => {
|
|
109
|
-
myCache
|
|
110
|
-
delete myCache
|
|
112
|
+
myCache['abc'] = 123;
|
|
113
|
+
delete myCache['abc'];
|
|
111
114
|
|
|
112
|
-
const result = myCache
|
|
115
|
+
const result = myCache['abc'];
|
|
113
116
|
expect(result).toEqual(undefined);
|
|
114
117
|
});
|
|
115
118
|
|
|
@@ -119,15 +122,15 @@ describe('LimitedCacheObject', () => {
|
|
|
119
122
|
});
|
|
120
123
|
|
|
121
124
|
it('keys: when populated', () => {
|
|
122
|
-
myCache
|
|
125
|
+
myCache['abc'] = 123;
|
|
123
126
|
|
|
124
127
|
const result = Object.keys(myCache);
|
|
125
128
|
expect(result).toEqual(['abc']);
|
|
126
129
|
});
|
|
127
130
|
|
|
128
131
|
it('keys: when all are removed', () => {
|
|
129
|
-
myCache
|
|
130
|
-
delete myCache
|
|
132
|
+
myCache['abc'] = 123;
|
|
133
|
+
delete myCache['abc'];
|
|
131
134
|
|
|
132
135
|
const result = Object.keys(myCache);
|
|
133
136
|
expect(result).toEqual([]);
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { beforeEach, describe, expect, it, vitest } from 'vitest';
|
|
2
2
|
import { defaultOptions } from '../core/defaultOptions.js';
|
|
3
3
|
import {
|
|
4
4
|
isCacheMeta,
|
|
5
|
-
upgradeCacheMeta,
|
|
6
|
-
lowLevelInit,
|
|
7
|
-
lowLevelGetOne,
|
|
8
5
|
lowLevelGetAll,
|
|
6
|
+
lowLevelGetOne,
|
|
9
7
|
lowLevelHas,
|
|
10
|
-
|
|
8
|
+
lowLevelInit,
|
|
11
9
|
lowLevelRemove,
|
|
12
10
|
lowLevelReset,
|
|
11
|
+
lowLevelSet,
|
|
12
|
+
upgradeCacheMeta,
|
|
13
13
|
} from '../core/lowLevelFunctions.js';
|
|
14
|
-
import { LimitedCacheMeta } from '../types.js';
|
|
14
|
+
import type { LimitedCacheMeta } from '../types.js';
|
|
15
15
|
|
|
16
16
|
describe('lowLevelFunctions', () => {
|
|
17
17
|
describe('isCacheMeta', () => {
|
|
@@ -29,7 +29,6 @@ describe('lowLevelFunctions', () => {
|
|
|
29
29
|
});
|
|
30
30
|
it('rejects invalid cacheMeta shapes', () => {
|
|
31
31
|
expect(
|
|
32
|
-
// @ts-expect-error Invalid cacheMeta shape
|
|
33
32
|
isCacheMeta({
|
|
34
33
|
cache: {},
|
|
35
34
|
}),
|
|
@@ -57,7 +56,7 @@ describe('lowLevelFunctions', () => {
|
|
|
57
56
|
upgradeCacheMeta({
|
|
58
57
|
cache: {},
|
|
59
58
|
});
|
|
60
|
-
}).
|
|
59
|
+
}).toThrow();
|
|
61
60
|
});
|
|
62
61
|
});
|
|
63
62
|
|
|
@@ -168,6 +167,7 @@ describe('lowLevelFunctions', () => {
|
|
|
168
167
|
});
|
|
169
168
|
|
|
170
169
|
it('get a missing key', () => {
|
|
170
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
171
171
|
const result = lowLevelGetOne(myCacheMeta, 'abc');
|
|
172
172
|
expect(result).toBeUndefined();
|
|
173
173
|
});
|
|
@@ -178,6 +178,7 @@ describe('lowLevelFunctions', () => {
|
|
|
178
178
|
myCacheMeta.keyList = ['abc'];
|
|
179
179
|
myCacheMeta.keyInfo['abc'] = [Date.now(), 0];
|
|
180
180
|
|
|
181
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
181
182
|
const result = lowLevelGetOne(myCacheMeta, 'abc');
|
|
182
183
|
expect(result).toEqual(123);
|
|
183
184
|
});
|
|
@@ -188,6 +189,7 @@ describe('lowLevelFunctions', () => {
|
|
|
188
189
|
myCacheMeta.keyList = ['abc'];
|
|
189
190
|
myCacheMeta.keyInfo['abc'] = [1, 0];
|
|
190
191
|
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
191
193
|
const result = lowLevelGetOne(myCacheMeta, 'abc');
|
|
192
194
|
expect(result).toEqual(undefined);
|
|
193
195
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
+
import { type LimitedCacheMeta, limitedCacheUtil } from '../../index.js';
|
|
3
3
|
|
|
4
4
|
describe('key names', () => {
|
|
5
5
|
let myCacheMeta: LimitedCacheMeta;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { LimitedCache, LimitedCacheInstance } from '../../index.js';
|
|
1
|
+
import { beforeEach, describe, expect, it, vitest } from 'vitest';
|
|
2
|
+
import { LimitedCache, type LimitedCacheInstance } from '../../index.js';
|
|
3
3
|
|
|
4
4
|
// To avoid race conditions or timing issues, since some expect() checks can take 10+ ms when busy,
|
|
5
5
|
// we use a long cache timeout even for 'immediate' expiration, and use delays slightly longer than that
|
|
6
6
|
const CACHE_TIMEOUT = 20;
|
|
7
|
-
const timeoutPromise = (): Promise<
|
|
7
|
+
const timeoutPromise = (): Promise<void> =>
|
|
8
8
|
new Promise((resolve) => setTimeout(resolve, CACHE_TIMEOUT + 2));
|
|
9
9
|
|
|
10
10
|
describe('maxCacheSize scenarios', () => {
|
|
@@ -95,7 +95,7 @@ describe('maxCacheSize scenarios', () => {
|
|
|
95
95
|
expect(Object.keys(myCache.getAll())).toEqual(['n=6', 'n=7', 'n=8', 'n=9', 'n=10']);
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
it('warns if a freshly-added item is pushed out too quickly',
|
|
98
|
+
it('warns if a freshly-added item is pushed out too quickly', () => {
|
|
99
99
|
myCache = LimitedCache({
|
|
100
100
|
maxCacheSize: 5,
|
|
101
101
|
maxCacheTime: 1000,
|
|
@@ -116,7 +116,10 @@ describe('maxCacheSize scenarios', () => {
|
|
|
116
116
|
// And we should have seen a warning about the force-removed key
|
|
117
117
|
const consoleWarnCalls = consoleWarnSpy.mock.calls;
|
|
118
118
|
expect(consoleWarnCalls.length).toBe(1);
|
|
119
|
-
const [warningString, warningObject] = consoleWarnCalls[0]
|
|
119
|
+
const [warningString, warningObject] = consoleWarnCalls[0] as [
|
|
120
|
+
string,
|
|
121
|
+
{ key: string; value: string },
|
|
122
|
+
];
|
|
120
123
|
expect(warningString).toBe(
|
|
121
124
|
'Purged an item from cache while it was still fresh: you may want to increase maxCacheSize',
|
|
122
125
|
);
|