@sebspark/promise-cache 2.0.1 → 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 +9 -11
- package/dist/index.d.ts +9 -11
- package/dist/index.js +47 -39
- package/dist/index.mjs +46 -36
- 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,30 +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;
|
|
26
|
+
private isConnected;
|
|
25
27
|
private readonly redis?;
|
|
26
|
-
constructor(
|
|
28
|
+
constructor({ redis, clientId, onSuccess, onError, }: PersistorConstructorType);
|
|
27
29
|
connect(): Promise<void>;
|
|
28
30
|
startConnection(): Promise<unknown>;
|
|
29
31
|
size(): Promise<number>;
|
|
30
32
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
31
|
-
getClientId(): UUID;
|
|
33
|
+
getClientId(): UUID | undefined;
|
|
34
|
+
getIsClientConnected(): boolean;
|
|
32
35
|
private createOptions;
|
|
33
36
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
34
37
|
delete(key: string): Promise<void>;
|
|
35
38
|
}
|
|
36
|
-
declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
37
|
-
redis?: RedisClientOptions;
|
|
38
|
-
onError?: () => void;
|
|
39
|
-
onSuccess?: () => void;
|
|
40
|
-
}) => Persistor;
|
|
41
39
|
|
|
42
40
|
type PromiseCacheOptions = {
|
|
43
41
|
ttlInSeconds?: number;
|
|
@@ -46,9 +44,9 @@ type PromiseCacheOptions = {
|
|
|
46
44
|
onError?: () => void;
|
|
47
45
|
onSuccess?: () => void;
|
|
48
46
|
};
|
|
49
|
-
declare const promises: {};
|
|
50
47
|
declare class PromiseCache<U> {
|
|
51
48
|
persistor: Persistor;
|
|
49
|
+
private clientId;
|
|
52
50
|
private readonly caseSensitive;
|
|
53
51
|
private readonly ttl?;
|
|
54
52
|
/**
|
|
@@ -98,4 +96,4 @@ declare class LocalStorage {
|
|
|
98
96
|
}
|
|
99
97
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
100
98
|
|
|
101
|
-
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,30 +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;
|
|
26
|
+
private isConnected;
|
|
25
27
|
private readonly redis?;
|
|
26
|
-
constructor(
|
|
28
|
+
constructor({ redis, clientId, onSuccess, onError, }: PersistorConstructorType);
|
|
27
29
|
connect(): Promise<void>;
|
|
28
30
|
startConnection(): Promise<unknown>;
|
|
29
31
|
size(): Promise<number>;
|
|
30
32
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
31
|
-
getClientId(): UUID;
|
|
33
|
+
getClientId(): UUID | undefined;
|
|
34
|
+
getIsClientConnected(): boolean;
|
|
32
35
|
private createOptions;
|
|
33
36
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
34
37
|
delete(key: string): Promise<void>;
|
|
35
38
|
}
|
|
36
|
-
declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
37
|
-
redis?: RedisClientOptions;
|
|
38
|
-
onError?: () => void;
|
|
39
|
-
onSuccess?: () => void;
|
|
40
|
-
}) => Persistor;
|
|
41
39
|
|
|
42
40
|
type PromiseCacheOptions = {
|
|
43
41
|
ttlInSeconds?: number;
|
|
@@ -46,9 +44,9 @@ type PromiseCacheOptions = {
|
|
|
46
44
|
onError?: () => void;
|
|
47
45
|
onSuccess?: () => void;
|
|
48
46
|
};
|
|
49
|
-
declare const promises: {};
|
|
50
47
|
declare class PromiseCache<U> {
|
|
51
48
|
persistor: Persistor;
|
|
49
|
+
private clientId;
|
|
52
50
|
private readonly caseSensitive;
|
|
53
51
|
private readonly ttl?;
|
|
54
52
|
/**
|
|
@@ -98,4 +96,4 @@ declare class LocalStorage {
|
|
|
98
96
|
}
|
|
99
97
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
100
98
|
|
|
101
|
-
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,20 +78,28 @@ 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
|
+
isConnected = false;
|
|
84
85
|
redis;
|
|
85
|
-
constructor(
|
|
86
|
-
|
|
86
|
+
constructor({
|
|
87
|
+
redis,
|
|
88
|
+
clientId,
|
|
89
|
+
onSuccess,
|
|
90
|
+
onError
|
|
91
|
+
}) {
|
|
87
92
|
this.onError = onError;
|
|
88
93
|
this.onSuccess = onSuccess;
|
|
94
|
+
this.clientId = clientId;
|
|
89
95
|
if (redis && !isTestRunning) {
|
|
90
96
|
this.redis = redis;
|
|
91
97
|
} else {
|
|
92
98
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
93
99
|
}
|
|
94
|
-
this.
|
|
100
|
+
if (!this.isConnected) {
|
|
101
|
+
this.connect();
|
|
102
|
+
}
|
|
95
103
|
}
|
|
96
104
|
async connect() {
|
|
97
105
|
const settings = {
|
|
@@ -113,6 +121,7 @@ var Persistor = class {
|
|
|
113
121
|
this.client = CACHE_CLIENT(this.redis);
|
|
114
122
|
this.client.on("error", (err) => {
|
|
115
123
|
var _a2, _b2;
|
|
124
|
+
this.isConnected = false;
|
|
116
125
|
if (this.onError) {
|
|
117
126
|
this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
118
127
|
}
|
|
@@ -120,6 +129,7 @@ var Persistor = class {
|
|
|
120
129
|
});
|
|
121
130
|
this.client.on("connect", () => {
|
|
122
131
|
var _a2, _b2, _c, _d;
|
|
132
|
+
this.isConnected = true;
|
|
123
133
|
if (this.onSuccess) {
|
|
124
134
|
this.onSuccess(
|
|
125
135
|
`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
|
|
@@ -160,6 +170,9 @@ var Persistor = class {
|
|
|
160
170
|
getClientId() {
|
|
161
171
|
return this.clientId;
|
|
162
172
|
}
|
|
173
|
+
getIsClientConnected() {
|
|
174
|
+
return this.isConnected;
|
|
175
|
+
}
|
|
163
176
|
createOptions(ttl) {
|
|
164
177
|
if (ttl !== null && ttl !== void 0) {
|
|
165
178
|
return { PX: Math.round(ttl) };
|
|
@@ -167,8 +180,8 @@ var Persistor = class {
|
|
|
167
180
|
return {};
|
|
168
181
|
}
|
|
169
182
|
async set(key, { value, timestamp, ttl }) {
|
|
170
|
-
if (!this.client) {
|
|
171
|
-
throw new Error("Client not
|
|
183
|
+
if (!this.isConnected || !this.client) {
|
|
184
|
+
throw new Error("Client not connected");
|
|
172
185
|
}
|
|
173
186
|
try {
|
|
174
187
|
const serializedData = JSON.stringify({ value, ttl, timestamp });
|
|
@@ -179,8 +192,8 @@ var Persistor = class {
|
|
|
179
192
|
}
|
|
180
193
|
}
|
|
181
194
|
async delete(key) {
|
|
182
|
-
if (!this.client) {
|
|
183
|
-
throw new Error("Client not
|
|
195
|
+
if (!this.isConnected || !this.client) {
|
|
196
|
+
throw new Error("Client not connected");
|
|
184
197
|
}
|
|
185
198
|
try {
|
|
186
199
|
await this.client.del(key);
|
|
@@ -189,37 +202,29 @@ var Persistor = class {
|
|
|
189
202
|
}
|
|
190
203
|
}
|
|
191
204
|
};
|
|
205
|
+
|
|
206
|
+
// src/promiseCache.ts
|
|
192
207
|
var persistors = {};
|
|
193
|
-
var
|
|
208
|
+
var getPersistor = ({
|
|
194
209
|
redis,
|
|
195
210
|
onError,
|
|
196
|
-
onSuccess
|
|
211
|
+
onSuccess,
|
|
212
|
+
clientId
|
|
197
213
|
}) => {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
redis,
|
|
207
|
-
onError,
|
|
208
|
-
onSuccess
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
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
|
+
});
|
|
212
222
|
}
|
|
213
|
-
return
|
|
214
|
-
onSuccess,
|
|
215
|
-
onError
|
|
216
|
-
});
|
|
223
|
+
return persistors[connectionName];
|
|
217
224
|
};
|
|
218
|
-
|
|
219
|
-
// src/promiseCache.ts
|
|
220
|
-
var promises = {};
|
|
221
225
|
var PromiseCache = class {
|
|
222
226
|
persistor;
|
|
227
|
+
clientId = (0, import_node_crypto.randomUUID)();
|
|
223
228
|
caseSensitive;
|
|
224
229
|
ttl;
|
|
225
230
|
// Time to live in milliseconds.
|
|
@@ -235,7 +240,12 @@ var PromiseCache = class {
|
|
|
235
240
|
onSuccess,
|
|
236
241
|
onError
|
|
237
242
|
}) {
|
|
238
|
-
this.persistor =
|
|
243
|
+
this.persistor = getPersistor({
|
|
244
|
+
redis,
|
|
245
|
+
onError,
|
|
246
|
+
onSuccess,
|
|
247
|
+
clientId: this.clientId
|
|
248
|
+
});
|
|
239
249
|
this.caseSensitive = caseSensitive;
|
|
240
250
|
if (ttlInSeconds) {
|
|
241
251
|
this.ttl = ttlInSeconds * 1e3;
|
|
@@ -305,7 +315,5 @@ var PromiseCache = class {
|
|
|
305
315
|
LocalStorage,
|
|
306
316
|
Persistor,
|
|
307
317
|
PromiseCache,
|
|
308
|
-
createLocalMemoryClient
|
|
309
|
-
createPersistor,
|
|
310
|
-
promises
|
|
318
|
+
createLocalMemoryClient
|
|
311
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,20 +49,28 @@ 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;
|
|
55
|
+
isConnected = false;
|
|
53
56
|
redis;
|
|
54
|
-
constructor(
|
|
55
|
-
|
|
57
|
+
constructor({
|
|
58
|
+
redis,
|
|
59
|
+
clientId,
|
|
60
|
+
onSuccess,
|
|
61
|
+
onError
|
|
62
|
+
}) {
|
|
56
63
|
this.onError = onError;
|
|
57
64
|
this.onSuccess = onSuccess;
|
|
65
|
+
this.clientId = clientId;
|
|
58
66
|
if (redis && !isTestRunning) {
|
|
59
67
|
this.redis = redis;
|
|
60
68
|
} else {
|
|
61
69
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
62
70
|
}
|
|
63
|
-
this.
|
|
71
|
+
if (!this.isConnected) {
|
|
72
|
+
this.connect();
|
|
73
|
+
}
|
|
64
74
|
}
|
|
65
75
|
async connect() {
|
|
66
76
|
const settings = {
|
|
@@ -82,6 +92,7 @@ var Persistor = class {
|
|
|
82
92
|
this.client = CACHE_CLIENT(this.redis);
|
|
83
93
|
this.client.on("error", (err) => {
|
|
84
94
|
var _a2, _b2;
|
|
95
|
+
this.isConnected = false;
|
|
85
96
|
if (this.onError) {
|
|
86
97
|
this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
87
98
|
}
|
|
@@ -89,6 +100,7 @@ var Persistor = class {
|
|
|
89
100
|
});
|
|
90
101
|
this.client.on("connect", () => {
|
|
91
102
|
var _a2, _b2, _c, _d;
|
|
103
|
+
this.isConnected = true;
|
|
92
104
|
if (this.onSuccess) {
|
|
93
105
|
this.onSuccess(
|
|
94
106
|
`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
|
|
@@ -129,6 +141,9 @@ var Persistor = class {
|
|
|
129
141
|
getClientId() {
|
|
130
142
|
return this.clientId;
|
|
131
143
|
}
|
|
144
|
+
getIsClientConnected() {
|
|
145
|
+
return this.isConnected;
|
|
146
|
+
}
|
|
132
147
|
createOptions(ttl) {
|
|
133
148
|
if (ttl !== null && ttl !== void 0) {
|
|
134
149
|
return { PX: Math.round(ttl) };
|
|
@@ -136,8 +151,8 @@ var Persistor = class {
|
|
|
136
151
|
return {};
|
|
137
152
|
}
|
|
138
153
|
async set(key, { value, timestamp, ttl }) {
|
|
139
|
-
if (!this.client) {
|
|
140
|
-
throw new Error("Client not
|
|
154
|
+
if (!this.isConnected || !this.client) {
|
|
155
|
+
throw new Error("Client not connected");
|
|
141
156
|
}
|
|
142
157
|
try {
|
|
143
158
|
const serializedData = JSON.stringify({ value, ttl, timestamp });
|
|
@@ -148,8 +163,8 @@ var Persistor = class {
|
|
|
148
163
|
}
|
|
149
164
|
}
|
|
150
165
|
async delete(key) {
|
|
151
|
-
if (!this.client) {
|
|
152
|
-
throw new Error("Client not
|
|
166
|
+
if (!this.isConnected || !this.client) {
|
|
167
|
+
throw new Error("Client not connected");
|
|
153
168
|
}
|
|
154
169
|
try {
|
|
155
170
|
await this.client.del(key);
|
|
@@ -158,37 +173,29 @@ var Persistor = class {
|
|
|
158
173
|
}
|
|
159
174
|
}
|
|
160
175
|
};
|
|
176
|
+
|
|
177
|
+
// src/promiseCache.ts
|
|
161
178
|
var persistors = {};
|
|
162
|
-
var
|
|
179
|
+
var getPersistor = ({
|
|
163
180
|
redis,
|
|
164
181
|
onError,
|
|
165
|
-
onSuccess
|
|
182
|
+
onSuccess,
|
|
183
|
+
clientId
|
|
166
184
|
}) => {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
redis,
|
|
176
|
-
onError,
|
|
177
|
-
onSuccess
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
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
|
+
});
|
|
181
193
|
}
|
|
182
|
-
return
|
|
183
|
-
onSuccess,
|
|
184
|
-
onError
|
|
185
|
-
});
|
|
194
|
+
return persistors[connectionName];
|
|
186
195
|
};
|
|
187
|
-
|
|
188
|
-
// src/promiseCache.ts
|
|
189
|
-
var promises = {};
|
|
190
196
|
var PromiseCache = class {
|
|
191
197
|
persistor;
|
|
198
|
+
clientId = randomUUID();
|
|
192
199
|
caseSensitive;
|
|
193
200
|
ttl;
|
|
194
201
|
// Time to live in milliseconds.
|
|
@@ -204,7 +211,12 @@ var PromiseCache = class {
|
|
|
204
211
|
onSuccess,
|
|
205
212
|
onError
|
|
206
213
|
}) {
|
|
207
|
-
this.persistor =
|
|
214
|
+
this.persistor = getPersistor({
|
|
215
|
+
redis,
|
|
216
|
+
onError,
|
|
217
|
+
onSuccess,
|
|
218
|
+
clientId: this.clientId
|
|
219
|
+
});
|
|
208
220
|
this.caseSensitive = caseSensitive;
|
|
209
221
|
if (ttlInSeconds) {
|
|
210
222
|
this.ttl = ttlInSeconds * 1e3;
|
|
@@ -273,7 +285,5 @@ export {
|
|
|
273
285
|
LocalStorage,
|
|
274
286
|
Persistor,
|
|
275
287
|
PromiseCache,
|
|
276
|
-
createLocalMemoryClient
|
|
277
|
-
createPersistor,
|
|
278
|
-
promises
|
|
288
|
+
createLocalMemoryClient
|
|
279
289
|
};
|