limited-cache 2.1.1 → 2.3.0

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.
Files changed (59) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/cjs/index.cjs +296 -0
  3. package/dist/cjs/index.d.cts +107 -0
  4. package/dist/cjs/index.d.cts.map +1 -0
  5. package/dist/esm/index.d.ts +107 -0
  6. package/dist/esm/index.d.ts.map +1 -0
  7. package/dist/esm/index.js +280 -0
  8. package/dist/esm/index.js.map +1 -0
  9. package/package.json +46 -63
  10. package/src/__tests__/LimitedCache.test.ts +2 -2
  11. package/src/__tests__/LimitedCacheObject.test.ts +30 -27
  12. package/src/__tests__/lowLevelFunctions.test.ts +10 -8
  13. package/src/__tests__/scenarios/keys.test.ts +2 -2
  14. package/src/__tests__/scenarios/maxCacheSize.test.ts +8 -5
  15. package/src/__tests__/scenarios/maxCacheTime.test.ts +58 -58
  16. package/src/__tests__/scenarios/values.test.ts +2 -2
  17. package/src/__tests__/typeChecks/LimitedCacheObjectTypes.test.ts +15 -15
  18. package/src/__tests__/typeChecks/LimitedCacheTypes.test.ts +1 -1
  19. package/src/__tests__/typeChecks/lowLevelFunctionsTypes.test.ts +2 -2
  20. package/src/core/LimitedCache.ts +20 -21
  21. package/src/core/LimitedCacheObject.ts +30 -19
  22. package/src/core/defaultOptions.ts +5 -4
  23. package/src/core/limitedCacheUtil.ts +15 -3
  24. package/src/core/lowLevelFunctions.ts +41 -32
  25. package/src/types.ts +16 -3
  26. package/dist/core/LimitedCache.d.ts +0 -4
  27. package/dist/core/LimitedCache.d.ts.map +0 -1
  28. package/dist/core/LimitedCache.js +0 -29
  29. package/dist/core/LimitedCache.js.map +0 -1
  30. package/dist/core/LimitedCacheObject.d.ts +0 -5
  31. package/dist/core/LimitedCacheObject.d.ts.map +0 -1
  32. package/dist/core/LimitedCacheObject.js +0 -52
  33. package/dist/core/LimitedCacheObject.js.map +0 -1
  34. package/dist/core/builtIns.d.ts +0 -12
  35. package/dist/core/builtIns.d.ts.map +0 -1
  36. package/dist/core/builtIns.js +0 -5
  37. package/dist/core/builtIns.js.map +0 -1
  38. package/dist/core/defaultOptions.d.ts +0 -6
  39. package/dist/core/defaultOptions.d.ts.map +0 -1
  40. package/dist/core/defaultOptions.js +0 -14
  41. package/dist/core/defaultOptions.js.map +0 -1
  42. package/dist/core/limitedCacheUtil.d.ts +0 -13
  43. package/dist/core/limitedCacheUtil.d.ts.map +0 -1
  44. package/dist/core/limitedCacheUtil.js +0 -14
  45. package/dist/core/limitedCacheUtil.js.map +0 -1
  46. package/dist/core/lowLevelFunctions.d.ts +0 -14
  47. package/dist/core/lowLevelFunctions.d.ts.map +0 -1
  48. package/dist/core/lowLevelFunctions.js +0 -240
  49. package/dist/core/lowLevelFunctions.js.map +0 -1
  50. package/dist/index.cjs +0 -388
  51. package/dist/index.d.ts +0 -7
  52. package/dist/index.d.ts.map +0 -1
  53. package/dist/index.js +0 -7
  54. package/dist/index.js.map +0 -1
  55. package/dist/types.d.ts +0 -58
  56. package/dist/types.d.ts.map +0 -1
  57. package/dist/types.js +0 -2
  58. package/dist/types.js.map +0 -1
  59. package/src/core/builtIns.ts +0 -9
@@ -1,19 +1,18 @@
1
- import { objectAssign, objectCreate, dateNow, hasOwnProperty } from './builtIns.js';
2
- import { CURRENT_META_VERSION, MAXIMUM_CACHE_TIME, defaultOptions } from './defaultOptions.js';
3
- import {
4
- LimitedCacheOptions,
5
- LimitedCacheOptionsReadonly,
1
+ import type {
2
+ DefaultItemType,
6
3
  LimitedCacheMeta,
4
+ LimitedCacheOptions,
7
5
  LimitedCacheOptionsFull,
8
- DefaultItemType,
6
+ LimitedCacheOptionsReadonly,
9
7
  } from '../types.js';
8
+ import { CURRENT_META_VERSION, defaultOptions, MAXIMUM_CACHE_TIME } from './defaultOptions.js';
10
9
 
11
10
  /* Initialization and options */
12
11
 
13
12
  const positiveNumberOrZero = (value: number): number => Math.max(value, 0) || 0;
14
13
 
15
14
  const normalizeOptions = (cacheMetaOptions: LimitedCacheOptionsFull): LimitedCacheOptionsFull => {
16
- objectAssign(cacheMetaOptions, {
15
+ Object.assign(cacheMetaOptions, {
17
16
  maxCacheSize: positiveNumberOrZero(cacheMetaOptions.maxCacheSize),
18
17
  maxCacheTime: positiveNumberOrZero(cacheMetaOptions.maxCacheTime),
19
18
  opLimit: positiveNumberOrZero(cacheMetaOptions.opLimit),
@@ -27,8 +26,9 @@ const normalizeOptions = (cacheMetaOptions: LimitedCacheOptionsFull): LimitedCac
27
26
  return cacheMetaOptions;
28
27
  };
29
28
 
30
- const isCacheMeta = (cacheMeta: LimitedCacheMeta): boolean => {
31
- return !!cacheMeta && !!cacheMeta.limitedCacheMetaVersion;
29
+ const isCacheMeta = (cacheMeta: unknown): cacheMeta is LimitedCacheMeta => {
30
+ // @ts-expect-error Duck-typing the unknown value
31
+ return !!cacheMeta?.limitedCacheMetaVersion;
32
32
  };
33
33
 
34
34
  const upgradeCacheMeta = (cacheMeta: LimitedCacheMeta): void => {
@@ -38,6 +38,7 @@ const upgradeCacheMeta = (cacheMeta: LimitedCacheMeta): void => {
38
38
  if (cacheMeta.limitedCacheMetaVersion !== CURRENT_META_VERSION) {
39
39
  // Version is out of date! (Today the only prior version is 1)
40
40
  // Version 1: Cache meta cannot be migrated because timestamps and keys are incompatible
41
+ // biome-ignore lint/suspicious/noConsole: Intentional warning
41
42
  console.warn('Limited-cache metadata is from an incompatible version (1). It must be reset.');
42
43
  cacheMeta.limitedCacheMetaVersion = CURRENT_META_VERSION;
43
44
  lowLevelReset(cacheMeta);
@@ -49,19 +50,22 @@ const lowLevelSetOptions = <ItemType = DefaultItemType>(
49
50
  options: LimitedCacheOptions,
50
51
  ): LimitedCacheOptionsReadonly => {
51
52
  upgradeCacheMeta(cacheMeta);
52
- return normalizeOptions(objectAssign(cacheMeta.options, options));
53
+ return normalizeOptions(Object.assign(cacheMeta.options, options));
53
54
  };
54
55
 
55
56
  const lowLevelInit = <ItemType = DefaultItemType>(
56
57
  optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType>,
57
58
  ): LimitedCacheMeta<ItemType> => {
58
- if (isCacheMeta(optionsOrCacheMeta as LimitedCacheMeta<ItemType>)) {
59
- const existingCacheMeta = optionsOrCacheMeta as LimitedCacheMeta<ItemType>;
59
+ if (isCacheMeta(optionsOrCacheMeta)) {
60
+ const existingCacheMeta = optionsOrCacheMeta;
60
61
  upgradeCacheMeta(existingCacheMeta);
61
62
  return existingCacheMeta;
62
63
  }
63
64
  // Else: it's options
64
- const fullOptions = normalizeOptions({ ...defaultOptions, ...optionsOrCacheMeta });
65
+ const fullOptions = normalizeOptions({
66
+ ...defaultOptions,
67
+ ...optionsOrCacheMeta,
68
+ });
65
69
 
66
70
  // The cacheMeta is created once, and persists per instance
67
71
  const newCacheMeta = lowLevelReset({
@@ -101,7 +105,7 @@ const lowLevelDoMaintenance = <ItemType = DefaultItemType>(
101
105
  ): LimitedCacheMeta<ItemType> => {
102
106
  upgradeCacheMeta(cacheMeta);
103
107
  const { cache, keyList, keyInfo } = cacheMeta;
104
- const now = dateNow();
108
+ const now = Date.now();
105
109
 
106
110
  // Rebuild cache from keyList only, checking timestamps to auto-remove expired
107
111
  const [newCache, newKeyList, newKeyInfo] = keyList.reduce(
@@ -115,13 +119,15 @@ const lowLevelDoMaintenance = <ItemType = DefaultItemType>(
115
119
  return acc;
116
120
  },
117
121
  [
122
+ // This manual assertion is required because TypeScript doesn't know that the initial value is of the same type as the accumulator.
123
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
118
124
  {} as (typeof cacheMeta)['cache'],
119
125
  [] as (typeof cacheMeta)['keyList'],
120
- objectCreate(null) as (typeof cacheMeta)['keyInfo'],
126
+ Object.create(null) as (typeof cacheMeta)['keyInfo'],
121
127
  ],
122
128
  );
123
129
 
124
- return objectAssign(cacheMeta, {
130
+ return Object.assign(cacheMeta, {
125
131
  cache: newCache,
126
132
  keyList: newKeyList,
127
133
  keyInfo: newKeyInfo,
@@ -134,7 +140,7 @@ const _removeFromIndex = (cacheMeta: LimitedCacheMeta, startIndex: number, now:
134
140
 
135
141
  // Always remove the item requested, and also remove any neighbors who have expired
136
142
  let nextIndex = startIndex;
137
- let nextCacheKey = keyList[startIndex];
143
+ let nextCacheKey = keyList[startIndex] as string;
138
144
  const keyListLength = keyList.length;
139
145
  do {
140
146
  // Remove the 'next' item
@@ -143,7 +149,7 @@ const _removeFromIndex = (cacheMeta: LimitedCacheMeta, startIndex: number, now:
143
149
 
144
150
  // Now advance and decide whether to keep going
145
151
  nextIndex++;
146
- nextCacheKey = keyList[nextIndex];
152
+ nextCacheKey = keyList[nextIndex] as string;
147
153
  } while (nextIndex < keyListLength && _cacheKeyHasExpired(cacheMeta, nextCacheKey, now));
148
154
 
149
155
  // Remove the index for everything from the startIndex until we stopped
@@ -161,14 +167,14 @@ const _removeItemsToMakeRoom = (cacheMeta: LimitedCacheMeta, now: number): void
161
167
  // These track the soonest-to-expire thing we've found. It may not actually be "oldest".
162
168
  // By default we'll remove the item at the head of the queue, unless we find something better.
163
169
  let oldestItemIndex = 0;
164
- let oldestExpireTime = _getExpireTime(cacheMeta, keyList[0]);
170
+ let oldestExpireTime = _getExpireTime(cacheMeta, keyList[0] as string);
165
171
 
166
172
  if (oldestExpireTime > now) {
167
173
  // The head of the list hasn't yet expired: scan for a better candidate to remove
168
174
  let indexToCheck = 0;
169
175
  const maxIndexToCheck = Math.min(keyList.length, scanLimit);
170
176
  while (indexToCheck < maxIndexToCheck) {
171
- const cacheKeyForIndex = keyList[indexToCheck];
177
+ const cacheKeyForIndex = keyList[indexToCheck] as string;
172
178
  const expireTimeForIndex = _getExpireTime(cacheMeta, cacheKeyForIndex);
173
179
 
174
180
  // We only consider it if it's eligible for expiration: otherwise it can't be a better option
@@ -195,15 +201,17 @@ const _removeItemsToMakeRoom = (cacheMeta: LimitedCacheMeta, now: number): void
195
201
  warnIfItemPurgedBeforeTime &&
196
202
  oldestExpireTime > now
197
203
  ) {
198
- const oldestItemKey = keyList[oldestItemIndex];
204
+ const oldestItemKey = keyList[oldestItemIndex] as string;
199
205
  const [oldestItemSetTime, oldestItemExpireTime] = keyInfo[oldestItemKey] as [number, number];
200
206
 
201
207
  if (now - oldestItemSetTime < warnIfItemPurgedBeforeTime) {
208
+ // biome-ignore lint/suspicious/noConsole: Dev-only code
202
209
  console.warn(
203
210
  'Purged an item from cache while it was still fresh: you may want to increase maxCacheSize',
204
211
  {
205
212
  currentTime: now,
206
213
  key: oldestItemKey,
214
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
207
215
  item: cache[oldestItemKey],
208
216
  setTime: oldestItemSetTime,
209
217
  expireTime: oldestItemExpireTime,
@@ -225,8 +233,9 @@ const lowLevelHas = <ItemType = DefaultItemType>(
225
233
  ): boolean => {
226
234
  upgradeCacheMeta(cacheMeta);
227
235
  const { cache } = cacheMeta;
228
- if (hasOwnProperty.call(cache, cacheKey) && cache[cacheKey] !== undefined) {
229
- if (!_cacheKeyHasExpired(cacheMeta, cacheKey, dateNow())) {
236
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: Keeping the legacy hasOwnProperty to avoid a breaking change
237
+ if (Object.prototype.hasOwnProperty.call(cache, cacheKey) && cache[cacheKey] !== undefined) {
238
+ if (!_cacheKeyHasExpired(cacheMeta, cacheKey, Date.now())) {
230
239
  return true;
231
240
  }
232
241
  // If it's expired, clear the value so that we can short-circuit future lookups
@@ -269,7 +278,7 @@ const lowLevelSet = <ItemType = DefaultItemType>(
269
278
  keyInfo,
270
279
  } = cacheMeta;
271
280
 
272
- const now = dateNow();
281
+ const now = Date.now();
273
282
  const isNew = !keyInfo[cacheKey];
274
283
 
275
284
  if (cacheMeta.cache[cacheKey] !== item) {
@@ -299,7 +308,7 @@ const lowLevelSet = <ItemType = DefaultItemType>(
299
308
  }
300
309
  }
301
310
 
302
- if (_cacheKeyHasExpired(cacheMeta, keyList[0], now)) {
311
+ if (_cacheKeyHasExpired(cacheMeta, keyList[0] as string, now)) {
303
312
  // While we're here, if we need to expire the head of the queue then drop it
304
313
  _removeFromIndex(cacheMeta, 0, now);
305
314
  }
@@ -331,24 +340,24 @@ const lowLevelReset = <ItemType = DefaultItemType>(
331
340
  cacheMeta: LimitedCacheMeta<ItemType>,
332
341
  ): LimitedCacheMeta<ItemType> => {
333
342
  upgradeCacheMeta(cacheMeta);
334
- return objectAssign(cacheMeta, {
343
+ return Object.assign(cacheMeta, {
335
344
  cache: {},
336
345
  keyList: [],
337
- keyInfo: objectCreate(null),
346
+ keyInfo: Object.create(null) as Record<string, ItemType>,
338
347
  opsLeft: cacheMeta.options.opLimit,
339
348
  });
340
349
  };
341
350
 
342
351
  export {
343
352
  isCacheMeta,
344
- upgradeCacheMeta,
345
- lowLevelInit,
346
- lowLevelGetOne,
353
+ lowLevelDoMaintenance,
347
354
  lowLevelGetAll,
355
+ lowLevelGetOne,
348
356
  lowLevelHas,
349
- lowLevelSet,
357
+ lowLevelInit,
350
358
  lowLevelRemove,
351
359
  lowLevelReset,
352
- lowLevelDoMaintenance,
360
+ lowLevelSet,
353
361
  lowLevelSetOptions,
362
+ upgradeCacheMeta,
354
363
  };
package/src/types.ts CHANGED
@@ -1,7 +1,20 @@
1
+ declare global {
2
+ // We only want the exact `process.env.NODE_ENV` (and not all Node typings) since that's handled by bundlers
3
+ // even in ESM.
4
+ // eslint-disable-next-line @typescript-eslint/no-namespace
5
+ namespace NodeJS {
6
+ interface ProcessEnv {
7
+ // Usually optional because env vars may be missing at runtime
8
+ NODE_ENV?: 'development' | 'production' | 'test';
9
+ }
10
+ }
11
+ }
12
+
1
13
  // This makes it easy to ensure that ItemType gets passed to any nested generics:
2
- // Using `unknown` helps catch errors during development but is a pain for consumers.
3
- // Using `any` is nicer for consumers, but errors could slip through during development.
4
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ // * Using `unknown` helps catch errors during development but is a pain for consumers.
15
+ // * Using `any` is nicer for consumers, but errors could slip through during development.
16
+ // In v3.0.0 the default will change from `any` to `unknown`
17
+ // biome-ignore lint/suspicious/noExplicitAny: Intentionally left as any for better DX
5
18
  export type DefaultItemType = any;
6
19
 
7
20
  export interface LimitedCacheOptionsFull {
@@ -1,4 +0,0 @@
1
- import { LimitedCacheOptions, LimitedCacheInstance } from '../types.js';
2
- declare const LimitedCache: <ItemType = any>(options?: LimitedCacheOptions) => LimitedCacheInstance<ItemType>;
3
- export { LimitedCache };
4
- //# sourceMappingURL=LimitedCache.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LimitedCache.d.ts","sourceRoot":"","sources":["../../src/core/LimitedCache.ts"],"names":[],"mappings":"AAWA,OAAO,EACL,mBAAmB,EAEnB,oBAAoB,EAGrB,MAAM,aAAa,CAAC;AAUrB,QAAA,MAAM,YAAY,6BACN,mBAAmB,mCAsB9B,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -1,29 +0,0 @@
1
- import { lowLevelInit, lowLevelGetOne, lowLevelGetAll, lowLevelHas, lowLevelSet, lowLevelRemove, lowLevelReset, lowLevelSetOptions, lowLevelDoMaintenance, } from './lowLevelFunctions.js';
2
- // Most public functions just call a low-level function directly, passing the cacheMeta.
3
- // Doing this via a helper function makes the typeChecks easier, and minifies better.
4
- const bindFunctionToCacheMeta = (
5
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
- fn, cacheMeta) => fn.bind(null, cacheMeta);
7
- const LimitedCache = (options) => {
8
- const cacheMeta = lowLevelInit(options);
9
- return {
10
- get: bindFunctionToCacheMeta(lowLevelGetOne, cacheMeta),
11
- getAll: bindFunctionToCacheMeta(lowLevelGetAll, cacheMeta),
12
- has: bindFunctionToCacheMeta(lowLevelHas, cacheMeta),
13
- set: (cacheKey, item) => {
14
- lowLevelSet(cacheMeta, cacheKey, item);
15
- return item;
16
- },
17
- remove: (cacheKey) => {
18
- lowLevelRemove(cacheMeta, cacheKey);
19
- return true;
20
- },
21
- reset: bindFunctionToCacheMeta(lowLevelReset, cacheMeta),
22
- getCacheMeta: () => cacheMeta,
23
- getOptions: () => cacheMeta.options,
24
- setOptions: bindFunctionToCacheMeta(lowLevelSetOptions, cacheMeta),
25
- doMaintenance: bindFunctionToCacheMeta(lowLevelDoMaintenance, cacheMeta),
26
- };
27
- };
28
- export { LimitedCache };
29
- //# sourceMappingURL=LimitedCache.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LimitedCache.js","sourceRoot":"","sources":["../../src/core/LimitedCache.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,cAAc,EACd,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,EACd,aAAa,EACb,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,wBAAwB,CAAC;AAShC,wFAAwF;AACxF,qFAAqF;AACrF,MAAM,uBAAuB,GAAG;AAC9B,8DAA8D;AAC9D,EAAqE,EACrE,SAAqC,EACrC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;AAE9B,MAAM,YAAY,GAAG,CACnB,OAA6B,EACG,EAAE;IAClC,MAAM,SAAS,GAAG,YAAY,CAAW,OAAO,CAAC,CAAC;IAElD,OAAO;QACL,GAAG,EAAE,uBAAuB,CAAW,cAAc,EAAE,SAAS,CAAC;QACjE,MAAM,EAAE,uBAAuB,CAAW,cAAc,EAAE,SAAS,CAAC;QACpE,GAAG,EAAE,uBAAuB,CAAW,WAAW,EAAE,SAAS,CAAC;QAC9D,GAAG,EAAE,CAAC,QAAQ,EAAE,IAAI,EAAY,EAAE;YAChC,WAAW,CAAC,SAAS,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;YACvC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,EAAE,CAAC,QAAQ,EAAQ,EAAE;YACzB,cAAc,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;YACpC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,KAAK,EAAE,uBAAuB,CAAW,aAAa,EAAE,SAAS,CAAC;QAClE,YAAY,EAAE,GAA+B,EAAE,CAAC,SAAS;QACzD,UAAU,EAAE,GAAgC,EAAE,CAAC,SAAS,CAAC,OAAO;QAChE,UAAU,EAAE,uBAAuB,CAAW,kBAAkB,EAAE,SAAS,CAAC;QAC5E,aAAa,EAAE,uBAAuB,CAAW,qBAAqB,EAAE,SAAS,CAAC;KACnF,CAAC;AACJ,CAAC,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -1,5 +0,0 @@
1
- import { LimitedCacheOptions, LimitedCacheObjectInstance, LimitedCacheMeta } from '../types.js';
2
- declare const LimitedCacheObject: <ItemType = any>(options?: LimitedCacheOptions) => LimitedCacheObjectInstance<ItemType>;
3
- declare const getCacheMetaFromObject: (instance: LimitedCacheObjectInstance) => LimitedCacheMeta;
4
- export { LimitedCacheObject, getCacheMetaFromObject };
5
- //# sourceMappingURL=LimitedCacheObject.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LimitedCacheObject.d.ts","sourceRoot":"","sources":["../../src/core/LimitedCacheObject.ts"],"names":[],"mappings":"AASA,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC1B,gBAAgB,EAEjB,MAAM,aAAa,CAAC;AA4CrB,QAAA,MAAM,kBAAkB,6BACZ,mBAAmB,yCAO9B,CAAC;AAEF,QAAA,MAAM,sBAAsB,aAAc,0BAA0B,KAAG,gBAEtE,CAAC;AAEF,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,CAAC"}
@@ -1,52 +0,0 @@
1
- import { hasOwnProperty } from './builtIns.js';
2
- import { lowLevelInit, lowLevelGetOne, lowLevelGetAll, lowLevelHas, lowLevelSet, lowLevelRemove, } from './lowLevelFunctions.js';
3
- // The `any` here doesn't escape out anywhere: it's overridden by the constructor below
4
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
- const proxyHandler = {
6
- get: (cacheMeta, cacheKey) => {
7
- if (cacheKey === 'hasOwnProperty') {
8
- return hasOwnProperty;
9
- }
10
- return lowLevelGetOne(cacheMeta, cacheKey);
11
- },
12
- getOwnPropertyDescriptor: (cacheMeta, cacheKey) => {
13
- const hasResult = lowLevelHas(cacheMeta, cacheKey);
14
- const getResult = lowLevelGetOne(cacheMeta, cacheKey);
15
- if (hasResult) {
16
- return {
17
- configurable: true,
18
- enumerable: hasResult,
19
- value: getResult,
20
- writable: true,
21
- };
22
- }
23
- return;
24
- },
25
- has: lowLevelHas,
26
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
- set: (cacheMeta, cacheKey, item) => {
28
- lowLevelSet(cacheMeta, cacheKey, item);
29
- return item;
30
- },
31
- deleteProperty: (cacheMeta, cacheKey) => {
32
- lowLevelRemove(cacheMeta, cacheKey);
33
- return true;
34
- },
35
- ownKeys: (cacheMeta) => Object.keys(lowLevelGetAll(cacheMeta)),
36
- };
37
- /**
38
- * So that we can retrieve the cacheMeta for a LimitedCacheObject, without polluting its properties, each proxy
39
- * is associated back to its internal cacheMeta here.
40
- */
41
- const cacheMetasForProxies = new WeakMap();
42
- const LimitedCacheObject = (options) => {
43
- const cacheMeta = lowLevelInit(options);
44
- const limitedCacheObject = new Proxy(cacheMeta, proxyHandler);
45
- cacheMetasForProxies.set(limitedCacheObject, cacheMeta);
46
- return limitedCacheObject;
47
- };
48
- const getCacheMetaFromObject = (instance) => {
49
- return cacheMetasForProxies.get(instance);
50
- };
51
- export { LimitedCacheObject, getCacheMetaFromObject };
52
- //# sourceMappingURL=LimitedCacheObject.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LimitedCacheObject.js","sourceRoot":"","sources":["../../src/core/LimitedCacheObject.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EACL,YAAY,EACZ,cAAc,EACd,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,GACf,MAAM,wBAAwB,CAAC;AAQhC,uFAAuF;AACvF,8DAA8D;AAC9D,MAAM,YAAY,GAAkD;IAClE,GAAG,EAAE,CAAC,SAA2B,EAAE,QAAgB,EAAE,EAAE;QACrD,IAAI,QAAQ,KAAK,gBAAgB,EAAE;YACjC,OAAO,cAAc,CAAC;SACvB;QACD,OAAO,cAAc,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAC7C,CAAC;IACD,wBAAwB,EAAE,CAAC,SAA2B,EAAE,QAAgB,EAAE,EAAE;QAC1E,MAAM,SAAS,GAAG,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACnD,MAAM,SAAS,GAAG,cAAc,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAEtD,IAAI,SAAS,EAAE;YACb,OAAO;gBACL,YAAY,EAAE,IAAI;gBAClB,UAAU,EAAE,SAAS;gBACrB,KAAK,EAAE,SAAS;gBAChB,QAAQ,EAAE,IAAI;aACf,CAAC;SACH;QACD,OAAO;IACT,CAAC;IACD,GAAG,EAAE,WAAW;IAChB,8DAA8D;IAC9D,GAAG,EAAE,CAAC,SAA2B,EAAE,QAAgB,EAAE,IAAS,EAAO,EAAE;QACrE,WAAW,CAAC,SAAS,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,cAAc,EAAE,CAAC,SAA2B,EAAE,QAAgB,EAAQ,EAAE;QACtE,cAAc,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QACpC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,EAAE,CAAC,SAA2B,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;CACjF,CAAC;AAEF;;;GAGG;AACH,MAAM,oBAAoB,GAAG,IAAI,OAAO,EAAE,CAAC;AAE3C,MAAM,kBAAkB,GAAG,CACzB,OAA6B,EACS,EAAE;IACxC,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,kBAAkB,GAAG,IAAI,KAAK,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;IAE9D,oBAAoB,CAAC,GAAG,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAAC;IACxD,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AAEF,MAAM,sBAAsB,GAAG,CAAC,QAAoC,EAAoB,EAAE;IACxF,OAAO,oBAAoB,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,CAAC"}
@@ -1,12 +0,0 @@
1
- declare const objectCreate: {
2
- (o: object | null): any;
3
- (o: object | null, properties: PropertyDescriptorMap & ThisType<any>): any;
4
- }, objectAssign: {
5
- <T extends {}, U>(target: T, source: U): T & U;
6
- <T_1 extends {}, U_1, V>(target: T_1, source1: U_1, source2: V): T_1 & U_1 & V;
7
- <T_2 extends {}, U_2, V_1, W>(target: T_2, source1: U_2, source2: V_1, source3: W): T_2 & U_2 & V_1 & W;
8
- (target: object, ...sources: any[]): any;
9
- }, hasOwnProperty: (v: PropertyKey) => boolean;
10
- declare const dateNow: () => number;
11
- export { objectAssign, objectCreate, dateNow, hasOwnProperty };
12
- //# sourceMappingURL=builtIns.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"builtIns.d.ts","sourceRoot":"","sources":["../../src/core/builtIns.ts"],"names":[],"mappings":"AACA,QAAA,MACU,YAAY;;;GACZ,YAAY;;;;;GACP,cAAc,6BACnB,CAAC;AACX,QAAA,MAAM,OAAO,cAAW,CAAC;AAEzB,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC"}
@@ -1,5 +0,0 @@
1
- // To help minification
2
- const { create: objectCreate, assign: objectAssign, prototype: { hasOwnProperty }, } = Object;
3
- const dateNow = Date.now;
4
- export { objectAssign, objectCreate, dateNow, hasOwnProperty };
5
- //# sourceMappingURL=builtIns.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"builtIns.js","sourceRoot":"","sources":["../../src/core/builtIns.ts"],"names":[],"mappings":"AAAA,uBAAuB;AACvB,MAAM,EACJ,MAAM,EAAE,YAAY,EACpB,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,EAAE,cAAc,EAAE,GAC9B,GAAG,MAAM,CAAC;AACX,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC;AAEzB,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC"}
@@ -1,6 +0,0 @@
1
- import { LimitedCacheOptionsReadonly } from '../types.js';
2
- declare const CURRENT_META_VERSION = 2;
3
- declare const MAXIMUM_CACHE_TIME: number;
4
- declare const defaultOptions: LimitedCacheOptionsReadonly;
5
- export { CURRENT_META_VERSION, MAXIMUM_CACHE_TIME, defaultOptions };
6
- //# sourceMappingURL=defaultOptions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defaultOptions.d.ts","sourceRoot":"","sources":["../../src/core/defaultOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAE1D,QAAA,MAAM,oBAAoB,IAAI,CAAC;AAC/B,QAAA,MAAM,kBAAkB,QAAqB,CAAC;AAE9C,QAAA,MAAM,cAAc,EAAE,2BASrB,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,cAAc,EAAE,CAAC"}
@@ -1,14 +0,0 @@
1
- const CURRENT_META_VERSION = 2;
2
- const MAXIMUM_CACHE_TIME = 365 * 86400 * 1000;
3
- const defaultOptions = {
4
- // Public
5
- maxCacheSize: 100,
6
- maxCacheTime: 86400 * 1000,
7
- // Development-only
8
- warnIfItemPurgedBeforeTime: 5000,
9
- // Private
10
- opLimit: 200,
11
- scanLimit: 50,
12
- };
13
- export { CURRENT_META_VERSION, MAXIMUM_CACHE_TIME, defaultOptions };
14
- //# sourceMappingURL=defaultOptions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defaultOptions.js","sourceRoot":"","sources":["../../src/core/defaultOptions.ts"],"names":[],"mappings":"AAEA,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAC/B,MAAM,kBAAkB,GAAG,GAAG,GAAG,KAAK,GAAG,IAAI,CAAC;AAE9C,MAAM,cAAc,GAAgC;IAClD,SAAS;IACT,YAAY,EAAE,GAAG;IACjB,YAAY,EAAE,KAAK,GAAG,IAAI;IAC1B,mBAAmB;IACnB,0BAA0B,EAAE,IAAI;IAChC,UAAU;IACV,OAAO,EAAE,GAAG;IACZ,SAAS,EAAE,EAAE;CACd,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,cAAc,EAAE,CAAC"}
@@ -1,13 +0,0 @@
1
- declare const limitedCacheUtil: {
2
- init: <ItemType = any>(optionsOrCacheMeta?: import("../types.js").LimitedCacheOptions | import("../types.js").LimitedCacheMeta<ItemType> | undefined) => import("../types.js").LimitedCacheMeta<ItemType>;
3
- get: <ItemType_1 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_1>, cacheKey: string) => ItemType_1 | undefined;
4
- getAll: <ItemType_2 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_2>) => Record<string, ItemType_2>;
5
- has: <ItemType_3 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_3>, cacheKey: string) => boolean;
6
- set: <ItemType_4 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_4>, cacheKey: string, item: ItemType_4) => import("../types.js").LimitedCacheMeta<ItemType_4>;
7
- remove: <ItemType_5 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_5>, cacheKey: string) => import("../types.js").LimitedCacheMeta<ItemType_5>;
8
- reset: <ItemType_6 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_6>) => import("../types.js").LimitedCacheMeta<ItemType_6>;
9
- doMaintenance: <ItemType_7 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_7>) => import("../types.js").LimitedCacheMeta<ItemType_7>;
10
- setOptions: <ItemType_8 = any>(cacheMeta: import("../types.js").LimitedCacheMeta<ItemType_8>, options: import("../types.js").LimitedCacheOptions) => Readonly<import("../types.js").LimitedCacheOptionsFull>;
11
- };
12
- export { limitedCacheUtil };
13
- //# sourceMappingURL=limitedCacheUtil.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"limitedCacheUtil.d.ts","sourceRoot":"","sources":["../../src/core/limitedCacheUtil.ts"],"names":[],"mappings":"AAYA,QAAA,MAAM,gBAAgB;;;;;;;;;;CAUrB,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1,14 +0,0 @@
1
- import { lowLevelGetOne, lowLevelGetAll, lowLevelHas, lowLevelInit, lowLevelDoMaintenance, lowLevelRemove, lowLevelReset, lowLevelSet, lowLevelSetOptions, } from './lowLevelFunctions.js';
2
- const limitedCacheUtil = {
3
- init: lowLevelInit,
4
- get: lowLevelGetOne,
5
- getAll: lowLevelGetAll,
6
- has: lowLevelHas,
7
- set: lowLevelSet,
8
- remove: lowLevelRemove,
9
- reset: lowLevelReset,
10
- doMaintenance: lowLevelDoMaintenance,
11
- setOptions: lowLevelSetOptions,
12
- };
13
- export { limitedCacheUtil };
14
- //# sourceMappingURL=limitedCacheUtil.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"limitedCacheUtil.js","sourceRoot":"","sources":["../../src/core/limitedCacheUtil.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,WAAW,EACX,YAAY,EACZ,qBAAqB,EACrB,cAAc,EACd,aAAa,EACb,WAAW,EACX,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAEhC,MAAM,gBAAgB,GAAG;IACvB,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,cAAc;IACnB,MAAM,EAAE,cAAc;IACtB,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,WAAW;IAChB,MAAM,EAAE,cAAc;IACtB,KAAK,EAAE,aAAa;IACpB,aAAa,EAAE,qBAAqB;IACpC,UAAU,EAAE,kBAAkB;CAC/B,CAAC;AAEF,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1,14 +0,0 @@
1
- import { LimitedCacheOptions, LimitedCacheOptionsReadonly, LimitedCacheMeta } from '../types.js';
2
- declare const isCacheMeta: (cacheMeta: LimitedCacheMeta) => boolean;
3
- declare const upgradeCacheMeta: (cacheMeta: LimitedCacheMeta) => void;
4
- declare const lowLevelSetOptions: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, options: LimitedCacheOptions) => LimitedCacheOptionsReadonly;
5
- declare const lowLevelInit: <ItemType = any>(optionsOrCacheMeta?: LimitedCacheOptions | LimitedCacheMeta<ItemType> | undefined) => LimitedCacheMeta<ItemType>;
6
- declare const lowLevelDoMaintenance: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
7
- declare const lowLevelHas: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => boolean;
8
- declare const lowLevelGetOne: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => ItemType | undefined;
9
- declare const lowLevelGetAll: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>) => Record<string, ItemType>;
10
- declare const lowLevelSet: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string, item: ItemType) => LimitedCacheMeta<ItemType>;
11
- declare const lowLevelRemove: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>, cacheKey: string) => LimitedCacheMeta<ItemType>;
12
- declare const lowLevelReset: <ItemType = any>(cacheMeta: LimitedCacheMeta<ItemType>) => LimitedCacheMeta<ItemType>;
13
- export { isCacheMeta, upgradeCacheMeta, lowLevelInit, lowLevelGetOne, lowLevelGetAll, lowLevelHas, lowLevelSet, lowLevelRemove, lowLevelReset, lowLevelDoMaintenance, lowLevelSetOptions, };
14
- //# sourceMappingURL=lowLevelFunctions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"lowLevelFunctions.d.ts","sourceRoot":"","sources":["../../src/core/lowLevelFunctions.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,gBAAgB,EAGjB,MAAM,aAAa,CAAC;AAqBrB,QAAA,MAAM,WAAW,cAAe,gBAAgB,KAAG,OAElD,CAAC;AAEF,QAAA,MAAM,gBAAgB,cAAe,gBAAgB,KAAG,IAWvD,CAAC;AAEF,QAAA,MAAM,kBAAkB,mEAEb,mBAAmB,KAC3B,2BAGF,CAAC;AAEF,QAAA,MAAM,YAAY,mIAkBjB,CAAC;AA0BF,QAAA,MAAM,qBAAqB,uFA+B1B,CAAC;AA4FF,QAAA,MAAM,WAAW,oEAEL,MAAM,KACf,OAWF,CAAC;AAEF,QAAA,MAAM,cAAc,oEAER,MAAM,yBAOjB,CAAC;AAEF,QAAA,MAAM,cAAc,qFAQnB,CAAC;AAEF,QAAA,MAAM,WAAW,oEAEL,MAAM,+CA+CjB,CAAC;AAEF,QAAA,MAAM,cAAc,oEAER,MAAM,+BAgBjB,CAAC;AAEF,QAAA,MAAM,aAAa,uFAUlB,CAAC;AAEF,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,EACd,aAAa,EACb,qBAAqB,EACrB,kBAAkB,GACnB,CAAC"}