@bgord/bun 1.7.1 → 1.7.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bgord/bun",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "author": "Bartosz Gordon",
@@ -1,32 +1,54 @@
1
- import { LRUCache } from "lru-cache";
1
+ import type { LRUCache } from "lru-cache";
2
2
  import type { CacheRepositoryPort, CacheRepositoryTtlType } from "./cache-repository.port";
3
3
  import type { Hash } from "./hash.vo";
4
4
  import type { HashValueType } from "./hash-value.vo";
5
5
 
6
+ export const CacheRepositoryLruCacheAdapterError = {
7
+ MissingDependency: "cache.repository.lru.cache.adapter.error.missing.dependency",
8
+ };
9
+
6
10
  export class CacheRepositoryLruCacheAdapter implements CacheRepositoryPort {
7
- private readonly store;
11
+ private readonly store: LRUCache<HashValueType, any>;
12
+
13
+ private constructor(store: LRUCache<HashValueType, any>) {
14
+ this.store = store;
15
+ }
16
+
17
+ static async build(config: CacheRepositoryTtlType): Promise<CacheRepositoryLruCacheAdapter> {
18
+ // biome-ignore lint: lint/suspicious/noAssignInExpressions
19
+ let LRUCacheConstructor;
8
20
 
9
- constructor(config: CacheRepositoryTtlType) {
10
- this.store = new LRUCache<HashValueType, any>({
21
+ try {
22
+ const module = await import("lru-cache");
23
+
24
+ LRUCacheConstructor = module.LRUCache;
25
+ } catch {
26
+ throw new Error(CacheRepositoryLruCacheAdapterError.MissingDependency);
27
+ }
28
+
29
+ const store = new LRUCacheConstructor<HashValueType, any>({
11
30
  max: 100_000,
12
31
  ttl: config.type === "finite" ? config.ttl.ms : undefined,
13
32
  ttlAutopurge: true,
14
33
  });
34
+
35
+ return new CacheRepositoryLruCacheAdapter(store);
15
36
  }
16
37
 
38
+ // 5. Clean implementation without null checks
17
39
  async get<T>(subject: Hash): Promise<T | null> {
18
40
  return (this.store.get(subject.get()) as T) ?? null;
19
41
  }
20
42
 
21
- async set<T>(subject: Hash, value: T) {
43
+ async set<T>(subject: Hash, value: T): Promise<void> {
22
44
  this.store.set(subject.get(), value);
23
45
  }
24
46
 
25
- async delete(subject: Hash) {
47
+ async delete(subject: Hash): Promise<void> {
26
48
  this.store.delete(subject.get());
27
49
  }
28
50
 
29
- async flush() {
51
+ async flush(): Promise<void> {
30
52
  this.store.clear();
31
53
  }
32
54
  }