@sebspark/promise-cache 2.0.2 → 2.0.4

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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { createClient, RedisClientOptions } from 'redis';
1
+ import { RedisClientOptions, createClient } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
3
  import { UUID } from 'node:crypto';
4
4
 
@@ -14,43 +14,39 @@ type SetParams<T> = {
14
14
  };
15
15
  type PersistorConstructorType = {
16
16
  redis?: RedisClientOptions;
17
- onError?: (c: string) => void;
18
- onSuccess?: (c: string) => void;
17
+ clientId?: UUID;
18
+ onError: (error: string) => void;
19
+ onSuccess: () => void;
19
20
  };
20
21
  declare class Persistor {
21
22
  client: ReturnType<typeof createClient> | null;
22
- private clientId;
23
+ private clientId?;
23
24
  private onError;
24
25
  private onSuccess;
25
26
  private isConnected;
26
27
  private readonly redis?;
27
- constructor(options: PersistorConstructorType);
28
+ constructor({ redis, clientId, onSuccess, onError, }: PersistorConstructorType);
28
29
  connect(): Promise<void>;
29
30
  startConnection(): Promise<unknown>;
30
31
  size(): Promise<number>;
31
32
  get<T>(key: string): Promise<GetType<T> | null>;
32
- getClientId(): UUID;
33
+ getClientId(): UUID | undefined;
33
34
  getIsClientConnected(): boolean;
34
35
  private createOptions;
35
36
  set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
36
37
  delete(key: string): Promise<void>;
37
38
  }
38
- declare const createPersistor: ({ redis, onError, onSuccess, }: {
39
- redis?: RedisClientOptions;
40
- onError?: () => void;
41
- onSuccess?: () => void;
42
- }) => Persistor;
43
39
 
44
40
  type PromiseCacheOptions = {
45
41
  ttlInSeconds?: number;
46
42
  caseSensitive?: boolean;
47
43
  redis?: RedisClientOptions;
48
- onError?: () => void;
44
+ onError?: (error: string) => void;
49
45
  onSuccess?: () => void;
50
46
  };
51
- declare const promises: {};
52
47
  declare class PromiseCache<U> {
53
48
  persistor: Persistor;
49
+ private clientId;
54
50
  private readonly caseSensitive;
55
51
  private readonly ttl?;
56
52
  /**
@@ -100,4 +96,4 @@ declare class LocalStorage {
100
96
  }
101
97
  declare const createLocalMemoryClient: () => LocalStorage;
102
98
 
103
- export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient, createPersistor, promises };
99
+ export { LocalStorage, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { createClient, RedisClientOptions } from 'redis';
1
+ import { RedisClientOptions, createClient } from 'redis';
2
2
  export { RedisClientOptions } from 'redis';
3
3
  import { UUID } from 'node:crypto';
4
4
 
@@ -14,43 +14,39 @@ type SetParams<T> = {
14
14
  };
15
15
  type PersistorConstructorType = {
16
16
  redis?: RedisClientOptions;
17
- onError?: (c: string) => void;
18
- onSuccess?: (c: string) => void;
17
+ clientId?: UUID;
18
+ onError: (error: string) => void;
19
+ onSuccess: () => void;
19
20
  };
20
21
  declare class Persistor {
21
22
  client: ReturnType<typeof createClient> | null;
22
- private clientId;
23
+ private clientId?;
23
24
  private onError;
24
25
  private onSuccess;
25
26
  private isConnected;
26
27
  private readonly redis?;
27
- constructor(options: PersistorConstructorType);
28
+ constructor({ redis, clientId, onSuccess, onError, }: PersistorConstructorType);
28
29
  connect(): Promise<void>;
29
30
  startConnection(): Promise<unknown>;
30
31
  size(): Promise<number>;
31
32
  get<T>(key: string): Promise<GetType<T> | null>;
32
- getClientId(): UUID;
33
+ getClientId(): UUID | undefined;
33
34
  getIsClientConnected(): boolean;
34
35
  private createOptions;
35
36
  set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
36
37
  delete(key: string): Promise<void>;
37
38
  }
38
- declare const createPersistor: ({ redis, onError, onSuccess, }: {
39
- redis?: RedisClientOptions;
40
- onError?: () => void;
41
- onSuccess?: () => void;
42
- }) => Persistor;
43
39
 
44
40
  type PromiseCacheOptions = {
45
41
  ttlInSeconds?: number;
46
42
  caseSensitive?: boolean;
47
43
  redis?: RedisClientOptions;
48
- onError?: () => void;
44
+ onError?: (error: string) => void;
49
45
  onSuccess?: () => void;
50
46
  };
51
- declare const promises: {};
52
47
  declare class PromiseCache<U> {
53
48
  persistor: Persistor;
49
+ private clientId;
54
50
  private readonly caseSensitive;
55
51
  private readonly ttl?;
56
52
  /**
@@ -100,4 +96,4 @@ declare class LocalStorage {
100
96
  }
101
97
  declare const createLocalMemoryClient: () => LocalStorage;
102
98
 
103
- export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient, createPersistor, promises };
99
+ export { LocalStorage, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient };
package/dist/index.js CHANGED
@@ -23,14 +23,14 @@ __export(src_exports, {
23
23
  LocalStorage: () => LocalStorage,
24
24
  Persistor: () => Persistor,
25
25
  PromiseCache: () => PromiseCache,
26
- createLocalMemoryClient: () => createLocalMemoryClient,
27
- createPersistor: () => createPersistor,
28
- promises: () => promises
26
+ createLocalMemoryClient: () => createLocalMemoryClient
29
27
  });
30
28
  module.exports = __toCommonJS(src_exports);
31
29
 
32
- // src/persistor.ts
30
+ // src/promiseCache.ts
33
31
  var import_node_crypto = require("crypto");
32
+
33
+ // src/persistor.ts
34
34
  var import_retry = require("@sebspark/retry");
35
35
  var import_redis = require("redis");
36
36
 
@@ -78,15 +78,20 @@ var CACHE_CLIENT = import_redis.createClient;
78
78
  var isTestRunning = process.env.NODE_ENV === "test";
79
79
  var Persistor = class {
80
80
  client = null;
81
- clientId = (0, import_node_crypto.randomUUID)();
81
+ clientId;
82
82
  onError;
83
83
  onSuccess;
84
84
  isConnected = false;
85
85
  redis;
86
- constructor(options) {
87
- const { redis, onError, onSuccess } = options;
86
+ constructor({
87
+ redis,
88
+ clientId,
89
+ onSuccess,
90
+ onError
91
+ }) {
88
92
  this.onError = onError;
89
93
  this.onSuccess = onSuccess;
94
+ this.clientId = clientId;
90
95
  if (redis && !isTestRunning) {
91
96
  this.redis = redis;
92
97
  } else {
@@ -111,36 +116,25 @@ var Persistor = class {
111
116
  await (0, import_retry.retry)(() => this.startConnection(), settings);
112
117
  }
113
118
  async startConnection() {
114
- var _a, _b;
115
- try {
116
- this.client = CACHE_CLIENT(this.redis);
117
- this.client.on("error", (err) => {
118
- var _a2, _b2;
119
- this.isConnected = false;
120
- if (this.onError) {
121
- this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
122
- }
123
- throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
124
- });
125
- this.client.on("connect", () => {
126
- var _a2, _b2, _c, _d;
127
- this.isConnected = true;
128
- if (this.onSuccess) {
129
- this.onSuccess(
130
- `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
131
- );
132
- }
133
- console.log(
134
- `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
135
- );
136
- });
137
- return await this.client.connect();
138
- } catch (err) {
139
- if (this.onError) {
140
- this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
119
+ return new Promise((resolve, reject) => {
120
+ try {
121
+ this.client = CACHE_CLIENT(this.redis);
122
+ this.client.on("error", (err) => {
123
+ this.isConnected = false;
124
+ this.onError(err);
125
+ reject();
126
+ });
127
+ this.client.on("connect", () => {
128
+ this.isConnected = true;
129
+ this.onSuccess();
130
+ resolve(true);
131
+ });
132
+ this.client.connect();
133
+ } catch (err) {
134
+ this.onError(`${err}`);
135
+ reject();
141
136
  }
142
- throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
143
- }
137
+ });
144
138
  }
145
139
  async size() {
146
140
  if (!this.client) {
@@ -176,7 +170,7 @@ var Persistor = class {
176
170
  }
177
171
  async set(key, { value, timestamp, ttl }) {
178
172
  if (!this.client) {
179
- throw new Error("Client not initialized");
173
+ throw new Error("Client not connected");
180
174
  }
181
175
  try {
182
176
  const serializedData = JSON.stringify({ value, ttl, timestamp });
@@ -188,7 +182,7 @@ var Persistor = class {
188
182
  }
189
183
  async delete(key) {
190
184
  if (!this.client) {
191
- throw new Error("Client not initialized");
185
+ throw new Error("Client not connected");
192
186
  }
193
187
  try {
194
188
  await this.client.del(key);
@@ -197,37 +191,39 @@ var Persistor = class {
197
191
  }
198
192
  }
199
193
  };
194
+
195
+ // src/promiseCache.ts
200
196
  var persistors = {};
201
- var createPersistor = ({
197
+ var getPersistor = ({
202
198
  redis,
203
199
  onError,
204
- onSuccess
200
+ onSuccess,
201
+ clientId
205
202
  }) => {
206
- if (redis) {
207
- let connectionName = redis.url;
208
- if (redis.name) {
209
- connectionName = redis.name;
210
- }
211
- const key = connectionName;
212
- if (!persistors[key]) {
213
- persistors[key] = new Persistor({
214
- redis,
215
- onError,
216
- onSuccess
217
- });
218
- }
219
- return persistors[key];
203
+ const connectionName = redis ? (redis == null ? void 0 : redis.name) || "default" : "local";
204
+ if (!persistors[connectionName]) {
205
+ persistors[connectionName] = new Persistor({
206
+ redis,
207
+ onError: (error) => {
208
+ onError == null ? void 0 : onError(error);
209
+ console.error(
210
+ `\u274C REDIS | Client Error | ${connectionName} | ${redis == null ? void 0 : redis.url}: ${error}`
211
+ );
212
+ },
213
+ onSuccess: () => {
214
+ onSuccess == null ? void 0 : onSuccess();
215
+ console.log(
216
+ `\u{1F4E6} REDIS | Connection Ready | ${connectionName} | ${redis == null ? void 0 : redis.url}`
217
+ );
218
+ },
219
+ clientId
220
+ });
220
221
  }
221
- return new Persistor({
222
- onSuccess,
223
- onError
224
- });
222
+ return persistors[connectionName];
225
223
  };
226
-
227
- // src/promiseCache.ts
228
- var promises = {};
229
224
  var PromiseCache = class {
230
225
  persistor;
226
+ clientId = (0, import_node_crypto.randomUUID)();
231
227
  caseSensitive;
232
228
  ttl;
233
229
  // Time to live in milliseconds.
@@ -243,7 +239,12 @@ var PromiseCache = class {
243
239
  onSuccess,
244
240
  onError
245
241
  }) {
246
- this.persistor = createPersistor({ redis, onError, onSuccess });
242
+ this.persistor = getPersistor({
243
+ redis,
244
+ onError,
245
+ onSuccess,
246
+ clientId: this.clientId
247
+ });
247
248
  this.caseSensitive = caseSensitive;
248
249
  if (ttlInSeconds) {
249
250
  this.ttl = ttlInSeconds * 1e3;
@@ -313,7 +314,5 @@ var PromiseCache = class {
313
314
  LocalStorage,
314
315
  Persistor,
315
316
  PromiseCache,
316
- createLocalMemoryClient,
317
- createPersistor,
318
- promises
317
+ createLocalMemoryClient
319
318
  });
package/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
- // src/persistor.ts
1
+ // src/promiseCache.ts
2
2
  import { randomUUID } from "node:crypto";
3
+
4
+ // src/persistor.ts
3
5
  import { retry } from "@sebspark/retry";
4
6
  import { createClient } from "redis";
5
7
 
@@ -47,15 +49,20 @@ var CACHE_CLIENT = createClient;
47
49
  var isTestRunning = process.env.NODE_ENV === "test";
48
50
  var Persistor = class {
49
51
  client = null;
50
- clientId = randomUUID();
52
+ clientId;
51
53
  onError;
52
54
  onSuccess;
53
55
  isConnected = false;
54
56
  redis;
55
- constructor(options) {
56
- const { redis, onError, onSuccess } = options;
57
+ constructor({
58
+ redis,
59
+ clientId,
60
+ onSuccess,
61
+ onError
62
+ }) {
57
63
  this.onError = onError;
58
64
  this.onSuccess = onSuccess;
65
+ this.clientId = clientId;
59
66
  if (redis && !isTestRunning) {
60
67
  this.redis = redis;
61
68
  } else {
@@ -80,36 +87,25 @@ var Persistor = class {
80
87
  await retry(() => this.startConnection(), settings);
81
88
  }
82
89
  async startConnection() {
83
- var _a, _b;
84
- try {
85
- this.client = CACHE_CLIENT(this.redis);
86
- this.client.on("error", (err) => {
87
- var _a2, _b2;
88
- this.isConnected = false;
89
- if (this.onError) {
90
- this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
91
- }
92
- throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
93
- });
94
- this.client.on("connect", () => {
95
- var _a2, _b2, _c, _d;
96
- this.isConnected = true;
97
- if (this.onSuccess) {
98
- this.onSuccess(
99
- `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
100
- );
101
- }
102
- console.log(
103
- `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
104
- );
105
- });
106
- return await this.client.connect();
107
- } catch (err) {
108
- if (this.onError) {
109
- this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
90
+ return new Promise((resolve, reject) => {
91
+ try {
92
+ this.client = CACHE_CLIENT(this.redis);
93
+ this.client.on("error", (err) => {
94
+ this.isConnected = false;
95
+ this.onError(err);
96
+ reject();
97
+ });
98
+ this.client.on("connect", () => {
99
+ this.isConnected = true;
100
+ this.onSuccess();
101
+ resolve(true);
102
+ });
103
+ this.client.connect();
104
+ } catch (err) {
105
+ this.onError(`${err}`);
106
+ reject();
110
107
  }
111
- throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
112
- }
108
+ });
113
109
  }
114
110
  async size() {
115
111
  if (!this.client) {
@@ -145,7 +141,7 @@ var Persistor = class {
145
141
  }
146
142
  async set(key, { value, timestamp, ttl }) {
147
143
  if (!this.client) {
148
- throw new Error("Client not initialized");
144
+ throw new Error("Client not connected");
149
145
  }
150
146
  try {
151
147
  const serializedData = JSON.stringify({ value, ttl, timestamp });
@@ -157,7 +153,7 @@ var Persistor = class {
157
153
  }
158
154
  async delete(key) {
159
155
  if (!this.client) {
160
- throw new Error("Client not initialized");
156
+ throw new Error("Client not connected");
161
157
  }
162
158
  try {
163
159
  await this.client.del(key);
@@ -166,37 +162,39 @@ var Persistor = class {
166
162
  }
167
163
  }
168
164
  };
165
+
166
+ // src/promiseCache.ts
169
167
  var persistors = {};
170
- var createPersistor = ({
168
+ var getPersistor = ({
171
169
  redis,
172
170
  onError,
173
- onSuccess
171
+ onSuccess,
172
+ clientId
174
173
  }) => {
175
- if (redis) {
176
- let connectionName = redis.url;
177
- if (redis.name) {
178
- connectionName = redis.name;
179
- }
180
- const key = connectionName;
181
- if (!persistors[key]) {
182
- persistors[key] = new Persistor({
183
- redis,
184
- onError,
185
- onSuccess
186
- });
187
- }
188
- return persistors[key];
174
+ const connectionName = redis ? (redis == null ? void 0 : redis.name) || "default" : "local";
175
+ if (!persistors[connectionName]) {
176
+ persistors[connectionName] = new Persistor({
177
+ redis,
178
+ onError: (error) => {
179
+ onError == null ? void 0 : onError(error);
180
+ console.error(
181
+ `\u274C REDIS | Client Error | ${connectionName} | ${redis == null ? void 0 : redis.url}: ${error}`
182
+ );
183
+ },
184
+ onSuccess: () => {
185
+ onSuccess == null ? void 0 : onSuccess();
186
+ console.log(
187
+ `\u{1F4E6} REDIS | Connection Ready | ${connectionName} | ${redis == null ? void 0 : redis.url}`
188
+ );
189
+ },
190
+ clientId
191
+ });
189
192
  }
190
- return new Persistor({
191
- onSuccess,
192
- onError
193
- });
193
+ return persistors[connectionName];
194
194
  };
195
-
196
- // src/promiseCache.ts
197
- var promises = {};
198
195
  var PromiseCache = class {
199
196
  persistor;
197
+ clientId = randomUUID();
200
198
  caseSensitive;
201
199
  ttl;
202
200
  // Time to live in milliseconds.
@@ -212,7 +210,12 @@ var PromiseCache = class {
212
210
  onSuccess,
213
211
  onError
214
212
  }) {
215
- this.persistor = createPersistor({ redis, onError, onSuccess });
213
+ this.persistor = getPersistor({
214
+ redis,
215
+ onError,
216
+ onSuccess,
217
+ clientId: this.clientId
218
+ });
216
219
  this.caseSensitive = caseSensitive;
217
220
  if (ttlInSeconds) {
218
221
  this.ttl = ttlInSeconds * 1e3;
@@ -281,7 +284,5 @@ export {
281
284
  LocalStorage,
282
285
  Persistor,
283
286
  PromiseCache,
284
- createLocalMemoryClient,
285
- createPersistor,
286
- promises
287
+ createLocalMemoryClient
287
288
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",