pnpm 6.32.2 → 6.32.5

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 (33) hide show
  1. package/README.md +3 -0
  2. package/dist/node_modules/.modules.yaml +1 -1
  3. package/dist/node_modules/.pnpm/lock.yaml +26 -21
  4. package/dist/node_modules/debug/package.json +1 -1
  5. package/dist/node_modules/debug/src/common.js +1 -1
  6. package/dist/node_modules/gauge/lib/index.js +1 -2
  7. package/dist/node_modules/gauge/package.json +13 -12
  8. package/dist/node_modules/graceful-fs/LICENSE +1 -1
  9. package/dist/node_modules/graceful-fs/graceful-fs.js +23 -4
  10. package/dist/node_modules/graceful-fs/package.json +1 -1
  11. package/dist/node_modules/graceful-fs/polyfills.js +35 -28
  12. package/dist/node_modules/semver/bin/semver.js +17 -8
  13. package/dist/node_modules/semver/classes/comparator.js +3 -2
  14. package/dist/node_modules/semver/classes/index.js +1 -1
  15. package/dist/node_modules/semver/classes/range.js +31 -22
  16. package/dist/node_modules/semver/functions/cmp.js +8 -4
  17. package/dist/node_modules/semver/functions/coerce.js +3 -2
  18. package/dist/node_modules/semver/functions/parse.js +1 -1
  19. package/dist/node_modules/semver/internal/constants.js +2 -2
  20. package/dist/node_modules/semver/internal/identifiers.js +1 -1
  21. package/dist/node_modules/semver/internal/parse-options.js +3 -3
  22. package/dist/node_modules/semver/internal/re.js +3 -3
  23. package/dist/node_modules/semver/node_modules/lru-cache/LICENSE +15 -0
  24. package/dist/node_modules/semver/node_modules/lru-cache/index.js +798 -0
  25. package/dist/node_modules/semver/node_modules/lru-cache/package.json +44 -0
  26. package/dist/node_modules/semver/package.json +46 -13
  27. package/dist/node_modules/semver/ranges/min-version.js +2 -1
  28. package/dist/node_modules/semver/ranges/outside.js +1 -1
  29. package/dist/node_modules/semver/ranges/simplify.js +15 -12
  30. package/dist/node_modules/semver/ranges/subset.js +53 -31
  31. package/dist/pnpm.cjs +1010 -1051
  32. package/package.json +15 -15
  33. package/dist/node_modules/gauge/lib/demo.js +0 -45
@@ -0,0 +1,798 @@
1
+ const perf = typeof performance === 'object' && performance &&
2
+ typeof performance.now === 'function' ? performance : Date
3
+
4
+ const hasAbortController = typeof AbortController !== 'undefined'
5
+
6
+ /* istanbul ignore next - minimal backwards compatibility polyfill */
7
+ const AC = hasAbortController ? AbortController : Object.assign(
8
+ class AbortController {
9
+ constructor () { this.signal = new AC.AbortSignal }
10
+ abort () { this.signal.aborted = true }
11
+ },
12
+ { AbortSignal: class AbortSignal { constructor () { this.aborted = false }}}
13
+ )
14
+
15
+ const warned = new Set()
16
+ const deprecatedOption = (opt, instead) => {
17
+ const code = `LRU_CACHE_OPTION_${opt}`
18
+ if (shouldWarn(code)) {
19
+ warn(code, `${opt} option`, `options.${instead}`, LRUCache)
20
+ }
21
+ }
22
+ const deprecatedMethod = (method, instead) => {
23
+ const code = `LRU_CACHE_METHOD_${method}`
24
+ if (shouldWarn(code)) {
25
+ const { prototype } = LRUCache
26
+ const { get } = Object.getOwnPropertyDescriptor(prototype, method)
27
+ warn(code, `${method} method`, `cache.${instead}()`, get)
28
+ }
29
+ }
30
+ const deprecatedProperty = (field, instead) => {
31
+ const code = `LRU_CACHE_PROPERTY_${field}`
32
+ if (shouldWarn(code)) {
33
+ const { prototype } = LRUCache
34
+ const { get } = Object.getOwnPropertyDescriptor(prototype, field)
35
+ warn(code, `${field} property`, `cache.${instead}`, get)
36
+ }
37
+ }
38
+
39
+ const shouldWarn = code => typeof process === 'object' &&
40
+ process &&
41
+ !warned.has(code)
42
+
43
+ const warn = (code, what, instead, fn) => {
44
+ warned.add(code)
45
+ const msg = `The ${what} is deprecated. Please use ${instead} instead.`
46
+ process.emitWarning(msg, 'DeprecationWarning', code, fn)
47
+ }
48
+
49
+ const isPosInt = n => n && n === Math.floor(n) && n > 0 && isFinite(n)
50
+
51
+ /* istanbul ignore next - This is a little bit ridiculous, tbh.
52
+ * The maximum array length is 2^32-1 or thereabouts on most JS impls.
53
+ * And well before that point, you're caching the entire world, I mean,
54
+ * that's ~32GB of just integers for the next/prev links, plus whatever
55
+ * else to hold that many keys and values. Just filling the memory with
56
+ * zeroes at init time is brutal when you get that big.
57
+ * But why not be complete?
58
+ * Maybe in the future, these limits will have expanded. */
59
+ const getUintArray = max => !isPosInt(max) ? null
60
+ : max <= Math.pow(2, 8) ? Uint8Array
61
+ : max <= Math.pow(2, 16) ? Uint16Array
62
+ : max <= Math.pow(2, 32) ? Uint32Array
63
+ : max <= Number.MAX_SAFE_INTEGER ? ZeroArray
64
+ : null
65
+
66
+ class ZeroArray extends Array {
67
+ constructor (size) {
68
+ super(size)
69
+ this.fill(0)
70
+ }
71
+ }
72
+
73
+ class Stack {
74
+ constructor (max) {
75
+ const UintArray = max ? getUintArray(max) : Array
76
+ this.heap = new UintArray(max)
77
+ this.length = 0
78
+ }
79
+ push (n) {
80
+ this.heap[this.length++] = n
81
+ }
82
+ pop () {
83
+ return this.heap[--this.length]
84
+ }
85
+ }
86
+
87
+ class LRUCache {
88
+ constructor (options = {}) {
89
+ const {
90
+ max = 0,
91
+ ttl,
92
+ ttlResolution = 1,
93
+ ttlAutopurge,
94
+ updateAgeOnGet,
95
+ allowStale,
96
+ dispose,
97
+ disposeAfter,
98
+ noDisposeOnSet,
99
+ noUpdateTTL,
100
+ maxSize = 0,
101
+ sizeCalculation,
102
+ fetchMethod,
103
+ } = options
104
+
105
+ // deprecated options, don't trigger a warning for getting them if
106
+ // the thing being passed in is another LRUCache we're copying.
107
+ const {
108
+ length,
109
+ maxAge,
110
+ stale,
111
+ } = options instanceof LRUCache ? {} : options
112
+
113
+ if (max !== 0 && !isPosInt(max)) {
114
+ throw new TypeError('max option must be a nonnegative integer')
115
+ }
116
+
117
+ const UintArray = max ? getUintArray(max) : Array
118
+ if (!UintArray) {
119
+ throw new Error('invalid max value: ' + max)
120
+ }
121
+
122
+ this.max = max
123
+ this.maxSize = maxSize
124
+ this.sizeCalculation = sizeCalculation || length
125
+ if (this.sizeCalculation) {
126
+ if (!this.maxSize) {
127
+ throw new TypeError('cannot set sizeCalculation without setting maxSize')
128
+ }
129
+ if (typeof this.sizeCalculation !== 'function') {
130
+ throw new TypeError('sizeCalculation set to non-function')
131
+ }
132
+ }
133
+
134
+ this.fetchMethod = fetchMethod || null
135
+ if (this.fetchMethod && typeof this.fetchMethod !== 'function') {
136
+ throw new TypeError('fetchMethod must be a function if specified')
137
+ }
138
+
139
+
140
+ this.keyMap = new Map()
141
+ this.keyList = new Array(max).fill(null)
142
+ this.valList = new Array(max).fill(null)
143
+ this.next = new UintArray(max)
144
+ this.prev = new UintArray(max)
145
+ this.head = 0
146
+ this.tail = 0
147
+ this.free = new Stack(max)
148
+ this.initialFill = 1
149
+ this.size = 0
150
+
151
+ if (typeof dispose === 'function') {
152
+ this.dispose = dispose
153
+ }
154
+ if (typeof disposeAfter === 'function') {
155
+ this.disposeAfter = disposeAfter
156
+ this.disposed = []
157
+ } else {
158
+ this.disposeAfter = null
159
+ this.disposed = null
160
+ }
161
+ this.noDisposeOnSet = !!noDisposeOnSet
162
+ this.noUpdateTTL = !!noUpdateTTL
163
+
164
+ if (this.maxSize !== 0) {
165
+ if (!isPosInt(this.maxSize)) {
166
+ throw new TypeError('maxSize must be a positive integer if specified')
167
+ }
168
+ this.initializeSizeTracking()
169
+ }
170
+
171
+ this.allowStale = !!allowStale || !!stale
172
+ this.updateAgeOnGet = !!updateAgeOnGet
173
+ this.ttlResolution = isPosInt(ttlResolution) || ttlResolution === 0
174
+ ? ttlResolution : 1
175
+ this.ttlAutopurge = !!ttlAutopurge
176
+ this.ttl = ttl || maxAge || 0
177
+ if (this.ttl) {
178
+ if (!isPosInt(this.ttl)) {
179
+ throw new TypeError('ttl must be a positive integer if specified')
180
+ }
181
+ this.initializeTTLTracking()
182
+ }
183
+
184
+ // do not allow completely unbounded caches
185
+ if (this.max === 0 && this.ttl === 0 && this.maxSize === 0) {
186
+ throw new TypeError('At least one of max, maxSize, or ttl is required')
187
+ }
188
+ if (!this.ttlAutopurge && !this.max && !this.maxSize) {
189
+ const code = 'LRU_CACHE_UNBOUNDED'
190
+ if (shouldWarn(code)) {
191
+ warned.add(code)
192
+ const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
193
+ 'result in unbounded memory consumption.'
194
+ process.emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache)
195
+ }
196
+ }
197
+
198
+ if (stale) {
199
+ deprecatedOption('stale', 'allowStale')
200
+ }
201
+ if (maxAge) {
202
+ deprecatedOption('maxAge', 'ttl')
203
+ }
204
+ if (length) {
205
+ deprecatedOption('length', 'sizeCalculation')
206
+ }
207
+ }
208
+
209
+ getRemainingTTL (key) {
210
+ return this.has(key) ? Infinity : 0
211
+ }
212
+
213
+ initializeTTLTracking () {
214
+ this.ttls = new ZeroArray(this.max)
215
+ this.starts = new ZeroArray(this.max)
216
+
217
+ this.setItemTTL = (index, ttl) => {
218
+ this.starts[index] = ttl !== 0 ? perf.now() : 0
219
+ this.ttls[index] = ttl
220
+ if (ttl !== 0 && this.ttlAutopurge) {
221
+ const t = setTimeout(() => {
222
+ if (this.isStale(index)) {
223
+ this.delete(this.keyList[index])
224
+ }
225
+ }, ttl + 1)
226
+ /* istanbul ignore else - unref() not supported on all platforms */
227
+ if (t.unref) {
228
+ t.unref()
229
+ }
230
+ }
231
+ }
232
+
233
+ this.updateItemAge = (index) => {
234
+ this.starts[index] = this.ttls[index] !== 0 ? perf.now() : 0
235
+ }
236
+
237
+ // debounce calls to perf.now() to 1s so we're not hitting
238
+ // that costly call repeatedly.
239
+ let cachedNow = 0
240
+ const getNow = () => {
241
+ const n = perf.now()
242
+ if (this.ttlResolution > 0) {
243
+ cachedNow = n
244
+ const t = setTimeout(() => cachedNow = 0, this.ttlResolution)
245
+ /* istanbul ignore else - not available on all platforms */
246
+ if (t.unref) {
247
+ t.unref()
248
+ }
249
+ }
250
+ return n
251
+ }
252
+
253
+ this.getRemainingTTL = (key) => {
254
+ const index = this.keyMap.get(key)
255
+ if (index === undefined) {
256
+ return 0
257
+ }
258
+ return this.ttls[index] === 0 || this.starts[index] === 0 ? Infinity
259
+ : ((this.starts[index] + this.ttls[index]) - (cachedNow || getNow()))
260
+ }
261
+
262
+ this.isStale = (index) => {
263
+ return this.ttls[index] !== 0 && this.starts[index] !== 0 &&
264
+ ((cachedNow || getNow()) - this.starts[index] > this.ttls[index])
265
+ }
266
+ }
267
+ updateItemAge (index) {}
268
+ setItemTTL (index, ttl) {}
269
+ isStale (index) { return false }
270
+
271
+ initializeSizeTracking () {
272
+ this.calculatedSize = 0
273
+ this.sizes = new ZeroArray(this.max)
274
+ this.removeItemSize = index => this.calculatedSize -= this.sizes[index]
275
+ this.requireSize = (k, v, size, sizeCalculation) => {
276
+ if (!isPosInt(size)) {
277
+ if (sizeCalculation) {
278
+ if (typeof sizeCalculation !== 'function') {
279
+ throw new TypeError('sizeCalculation must be a function')
280
+ }
281
+ size = sizeCalculation(v, k)
282
+ if (!isPosInt(size)) {
283
+ throw new TypeError('sizeCalculation return invalid (expect positive integer)')
284
+ }
285
+ } else {
286
+ throw new TypeError('invalid size value (must be positive integer)')
287
+ }
288
+ }
289
+ return size
290
+ }
291
+ this.addItemSize = (index, v, k, size) => {
292
+ this.sizes[index] = size
293
+ const maxSize = this.maxSize - this.sizes[index]
294
+ while (this.calculatedSize > maxSize) {
295
+ this.evict()
296
+ }
297
+ this.calculatedSize += this.sizes[index]
298
+ }
299
+ this.delete = k => {
300
+ if (this.size !== 0) {
301
+ const index = this.keyMap.get(k)
302
+ if (index !== undefined) {
303
+ this.calculatedSize -= this.sizes[index]
304
+ }
305
+ }
306
+ return LRUCache.prototype.delete.call(this, k)
307
+ }
308
+ }
309
+ removeItemSize (index) {}
310
+ addItemSize (index, v, k, size) {}
311
+ requireSize (k, v, size, sizeCalculation) {
312
+ if (size || sizeCalculation) {
313
+ throw new TypeError('cannot set size without setting maxSize on cache')
314
+ }
315
+ }
316
+
317
+ *indexes ({ allowStale = this.allowStale } = {}) {
318
+ if (this.size) {
319
+ for (let i = this.tail; true; ) {
320
+ if (!this.isValidIndex(i)) {
321
+ break
322
+ }
323
+ if (allowStale || !this.isStale(i)) {
324
+ yield i
325
+ }
326
+ if (i === this.head) {
327
+ break
328
+ } else {
329
+ i = this.prev[i]
330
+ }
331
+ }
332
+ }
333
+ }
334
+
335
+ *rindexes ({ allowStale = this.allowStale } = {}) {
336
+ if (this.size) {
337
+ for (let i = this.head; true; ) {
338
+ if (!this.isValidIndex(i)) {
339
+ break
340
+ }
341
+ if (allowStale || !this.isStale(i)) {
342
+ yield i
343
+ }
344
+ if (i === this.tail) {
345
+ break
346
+ } else {
347
+ i = this.next[i]
348
+ }
349
+ }
350
+ }
351
+ }
352
+
353
+ isValidIndex (index) {
354
+ return this.keyMap.get(this.keyList[index]) === index
355
+ }
356
+
357
+ *entries () {
358
+ for (const i of this.indexes()) {
359
+ yield [this.keyList[i], this.valList[i]]
360
+ }
361
+ }
362
+ *rentries () {
363
+ for (const i of this.rindexes()) {
364
+ yield [this.keyList[i], this.valList[i]]
365
+ }
366
+ }
367
+
368
+ *keys () {
369
+ for (const i of this.indexes()) {
370
+ yield this.keyList[i]
371
+ }
372
+ }
373
+ *rkeys () {
374
+ for (const i of this.rindexes()) {
375
+ yield this.keyList[i]
376
+ }
377
+ }
378
+
379
+ *values () {
380
+ for (const i of this.indexes()) {
381
+ yield this.valList[i]
382
+ }
383
+ }
384
+ *rvalues () {
385
+ for (const i of this.rindexes()) {
386
+ yield this.valList[i]
387
+ }
388
+ }
389
+
390
+ [Symbol.iterator] () {
391
+ return this.entries()
392
+ }
393
+
394
+ find (fn, getOptions = {}) {
395
+ for (const i of this.indexes()) {
396
+ if (fn(this.valList[i], this.keyList[i], this)) {
397
+ return this.get(this.keyList[i], getOptions)
398
+ }
399
+ }
400
+ }
401
+
402
+ forEach (fn, thisp = this) {
403
+ for (const i of this.indexes()) {
404
+ fn.call(thisp, this.valList[i], this.keyList[i], this)
405
+ }
406
+ }
407
+
408
+ rforEach (fn, thisp = this) {
409
+ for (const i of this.rindexes()) {
410
+ fn.call(thisp, this.valList[i], this.keyList[i], this)
411
+ }
412
+ }
413
+
414
+ get prune () {
415
+ deprecatedMethod('prune', 'purgeStale')
416
+ return this.purgeStale
417
+ }
418
+
419
+ purgeStale () {
420
+ let deleted = false
421
+ for (const i of this.rindexes({ allowStale: true })) {
422
+ if (this.isStale(i)) {
423
+ this.delete(this.keyList[i])
424
+ deleted = true
425
+ }
426
+ }
427
+ return deleted
428
+ }
429
+
430
+ dump () {
431
+ const arr = []
432
+ for (const i of this.indexes()) {
433
+ const key = this.keyList[i]
434
+ const value = this.valList[i]
435
+ const entry = { value }
436
+ if (this.ttls) {
437
+ entry.ttl = this.ttls[i]
438
+ }
439
+ if (this.sizes) {
440
+ entry.size = this.sizes[i]
441
+ }
442
+ arr.unshift([key, entry])
443
+ }
444
+ return arr
445
+ }
446
+
447
+ load (arr) {
448
+ this.clear()
449
+ for (const [key, entry] of arr) {
450
+ this.set(key, entry.value, entry)
451
+ }
452
+ }
453
+
454
+ dispose (v, k, reason) {}
455
+
456
+ set (k, v, {
457
+ ttl = this.ttl,
458
+ noDisposeOnSet = this.noDisposeOnSet,
459
+ size = 0,
460
+ sizeCalculation = this.sizeCalculation,
461
+ noUpdateTTL = this.noUpdateTTL,
462
+ } = {}) {
463
+ size = this.requireSize(k, v, size, sizeCalculation)
464
+ let index = this.size === 0 ? undefined : this.keyMap.get(k)
465
+ if (index === undefined) {
466
+ // addition
467
+ index = this.newIndex()
468
+ this.keyList[index] = k
469
+ this.valList[index] = v
470
+ this.keyMap.set(k, index)
471
+ this.next[this.tail] = index
472
+ this.prev[index] = this.tail
473
+ this.tail = index
474
+ this.size ++
475
+ this.addItemSize(index, v, k, size)
476
+ noUpdateTTL = false
477
+ } else {
478
+ // update
479
+ const oldVal = this.valList[index]
480
+ if (v !== oldVal) {
481
+ if (this.isBackgroundFetch(oldVal)) {
482
+ oldVal.__abortController.abort()
483
+ } else {
484
+ if (!noDisposeOnSet) {
485
+ this.dispose(oldVal, k, 'set')
486
+ if (this.disposeAfter) {
487
+ this.disposed.push([oldVal, k, 'set'])
488
+ }
489
+ }
490
+ }
491
+ this.removeItemSize(index)
492
+ this.valList[index] = v
493
+ this.addItemSize(index, v, k, size)
494
+ }
495
+ this.moveToTail(index)
496
+ }
497
+ if (ttl !== 0 && this.ttl === 0 && !this.ttls) {
498
+ this.initializeTTLTracking()
499
+ }
500
+ if (!noUpdateTTL) {
501
+ this.setItemTTL(index, ttl)
502
+ }
503
+ if (this.disposeAfter) {
504
+ while (this.disposed.length) {
505
+ this.disposeAfter(...this.disposed.shift())
506
+ }
507
+ }
508
+ return this
509
+ }
510
+
511
+ newIndex () {
512
+ if (this.size === 0) {
513
+ return this.tail
514
+ }
515
+ if (this.size === this.max) {
516
+ return this.evict()
517
+ }
518
+ if (this.free.length !== 0) {
519
+ return this.free.pop()
520
+ }
521
+ // initial fill, just keep writing down the list
522
+ return this.initialFill++
523
+ }
524
+
525
+ pop () {
526
+ if (this.size) {
527
+ const val = this.valList[this.head]
528
+ this.evict()
529
+ return val
530
+ }
531
+ }
532
+
533
+ evict () {
534
+ const head = this.head
535
+ const k = this.keyList[head]
536
+ const v = this.valList[head]
537
+ if (this.isBackgroundFetch(v)) {
538
+ v.__abortController.abort()
539
+ } else {
540
+ this.dispose(v, k, 'evict')
541
+ if (this.disposeAfter) {
542
+ this.disposed.push([v, k, 'evict'])
543
+ }
544
+ }
545
+ this.removeItemSize(head)
546
+ this.head = this.next[head]
547
+ this.keyMap.delete(k)
548
+ this.size --
549
+ return head
550
+ }
551
+
552
+ has (k) {
553
+ return this.keyMap.has(k) && !this.isStale(this.keyMap.get(k))
554
+ }
555
+
556
+ // like get(), but without any LRU updating or TTL expiration
557
+ peek (k, { allowStale = this.allowStale } = {}) {
558
+ const index = this.keyMap.get(k)
559
+ if (index !== undefined && (allowStale || !this.isStale(index))) {
560
+ return this.valList[index]
561
+ }
562
+ }
563
+
564
+ backgroundFetch (k, index, options) {
565
+ const v = index === undefined ? undefined : this.valList[index]
566
+ if (this.isBackgroundFetch(v)) {
567
+ return v
568
+ }
569
+ const ac = new AC()
570
+ const fetchOpts = {
571
+ signal: ac.signal,
572
+ options,
573
+ }
574
+ const p = Promise.resolve(this.fetchMethod(k, v, fetchOpts)).then(v => {
575
+ if (!ac.signal.aborted) {
576
+ this.set(k, v, fetchOpts.options)
577
+ }
578
+ return v
579
+ })
580
+ p.__abortController = ac
581
+ p.__staleWhileFetching = v
582
+ if (index === undefined) {
583
+ this.set(k, p, fetchOpts.options)
584
+ index = this.keyMap.get(k)
585
+ } else {
586
+ this.valList[index] = p
587
+ }
588
+ return p
589
+ }
590
+
591
+ isBackgroundFetch (p) {
592
+ return p && typeof p === 'object' && typeof p.then === 'function' &&
593
+ Object.prototype.hasOwnProperty.call(p, '__staleWhileFetching')
594
+ }
595
+
596
+ // this takes the union of get() and set() opts, because it does both
597
+ async fetch (k, {
598
+ allowStale = this.allowStale,
599
+ updateAgeOnGet = this.updateAgeOnGet,
600
+ ttl = this.ttl,
601
+ noDisposeOnSet = this.noDisposeOnSet,
602
+ size = 0,
603
+ sizeCalculation = this.sizeCalculation,
604
+ noUpdateTTL = this.noUpdateTTL,
605
+ } = {}) {
606
+ if (!this.fetchMethod) {
607
+ return this.get(k, {allowStale, updateAgeOnGet})
608
+ }
609
+
610
+ const options = {
611
+ allowStale,
612
+ updateAgeOnGet,
613
+ ttl,
614
+ noDisposeOnSet,
615
+ size,
616
+ sizeCalculation,
617
+ noUpdateTTL,
618
+ }
619
+
620
+ let index = this.keyMap.get(k)
621
+ if (index === undefined) {
622
+ return this.backgroundFetch(k, index, options)
623
+ } else {
624
+ // in cache, maybe already fetching
625
+ const v = this.valList[index]
626
+ if (this.isBackgroundFetch(v)) {
627
+ return allowStale && v.__staleWhileFetching !== undefined
628
+ ? v.__staleWhileFetching : v
629
+ }
630
+
631
+ if (!this.isStale(index)) {
632
+ this.moveToTail(index)
633
+ if (updateAgeOnGet) {
634
+ this.updateItemAge(index)
635
+ }
636
+ return v
637
+ }
638
+
639
+ // ok, it is stale, and not already fetching
640
+ // refresh the cache.
641
+ const p = this.backgroundFetch(k, index, options)
642
+ return allowStale && p.__staleWhileFetching !== undefined
643
+ ? p.__staleWhileFetching : p
644
+ }
645
+ }
646
+
647
+ get (k, {
648
+ allowStale = this.allowStale,
649
+ updateAgeOnGet = this.updateAgeOnGet,
650
+ } = {}) {
651
+ const index = this.keyMap.get(k)
652
+ if (index !== undefined) {
653
+ const value = this.valList[index]
654
+ const fetching = this.isBackgroundFetch(value)
655
+ if (this.isStale(index)) {
656
+ // delete only if not an in-flight background fetch
657
+ if (!fetching) {
658
+ this.delete(k)
659
+ return allowStale ? value : undefined
660
+ } else {
661
+ return allowStale ? value.__staleWhileFetching : undefined
662
+ }
663
+ } else {
664
+ // if we're currently fetching it, we don't actually have it yet
665
+ // it's not stale, which means this isn't a staleWhileRefetching,
666
+ // so we just return undefined
667
+ if (fetching) {
668
+ return undefined
669
+ }
670
+ this.moveToTail(index)
671
+ if (updateAgeOnGet) {
672
+ this.updateItemAge(index)
673
+ }
674
+ return value
675
+ }
676
+ }
677
+ }
678
+
679
+ connect (p, n) {
680
+ this.prev[n] = p
681
+ this.next[p] = n
682
+ }
683
+
684
+ moveToTail (index) {
685
+ // if tail already, nothing to do
686
+ // if head, move head to next[index]
687
+ // else
688
+ // move next[prev[index]] to next[index] (head has no prev)
689
+ // move prev[next[index]] to prev[index]
690
+ // prev[index] = tail
691
+ // next[tail] = index
692
+ // tail = index
693
+ if (index !== this.tail) {
694
+ if (index === this.head) {
695
+ this.head = this.next[index]
696
+ } else {
697
+ this.connect(this.prev[index], this.next[index])
698
+ }
699
+ this.connect(this.tail, index)
700
+ this.tail = index
701
+ }
702
+ }
703
+
704
+ get del () {
705
+ deprecatedMethod('del', 'delete')
706
+ return this.delete
707
+ }
708
+ delete (k) {
709
+ let deleted = false
710
+ if (this.size !== 0) {
711
+ const index = this.keyMap.get(k)
712
+ if (index !== undefined) {
713
+ deleted = true
714
+ if (this.size === 1) {
715
+ this.clear()
716
+ } else {
717
+ this.removeItemSize(index)
718
+ const v = this.valList[index]
719
+ if (this.isBackgroundFetch(v)) {
720
+ v.__abortController.abort()
721
+ } else {
722
+ this.dispose(v, k, 'delete')
723
+ if (this.disposeAfter) {
724
+ this.disposed.push([v, k, 'delete'])
725
+ }
726
+ }
727
+ this.keyMap.delete(k)
728
+ this.keyList[index] = null
729
+ this.valList[index] = null
730
+ if (index === this.tail) {
731
+ this.tail = this.prev[index]
732
+ } else if (index === this.head) {
733
+ this.head = this.next[index]
734
+ } else {
735
+ this.next[this.prev[index]] = this.next[index]
736
+ this.prev[this.next[index]] = this.prev[index]
737
+ }
738
+ this.size --
739
+ this.free.push(index)
740
+ }
741
+ }
742
+ }
743
+ if (this.disposed) {
744
+ while (this.disposed.length) {
745
+ this.disposeAfter(...this.disposed.shift())
746
+ }
747
+ }
748
+ return deleted
749
+ }
750
+
751
+ clear () {
752
+ for (const index of this.rindexes({ allowStale: true })) {
753
+ const v = this.valList[index]
754
+ if (this.isBackgroundFetch(v)) {
755
+ v.__abortController.abort()
756
+ } else {
757
+ const k = this.keyList[index]
758
+ this.dispose(v, k, 'delete')
759
+ if (this.disposeAfter) {
760
+ this.disposed.push([v, k, 'delete'])
761
+ }
762
+ }
763
+ }
764
+
765
+ this.keyMap.clear()
766
+ this.valList.fill(null)
767
+ this.keyList.fill(null)
768
+ if (this.ttls) {
769
+ this.ttls.fill(0)
770
+ this.starts.fill(0)
771
+ }
772
+ if (this.sizes) {
773
+ this.sizes.fill(0)
774
+ }
775
+ this.head = 0
776
+ this.tail = 0
777
+ this.initialFill = 1
778
+ this.free.length = 0
779
+ this.calculatedSize = 0
780
+ this.size = 0
781
+ if (this.disposed) {
782
+ while (this.disposed.length) {
783
+ this.disposeAfter(...this.disposed.shift())
784
+ }
785
+ }
786
+ }
787
+ get reset () {
788
+ deprecatedMethod('reset', 'clear')
789
+ return this.clear
790
+ }
791
+
792
+ get length () {
793
+ deprecatedProperty('length', 'size')
794
+ return this.size
795
+ }
796
+ }
797
+
798
+ module.exports = LRUCache