tricache 1.0.1 → 1.0.2

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 (96) hide show
  1. package/README.md +36 -28
  2. package/dist/{cache-service-CREOp7MM.d.ts → cache-service-B108FzER.d.ts} +36 -4
  3. package/dist/cache-service-CWI4jkS7.d.cts +963 -0
  4. package/dist/{cache-service-MG7PN6MX.js → cache-service-GYPZFB7O.js} +2 -2
  5. package/dist/{chunk-IITRFWHY.js → chunk-EUKB7RKP.js} +21 -4
  6. package/dist/{chunk-2AJKEQMZ.js → chunk-H4GFLQ26.js} +7 -4
  7. package/dist/{chunk-7RV5ZVBB.js → chunk-JNSLMK4Q.js} +111 -29
  8. package/dist/chunk-OYDU3SMI.js +89 -0
  9. package/dist/{chunk-3I5L7K6I.js → chunk-VNRDL5RA.js} +102 -140
  10. package/dist/{chunk-JYQFQJNJ.js → chunk-WYF7NOAT.js} +1443 -609
  11. package/dist/cli.cjs +1367 -625
  12. package/dist/cli.d.cts +12 -0
  13. package/dist/cli.js +27 -6
  14. package/dist/dashboard/index.cjs +111 -29
  15. package/dist/dashboard/index.d.cts +185 -0
  16. package/dist/dashboard/index.d.ts +5 -3
  17. package/dist/dashboard/index.js +1 -1
  18. package/dist/drizzle/index.cjs +1370 -624
  19. package/dist/drizzle/index.d.cts +46 -0
  20. package/dist/drizzle/index.d.ts +2 -2
  21. package/dist/drizzle/index.js +1 -1
  22. package/dist/edge/index.cjs +227 -123
  23. package/dist/edge/index.d.cts +127 -0
  24. package/dist/edge/index.d.ts +3 -3
  25. package/dist/edge/index.js +201 -117
  26. package/dist/hono/index.cjs +1418 -644
  27. package/dist/hono/index.d.cts +93 -0
  28. package/dist/hono/index.d.ts +7 -5
  29. package/dist/hono/index.js +16 -9
  30. package/dist/{hono-DSV2ve66.d.ts → hono-3rDMYVtY.d.ts} +16 -3
  31. package/dist/hono-C886ok3X.d.cts +391 -0
  32. package/dist/http/index.cjs +1514 -679
  33. package/dist/http/index.d.cts +103 -0
  34. package/dist/http/index.d.ts +13 -5
  35. package/dist/http/index.js +92 -43
  36. package/dist/index.cjs +1671 -790
  37. package/dist/index.d.cts +1006 -0
  38. package/dist/index.d.ts +39 -8
  39. package/dist/index.js +8 -8
  40. package/dist/murmur3-CMGw9PbV.d.cts +98 -0
  41. package/dist/nestjs/index.cjs +1454 -649
  42. package/dist/nestjs/index.d.cts +285 -0
  43. package/dist/nestjs/index.d.ts +103 -30
  44. package/dist/nestjs/index.js +115 -32
  45. package/dist/next/index.cjs +1512 -718
  46. package/dist/next/index.d.cts +232 -0
  47. package/dist/next/index.d.ts +33 -11
  48. package/dist/next/index.js +172 -99
  49. package/dist/prisma/index.cjs +11 -1
  50. package/dist/prisma/index.d.cts +62 -0
  51. package/dist/prisma/index.d.ts +2 -2
  52. package/dist/prisma/index.js +11 -1
  53. package/dist/serialize-worker.d.cts +2 -0
  54. package/dist/{types-Dyip5wAl.d.ts → types-BA4pz1A0.d.cts} +24 -9
  55. package/dist/types-BA4pz1A0.d.ts +1519 -0
  56. package/dist/{utils-CEsU_Z0s.d.ts → utils-Bvzn-rKS.d.cts} +3 -1
  57. package/dist/utils-Bvzn-rKS.d.ts +29 -0
  58. package/package.json +96 -36
  59. package/src/adapters/auto-pipeliner.ts +107 -24
  60. package/src/adapters/node-redis.ts +106 -27
  61. package/src/cache-service.ts +802 -502
  62. package/src/cli.ts +24 -6
  63. package/src/cross-region.ts +21 -3
  64. package/src/dashboard/handler.ts +26 -6
  65. package/src/dashboard/html.ts +35 -10
  66. package/src/dashboard/index.ts +35 -13
  67. package/src/dashboard/types.ts +3 -1
  68. package/src/disk-tier.ts +171 -65
  69. package/src/edge/cache.ts +145 -97
  70. package/src/edge/hono.ts +23 -6
  71. package/src/edge/types.ts +2 -0
  72. package/src/hono/index.ts +19 -9
  73. package/src/http/express.ts +62 -17
  74. package/src/http/fastify.ts +35 -21
  75. package/src/http/utils.ts +54 -20
  76. package/src/ipc-telemetry.ts +35 -9
  77. package/src/nestjs/decorators.ts +1 -1
  78. package/src/nestjs/index.ts +2 -0
  79. package/src/nestjs/keyv-adapter.ts +68 -0
  80. package/src/nestjs/tricache.module.ts +13 -11
  81. package/src/nestjs/tricache.store.ts +51 -20
  82. package/src/nestjs/types.ts +29 -15
  83. package/src/next/cache-handler.ts +131 -53
  84. package/src/next/index.ts +1 -0
  85. package/src/next/types.ts +21 -0
  86. package/src/prisma/index.ts +12 -1
  87. package/src/smart-memory-cache.ts +63 -26
  88. package/src/types.ts +25 -2
  89. package/src/utils/cgroup.ts +12 -0
  90. package/src/utils/deadline.ts +14 -0
  91. package/src/utils/diagnostics.ts +27 -0
  92. package/src/utils/redis-slot.ts +13 -0
  93. package/src/wasm/bloom-filter-wasm.ts +9 -4
  94. package/src/worker-pool.ts +17 -8
  95. package/src/wtiny-lfu.ts +20 -6
  96. package/dist/chunk-BQRURGGD.js +0 -60
@@ -230,193 +230,118 @@ function createMemorySnapshotAdapter() {
230
230
  };
231
231
  }
232
232
 
233
- // src/cross-region.ts
234
- function createHttpMeshRelay(options) {
235
- const peerUrls = options.peerUrls;
236
- const timeoutMs = options.timeoutMs ?? 3e3;
237
- const fetcher = options.fetchFn ?? globalThis.fetch;
238
- const headers = {
239
- "Content-Type": "application/json"
240
- };
241
- if (options.authSecret) {
242
- headers["Authorization"] = `Bearer ${options.authSecret}`;
243
- headers["X-TriCache-Auth"] = options.authSecret;
244
- }
245
- return {
246
- async broadcast(event) {
247
- if (!peerUrls.length) return;
248
- const body = JSON.stringify(event);
249
- await Promise.allSettled(
250
- peerUrls.map(async (url) => {
251
- const res = await fetcher(url, {
252
- method: "POST",
253
- headers,
254
- body,
255
- signal: AbortSignal.timeout(timeoutMs)
256
- });
257
- if (!res.ok) {
258
- throw new Error(`HttpMeshRelay: peer ${url} returned HTTP ${res.status} ${res.statusText}`);
259
- }
260
- })
261
- );
262
- }
263
- };
264
- }
265
- function createCustomCrossRegionRelay(relay) {
266
- return relay;
267
- }
268
- function createMemoryCrossRegionRelay() {
269
- const handlers = /* @__PURE__ */ new Map();
270
- let broadcastCount = 0;
271
- return {
272
- async broadcast(event) {
273
- broadcastCount++;
274
- const promises = [];
275
- for (const [region, set] of handlers.entries()) {
276
- if (region !== event.originRegion) {
277
- for (const fn of set) {
278
- promises.push(Promise.resolve(fn(event)));
279
- }
280
- }
281
- }
282
- await Promise.allSettled(promises);
283
- },
284
- register(region, handler) {
285
- if (!handlers.has(region)) {
286
- handlers.set(region, /* @__PURE__ */ new Set());
287
- }
288
- handlers.get(region).add(handler);
289
- return () => {
290
- handlers.get(region)?.delete(handler);
233
+ // src/adapters/node-redis.ts
234
+ import { EventEmitter } from "events";
235
+ var NodeRedisPipelineAdapter = class {
236
+ constructor(multiInstance, transactional = true) {
237
+ this.transactional = transactional;
238
+ this.multiInstance = multiInstance;
239
+ if (typeof multiInstance.pTTL === "function" || typeof multiInstance.pttl === "function") {
240
+ this.pttl = (key) => {
241
+ this.queueCommand(typeof multiInstance.pTTL === "function" ? "pTTL" : "pttl", [key]);
242
+ return this;
291
243
  };
292
- },
293
- getBroadcastCount() {
294
- return broadcastCount;
295
- },
296
- clear() {
297
- handlers.clear();
298
- broadcastCount = 0;
299
- }
300
- };
301
- }
302
- function createCrossRegionWebhookHandler(cache, options = {}) {
303
- return async function handleCrossRegionWebhook(req) {
304
- if (options.authSecret) {
305
- const authHeader = req.headers["authorization"] ?? req.headers["Authorization"];
306
- const customHeader = req.headers["x-tricache-auth"] ?? req.headers["X-TriCache-Auth"];
307
- const bearer = typeof authHeader === "string" && authHeader.startsWith("Bearer ") ? authHeader.slice(7).trim() : authHeader;
308
- const provided = typeof customHeader === "string" ? customHeader : bearer;
309
- if (!provided || provided !== options.authSecret) {
310
- return {
311
- status: 401,
312
- body: { ok: false, message: "Unauthorized: invalid or missing auth secret" }
313
- };
314
- }
315
244
  }
316
- try {
317
- let event;
318
- if (typeof req.body === "string") {
319
- event = JSON.parse(req.body);
320
- } else if (req.body && typeof req.body === "object") {
321
- event = req.body;
322
- } else {
323
- return {
324
- status: 400,
325
- body: { ok: false, message: "Invalid payload: body must be JSON object" }
326
- };
327
- }
328
- const accepted = await cache.receiveCrossRegionInvalidation(event);
329
- return {
330
- status: 200,
331
- body: { ok: true, message: accepted ? "Invalidation applied" : "Invalidation ignored (deduplicated or self)" }
332
- };
333
- } catch (err) {
334
- return {
335
- status: 400,
336
- body: { ok: false, message: err.message }
245
+ if (typeof multiInstance.eval === "function" || typeof multiInstance.addCommand === "function") {
246
+ this.eval = (script, numkeys, ...args) => {
247
+ if (typeof multiInstance.eval === "function") {
248
+ this.queueCommand("eval", [script, { keys: args.slice(0, numkeys).map(String), arguments: args.slice(numkeys).map(String) }]);
249
+ } else this.queueCommand("addCommand", [["EVAL", script, String(numkeys), ...args.map(String)]]);
250
+ return this;
337
251
  };
338
252
  }
339
- };
340
- }
341
-
342
- // src/adapters/node-redis.ts
343
- import { EventEmitter } from "events";
344
- var NodeRedisPipelineAdapter = class {
253
+ }
254
+ transactional;
345
255
  multiInstance;
346
- constructor(multiInstance) {
347
- this.multiInstance = multiInstance;
256
+ operations = [];
257
+ pttl;
258
+ eval;
259
+ queueCommand(method, args) {
260
+ if (this.transactional) this.multiInstance[method](...args);
261
+ else this.operations.push(() => this.multiInstance[method](...args));
348
262
  }
349
263
  get(key) {
350
- this.multiInstance.get(key);
264
+ this.queueCommand("get", [key]);
351
265
  return this;
352
266
  }
353
267
  set(key, value, ...args) {
354
268
  const opts = parseSetArgs(args);
355
269
  if (opts) {
356
- this.multiInstance.set(key, value, opts);
270
+ this.queueCommand("set", [key, value, opts]);
357
271
  } else {
358
- this.multiInstance.set(key, value);
272
+ this.queueCommand("set", [key, value]);
359
273
  }
360
274
  return this;
361
275
  }
362
276
  setex(key, seconds, value) {
363
277
  if (typeof this.multiInstance.setEx === "function") {
364
- this.multiInstance.setEx(key, seconds, value);
278
+ this.queueCommand("setEx", [key, seconds, value]);
365
279
  } else if (typeof this.multiInstance.setex === "function") {
366
- this.multiInstance.setex(key, seconds, value);
280
+ this.queueCommand("setex", [key, seconds, value]);
367
281
  } else {
368
- this.multiInstance.set(key, value, { EX: seconds });
282
+ this.queueCommand("set", [key, value, { EX: seconds }]);
369
283
  }
370
284
  return this;
371
285
  }
372
286
  del(...keys) {
373
287
  if (keys.length === 0) return this;
374
- this.multiInstance.del(keys.length === 1 ? keys[0] : keys);
288
+ this.queueCommand("del", [keys.length === 1 ? keys[0] : keys]);
375
289
  return this;
376
290
  }
377
291
  expire(key, seconds) {
378
- this.multiInstance.expire(key, seconds);
292
+ this.queueCommand("expire", [key, seconds]);
379
293
  return this;
380
294
  }
381
295
  incr(key) {
382
- this.multiInstance.incr(key);
296
+ this.queueCommand("incr", [key]);
383
297
  return this;
384
298
  }
385
299
  sadd(key, ...members) {
386
300
  if (members.length === 0) return this;
387
301
  if (typeof this.multiInstance.sAdd === "function") {
388
- this.multiInstance.sAdd(key, members.length === 1 ? members[0] : members);
302
+ this.queueCommand("sAdd", [key, members.length === 1 ? members[0] : members]);
389
303
  } else {
390
- this.multiInstance.sadd(key, ...members);
304
+ this.queueCommand("sadd", [key, ...members]);
391
305
  }
392
306
  return this;
393
307
  }
394
308
  smembers(key) {
395
309
  if (typeof this.multiInstance.sMembers === "function") {
396
- this.multiInstance.sMembers(key);
310
+ this.queueCommand("sMembers", [key]);
397
311
  } else {
398
- this.multiInstance.smembers(key);
312
+ this.queueCommand("smembers", [key]);
399
313
  }
400
314
  return this;
401
315
  }
402
316
  hset(key, value) {
403
317
  if (typeof this.multiInstance.hSet === "function") {
404
- this.multiInstance.hSet(key, value);
318
+ this.queueCommand("hSet", [key, value]);
405
319
  } else {
406
- this.multiInstance.hset(key, value);
320
+ this.queueCommand("hset", [key, value]);
407
321
  }
408
322
  return this;
409
323
  }
410
324
  hgetall(key) {
411
325
  if (typeof this.multiInstance.hGetAll === "function") {
412
- this.multiInstance.hGetAll(key);
326
+ this.queueCommand("hGetAll", [key]);
413
327
  } else {
414
- this.multiInstance.hgetall(key);
328
+ this.queueCommand("hgetall", [key]);
415
329
  }
416
330
  return this;
417
331
  }
418
332
  async exec() {
419
- const raw = await this.multiInstance.exec();
333
+ let raw;
334
+ if (!this.transactional) {
335
+ const results = await Promise.allSettled(this.operations.splice(0).map((operation) => Promise.resolve().then(operation)));
336
+ return results.map((result) => result.status === "fulfilled" ? [null, result.value] : [result.reason instanceof Error ? result.reason : new Error(String(result.reason)), null]);
337
+ }
338
+ try {
339
+ raw = await this.multiInstance.exec();
340
+ } catch (error) {
341
+ const replies = error.replies;
342
+ if (!Array.isArray(replies)) throw error;
343
+ raw = replies;
344
+ }
420
345
  if (!raw) return null;
421
346
  return raw.map((item) => {
422
347
  if (item instanceof Error) {
@@ -426,20 +351,32 @@ var NodeRedisPipelineAdapter = class {
426
351
  });
427
352
  }
428
353
  };
429
- var NodeRedisAdapter = class _NodeRedisAdapter {
354
+ var NodeRedisAdapter = class _NodeRedisAdapter extends EventEmitter {
430
355
  client;
356
+ connecting = null;
357
+ pttl;
431
358
  constructor(client) {
359
+ super();
432
360
  if (!client) {
433
361
  throw new Error("NodeRedisAdapter requires an active @redis/client instance");
434
362
  }
435
363
  this.client = client;
364
+ if (typeof client.pTTL === "function" || typeof client.pttl === "function") {
365
+ this.pttl = (key) => (client.pTTL ?? client.pttl).call(client, key);
366
+ }
436
367
  }
437
368
  get isOpen() {
438
369
  return this.client.isOpen !== false;
439
370
  }
440
371
  async connect() {
372
+ if (this.connecting) return this.connecting;
441
373
  if (typeof this.client.connect === "function" && !this.client.isOpen) {
442
- return await this.client.connect();
374
+ this.connecting = Promise.resolve(this.client.connect());
375
+ try {
376
+ return await this.connecting;
377
+ } finally {
378
+ this.connecting = null;
379
+ }
443
380
  }
444
381
  return void 0;
445
382
  }
@@ -516,7 +453,7 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
516
453
  return new NodeRedisPipelineAdapter(multiInstance);
517
454
  }
518
455
  pipeline() {
519
- return this.multi();
456
+ return new NodeRedisPipelineAdapter(this.client, false);
520
457
  }
521
458
  async smembers(key) {
522
459
  if (typeof this.client.sMembers === "function") {
@@ -570,7 +507,29 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
570
507
  }
571
508
  async subscribe(...channels) {
572
509
  if (channels.length === 0) return void 0;
573
- return await this.client.subscribe(channels);
510
+ await this.connect();
511
+ return await this.client.subscribe(channels, (message, channel) => this.emit("message", channel, message));
512
+ }
513
+ async unsubscribe(...channels) {
514
+ if (!channels.length || !this.isOpen) return void 0;
515
+ return this.client.unsubscribe(channels);
516
+ }
517
+ async xread(...args) {
518
+ await this.connect();
519
+ const raw = await this.client.sendCommand(["XREAD", ...args.map(String)]);
520
+ if (raw == null) return null;
521
+ if (raw instanceof Map) return [...raw.entries()];
522
+ if (!Array.isArray(raw) && typeof raw === "object") return Object.entries(raw);
523
+ return raw;
524
+ }
525
+ async xadd(...args) {
526
+ return this.client.sendCommand(["XADD", ...args.map(String)]);
527
+ }
528
+ async xrange(...args) {
529
+ return this.client.sendCommand(["XRANGE", ...args.map(String)]);
530
+ }
531
+ async xrevrange(...args) {
532
+ return this.client.sendCommand(["XREVRANGE", ...args.map(String)]);
574
533
  }
575
534
  scanStream(options) {
576
535
  const ee = new EventEmitter();
@@ -580,8 +539,8 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
580
539
  try {
581
540
  if (typeof this.client.scanIterator === "function") {
582
541
  const batch = [];
583
- for await (const key of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
584
- batch.push(key);
542
+ for await (const keys of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
543
+ batch.push(...Array.isArray(keys) ? keys : [keys]);
585
544
  if (batch.length >= count) {
586
545
  ee.emit("data", [...batch]);
587
546
  batch.length = 0;
@@ -625,17 +584,24 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
625
584
  return this;
626
585
  }
627
586
  on(event, listener) {
587
+ if (event === "message" || event === "smessage") return super.on(event, listener);
628
588
  if (typeof this.client.on === "function") {
629
- this.client.on(event, listener);
589
+ this.client.on(event === "close" ? "end" : event, listener);
630
590
  }
631
591
  return this;
632
592
  }
633
593
  once(event, listener) {
594
+ if (event === "message" || event === "smessage") return super.once(event, listener);
634
595
  if (typeof this.client.once === "function") {
635
- this.client.once(event, listener);
596
+ this.client.once(event === "close" ? "end" : event, listener);
636
597
  }
637
598
  return this;
638
599
  }
600
+ removeListener(event, listener) {
601
+ super.removeListener(event, listener);
602
+ this.client.removeListener?.(event === "close" ? "end" : event, listener);
603
+ return this;
604
+ }
639
605
  };
640
606
  function createNodeRedisAdapter(client) {
641
607
  if (client instanceof NodeRedisAdapter) return client;
@@ -693,10 +659,6 @@ export {
693
659
  createHttpSnapshotAdapter,
694
660
  createCustomSnapshotAdapter,
695
661
  createMemorySnapshotAdapter,
696
- createHttpMeshRelay,
697
- createCustomCrossRegionRelay,
698
- createMemoryCrossRegionRelay,
699
- createCrossRegionWebhookHandler,
700
662
  NodeRedisAdapter,
701
663
  createNodeRedisAdapter,
702
664
  parseTraceParent,