tricache 0.8.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.
- package/README.md +10 -4
- package/dashboards/prometheus-scrape.yml +18 -0
- package/dist/{cache-service-5MZXFIGK.js → cache-service-26GCO3UU.js} +3 -3
- package/dist/{cache-service-CyLdpo77.d.ts → cache-service-pJ-nFcVj.d.ts} +44 -12
- package/dist/{chunk-XZL45ABR.js → chunk-2AJKEQMZ.js} +54 -1
- package/dist/{chunk-6PQ5MOWR.js → chunk-7SZSQS55.js} +571 -115
- package/dist/chunk-BQRURGGD.js +60 -0
- package/dist/chunk-IITRFWHY.js +196 -0
- package/dist/{chunk-LJ5QJB5O.js → chunk-UBWLPKTI.js} +127 -11
- package/dist/cli.cjs +767 -133
- package/dist/cli.js +24 -6
- package/dist/dashboard/index.d.ts +2 -2
- package/dist/drizzle/index.cjs +734 -132
- package/dist/drizzle/index.d.ts +2 -2
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +163 -46
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +42 -14
- package/dist/hono/index.cjs +8093 -0
- package/dist/hono/index.d.ts +91 -0
- package/dist/hono/index.js +185 -0
- package/dist/{hono-DQRS1Xd7.d.ts → hono-DuNvciyq.d.ts} +6 -5
- package/dist/http/index.cjs +833 -170
- package/dist/http/index.d.ts +6 -30
- package/dist/http/index.js +9 -59
- package/dist/index.cjs +755 -130
- package/dist/index.d.ts +35 -6
- package/dist/index.js +16 -6
- package/dist/nestjs/index.cjs +725 -130
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/nestjs/index.js +4 -4
- package/dist/next/index.cjs +725 -130
- package/dist/next/index.d.ts +2 -2
- package/dist/next/index.js +3 -3
- package/dist/prisma/index.d.ts +2 -2
- package/dist/serialize-worker.cjs +127 -9
- package/dist/serialize-worker.js +8 -4
- package/dist/{types-B6krw64X.d.ts → types-5HXqBfAf.d.ts} +74 -7
- package/dist/utils-CEsU_Z0s.d.ts +27 -0
- package/package.json +22 -9
- package/src/cache-service.ts +540 -68
- package/src/cli.ts +21 -4
- package/src/compression.ts +193 -15
- package/src/cross-region.ts +1 -1
- package/src/edge/cache.ts +38 -11
- package/src/edge/hono.ts +123 -42
- package/src/env.d.ts +13 -0
- package/src/hono/index.ts +316 -0
- package/src/http/index.ts +5 -0
- package/src/index.ts +9 -1
- package/src/ipc-telemetry.ts +29 -1
- package/src/serialize-worker.ts +6 -2
- package/src/types.ts +68 -5
- package/src/utils/tag-matcher.ts +90 -0
- package/dist/chunk-ZTSWCFYO.js +0 -135
- /package/dist/{chunk-QNU7B43J.js → chunk-VXFTFZ5N.js} +0 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/http/utils.ts
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
function buildDeterministicKey(req, options) {
|
|
4
|
+
if (options?.keyGenerator) {
|
|
5
|
+
return options.keyGenerator(req);
|
|
6
|
+
}
|
|
7
|
+
const method = (req.method || "GET").toUpperCase();
|
|
8
|
+
const rawUrl = req.originalUrl || req.url || "/";
|
|
9
|
+
const qIdx = rawUrl.indexOf("?");
|
|
10
|
+
const path = qIdx >= 0 ? rawUrl.slice(0, qIdx) : rawUrl;
|
|
11
|
+
let queryString = "";
|
|
12
|
+
if (qIdx >= 0) {
|
|
13
|
+
const rawQuery = rawUrl.slice(qIdx + 1);
|
|
14
|
+
try {
|
|
15
|
+
const params = new URLSearchParams(rawQuery);
|
|
16
|
+
params.sort();
|
|
17
|
+
queryString = params.toString();
|
|
18
|
+
} catch {
|
|
19
|
+
queryString = rawQuery;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
let key = `http:${method}:${path}`;
|
|
23
|
+
if (queryString) {
|
|
24
|
+
key += `?${queryString}`;
|
|
25
|
+
}
|
|
26
|
+
if (options?.headerWhitelist && options.headerWhitelist.length > 0 && req.headers) {
|
|
27
|
+
const sortedHeaders = [...options.headerWhitelist].sort();
|
|
28
|
+
for (const h of sortedHeaders) {
|
|
29
|
+
const normalizedHeader = h.toLowerCase();
|
|
30
|
+
const val = req.headers[normalizedHeader];
|
|
31
|
+
if (val !== void 0 && val !== null && val !== "") {
|
|
32
|
+
const headerStr = Array.isArray(val) ? val.join(",") : String(val);
|
|
33
|
+
key += `|h:${normalizedHeader}=${headerStr}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return key;
|
|
38
|
+
}
|
|
39
|
+
function shouldSkipCache(req, customSkip) {
|
|
40
|
+
if (customSkip && customSkip(req)) return true;
|
|
41
|
+
const cc = req.headers ? req.headers["cache-control"] || req.headers["Cache-Control"] : void 0;
|
|
42
|
+
if (typeof cc === "string") {
|
|
43
|
+
const lower = cc.toLowerCase();
|
|
44
|
+
if (lower.includes("no-cache") || lower.includes("no-store")) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
function generateETag(data) {
|
|
51
|
+
const str = typeof data === "string" ? data : Buffer.isBuffer(data) ? data.toString("utf8") : JSON.stringify(data);
|
|
52
|
+
const hash = crypto.createHash("sha1").update(str).digest("hex").slice(0, 16);
|
|
53
|
+
return `W/"${str.length.toString(16)}-${hash}"`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
buildDeterministicKey,
|
|
58
|
+
shouldSkipCache,
|
|
59
|
+
generateETag
|
|
60
|
+
};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// src/edge/hono.ts
|
|
2
|
+
async function computeEdgeETag(text) {
|
|
3
|
+
try {
|
|
4
|
+
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const data = encoder.encode(text);
|
|
7
|
+
const hashBuf = await crypto.subtle.digest("SHA-1", data);
|
|
8
|
+
const hashArr = Array.from(new Uint8Array(hashBuf));
|
|
9
|
+
const hashHex = hashArr.map((b) => b.toString(16).padStart(2, "0")).join("").slice(0, 16);
|
|
10
|
+
return `W/"${text.length.toString(16)}-${hashHex}"`;
|
|
11
|
+
}
|
|
12
|
+
} catch {
|
|
13
|
+
}
|
|
14
|
+
let h = 0;
|
|
15
|
+
for (let i = 0; i < text.length; i++) {
|
|
16
|
+
h = Math.imul(31, h) + text.charCodeAt(i) | 0;
|
|
17
|
+
}
|
|
18
|
+
return `W/"${text.length.toString(16)}-${Math.abs(h).toString(16)}"`;
|
|
19
|
+
}
|
|
20
|
+
function buildEdgeDeterministicKey(c, options) {
|
|
21
|
+
if (options?.keyGenerator) {
|
|
22
|
+
return options.keyGenerator(c);
|
|
23
|
+
}
|
|
24
|
+
const req = c.req;
|
|
25
|
+
const method = (req.method || "GET").toUpperCase();
|
|
26
|
+
const rawUrl = req.url || "/";
|
|
27
|
+
const qIdx = rawUrl.indexOf("?");
|
|
28
|
+
const path = qIdx >= 0 ? rawUrl.slice(0, qIdx) : rawUrl;
|
|
29
|
+
let queryString = "";
|
|
30
|
+
if (qIdx >= 0) {
|
|
31
|
+
const rawQuery = rawUrl.slice(qIdx + 1);
|
|
32
|
+
try {
|
|
33
|
+
const params = new URLSearchParams(rawQuery);
|
|
34
|
+
params.sort();
|
|
35
|
+
queryString = params.toString();
|
|
36
|
+
} catch {
|
|
37
|
+
queryString = rawQuery;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let key = `edge:http:${method}:${path}`;
|
|
41
|
+
if (queryString) {
|
|
42
|
+
key += `?${queryString}`;
|
|
43
|
+
}
|
|
44
|
+
if (options?.headerWhitelist && options.headerWhitelist.length > 0 && req.header) {
|
|
45
|
+
const sortedHeaders = [...options.headerWhitelist].sort();
|
|
46
|
+
for (const h of sortedHeaders) {
|
|
47
|
+
const val = req.header(h);
|
|
48
|
+
if (val !== void 0 && val !== null && val !== "") {
|
|
49
|
+
key += `|h:${h.toLowerCase()}=${val}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return key;
|
|
54
|
+
}
|
|
55
|
+
var NonCacheableEdgeResponseError = class extends Error {
|
|
56
|
+
constructor(response) {
|
|
57
|
+
super(`Non-cacheable status: ${response.status}`);
|
|
58
|
+
this.response = response;
|
|
59
|
+
this.name = "NonCacheableEdgeResponseError";
|
|
60
|
+
}
|
|
61
|
+
response;
|
|
62
|
+
isNonCacheable = true;
|
|
63
|
+
};
|
|
64
|
+
function honoEdgeCache(options = {}) {
|
|
65
|
+
const {
|
|
66
|
+
cache,
|
|
67
|
+
etag = true,
|
|
68
|
+
ttl = 300,
|
|
69
|
+
swr,
|
|
70
|
+
tags,
|
|
71
|
+
skipCache,
|
|
72
|
+
keyGenerator,
|
|
73
|
+
headerWhitelist
|
|
74
|
+
} = options;
|
|
75
|
+
return async (c, next) => {
|
|
76
|
+
const method = c.req.method;
|
|
77
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
78
|
+
return await next();
|
|
79
|
+
}
|
|
80
|
+
if (skipCache && skipCache(c)) {
|
|
81
|
+
return await next();
|
|
82
|
+
}
|
|
83
|
+
const cc = c.req.header?.("cache-control") || c.req.header?.("Cache-Control");
|
|
84
|
+
if (typeof cc === "string") {
|
|
85
|
+
const lower = cc.toLowerCase();
|
|
86
|
+
if (lower.includes("no-cache") || lower.includes("no-store")) {
|
|
87
|
+
return await next();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!cache) {
|
|
91
|
+
return await next();
|
|
92
|
+
}
|
|
93
|
+
const key = buildEdgeDeterministicKey(c, { keyGenerator, headerWhitelist });
|
|
94
|
+
const ifNoneMatch = c.req.header?.("if-none-match") || c.req.header?.("If-None-Match");
|
|
95
|
+
const resolvedTags = typeof tags === "function" ? tags(c) : tags;
|
|
96
|
+
let ranNext = false;
|
|
97
|
+
try {
|
|
98
|
+
const cached = await cache.get(
|
|
99
|
+
key,
|
|
100
|
+
async () => {
|
|
101
|
+
ranNext = true;
|
|
102
|
+
await next();
|
|
103
|
+
const res = c.res;
|
|
104
|
+
if (!res) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const contentType = res.headers?.get?.("content-type") || "text/plain; charset=utf-8";
|
|
108
|
+
if (contentType.toLowerCase().includes("text/event-stream")) {
|
|
109
|
+
throw new NonCacheableEdgeResponseError({
|
|
110
|
+
body: "",
|
|
111
|
+
contentType,
|
|
112
|
+
status: 0
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const cc2 = res.headers?.get?.("cache-control");
|
|
116
|
+
if (cc2) {
|
|
117
|
+
const lowerCc = cc2.toLowerCase();
|
|
118
|
+
if (lowerCc.includes("no-store") || lowerCc.includes("no-cache") || lowerCc.includes("private")) {
|
|
119
|
+
throw new NonCacheableEdgeResponseError({
|
|
120
|
+
body: "",
|
|
121
|
+
contentType,
|
|
122
|
+
status: 0
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const statusCode = res.status ?? 200;
|
|
127
|
+
if (statusCode < 200 || statusCode >= 300 || statusCode === 206) {
|
|
128
|
+
throw new NonCacheableEdgeResponseError({
|
|
129
|
+
body: "",
|
|
130
|
+
contentType,
|
|
131
|
+
status: statusCode
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const clone = res.clone();
|
|
135
|
+
const text = await clone.text();
|
|
136
|
+
const bodyEtag = etag ? await computeEdgeETag(text) : void 0;
|
|
137
|
+
const capturedHeaders = {};
|
|
138
|
+
if (res.headers && typeof res.headers.forEach === "function") {
|
|
139
|
+
res.headers.forEach((value, name) => {
|
|
140
|
+
const lower = name.toLowerCase();
|
|
141
|
+
if (lower !== "content-length" && lower !== "transfer-encoding" && lower !== "connection" && lower !== "etag" && lower !== "content-type") {
|
|
142
|
+
capturedHeaders[name] = value;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const snapshot = {
|
|
147
|
+
body: text,
|
|
148
|
+
contentType,
|
|
149
|
+
headers: capturedHeaders,
|
|
150
|
+
etag: bodyEtag,
|
|
151
|
+
status: statusCode
|
|
152
|
+
};
|
|
153
|
+
return snapshot;
|
|
154
|
+
},
|
|
155
|
+
ttl,
|
|
156
|
+
{
|
|
157
|
+
swr,
|
|
158
|
+
tags: resolvedTags,
|
|
159
|
+
ctx: c.executionCtx
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
if (!cached) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (cached.etag && ifNoneMatch === cached.etag) {
|
|
166
|
+
const notModifiedHeaders = { ETag: cached.etag };
|
|
167
|
+
if (cached.headers?.["cache-control"]) notModifiedHeaders["Cache-Control"] = cached.headers["cache-control"];
|
|
168
|
+
if (cached.headers?.["vary"]) notModifiedHeaders["Vary"] = cached.headers["vary"];
|
|
169
|
+
return applyEdgeResponse(c, null, 304, notModifiedHeaders);
|
|
170
|
+
}
|
|
171
|
+
const headers = { ...cached.headers };
|
|
172
|
+
if (cached.etag) headers["ETag"] = cached.etag;
|
|
173
|
+
if (cached.contentType) headers["Content-Type"] = cached.contentType;
|
|
174
|
+
return applyEdgeResponse(c, cached.body, cached.status ?? 200, headers);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
if (err instanceof NonCacheableEdgeResponseError) {
|
|
177
|
+
if (!ranNext) {
|
|
178
|
+
return await next();
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function applyEdgeResponse(c, body, status, headers) {
|
|
187
|
+
const response = c.body(body, status, headers);
|
|
188
|
+
c.res = response;
|
|
189
|
+
return response;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export {
|
|
193
|
+
computeEdgeETag,
|
|
194
|
+
buildEdgeDeterministicKey,
|
|
195
|
+
honoEdgeCache
|
|
196
|
+
};
|
|
@@ -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
|
-
|
|
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() ??
|
|
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 = "
|
|
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,
|