@sebspark/promise-cache 2.0.2 → 2.0.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/dist/index.d.mts +7 -11
- package/dist/index.d.ts +7 -11
- package/dist/index.js +38 -38
- package/dist/index.mjs +37 -35
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RedisClientOptions, createClient } from 'redis';
|
|
2
2
|
export { RedisClientOptions } from 'redis';
|
|
3
3
|
import { UUID } from 'node:crypto';
|
|
4
4
|
|
|
@@ -14,32 +14,28 @@ type SetParams<T> = {
|
|
|
14
14
|
};
|
|
15
15
|
type PersistorConstructorType = {
|
|
16
16
|
redis?: RedisClientOptions;
|
|
17
|
+
clientId?: UUID;
|
|
17
18
|
onError?: (c: string) => void;
|
|
18
19
|
onSuccess?: (c: string) => 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(
|
|
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;
|
|
@@ -48,9 +44,9 @@ type PromiseCacheOptions = {
|
|
|
48
44
|
onError?: () => 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
|
|
99
|
+
export { LocalStorage, Persistor, type PersistorConstructorType, PromiseCache, type PromiseCacheOptions, createLocalMemoryClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RedisClientOptions, createClient } from 'redis';
|
|
2
2
|
export { RedisClientOptions } from 'redis';
|
|
3
3
|
import { UUID } from 'node:crypto';
|
|
4
4
|
|
|
@@ -14,32 +14,28 @@ type SetParams<T> = {
|
|
|
14
14
|
};
|
|
15
15
|
type PersistorConstructorType = {
|
|
16
16
|
redis?: RedisClientOptions;
|
|
17
|
+
clientId?: UUID;
|
|
17
18
|
onError?: (c: string) => void;
|
|
18
19
|
onSuccess?: (c: string) => 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(
|
|
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;
|
|
@@ -48,9 +44,9 @@ type PromiseCacheOptions = {
|
|
|
48
44
|
onError?: () => 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
|
|
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/
|
|
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
|
|
81
|
+
clientId;
|
|
82
82
|
onError;
|
|
83
83
|
onSuccess;
|
|
84
84
|
isConnected = false;
|
|
85
85
|
redis;
|
|
86
|
-
constructor(
|
|
87
|
-
|
|
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 {
|
|
@@ -175,8 +180,8 @@ var Persistor = class {
|
|
|
175
180
|
return {};
|
|
176
181
|
}
|
|
177
182
|
async set(key, { value, timestamp, ttl }) {
|
|
178
|
-
if (!this.client) {
|
|
179
|
-
throw new Error("Client not
|
|
183
|
+
if (!this.isConnected || !this.client) {
|
|
184
|
+
throw new Error("Client not connected");
|
|
180
185
|
}
|
|
181
186
|
try {
|
|
182
187
|
const serializedData = JSON.stringify({ value, ttl, timestamp });
|
|
@@ -187,8 +192,8 @@ var Persistor = class {
|
|
|
187
192
|
}
|
|
188
193
|
}
|
|
189
194
|
async delete(key) {
|
|
190
|
-
if (!this.client) {
|
|
191
|
-
throw new Error("Client not
|
|
195
|
+
if (!this.isConnected || !this.client) {
|
|
196
|
+
throw new Error("Client not connected");
|
|
192
197
|
}
|
|
193
198
|
try {
|
|
194
199
|
await this.client.del(key);
|
|
@@ -197,37 +202,29 @@ var Persistor = class {
|
|
|
197
202
|
}
|
|
198
203
|
}
|
|
199
204
|
};
|
|
205
|
+
|
|
206
|
+
// src/promiseCache.ts
|
|
200
207
|
var persistors = {};
|
|
201
|
-
var
|
|
208
|
+
var getPersistor = ({
|
|
202
209
|
redis,
|
|
203
210
|
onError,
|
|
204
|
-
onSuccess
|
|
211
|
+
onSuccess,
|
|
212
|
+
clientId
|
|
205
213
|
}) => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
redis,
|
|
215
|
-
onError,
|
|
216
|
-
onSuccess
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
return persistors[key];
|
|
214
|
+
const connectionName = (redis == null ? void 0 : redis.name) || "default";
|
|
215
|
+
if (!persistors[connectionName]) {
|
|
216
|
+
persistors[connectionName] = new Persistor({
|
|
217
|
+
redis,
|
|
218
|
+
onError,
|
|
219
|
+
onSuccess,
|
|
220
|
+
clientId
|
|
221
|
+
});
|
|
220
222
|
}
|
|
221
|
-
return
|
|
222
|
-
onSuccess,
|
|
223
|
-
onError
|
|
224
|
-
});
|
|
223
|
+
return persistors[connectionName];
|
|
225
224
|
};
|
|
226
|
-
|
|
227
|
-
// src/promiseCache.ts
|
|
228
|
-
var promises = {};
|
|
229
225
|
var PromiseCache = class {
|
|
230
226
|
persistor;
|
|
227
|
+
clientId = (0, import_node_crypto.randomUUID)();
|
|
231
228
|
caseSensitive;
|
|
232
229
|
ttl;
|
|
233
230
|
// Time to live in milliseconds.
|
|
@@ -243,7 +240,12 @@ var PromiseCache = class {
|
|
|
243
240
|
onSuccess,
|
|
244
241
|
onError
|
|
245
242
|
}) {
|
|
246
|
-
this.persistor =
|
|
243
|
+
this.persistor = getPersistor({
|
|
244
|
+
redis,
|
|
245
|
+
onError,
|
|
246
|
+
onSuccess,
|
|
247
|
+
clientId: this.clientId
|
|
248
|
+
});
|
|
247
249
|
this.caseSensitive = caseSensitive;
|
|
248
250
|
if (ttlInSeconds) {
|
|
249
251
|
this.ttl = ttlInSeconds * 1e3;
|
|
@@ -313,7 +315,5 @@ var PromiseCache = class {
|
|
|
313
315
|
LocalStorage,
|
|
314
316
|
Persistor,
|
|
315
317
|
PromiseCache,
|
|
316
|
-
createLocalMemoryClient
|
|
317
|
-
createPersistor,
|
|
318
|
-
promises
|
|
318
|
+
createLocalMemoryClient
|
|
319
319
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
// src/
|
|
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
|
|
52
|
+
clientId;
|
|
51
53
|
onError;
|
|
52
54
|
onSuccess;
|
|
53
55
|
isConnected = false;
|
|
54
56
|
redis;
|
|
55
|
-
constructor(
|
|
56
|
-
|
|
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 {
|
|
@@ -144,8 +151,8 @@ var Persistor = class {
|
|
|
144
151
|
return {};
|
|
145
152
|
}
|
|
146
153
|
async set(key, { value, timestamp, ttl }) {
|
|
147
|
-
if (!this.client) {
|
|
148
|
-
throw new Error("Client not
|
|
154
|
+
if (!this.isConnected || !this.client) {
|
|
155
|
+
throw new Error("Client not connected");
|
|
149
156
|
}
|
|
150
157
|
try {
|
|
151
158
|
const serializedData = JSON.stringify({ value, ttl, timestamp });
|
|
@@ -156,8 +163,8 @@ var Persistor = class {
|
|
|
156
163
|
}
|
|
157
164
|
}
|
|
158
165
|
async delete(key) {
|
|
159
|
-
if (!this.client) {
|
|
160
|
-
throw new Error("Client not
|
|
166
|
+
if (!this.isConnected || !this.client) {
|
|
167
|
+
throw new Error("Client not connected");
|
|
161
168
|
}
|
|
162
169
|
try {
|
|
163
170
|
await this.client.del(key);
|
|
@@ -166,37 +173,29 @@ var Persistor = class {
|
|
|
166
173
|
}
|
|
167
174
|
}
|
|
168
175
|
};
|
|
176
|
+
|
|
177
|
+
// src/promiseCache.ts
|
|
169
178
|
var persistors = {};
|
|
170
|
-
var
|
|
179
|
+
var getPersistor = ({
|
|
171
180
|
redis,
|
|
172
181
|
onError,
|
|
173
|
-
onSuccess
|
|
182
|
+
onSuccess,
|
|
183
|
+
clientId
|
|
174
184
|
}) => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
redis,
|
|
184
|
-
onError,
|
|
185
|
-
onSuccess
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
return persistors[key];
|
|
185
|
+
const connectionName = (redis == null ? void 0 : redis.name) || "default";
|
|
186
|
+
if (!persistors[connectionName]) {
|
|
187
|
+
persistors[connectionName] = new Persistor({
|
|
188
|
+
redis,
|
|
189
|
+
onError,
|
|
190
|
+
onSuccess,
|
|
191
|
+
clientId
|
|
192
|
+
});
|
|
189
193
|
}
|
|
190
|
-
return
|
|
191
|
-
onSuccess,
|
|
192
|
-
onError
|
|
193
|
-
});
|
|
194
|
+
return persistors[connectionName];
|
|
194
195
|
};
|
|
195
|
-
|
|
196
|
-
// src/promiseCache.ts
|
|
197
|
-
var promises = {};
|
|
198
196
|
var PromiseCache = class {
|
|
199
197
|
persistor;
|
|
198
|
+
clientId = randomUUID();
|
|
200
199
|
caseSensitive;
|
|
201
200
|
ttl;
|
|
202
201
|
// Time to live in milliseconds.
|
|
@@ -212,7 +211,12 @@ var PromiseCache = class {
|
|
|
212
211
|
onSuccess,
|
|
213
212
|
onError
|
|
214
213
|
}) {
|
|
215
|
-
this.persistor =
|
|
214
|
+
this.persistor = getPersistor({
|
|
215
|
+
redis,
|
|
216
|
+
onError,
|
|
217
|
+
onSuccess,
|
|
218
|
+
clientId: this.clientId
|
|
219
|
+
});
|
|
216
220
|
this.caseSensitive = caseSensitive;
|
|
217
221
|
if (ttlInSeconds) {
|
|
218
222
|
this.ttl = ttlInSeconds * 1e3;
|
|
@@ -281,7 +285,5 @@ export {
|
|
|
281
285
|
LocalStorage,
|
|
282
286
|
Persistor,
|
|
283
287
|
PromiseCache,
|
|
284
|
-
createLocalMemoryClient
|
|
285
|
-
createPersistor,
|
|
286
|
-
promises
|
|
288
|
+
createLocalMemoryClient
|
|
287
289
|
};
|