tricache 0.7.1 → 0.9.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/LICENSE +21 -21
- package/README.md +89 -1371
- package/dashboards/prometheus-scrape.yml +18 -0
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-DVvroyqA.d.ts +886 -0
- package/dist/cache-service-KI5ACF3F.js +12 -0
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/chunk-BQRURGGD.js +60 -0
- package/dist/chunk-IITRFWHY.js +196 -0
- package/dist/{chunk-X5AXHPVD.js → chunk-LJ5QJB5O.js} +90 -5
- package/dist/{chunk-EUGYR7KR.js → chunk-PL5J3ZNT.js} +3184 -514
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/cli.cjs +3122 -495
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +121 -5
- package/dist/dashboard/index.cjs +1065 -0
- package/dist/dashboard/index.d.ts +183 -0
- package/dist/dashboard/index.js +22 -0
- package/dist/drizzle/index.cjs +2846 -494
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1559 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono/index.cjs +7760 -0
- package/dist/hono/index.d.ts +91 -0
- package/dist/hono/index.js +185 -0
- package/dist/hono-VA9A1m0I.d.ts +371 -0
- package/dist/http/index.cjs +3281 -587
- package/dist/http/index.d.ts +85 -23
- package/dist/http/index.js +176 -78
- package/dist/index.cjs +7212 -2505
- package/dist/index.d.ts +547 -30
- package/dist/index.js +104 -5
- package/dist/murmur3-CMGw9PbV.d.ts +98 -0
- package/dist/nestjs/index.cjs +2787 -487
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2804 -501
- package/dist/next/index.d.ts +4 -2
- package/dist/next/index.js +14 -10
- package/dist/prisma/index.d.ts +3 -1
- package/dist/serialize-worker.cjs +5 -4
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-D8tIXmzb.d.ts → types-B_QkVwJ7.d.ts} +622 -662
- package/dist/utils-CEsU_Z0s.d.ts +27 -0
- package/package.json +48 -10
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +1619 -415
- package/src/cli.ts +137 -2
- package/src/codec.ts +72 -0
- package/src/cross-region.ts +245 -0
- package/src/dashboard/handler.ts +392 -0
- package/src/dashboard/html.ts +594 -0
- package/src/dashboard/index.ts +195 -0
- package/src/dashboard/types.ts +96 -0
- package/src/disk-tier.ts +449 -47
- package/src/drizzle/index.ts +86 -86
- package/src/edge/adapters/cloudflare-do.ts +130 -0
- package/src/edge/adapters/cloudflare-kv.ts +115 -0
- package/src/edge/adapters/upstash.ts +143 -0
- package/src/edge/cache.ts +797 -0
- package/src/edge/crypto.ts +182 -0
- package/src/edge/hono.ts +284 -0
- package/src/edge/index.ts +37 -0
- package/src/edge/types.ts +230 -0
- package/src/edge/utils/base64.ts +63 -0
- package/src/edge/utils/murmur3.ts +179 -0
- package/src/encryption.ts +127 -5
- package/src/hono/index.ts +316 -0
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +44 -258
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +509 -0
- package/src/latency-watchdog.ts +282 -0
- package/src/nestjs/decorators.ts +161 -161
- package/src/nestjs/index.ts +23 -23
- package/src/nestjs/tricache.module.ts +104 -104
- package/src/nestjs/tricache.store.ts +97 -97
- package/src/nestjs/types.ts +79 -79
- package/src/next/cache-handler.ts +12 -9
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- package/src/remote-snapshot.ts +153 -0
- package/src/serialize-worker.ts +120 -120
- package/src/sigv4-snapshot-adapter.ts +260 -0
- package/src/smart-memory-cache.ts +91 -8
- package/src/types.ts +315 -2
- package/src/utils/cgroup.ts +100 -0
- package/src/utils/tracing.ts +54 -0
- package/src/wasm/bloom-filter-wasm.ts +109 -102
- package/src/worker-pool.ts +299 -299
- package/src/wtiny-lfu.ts +624 -0
- package/dist/cache-service-5GATUYAK.js +0 -7
- package/dist/chunk-UBSR5UYA.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
|
+
};
|
|
@@ -15,7 +15,16 @@ var CachePriority = /* @__PURE__ */ ((CachePriority2) => {
|
|
|
15
15
|
})(CachePriority || {});
|
|
16
16
|
|
|
17
17
|
// src/encryption.ts
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
createCipheriv,
|
|
20
|
+
createDecipheriv,
|
|
21
|
+
createSecretKey,
|
|
22
|
+
randomFillSync,
|
|
23
|
+
constants,
|
|
24
|
+
publicEncrypt,
|
|
25
|
+
privateDecrypt,
|
|
26
|
+
randomBytes
|
|
27
|
+
} from "crypto";
|
|
19
28
|
var IV_BYTES = 12;
|
|
20
29
|
var TAG_BYTES = 16;
|
|
21
30
|
var PREFIX_256 = "enc:v1:";
|
|
@@ -132,17 +141,17 @@ var CacheEncryption = class {
|
|
|
132
141
|
throw new Error("XOR key must be at least 1 byte");
|
|
133
142
|
}
|
|
134
143
|
this._key = buf;
|
|
135
|
-
if (
|
|
144
|
+
if (resolvedMode !== "xor") {
|
|
136
145
|
this._keyObj = createSecretKey(buf);
|
|
137
146
|
const _wc = createCipheriv(
|
|
138
|
-
|
|
147
|
+
resolvedMode,
|
|
139
148
|
this._keyObj,
|
|
140
|
-
Buffer.alloc(
|
|
149
|
+
Buffer.alloc(resolvedMode === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
141
150
|
);
|
|
142
151
|
_wc.update(Buffer.alloc(0));
|
|
143
152
|
_wc.final();
|
|
144
153
|
}
|
|
145
|
-
if (
|
|
154
|
+
if (resolvedMode === "xor") {
|
|
146
155
|
logger.warn(
|
|
147
156
|
"Cache obfuscation enabled (XOR). WARNING: XOR is NOT cryptographic \u2014 use only for dev or non-sensitive data."
|
|
148
157
|
);
|
|
@@ -410,6 +419,81 @@ var CacheEncryption = class {
|
|
|
410
419
|
return plain;
|
|
411
420
|
}
|
|
412
421
|
};
|
|
422
|
+
var MAGIC_ENVELOPE = Buffer.from([84, 82, 73, 67, 69, 78, 86, 49]);
|
|
423
|
+
var EnvelopeEncryption = class {
|
|
424
|
+
static isEnvelope(data) {
|
|
425
|
+
return data.length >= MAGIC_LEN && data.subarray(0, MAGIC_LEN).equals(MAGIC_ENVELOPE);
|
|
426
|
+
}
|
|
427
|
+
static async encrypt(data, options) {
|
|
428
|
+
const dek = randomBytes(32);
|
|
429
|
+
let encryptedKey;
|
|
430
|
+
if (options.kmsEncrypt) {
|
|
431
|
+
encryptedKey = await options.kmsEncrypt(dek);
|
|
432
|
+
} else if (options.publicKey) {
|
|
433
|
+
encryptedKey = publicEncrypt(
|
|
434
|
+
{
|
|
435
|
+
key: options.publicKey,
|
|
436
|
+
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
|
437
|
+
oaepHash: "sha256"
|
|
438
|
+
},
|
|
439
|
+
dek
|
|
440
|
+
);
|
|
441
|
+
} else {
|
|
442
|
+
throw new Error("EnvelopeEncryption: either publicKey or kmsEncrypt must be provided to encrypt");
|
|
443
|
+
}
|
|
444
|
+
const iv = randomBytes(IV_BYTES);
|
|
445
|
+
const cipher = createCipheriv("aes-256-gcm", dek, iv);
|
|
446
|
+
const ct = cipher.update(data);
|
|
447
|
+
cipher.final();
|
|
448
|
+
const tag = cipher.getAuthTag();
|
|
449
|
+
const keyLen = encryptedKey.length;
|
|
450
|
+
const out = Buffer.allocUnsafe(MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES + ct.length);
|
|
451
|
+
MAGIC_ENVELOPE.copy(out, 0);
|
|
452
|
+
out.writeUInt32BE(keyLen, MAGIC_LEN);
|
|
453
|
+
encryptedKey.copy(out, MAGIC_LEN + 4);
|
|
454
|
+
iv.copy(out, MAGIC_LEN + 4 + keyLen);
|
|
455
|
+
tag.copy(out, MAGIC_LEN + 4 + keyLen + IV_BYTES);
|
|
456
|
+
ct.copy(out, MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES);
|
|
457
|
+
return out;
|
|
458
|
+
}
|
|
459
|
+
static async decrypt(data, options) {
|
|
460
|
+
if (!this.isEnvelope(data)) {
|
|
461
|
+
throw new Error("EnvelopeEncryption: invalid header, expected TRICENV1");
|
|
462
|
+
}
|
|
463
|
+
if (data.length < MAGIC_LEN + 4 + IV_BYTES + TAG_BYTES) {
|
|
464
|
+
throw new Error("EnvelopeEncryption: truncated envelope buffer");
|
|
465
|
+
}
|
|
466
|
+
const keyLen = data.readUInt32BE(MAGIC_LEN);
|
|
467
|
+
const minExpected = MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES;
|
|
468
|
+
if (data.length < minExpected) {
|
|
469
|
+
throw new Error("EnvelopeEncryption: truncated envelope buffer (key length mismatch)");
|
|
470
|
+
}
|
|
471
|
+
const encryptedKey = data.subarray(MAGIC_LEN + 4, MAGIC_LEN + 4 + keyLen);
|
|
472
|
+
const iv = data.subarray(MAGIC_LEN + 4 + keyLen, MAGIC_LEN + 4 + keyLen + IV_BYTES);
|
|
473
|
+
const tag = data.subarray(MAGIC_LEN + 4 + keyLen + IV_BYTES, MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES);
|
|
474
|
+
const ct = data.subarray(MAGIC_LEN + 4 + keyLen + IV_BYTES + TAG_BYTES);
|
|
475
|
+
let dek;
|
|
476
|
+
if (options.kmsDecrypt) {
|
|
477
|
+
dek = await options.kmsDecrypt(encryptedKey);
|
|
478
|
+
} else if (options.privateKey) {
|
|
479
|
+
dek = privateDecrypt(
|
|
480
|
+
{
|
|
481
|
+
key: options.privateKey,
|
|
482
|
+
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
|
483
|
+
oaepHash: "sha256"
|
|
484
|
+
},
|
|
485
|
+
encryptedKey
|
|
486
|
+
);
|
|
487
|
+
} else {
|
|
488
|
+
throw new Error("EnvelopeEncryption: either privateKey or kmsDecrypt must be provided to decrypt");
|
|
489
|
+
}
|
|
490
|
+
const decipher = createDecipheriv("aes-256-gcm", dek, iv);
|
|
491
|
+
decipher.setAuthTag(tag);
|
|
492
|
+
const pt = decipher.update(ct);
|
|
493
|
+
decipher.final();
|
|
494
|
+
return pt;
|
|
495
|
+
}
|
|
496
|
+
};
|
|
413
497
|
|
|
414
498
|
// src/compression.ts
|
|
415
499
|
import zlib from "zlib";
|
|
@@ -470,6 +554,7 @@ export {
|
|
|
470
554
|
consoleLogger,
|
|
471
555
|
CachePriority,
|
|
472
556
|
CacheEncryption,
|
|
557
|
+
EnvelopeEncryption,
|
|
473
558
|
PREFIX_COMPRESSED,
|
|
474
559
|
PREFIX_ENC_COMPRESSED,
|
|
475
560
|
compressBuffer,
|