tricache 0.9.0 → 1.0.0

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 (47) hide show
  1. package/README.md +7 -3
  2. package/dist/{cache-service-KI5ACF3F.js → cache-service-26GCO3UU.js} +3 -3
  3. package/dist/{cache-service-DVvroyqA.d.ts → cache-service-pJ-nFcVj.d.ts} +29 -12
  4. package/dist/{chunk-XZL45ABR.js → chunk-2AJKEQMZ.js} +54 -1
  5. package/dist/{chunk-PL5J3ZNT.js → chunk-7SZSQS55.js} +201 -34
  6. package/dist/{chunk-LJ5QJB5O.js → chunk-UBWLPKTI.js} +127 -11
  7. package/dist/cli.cjs +376 -50
  8. package/dist/cli.js +4 -4
  9. package/dist/dashboard/index.d.ts +2 -2
  10. package/dist/drizzle/index.cjs +384 -51
  11. package/dist/drizzle/index.d.ts +2 -2
  12. package/dist/drizzle/index.js +1 -1
  13. package/dist/edge/index.cjs +64 -8
  14. package/dist/edge/index.d.ts +3 -3
  15. package/dist/edge/index.js +37 -9
  16. package/dist/hono/index.cjs +384 -51
  17. package/dist/hono/index.d.ts +2 -2
  18. package/dist/hono/index.js +1 -1
  19. package/dist/{hono-VA9A1m0I.d.ts → hono-DuNvciyq.d.ts} +4 -4
  20. package/dist/http/index.cjs +384 -51
  21. package/dist/http/index.d.ts +3 -3
  22. package/dist/http/index.js +8 -8
  23. package/dist/index.cjs +385 -49
  24. package/dist/index.d.ts +35 -6
  25. package/dist/index.js +16 -6
  26. package/dist/nestjs/index.cjs +375 -49
  27. package/dist/nestjs/index.d.ts +2 -2
  28. package/dist/nestjs/index.js +4 -4
  29. package/dist/next/index.cjs +375 -49
  30. package/dist/next/index.d.ts +2 -2
  31. package/dist/next/index.js +3 -3
  32. package/dist/prisma/index.d.ts +2 -2
  33. package/dist/serialize-worker.cjs +127 -9
  34. package/dist/serialize-worker.js +8 -4
  35. package/dist/{types-B_QkVwJ7.d.ts → types-5HXqBfAf.d.ts} +51 -7
  36. package/package.json +7 -4
  37. package/src/cache-service.ts +238 -33
  38. package/src/cli.ts +1 -1
  39. package/src/compression.ts +193 -15
  40. package/src/cross-region.ts +1 -1
  41. package/src/edge/cache.ts +38 -11
  42. package/src/env.d.ts +13 -0
  43. package/src/index.ts +9 -1
  44. package/src/serialize-worker.ts +6 -2
  45. package/src/types.ts +43 -5
  46. package/src/utils/tag-matcher.ts +90 -0
  47. /package/dist/{chunk-QNU7B43J.js → chunk-VXFTFZ5N.js} +0 -0
@@ -497,13 +497,38 @@ var EnvelopeEncryption = class {
497
497
 
498
498
  // src/compression.ts
499
499
  import zlib from "zlib";
500
+ import lz4js from "lz4js";
501
+ import { ZstdCodec } from "zstd-codec";
500
502
  var PREFIX_COMPRESSED = "cmp:v1:";
501
503
  var PREFIX_ENC_COMPRESSED = "ecp:v1:";
502
504
  var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
505
+ var zstdSimple = null;
506
+ var zstdInitPromise = null;
507
+ function initZstd() {
508
+ if (zstdSimple) return Promise.resolve(zstdSimple);
509
+ if (!zstdInitPromise) {
510
+ zstdInitPromise = new Promise((resolve) => {
511
+ try {
512
+ ZstdCodec.run((zstd) => {
513
+ zstdSimple = new zstd.Simple();
514
+ resolve(zstdSimple);
515
+ });
516
+ } catch {
517
+ resolve(null);
518
+ }
519
+ });
520
+ }
521
+ return zstdInitPromise;
522
+ }
523
+ initZstd().catch(() => {
524
+ });
503
525
  function isCompressed(buf) {
504
526
  return buf.length >= 4 && buf[0] === 84 && buf[1] === 82 && buf[2] === 67 && buf[3] === 1;
505
527
  }
506
- function compressBuffer(buf, algorithm) {
528
+ function compressBuffer(buf, algorithm, custom) {
529
+ if (custom) {
530
+ return custom.compress(buf);
531
+ }
507
532
  if (algorithm === "brotli") {
508
533
  return zlib.brotliCompressSync(buf, {
509
534
  params: {
@@ -515,37 +540,127 @@ function compressBuffer(buf, algorithm) {
515
540
  if (algorithm === "gzip") {
516
541
  return zlib.gzipSync(buf, { level: 6 });
517
542
  }
543
+ if (algorithm === "lz4") {
544
+ return Buffer.from(lz4js.compress(buf));
545
+ }
546
+ if (algorithm === "zstd") {
547
+ if (!zstdSimple) {
548
+ return zlib.brotliCompressSync(buf, {
549
+ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 }
550
+ });
551
+ }
552
+ return Buffer.from(zstdSimple.compress(buf));
553
+ }
518
554
  return buf;
519
555
  }
520
- function decompressBuffer(buf, algorithm = "brotli") {
556
+ var MAX_DECOMPRESS_BYTES = 25 * 1024 * 1024;
557
+ function decompressBuffer(buf, algorithm = "zstd", custom) {
558
+ if (custom) {
559
+ const res = custom.decompress(buf);
560
+ if (res.length > MAX_DECOMPRESS_BYTES) {
561
+ throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
562
+ }
563
+ return res;
564
+ }
565
+ const checkSafety = (decomp) => {
566
+ if (decomp.length > MAX_DECOMPRESS_BYTES) {
567
+ throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
568
+ }
569
+ return decomp;
570
+ };
571
+ if (buf.length >= 4 && buf[0] === 40 && buf[1] === 181 && buf[2] === 47 && buf[3] === 253) {
572
+ if (zstdSimple) {
573
+ try {
574
+ return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
575
+ } catch (err) {
576
+ if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
577
+ }
578
+ }
579
+ }
580
+ if (buf.length >= 4 && buf[0] === 4 && buf[1] === 34 && buf[2] === 77 && buf[3] === 24) {
581
+ try {
582
+ return checkSafety(Buffer.from(lz4js.decompress(buf)));
583
+ } catch (err) {
584
+ if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
585
+ }
586
+ }
587
+ if (buf.length >= 2 && buf[0] === 31 && buf[1] === 139) {
588
+ try {
589
+ return zlib.gunzipSync(buf, { maxOutputLength: MAX_DECOMPRESS_BYTES });
590
+ } catch (err) {
591
+ if (err?.code === "ERR_BUFFER_TOO_LARGE") {
592
+ throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
593
+ }
594
+ }
595
+ }
596
+ if (algorithm === "none") {
597
+ return buf;
598
+ }
599
+ if (algorithm === "lz4") {
600
+ try {
601
+ return checkSafety(Buffer.from(lz4js.decompress(buf)));
602
+ } catch (err) {
603
+ if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
604
+ }
605
+ }
606
+ if (algorithm === "zstd" && zstdSimple) {
607
+ try {
608
+ return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
609
+ } catch (err) {
610
+ if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
611
+ }
612
+ }
521
613
  const tryBrotli = () => {
522
614
  try {
523
- return zlib.brotliDecompressSync(buf);
524
- } catch {
615
+ return zlib.brotliDecompressSync(buf, { maxOutputLength: MAX_DECOMPRESS_BYTES });
616
+ } catch (err) {
617
+ if (err?.code === "ERR_BUFFER_TOO_LARGE") {
618
+ throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
619
+ }
525
620
  return null;
526
621
  }
527
622
  };
528
623
  const tryGzip = () => {
529
624
  try {
530
- return zlib.gunzipSync(buf);
531
- } catch {
625
+ return zlib.gunzipSync(buf, { maxOutputLength: MAX_DECOMPRESS_BYTES });
626
+ } catch (err) {
627
+ if (err?.code === "ERR_BUFFER_TOO_LARGE") {
628
+ throw new Error(`tricache: decompressed payload exceeds maximum safety threshold of ${MAX_DECOMPRESS_BYTES} bytes`);
629
+ }
630
+ return null;
631
+ }
632
+ };
633
+ const tryLz4 = () => {
634
+ try {
635
+ return checkSafety(Buffer.from(lz4js.decompress(buf)));
636
+ } catch (err) {
637
+ if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
638
+ return null;
639
+ }
640
+ };
641
+ const tryZstd = () => {
642
+ if (!zstdSimple) return null;
643
+ try {
644
+ return checkSafety(Buffer.from(zstdSimple.decompress(buf)));
645
+ } catch (err) {
646
+ if (err?.message?.includes("exceeds maximum safety threshold")) throw err;
532
647
  return null;
533
648
  }
534
649
  };
535
- const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
650
+ const result = (algorithm === "gzip" ? tryGzip() : null) ?? (algorithm === "lz4" ? tryLz4() : null) ?? (algorithm === "zstd" ? tryZstd() : null) ?? tryBrotli() ?? tryGzip() ?? tryLz4() ?? tryZstd();
536
651
  if (result === null) {
537
652
  throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
538
653
  }
539
654
  return result;
540
655
  }
541
- function compressWithHeader(buf, algorithm) {
656
+ function compressWithHeader(buf, algorithm, custom) {
542
657
  if (algorithm === "none") return buf;
543
- const compressed = compressBuffer(buf, algorithm);
658
+ const compressed = compressBuffer(buf, algorithm, custom);
544
659
  return Buffer.concat([COMPRESSION_MAGIC, compressed]);
545
660
  }
546
- function decompressWithHeader(buf, algorithm = "brotli") {
661
+ function decompressWithHeader(buf, algorithm = "zstd", custom) {
547
662
  if (isCompressed(buf)) {
548
- return decompressBuffer(buf.subarray(4), algorithm);
663
+ return decompressBuffer(buf.subarray(4), algorithm, custom);
549
664
  }
550
665
  return buf;
551
666
  }
@@ -557,6 +672,7 @@ export {
557
672
  EnvelopeEncryption,
558
673
  PREFIX_COMPRESSED,
559
674
  PREFIX_ENC_COMPRESSED,
675
+ initZstd,
560
676
  compressBuffer,
561
677
  decompressBuffer,
562
678
  compressWithHeader,