@helia/verified-fetch 2.6.17 → 2.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils/tlru.ts CHANGED
@@ -1,4 +1,4 @@
1
- import hashlru from 'hashlru'
1
+ import QuickLRU from 'quick-lru'
2
2
 
3
3
  /**
4
4
  * Time Aware Least Recent Used Cache
@@ -6,30 +6,20 @@ import hashlru from 'hashlru'
6
6
  * @see https://arxiv.org/pdf/1801.00390
7
7
  */
8
8
  export class TLRU<T> {
9
- private readonly lru: ReturnType<typeof hashlru>
9
+ private readonly lru: QuickLRU<string, T>
10
10
 
11
11
  constructor (maxSize: number) {
12
- this.lru = hashlru(maxSize)
12
+ this.lru = new QuickLRU({ maxSize })
13
13
  }
14
14
 
15
15
  get (key: string): T | undefined {
16
- const value = this.lru.get(key)
17
-
18
- if (value != null) {
19
- if (value.expire != null && value.expire < Date.now()) {
20
- this.lru.remove(key)
21
-
22
- return undefined
23
- }
24
-
25
- return value.value
26
- }
27
-
28
- return undefined
16
+ return this.lru.get(key)
29
17
  }
30
18
 
31
19
  set (key: string, value: T, ttlMs: number): void {
32
- this.lru.set(key, { value, expire: Date.now() + ttlMs })
20
+ this.lru.set(key, value, {
21
+ maxAge: Date.now() + ttlMs
22
+ })
33
23
  }
34
24
 
35
25
  has (key: string): boolean {
@@ -43,7 +33,7 @@ export class TLRU<T> {
43
33
  }
44
34
 
45
35
  remove (key: string): void {
46
- this.lru.remove(key)
36
+ this.lru.delete(key)
47
37
  }
48
38
 
49
39
  clear (): void {
@@ -1,7 +1,7 @@
1
1
  import { ipns as heliaIpns } from '@helia/ipns'
2
2
  import { prefixLogger } from '@libp2p/logger'
3
- import { LRUCache } from 'lru-cache'
4
3
  import { CustomProgressEvent } from 'progress-events'
4
+ import QuickLRU from 'quick-lru'
5
5
  import { ByteRangeContextPlugin } from './plugins/plugin-handle-byte-range-context.js'
6
6
  import { CarPlugin } from './plugins/plugin-handle-car.js'
7
7
  import { DagCborPlugin } from './plugins/plugin-handle-dag-cbor.js'
@@ -62,7 +62,7 @@ export class VerifiedFetch {
62
62
  private readonly ipns: IPNS
63
63
  private readonly log: Logger
64
64
  private readonly contentTypeParser: ContentTypeParser | undefined
65
- private readonly blockstoreSessions: LRUCache<string, SessionBlockstore>
65
+ private readonly blockstoreSessions: QuickLRU<string, SessionBlockstore>
66
66
  private serverTimingHeaders: string[] = []
67
67
  private readonly withServerTiming: boolean
68
68
  private readonly plugins: VerifiedFetchPlugin[] = []
@@ -72,10 +72,10 @@ export class VerifiedFetch {
72
72
  this.log = helia.logger.forComponent('helia:verified-fetch')
73
73
  this.ipns = ipns ?? heliaIpns(helia)
74
74
  this.contentTypeParser = init?.contentTypeParser ?? contentTypeParser
75
- this.blockstoreSessions = new LRUCache({
76
- max: init?.sessionCacheSize ?? SESSION_CACHE_MAX_SIZE,
77
- ttl: init?.sessionTTLms ?? SESSION_CACHE_TTL_MS,
78
- dispose: (store) => {
75
+ this.blockstoreSessions = new QuickLRU({
76
+ maxSize: init?.sessionCacheSize ?? SESSION_CACHE_MAX_SIZE,
77
+ maxAge: init?.sessionTTLms ?? SESSION_CACHE_TTL_MS,
78
+ onEviction: (key, store) => {
79
79
  store.close()
80
80
  }
81
81
  })