@nocobase/plugin-workflow 2.1.0-beta.15 → 2.1.0-beta.17

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 (125) hide show
  1. package/dist/client/214.7e602cfe7a8251b8.js +10 -0
  2. package/dist/client/261.7722d7400942730e.js +10 -0
  3. package/dist/client/67.f904ef4520868b8a.js +10 -0
  4. package/dist/client/964.6251d37b35710747.js +10 -0
  5. package/dist/client/index.js +1 -1
  6. package/dist/client/nodes/create.d.ts +10 -0
  7. package/dist/client/nodes/destroy.d.ts +10 -0
  8. package/dist/client/nodes/index.d.ts +3 -0
  9. package/dist/client/nodes/query.d.ts +10 -0
  10. package/dist/client/nodes/update.d.ts +10 -0
  11. package/dist/client/triggers/collection.d.ts +14 -1
  12. package/dist/client/triggers/index.d.ts +4 -0
  13. package/dist/client/triggers/schedule/constants.d.ts +4 -0
  14. package/dist/client/triggers/schedule/index.d.ts +15 -0
  15. package/dist/common/collections/jobs.js +7 -0
  16. package/dist/externalVersion.js +12 -12
  17. package/dist/locale/zh-CN.json +1 -0
  18. package/dist/node_modules/cron-parser/lib/parser.js +1 -1
  19. package/dist/node_modules/cron-parser/package.json +1 -1
  20. package/dist/node_modules/joi/dist/joi-browser.min.js +1 -0
  21. package/dist/node_modules/joi/lib/annotate.js +175 -0
  22. package/dist/node_modules/joi/lib/base.js +1069 -0
  23. package/dist/node_modules/joi/lib/cache.js +143 -0
  24. package/dist/node_modules/joi/lib/common.js +216 -0
  25. package/dist/node_modules/joi/lib/compile.js +283 -0
  26. package/dist/node_modules/joi/lib/errors.js +271 -0
  27. package/dist/node_modules/joi/lib/extend.js +312 -0
  28. package/dist/node_modules/joi/lib/index.d.ts +2365 -0
  29. package/dist/node_modules/joi/lib/index.js +1 -0
  30. package/dist/node_modules/joi/lib/manifest.js +476 -0
  31. package/dist/node_modules/joi/lib/messages.js +178 -0
  32. package/dist/node_modules/joi/lib/modify.js +267 -0
  33. package/dist/node_modules/joi/lib/ref.js +414 -0
  34. package/dist/node_modules/joi/lib/schemas.js +302 -0
  35. package/dist/node_modules/joi/lib/state.js +166 -0
  36. package/dist/node_modules/joi/lib/template.js +463 -0
  37. package/dist/node_modules/joi/lib/trace.js +346 -0
  38. package/dist/node_modules/joi/lib/types/alternatives.js +364 -0
  39. package/dist/node_modules/joi/lib/types/any.js +174 -0
  40. package/dist/node_modules/joi/lib/types/array.js +809 -0
  41. package/dist/node_modules/joi/lib/types/binary.js +100 -0
  42. package/dist/node_modules/joi/lib/types/boolean.js +150 -0
  43. package/dist/node_modules/joi/lib/types/date.js +233 -0
  44. package/dist/node_modules/joi/lib/types/function.js +93 -0
  45. package/dist/node_modules/joi/lib/types/keys.js +1067 -0
  46. package/dist/node_modules/joi/lib/types/link.js +168 -0
  47. package/dist/node_modules/joi/lib/types/number.js +363 -0
  48. package/dist/node_modules/joi/lib/types/object.js +22 -0
  49. package/dist/node_modules/joi/lib/types/string.js +850 -0
  50. package/dist/node_modules/joi/lib/types/symbol.js +102 -0
  51. package/dist/node_modules/joi/lib/validator.js +750 -0
  52. package/dist/node_modules/joi/lib/values.js +263 -0
  53. package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.d.ts +60 -0
  54. package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.js +225 -0
  55. package/dist/node_modules/joi/node_modules/@hapi/topo/package.json +30 -0
  56. package/dist/node_modules/joi/package.json +1 -0
  57. package/dist/node_modules/lru-cache/dist/commonjs/diagnostics-channel.d.ts +5 -0
  58. package/dist/node_modules/lru-cache/dist/commonjs/diagnostics-channel.js +10 -0
  59. package/dist/node_modules/lru-cache/dist/commonjs/index.d.ts +1381 -0
  60. package/dist/node_modules/lru-cache/dist/commonjs/index.js +1692 -0
  61. package/dist/node_modules/lru-cache/dist/commonjs/index.min.js +1 -0
  62. package/dist/node_modules/lru-cache/dist/esm/browser/diagnostics-channel.d.ts +5 -0
  63. package/dist/node_modules/lru-cache/dist/esm/browser/diagnostics-channel.js +4 -0
  64. package/dist/node_modules/lru-cache/dist/esm/browser/index.d.ts +1381 -0
  65. package/dist/node_modules/lru-cache/dist/{mjs → esm/browser}/index.js +537 -179
  66. package/dist/node_modules/lru-cache/dist/esm/browser/index.min.js +2 -0
  67. package/dist/node_modules/lru-cache/dist/esm/diagnostics-channel.d.ts +5 -0
  68. package/dist/node_modules/lru-cache/dist/esm/diagnostics-channel.js +19 -0
  69. package/dist/node_modules/lru-cache/dist/esm/index.d.ts +1381 -0
  70. package/dist/node_modules/lru-cache/dist/{cjs → esm}/index.js +538 -184
  71. package/dist/node_modules/lru-cache/dist/esm/index.min.js +2 -0
  72. package/dist/node_modules/lru-cache/dist/esm/node/diagnostics-channel.d.ts +5 -0
  73. package/dist/node_modules/lru-cache/dist/esm/node/diagnostics-channel.js +7 -0
  74. package/dist/node_modules/lru-cache/dist/esm/node/index.d.ts +1381 -0
  75. package/dist/node_modules/lru-cache/dist/esm/node/index.js +1688 -0
  76. package/dist/node_modules/lru-cache/dist/esm/node/index.min.js +2 -0
  77. package/dist/node_modules/lru-cache/package.json +1 -1
  78. package/dist/node_modules/nodejs-snowflake/nodejs_snowflake.js +1 -1
  79. package/dist/node_modules/nodejs-snowflake/package.json +1 -1
  80. package/dist/server/Plugin.d.ts +1 -0
  81. package/dist/server/Plugin.js +28 -3
  82. package/dist/server/actions/nodes.d.ts +5 -0
  83. package/dist/server/actions/nodes.js +38 -5
  84. package/dist/server/actions/workflows.d.ts +6 -0
  85. package/dist/server/actions/workflows.js +38 -0
  86. package/dist/server/instructions/ConditionInstruction.d.ts +2 -0
  87. package/dist/server/instructions/ConditionInstruction.js +17 -0
  88. package/dist/server/instructions/CreateInstruction.d.ts +3 -0
  89. package/dist/server/instructions/CreateInstruction.js +25 -0
  90. package/dist/server/instructions/DestroyInstruction.d.ts +3 -0
  91. package/dist/server/instructions/DestroyInstruction.js +25 -0
  92. package/dist/server/instructions/EndInstruction.d.ts +2 -0
  93. package/dist/server/instructions/EndInstruction.js +4 -0
  94. package/dist/server/instructions/MultiConditionsInstruction.d.ts +2 -0
  95. package/dist/server/instructions/MultiConditionsInstruction.js +23 -0
  96. package/dist/server/instructions/OutputInstruction.d.ts +2 -0
  97. package/dist/server/instructions/OutputInstruction.js +14 -0
  98. package/dist/server/instructions/QueryInstruction.d.ts +3 -0
  99. package/dist/server/instructions/QueryInstruction.js +30 -0
  100. package/dist/server/instructions/UpdateInstruction.d.ts +3 -0
  101. package/dist/server/instructions/UpdateInstruction.js +27 -0
  102. package/dist/server/instructions/index.d.ts +6 -1
  103. package/dist/server/instructions/index.js +18 -0
  104. package/dist/server/triggers/CollectionTrigger.d.ts +3 -0
  105. package/dist/server/triggers/CollectionTrigger.js +28 -0
  106. package/dist/server/triggers/ScheduleTrigger/index.d.ts +3 -0
  107. package/dist/server/triggers/ScheduleTrigger/index.js +18 -3
  108. package/dist/server/triggers/index.d.ts +3 -0
  109. package/dist/server/triggers/index.js +18 -0
  110. package/dist/server/utils.d.ts +2 -0
  111. package/dist/server/utils.js +22 -2
  112. package/package.json +4 -3
  113. package/dist/client/10.54a0831f49cae121.js +0 -10
  114. package/dist/client/626.7d24ff2a47742f1b.js +0 -10
  115. package/dist/client/771.5e7be8b4d4ac579f.js +0 -10
  116. package/dist/client/781.d46db08dcfead1b0.js +0 -10
  117. package/dist/node_modules/lru-cache/LICENSE +0 -15
  118. package/dist/node_modules/lru-cache/dist/cjs/index-cjs.d.ts +0 -7
  119. package/dist/node_modules/lru-cache/dist/cjs/index-cjs.js +0 -1
  120. package/dist/node_modules/lru-cache/dist/cjs/index.d.ts +0 -807
  121. package/dist/node_modules/lru-cache/dist/cjs/index.min.js +0 -2
  122. package/dist/node_modules/lru-cache/dist/mjs/index.d.ts +0 -807
  123. package/dist/node_modules/lru-cache/dist/mjs/index.min.js +0 -2
  124. /package/dist/node_modules/lru-cache/dist/{cjs → commonjs}/package.json +0 -0
  125. /package/dist/node_modules/lru-cache/dist/{mjs → esm}/package.json +0 -0
@@ -0,0 +1,1381 @@
1
+ /**
2
+ * @module LRUCache
3
+ */
4
+ export type Perf = {
5
+ now: () => number;
6
+ };
7
+ declare const TYPE: unique symbol;
8
+ export type PosInt = number & {
9
+ [TYPE]: 'Positive Integer';
10
+ };
11
+ export type Index = number & {
12
+ [TYPE]: 'LRUCache Index';
13
+ };
14
+ export type UintArray = Uint8Array | Uint16Array | Uint32Array;
15
+ export type NumberArray = UintArray | number[];
16
+ declare class ZeroArray extends Array<number> {
17
+ constructor(size: number);
18
+ }
19
+ export type { ZeroArray };
20
+ export type { Stack };
21
+ export type StackLike = Stack | Index[];
22
+ declare class Stack {
23
+ #private;
24
+ heap: NumberArray;
25
+ length: number;
26
+ static create(max: number): StackLike;
27
+ constructor(max: number, HeapCls: {
28
+ new (n: number): NumberArray;
29
+ });
30
+ push(n: Index): void;
31
+ pop(): Index;
32
+ }
33
+ /**
34
+ * Promise representing an in-progress {@link LRUCache#fetch} call
35
+ */
36
+ export type BackgroundFetch<V> = Promise<V | undefined> & {
37
+ __returned: BackgroundFetch<V> | undefined;
38
+ __abortController: AbortController;
39
+ __staleWhileFetching: V | undefined;
40
+ };
41
+ export type DisposeTask<K, V> = [
42
+ value: V,
43
+ key: K,
44
+ reason: LRUCache.DisposeReason
45
+ ];
46
+ export declare namespace LRUCache {
47
+ /**
48
+ * An integer greater than 0, reflecting the calculated size of items
49
+ */
50
+ type Size = number;
51
+ /**
52
+ * Integer greater than 0, representing some number of milliseconds, or the
53
+ * time at which a TTL started counting from.
54
+ */
55
+ type Milliseconds = number;
56
+ /**
57
+ * An integer greater than 0, reflecting a number of items
58
+ */
59
+ type Count = number;
60
+ /**
61
+ * The reason why an item was removed from the cache, passed
62
+ * to the {@link Disposer} methods.
63
+ *
64
+ * - `evict`: The item was evicted because it is the least recently used,
65
+ * and the cache is full.
66
+ * - `set`: A new value was set, overwriting the old value being disposed.
67
+ * - `delete`: The item was explicitly deleted, either by calling
68
+ * {@link LRUCache#delete}, {@link LRUCache#clear}, or
69
+ * {@link LRUCache#set} with an undefined value.
70
+ * - `expire`: The item was removed due to exceeding its TTL.
71
+ * - `fetch`: A {@link OptionsBase#fetchMethod} operation returned
72
+ * `undefined` or was aborted, causing the item to be deleted.
73
+ */
74
+ type DisposeReason = 'evict' | 'set' | 'delete' | 'expire' | 'fetch';
75
+ /**
76
+ * A method called upon item removal, passed as the
77
+ * {@link OptionsBase.dispose} and/or
78
+ * {@link OptionsBase.disposeAfter} options.
79
+ */
80
+ type Disposer<K, V> = (value: V, key: K, reason: DisposeReason) => void;
81
+ /**
82
+ * The reason why an item was added to the cache, passed
83
+ * to the {@link Inserter} methods.
84
+ *
85
+ * - `add`: the item was not found in the cache, and was added
86
+ * - `update`: the item was in the cache, with the same value provided
87
+ * - `replace`: the item was in the cache, and replaced
88
+ */
89
+ type InsertReason = 'add' | 'update' | 'replace';
90
+ /**
91
+ * A method called upon item insertion, passed as the
92
+ * {@link OptionsBase.insert}
93
+ */
94
+ type Inserter<K, V> = (value: V, key: K, reason: InsertReason) => void;
95
+ /**
96
+ * A function that returns the effective calculated size
97
+ * of an entry in the cache.
98
+ */
99
+ type SizeCalculator<K, V> = (value: V, key: K) => Size;
100
+ /**
101
+ * Options provided to the
102
+ * {@link OptionsBase.fetchMethod} function.
103
+ */
104
+ interface FetcherOptions<K, V, FC = unknown> {
105
+ signal: AbortSignal;
106
+ options: FetcherFetchOptions<K, V, FC>;
107
+ /**
108
+ * Object provided in the {@link FetchOptions.context} option to
109
+ * {@link LRUCache#fetch}
110
+ */
111
+ context: FC;
112
+ }
113
+ /**
114
+ * Occasionally, it may be useful to track the internal behavior of the
115
+ * cache, particularly for logging, debugging, or for behavior within the
116
+ * `fetchMethod`. To do this, you can pass a `status` object to the
117
+ * {@link LRUCache#fetch}, {@link LRUCache#get}, {@link LRUCache#set},
118
+ * {@link LRUCache#memo}, and {@link LRUCache#has} methods.
119
+ *
120
+ * The `status` option should be a plain JavaScript object. The following
121
+ * fields will be set on it appropriately, depending on the situation.
122
+ *
123
+ * These objects are also the context objects passed to listeners on the
124
+ * `lru-cache:metrics` diagnostic channel, and the `lru-cache` tracing
125
+ * channels, in platforms that support them.
126
+ */
127
+ interface Status<K, V> {
128
+ /**
129
+ * The operation being performed
130
+ */
131
+ op?: 'get' | 'set' | 'memo' | 'fetch' | 'delete' | 'has' | 'peek';
132
+ /**
133
+ * The status of a set() operation.
134
+ *
135
+ * - add: the item was not found in the cache, and was added
136
+ * - update: the item was in the cache, with the same value provided
137
+ * - replace: the item was in the cache, and replaced
138
+ * - miss: the item was not added to the cache for some reason
139
+ */
140
+ set?: 'add' | 'update' | 'replace' | 'miss' | 'deleted';
141
+ /**
142
+ * The status of a delete() operation.
143
+ */
144
+ delete?: LRUCache.DisposeReason;
145
+ /**
146
+ * The result of a peek() operation
147
+ *
148
+ * - hit: the item was found and returned
149
+ * - stale: the item is in the cache, but past its ttl and not returned
150
+ * - miss: item not in the cache
151
+ */
152
+ peek?: 'hit' | 'miss' | 'stale';
153
+ /**
154
+ * The status of a memo() operation.
155
+ *
156
+ * - 'hit': the item was found in the cache and returned
157
+ * - 'miss': the `memoMethod` function was called
158
+ */
159
+ memo?: 'hit' | 'miss';
160
+ /**
161
+ * The `context` option provided to a memo or fetch operation
162
+ *
163
+ * In practice, of course, this will be the same type as the `FC`
164
+ * fetch context param used to instantiate the LRUCache, but the
165
+ * convolutions of threading that through would get quite complicated,
166
+ * and preclude forcing/forbidding the passing of a `context` param
167
+ * where it is/isn't expected, which is more valuable for error
168
+ * prevention.
169
+ */
170
+ context?: unknown;
171
+ /**
172
+ * the ttl stored for the item, or undefined if ttls are not used.
173
+ */
174
+ ttl?: Milliseconds;
175
+ /**
176
+ * the start time for the item, or undefined if ttls are not used.
177
+ */
178
+ start?: Milliseconds;
179
+ /**
180
+ * The timestamp used for TTL calculation
181
+ */
182
+ now?: Milliseconds;
183
+ /**
184
+ * the remaining ttl for the item, or undefined if ttls are not used.
185
+ */
186
+ remainingTTL?: Milliseconds;
187
+ /**
188
+ * The calculated size for the item, if sizes are used.
189
+ */
190
+ entrySize?: Size;
191
+ /**
192
+ * The total calculated size of the cache, if sizes are used.
193
+ */
194
+ totalCalculatedSize?: Size;
195
+ /**
196
+ * A flag indicating that the item was not stored, due to exceeding the
197
+ * {@link OptionsBase.maxEntrySize}
198
+ */
199
+ maxEntrySizeExceeded?: true;
200
+ /**
201
+ * The key that was set or retrieved
202
+ */
203
+ key?: K;
204
+ /**
205
+ * The value that was set
206
+ */
207
+ value?: V;
208
+ /**
209
+ * The old value, specified in the case of `set:'replace'`
210
+ */
211
+ oldValue?: V;
212
+ /**
213
+ * The results of a {@link LRUCache#has} operation
214
+ *
215
+ * - hit: the item was found in the cache
216
+ * - stale: the item was found in the cache, but is stale
217
+ * - miss: the item was not found in the cache
218
+ */
219
+ has?: 'hit' | 'stale' | 'miss';
220
+ /**
221
+ * The status of a {@link LRUCache#fetch} operation.
222
+ * Note that this can change as the underlying fetch() moves through
223
+ * various states.
224
+ *
225
+ * - inflight: there is another fetch() for this key which is in process
226
+ * - get: there is no {@link OptionsBase.fetchMethod}, so
227
+ * {@link LRUCache#get} was called.
228
+ * - miss: the item is not in cache, and will be fetched.
229
+ * - hit: the item is in the cache, and was resolved immediately.
230
+ * - stale: the item is in the cache, but stale.
231
+ * - refresh: the item is in the cache, and not stale, but
232
+ * {@link FetchOptions.forceRefresh} was specified.
233
+ */
234
+ fetch?: 'get' | 'inflight' | 'miss' | 'hit' | 'stale' | 'refresh';
235
+ /**
236
+ * `forceRefresh` option was used for either a fetch or memo operation
237
+ */
238
+ forceRefresh?: boolean;
239
+ /**
240
+ * The {@link OptionsBase.fetchMethod} was called
241
+ */
242
+ fetchDispatched?: true;
243
+ /**
244
+ * The cached value was updated after a successful call to
245
+ * {@link OptionsBase.fetchMethod}
246
+ */
247
+ fetchUpdated?: true;
248
+ /**
249
+ * The reason for a fetch() rejection. Either the error raised by the
250
+ * {@link OptionsBase.fetchMethod}, or the reason for an
251
+ * AbortSignal.
252
+ */
253
+ fetchError?: Error;
254
+ /**
255
+ * The fetch received an abort signal
256
+ */
257
+ fetchAborted?: true;
258
+ /**
259
+ * The abort signal received was ignored, and the fetch was allowed to
260
+ * continue in the background.
261
+ */
262
+ fetchAbortIgnored?: true;
263
+ /**
264
+ * The fetchMethod promise resolved successfully
265
+ */
266
+ fetchResolved?: true;
267
+ /**
268
+ * The fetchMethod promise was rejected
269
+ */
270
+ fetchRejected?: true;
271
+ /**
272
+ * The status of a {@link LRUCache#get} operation.
273
+ *
274
+ * - fetching: The item is currently being fetched. If a previous value
275
+ * is present and allowed, that will be returned.
276
+ * - stale: The item is in the cache, and is stale. If it was returned,
277
+ * then the `returnedStale` flag will be set.
278
+ * - stale-fetching: The value is being fetched in the background, but is
279
+ * currently stale. If the stale value was returned, then the
280
+ * `returnedStale` flag will be set.
281
+ * - hit: the item is in the cache
282
+ * - miss: the item is not in the cache
283
+ */
284
+ get?: 'stale' | 'hit' | 'miss' | 'fetching' | 'stale-fetching';
285
+ /**
286
+ * A fetch or get operation returned a stale value.
287
+ */
288
+ returnedStale?: true;
289
+ /**
290
+ * A tracingChannel trace was started for this operation
291
+ */
292
+ trace?: boolean;
293
+ }
294
+ /**
295
+ * options which override the options set in the LRUCache constructor
296
+ * when calling {@link LRUCache#fetch}.
297
+ *
298
+ * This is the union of {@link GetOptions} and {@link SetOptions}, plus
299
+ * {@link OptionsBase.noDeleteOnFetchRejection},
300
+ * {@link OptionsBase.allowStaleOnFetchRejection},
301
+ * {@link FetchOptions.forceRefresh}, and
302
+ * {@link FetcherOptions.context}
303
+ *
304
+ * Any of these may be modified in the {@link OptionsBase.fetchMethod}
305
+ * function, but the {@link GetOptions} fields will of course have no
306
+ * effect, as the {@link LRUCache#get} call already happened by the time
307
+ * the fetchMethod is called.
308
+ */
309
+ interface FetcherFetchOptions<K, V, FC = unknown> extends Pick<OptionsBase<K, V, FC>, 'allowStale' | 'updateAgeOnGet' | 'noDeleteOnStaleGet' | 'sizeCalculation' | 'ttl' | 'noDisposeOnSet' | 'noUpdateTTL' | 'noDeleteOnFetchRejection' | 'allowStaleOnFetchRejection' | 'ignoreFetchAbort' | 'allowStaleOnFetchAbort'> {
310
+ status?: Status<K, V>;
311
+ size?: Size;
312
+ }
313
+ /**
314
+ * Options that may be passed to the {@link LRUCache#fetch} method.
315
+ */
316
+ interface FetchOptions<K, V, FC> extends FetcherFetchOptions<K, V, FC> {
317
+ /**
318
+ * Set to true to force a re-load of the existing data, even if it
319
+ * is not yet stale.
320
+ */
321
+ forceRefresh?: boolean;
322
+ /**
323
+ * Context provided to the {@link OptionsBase.fetchMethod} as
324
+ * the {@link FetcherOptions.context} param.
325
+ *
326
+ * If the FC type is specified as unknown (the default),
327
+ * undefined or void, then this is optional. Otherwise, it will
328
+ * be required.
329
+ */
330
+ context?: FC;
331
+ signal?: AbortSignal;
332
+ status?: Status<K, V>;
333
+ }
334
+ /**
335
+ * Options provided to {@link LRUCache#fetch} when the FC type is something
336
+ * other than `unknown`, `undefined`, or `void`
337
+ */
338
+ interface FetchOptionsWithContext<K, V, FC> extends FetchOptions<K, V, FC> {
339
+ context: FC;
340
+ }
341
+ /**
342
+ * Options provided to {@link LRUCache#fetch} when the FC type is
343
+ * `undefined` or `void`
344
+ */
345
+ interface FetchOptionsNoContext<K, V> extends FetchOptions<K, V, undefined> {
346
+ context?: undefined;
347
+ }
348
+ interface MemoOptions<K, V, FC = unknown> extends Pick<OptionsBase<K, V, FC>, 'allowStale' | 'updateAgeOnGet' | 'noDeleteOnStaleGet' | 'sizeCalculation' | 'ttl' | 'noDisposeOnSet' | 'noUpdateTTL' | 'noDeleteOnFetchRejection' | 'allowStaleOnFetchRejection' | 'ignoreFetchAbort' | 'allowStaleOnFetchAbort'> {
349
+ /**
350
+ * Set to true to force a re-load of the existing data, even if it
351
+ * is not yet stale.
352
+ */
353
+ forceRefresh?: boolean;
354
+ /**
355
+ * Context provided to the {@link OptionsBase.memoMethod} as
356
+ * the {@link MemoizerOptions.context} param.
357
+ *
358
+ * If the FC type is specified as unknown (the default),
359
+ * undefined or void, then this is optional. Otherwise, it will
360
+ * be required.
361
+ */
362
+ context?: FC;
363
+ status?: Status<K, V>;
364
+ }
365
+ /**
366
+ * Options provided to {@link LRUCache#memo} when the FC type is something
367
+ * other than `unknown`, `undefined`, or `void`
368
+ */
369
+ interface MemoOptionsWithContext<K, V, FC> extends MemoOptions<K, V, FC> {
370
+ context: FC;
371
+ }
372
+ /**
373
+ * Options provided to {@link LRUCache#memo} when the FC type is
374
+ * `undefined` or `void`
375
+ */
376
+ interface MemoOptionsNoContext<K, V> extends MemoOptions<K, V, undefined> {
377
+ context?: undefined;
378
+ }
379
+ /**
380
+ * Options provided to the
381
+ * {@link OptionsBase.memoMethod} function.
382
+ */
383
+ interface MemoizerOptions<K, V, FC = unknown> {
384
+ options: MemoizerMemoOptions<K, V, FC>;
385
+ /**
386
+ * Object provided in the {@link MemoOptions.context} option to
387
+ * {@link LRUCache#memo}
388
+ */
389
+ context: FC;
390
+ }
391
+ /**
392
+ * options which override the options set in the LRUCache constructor
393
+ * when calling {@link LRUCache#memo}.
394
+ *
395
+ * This is the union of {@link GetOptions} and {@link SetOptions}, plus
396
+ * {@link MemoOptions.forceRefresh}, and
397
+ * {@link MemoOptions.context}
398
+ *
399
+ * Any of these may be modified in the {@link OptionsBase.memoMethod}
400
+ * function, but the {@link GetOptions} fields will of course have no
401
+ * effect, as the {@link LRUCache#get} call already happened by the time
402
+ * the memoMethod is called.
403
+ */
404
+ interface MemoizerMemoOptions<K, V, FC = unknown> extends Pick<OptionsBase<K, V, FC>, 'allowStale' | 'updateAgeOnGet' | 'noDeleteOnStaleGet' | 'sizeCalculation' | 'ttl' | 'noDisposeOnSet' | 'noUpdateTTL'> {
405
+ status?: Status<K, V>;
406
+ size?: Size;
407
+ start?: Milliseconds;
408
+ }
409
+ /**
410
+ * Options that may be passed to the {@link LRUCache#has} method.
411
+ */
412
+ interface HasOptions<K, V, FC> extends Pick<OptionsBase<K, V, FC>, 'updateAgeOnHas'> {
413
+ status?: Status<K, V>;
414
+ }
415
+ /**
416
+ * Options that may be passed to the {@link LRUCache#get} method.
417
+ */
418
+ interface GetOptions<K, V, FC> extends Pick<OptionsBase<K, V, FC>, 'allowStale' | 'updateAgeOnGet' | 'noDeleteOnStaleGet'> {
419
+ status?: Status<K, V>;
420
+ }
421
+ /**
422
+ * Options that may be passed to the {@link LRUCache#peek} method.
423
+ */
424
+ interface PeekOptions<K, V, FC> extends Pick<OptionsBase<K, V, FC>, 'allowStale'> {
425
+ status?: Status<K, V>;
426
+ }
427
+ /**
428
+ * Options that may be passed to the {@link LRUCache#set} method.
429
+ */
430
+ interface SetOptions<K, V, FC> extends Pick<OptionsBase<K, V, FC>, 'sizeCalculation' | 'ttl' | 'noDisposeOnSet' | 'noUpdateTTL'> {
431
+ /**
432
+ * If size tracking is enabled, then setting an explicit size
433
+ * in the {@link LRUCache#set} call will prevent calling the
434
+ * {@link OptionsBase.sizeCalculation} function.
435
+ */
436
+ size?: Size;
437
+ /**
438
+ * If TTL tracking is enabled, then setting an explicit start
439
+ * time in the {@link LRUCache#set} call will override the
440
+ * default time from `performance.now()` or `Date.now()`.
441
+ *
442
+ * Note that it must be a valid value for whichever time-tracking
443
+ * method is in use.
444
+ */
445
+ start?: Milliseconds;
446
+ status?: Status<K, V>;
447
+ }
448
+ /**
449
+ * The type signature for the {@link OptionsBase.fetchMethod} option.
450
+ */
451
+ type Fetcher<K, V, FC = unknown> = (key: K, staleValue: V | undefined, options: FetcherOptions<K, V, FC>) => Promise<V | undefined | void> | V | undefined | void;
452
+ /**
453
+ * the type signature for the {@link OptionsBase.memoMethod} option.
454
+ */
455
+ type Memoizer<K, V, FC = unknown> = (key: K, staleValue: V | undefined, options: MemoizerOptions<K, V, FC>) => V;
456
+ /**
457
+ * Options which may be passed to the {@link LRUCache} constructor.
458
+ *
459
+ * Most of these may be overridden in the various options that use
460
+ * them.
461
+ *
462
+ * Despite all being technically optional, the constructor requires that
463
+ * a cache is at minimum limited by one or more of {@link OptionsBase.max},
464
+ * {@link OptionsBase.ttl}, or {@link OptionsBase.maxSize}.
465
+ *
466
+ * If {@link OptionsBase.ttl} is used alone, then it is strongly advised
467
+ * (and in fact required by the type definitions here) that the cache
468
+ * also set {@link OptionsBase.ttlAutopurge}, to prevent potentially
469
+ * unbounded storage.
470
+ *
471
+ * All options are also available on the {@link LRUCache} instance, making
472
+ * it safe to pass an LRUCache instance as the options argumemnt to
473
+ * make another empty cache of the same type.
474
+ *
475
+ * Some options are marked as read-only, because changing them after
476
+ * instantiation is not safe. Changing any of the other options will of
477
+ * course only have an effect on subsequent method calls.
478
+ */
479
+ interface OptionsBase<K, V, FC> {
480
+ /**
481
+ * The maximum number of items to store in the cache before evicting
482
+ * old entries. This is read-only on the {@link LRUCache} instance,
483
+ * and may not be overridden.
484
+ *
485
+ * If set, then storage space will be pre-allocated at construction
486
+ * time, and the cache will perform significantly faster.
487
+ *
488
+ * Note that significantly fewer items may be stored, if
489
+ * {@link OptionsBase.maxSize} and/or {@link OptionsBase.ttl} are also
490
+ * set.
491
+ *
492
+ * **It is strongly recommended to set a `max` to prevent unbounded growth
493
+ * of the cache.**
494
+ */
495
+ max?: Count;
496
+ /**
497
+ * Max time in milliseconds for items to live in cache before they are
498
+ * considered stale. Note that stale items are NOT preemptively removed by
499
+ * default, and MAY live in the cache, contributing to its LRU max, long
500
+ * after they have expired, unless {@link OptionsBase.ttlAutopurge} is
501
+ * set.
502
+ *
503
+ * If set to `0` (the default value), then that means "do not track
504
+ * TTL", not "expire immediately".
505
+ *
506
+ * Also, as this cache is optimized for LRU/MRU operations, some of
507
+ * the staleness/TTL checks will reduce performance, as they will incur
508
+ * overhead by deleting items.
509
+ *
510
+ * This is not primarily a TTL cache, and does not make strong TTL
511
+ * guarantees. There is no pre-emptive pruning of expired items, but you
512
+ * _may_ set a TTL on the cache, and it will treat expired items as missing
513
+ * when they are fetched, and delete them.
514
+ *
515
+ * Optional, but must be a non-negative integer in ms if specified.
516
+ *
517
+ * This may be overridden by passing an options object to `cache.set()`.
518
+ *
519
+ * At least one of `max`, `maxSize`, or `TTL` is required. This must be a
520
+ * positive integer if set.
521
+ *
522
+ * Even if ttl tracking is enabled, **it is strongly recommended to set a
523
+ * `max` to prevent unbounded growth of the cache.**
524
+ *
525
+ * If ttl tracking is enabled, and `max` and `maxSize` are not set,
526
+ * and `ttlAutopurge` is not set, then a warning will be emitted
527
+ * cautioning about the potential for unbounded memory consumption.
528
+ * (The TypeScript definitions will also discourage this.)
529
+ */
530
+ ttl?: Milliseconds;
531
+ /**
532
+ * Minimum amount of time in ms in which to check for staleness.
533
+ * Defaults to 1, which means that the current time is checked
534
+ * at most once per millisecond.
535
+ *
536
+ * Set to 0 to check the current time every time staleness is tested.
537
+ * (This reduces performance, and is theoretically unnecessary.)
538
+ *
539
+ * Setting this to a higher value will improve performance somewhat
540
+ * while using ttl tracking, albeit at the expense of keeping stale
541
+ * items around a bit longer than their TTLs would indicate.
542
+ *
543
+ * @default 1
544
+ */
545
+ ttlResolution?: Milliseconds;
546
+ /**
547
+ * Preemptively remove stale items from the cache.
548
+ *
549
+ * Note that this may *significantly* degrade performance, especially if
550
+ * the cache is storing a large number of items. It is almost always best
551
+ * to just leave the stale items in the cache, and let them fall out as new
552
+ * items are added.
553
+ *
554
+ * Note that this means that {@link OptionsBase.allowStale} is a bit
555
+ * pointless, as stale items will be deleted almost as soon as they
556
+ * expire.
557
+ *
558
+ * Use with caution!
559
+ */
560
+ ttlAutopurge?: boolean;
561
+ /**
562
+ * When using time-expiring entries with `ttl`, setting this to `true` will
563
+ * make each item's age reset to 0 whenever it is retrieved from cache with
564
+ * {@link LRUCache#get}, causing it to not expire. (It can still fall out
565
+ * of cache based on recency of use, of course.)
566
+ *
567
+ * Has no effect if {@link OptionsBase.ttl} is not set.
568
+ *
569
+ * This may be overridden by passing an options object to `cache.get()`.
570
+ */
571
+ updateAgeOnGet?: boolean;
572
+ /**
573
+ * When using time-expiring entries with `ttl`, setting this to `true` will
574
+ * make each item's age reset to 0 whenever its presence in the cache is
575
+ * checked with {@link LRUCache#has}, causing it to not expire. (It can
576
+ * still fall out of cache based on recency of use, of course.)
577
+ *
578
+ * Has no effect if {@link OptionsBase.ttl} is not set.
579
+ */
580
+ updateAgeOnHas?: boolean;
581
+ /**
582
+ * Allow {@link LRUCache#get} and {@link LRUCache#fetch} calls to return
583
+ * stale data, if available.
584
+ *
585
+ * By default, if you set `ttl`, stale items will only be deleted from the
586
+ * cache when you `get(key)`. That is, it's not preemptively pruning items,
587
+ * unless {@link OptionsBase.ttlAutopurge} is set.
588
+ *
589
+ * If you set `allowStale:true`, it'll return the stale value *as well as*
590
+ * deleting it. If you don't set this, then it'll return `undefined` when
591
+ * you try to get a stale entry.
592
+ *
593
+ * Note that when a stale entry is fetched, _even if it is returned due to
594
+ * `allowStale` being set_, it is removed from the cache immediately. You
595
+ * can suppress this behavior by setting
596
+ * {@link OptionsBase.noDeleteOnStaleGet}, either in the constructor, or in
597
+ * the options provided to {@link LRUCache#get}.
598
+ *
599
+ * This may be overridden by passing an options object to `cache.get()`.
600
+ * The `cache.has()` method will always return `false` for stale items.
601
+ *
602
+ * Only relevant if a ttl is set.
603
+ */
604
+ allowStale?: boolean;
605
+ /**
606
+ * Function that is called on items when they are dropped from the
607
+ * cache, as `dispose(value, key, reason)`.
608
+ *
609
+ * This can be handy if you want to close file descriptors or do
610
+ * other cleanup tasks when items are no longer stored in the cache.
611
+ *
612
+ * **NOTE**: It is called _before_ the item has been fully removed
613
+ * from the cache, so if you want to put it right back in, you need
614
+ * to wait until the next tick. If you try to add it back in during
615
+ * the `dispose()` function call, it will break things in subtle and
616
+ * weird ways.
617
+ *
618
+ * Unlike several other options, this may _not_ be overridden by
619
+ * passing an option to `set()`, for performance reasons.
620
+ *
621
+ * The `reason` will be one of the following strings, corresponding
622
+ * to the reason for the item's deletion:
623
+ *
624
+ * - `evict` Item was evicted to make space for a new addition
625
+ * - `set` Item was overwritten by a new value
626
+ * - `expire` Item expired its TTL
627
+ * - `fetch` Item was deleted due to a failed or aborted fetch, or a
628
+ * fetchMethod returning `undefined.
629
+ * - `delete` Item was removed by explicit `cache.delete(key)`,
630
+ * `cache.clear()`, or `cache.set(key, undefined)`.
631
+ */
632
+ dispose?: Disposer<K, V>;
633
+ /**
634
+ * Function that is called when new items are inserted into the cache,
635
+ * as `onInsert(value, key, reason)`.
636
+ *
637
+ * This can be useful if you need to perform actions when an item is
638
+ * added, such as logging or tracking insertions.
639
+ *
640
+ * Unlike some other options, this may _not_ be overridden by passing
641
+ * an option to `set()`, for performance and consistency reasons.
642
+ */
643
+ onInsert?: Inserter<K, V>;
644
+ /**
645
+ * The same as {@link OptionsBase.dispose}, but called *after* the entry
646
+ * is completely removed and the cache is once again in a clean state.
647
+ *
648
+ * It is safe to add an item right back into the cache at this point.
649
+ * However, note that it is *very* easy to inadvertently create infinite
650
+ * recursion this way.
651
+ */
652
+ disposeAfter?: Disposer<K, V>;
653
+ /**
654
+ * Set to true to suppress calling the
655
+ * {@link OptionsBase.dispose} function if the entry key is
656
+ * still accessible within the cache.
657
+ *
658
+ * This may be overridden by passing an options object to
659
+ * {@link LRUCache#set}.
660
+ *
661
+ * Only relevant if `dispose` or `disposeAfter` are set.
662
+ */
663
+ noDisposeOnSet?: boolean;
664
+ /**
665
+ * Boolean flag to tell the cache to not update the TTL when setting a new
666
+ * value for an existing key (ie, when updating a value rather than
667
+ * inserting a new value). Note that the TTL value is _always_ set (if
668
+ * provided) when adding a new entry into the cache.
669
+ *
670
+ * Has no effect if a {@link OptionsBase.ttl} is not set.
671
+ *
672
+ * May be passed as an option to {@link LRUCache#set}.
673
+ */
674
+ noUpdateTTL?: boolean;
675
+ /**
676
+ * Set to a positive integer to track the sizes of items added to the
677
+ * cache, and automatically evict items in order to stay below this size.
678
+ * Note that this may result in fewer than `max` items being stored.
679
+ *
680
+ * Attempting to add an item to the cache whose calculated size is greater
681
+ * that this amount will be a no-op. The item will not be cached, and no
682
+ * other items will be evicted.
683
+ *
684
+ * Optional, must be a positive integer if provided.
685
+ *
686
+ * Sets `maxEntrySize` to the same value, unless a different value is
687
+ * provided for `maxEntrySize`.
688
+ *
689
+ * At least one of `max`, `maxSize`, or `TTL` is required. This must be a
690
+ * positive integer if set.
691
+ *
692
+ * Even if size tracking is enabled, **it is strongly recommended to set a
693
+ * `max` to prevent unbounded growth of the cache.**
694
+ *
695
+ * Note also that size tracking can negatively impact performance,
696
+ * though for most cases, only minimally.
697
+ */
698
+ maxSize?: Size;
699
+ /**
700
+ * The maximum allowed size for any single item in the cache.
701
+ *
702
+ * If a larger item is passed to {@link LRUCache#set} or returned by a
703
+ * {@link OptionsBase.fetchMethod} or {@link OptionsBase.memoMethod}, then
704
+ * it will not be stored in the cache.
705
+ *
706
+ * Attempting to add an item whose calculated size is greater than
707
+ * this amount will not cache the item or evict any old items, but
708
+ * WILL delete an existing value if one is already present.
709
+ *
710
+ * Optional, must be a positive integer if provided. Defaults to
711
+ * the value of `maxSize` if provided.
712
+ */
713
+ maxEntrySize?: Size;
714
+ /**
715
+ * A function that returns a number indicating the item's size.
716
+ *
717
+ * Requires {@link OptionsBase.maxSize} to be set.
718
+ *
719
+ * If not provided, and {@link OptionsBase.maxSize} or
720
+ * {@link OptionsBase.maxEntrySize} are set, then all
721
+ * {@link LRUCache#set} calls **must** provide an explicit
722
+ * {@link SetOptions.size} or sizeCalculation param.
723
+ */
724
+ sizeCalculation?: SizeCalculator<K, V>;
725
+ /**
726
+ * Method that provides the implementation for {@link LRUCache#fetch}
727
+ *
728
+ * ```ts
729
+ * fetchMethod(key, staleValue, { signal, options, context })
730
+ * ```
731
+ *
732
+ * If `fetchMethod` is not provided, then `cache.fetch(key)` is equivalent
733
+ * to `Promise.resolve(cache.get(key))`.
734
+ *
735
+ * If at any time, `signal.aborted` is set to `true`, or if the
736
+ * `signal.onabort` method is called, or if it emits an `'abort'` event
737
+ * which you can listen to with `addEventListener`, then that means that
738
+ * the fetch should be abandoned. This may be passed along to async
739
+ * functions aware of AbortController/AbortSignal behavior.
740
+ *
741
+ * The `fetchMethod` should **only** return `undefined` or a Promise
742
+ * resolving to `undefined` if the AbortController signaled an `abort`
743
+ * event. In all other cases, it should return or resolve to a value
744
+ * suitable for adding to the cache.
745
+ *
746
+ * The `options` object is a union of the options that may be provided to
747
+ * `set()` and `get()`. If they are modified, then that will result in
748
+ * modifying the settings to `cache.set()` when the value is resolved, and
749
+ * in the case of
750
+ * {@link OptionsBase.noDeleteOnFetchRejection} and
751
+ * {@link OptionsBase.allowStaleOnFetchRejection}, the handling of
752
+ * `fetchMethod` failures.
753
+ *
754
+ * For example, a DNS cache may update the TTL based on the value returned
755
+ * from a remote DNS server by changing `options.ttl` in the `fetchMethod`.
756
+ */
757
+ fetchMethod?: Fetcher<K, V, FC>;
758
+ /**
759
+ * Method that provides the implementation for {@link LRUCache#memo}
760
+ */
761
+ memoMethod?: Memoizer<K, V, FC>;
762
+ /**
763
+ * Set to true to suppress the deletion of stale data when a
764
+ * {@link OptionsBase.fetchMethod} returns a rejected promise.
765
+ */
766
+ noDeleteOnFetchRejection?: boolean;
767
+ /**
768
+ * Do not delete stale items when they are retrieved with
769
+ * {@link LRUCache#get}.
770
+ *
771
+ * Note that the `get` return value will still be `undefined`
772
+ * unless {@link OptionsBase.allowStale} is true.
773
+ *
774
+ * When using time-expiring entries with `ttl`, by default stale
775
+ * items will be removed from the cache when the key is accessed
776
+ * with `cache.get()`.
777
+ *
778
+ * Setting this option will cause stale items to remain in the cache, until
779
+ * they are explicitly deleted with `cache.delete(key)`, or retrieved with
780
+ * `noDeleteOnStaleGet` set to `false`.
781
+ *
782
+ * This may be overridden by passing an options object to `cache.get()`.
783
+ *
784
+ * Only relevant if a ttl is used.
785
+ */
786
+ noDeleteOnStaleGet?: boolean;
787
+ /**
788
+ * Set to true to allow returning stale data when a
789
+ * {@link OptionsBase.fetchMethod} throws an error or returns a rejected
790
+ * promise.
791
+ *
792
+ * This differs from using {@link OptionsBase.allowStale} in that stale
793
+ * data will ONLY be returned in the case that the {@link LRUCache#fetch}
794
+ * fails, not any other times.
795
+ *
796
+ * If a `fetchMethod` fails, and there is no stale value available, the
797
+ * `fetch()` will resolve to `undefined`. Ie, all `fetchMethod` errors are
798
+ * suppressed.
799
+ *
800
+ * Implies `noDeleteOnFetchRejection`.
801
+ *
802
+ * This may be set in calls to `fetch()`, or defaulted on the constructor,
803
+ * or overridden by modifying the options object in the `fetchMethod`.
804
+ */
805
+ allowStaleOnFetchRejection?: boolean;
806
+ /**
807
+ * Set to true to return a stale value from the cache when the
808
+ * `AbortSignal` passed to the {@link OptionsBase.fetchMethod} dispatches
809
+ * an `'abort'` event, whether user-triggered, or due to internal cache
810
+ * behavior.
811
+ *
812
+ * Unless {@link OptionsBase.ignoreFetchAbort} is also set, the underlying
813
+ * {@link OptionsBase.fetchMethod} will still be considered canceled, and
814
+ * any value it returns will be ignored and not cached.
815
+ *
816
+ * Caveat: since fetches are aborted when a new value is explicitly
817
+ * set in the cache, this can lead to fetch returning a stale value,
818
+ * since that was the fallback value _at the moment the `fetch()` was
819
+ * initiated_, even though the new updated value is now present in
820
+ * the cache.
821
+ *
822
+ * For example:
823
+ *
824
+ * ```ts
825
+ * const cache = new LRUCache<string, any>({
826
+ * ttl: 100,
827
+ * fetchMethod: async (url, oldValue, { signal }) => {
828
+ * const res = await fetch(url, { signal })
829
+ * return await res.json()
830
+ * }
831
+ * })
832
+ * cache.set('https://example.com/', { some: 'data' })
833
+ * // 100ms go by...
834
+ * const result = cache.fetch('https://example.com/')
835
+ * cache.set('https://example.com/', { other: 'thing' })
836
+ * console.log(await result) // { some: 'data' }
837
+ * console.log(cache.get('https://example.com/')) // { other: 'thing' }
838
+ * ```
839
+ */
840
+ allowStaleOnFetchAbort?: boolean;
841
+ /**
842
+ * Set to true to ignore the `abort` event emitted by the `AbortSignal`
843
+ * object passed to {@link OptionsBase.fetchMethod}, and still cache the
844
+ * resulting resolution value, as long as it is not `undefined`.
845
+ *
846
+ * When used on its own, this means aborted {@link LRUCache#fetch} calls
847
+ * are not immediately resolved or rejected when they are aborted, and
848
+ * instead take the full time to await.
849
+ *
850
+ * When used with {@link OptionsBase.allowStaleOnFetchAbort}, aborted
851
+ * {@link LRUCache#fetch} calls will resolve immediately to their stale
852
+ * cached value or `undefined`, and will continue to process and eventually
853
+ * update the cache when they resolve, as long as the resulting value is
854
+ * not `undefined`, thus supporting a "return stale on timeout while
855
+ * refreshing" mechanism by passing `AbortSignal.timeout(n)` as the signal.
856
+ *
857
+ * For example:
858
+ *
859
+ * ```ts
860
+ * const c = new LRUCache({
861
+ * ttl: 100,
862
+ * ignoreFetchAbort: true,
863
+ * allowStaleOnFetchAbort: true,
864
+ * fetchMethod: async (key, oldValue, { signal }) => {
865
+ * // note: do NOT pass the signal to fetch()!
866
+ * // let's say this fetch can take a long time.
867
+ * const res = await fetch(`https://slow-backend-server/${key}`)
868
+ * return await res.json()
869
+ * },
870
+ * })
871
+ *
872
+ * // this will return the stale value after 100ms, while still
873
+ * // updating in the background for next time.
874
+ * const val = await c.fetch('key', { signal: AbortSignal.timeout(100) })
875
+ * ```
876
+ *
877
+ * **Note**: regardless of this setting, an `abort` event _is still
878
+ * emitted on the `AbortSignal` object_, so may result in invalid results
879
+ * when passed to other underlying APIs that use AbortSignals.
880
+ *
881
+ * This may be overridden in the {@link OptionsBase.fetchMethod} or the
882
+ * call to {@link LRUCache#fetch}.
883
+ */
884
+ ignoreFetchAbort?: boolean;
885
+ /**
886
+ * In some cases, you may want to swap out the performance/Date object
887
+ * used for TTL tracking. This should almost certainly NOT be done in
888
+ * production environments!
889
+ *
890
+ * This value defaults to `global.performance` if it has a `now()` method,
891
+ * or the `global.Date` object otherwise.
892
+ */
893
+ perf?: Perf;
894
+ }
895
+ interface OptionsMaxLimit<K, V, FC> extends OptionsBase<K, V, FC> {
896
+ max: Count;
897
+ }
898
+ interface OptionsTTLLimit<K, V, FC> extends OptionsBase<K, V, FC> {
899
+ ttl: Milliseconds;
900
+ ttlAutopurge: boolean;
901
+ }
902
+ interface OptionsSizeLimit<K, V, FC> extends OptionsBase<K, V, FC> {
903
+ maxSize: Size;
904
+ }
905
+ /**
906
+ * The valid safe options for the {@link LRUCache} constructor
907
+ */
908
+ type Options<K, V, FC> = OptionsMaxLimit<K, V, FC> | OptionsSizeLimit<K, V, FC> | OptionsTTLLimit<K, V, FC>;
909
+ /**
910
+ * Entry objects used by {@link LRUCache#load} and {@link LRUCache#dump},
911
+ * and returned by {@link LRUCache#info}.
912
+ */
913
+ interface Entry<V> {
914
+ value: V;
915
+ ttl?: Milliseconds;
916
+ size?: Size;
917
+ start?: Milliseconds;
918
+ }
919
+ }
920
+ /**
921
+ * Default export, the thing you're using this module to get.
922
+ *
923
+ * The `K` and `V` types define the key and value types, respectively. The
924
+ * optional `FC` type defines the type of the `context` object passed to
925
+ * `cache.fetch()` and `cache.memo()`.
926
+ *
927
+ * Keys and values **must not** be `null` or `undefined`.
928
+ *
929
+ * All properties from the options object (with the exception of `max`,
930
+ * `maxSize`, `fetchMethod`, `memoMethod`, `dispose` and `disposeAfter`) are
931
+ * added as normal public members. (The listed options are read-only getters.)
932
+ *
933
+ * Changing any of these will alter the defaults for subsequent method calls.
934
+ */
935
+ export declare class LRUCache<K extends {}, V extends {}, FC = unknown> {
936
+ #private;
937
+ /**
938
+ * {@link LRUCache.OptionsBase.perf}
939
+ */
940
+ get perf(): Perf;
941
+ /**
942
+ * {@link LRUCache.OptionsBase.ttl}
943
+ */
944
+ ttl: LRUCache.Milliseconds;
945
+ /**
946
+ * {@link LRUCache.OptionsBase.ttlResolution}
947
+ */
948
+ ttlResolution: LRUCache.Milliseconds;
949
+ /**
950
+ * {@link LRUCache.OptionsBase.ttlAutopurge}
951
+ */
952
+ ttlAutopurge: boolean;
953
+ /**
954
+ * {@link LRUCache.OptionsBase.updateAgeOnGet}
955
+ */
956
+ updateAgeOnGet: boolean;
957
+ /**
958
+ * {@link LRUCache.OptionsBase.updateAgeOnHas}
959
+ */
960
+ updateAgeOnHas: boolean;
961
+ /**
962
+ * {@link LRUCache.OptionsBase.allowStale}
963
+ */
964
+ allowStale: boolean;
965
+ /**
966
+ * {@link LRUCache.OptionsBase.noDisposeOnSet}
967
+ */
968
+ noDisposeOnSet: boolean;
969
+ /**
970
+ * {@link LRUCache.OptionsBase.noUpdateTTL}
971
+ */
972
+ noUpdateTTL: boolean;
973
+ /**
974
+ * {@link LRUCache.OptionsBase.maxEntrySize}
975
+ */
976
+ maxEntrySize: LRUCache.Size;
977
+ /**
978
+ * {@link LRUCache.OptionsBase.sizeCalculation}
979
+ */
980
+ sizeCalculation?: LRUCache.SizeCalculator<K, V>;
981
+ /**
982
+ * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
983
+ */
984
+ noDeleteOnFetchRejection: boolean;
985
+ /**
986
+ * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
987
+ */
988
+ noDeleteOnStaleGet: boolean;
989
+ /**
990
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
991
+ */
992
+ allowStaleOnFetchAbort: boolean;
993
+ /**
994
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
995
+ */
996
+ allowStaleOnFetchRejection: boolean;
997
+ /**
998
+ * {@link LRUCache.OptionsBase.ignoreFetchAbort}
999
+ */
1000
+ ignoreFetchAbort: boolean;
1001
+ /**
1002
+ * Do not call this method unless you need to inspect the
1003
+ * inner workings of the cache. If anything returned by this
1004
+ * object is modified in any way, strange breakage may occur.
1005
+ *
1006
+ * These fields are private for a reason!
1007
+ *
1008
+ * @internal
1009
+ */
1010
+ static unsafeExposeInternals<K extends {}, V extends {}, FC extends unknown = unknown>(c: LRUCache<K, V, FC>): {
1011
+ starts: ZeroArray | undefined;
1012
+ ttls: ZeroArray | undefined;
1013
+ autopurgeTimers: (NodeJS.Timeout | undefined)[] | undefined;
1014
+ sizes: ZeroArray | undefined;
1015
+ keyMap: Map<K, number>;
1016
+ keyList: (K | undefined)[];
1017
+ valList: (V | BackgroundFetch<V> | undefined)[];
1018
+ next: NumberArray;
1019
+ prev: NumberArray;
1020
+ readonly head: Index;
1021
+ readonly tail: Index;
1022
+ free: StackLike;
1023
+ isBackgroundFetch: (p: unknown) => p is BackgroundFetch<V>;
1024
+ backgroundFetch: (k: K, index: number | undefined, options: LRUCache.FetchOptions<K, V, FC>, context: unknown) => BackgroundFetch<V>;
1025
+ moveToTail: (index: number) => void;
1026
+ indexes: (options?: {
1027
+ allowStale: boolean;
1028
+ }) => Generator<Index, void, unknown>;
1029
+ rindexes: (options?: {
1030
+ allowStale: boolean;
1031
+ }) => Generator<Index, void, unknown>;
1032
+ isStale: (index: number | undefined) => boolean;
1033
+ };
1034
+ /**
1035
+ * {@link LRUCache.OptionsBase.max} (read-only)
1036
+ */
1037
+ get max(): LRUCache.Count;
1038
+ /**
1039
+ * {@link LRUCache.OptionsBase.maxSize} (read-only)
1040
+ */
1041
+ get maxSize(): LRUCache.Count;
1042
+ /**
1043
+ * The total computed size of items in the cache (read-only)
1044
+ */
1045
+ get calculatedSize(): LRUCache.Size;
1046
+ /**
1047
+ * The number of items stored in the cache (read-only)
1048
+ */
1049
+ get size(): LRUCache.Count;
1050
+ /**
1051
+ * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
1052
+ */
1053
+ get fetchMethod(): LRUCache.Fetcher<K, V, FC> | undefined;
1054
+ get memoMethod(): LRUCache.Memoizer<K, V, FC> | undefined;
1055
+ /**
1056
+ * {@link LRUCache.OptionsBase.dispose} (read-only)
1057
+ */
1058
+ get dispose(): LRUCache.Disposer<K, V> | undefined;
1059
+ /**
1060
+ * {@link LRUCache.OptionsBase.onInsert} (read-only)
1061
+ */
1062
+ get onInsert(): LRUCache.Inserter<K, V> | undefined;
1063
+ /**
1064
+ * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
1065
+ */
1066
+ get disposeAfter(): LRUCache.Disposer<K, V> | undefined;
1067
+ constructor(options: LRUCache.Options<K, V, FC> | LRUCache<K, V, FC>);
1068
+ /**
1069
+ * Return the number of ms left in the item's TTL. If item is not in cache,
1070
+ * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
1071
+ */
1072
+ getRemainingTTL(key: K): number;
1073
+ /**
1074
+ * Return a generator yielding `[key, value]` pairs,
1075
+ * in order from most recently used to least recently used.
1076
+ */
1077
+ entries(): Generator<[K, V], void, unknown>;
1078
+ /**
1079
+ * Inverse order version of {@link LRUCache.entries}
1080
+ *
1081
+ * Return a generator yielding `[key, value]` pairs,
1082
+ * in order from least recently used to most recently used.
1083
+ */
1084
+ rentries(): Generator<(K | V)[], void, unknown>;
1085
+ /**
1086
+ * Return a generator yielding the keys in the cache,
1087
+ * in order from most recently used to least recently used.
1088
+ */
1089
+ keys(): Generator<K, void, unknown>;
1090
+ /**
1091
+ * Inverse order version of {@link LRUCache.keys}
1092
+ *
1093
+ * Return a generator yielding the keys in the cache,
1094
+ * in order from least recently used to most recently used.
1095
+ */
1096
+ rkeys(): Generator<K, void, unknown>;
1097
+ /**
1098
+ * Return a generator yielding the values in the cache,
1099
+ * in order from most recently used to least recently used.
1100
+ */
1101
+ values(): Generator<V, void, unknown>;
1102
+ /**
1103
+ * Inverse order version of {@link LRUCache.values}
1104
+ *
1105
+ * Return a generator yielding the values in the cache,
1106
+ * in order from least recently used to most recently used.
1107
+ */
1108
+ rvalues(): Generator<V | undefined, void, unknown>;
1109
+ /**
1110
+ * Iterating over the cache itself yields the same results as
1111
+ * {@link LRUCache.entries}
1112
+ */
1113
+ [Symbol.iterator](): Generator<[K, V], void, unknown>;
1114
+ /**
1115
+ * A String value that is used in the creation of the default string
1116
+ * description of an object. Called by the built-in method
1117
+ * `Object.prototype.toString`.
1118
+ */
1119
+ [Symbol.toStringTag]: string;
1120
+ /**
1121
+ * Find a value for which the supplied fn method returns a truthy value,
1122
+ * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
1123
+ */
1124
+ find(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => boolean, getOptions?: LRUCache.GetOptions<K, V, FC>): V | undefined;
1125
+ /**
1126
+ * Call the supplied function on each item in the cache, in order from most
1127
+ * recently used to least recently used.
1128
+ *
1129
+ * `fn` is called as `fn(value, key, cache)`.
1130
+ *
1131
+ * If `thisp` is provided, function will be called in the `this`-context of
1132
+ * the provided object, or the cache if no `thisp` object is provided.
1133
+ *
1134
+ * Does not update age or recenty of use, or iterate over stale values.
1135
+ */
1136
+ forEach(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => unknown, thisp?: unknown): void;
1137
+ /**
1138
+ * The same as {@link LRUCache.forEach} but items are iterated over in
1139
+ * reverse order. (ie, less recently used items are iterated over first.)
1140
+ */
1141
+ rforEach(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => unknown, thisp?: unknown): void;
1142
+ /**
1143
+ * Delete any stale entries. Returns true if anything was removed,
1144
+ * false otherwise.
1145
+ */
1146
+ purgeStale(): boolean;
1147
+ /**
1148
+ * Get the extended info about a given entry, to get its value, size, and
1149
+ * TTL info simultaneously. Returns `undefined` if the key is not present.
1150
+ *
1151
+ * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
1152
+ * serialization, the `start` value is always the current timestamp, and the
1153
+ * `ttl` is a calculated remaining time to live (negative if expired).
1154
+ *
1155
+ * Always returns stale values, if their info is found in the cache, so be
1156
+ * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
1157
+ * if relevant.
1158
+ */
1159
+ info(key: K): LRUCache.Entry<V> | undefined;
1160
+ /**
1161
+ * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
1162
+ * passed to {@link LRUCache#load}.
1163
+ *
1164
+ * The `start` fields are calculated relative to a portable `Date.now()`
1165
+ * timestamp, even if `performance.now()` is available.
1166
+ *
1167
+ * Stale entries are always included in the `dump`, even if
1168
+ * {@link LRUCache.OptionsBase.allowStale} is false.
1169
+ *
1170
+ * Note: this returns an actual array, not a generator, so it can be more
1171
+ * easily passed around.
1172
+ */
1173
+ dump(): [K, LRUCache.Entry<V>][];
1174
+ /**
1175
+ * Reset the cache and load in the items in entries in the order listed.
1176
+ *
1177
+ * The shape of the resulting cache may be different if the same options are
1178
+ * not used in both caches.
1179
+ *
1180
+ * The `start` fields are assumed to be calculated relative to a portable
1181
+ * `Date.now()` timestamp, even if `performance.now()` is available.
1182
+ */
1183
+ load(arr: [K, LRUCache.Entry<V>][]): void;
1184
+ /**
1185
+ * Add a value to the cache.
1186
+ *
1187
+ * Note: if `undefined` is specified as a value, this is an alias for
1188
+ * {@link LRUCache#delete}
1189
+ *
1190
+ * Fields on the {@link LRUCache.SetOptions} options param will override
1191
+ * their corresponding values in the constructor options for the scope
1192
+ * of this single `set()` operation.
1193
+ *
1194
+ * If `start` is provided, then that will set the effective start
1195
+ * time for the TTL calculation. Note that this must be a previous
1196
+ * value of `performance.now()` if supported, or a previous value of
1197
+ * `Date.now()` if not.
1198
+ *
1199
+ * Options object may also include `size`, which will prevent
1200
+ * calling the `sizeCalculation` function and just use the specified
1201
+ * number if it is a positive integer, and `noDisposeOnSet` which
1202
+ * will prevent calling a `dispose` function in the case of
1203
+ * overwrites.
1204
+ *
1205
+ * If the `size` (or return value of `sizeCalculation`) for a given
1206
+ * entry is greater than `maxEntrySize`, then the item will not be
1207
+ * added to the cache.
1208
+ *
1209
+ * Will update the recency of the entry.
1210
+ *
1211
+ * If the value is `undefined`, then this is an alias for
1212
+ * `cache.delete(key)`. `undefined` is never stored in the cache.
1213
+ */
1214
+ set(k: K, v: V | undefined, setOptions?: LRUCache.SetOptions<K, V, FC>): this;
1215
+ /**
1216
+ * Evict the least recently used item, returning its value or
1217
+ * `undefined` if cache is empty.
1218
+ */
1219
+ pop(): V | undefined;
1220
+ /**
1221
+ * Check if a key is in the cache, without updating the recency of use.
1222
+ * Will return false if the item is stale, even though it is technically
1223
+ * in the cache.
1224
+ *
1225
+ * Check if a key is in the cache, without updating the recency of
1226
+ * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
1227
+ * to `true` in either the options or the constructor.
1228
+ *
1229
+ * Will return `false` if the item is stale, even though it is technically in
1230
+ * the cache. The difference can be determined (if it matters) by using a
1231
+ * `status` argument, and inspecting the `has` field.
1232
+ *
1233
+ * Will not update item age unless
1234
+ * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
1235
+ */
1236
+ has(k: K, hasOptions?: LRUCache.HasOptions<K, V, FC>): boolean;
1237
+ /**
1238
+ * Like {@link LRUCache#get} but doesn't update recency or delete stale
1239
+ * items.
1240
+ *
1241
+ * Returns `undefined` if the item is stale, unless
1242
+ * {@link LRUCache.OptionsBase.allowStale} is set.
1243
+ */
1244
+ peek(k: K, peekOptions?: LRUCache.PeekOptions<K, V, FC>): V | undefined;
1245
+ /**
1246
+ * Make an asynchronous cached fetch using the
1247
+ * {@link LRUCache.OptionsBase.fetchMethod} function.
1248
+ *
1249
+ * If the value is in the cache and not stale, then the returned
1250
+ * Promise resolves to the value.
1251
+ *
1252
+ * If not in the cache, or beyond its TTL staleness, then
1253
+ * `fetchMethod(key, staleValue, { options, signal, context })` is
1254
+ * called, and the value returned will be added to the cache once
1255
+ * resolved.
1256
+ *
1257
+ * If called with `allowStale`, and an asynchronous fetch is
1258
+ * currently in progress to reload a stale value, then the former
1259
+ * stale value will be returned.
1260
+ *
1261
+ * If called with `forceRefresh`, then the cached item will be
1262
+ * re-fetched, even if it is not stale. However, if `allowStale` is also
1263
+ * set, then the old value will still be returned. This is useful
1264
+ * in cases where you want to force a reload of a cached value. If
1265
+ * a background fetch is already in progress, then `forceRefresh`
1266
+ * has no effect.
1267
+ *
1268
+ * If multiple fetches for the same key are issued, then they will all be
1269
+ * coalesced into a single call to fetchMethod.
1270
+ *
1271
+ * Note that this means that handling options such as
1272
+ * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort},
1273
+ * {@link LRUCache.FetchOptions.signal},
1274
+ * and {@link LRUCache.OptionsBase.allowStaleOnFetchRejection} will be
1275
+ * determined by the FIRST fetch() call for a given key.
1276
+ *
1277
+ * This is a known (fixable) shortcoming which will be addresed on when
1278
+ * someone complains about it, as the fix would involve added complexity and
1279
+ * may not be worth the costs for this edge case.
1280
+ *
1281
+ * If {@link LRUCache.OptionsBase.fetchMethod} is not specified, then this is
1282
+ * effectively an alias for `Promise.resolve(cache.get(key))`.
1283
+ *
1284
+ * When the fetch method resolves to a value, if the fetch has not
1285
+ * been aborted due to deletion, eviction, or being overwritten,
1286
+ * then it is added to the cache using the options provided.
1287
+ *
1288
+ * If the key is evicted or deleted before the `fetchMethod`
1289
+ * resolves, then the AbortSignal passed to the `fetchMethod` will
1290
+ * receive an `abort` event, and the promise returned by `fetch()`
1291
+ * will reject with the reason for the abort.
1292
+ *
1293
+ * If a `signal` is passed to the `fetch()` call, then aborting the
1294
+ * signal will abort the fetch and cause the `fetch()` promise to
1295
+ * reject with the reason provided.
1296
+ *
1297
+ * **Setting `context`**
1298
+ *
1299
+ * If an `FC` type is set to a type other than `unknown`, `void`, or
1300
+ * `undefined` in the {@link LRUCache} constructor, then all
1301
+ * calls to `cache.fetch()` _must_ provide a `context` option. If
1302
+ * set to `undefined` or `void`, then calls to fetch _must not_
1303
+ * provide a `context` option.
1304
+ *
1305
+ * The `context` param allows you to provide arbitrary data that
1306
+ * might be relevant in the course of fetching the data. It is only
1307
+ * relevant for the course of a single `fetch()` operation, and
1308
+ * discarded afterwards.
1309
+ *
1310
+ * **Note: `fetch()` calls are inflight-unique**
1311
+ *
1312
+ * If you call `fetch()` multiple times with the same key value,
1313
+ * then every call after the first will resolve on the same
1314
+ * promise<sup>1</sup>,
1315
+ * _even if they have different settings that would otherwise change
1316
+ * the behavior of the fetch_, such as `noDeleteOnFetchRejection`
1317
+ * or `ignoreFetchAbort`.
1318
+ *
1319
+ * In most cases, this is not a problem (in fact, only fetching
1320
+ * something once is what you probably want, if you're caching in
1321
+ * the first place). If you are changing the fetch() options
1322
+ * dramatically between runs, there's a good chance that you might
1323
+ * be trying to fit divergent semantics into a single object, and
1324
+ * would be better off with multiple cache instances.
1325
+ *
1326
+ * **1**: Ie, they're not the "same Promise", but they resolve at
1327
+ * the same time, because they're both waiting on the same
1328
+ * underlying fetchMethod response.
1329
+ */
1330
+ fetch(k: K, fetchOptions: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : LRUCache.FetchOptionsWithContext<K, V, FC>): Promise<undefined | V>;
1331
+ fetch(k: unknown extends FC ? K : FC extends undefined | void ? K : never, fetchOptions?: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : never): Promise<undefined | V>;
1332
+ /**
1333
+ * In some cases, `cache.fetch()` may resolve to `undefined`, either because
1334
+ * a {@link LRUCache.OptionsBase#fetchMethod} was not provided (turning
1335
+ * `cache.fetch(k)` into just an async wrapper around `cache.get(k)`) or
1336
+ * because `ignoreFetchAbort` was specified (either to the constructor or
1337
+ * in the {@link LRUCache.FetchOptions}). Also, the
1338
+ * {@link LRUCache.OptionsBase.fetchMethod} may return `undefined` or `void`, making
1339
+ * the test even more complicated.
1340
+ *
1341
+ * Because inferring the cases where `undefined` might be returned are so
1342
+ * cumbersome, but testing for `undefined` can also be annoying, this method
1343
+ * can be used, which will reject if `this.fetch()` resolves to undefined.
1344
+ */
1345
+ forceFetch(k: K, fetchOptions: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : LRUCache.FetchOptionsWithContext<K, V, FC>): Promise<V>;
1346
+ forceFetch(k: unknown extends FC ? K : FC extends undefined | void ? K : never, fetchOptions?: unknown extends FC ? LRUCache.FetchOptions<K, V, FC> : FC extends undefined | void ? LRUCache.FetchOptionsNoContext<K, V> : never): Promise<V>;
1347
+ /**
1348
+ * If the key is found in the cache, then this is equivalent to
1349
+ * {@link LRUCache#get}. If not, in the cache, then calculate the value using
1350
+ * the {@link LRUCache.OptionsBase.memoMethod}, and add it to the cache.
1351
+ *
1352
+ * If an `FC` type is set to a type other than `unknown`, `void`, or
1353
+ * `undefined` in the LRUCache constructor, then all calls to `cache.memo()`
1354
+ * _must_ provide a `context` option. If set to `undefined` or `void`, then
1355
+ * calls to memo _must not_ provide a `context` option.
1356
+ *
1357
+ * The `context` param allows you to provide arbitrary data that might be
1358
+ * relevant in the course of fetching the data. It is only relevant for the
1359
+ * course of a single `memo()` operation, and discarded afterwards.
1360
+ */
1361
+ memo(k: K, memoOptions: unknown extends FC ? LRUCache.MemoOptions<K, V, FC> : FC extends undefined | void ? LRUCache.MemoOptionsNoContext<K, V> : LRUCache.MemoOptionsWithContext<K, V, FC>): V;
1362
+ memo(k: unknown extends FC ? K : FC extends undefined | void ? K : never, memoOptions?: unknown extends FC ? LRUCache.MemoOptions<K, V, FC> : FC extends undefined | void ? LRUCache.MemoOptionsNoContext<K, V> : never): V;
1363
+ /**
1364
+ * Return a value from the cache. Will update the recency of the cache
1365
+ * entry found.
1366
+ *
1367
+ * If the key is not found, get() will return `undefined`.
1368
+ */
1369
+ get(k: K, getOptions?: LRUCache.GetOptions<K, V, FC>): V | undefined;
1370
+ /**
1371
+ * Deletes a key out of the cache.
1372
+ *
1373
+ * Returns true if the key was deleted, false otherwise.
1374
+ */
1375
+ delete(k: K): boolean;
1376
+ /**
1377
+ * Clear the cache entirely, throwing away all values.
1378
+ */
1379
+ clear(): void;
1380
+ }
1381
+ //# sourceMappingURL=index.d.ts.map