tricache 0.6.7 → 0.7.1
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 +1 -1
- package/README.md +1428 -970
- package/bin/tricache.js +8 -0
- package/dist/cache-service-5GATUYAK.js +7 -0
- package/dist/cache-service-D8tIXmzb.d.ts +1498 -0
- package/dist/chunk-EUGYR7KR.js +4629 -0
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-X5AXHPVD.js} +139 -6
- package/dist/cli.cjs +5238 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5218 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5374 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +200 -0
- package/dist/index.cjs +1245 -209
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3705
- package/dist/nestjs/index.cjs +5362 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5407 -0
- package/dist/next/index.d.ts +208 -0
- package/dist/next/index.js +297 -0
- package/dist/prisma/index.cjs +123 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +87 -0
- package/dist/serialize-worker.cjs +190 -15
- package/dist/serialize-worker.js +63 -12
- package/package.json +71 -21
- package/src/cache-service.ts +3294 -2429
- package/src/cli.ts +143 -0
- package/src/compression.ts +56 -0
- package/src/disk-tier.ts +821 -757
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +513 -442
- package/src/http/index.ts +265 -0
- package/src/index.ts +7 -0
- package/src/nestjs/decorators.ts +161 -0
- package/src/nestjs/index.ts +23 -0
- package/src/nestjs/tricache.module.ts +104 -0
- package/src/nestjs/tricache.store.ts +97 -0
- package/src/nestjs/types.ts +79 -0
- package/src/next/cache-handler.ts +268 -0
- package/src/next/index.ts +37 -0
- package/src/next/isr-handler.ts +105 -0
- package/src/next/types.ts +130 -0
- package/src/prisma/index.ts +154 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +1026 -1010
- package/src/types.ts +911 -732
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- package/src/worker-pool.ts +156 -42
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/prisma/index.ts
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
var READ_OPERATIONS = /* @__PURE__ */ new Set([
|
|
4
|
+
"findUnique",
|
|
5
|
+
"findUniqueOrThrow",
|
|
6
|
+
"findFirst",
|
|
7
|
+
"findFirstOrThrow",
|
|
8
|
+
"findMany",
|
|
9
|
+
"count",
|
|
10
|
+
"aggregate",
|
|
11
|
+
"groupBy"
|
|
12
|
+
]);
|
|
13
|
+
var MUTATION_OPERATIONS = /* @__PURE__ */ new Set([
|
|
14
|
+
"create",
|
|
15
|
+
"createMany",
|
|
16
|
+
"createManyAndReturn",
|
|
17
|
+
"update",
|
|
18
|
+
"updateMany",
|
|
19
|
+
"updateManyAndReturn",
|
|
20
|
+
"upsert",
|
|
21
|
+
"delete",
|
|
22
|
+
"deleteMany"
|
|
23
|
+
]);
|
|
24
|
+
function deterministicStringify(val) {
|
|
25
|
+
if (val === null || typeof val !== "object") {
|
|
26
|
+
return JSON.stringify(val);
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(val)) {
|
|
29
|
+
return `[${val.map(deterministicStringify).join(",")}]`;
|
|
30
|
+
}
|
|
31
|
+
const obj = val;
|
|
32
|
+
const keys = Object.keys(obj).sort();
|
|
33
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${deterministicStringify(obj[k])}`).join(",")}}`;
|
|
34
|
+
}
|
|
35
|
+
function generatePrismaCacheKey(model, operation, args) {
|
|
36
|
+
const normalized = deterministicStringify({ model, operation, args });
|
|
37
|
+
const hash = crypto.createHash("sha256").update(normalized, "utf8").digest("hex").slice(0, 32);
|
|
38
|
+
return `prisma:${model.toLowerCase()}:${operation}:${hash}`;
|
|
39
|
+
}
|
|
40
|
+
function withTriCache(options) {
|
|
41
|
+
const { cache, defaultTtl = 300, autoInvalidate = true, autoCache = false } = options;
|
|
42
|
+
return {
|
|
43
|
+
name: "tricache-prisma-extension",
|
|
44
|
+
query: {
|
|
45
|
+
$allModels: {
|
|
46
|
+
async $allOperations({ model, operation, args, query }) {
|
|
47
|
+
const modelTag = model.toLowerCase();
|
|
48
|
+
if (autoInvalidate && MUTATION_OPERATIONS.has(operation)) {
|
|
49
|
+
const { cache: _cacheOpt, ...cleanMutationArgs } = args ?? {};
|
|
50
|
+
const result = await query(cleanMutationArgs);
|
|
51
|
+
await cache.invalidateTag(modelTag);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
if (READ_OPERATIONS.has(operation)) {
|
|
55
|
+
const queryArgs = args ?? {};
|
|
56
|
+
const cacheConfig = queryArgs.cache;
|
|
57
|
+
if (cacheConfig !== void 0 || autoCache) {
|
|
58
|
+
const opts = typeof cacheConfig === "object" && cacheConfig !== null ? cacheConfig : {};
|
|
59
|
+
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
60
|
+
const key = opts.key ?? generatePrismaCacheKey(model, operation, cleanArgs);
|
|
61
|
+
const tags = [.../* @__PURE__ */ new Set([modelTag, ...opts.tags ?? []])];
|
|
62
|
+
return cache.wrap(
|
|
63
|
+
key,
|
|
64
|
+
() => query(cleanArgs),
|
|
65
|
+
{
|
|
66
|
+
ttl: opts.ttl ?? defaultTtl,
|
|
67
|
+
swr: opts.swr,
|
|
68
|
+
priority: opts.priority,
|
|
69
|
+
tags,
|
|
70
|
+
dependsOn: opts.dependsOn,
|
|
71
|
+
refreshAhead: opts.refreshAhead,
|
|
72
|
+
xfetchBeta: opts.xfetchBeta,
|
|
73
|
+
notFoundTtl: opts.notFoundTtl
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return query(args);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
generatePrismaCacheKey,
|
|
86
|
+
withTriCache
|
|
87
|
+
};
|
|
@@ -1,10 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
2
24
|
|
|
3
25
|
// src/serialize-worker.ts
|
|
4
26
|
var import_worker_threads = require("worker_threads");
|
|
5
27
|
|
|
6
28
|
// src/encryption.ts
|
|
7
29
|
var import_crypto = require("crypto");
|
|
30
|
+
|
|
31
|
+
// src/types.ts
|
|
32
|
+
var consoleLogger = {
|
|
33
|
+
debug: () => {
|
|
34
|
+
},
|
|
35
|
+
info: (msg, meta) => console.info("[tricache]", msg, meta ?? ""),
|
|
36
|
+
warn: (msg, meta) => console.warn("[tricache]", msg, meta ?? ""),
|
|
37
|
+
error: (msg, meta, err) => console.error("[tricache]", msg, meta ?? "", err ?? "")
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/encryption.ts
|
|
8
41
|
var IV_BYTES = 12;
|
|
9
42
|
var TAG_BYTES = 16;
|
|
10
43
|
var PREFIX_256 = "enc:v1:";
|
|
@@ -24,6 +57,8 @@ var CacheEncryption = class {
|
|
|
24
57
|
_keyObj = null;
|
|
25
58
|
// parsed KeyObject — used for all AES modes
|
|
26
59
|
_mode;
|
|
60
|
+
/** Fail-closed on invalid key material when opted in via constructor opts. */
|
|
61
|
+
_strictKeyValidation;
|
|
27
62
|
_prevKey = null;
|
|
28
63
|
_prevKeyObj = null;
|
|
29
64
|
_prevMode = "aes-256-gcm";
|
|
@@ -89,9 +124,12 @@ var CacheEncryption = class {
|
|
|
89
124
|
}
|
|
90
125
|
return out;
|
|
91
126
|
}
|
|
92
|
-
constructor(keyBase642,
|
|
93
|
-
|
|
94
|
-
|
|
127
|
+
constructor(keyBase642, loggerOrMode = consoleLogger, mode2 = "aes-256-gcm", previousKeyBase64, previousMode, opts) {
|
|
128
|
+
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
129
|
+
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode2;
|
|
130
|
+
this._strictKeyValidation = opts?.strictKeyValidation ?? false;
|
|
131
|
+
this._mode = resolvedMode;
|
|
132
|
+
if (keyBase642 === void 0 || keyBase642 === null) {
|
|
95
133
|
if (process.env.NODE_ENV === "production") {
|
|
96
134
|
logger.warn(
|
|
97
135
|
"SECURITY: encryption key not set \u2014 cache data stored unencrypted at rest",
|
|
@@ -100,11 +138,17 @@ var CacheEncryption = class {
|
|
|
100
138
|
}
|
|
101
139
|
return;
|
|
102
140
|
}
|
|
141
|
+
if (keyBase642 === "") {
|
|
142
|
+
const err = new Error("Encryption key is empty \u2014 set encryptionKey option or CACHE_ENCRYPTION_KEY");
|
|
143
|
+
if (this._strictKeyValidation) throw err;
|
|
144
|
+
logger.error("Empty encryption key \u2014 falling back to plaintext", {}, err);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
103
147
|
try {
|
|
104
148
|
const buf = Buffer.from(keyBase642, "base64");
|
|
105
|
-
const requiredLen =
|
|
149
|
+
const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
|
|
106
150
|
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
107
|
-
throw new Error(`${
|
|
151
|
+
throw new Error(`${resolvedMode} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
108
152
|
}
|
|
109
153
|
if (buf.length < 1) {
|
|
110
154
|
throw new Error("XOR key must be at least 1 byte");
|
|
@@ -128,6 +172,9 @@ var CacheEncryption = class {
|
|
|
128
172
|
logger.debug(`Cache encryption enabled (${mode2.toUpperCase()})`);
|
|
129
173
|
}
|
|
130
174
|
} catch (err) {
|
|
175
|
+
if (this._strictKeyValidation) {
|
|
176
|
+
throw err;
|
|
177
|
+
}
|
|
131
178
|
logger.error("Invalid encryption key \u2014 falling back to plaintext", {}, err);
|
|
132
179
|
}
|
|
133
180
|
if (previousKeyBase64) {
|
|
@@ -153,6 +200,9 @@ var CacheEncryption = class {
|
|
|
153
200
|
get isEnabled() {
|
|
154
201
|
return this._key !== null;
|
|
155
202
|
}
|
|
203
|
+
get mode() {
|
|
204
|
+
return this._mode;
|
|
205
|
+
}
|
|
156
206
|
/**
|
|
157
207
|
* Snapshot the key material needed to spin up an off-main-thread worker pool.
|
|
158
208
|
* Replaces the previously-used `as unknown as { _key, _mode, … }` private-field
|
|
@@ -167,6 +217,43 @@ var CacheEncryption = class {
|
|
|
167
217
|
prevMode: this._prevMode
|
|
168
218
|
};
|
|
169
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Rotate the active encryption key dynamically at runtime.
|
|
222
|
+
* Promotes the current primary key to previousEncryptionKey for zero-downtime
|
|
223
|
+
* fallback decryption of existing cache entries in L2/Disk, and installs
|
|
224
|
+
* the new key as primary for all subsequent writes.
|
|
225
|
+
*/
|
|
226
|
+
rotateKey(newKeyBase64, newMode) {
|
|
227
|
+
if (!newKeyBase64) {
|
|
228
|
+
throw new Error("rotateKey requires a valid non-empty base64 key");
|
|
229
|
+
}
|
|
230
|
+
const mode2 = newMode ?? this._mode;
|
|
231
|
+
const buf = Buffer.from(newKeyBase64, "base64");
|
|
232
|
+
const requiredLen = mode2 === "aes-256-gcm" ? 32 : mode2 === "aes-128-gcm" || mode2 === "aes-128-ctr" ? 16 : 0;
|
|
233
|
+
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
234
|
+
throw new Error(`${mode2} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
235
|
+
}
|
|
236
|
+
if (buf.length < 1) {
|
|
237
|
+
throw new Error("XOR key must be at least 1 byte");
|
|
238
|
+
}
|
|
239
|
+
let newKeyObj = null;
|
|
240
|
+
if (mode2 !== "xor") {
|
|
241
|
+
newKeyObj = (0, import_crypto.createSecretKey)(buf);
|
|
242
|
+
const _wc = (0, import_crypto.createCipheriv)(
|
|
243
|
+
mode2,
|
|
244
|
+
newKeyObj,
|
|
245
|
+
Buffer.alloc(mode2 === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
246
|
+
);
|
|
247
|
+
_wc.update(Buffer.alloc(0));
|
|
248
|
+
_wc.final();
|
|
249
|
+
}
|
|
250
|
+
this._prevKey = this._key;
|
|
251
|
+
this._prevKeyObj = this._keyObj;
|
|
252
|
+
this._prevMode = this._mode;
|
|
253
|
+
this._key = buf;
|
|
254
|
+
this._keyObj = newKeyObj;
|
|
255
|
+
this._mode = mode2;
|
|
256
|
+
}
|
|
170
257
|
// ── String (Redis) ────────────────────────────────────────────────────────
|
|
171
258
|
/**
|
|
172
259
|
* Encrypt a string for Redis storage.
|
|
@@ -346,6 +433,47 @@ var CacheEncryption = class {
|
|
|
346
433
|
}
|
|
347
434
|
};
|
|
348
435
|
|
|
436
|
+
// src/compression.ts
|
|
437
|
+
var import_zlib = __toESM(require("zlib"), 1);
|
|
438
|
+
var PREFIX_COMPRESSED = "cmp:v1:";
|
|
439
|
+
var PREFIX_ENC_COMPRESSED = "ecp:v1:";
|
|
440
|
+
var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
|
|
441
|
+
function compressBuffer(buf, algorithm) {
|
|
442
|
+
if (algorithm === "brotli") {
|
|
443
|
+
return import_zlib.default.brotliCompressSync(buf, {
|
|
444
|
+
params: {
|
|
445
|
+
[import_zlib.default.constants.BROTLI_PARAM_QUALITY]: 4
|
|
446
|
+
// Fast level 4 compression
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
if (algorithm === "gzip") {
|
|
451
|
+
return import_zlib.default.gzipSync(buf, { level: 6 });
|
|
452
|
+
}
|
|
453
|
+
return buf;
|
|
454
|
+
}
|
|
455
|
+
function decompressBuffer(buf, algorithm = "brotli") {
|
|
456
|
+
const tryBrotli = () => {
|
|
457
|
+
try {
|
|
458
|
+
return import_zlib.default.brotliDecompressSync(buf);
|
|
459
|
+
} catch {
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
const tryGzip = () => {
|
|
464
|
+
try {
|
|
465
|
+
return import_zlib.default.gunzipSync(buf);
|
|
466
|
+
} catch {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
const result = algorithm === "gzip" ? tryGzip() ?? tryBrotli() : tryBrotli() ?? tryGzip();
|
|
471
|
+
if (result === null) {
|
|
472
|
+
throw new Error(`tricache: corrupt compressed payload (${buf.length} bytes, algorithm=${algorithm})`);
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
476
|
+
|
|
349
477
|
// src/serialize-worker.ts
|
|
350
478
|
var silentLogger = {
|
|
351
479
|
debug: () => {
|
|
@@ -357,7 +485,7 @@ var silentLogger = {
|
|
|
357
485
|
error: () => {
|
|
358
486
|
}
|
|
359
487
|
};
|
|
360
|
-
var { keyBase64, mode, prevKeyBase64, prevMode } = import_worker_threads.workerData;
|
|
488
|
+
var { keyBase64, mode, prevKeyBase64, prevMode, compression, compressionThresholdBytes } = import_worker_threads.workerData;
|
|
361
489
|
var enc = new CacheEncryption(
|
|
362
490
|
keyBase64 || void 0,
|
|
363
491
|
silentLogger,
|
|
@@ -365,14 +493,61 @@ var enc = new CacheEncryption(
|
|
|
365
493
|
prevKeyBase64 || void 0,
|
|
366
494
|
prevMode
|
|
367
495
|
);
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
496
|
+
var cmpAlgo = compression ?? "none";
|
|
497
|
+
var cmpThreshold = compressionThresholdBytes ?? 1024;
|
|
498
|
+
import_worker_threads.parentPort.on(
|
|
499
|
+
"message",
|
|
500
|
+
({ id, type, payload }) => {
|
|
501
|
+
try {
|
|
502
|
+
const textPayload = typeof payload === "string" ? payload : Buffer.from(payload.buffer, payload.byteOffset, payload.byteLength).toString("utf8");
|
|
503
|
+
if (type === "encrypt") {
|
|
504
|
+
let result;
|
|
505
|
+
if (cmpAlgo !== "none" && textPayload.length > cmpThreshold) {
|
|
506
|
+
const compressed = compressBuffer(Buffer.from(textPayload, "utf8"), cmpAlgo);
|
|
507
|
+
if (enc.isEnabled) {
|
|
508
|
+
const encrypted = enc.encryptBuffer(compressed);
|
|
509
|
+
result = PREFIX_ENC_COMPRESSED + encrypted.toString("base64");
|
|
510
|
+
} else {
|
|
511
|
+
result = PREFIX_COMPRESSED + compressed.toString("base64");
|
|
512
|
+
}
|
|
513
|
+
} else {
|
|
514
|
+
result = enc.encrypt(textPayload);
|
|
515
|
+
}
|
|
516
|
+
if (result.length >= 131072) {
|
|
517
|
+
const outBuf = Buffer.from(result, "utf8");
|
|
518
|
+
const transferable = new ArrayBuffer(outBuf.byteLength);
|
|
519
|
+
new Uint8Array(transferable).set(outBuf);
|
|
520
|
+
const outputUint8 = new Uint8Array(transferable);
|
|
521
|
+
import_worker_threads.parentPort.postMessage({ id, result: outputUint8, isTransfer: true }, [transferable]);
|
|
522
|
+
} else {
|
|
523
|
+
import_worker_threads.parentPort.postMessage({ id, result });
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
let result;
|
|
527
|
+
if (textPayload.startsWith(PREFIX_ENC_COMPRESSED)) {
|
|
528
|
+
const rawBuf = Buffer.from(textPayload.slice(PREFIX_ENC_COMPRESSED.length), "base64");
|
|
529
|
+
const decryptedBuf = enc.decryptBuffer(rawBuf);
|
|
530
|
+
const decompressed = decompressBuffer(decryptedBuf, cmpAlgo !== "none" ? cmpAlgo : "brotli");
|
|
531
|
+
result = decompressed.toString("utf8");
|
|
532
|
+
} else if (textPayload.startsWith(PREFIX_COMPRESSED)) {
|
|
533
|
+
const rawBuf = Buffer.from(textPayload.slice(PREFIX_COMPRESSED.length), "base64");
|
|
534
|
+
const decompressed = decompressBuffer(rawBuf, cmpAlgo !== "none" ? cmpAlgo : "brotli");
|
|
535
|
+
result = decompressed.toString("utf8");
|
|
536
|
+
} else {
|
|
537
|
+
result = enc.decrypt(textPayload);
|
|
538
|
+
}
|
|
539
|
+
if (result.length >= 131072) {
|
|
540
|
+
const outBuf = Buffer.from(result, "utf8");
|
|
541
|
+
const transferable = new ArrayBuffer(outBuf.byteLength);
|
|
542
|
+
new Uint8Array(transferable).set(outBuf);
|
|
543
|
+
const outputUint8 = new Uint8Array(transferable);
|
|
544
|
+
import_worker_threads.parentPort.postMessage({ id, result: outputUint8, isTransfer: true }, [transferable]);
|
|
545
|
+
} else {
|
|
546
|
+
import_worker_threads.parentPort.postMessage({ id, result });
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
} catch (e) {
|
|
550
|
+
import_worker_threads.parentPort.postMessage({ id, error: e.message });
|
|
374
551
|
}
|
|
375
|
-
} catch (e) {
|
|
376
|
-
import_worker_threads.parentPort.postMessage({ id, error: e.message });
|
|
377
552
|
}
|
|
378
|
-
|
|
553
|
+
);
|
package/dist/serialize-worker.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
CacheEncryption
|
|
3
|
-
|
|
2
|
+
CacheEncryption,
|
|
3
|
+
PREFIX_COMPRESSED,
|
|
4
|
+
PREFIX_ENC_COMPRESSED,
|
|
5
|
+
compressBuffer,
|
|
6
|
+
decompressBuffer
|
|
7
|
+
} from "./chunk-X5AXHPVD.js";
|
|
4
8
|
|
|
5
9
|
// src/serialize-worker.ts
|
|
6
10
|
import { parentPort, workerData } from "worker_threads";
|
|
@@ -14,7 +18,7 @@ var silentLogger = {
|
|
|
14
18
|
error: () => {
|
|
15
19
|
}
|
|
16
20
|
};
|
|
17
|
-
var { keyBase64, mode, prevKeyBase64, prevMode } = workerData;
|
|
21
|
+
var { keyBase64, mode, prevKeyBase64, prevMode, compression, compressionThresholdBytes } = workerData;
|
|
18
22
|
var enc = new CacheEncryption(
|
|
19
23
|
keyBase64 || void 0,
|
|
20
24
|
silentLogger,
|
|
@@ -22,14 +26,61 @@ var enc = new CacheEncryption(
|
|
|
22
26
|
prevKeyBase64 || void 0,
|
|
23
27
|
prevMode
|
|
24
28
|
);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
var cmpAlgo = compression ?? "none";
|
|
30
|
+
var cmpThreshold = compressionThresholdBytes ?? 1024;
|
|
31
|
+
parentPort.on(
|
|
32
|
+
"message",
|
|
33
|
+
({ id, type, payload }) => {
|
|
34
|
+
try {
|
|
35
|
+
const textPayload = typeof payload === "string" ? payload : Buffer.from(payload.buffer, payload.byteOffset, payload.byteLength).toString("utf8");
|
|
36
|
+
if (type === "encrypt") {
|
|
37
|
+
let result;
|
|
38
|
+
if (cmpAlgo !== "none" && textPayload.length > cmpThreshold) {
|
|
39
|
+
const compressed = compressBuffer(Buffer.from(textPayload, "utf8"), cmpAlgo);
|
|
40
|
+
if (enc.isEnabled) {
|
|
41
|
+
const encrypted = enc.encryptBuffer(compressed);
|
|
42
|
+
result = PREFIX_ENC_COMPRESSED + encrypted.toString("base64");
|
|
43
|
+
} else {
|
|
44
|
+
result = PREFIX_COMPRESSED + compressed.toString("base64");
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
result = enc.encrypt(textPayload);
|
|
48
|
+
}
|
|
49
|
+
if (result.length >= 131072) {
|
|
50
|
+
const outBuf = Buffer.from(result, "utf8");
|
|
51
|
+
const transferable = new ArrayBuffer(outBuf.byteLength);
|
|
52
|
+
new Uint8Array(transferable).set(outBuf);
|
|
53
|
+
const outputUint8 = new Uint8Array(transferable);
|
|
54
|
+
parentPort.postMessage({ id, result: outputUint8, isTransfer: true }, [transferable]);
|
|
55
|
+
} else {
|
|
56
|
+
parentPort.postMessage({ id, result });
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
let result;
|
|
60
|
+
if (textPayload.startsWith(PREFIX_ENC_COMPRESSED)) {
|
|
61
|
+
const rawBuf = Buffer.from(textPayload.slice(PREFIX_ENC_COMPRESSED.length), "base64");
|
|
62
|
+
const decryptedBuf = enc.decryptBuffer(rawBuf);
|
|
63
|
+
const decompressed = decompressBuffer(decryptedBuf, cmpAlgo !== "none" ? cmpAlgo : "brotli");
|
|
64
|
+
result = decompressed.toString("utf8");
|
|
65
|
+
} else if (textPayload.startsWith(PREFIX_COMPRESSED)) {
|
|
66
|
+
const rawBuf = Buffer.from(textPayload.slice(PREFIX_COMPRESSED.length), "base64");
|
|
67
|
+
const decompressed = decompressBuffer(rawBuf, cmpAlgo !== "none" ? cmpAlgo : "brotli");
|
|
68
|
+
result = decompressed.toString("utf8");
|
|
69
|
+
} else {
|
|
70
|
+
result = enc.decrypt(textPayload);
|
|
71
|
+
}
|
|
72
|
+
if (result.length >= 131072) {
|
|
73
|
+
const outBuf = Buffer.from(result, "utf8");
|
|
74
|
+
const transferable = new ArrayBuffer(outBuf.byteLength);
|
|
75
|
+
new Uint8Array(transferable).set(outBuf);
|
|
76
|
+
const outputUint8 = new Uint8Array(transferable);
|
|
77
|
+
parentPort.postMessage({ id, result: outputUint8, isTransfer: true }, [transferable]);
|
|
78
|
+
} else {
|
|
79
|
+
parentPort.postMessage({ id, result });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} catch (e) {
|
|
83
|
+
parentPort.postMessage({ id, error: e.message });
|
|
31
84
|
}
|
|
32
|
-
} catch (e) {
|
|
33
|
-
parentPort.postMessage({ id, error: e.message });
|
|
34
85
|
}
|
|
35
|
-
|
|
86
|
+
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tricache",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Three-tier Node.js cache: L1 smart RAM (adaptive LFU/LRU + Count-Min Sketch) → L1.5 NVMe disk spill → L2 Redis/Valkey. Includes AES-256-GCM at-rest encryption, WASM Bloom filter, Stale-While-Revalidate, and thundering-herd prevention.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
@@ -18,50 +18,100 @@
|
|
|
18
18
|
"nodejs"
|
|
19
19
|
],
|
|
20
20
|
"license": "MIT",
|
|
21
|
-
"
|
|
21
|
+
"author": "Kareem (https://github.com/Kareem411)",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/Kareem411/TriCache.git"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/Kareem411/TriCache#readme",
|
|
22
27
|
"type": "module",
|
|
23
28
|
"main": "./dist/index.js",
|
|
24
29
|
"module": "./dist/index.js",
|
|
25
30
|
"types": "./dist/index.d.ts",
|
|
31
|
+
"bin": {
|
|
32
|
+
"tricache": "./bin/tricache.js"
|
|
33
|
+
},
|
|
26
34
|
"exports": {
|
|
27
35
|
".": {
|
|
28
36
|
"types": "./dist/index.d.ts",
|
|
29
37
|
"import": "./dist/index.js",
|
|
30
38
|
"require": "./dist/index.cjs"
|
|
39
|
+
},
|
|
40
|
+
"./next": {
|
|
41
|
+
"types": "./dist/next/index.d.ts",
|
|
42
|
+
"import": "./dist/next/index.js",
|
|
43
|
+
"require": "./dist/next/index.cjs"
|
|
44
|
+
},
|
|
45
|
+
"./nestjs": {
|
|
46
|
+
"types": "./dist/nestjs/index.d.ts",
|
|
47
|
+
"import": "./dist/nestjs/index.js",
|
|
48
|
+
"require": "./dist/nestjs/index.cjs"
|
|
49
|
+
},
|
|
50
|
+
"./prisma": {
|
|
51
|
+
"types": "./dist/prisma/index.d.ts",
|
|
52
|
+
"import": "./dist/prisma/index.js",
|
|
53
|
+
"require": "./dist/prisma/index.cjs"
|
|
54
|
+
},
|
|
55
|
+
"./drizzle": {
|
|
56
|
+
"types": "./dist/drizzle/index.d.ts",
|
|
57
|
+
"import": "./dist/drizzle/index.js",
|
|
58
|
+
"require": "./dist/drizzle/index.cjs"
|
|
59
|
+
},
|
|
60
|
+
"./http": {
|
|
61
|
+
"types": "./dist/http/index.d.ts",
|
|
62
|
+
"import": "./dist/http/index.js",
|
|
63
|
+
"require": "./dist/http/index.cjs"
|
|
31
64
|
}
|
|
32
65
|
},
|
|
33
66
|
"files": [
|
|
67
|
+
"bin",
|
|
34
68
|
"dist",
|
|
35
69
|
"src",
|
|
36
70
|
"README.md",
|
|
37
71
|
"LICENSE"
|
|
38
72
|
],
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
75
|
+
"@nestjs/core": "^10.0.0 || ^11.0.0"
|
|
76
|
+
},
|
|
77
|
+
"peerDependenciesMeta": {
|
|
78
|
+
"@nestjs/common": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
81
|
+
"@nestjs/core": {
|
|
82
|
+
"optional": true
|
|
83
|
+
}
|
|
47
84
|
},
|
|
48
85
|
"dependencies": {
|
|
49
|
-
"ioredis": "
|
|
50
|
-
"msgpackr": "2.0.
|
|
86
|
+
"ioredis": "6.0.0",
|
|
87
|
+
"msgpackr": "2.0.5"
|
|
51
88
|
},
|
|
52
89
|
"devDependencies": {
|
|
53
|
-
"@
|
|
54
|
-
"
|
|
55
|
-
"
|
|
90
|
+
"@nestjs/common": "^11.0.0",
|
|
91
|
+
"@nestjs/core": "^11.0.0",
|
|
92
|
+
"@types/node": "^26.2.0",
|
|
93
|
+
"oxlint": "^1.79.0",
|
|
94
|
+
"redis": "^6.2.1",
|
|
95
|
+
"tsup": "^8.5.1",
|
|
96
|
+
"tsx": "^4.23.12",
|
|
56
97
|
"typescript": "^6.0.3",
|
|
57
|
-
"
|
|
98
|
+
"vite": "^8.2.2",
|
|
99
|
+
"vitest": "^4.1.11"
|
|
58
100
|
},
|
|
59
101
|
"engines": {
|
|
60
102
|
"node": ">=22.13.0"
|
|
61
103
|
},
|
|
62
|
-
"
|
|
63
|
-
|
|
64
|
-
"
|
|
65
|
-
"
|
|
104
|
+
"scripts": {
|
|
105
|
+
"build": "tsup src/index.ts src/cli.ts src/serialize-worker.ts src/next/index.ts src/nestjs/index.ts src/prisma/index.ts src/drizzle/index.ts src/http/index.ts --format esm,cjs --dts --clean",
|
|
106
|
+
"postbuild": "node --input-type=module -e \"import{readdirSync,rmSync,statSync}from'fs';import{join}from'path';function clean(d){for(const f of readdirSync(d)){const p=join(d,f);if(statSync(p).isDirectory())clean(p);else if(p.endsWith('.d.cts'))rmSync(p,{force:true});}}clean('dist');\"",
|
|
107
|
+
"dev": "tsup src/index.ts src/serialize-worker.ts --format esm,cjs --dts --watch",
|
|
108
|
+
"typecheck": "tsc --noEmit",
|
|
109
|
+
"typecheck:tests": "tsc --noEmit -p tsconfig.test.json",
|
|
110
|
+
"typecheck:all": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
111
|
+
"lint": "oxlint src tests scripts",
|
|
112
|
+
"test": "vitest run",
|
|
113
|
+
"test:integration": "vitest run --config vitest.integration.config.ts",
|
|
114
|
+
"test:watch": "vitest",
|
|
115
|
+
"bench": "node --expose-gc --max-old-space-size=8192 --import tsx/esm bench/benchmark.ts"
|
|
66
116
|
}
|
|
67
|
-
}
|
|
117
|
+
}
|