cache-manager 5.5.2 → 5.5.3

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -104,13 +104,13 @@ As `caching()` requires async functionality to resolve some stores, this is not
104
104
  If you need to create a cache store synchronously, you can instead use `createCache()`:
105
105
 
106
106
  ```typescript
107
- import { createCache, memoryStore } from 'node-cache-manager';
107
+ import { createCache, memoryStore } from 'cache-manager';
108
108
 
109
109
  // Create memory cache synchronously
110
- const memoryCache = createCache(memoryStore(), {
110
+ const memoryCache = createCache(memoryStore({
111
111
  max: 100,
112
112
  ttl: 10 * 1000 /*milliseconds*/,
113
- });
113
+ }));
114
114
 
115
115
  // Default parameter in function
116
116
  function myService(cache = createCache(memoryStore())) {}
package/dist/caching.d.ts CHANGED
@@ -27,13 +27,26 @@ export type FactoryStore<S extends Store, T extends Record<string, unknown> = ne
27
27
  export type Stores<S extends Store, T extends Record<string, unknown>> = 'memory' | Store | FactoryStore<S, T>;
28
28
  export type CachingConfig<T> = MemoryConfig | StoreConfig | FactoryConfig<T>;
29
29
  export type WrapTTL<T> = Milliseconds | ((v: T) => Milliseconds);
30
+ export type ErrorEvent<T = never> = {
31
+ operation: 'get' | 'set' | 'del' | 'wrap' | 'reset';
32
+ error: unknown;
33
+ key?: string;
34
+ data?: T;
35
+ } | {
36
+ operation: 'mget' | 'mset' | 'mdel';
37
+ error: unknown;
38
+ keys: string[];
39
+ data?: T[];
40
+ };
41
+ export type ErrorEventHandler<T = unknown> = (event: ErrorEvent<T>) => void;
30
42
  export type Cache<S extends Store = Store> = {
31
43
  store: S;
32
44
  set: (key: string, value: unknown, ttl?: Milliseconds) => Promise<void>;
33
45
  get: <T>(key: string) => Promise<T | undefined>;
34
46
  del: (key: string) => Promise<void>;
35
47
  reset: () => Promise<void>;
36
- on: (event: 'error', handler: (error: Error) => void) => void;
48
+ on: <T>(event: 'error', handler: ErrorEventHandler<T>) => void;
49
+ removeListener: <T>(event: 'error', handler: ErrorEventHandler<T>) => void;
37
50
  wrap<T>(key: string, function_: () => Promise<T>, ttl?: WrapTTL<T>, refreshThreshold?: Milliseconds): Promise<T>;
38
51
  };
39
52
  export declare function caching(name: 'memory', arguments_?: MemoryConfig): Promise<MemoryCache>;
package/dist/caching.js CHANGED
@@ -42,12 +42,18 @@ function createCache(store, arguments_) {
42
42
  const refreshThresholdConfig = refreshThreshold ?? arguments_?.refreshThreshold ?? 0;
43
43
  return (0, promise_coalesce_1.coalesceAsync)(key, async () => {
44
44
  const value = await store.get(key).catch(error => {
45
- eventEmitter.emit('error', error);
45
+ const errorEvent = { error, key, operation: 'wrap' };
46
+ eventEmitter.emit('error', errorEvent);
46
47
  });
47
48
  if (value === undefined) {
48
49
  const result = await function_();
49
50
  const cacheTtl = typeof ttl === 'function' ? ttl(result) : ttl;
50
- await store.set(key, result, cacheTtl).catch(error => eventEmitter.emit('error', error));
51
+ await store.set(key, result, cacheTtl).catch(error => {
52
+ const errorEvent = {
53
+ error, key, operation: 'wrap', data: result,
54
+ };
55
+ eventEmitter.emit('error', errorEvent);
56
+ });
51
57
  return result;
52
58
  }
53
59
  if (refreshThresholdConfig) {
@@ -57,7 +63,10 @@ function createCache(store, arguments_) {
57
63
  (0, promise_coalesce_1.coalesceAsync)(`+++${key}`, function_)
58
64
  .then(async (result) => store.set(key, result, cacheTtl))
59
65
  .catch(async (error) => {
60
- eventEmitter.emit('error', error);
66
+ const errorEvent = {
67
+ error, key, operation: 'wrap', data: value,
68
+ };
69
+ eventEmitter.emit('error', errorEvent);
61
70
  eventEmitter.emit('onBackgroundRefreshError', error);
62
71
  if (arguments_?.onBackgroundRefreshError) {
63
72
  arguments_.onBackgroundRefreshError(error);
@@ -78,6 +87,7 @@ function createCache(store, arguments_) {
78
87
  set: async (key, value, ttl) => store.set(key, value, ttl),
79
88
  reset: async () => store.reset(),
80
89
  on: (event, handler) => eventEmitter.on('error', handler),
90
+ removeListener: (event, handler) => eventEmitter.removeListener(event, handler),
81
91
  };
82
92
  }
83
93
  exports.createCache = createCache;
@@ -10,6 +10,9 @@ const eventemitter3_1 = __importDefault(require("eventemitter3"));
10
10
  */
11
11
  function multiCaching(caches) {
12
12
  const eventEmitter = new eventemitter3_1.default();
13
+ for (const cache of caches) {
14
+ cache.on('error', event => eventEmitter.emit('error', event));
15
+ }
13
16
  const get = async (key) => {
14
17
  for (const cache of caches) {
15
18
  try {
@@ -20,18 +23,27 @@ function multiCaching(caches) {
20
23
  }
21
24
  }
22
25
  catch (error) {
23
- eventEmitter.emit('error', error);
26
+ const errorEvent = { error, key, operation: 'get' };
27
+ eventEmitter.emit('error', errorEvent);
24
28
  }
25
29
  }
26
30
  };
27
31
  const set = async (key, data, ttl) => {
28
- await Promise.all(caches.map(async (cache) => cache.set(key, data, ttl))).catch(error => eventEmitter.emit('error', error));
32
+ await Promise.all(caches.map(async (cache) => cache.set(key, data, ttl))).catch(error => {
33
+ const errorEvent = {
34
+ error, key, operation: 'set', data,
35
+ };
36
+ eventEmitter.emit('error', errorEvent);
37
+ });
29
38
  };
30
39
  return {
31
40
  get,
32
41
  set,
33
42
  async del(key) {
34
- await Promise.all(caches.map(async (cache) => cache.del(key))).catch(error => eventEmitter.emit('error', error));
43
+ await Promise.all(caches.map(async (cache) => cache.del(key))).catch(error => {
44
+ const errorEvent = { error, key, operation: 'del' };
45
+ eventEmitter.emit('error', errorEvent);
46
+ });
35
47
  },
36
48
  async wrap(key, function_, ttl, refreshThreshold) {
37
49
  let value;
@@ -45,7 +57,8 @@ function multiCaching(caches) {
45
57
  }
46
58
  }
47
59
  catch (error) {
48
- eventEmitter.emit('error', error);
60
+ const errorEvent = { error, key, operation: 'wrap' };
61
+ eventEmitter.emit('error', errorEvent);
49
62
  }
50
63
  }
51
64
  if (value === undefined) {
@@ -55,12 +68,20 @@ function multiCaching(caches) {
55
68
  return result;
56
69
  }
57
70
  const cacheTtl = typeof ttl === 'function' ? ttl(value) : ttl;
58
- await Promise.all(caches.slice(0, i).map(async (cache) => cache.set(key, value, cacheTtl))).then().catch(error => eventEmitter.emit('error', error));
71
+ await Promise.all(caches.slice(0, i).map(async (cache) => cache.set(key, value, cacheTtl))).then().catch(error => {
72
+ const errorEvent = {
73
+ error, key, operation: 'wrap', data: value,
74
+ };
75
+ eventEmitter.emit('error', errorEvent);
76
+ });
59
77
  await caches[i].wrap(key, function_, ttl, refreshThreshold).then(); // Call wrap for store for internal refreshThreshold logic, see: src/caching.ts caching.wrap
60
78
  return value;
61
79
  },
62
80
  async reset() {
63
- await Promise.all(caches.map(async (x) => x.reset())).catch(error => eventEmitter.emit('error', error));
81
+ await Promise.all(caches.map(async (x) => x.reset())).catch(error => {
82
+ const errorEvent = { error, key: '', operation: 'reset' };
83
+ eventEmitter.emit('error', errorEvent);
84
+ });
64
85
  },
65
86
  async mget(...keys) {
66
87
  const values = Array.from({ length: keys.length }).fill(undefined);
@@ -78,19 +99,31 @@ function multiCaching(caches) {
78
99
  }
79
100
  }
80
101
  catch (error) {
81
- eventEmitter.emit('error', error);
102
+ const errorEvent = { error, keys, operation: 'mget' };
103
+ eventEmitter.emit('error', errorEvent);
82
104
  }
83
105
  }
84
106
  return values;
85
107
  },
86
108
  async mset(arguments_, ttl) {
87
- await Promise.all(caches.map(async (cache) => cache.store.mset(arguments_, ttl))).catch(error => eventEmitter.emit('error', error));
109
+ await Promise.all(caches.map(async (cache) => cache.store.mset(arguments_, ttl))).catch(error => {
110
+ const keys = arguments_.map(([key]) => key);
111
+ const data = arguments_.map(([, value]) => value);
112
+ const errorEvent = {
113
+ error, keys, data, operation: 'mset',
114
+ };
115
+ eventEmitter.emit('error', errorEvent);
116
+ });
88
117
  },
89
118
  async mdel(...keys) {
90
119
  await Promise.all(caches.map(async (cache) => cache.store.mdel(...keys)))
91
- .catch(error => eventEmitter.emit('error', error));
120
+ .catch(error => {
121
+ const errorEvent = { error, keys, operation: 'mdel' };
122
+ eventEmitter.emit('error', errorEvent);
123
+ });
92
124
  },
93
125
  on: (event, handler) => eventEmitter.on('error', handler),
126
+ removeListener: (event, handler) => eventEmitter.removeListener(event, handler),
94
127
  };
95
128
  }
96
129
  exports.multiCaching = multiCaching;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "5.5.2",
3
+ "version": "5.5.3",
4
4
  "description": "Cache module for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,13 +8,6 @@
8
8
  "dist/**/*.js",
9
9
  "dist/**/*.d.ts"
10
10
  ],
11
- "scripts": {
12
- "build": "tsc -p tsconfig.build.json",
13
- "clean": "rimraf ./dist ./coverage ./node_modules ./package-lock.json ./yarn.lock ./pnpm-lock.yaml",
14
- "test": "xo --fix && vitest run --coverage",
15
- "test:ci": "xo && vitest run",
16
- "prepare": "pnpm build"
17
- },
18
11
  "repository": {
19
12
  "type": "git",
20
13
  "url": "https://github.com/jaredwray/cache-manager.git"
@@ -65,5 +58,11 @@
65
58
  "ts",
66
59
  "tsx"
67
60
  ]
61
+ },
62
+ "scripts": {
63
+ "build": "rimraf ./dist && tsc -p tsconfig.build.json",
64
+ "clean": "rimraf ./dist ./coverage ./node_modules ./package-lock.json ./yarn.lock ./pnpm-lock.yaml",
65
+ "test": "xo --fix && vitest run --coverage",
66
+ "test:ci": "xo && vitest run"
68
67
  }
69
- }
68
+ }