@guren/server 2.9.0 → 2.10.1

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 (92) hide show
  1. package/dist/DocsViewerServiceProvider-qFCaeiUv.js +77 -0
  2. package/dist/Hash-CzrwKeDp.js +133 -0
  3. package/dist/HttpException-BkS3euNG.js +161 -0
  4. package/dist/Job-BVT813DO.js +441 -0
  5. package/dist/McpServiceProvider-BoAN-UcD.js +367 -0
  6. package/dist/MessageSigner-Bd25ZJAV.js +93 -0
  7. package/dist/Random-DbIJ22KQ.js +191 -0
  8. package/dist/address-D0Iq-XIH.js +20 -0
  9. package/dist/auth/index.js +2 -122
  10. package/dist/auth-C7sVmYgc.js +1954 -0
  11. package/dist/authorization/index.js +2 -38
  12. package/dist/authorization-Dbcuc5ZK.js +610 -0
  13. package/dist/broadcasting/index.js +2 -26
  14. package/dist/broadcasting-Dw-FhyjN.js +1055 -0
  15. package/dist/cache/index.js +2 -17
  16. package/dist/cache/types.d.ts +4 -4
  17. package/dist/cache-BYnZf2FR.js +812 -0
  18. package/dist/client-Dd7vLcqp.js +27 -0
  19. package/dist/console/Output.d.ts +18 -18
  20. package/dist/discovery/AutoDiscovery.d.ts +8 -0
  21. package/dist/encryption/index.js +5 -96
  22. package/dist/encryption-fOWQaKqX.js +250 -0
  23. package/dist/endpoint-D-20PiHY.js +46 -0
  24. package/dist/endpoint-D6GIqOpe.js +31 -0
  25. package/dist/errors-quJOkkCn.js +81 -0
  26. package/dist/events/index.js +2 -28
  27. package/dist/events-aIH_bWRx.js +410 -0
  28. package/dist/health/index.js +2 -24
  29. package/dist/health-VWdWXRwh.js +345 -0
  30. package/dist/hot-disposables-WsTX2ySN.js +281 -0
  31. package/dist/hot-reload/testing.d.ts +1 -1
  32. package/dist/http/Application.d.ts +0 -1
  33. package/dist/i18n/index.js +2 -30
  34. package/dist/i18n-mcWi7LCd.js +617 -0
  35. package/dist/index.js +7496 -7281
  36. package/dist/lambda/index.js +114 -71
  37. package/dist/logging/index.js +2 -24
  38. package/dist/logging-CPaRMuPP.js +518 -0
  39. package/dist/loopback-guard-D0Kc7-xc.js +224 -0
  40. package/dist/mail/index.js +2 -27
  41. package/dist/mail-DfEfSQEr.js +699 -0
  42. package/dist/mcp/index.js +2 -10
  43. package/dist/notifications/index.js +2 -33
  44. package/dist/notifications-BGqmUNZ8.js +759 -0
  45. package/dist/queue/index.js +3 -42
  46. package/dist/queue-CXrRK85G.js +948 -0
  47. package/dist/redis/index.js +672 -642
  48. package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
  49. package/dist/runtime/index.js +474 -604
  50. package/dist/scheduling/index.js +2 -29
  51. package/dist/scheduling-CPiEdtQd.js +839 -0
  52. package/dist/storage/index.js +2 -15
  53. package/dist/storage-tWHMv2H_.js +841 -0
  54. package/dist/support/expiry.js +69 -12
  55. package/dist/trim-slashes-ZJ_VsY5s.js +25 -0
  56. package/dist/vite/index.js +118 -181
  57. package/dist/vite/plugin.d.ts +1 -1
  58. package/dist/vite-dev-server-CpCUqmiC.js +131 -0
  59. package/package.json +6 -5
  60. package/dist/DocsViewerServiceProvider-RUYDFBMW.js +0 -75
  61. package/dist/McpServiceProvider-SL7DUZ4G.js +0 -8
  62. package/dist/chunk-2SC566CD.js +0 -376
  63. package/dist/chunk-2V3YID4H.js +0 -20
  64. package/dist/chunk-3KV5X2FL.js +0 -31
  65. package/dist/chunk-63SKW5PS.js +0 -457
  66. package/dist/chunk-6J6B5KOB.js +0 -425
  67. package/dist/chunk-A5JJ7XTH.js +0 -24
  68. package/dist/chunk-AZOYI43J.js +0 -18
  69. package/dist/chunk-B5E5VBDI.js +0 -115
  70. package/dist/chunk-DAQKYKLH.js +0 -182
  71. package/dist/chunk-DRNPVBAA.js +0 -1002
  72. package/dist/chunk-EI5GYMAT.js +0 -853
  73. package/dist/chunk-H3B23O6T.js +0 -271
  74. package/dist/chunk-HPET6XY5.js +0 -853
  75. package/dist/chunk-HR2KZ3WL.js +0 -616
  76. package/dist/chunk-IOTWFHZU.js +0 -558
  77. package/dist/chunk-K6CUBJX6.js +0 -642
  78. package/dist/chunk-KTNJG7R7.js +0 -911
  79. package/dist/chunk-LAJQCSSF.js +0 -355
  80. package/dist/chunk-LHR6OHI7.js +0 -189
  81. package/dist/chunk-N7PLD7MB.js +0 -729
  82. package/dist/chunk-O5XIXGYM.js +0 -274
  83. package/dist/chunk-Q2P6YLRR.js +0 -24
  84. package/dist/chunk-QQKTH5KX.js +0 -114
  85. package/dist/chunk-SFQP6O52.js +0 -126
  86. package/dist/chunk-SQR7277M.js +0 -911
  87. package/dist/chunk-T2FFUU6J.js +0 -484
  88. package/dist/chunk-TVV5YNEC.js +0 -1842
  89. package/dist/chunk-UY3AZSYL.js +0 -14
  90. package/dist/chunk-V4DNC2ER.js +0 -114
  91. package/dist/chunk-YSOJKAAU.js +0 -427
  92. package/dist/errors-PUNHAFL2.js +0 -34
@@ -0,0 +1,812 @@
1
+ import { n as isHotReloadRuntime, t as claimHotDisposable } from "./hot-disposables-WsTX2ySN.js";
2
+ import { createHash } from "node:crypto";
3
+ import { mkdir, readFile, readdir, rm, unlink, writeFile } from "node:fs/promises";
4
+ import { dirname, join } from "node:path";
5
+ import { existsSync } from "node:fs";
6
+ //#region src/cache/stores/MemoryStore.ts
7
+ /**
8
+ * In-memory cache store.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const store = new MemoryStore()
13
+ *
14
+ * await store.set('user:1', { name: 'John' }, 3600)
15
+ * const user = await store.get<User>('user:1')
16
+ * ```
17
+ */
18
+ var MemoryStore = class {
19
+ cache = /* @__PURE__ */ new Map();
20
+ maxSize;
21
+ now;
22
+ checkInterval = null;
23
+ constructor(options = {}) {
24
+ this.maxSize = options.maxSize ?? Infinity;
25
+ this.now = options.now ?? Date.now;
26
+ const checkPeriod = options.checkPeriod ?? 6e4;
27
+ if (checkPeriod > 0) {
28
+ this.checkInterval = setInterval(() => {
29
+ this.removeExpired();
30
+ }, checkPeriod);
31
+ if (this.checkInterval.unref) this.checkInterval.unref();
32
+ }
33
+ }
34
+ /**
35
+ * Remove expired items from the cache.
36
+ */
37
+ removeExpired() {
38
+ const now = this.now();
39
+ for (const [key, item] of this.cache) if (item.expiresAt !== null && item.expiresAt <= now) this.cache.delete(key);
40
+ }
41
+ /**
42
+ * Check if an item is expired.
43
+ */
44
+ isExpired(item) {
45
+ return item.expiresAt !== null && item.expiresAt <= this.now();
46
+ }
47
+ /**
48
+ * Evict items if cache is full.
49
+ */
50
+ evictIfNeeded() {
51
+ if (this.cache.size >= this.maxSize) {
52
+ const firstKey = this.cache.keys().next().value;
53
+ if (firstKey !== void 0) this.cache.delete(firstKey);
54
+ }
55
+ }
56
+ async get(key) {
57
+ const item = this.cache.get(key);
58
+ if (!item) return null;
59
+ if (this.isExpired(item)) {
60
+ this.cache.delete(key);
61
+ return null;
62
+ }
63
+ return item.value;
64
+ }
65
+ async set(key, value, ttl) {
66
+ this.evictIfNeeded();
67
+ const expiresAt = ttl ? this.now() + ttl * 1e3 : null;
68
+ this.cache.set(key, {
69
+ value,
70
+ expiresAt
71
+ });
72
+ }
73
+ async has(key) {
74
+ const item = this.cache.get(key);
75
+ if (!item) return false;
76
+ if (this.isExpired(item)) {
77
+ this.cache.delete(key);
78
+ return false;
79
+ }
80
+ return true;
81
+ }
82
+ async delete(key) {
83
+ return this.cache.delete(key);
84
+ }
85
+ async clear() {
86
+ this.cache.clear();
87
+ }
88
+ async increment(key, value = 1) {
89
+ const newValue = (await this.get(key) ?? 0) + value;
90
+ const item = this.cache.get(key);
91
+ const ttl = item?.expiresAt ? Math.max(0, Math.ceil((item.expiresAt - this.now()) / 1e3)) : void 0;
92
+ await this.set(key, newValue, ttl);
93
+ return newValue;
94
+ }
95
+ async decrement(key, value = 1) {
96
+ return this.increment(key, -value);
97
+ }
98
+ async remember(key, ttl, callback) {
99
+ const cached = await this.get(key);
100
+ if (cached !== null) return cached;
101
+ const value = await callback();
102
+ await this.set(key, value, ttl);
103
+ return value;
104
+ }
105
+ async rememberForever(key, callback) {
106
+ const cached = await this.get(key);
107
+ if (cached !== null) return cached;
108
+ const value = await callback();
109
+ await this.set(key, value);
110
+ return value;
111
+ }
112
+ async getMany(keys) {
113
+ const result = /* @__PURE__ */ new Map();
114
+ for (const key of keys) result.set(key, await this.get(key));
115
+ return result;
116
+ }
117
+ async setMany(items, ttl) {
118
+ for (const [key, value] of items) await this.set(key, value, ttl);
119
+ }
120
+ async deleteMany(keys) {
121
+ let deleted = 0;
122
+ for (const key of keys) if (await this.delete(key)) deleted++;
123
+ return deleted;
124
+ }
125
+ async ttl(key) {
126
+ const item = this.cache.get(key);
127
+ if (!item) return -2;
128
+ if (this.isExpired(item)) {
129
+ this.cache.delete(key);
130
+ return -2;
131
+ }
132
+ if (item.expiresAt === null) return -1;
133
+ return Math.max(0, Math.ceil((item.expiresAt - this.now()) / 1e3));
134
+ }
135
+ /**
136
+ * Get the number of items in the cache.
137
+ */
138
+ size() {
139
+ return this.cache.size;
140
+ }
141
+ /**
142
+ * Stop the expiration check interval.
143
+ */
144
+ destroy() {
145
+ if (this.checkInterval) {
146
+ clearInterval(this.checkInterval);
147
+ this.checkInterval = null;
148
+ }
149
+ }
150
+ };
151
+ //#endregion
152
+ //#region src/cache/stores/RedisStore.ts
153
+ /**
154
+ * Redis-backed cache store.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import Redis from 'ioredis'
159
+ *
160
+ * const redis = new Redis(process.env.REDIS_URL)
161
+ * const store = new RedisStore({ client: redis })
162
+ *
163
+ * await store.set('user:1', { name: 'John' }, 3600)
164
+ * const user = await store.get<User>('user:1')
165
+ * ```
166
+ */
167
+ var RedisStore = class {
168
+ client;
169
+ prefix;
170
+ constructor(options) {
171
+ this.client = options.client;
172
+ this.prefix = options.prefix ?? "cache:";
173
+ }
174
+ /**
175
+ * Get the prefixed key.
176
+ */
177
+ prefixKey(key) {
178
+ return `${this.prefix}${key}`;
179
+ }
180
+ /**
181
+ * Serialize a value for storage.
182
+ */
183
+ serialize(value) {
184
+ return JSON.stringify(value);
185
+ }
186
+ /**
187
+ * Deserialize a stored value.
188
+ */
189
+ deserialize(value) {
190
+ if (value === null) return null;
191
+ try {
192
+ return JSON.parse(value);
193
+ } catch {
194
+ return null;
195
+ }
196
+ }
197
+ async get(key) {
198
+ const value = await this.client.get(this.prefixKey(key));
199
+ return this.deserialize(value);
200
+ }
201
+ async set(key, value, ttl) {
202
+ const serialized = this.serialize(value);
203
+ const prefixedKey = this.prefixKey(key);
204
+ if (ttl) await this.client.setex(prefixedKey, ttl, serialized);
205
+ else await this.client.set(prefixedKey, serialized);
206
+ }
207
+ async has(key) {
208
+ return await this.client.exists(this.prefixKey(key)) > 0;
209
+ }
210
+ async delete(key) {
211
+ return await this.client.del(this.prefixKey(key)) > 0;
212
+ }
213
+ async clear() {
214
+ const keys = await this.client.keys(`${this.prefix}*`);
215
+ if (keys.length > 0) await this.client.del(...keys);
216
+ }
217
+ async increment(key, value = 1) {
218
+ const prefixedKey = this.prefixKey(key);
219
+ if (!await this.client.exists(prefixedKey)) await this.client.set(prefixedKey, "0");
220
+ return this.client.incrby(prefixedKey, value);
221
+ }
222
+ async decrement(key, value = 1) {
223
+ const prefixedKey = this.prefixKey(key);
224
+ if (!await this.client.exists(prefixedKey)) await this.client.set(prefixedKey, "0");
225
+ return this.client.decrby(prefixedKey, value);
226
+ }
227
+ async remember(key, ttl, callback) {
228
+ const cached = await this.get(key);
229
+ if (cached !== null) return cached;
230
+ const value = await callback();
231
+ await this.set(key, value, ttl);
232
+ return value;
233
+ }
234
+ async rememberForever(key, callback) {
235
+ const cached = await this.get(key);
236
+ if (cached !== null) return cached;
237
+ const value = await callback();
238
+ await this.set(key, value);
239
+ return value;
240
+ }
241
+ async getMany(keys) {
242
+ const prefixedKeys = keys.map((key) => this.prefixKey(key));
243
+ const values = await this.client.mget(...prefixedKeys);
244
+ const result = /* @__PURE__ */ new Map();
245
+ for (let i = 0; i < keys.length; i++) result.set(keys[i], this.deserialize(values[i]));
246
+ return result;
247
+ }
248
+ async setMany(items, ttl) {
249
+ const pipeline = this.client.pipeline();
250
+ for (const [key, value] of items) {
251
+ const serialized = this.serialize(value);
252
+ const prefixedKey = this.prefixKey(key);
253
+ if (ttl) pipeline.setex(prefixedKey, ttl, serialized);
254
+ else pipeline.set(prefixedKey, serialized);
255
+ }
256
+ await pipeline.exec();
257
+ }
258
+ async deleteMany(keys) {
259
+ if (keys.length === 0) return 0;
260
+ const prefixedKeys = keys.map((key) => this.prefixKey(key));
261
+ return this.client.del(...prefixedKeys);
262
+ }
263
+ async ttl(key) {
264
+ return this.client.ttl(this.prefixKey(key));
265
+ }
266
+ /**
267
+ * Get the Redis client instance.
268
+ */
269
+ getClient() {
270
+ return this.client;
271
+ }
272
+ /**
273
+ * Get the key prefix.
274
+ */
275
+ getPrefix() {
276
+ return this.prefix;
277
+ }
278
+ };
279
+ //#endregion
280
+ //#region src/cache/stores/FileStore.ts
281
+ /**
282
+ * File-based cache store.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * const store = new FileStore({ path: './storage/cache' })
287
+ *
288
+ * await store.set('user:1', { name: 'John' }, 3600)
289
+ * const user = await store.get<User>('user:1')
290
+ * ```
291
+ */
292
+ var FileStore = class {
293
+ basePath;
294
+ extension;
295
+ now;
296
+ constructor(options) {
297
+ this.basePath = options.path;
298
+ this.extension = options.extension ?? ".cache";
299
+ this.now = options.now ?? Date.now;
300
+ }
301
+ /**
302
+ * Generate a hashed filename for a cache key.
303
+ */
304
+ getFilePath(key) {
305
+ const hash = createHash("sha256").update(key).digest("hex");
306
+ const dir = hash.slice(0, 2);
307
+ return join(this.basePath, dir, `${hash}${this.extension}`);
308
+ }
309
+ /**
310
+ * Ensure the cache directory exists.
311
+ */
312
+ async ensureDirectory(filePath) {
313
+ const dir = dirname(filePath);
314
+ if (!existsSync(dir)) await mkdir(dir, { recursive: true });
315
+ }
316
+ /**
317
+ * Read a cache file.
318
+ */
319
+ async readCacheFile(filePath) {
320
+ try {
321
+ const content = await readFile(filePath, "utf-8");
322
+ return JSON.parse(content);
323
+ } catch {
324
+ return null;
325
+ }
326
+ }
327
+ /**
328
+ * Write a cache file.
329
+ */
330
+ async writeCacheFile(filePath, item) {
331
+ await this.ensureDirectory(filePath);
332
+ await writeFile(filePath, JSON.stringify(item), "utf-8");
333
+ }
334
+ /**
335
+ * Delete a cache file.
336
+ */
337
+ async deleteCacheFile(filePath) {
338
+ try {
339
+ await unlink(filePath);
340
+ return true;
341
+ } catch {
342
+ return false;
343
+ }
344
+ }
345
+ /**
346
+ * Check if a cached item is expired.
347
+ */
348
+ isExpired(item) {
349
+ return item.expiresAt !== null && item.expiresAt <= this.now();
350
+ }
351
+ async get(key) {
352
+ const filePath = this.getFilePath(key);
353
+ const item = await this.readCacheFile(filePath);
354
+ if (!item) return null;
355
+ if (this.isExpired(item)) {
356
+ await this.deleteCacheFile(filePath);
357
+ return null;
358
+ }
359
+ return item.value;
360
+ }
361
+ async set(key, value, ttl) {
362
+ const filePath = this.getFilePath(key);
363
+ const expiresAt = ttl ? this.now() + ttl * 1e3 : null;
364
+ await this.writeCacheFile(filePath, {
365
+ value,
366
+ expiresAt
367
+ });
368
+ }
369
+ async has(key) {
370
+ return await this.get(key) !== null;
371
+ }
372
+ async delete(key) {
373
+ const filePath = this.getFilePath(key);
374
+ return this.deleteCacheFile(filePath);
375
+ }
376
+ async clear() {
377
+ if (!existsSync(this.basePath)) return;
378
+ await rm(this.basePath, {
379
+ recursive: true,
380
+ force: true
381
+ });
382
+ await mkdir(this.basePath, { recursive: true });
383
+ }
384
+ async increment(key, value = 1) {
385
+ const newValue = (await this.get(key) ?? 0) + value;
386
+ const filePath = this.getFilePath(key);
387
+ const item = await this.readCacheFile(filePath);
388
+ const ttl = item?.expiresAt ? Math.max(0, Math.ceil((item.expiresAt - this.now()) / 1e3)) : void 0;
389
+ await this.set(key, newValue, ttl);
390
+ return newValue;
391
+ }
392
+ async decrement(key, value = 1) {
393
+ return this.increment(key, -value);
394
+ }
395
+ async remember(key, ttl, callback) {
396
+ const cached = await this.get(key);
397
+ if (cached !== null) return cached;
398
+ const value = await callback();
399
+ await this.set(key, value, ttl);
400
+ return value;
401
+ }
402
+ async rememberForever(key, callback) {
403
+ const cached = await this.get(key);
404
+ if (cached !== null) return cached;
405
+ const value = await callback();
406
+ await this.set(key, value);
407
+ return value;
408
+ }
409
+ async getMany(keys) {
410
+ const result = /* @__PURE__ */ new Map();
411
+ for (const key of keys) result.set(key, await this.get(key));
412
+ return result;
413
+ }
414
+ async setMany(items, ttl) {
415
+ const promises = [];
416
+ for (const [key, value] of items) promises.push(this.set(key, value, ttl));
417
+ await Promise.all(promises);
418
+ }
419
+ async deleteMany(keys) {
420
+ let deleted = 0;
421
+ for (const key of keys) if (await this.delete(key)) deleted++;
422
+ return deleted;
423
+ }
424
+ async ttl(key) {
425
+ const filePath = this.getFilePath(key);
426
+ const item = await this.readCacheFile(filePath);
427
+ if (!item) return -2;
428
+ if (this.isExpired(item)) {
429
+ await this.deleteCacheFile(filePath);
430
+ return -2;
431
+ }
432
+ if (item.expiresAt === null) return -1;
433
+ return Math.max(0, Math.ceil((item.expiresAt - this.now()) / 1e3));
434
+ }
435
+ /**
436
+ * Clean up expired cache files.
437
+ * This can be called periodically to free disk space.
438
+ */
439
+ async cleanup() {
440
+ let cleaned = 0;
441
+ if (!existsSync(this.basePath)) return cleaned;
442
+ const subdirs = await readdir(this.basePath);
443
+ for (const subdir of subdirs) {
444
+ const subdirPath = join(this.basePath, subdir);
445
+ let files;
446
+ try {
447
+ files = await readdir(subdirPath);
448
+ } catch {
449
+ continue;
450
+ }
451
+ for (const file of files) {
452
+ if (!file.endsWith(this.extension)) continue;
453
+ const filePath = join(subdirPath, file);
454
+ const item = await this.readCacheFile(filePath);
455
+ if (item && this.isExpired(item)) {
456
+ await this.deleteCacheFile(filePath);
457
+ cleaned++;
458
+ }
459
+ }
460
+ }
461
+ return cleaned;
462
+ }
463
+ /**
464
+ * Get the base path.
465
+ */
466
+ getBasePath() {
467
+ return this.basePath;
468
+ }
469
+ };
470
+ //#endregion
471
+ //#region src/cache/TaggedCache.ts
472
+ /**
473
+ * Tagged cache implementation.
474
+ * Allows grouping cache items by tags for bulk invalidation.
475
+ *
476
+ * @example
477
+ * ```ts
478
+ * const tagged = new TaggedCache(store, ['posts', 'user:1'])
479
+ *
480
+ * await tagged.set('user:1:posts', posts, 3600)
481
+ * await tagged.flush() // Removes all items with these tags
482
+ * ```
483
+ */
484
+ var TaggedCache = class {
485
+ store;
486
+ tags;
487
+ tagSetPrefix;
488
+ constructor(store, tags, tagSetPrefix = "tag:") {
489
+ this.store = store;
490
+ this.tags = tags;
491
+ this.tagSetPrefix = tagSetPrefix;
492
+ }
493
+ /**
494
+ * Get a unique namespace for the current tags.
495
+ */
496
+ async getTagNamespace() {
497
+ const namespaces = [];
498
+ for (const tag of this.tags) {
499
+ const tagKey = `${this.tagSetPrefix}${tag}`;
500
+ let namespace = await this.store.get(tagKey);
501
+ if (!namespace) {
502
+ namespace = this.generateNamespace();
503
+ await this.store.set(tagKey, namespace);
504
+ }
505
+ namespaces.push(namespace);
506
+ }
507
+ return namespaces.join(":");
508
+ }
509
+ /**
510
+ * Generate a unique namespace identifier.
511
+ */
512
+ generateNamespace() {
513
+ return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
514
+ }
515
+ /**
516
+ * Get the tagged key.
517
+ */
518
+ async taggedKey(key) {
519
+ return `tagged:${await this.getTagNamespace()}:${key}`;
520
+ }
521
+ /**
522
+ * Track a key in the tag sets.
523
+ */
524
+ async trackKey(key) {
525
+ const taggedKey = await this.taggedKey(key);
526
+ for (const tag of this.tags) {
527
+ const setKey = `${this.tagSetPrefix}${tag}:keys`;
528
+ const keys = await this.store.get(setKey) ?? [];
529
+ if (!keys.includes(taggedKey)) {
530
+ keys.push(taggedKey);
531
+ await this.store.set(setKey, keys);
532
+ }
533
+ }
534
+ }
535
+ async get(key) {
536
+ const taggedKey = await this.taggedKey(key);
537
+ return this.store.get(taggedKey);
538
+ }
539
+ async set(key, value, ttl) {
540
+ const taggedKey = await this.taggedKey(key);
541
+ await this.store.set(taggedKey, value, ttl);
542
+ await this.trackKey(key);
543
+ }
544
+ async has(key) {
545
+ const taggedKey = await this.taggedKey(key);
546
+ return this.store.has(taggedKey);
547
+ }
548
+ async delete(key) {
549
+ const taggedKey = await this.taggedKey(key);
550
+ return this.store.delete(taggedKey);
551
+ }
552
+ async clear() {
553
+ await this.flush();
554
+ }
555
+ async increment(key, value = 1) {
556
+ const taggedKey = await this.taggedKey(key);
557
+ await this.trackKey(key);
558
+ return this.store.increment(taggedKey, value);
559
+ }
560
+ async decrement(key, value = 1) {
561
+ const taggedKey = await this.taggedKey(key);
562
+ await this.trackKey(key);
563
+ return this.store.decrement(taggedKey, value);
564
+ }
565
+ async remember(key, ttl, callback) {
566
+ const cached = await this.get(key);
567
+ if (cached !== null) return cached;
568
+ const value = await callback();
569
+ await this.set(key, value, ttl);
570
+ return value;
571
+ }
572
+ async rememberForever(key, callback) {
573
+ const cached = await this.get(key);
574
+ if (cached !== null) return cached;
575
+ const value = await callback();
576
+ await this.set(key, value);
577
+ return value;
578
+ }
579
+ async getMany(keys) {
580
+ const result = /* @__PURE__ */ new Map();
581
+ for (const key of keys) result.set(key, await this.get(key));
582
+ return result;
583
+ }
584
+ async setMany(items, ttl) {
585
+ for (const [key, value] of items) await this.set(key, value, ttl);
586
+ }
587
+ async deleteMany(keys) {
588
+ let deleted = 0;
589
+ for (const key of keys) if (await this.delete(key)) deleted++;
590
+ return deleted;
591
+ }
592
+ async ttl(key) {
593
+ const taggedKey = await this.taggedKey(key);
594
+ return this.store.ttl(taggedKey);
595
+ }
596
+ /**
597
+ * Flush all items with the current tags.
598
+ * This works by resetting the tag namespaces, effectively
599
+ * making all previously tagged items inaccessible.
600
+ */
601
+ async flush() {
602
+ for (const tag of this.tags) {
603
+ const tagKey = `${this.tagSetPrefix}${tag}`;
604
+ const setKey = `${this.tagSetPrefix}${tag}:keys`;
605
+ const keys = await this.store.get(setKey) ?? [];
606
+ for (const key of keys) await this.store.delete(key);
607
+ await this.store.delete(tagKey);
608
+ await this.store.delete(setKey);
609
+ }
610
+ }
611
+ /**
612
+ * Get the current tags.
613
+ */
614
+ getTags() {
615
+ return [...this.tags];
616
+ }
617
+ /**
618
+ * Get the underlying store.
619
+ */
620
+ getStore() {
621
+ return this.store;
622
+ }
623
+ };
624
+ //#endregion
625
+ //#region src/cache/CacheManager.ts
626
+ /**
627
+ * Taggable wrapper that adds tag support to any cache store.
628
+ */
629
+ var TaggableCacheStoreWrapper = class {
630
+ store;
631
+ constructor(store) {
632
+ this.store = store;
633
+ }
634
+ get(key) {
635
+ return this.store.get(key);
636
+ }
637
+ set(key, value, ttl) {
638
+ return this.store.set(key, value, ttl);
639
+ }
640
+ has(key) {
641
+ return this.store.has(key);
642
+ }
643
+ delete(key) {
644
+ return this.store.delete(key);
645
+ }
646
+ clear() {
647
+ return this.store.clear();
648
+ }
649
+ increment(key, value) {
650
+ return this.store.increment(key, value);
651
+ }
652
+ decrement(key, value) {
653
+ return this.store.decrement(key, value);
654
+ }
655
+ remember(key, ttl, callback) {
656
+ return this.store.remember(key, ttl, callback);
657
+ }
658
+ rememberForever(key, callback) {
659
+ return this.store.rememberForever(key, callback);
660
+ }
661
+ getMany(keys) {
662
+ return this.store.getMany(keys);
663
+ }
664
+ setMany(items, ttl) {
665
+ return this.store.setMany(items, ttl);
666
+ }
667
+ deleteMany(keys) {
668
+ return this.store.deleteMany(keys);
669
+ }
670
+ ttl(key) {
671
+ return this.store.ttl(key);
672
+ }
673
+ tags(tags) {
674
+ return new TaggedCache(this.store, tags);
675
+ }
676
+ };
677
+ /**
678
+ * Cache manager for handling multiple cache stores.
679
+ *
680
+ * @example
681
+ * ```ts
682
+ * const cache = new CacheManager({
683
+ * default: 'redis',
684
+ * stores: {
685
+ * memory: { driver: 'memory' },
686
+ * redis: { driver: 'redis', client: redisClient },
687
+ * file: { driver: 'file', path: './storage/cache' },
688
+ * }
689
+ * })
690
+ *
691
+ * // Use the default store
692
+ * await cache.store().set('key', 'value', 3600)
693
+ *
694
+ * // Use a specific store
695
+ * await cache.store('memory').set('key', 'value')
696
+ *
697
+ * // Use tagged cache
698
+ * await cache.store().tags(['posts', 'user:1']).set('user:1:posts', posts)
699
+ * await cache.store().tags(['user:1']).flush()
700
+ * ```
701
+ */
702
+ var CacheManager = class {
703
+ defaultStoreName;
704
+ storeFactories = /* @__PURE__ */ new Map();
705
+ resolvedStores = /* @__PURE__ */ new Map();
706
+ /**
707
+ * Where this manager was built, for identifying its stores across hot reloads.
708
+ *
709
+ * Captured here rather than in `store()` because stores resolve lazily, from
710
+ * whichever request first asks for one — a call site that says nothing about
711
+ * which manager owns the result. Skipped outside `--hot`, where it would be a
712
+ * formatted stack string held for the manager's lifetime and never read.
713
+ */
714
+ builtAt;
715
+ constructor(config = {}) {
716
+ this.builtAt = isHotReloadRuntime() ? (/* @__PURE__ */ new Error()).stack : void 0;
717
+ this.defaultStoreName = config.default ?? "memory";
718
+ this.registerBuiltinDrivers();
719
+ if (config.stores) for (const [name, storeConfig] of Object.entries(config.stores)) this.registerStoreFromConfig(name, storeConfig);
720
+ if (!this.storeFactories.has(this.defaultStoreName) && this.defaultStoreName === "memory") this.storeFactories.set("memory", () => new MemoryStore());
721
+ }
722
+ driverFactories = /* @__PURE__ */ new Map();
723
+ /**
724
+ * Register built-in store drivers.
725
+ */
726
+ registerBuiltinDrivers() {
727
+ this.driverFactories.set("memory", (options) => {
728
+ return new MemoryStore(options);
729
+ });
730
+ this.driverFactories.set("redis", (options) => {
731
+ return new RedisStore(options);
732
+ });
733
+ this.driverFactories.set("file", (options) => {
734
+ return new FileStore(options);
735
+ });
736
+ }
737
+ /**
738
+ * Register a store from configuration.
739
+ */
740
+ registerStoreFromConfig(name, config) {
741
+ const { driver, ...options } = config;
742
+ const factory = this.driverFactories.get(driver);
743
+ if (!factory) throw new Error(`Unknown cache driver: ${driver}`);
744
+ this.storeFactories.set(name, () => factory(options));
745
+ }
746
+ /**
747
+ * Get a cache store by name.
748
+ * Returns the default store if no name is specified.
749
+ */
750
+ store(name) {
751
+ const storeName = name ?? this.defaultStoreName;
752
+ const cached = this.resolvedStores.get(storeName);
753
+ if (cached) return cached;
754
+ const factory = this.storeFactories.get(storeName);
755
+ if (!factory) throw new Error(`Cache store not found: ${storeName}`);
756
+ const raw = factory();
757
+ this.stopPreviousStore(storeName, raw);
758
+ const store = new TaggableCacheStoreWrapper(raw);
759
+ this.resolvedStores.set(storeName, store);
760
+ return store;
761
+ }
762
+ /**
763
+ * Under `bun --hot`, stops the sweep timer held by the store this one replaces.
764
+ *
765
+ * Registered from here rather than from `MemoryStore` itself because this is
766
+ * where the store's name is known, and the name is what keeps two memory
767
+ * stores in one config from sharing a slot and cancelling each other's sweep.
768
+ */
769
+ stopPreviousStore(storeName, store) {
770
+ const disposable = store;
771
+ if (typeof disposable.destroy === "function") claimHotDisposable("cache-store", this.builtAt, storeName, () => disposable.destroy?.());
772
+ }
773
+ /**
774
+ * Register a custom store factory.
775
+ */
776
+ registerStore(name, factory) {
777
+ this.storeFactories.set(name, factory);
778
+ this.resolvedStores.delete(name);
779
+ }
780
+ /**
781
+ * Register a custom driver.
782
+ */
783
+ registerDriver(name, factory) {
784
+ this.driverFactories.set(name, factory);
785
+ }
786
+ /**
787
+ * Check if a store is registered.
788
+ */
789
+ hasStore(name) {
790
+ return this.storeFactories.has(name);
791
+ }
792
+ /**
793
+ * Get the default store name.
794
+ */
795
+ getDefaultStoreName() {
796
+ return this.defaultStoreName;
797
+ }
798
+ /**
799
+ * Get all registered store names.
800
+ */
801
+ getStoreNames() {
802
+ return Array.from(this.storeFactories.keys());
803
+ }
804
+ };
805
+ /**
806
+ * Create a cache manager with configuration.
807
+ */
808
+ function createCacheManager(config) {
809
+ return new CacheManager(config);
810
+ }
811
+ //#endregion
812
+ export { RedisStore as a, FileStore as i, createCacheManager as n, MemoryStore as o, TaggedCache as r, CacheManager as t };