tricache 1.0.0 → 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 (97) hide show
  1. package/README.md +36 -28
  2. package/dist/{cache-service-pJ-nFcVj.d.ts → cache-service-B108FzER.d.ts} +63 -3
  3. package/dist/cache-service-CWI4jkS7.d.cts +963 -0
  4. package/dist/{cache-service-26GCO3UU.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-VXFTFZ5N.js → chunk-VNRDL5RA.js} +117 -138
  10. package/dist/{chunk-7SZSQS55.js → chunk-WYF7NOAT.js} +1831 -759
  11. package/dist/cli.cjs +1757 -777
  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 +1757 -773
  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 +280 -153
  23. package/dist/edge/index.d.cts +127 -0
  24. package/dist/edge/index.d.ts +3 -3
  25. package/dist/edge/index.js +254 -147
  26. package/dist/hono/index.cjs +1805 -793
  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-DuNvciyq.d.ts → hono-3rDMYVtY.d.ts} +23 -3
  31. package/dist/hono-C886ok3X.d.cts +391 -0
  32. package/dist/http/index.cjs +1946 -842
  33. package/dist/http/index.d.cts +103 -0
  34. package/dist/http/index.d.ts +13 -5
  35. package/dist/http/index.js +137 -57
  36. package/dist/index.cjs +2073 -937
  37. package/dist/index.d.cts +1006 -0
  38. package/dist/index.d.ts +40 -8
  39. package/dist/index.js +8 -8
  40. package/dist/murmur3-CMGw9PbV.d.cts +98 -0
  41. package/dist/nestjs/index.cjs +1849 -806
  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 +1913 -887
  46. package/dist/next/index.d.cts +232 -0
  47. package/dist/next/index.d.ts +33 -11
  48. package/dist/next/index.js +174 -107
  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-5HXqBfAf.d.ts → types-BA4pz1A0.d.cts} +26 -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 +123 -25
  61. package/src/cache-service.ts +1238 -659
  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 +202 -129
  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 +110 -26
  74. package/src/http/fastify.ts +38 -28
  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 +132 -57
  84. package/src/next/index.ts +1 -0
  85. package/src/next/isr-handler.ts +1 -4
  86. package/src/next/types.ts +21 -0
  87. package/src/prisma/index.ts +12 -1
  88. package/src/smart-memory-cache.ts +65 -27
  89. package/src/types.ts +27 -2
  90. package/src/utils/cgroup.ts +12 -0
  91. package/src/utils/deadline.ts +14 -0
  92. package/src/utils/diagnostics.ts +27 -0
  93. package/src/utils/redis-slot.ts +13 -0
  94. package/src/wasm/bloom-filter-wasm.ts +9 -4
  95. package/src/worker-pool.ts +17 -8
  96. package/src/wtiny-lfu.ts +20 -6
  97. package/dist/chunk-BQRURGGD.js +0 -60
@@ -230,185 +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
244
  }
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
- }
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]);
321
+ }
322
+ return this;
323
+ }
324
+ hgetall(key) {
325
+ if (typeof this.multiInstance.hGetAll === "function") {
326
+ this.queueCommand("hGetAll", [key]);
327
+ } else {
328
+ this.queueCommand("hgetall", [key]);
407
329
  }
408
330
  return this;
409
331
  }
410
332
  async exec() {
411
- 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
+ }
412
345
  if (!raw) return null;
413
346
  return raw.map((item) => {
414
347
  if (item instanceof Error) {
@@ -418,20 +351,32 @@ var NodeRedisPipelineAdapter = class {
418
351
  });
419
352
  }
420
353
  };
421
- var NodeRedisAdapter = class _NodeRedisAdapter {
354
+ var NodeRedisAdapter = class _NodeRedisAdapter extends EventEmitter {
422
355
  client;
356
+ connecting = null;
357
+ pttl;
423
358
  constructor(client) {
359
+ super();
424
360
  if (!client) {
425
361
  throw new Error("NodeRedisAdapter requires an active @redis/client instance");
426
362
  }
427
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
+ }
428
367
  }
429
368
  get isOpen() {
430
369
  return this.client.isOpen !== false;
431
370
  }
432
371
  async connect() {
372
+ if (this.connecting) return this.connecting;
433
373
  if (typeof this.client.connect === "function" && !this.client.isOpen) {
434
- 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
+ }
435
380
  }
436
381
  return void 0;
437
382
  }
@@ -508,7 +453,7 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
508
453
  return new NodeRedisPipelineAdapter(multiInstance);
509
454
  }
510
455
  pipeline() {
511
- return this.multi();
456
+ return new NodeRedisPipelineAdapter(this.client, false);
512
457
  }
513
458
  async smembers(key) {
514
459
  if (typeof this.client.sMembers === "function") {
@@ -548,12 +493,43 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
548
493
  }
549
494
  return void 0;
550
495
  }
496
+ async hgetall(key) {
497
+ if (typeof this.client.hGetAll === "function") {
498
+ return await this.client.hGetAll(key) ?? {};
499
+ }
500
+ if (typeof this.client.hgetall === "function") {
501
+ return await this.client.hgetall(key) ?? {};
502
+ }
503
+ return {};
504
+ }
551
505
  async publish(channel, message) {
552
506
  return await this.client.publish(channel, message);
553
507
  }
554
508
  async subscribe(...channels) {
555
509
  if (channels.length === 0) return void 0;
556
- 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)]);
557
533
  }
558
534
  scanStream(options) {
559
535
  const ee = new EventEmitter();
@@ -563,8 +539,8 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
563
539
  try {
564
540
  if (typeof this.client.scanIterator === "function") {
565
541
  const batch = [];
566
- for await (const key of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
567
- batch.push(key);
542
+ for await (const keys of this.client.scanIterator({ MATCH: pattern, COUNT: count })) {
543
+ batch.push(...Array.isArray(keys) ? keys : [keys]);
568
544
  if (batch.length >= count) {
569
545
  ee.emit("data", [...batch]);
570
546
  batch.length = 0;
@@ -608,17 +584,24 @@ var NodeRedisAdapter = class _NodeRedisAdapter {
608
584
  return this;
609
585
  }
610
586
  on(event, listener) {
587
+ if (event === "message" || event === "smessage") return super.on(event, listener);
611
588
  if (typeof this.client.on === "function") {
612
- this.client.on(event, listener);
589
+ this.client.on(event === "close" ? "end" : event, listener);
613
590
  }
614
591
  return this;
615
592
  }
616
593
  once(event, listener) {
594
+ if (event === "message" || event === "smessage") return super.once(event, listener);
617
595
  if (typeof this.client.once === "function") {
618
- this.client.once(event, listener);
596
+ this.client.once(event === "close" ? "end" : event, listener);
619
597
  }
620
598
  return this;
621
599
  }
600
+ removeListener(event, listener) {
601
+ super.removeListener(event, listener);
602
+ this.client.removeListener?.(event === "close" ? "end" : event, listener);
603
+ return this;
604
+ }
622
605
  };
623
606
  function createNodeRedisAdapter(client) {
624
607
  if (client instanceof NodeRedisAdapter) return client;
@@ -676,10 +659,6 @@ export {
676
659
  createHttpSnapshotAdapter,
677
660
  createCustomSnapshotAdapter,
678
661
  createMemorySnapshotAdapter,
679
- createHttpMeshRelay,
680
- createCustomCrossRegionRelay,
681
- createMemoryCrossRegionRelay,
682
- createCrossRegionWebhookHandler,
683
662
  NodeRedisAdapter,
684
663
  createNodeRedisAdapter,
685
664
  parseTraceParent,