lakutata 2.0.66 → 2.0.68

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.
Files changed (91) hide show
  1. package/com/cacher.cjs +1 -1
  2. package/com/cacher.mjs +1 -1
  3. package/com/entrypoint.cjs +3 -1
  4. package/com/entrypoint.mjs +3 -1
  5. package/decorator/ctrl.cjs +3 -1
  6. package/decorator/ctrl.mjs +3 -1
  7. package/helper.cjs +24 -18
  8. package/helper.d.ts +128 -2
  9. package/helper.mjs +6 -2
  10. package/lakutata.cjs +3 -1
  11. package/lakutata.d.ts +1 -1
  12. package/lakutata.mjs +3 -1
  13. package/package.json +1 -1
  14. package/src/components/cacher/Cacher.cjs +1 -1
  15. package/src/components/cacher/Cacher.mjs +1 -1
  16. package/src/components/cacher/adapters/CreateFileCacheAdapter.cjs +1 -1
  17. package/src/components/cacher/adapters/CreateFileCacheAdapter.mjs +1 -1
  18. package/src/components/cacher/adapters/CreateMemcacheCacheAdapter.cjs +2 -2
  19. package/src/components/cacher/adapters/CreateMemcacheCacheAdapter.mjs +2 -2
  20. package/src/components/cacher/adapters/CreateMongoCacheAdapter.cjs +2 -2
  21. package/src/components/cacher/adapters/CreateMongoCacheAdapter.mjs +2 -2
  22. package/src/components/cacher/adapters/CreateMysqlCacheAdapter.cjs +2 -2
  23. package/src/components/cacher/adapters/CreateMysqlCacheAdapter.mjs +2 -2
  24. package/src/components/cacher/adapters/CreatePostgresCacheAdapter.cjs +2 -2
  25. package/src/components/cacher/adapters/CreatePostgresCacheAdapter.mjs +2 -2
  26. package/src/components/cacher/adapters/CreateRedisCacheAdapter.cjs +2 -2
  27. package/src/components/cacher/adapters/CreateRedisCacheAdapter.mjs +2 -2
  28. package/src/components/cacher/adapters/CreateSqliteCacheAdapter.cjs +2 -2
  29. package/src/components/cacher/adapters/CreateSqliteCacheAdapter.mjs +2 -2
  30. package/src/components/entrypoint/Entrypoint.cjs +6 -1
  31. package/src/components/entrypoint/Entrypoint.mjs +6 -1
  32. package/src/decorators/ctrl/CLIAction.cjs +7 -5
  33. package/src/decorators/ctrl/CLIAction.mjs +3 -1
  34. package/src/decorators/ctrl/HTTPAction.cjs +8 -6
  35. package/src/decorators/ctrl/HTTPAction.mjs +3 -1
  36. package/src/decorators/ctrl/ServiceAction.cjs +3 -1
  37. package/src/decorators/ctrl/ServiceAction.mjs +7 -5
  38. package/src/decorators/ctrl/http/DELETE.cjs +3 -1
  39. package/src/decorators/ctrl/http/DELETE.mjs +3 -1
  40. package/src/decorators/ctrl/http/GET.cjs +3 -1
  41. package/src/decorators/ctrl/http/GET.mjs +3 -1
  42. package/src/decorators/ctrl/http/HEAD.cjs +3 -1
  43. package/src/decorators/ctrl/http/HEAD.mjs +3 -1
  44. package/src/decorators/ctrl/http/OPTIONS.cjs +3 -1
  45. package/src/decorators/ctrl/http/OPTIONS.mjs +3 -1
  46. package/src/decorators/ctrl/http/PATCH.cjs +3 -1
  47. package/src/decorators/ctrl/http/PATCH.mjs +3 -1
  48. package/src/decorators/ctrl/http/POST.cjs +3 -1
  49. package/src/decorators/ctrl/http/POST.mjs +3 -1
  50. package/src/decorators/ctrl/http/PUT.cjs +3 -1
  51. package/src/decorators/ctrl/http/PUT.mjs +3 -1
  52. package/src/lib/base/internal/ControllerEntrypoint.cjs +70 -610
  53. package/src/lib/base/internal/ControllerEntrypoint.mjs +87 -627
  54. package/src/lib/base/internal/StringifyPattern.cjs +1 -1
  55. package/src/lib/base/internal/StringifyPattern.mjs +1 -1
  56. package/src/lib/core/Application.cjs +3 -1
  57. package/src/lib/core/Application.mjs +3 -1
  58. package/src/lib/helpers/MD5.cjs +1 -1
  59. package/src/lib/helpers/MD5.mjs +1 -1
  60. package/src/lib/helpers/SHA1.cjs +1 -1
  61. package/src/lib/helpers/SHA1.mjs +1 -1
  62. package/src/lib/helpers/SHA256.cjs +1 -1
  63. package/src/lib/helpers/SHA256.mjs +1 -1
  64. package/src/lib/helpers/SortArray.cjs +1 -1
  65. package/src/lib/helpers/SortArray.mjs +1 -1
  66. package/src/lib/helpers/UUID.cjs +45 -0
  67. package/src/lib/helpers/UUID.mjs +39 -0
  68. package/vendor/Package.19.cjs +1 -1
  69. package/vendor/Package.19.mjs +176 -187
  70. package/vendor/Package.610.cjs +297 -113
  71. package/vendor/Package.610.mjs +285 -97
  72. package/vendor/Package.611.cjs +85 -103
  73. package/vendor/Package.611.mjs +80 -98
  74. package/vendor/Package.612.cjs +100 -121
  75. package/vendor/Package.612.mjs +97 -120
  76. package/vendor/Package.613.cjs +196 -0
  77. package/vendor/Package.613.mjs +180 -0
  78. package/vendor/Package.64.cjs +511 -134
  79. package/vendor/Package.64.mjs +491 -134
  80. package/vendor/Package.65.cjs +140 -4118
  81. package/vendor/Package.65.mjs +140 -4126
  82. package/vendor/Package.66.cjs +4142 -692
  83. package/vendor/Package.66.mjs +4151 -689
  84. package/vendor/Package.67.cjs +571 -535
  85. package/vendor/Package.67.mjs +586 -528
  86. package/vendor/Package.68.cjs +633 -96
  87. package/vendor/Package.68.mjs +619 -94
  88. package/vendor/Package.69.cjs +96 -294
  89. package/vendor/Package.69.mjs +95 -295
  90. package/vendor/TypeDef.internal.13.d.ts +1 -0
  91. package/vendor/TypeDef.internal.5.d.ts +1 -1
@@ -1,652 +1,710 @@
1
- import { createClient as t, createCluster as e } from "@redis/client";
1
+ import { Buffer as e } from "buffer";
2
2
 
3
- export { createClient, createCluster } from "@redis/client";
4
-
5
- import { K as s } from "./Package.66.mjs";
6
-
7
- import r from "cluster-key-slot";
3
+ var t = s => {
4
+ if (s === void 0 || s === null) {
5
+ return "null";
6
+ }
7
+ if (typeof s === "string") {
8
+ return JSON.stringify(s.startsWith(":") ? ":" + s : s);
9
+ }
10
+ if (e.isBuffer(s)) {
11
+ return JSON.stringify(":base64:" + s.toString("base64"));
12
+ }
13
+ if (s?.toJSON) {
14
+ s = s.toJSON();
15
+ }
16
+ if (typeof s === "object") {
17
+ let e = "";
18
+ const i = Array.isArray(s);
19
+ e = i ? "[" : "{";
20
+ let r = true;
21
+ for (const o in s) {
22
+ const a = typeof s[o] === "function" || !i && s[o] === void 0;
23
+ if (!Object.hasOwn(s, o) || a) {
24
+ continue;
25
+ }
26
+ if (!r) {
27
+ e += ",";
28
+ }
29
+ r = false;
30
+ if (i) {
31
+ e += t(s[o]);
32
+ } else if (s[o] !== void 0) {
33
+ e += t(o) + ":" + t(s[o]);
34
+ }
35
+ }
36
+ e += i ? "]" : "}";
37
+ return e;
38
+ }
39
+ return JSON.stringify(s);
40
+ };
8
41
 
9
- import "buffer";
42
+ var s = t => JSON.parse(t, ((t, s) => {
43
+ if (typeof s === "string") {
44
+ if (s.startsWith(":base64:")) {
45
+ return e.from(s.slice(8), "base64");
46
+ }
47
+ return s.startsWith(":") ? s.slice(1) : s;
48
+ }
49
+ return s;
50
+ }));
10
51
 
11
52
  var i = class {
12
53
  _eventListeners;
13
54
  _maxListeners;
14
- _logger;
15
- constructor(t) {
16
- this._eventListeners = new Map, this._maxListeners = 100, this._logger = t?.logger;
17
- }
18
- once(t, e) {
19
- let s = (...r) => {
20
- this.off(t, s), e(...r);
21
- };
22
- return this.on(t, s), this;
23
- }
24
- listenerCount(t) {
25
- if (!t) return this.getAllListeners().length;
26
- let e = this._eventListeners.get(t);
27
- return e ? e.length : 0;
28
- }
29
- eventNames() {
30
- return [ ...this._eventListeners.keys() ];
31
- }
32
- rawListeners(t) {
33
- return t ? this._eventListeners.get(t) ?? [] : this.getAllListeners();
34
- }
35
- prependListener(t, e) {
36
- let s = this._eventListeners.get(t) ?? [];
37
- return s.unshift(e), this._eventListeners.set(t, s), this;
38
- }
39
- prependOnceListener(t, e) {
40
- let s = (...r) => {
41
- this.off(t, s), e(...r);
42
- };
43
- return this.prependListener(t, s), this;
55
+ constructor() {
56
+ this._eventListeners = new Map;
57
+ this._maxListeners = 100;
44
58
  }
45
59
  maxListeners() {
46
60
  return this._maxListeners;
47
61
  }
48
- addListener(t, e) {
49
- return this.on(t, e), this;
62
+ addListener(e, t) {
63
+ this.on(e, t);
50
64
  }
51
- on(t, e) {
52
- this._eventListeners.has(t) || this._eventListeners.set(t, []);
53
- let s = this._eventListeners.get(t);
54
- return s && (s.length >= this._maxListeners && console.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${s.length + 1} ${t} listeners added. Use setMaxListeners() to increase limit.`),
55
- s.push(e)), this;
65
+ on(e, t) {
66
+ if (!this._eventListeners.has(e)) {
67
+ this._eventListeners.set(e, []);
68
+ }
69
+ const s = this._eventListeners.get(e);
70
+ if (s) {
71
+ if (s.length >= this._maxListeners) {
72
+ console.warn(`MaxListenersExceededWarning: Possible event memory leak detected. ${s.length + 1} ${e} listeners added. Use setMaxListeners() to increase limit.`);
73
+ }
74
+ s.push(t);
75
+ }
76
+ return this;
56
77
  }
57
- removeListener(t, e) {
58
- return this.off(t, e), this;
78
+ removeListener(e, t) {
79
+ this.off(e, t);
59
80
  }
60
- off(t, e) {
61
- let s = this._eventListeners.get(t) ?? [], r = s.indexOf(e);
62
- return r !== -1 && s.splice(r, 1), s.length === 0 && this._eventListeners.delete(t),
63
- this;
81
+ off(e, t) {
82
+ const s = this._eventListeners.get(e) ?? [];
83
+ const i = s.indexOf(t);
84
+ if (i !== -1) {
85
+ s.splice(i, 1);
86
+ }
87
+ if (s.length === 0) {
88
+ this._eventListeners.delete(e);
89
+ }
64
90
  }
65
- emit(t, ...e) {
66
- let s = !1, r = this._eventListeners.get(t);
67
- if (r && r.length > 0) for (let t of r) t(...e), s = !0;
68
- return s;
91
+ once(e, t) {
92
+ const s = (...i) => {
93
+ t(...i);
94
+ this.off(e, s);
95
+ };
96
+ this.on(e, s);
69
97
  }
70
- listeners(t) {
71
- return this._eventListeners.get(t) ?? [];
98
+ emit(e, ...t) {
99
+ const s = this._eventListeners.get(e);
100
+ if (s && s.length > 0) {
101
+ for (const e of s) {
102
+ e(...t);
103
+ }
104
+ }
72
105
  }
73
- removeAllListeners(t) {
74
- return t ? this._eventListeners.delete(t) : this._eventListeners.clear(), this;
106
+ listeners(e) {
107
+ return this._eventListeners.get(e) ?? [];
75
108
  }
76
- setMaxListeners(t) {
77
- this._maxListeners = t;
78
- for (let e of this._eventListeners.values()) e.length > t && e.splice(t);
109
+ removeAllListeners(e) {
110
+ if (e) {
111
+ this._eventListeners.delete(e);
112
+ } else {
113
+ this._eventListeners.clear();
114
+ }
79
115
  }
80
- getAllListeners() {
81
- let t = new Array;
82
- for (let e of this._eventListeners.values()) t = [ ...t, ...e ];
83
- return t;
116
+ setMaxListeners(e) {
117
+ this._maxListeners = e;
84
118
  }
85
119
  };
86
120
 
87
- var n = class extends i {
88
- _hooks;
89
- _throwHookErrors=!1;
90
- constructor(t) {
91
- super({
92
- logger: t?.logger
93
- }), this._hooks = new Map, t?.throwHookErrors !== void 0 && (this._throwHookErrors = t.throwHookErrors);
94
- }
95
- get hooks() {
96
- return this._hooks;
97
- }
98
- get throwHookErrors() {
99
- return this._throwHookErrors;
100
- }
101
- set throwHookErrors(t) {
102
- this._throwHookErrors = t;
103
- }
104
- get logger() {
105
- return this._logger;
106
- }
107
- set logger(t) {
108
- this._logger = t;
109
- }
110
- onHook(t, e) {
111
- let s = this._hooks.get(t);
112
- s ? s.push(e) : this._hooks.set(t, [ e ]);
113
- }
114
- addHook(t, e) {
115
- this.onHook(t, e);
121
+ var r = i;
122
+
123
+ var o = class extends r {
124
+ _hookHandlers;
125
+ constructor() {
126
+ super();
127
+ this._hookHandlers = new Map;
116
128
  }
117
- onHooks(t) {
118
- for (let e of t) this.onHook(e.event, e.handler);
129
+ addHandler(e, t) {
130
+ const s = this._hookHandlers.get(e);
131
+ if (s) {
132
+ s.push(t);
133
+ } else {
134
+ this._hookHandlers.set(e, [ t ]);
135
+ }
119
136
  }
120
- prependHook(t, e) {
121
- let s = this._hooks.get(t);
122
- s ? s.unshift(e) : this._hooks.set(t, [ e ]);
137
+ removeHandler(e, t) {
138
+ const s = this._hookHandlers.get(e);
139
+ if (s) {
140
+ const e = s.indexOf(t);
141
+ if (e !== -1) {
142
+ s.splice(e, 1);
143
+ }
144
+ }
123
145
  }
124
- prependOnceHook(t, e) {
125
- let s = async (...r) => (this.removeHook(t, s), e(...r));
126
- this.prependHook(t, s);
146
+ trigger(e, t) {
147
+ const s = this._hookHandlers.get(e);
148
+ if (s) {
149
+ for (const i of s) {
150
+ try {
151
+ i(t);
152
+ } catch (t) {
153
+ this.emit("error", new Error(`Error in hook handler for event "${e}": ${t.message}`));
154
+ }
155
+ }
156
+ }
127
157
  }
128
- onceHook(t, e) {
129
- let s = async (...r) => (this.removeHook(t, s), e(...r));
130
- this.onHook(t, s);
158
+ get handlers() {
159
+ return new Map(this._hookHandlers);
131
160
  }
132
- removeHook(t, e) {
133
- let s = this._hooks.get(t);
134
- if (s) {
135
- let t = s.indexOf(e);
136
- t !== -1 && s.splice(t, 1);
161
+ };
162
+
163
+ var a = o;
164
+
165
+ var n = class extends r {
166
+ enabled=true;
167
+ hits=0;
168
+ misses=0;
169
+ sets=0;
170
+ deletes=0;
171
+ errors=0;
172
+ constructor(e) {
173
+ super();
174
+ if (e !== void 0) {
175
+ this.enabled = e;
137
176
  }
177
+ this.reset();
138
178
  }
139
- removeHooks(t) {
140
- for (let e of t) this.removeHook(e.event, e.handler);
179
+ hit() {
180
+ if (this.enabled) {
181
+ this.hits++;
182
+ }
141
183
  }
142
- async hook(t, ...e) {
143
- let s = this._hooks.get(t);
144
- if (s) for (let r of s) try {
145
- await r(...e);
146
- } catch (e) {
147
- let s = `${t}: ${e.message}`;
148
- if (this.emit("error", new Error(s)), this._logger && this._logger.error(s), this._throwHookErrors) throw new Error(s);
184
+ miss() {
185
+ if (this.enabled) {
186
+ this.misses++;
149
187
  }
150
188
  }
151
- async callHook(t, ...e) {
152
- await this.hook(t, ...e);
189
+ set() {
190
+ if (this.enabled) {
191
+ this.sets++;
192
+ }
153
193
  }
154
- getHooks(t) {
155
- return this._hooks.get(t);
194
+ delete() {
195
+ if (this.enabled) {
196
+ this.deletes++;
197
+ }
156
198
  }
157
- clearHooks() {
158
- this._hooks.clear();
199
+ reset() {
200
+ this.hits = 0;
201
+ this.misses = 0;
202
+ this.sets = 0;
203
+ this.deletes = 0;
204
+ this.errors = 0;
159
205
  }
160
206
  };
161
207
 
162
- var o = (t => {
163
- t["RedisClientNotConnectedThrown"] = "Redis client is not connected or has failed to connect. This is thrown because throwOnConnectError is set to true.";
164
- return t;
165
- })(o || {});
208
+ var h = n;
166
209
 
167
- var a = t => {
168
- const e = Math.min(2 ** t * 100, 2e3);
169
- const s = (Math.random() - .5) * 100;
170
- return e + s;
171
- };
210
+ var l = (e => {
211
+ e["PRE_SET"] = "preSet";
212
+ e["POST_SET"] = "postSet";
213
+ e["PRE_GET"] = "preGet";
214
+ e["POST_GET"] = "postGet";
215
+ e["PRE_GET_MANY"] = "preGetMany";
216
+ e["POST_GET_MANY"] = "postGetMany";
217
+ e["PRE_DELETE"] = "preDelete";
218
+ e["POST_DELETE"] = "postDelete";
219
+ return e;
220
+ })(l || {});
172
221
 
173
- var c = class extends n {
174
- _client=t();
222
+ var c = [ "sqlite", "postgres", "mysql", "mongo", "redis", "valkey", "etcd" ];
223
+
224
+ var f = class extends r {
225
+ opts;
226
+ iterator;
227
+ hooks=new a;
228
+ stats=new h(false);
229
+ _ttl;
175
230
  _namespace;
176
- _keyPrefixSeparator="::";
177
- _clearBatchSize=1e3;
178
- _useUnlink=true;
179
- _noNamespaceAffectsAll=false;
180
- _throwOnConnectError=true;
181
- _throwErrors=false;
182
- _connectionTimeout;
183
- constructor(s, r) {
231
+ _store=new Map;
232
+ _serialize=t;
233
+ _deserialize=s;
234
+ _compression;
235
+ _useKeyPrefix=true;
236
+ constructor(e, i) {
184
237
  super();
185
- const i = {
186
- reconnectStrategy: a
238
+ i ??= {};
239
+ e ??= {};
240
+ this.opts = {
241
+ namespace: "keyv",
242
+ serialize: t,
243
+ deserialize: s,
244
+ emitErrors: true,
245
+ store: new Map,
246
+ ...i
187
247
  };
188
- if (s) {
189
- if (typeof s === "string") {
190
- this._client = t({
191
- url: s,
192
- socket: i
193
- });
194
- } else if (s.connect !== void 0) {
195
- this._client = this.isClientCluster(s) ? s : s;
196
- } else if (s instanceof Object) {
197
- this._client = s.rootNodes === void 0 ? t(s) : e(s);
248
+ if (e && e.get) {
249
+ this.opts.store = e;
250
+ } else {
251
+ this.opts = {
252
+ ...this.opts,
253
+ ...e
254
+ };
255
+ }
256
+ this._store = this.opts.store ?? new Map;
257
+ this._compression = this.opts.compression;
258
+ this._serialize = this.opts.serialize;
259
+ this._deserialize = this.opts.deserialize;
260
+ if (this.opts.namespace) {
261
+ this._namespace = this.opts.namespace;
262
+ }
263
+ if (this._store) {
264
+ if (!this._isValidStorageAdapter(this._store)) {
265
+ throw new Error("Invalid storage adapter");
266
+ }
267
+ if (typeof this._store.on === "function") {
268
+ this._store.on("error", (e => this.emit("error", e)));
269
+ }
270
+ this._store.namespace = this._namespace;
271
+ if (typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map) {
272
+ this.iterator = this.generateIterator(this._store);
273
+ } else if ("iterator" in this._store && this._store.opts && this._checkIterableAdapter()) {
274
+ this.iterator = this.generateIterator(this._store.iterator.bind(this._store));
198
275
  }
199
276
  }
200
- this.setOptions(r);
201
- this.initClient();
202
- }
203
- get client() {
204
- return this._client;
277
+ if (this.opts.stats) {
278
+ this.stats.enabled = this.opts.stats;
279
+ }
280
+ if (this.opts.ttl) {
281
+ this._ttl = this.opts.ttl;
282
+ }
283
+ if (this.opts.useKeyPrefix !== void 0) {
284
+ this._useKeyPrefix = this.opts.useKeyPrefix;
285
+ }
205
286
  }
206
- set client(t) {
207
- this._client = t;
208
- this.initClient();
287
+ get store() {
288
+ return this._store;
209
289
  }
210
- get opts() {
211
- let t = "redis://localhost:6379";
212
- if (this._client.options) {
213
- const e = this._client.options?.url;
214
- if (e) {
215
- t = e;
290
+ set store(e) {
291
+ if (this._isValidStorageAdapter(e)) {
292
+ this._store = e;
293
+ this.opts.store = e;
294
+ if (typeof e.on === "function") {
295
+ e.on("error", (e => this.emit("error", e)));
296
+ }
297
+ if (this._namespace) {
298
+ this._store.namespace = this._namespace;
299
+ }
300
+ if (typeof e[Symbol.iterator] === "function" && e instanceof Map) {
301
+ this.iterator = this.generateIterator(e);
302
+ } else if ("iterator" in e && e.opts && this._checkIterableAdapter()) {
303
+ this.iterator = this.generateIterator(e.iterator.bind(e));
216
304
  }
305
+ } else {
306
+ throw new Error("Invalid storage adapter");
217
307
  }
218
- const e = {
219
- namespace: this._namespace,
220
- keyPrefixSeparator: this._keyPrefixSeparator,
221
- clearBatchSize: this._clearBatchSize,
222
- noNamespaceAffectsAll: this._noNamespaceAffectsAll,
223
- useUnlink: this._useUnlink,
224
- throwOnConnectError: this._throwOnConnectError,
225
- throwErrors: this._throwErrors,
226
- connectionTimeout: this._connectionTimeout,
227
- dialect: "redis",
228
- url: t
229
- };
230
- return e;
231
308
  }
232
- set opts(t) {
233
- this.setOptions(t);
309
+ get compression() {
310
+ return this._compression;
311
+ }
312
+ set compression(e) {
313
+ this._compression = e;
234
314
  }
235
315
  get namespace() {
236
316
  return this._namespace;
237
317
  }
238
- set namespace(t) {
239
- this._namespace = t;
240
- }
241
- get keyPrefixSeparator() {
242
- return this._keyPrefixSeparator;
243
- }
244
- set keyPrefixSeparator(t) {
245
- this._keyPrefixSeparator = t;
246
- }
247
- get clearBatchSize() {
248
- return this._clearBatchSize;
249
- }
250
- set clearBatchSize(t) {
251
- if (t > 0) {
252
- this._clearBatchSize = t;
253
- } else {
254
- this.emit("error", "clearBatchSize must be greater than 0");
318
+ set namespace(e) {
319
+ this._namespace = e;
320
+ this.opts.namespace = e;
321
+ this._store.namespace = e;
322
+ if (this.opts.store) {
323
+ this.opts.store.namespace = e;
255
324
  }
256
325
  }
257
- get useUnlink() {
258
- return this._useUnlink;
326
+ get ttl() {
327
+ return this._ttl;
328
+ }
329
+ set ttl(e) {
330
+ this.opts.ttl = e;
331
+ this._ttl = e;
259
332
  }
260
- set useUnlink(t) {
261
- this._useUnlink = t;
333
+ get serialize() {
334
+ return this._serialize;
262
335
  }
263
- get noNamespaceAffectsAll() {
264
- return this._noNamespaceAffectsAll;
336
+ set serialize(e) {
337
+ this.opts.serialize = e;
338
+ this._serialize = e;
265
339
  }
266
- set noNamespaceAffectsAll(t) {
267
- this._noNamespaceAffectsAll = t;
340
+ get deserialize() {
341
+ return this._deserialize;
268
342
  }
269
- get throwOnConnectError() {
270
- return this._throwOnConnectError;
343
+ set deserialize(e) {
344
+ this.opts.deserialize = e;
345
+ this._deserialize = e;
271
346
  }
272
- set throwOnConnectError(t) {
273
- this._throwOnConnectError = t;
347
+ get useKeyPrefix() {
348
+ return this._useKeyPrefix;
274
349
  }
275
- get throwErrors() {
276
- return this._throwErrors;
350
+ set useKeyPrefix(e) {
351
+ this._useKeyPrefix = e;
352
+ this.opts.useKeyPrefix = e;
277
353
  }
278
- set throwErrors(t) {
279
- this._throwErrors = t;
354
+ generateIterator(e) {
355
+ const t = async function*() {
356
+ for await (const [t, s] of typeof e === "function" ? e(this._store.namespace) : e) {
357
+ const e = await this.deserializeData(s);
358
+ if (this._useKeyPrefix && this._store.namespace && !t.includes(this._store.namespace)) {
359
+ continue;
360
+ }
361
+ if (typeof e.expires === "number" && Date.now() > e.expires) {
362
+ this.delete(t);
363
+ continue;
364
+ }
365
+ yield [ this._getKeyUnprefix(t), e.value ];
366
+ }
367
+ };
368
+ return t.bind(this);
280
369
  }
281
- get connectionTimeout() {
282
- return this._connectionTimeout;
370
+ _checkIterableAdapter() {
371
+ return c.includes(this._store.opts.dialect) || c.some((e => this._store.opts.url.includes(e)));
283
372
  }
284
- set connectionTimeout(t) {
285
- this._connectionTimeout = t;
373
+ _getKeyPrefix(e) {
374
+ if (!this._useKeyPrefix) {
375
+ return e;
376
+ }
377
+ if (!this._namespace) {
378
+ return e;
379
+ }
380
+ return `${this._namespace}:${e}`;
286
381
  }
287
- async getClient() {
288
- if (this._client.isOpen) {
289
- return this._client;
382
+ _getKeyPrefixArray(e) {
383
+ if (!this._useKeyPrefix) {
384
+ return e;
290
385
  }
291
- try {
292
- if (this._connectionTimeout === void 0) {
293
- await this._client.connect();
294
- } else {
295
- await Promise.race([ this._client.connect(), this.createTimeoutPromise(this._connectionTimeout) ]);
296
- }
297
- } catch (t) {
298
- this.emit("error", t);
299
- if (this._throwOnConnectError) {
300
- throw new Error("Redis client is not connected or has failed to connect. This is thrown because throwOnConnectError is set to true.");
301
- }
302
- await this.disconnect(true);
386
+ if (!this._namespace) {
387
+ return e;
303
388
  }
304
- this.initClient();
305
- return this._client;
389
+ return e.map((e => `${this._namespace}:${e}`));
306
390
  }
307
- async set(t, e, s) {
308
- const r = await this.getClient();
309
- try {
310
- t = this.createKeyPrefix(t, this._namespace);
311
- if (s) {
312
- await r.set(t, e, {
313
- PX: s
391
+ _getKeyUnprefix(e) {
392
+ if (!this._useKeyPrefix) {
393
+ return e;
394
+ }
395
+ return e.split(":").splice(1).join(":");
396
+ }
397
+ _isValidStorageAdapter(e) {
398
+ return e instanceof Map || typeof e.get === "function" && typeof e.set === "function" && typeof e.delete === "function" && typeof e.clear === "function";
399
+ }
400
+ async get(e, t) {
401
+ const {store: s} = this.opts;
402
+ const i = Array.isArray(e);
403
+ const r = i ? this._getKeyPrefixArray(e) : this._getKeyPrefix(e);
404
+ const o = e => typeof e.expires === "number" && Date.now() > e.expires;
405
+ if (i) {
406
+ if (t?.raw === true) {
407
+ return this.getMany(e, {
408
+ raw: true
314
409
  });
315
- } else {
316
- await r.set(t, e);
317
- }
318
- } catch (t) {
319
- this.emit("error", t);
320
- if (this._throwErrors) {
321
- throw t;
322
410
  }
411
+ return this.getMany(e, {
412
+ raw: false
413
+ });
414
+ }
415
+ this.hooks.trigger("preGet", {
416
+ key: r
417
+ });
418
+ const a = await s.get(r);
419
+ const n = typeof a === "string" || this.opts.compression ? await this.deserializeData(a) : a;
420
+ if (n === void 0 || n === null) {
421
+ this.stats.miss();
422
+ return void 0;
323
423
  }
324
- }
325
- async setMany(t) {
326
- const e = await this.getClient();
327
- try {
328
- const s = e.multi();
329
- for (const {key: e, value: r, ttl: i} of t) {
330
- const t = this.createKeyPrefix(e, this._namespace);
331
- if (i) {
332
- s.set(t, r, {
333
- PX: i
334
- });
335
- } else {
336
- s.set(t, r);
424
+ if (o(n)) {
425
+ await this.delete(e);
426
+ this.stats.miss();
427
+ return void 0;
428
+ }
429
+ this.hooks.trigger("postGet", {
430
+ key: r,
431
+ value: n
432
+ });
433
+ this.stats.hit();
434
+ return t?.raw ? n : n.value;
435
+ }
436
+ async getMany(e, t) {
437
+ const {store: s} = this.opts;
438
+ const i = this._getKeyPrefixArray(e);
439
+ const r = e => typeof e.expires === "number" && Date.now() > e.expires;
440
+ this.hooks.trigger("preGetMany", {
441
+ keys: i
442
+ });
443
+ if (s.getMany === void 0) {
444
+ const e = i.map((async e => {
445
+ const i = await s.get(e);
446
+ const o = typeof i === "string" || this.opts.compression ? await this.deserializeData(i) : i;
447
+ if (o === void 0 || o === null) {
448
+ return void 0;
449
+ }
450
+ if (r(o)) {
451
+ await this.delete(e);
452
+ return void 0;
337
453
  }
454
+ return t?.raw ? o : o.value;
455
+ }));
456
+ const o = await Promise.allSettled(e);
457
+ const a = o.map((e => e.value));
458
+ this.hooks.trigger("postGetMany", a);
459
+ if (a.length > 0) {
460
+ this.stats.hit();
338
461
  }
339
- await s.exec();
340
- } catch (t) {
341
- this.emit("error", t);
342
- if (this._throwErrors) {
343
- throw t;
462
+ return a;
463
+ }
464
+ const o = await s.getMany(i);
465
+ const a = [];
466
+ for (const s in o) {
467
+ let i = o[s];
468
+ if (typeof i === "string") {
469
+ i = await this.deserializeData(i);
344
470
  }
345
- }
346
- }
347
- async has(t) {
348
- const e = await this.getClient();
349
- try {
350
- t = this.createKeyPrefix(t, this._namespace);
351
- const s = await e.exists(t);
352
- return s === 1;
353
- } catch (t) {
354
- this.emit("error", t);
355
- if (this._throwErrors) {
356
- throw t;
471
+ if (i === void 0 || i === null) {
472
+ a.push(void 0);
473
+ continue;
357
474
  }
358
- return false;
475
+ if (r(i)) {
476
+ await this.delete(e[s]);
477
+ a.push(void 0);
478
+ continue;
479
+ }
480
+ const n = t?.raw ? i : i.value;
481
+ a.push(n);
482
+ }
483
+ this.hooks.trigger("postGetMany", a);
484
+ if (a.length > 0) {
485
+ this.stats.hit();
359
486
  }
487
+ return a;
360
488
  }
361
- async hasMany(t) {
362
- const e = await this.getClient();
489
+ async set(e, t, s) {
490
+ const i = {
491
+ key: e,
492
+ value: t,
493
+ ttl: s
494
+ };
495
+ this.hooks.trigger("preSet", i);
496
+ const r = this._getKeyPrefix(i.key);
497
+ i.ttl ??= this._ttl;
498
+ if (i.ttl === 0) {
499
+ i.ttl = void 0;
500
+ }
501
+ const {store: o} = this.opts;
502
+ const a = typeof i.ttl === "number" ? Date.now() + i.ttl : void 0;
503
+ if (typeof i.value === "symbol") {
504
+ this.emit("error", "symbol cannot be serialized");
505
+ throw new Error("symbol cannot be serialized");
506
+ }
507
+ const n = {
508
+ value: i.value,
509
+ expires: a
510
+ };
511
+ const h = await this.serializeData(n);
512
+ let l = true;
363
513
  try {
364
- const s = e.multi();
365
- for (const e of t) {
366
- const t = this.createKeyPrefix(e, this._namespace);
367
- s.exists(t);
514
+ const e = await o.set(r, h, i.ttl);
515
+ if (typeof e === "boolean") {
516
+ l = e;
368
517
  }
369
- const r = await s.exec();
370
- return r.map((t => t === 1));
371
518
  } catch (e) {
519
+ l = false;
372
520
  this.emit("error", e);
373
- if (this._throwErrors) {
374
- throw e;
375
- }
376
- return Array.from({
377
- length: t.length
378
- }).fill(false);
379
521
  }
522
+ this.hooks.trigger("postSet", {
523
+ key: r,
524
+ value: h,
525
+ ttl: s
526
+ });
527
+ this.stats.set();
528
+ return l;
380
529
  }
381
- async get(t) {
382
- const e = await this.getClient();
530
+ async setMany(e) {
531
+ let t = [];
383
532
  try {
384
- t = this.createKeyPrefix(t, this._namespace);
385
- const s = await e.get(t);
386
- if (s === null) {
387
- return void 0;
533
+ if (this._store.setMany !== void 0) {
534
+ const s = await Promise.all(e.map((async ({key: e, value: t, ttl: s}) => {
535
+ s ??= this._ttl;
536
+ if (s === 0) {
537
+ s = void 0;
538
+ }
539
+ const i = typeof s === "number" ? Date.now() + s : void 0;
540
+ if (typeof t === "symbol") {
541
+ this.emit("error", "symbol cannot be serialized");
542
+ throw new Error("symbol cannot be serialized");
543
+ }
544
+ const r = {
545
+ value: t,
546
+ expires: i
547
+ };
548
+ const o = await this.serializeData(r);
549
+ return {
550
+ key: e,
551
+ value: o,
552
+ ttl: s
553
+ };
554
+ })));
555
+ t = await this._store.setMany(s);
388
556
  }
389
- return s;
390
- } catch (t) {
391
- this.emit("error", t);
392
- if (this._throwErrors) {
393
- throw t;
557
+ const s = [];
558
+ for (const t of e) {
559
+ s.push(this.set(t.key, t.value, t.ttl));
394
560
  }
395
- return void 0;
561
+ const i = await Promise.allSettled(s);
562
+ t = i.map((e => e.value));
563
+ } catch (s) {
564
+ this.emit("error", s);
565
+ t = e.map((() => false));
396
566
  }
567
+ return t;
397
568
  }
398
- async getMany(t) {
399
- if (t.length === 0) {
400
- return [];
569
+ async delete(e) {
570
+ const {store: t} = this.opts;
571
+ if (Array.isArray(e)) {
572
+ return this.deleteMany(e);
401
573
  }
402
- t = t.map((t => this.createKeyPrefix(t, this._namespace)));
574
+ const s = this._getKeyPrefix(e);
575
+ this.hooks.trigger("preDelete", {
576
+ key: s
577
+ });
578
+ let i = true;
403
579
  try {
404
- const e = await this.mget(t);
405
- return e;
580
+ const e = await t.delete(s);
581
+ if (typeof e === "boolean") {
582
+ i = e;
583
+ }
406
584
  } catch (e) {
585
+ i = false;
407
586
  this.emit("error", e);
408
- if (this._throwErrors) {
409
- throw e;
410
- }
411
- return Array.from({
412
- length: t.length
413
- }).fill(void 0);
414
587
  }
588
+ this.hooks.trigger("postDelete", {
589
+ key: s,
590
+ value: i
591
+ });
592
+ this.stats.delete();
593
+ return i;
415
594
  }
416
- async delete(t) {
417
- const e = await this.getClient();
595
+ async deleteMany(e) {
418
596
  try {
419
- t = this.createKeyPrefix(t, this._namespace);
420
- let s = 0;
421
- s = await (this._useUnlink ? e.unlink(t) : e.del(t));
422
- return s > 0;
423
- } catch (t) {
424
- this.emit("error", t);
425
- if (this._throwErrors) {
426
- throw t;
597
+ const {store: t} = this.opts;
598
+ const s = this._getKeyPrefixArray(e);
599
+ this.hooks.trigger("preDelete", {
600
+ key: s
601
+ });
602
+ if (t.deleteMany !== void 0) {
603
+ return await t.deleteMany(s);
427
604
  }
605
+ const i = s.map((async e => t.delete(e)));
606
+ const r = await Promise.allSettled(i);
607
+ const o = r.every((e => e.value === true));
608
+ this.hooks.trigger("postDelete", {
609
+ key: s,
610
+ value: o
611
+ });
612
+ return o;
613
+ } catch (e) {
614
+ this.emit("error", e);
428
615
  return false;
429
616
  }
430
617
  }
431
- async deleteMany(t) {
432
- let e = false;
433
- const s = await this.getClient();
618
+ async clear() {
619
+ this.emit("clear");
620
+ const {store: e} = this.opts;
434
621
  try {
435
- const r = s.multi();
436
- for (const e of t) {
437
- const t = this.createKeyPrefix(e, this._namespace);
438
- if (this._useUnlink) {
439
- r.unlink(t);
440
- } else {
441
- r.del(t);
442
- }
443
- }
444
- const i = await r.exec();
445
- for (const t of i) {
446
- if (typeof t === "number" && t > 0) {
447
- e = true;
448
- }
449
- }
450
- } catch (t) {
451
- this.emit("error", t);
452
- if (this._throwErrors) {
453
- throw t;
454
- }
455
- }
456
- return e;
457
- }
458
- async disconnect(t) {
459
- if (this._client.isOpen) {
460
- await (t ? this._client.disconnect() : this._client.quit());
461
- }
462
- }
463
- createKeyPrefix(t, e) {
464
- if (e) {
465
- return `${e}${this._keyPrefixSeparator}${t}`;
622
+ await e.clear();
623
+ } catch (e) {
624
+ this.emit("error", e);
466
625
  }
467
- return t;
468
626
  }
469
- getKeyWithoutPrefix(t, e) {
470
- if (e) {
471
- return t.replace(`${e}${this._keyPrefixSeparator}`, "");
627
+ async has(e) {
628
+ if (Array.isArray(e)) {
629
+ return this.hasMany(e);
472
630
  }
473
- return t;
474
- }
475
- isCluster() {
476
- return this.isClientCluster(this._client);
477
- }
478
- async getMasterNodes() {
479
- if (this.isCluster()) {
480
- const t = await this.getClient();
481
- return Promise.all(t.masters.map((async e => t.nodeClient(e))));
482
- }
483
- return [ await this.getClient() ];
484
- }
485
- async* iterator(t) {
486
- const e = await this.getMasterNodes();
487
- for (const s of e) {
488
- const e = t ? `${t}${this._keyPrefixSeparator}*` : "*";
489
- let r = "0";
490
- do {
491
- const i = await s.scan(Number.parseInt(r, 10), {
492
- MATCH: e,
493
- TYPE: "string"
494
- });
495
- r = i.cursor.toString();
496
- let {keys: n} = i;
497
- if (!t && !this._noNamespaceAffectsAll) {
498
- n = n.filter((t => !t.includes(this._keyPrefixSeparator)));
499
- }
500
- if (n.length > 0) {
501
- const e = await this.mget(n);
502
- for (const s of n.keys()) {
503
- const r = this.getKeyWithoutPrefix(n[s], t);
504
- const i = e[s];
505
- yield [ r, i ];
506
- }
507
- }
508
- } while (r !== "0");
631
+ const t = this._getKeyPrefix(e);
632
+ const {store: s} = this.opts;
633
+ if (s.has !== void 0 && !(s instanceof Map)) {
634
+ return s.has(t);
509
635
  }
510
- }
511
- async clear() {
636
+ let i;
512
637
  try {
513
- const t = await this.getMasterNodes();
514
- await Promise.all(t.map((async t => {
515
- if (!this._namespace && this._noNamespaceAffectsAll) {
516
- await t.flushDb();
517
- return;
638
+ i = await s.get(t);
639
+ } catch (e) {
640
+ this.emit("error", e);
641
+ }
642
+ if (i) {
643
+ const e = await this.deserializeData(i);
644
+ if (e) {
645
+ if (e.expires === void 0 || e.expires === null) {
646
+ return true;
518
647
  }
519
- let e = "0";
520
- const s = this._clearBatchSize;
521
- const r = this._namespace ? `${this._namespace}${this._keyPrefixSeparator}*` : "*";
522
- const i = [];
523
- do {
524
- const n = await t.scan(Number.parseInt(e, 10), {
525
- MATCH: r,
526
- COUNT: s,
527
- TYPE: "string"
528
- });
529
- e = n.cursor.toString();
530
- let {keys: o} = n;
531
- if (o.length === 0) {
532
- continue;
533
- }
534
- if (!this._namespace) {
535
- o = o.filter((t => !t.includes(this._keyPrefixSeparator)));
536
- }
537
- i.push(this.clearWithClusterSupport(o));
538
- } while (e !== "0");
539
- await Promise.all(i);
540
- })));
541
- } catch (t) {
542
- this.emit("error", t);
543
- }
544
- }
545
- async mget(t) {
546
- const e = this.getSlotMap(t);
547
- const s = new Map;
548
- await Promise.all(Array.from(e.entries(), (async ([t, e]) => {
549
- const r = await this.getSlotMaster(t);
550
- const i = await r.mGet(e);
551
- for (const [t, r] of i.entries()) {
552
- s.set(e[t], r ?? void 0);
648
+ return e.expires > Date.now();
553
649
  }
554
- })));
555
- return t.map((t => s.get(t)));
556
- }
557
- async clearWithClusterSupport(t) {
558
- if (t.length > 0) {
559
- const e = this.getSlotMap(t);
560
- await Promise.all(Array.from(e.entries(), (async ([t, e]) => {
561
- const s = await this.getSlotMaster(t);
562
- return this._useUnlink ? s.unlink(e) : s.del(e);
563
- })));
564
650
  }
651
+ return false;
565
652
  }
566
- async getSlotMaster(t) {
567
- const e = await this.getClient();
568
- if (this.isCluster()) {
569
- const s = e;
570
- const r = s.slots[t].master;
571
- return s.nodeClient(r);
653
+ async hasMany(e) {
654
+ const t = this._getKeyPrefixArray(e);
655
+ const {store: s} = this.opts;
656
+ if (s.hasMany !== void 0) {
657
+ return s.hasMany(t);
572
658
  }
573
- return e;
574
- }
575
- getSlotMap(t) {
576
- const e = new Map;
577
- if (this.isCluster()) {
578
- for (const s of t) {
579
- const t = r(s);
580
- const i = e.get(t) ?? [];
581
- i.push(s);
582
- e.set(t, i);
583
- }
584
- } else {
585
- e.set(0, t);
659
+ const i = [];
660
+ for (const e of t) {
661
+ i.push(await this.has(e));
586
662
  }
587
- return e;
663
+ return i;
588
664
  }
589
- isClientCluster(t) {
590
- if (t.options === void 0 && t.scan === void 0) {
591
- return true;
665
+ async disconnect() {
666
+ const {store: e} = this.opts;
667
+ this.emit("disconnect");
668
+ if (typeof e.disconnect === "function") {
669
+ return e.disconnect();
592
670
  }
593
- return false;
594
671
  }
595
- setOptions(t) {
596
- if (!t) {
672
+ emit(e, ...t) {
673
+ if (e === "error" && !this.opts.emitErrors) {
597
674
  return;
598
675
  }
599
- if (t.namespace) {
600
- this._namespace = t.namespace;
601
- }
602
- if (t.keyPrefixSeparator !== void 0) {
603
- this._keyPrefixSeparator = t.keyPrefixSeparator;
604
- }
605
- if (t.clearBatchSize !== void 0 && t.clearBatchSize > 0) {
606
- this._clearBatchSize = t.clearBatchSize;
607
- }
608
- if (t.useUnlink !== void 0) {
609
- this._useUnlink = t.useUnlink;
676
+ super.emit(e, ...t);
677
+ }
678
+ async serializeData(e) {
679
+ if (!this._serialize) {
680
+ return e;
610
681
  }
611
- if (t.noNamespaceAffectsAll !== void 0) {
612
- this._noNamespaceAffectsAll = t.noNamespaceAffectsAll;
682
+ if (this._compression?.compress) {
683
+ return this._serialize({
684
+ value: await this._compression.compress(e.value),
685
+ expires: e.expires
686
+ });
613
687
  }
614
- if (t.throwOnConnectError !== void 0) {
615
- this._throwOnConnectError = t.throwOnConnectError;
688
+ return this._serialize(e);
689
+ }
690
+ async deserializeData(e) {
691
+ if (!this._deserialize) {
692
+ return e;
616
693
  }
617
- if (t.throwErrors !== void 0) {
618
- this._throwErrors = t.throwErrors;
694
+ if (this._compression?.decompress && typeof e === "string") {
695
+ const t = await this._deserialize(e);
696
+ return {
697
+ value: await this._compression.decompress(t?.value),
698
+ expires: t?.expires
699
+ };
619
700
  }
620
- if (t.connectionTimeout !== void 0) {
621
- this._connectionTimeout = t.connectionTimeout;
701
+ if (typeof e === "string") {
702
+ return this._deserialize(e);
622
703
  }
623
- }
624
- initClient() {
625
- this._client.on("connect", (() => {
626
- this.emit("connect", this._client);
627
- }));
628
- this._client.on("disconnect", (() => {
629
- this.emit("disconnect", this._client);
630
- }));
631
- this._client.on("reconnecting", (t => {
632
- this.emit("reconnecting", t);
633
- }));
634
- }
635
- async createTimeoutPromise(t) {
636
- return new Promise(((e, s) => setTimeout((() => {
637
- s(new Error(`Redis timed out after ${t}ms`));
638
- }), t)));
704
+ return void 0;
639
705
  }
640
706
  };
641
707
 
642
- function h(t, e) {
643
- t ??= "redis://localhost:6379";
644
- const r = new c(t, e);
645
- const i = new s(r, {
646
- namespace: e?.namespace,
647
- useKeyPrefix: false
648
- });
649
- return i;
650
- }
708
+ var p = f;
651
709
 
652
- export { s as Keyv, o as RedisErrorMessages, h as createKeyv, c as default, a as defaultReconnectStrategy };
710
+ export { f as K, s as d, p as i };