tricache 0.6.6 → 0.7.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 +1 -1
- package/README.md +500 -24
- package/bin/tricache.js +8 -0
- package/dist/cache-service-AJASH5FX.js +7 -0
- package/dist/cache-service-BblHuF3i.d.ts +1476 -0
- package/dist/chunk-3XQUQBOZ.js +4583 -0
- package/dist/{chunk-FKY5R3CE.js → chunk-PFB5FMEY.js} +126 -4
- package/dist/chunk-UBSR5UYA.js +0 -0
- package/dist/cli.cjs +5181 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.js +130 -0
- package/dist/drizzle/index.cjs +5161 -0
- package/dist/drizzle/index.d.ts +44 -0
- package/dist/drizzle/index.js +46 -0
- package/dist/http/index.cjs +5289 -0
- package/dist/http/index.d.ts +33 -0
- package/dist/http/index.js +172 -0
- package/dist/index.cjs +1181 -194
- package/dist/index.d.ts +15 -1230
- package/dist/index.js +11 -3697
- package/dist/nestjs/index.cjs +5305 -0
- package/dist/nestjs/index.d.ts +210 -0
- package/dist/nestjs/index.js +250 -0
- package/dist/next/index.cjs +5327 -0
- package/dist/next/index.d.ts +204 -0
- package/dist/next/index.js +274 -0
- package/dist/prisma/index.cjs +122 -0
- package/dist/prisma/index.d.ts +60 -0
- package/dist/prisma/index.js +86 -0
- package/dist/serialize-worker.cjs +177 -13
- package/dist/serialize-worker.js +63 -12
- package/package.json +64 -15
- package/src/cache-service.ts +1005 -192
- package/src/cli.ts +143 -0
- package/src/compression.ts +63 -0
- package/src/disk-tier.ts +71 -7
- package/src/drizzle/index.ts +86 -0
- package/src/encryption.ts +56 -5
- package/src/http/index.ts +223 -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 +244 -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 +151 -0
- package/src/serialize-worker.ts +77 -13
- package/src/smart-memory-cache.ts +22 -8
- package/src/types.ts +172 -2
- package/src/wasm/bloom-filter-wasm.ts +3 -2
- package/src/worker-pool.ts +156 -42
|
@@ -0,0 +1,86 @@
|
|
|
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 result = await query(args);
|
|
50
|
+
await cache.invalidateTag(modelTag);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
if (READ_OPERATIONS.has(operation)) {
|
|
54
|
+
const queryArgs = args ?? {};
|
|
55
|
+
const cacheConfig = queryArgs.cache;
|
|
56
|
+
if (cacheConfig !== void 0 || autoCache) {
|
|
57
|
+
const opts = typeof cacheConfig === "object" && cacheConfig !== null ? cacheConfig : {};
|
|
58
|
+
const { cache: _c, ...cleanArgs } = queryArgs;
|
|
59
|
+
const key = opts.key ?? generatePrismaCacheKey(model, operation, cleanArgs);
|
|
60
|
+
const tags = [.../* @__PURE__ */ new Set([modelTag, ...opts.tags ?? []])];
|
|
61
|
+
return cache.wrap(
|
|
62
|
+
key,
|
|
63
|
+
() => query(cleanArgs),
|
|
64
|
+
{
|
|
65
|
+
ttl: opts.ttl ?? defaultTtl,
|
|
66
|
+
swr: opts.swr,
|
|
67
|
+
priority: opts.priority,
|
|
68
|
+
tags,
|
|
69
|
+
dependsOn: opts.dependsOn,
|
|
70
|
+
refreshAhead: opts.refreshAhead,
|
|
71
|
+
xfetchBeta: opts.xfetchBeta,
|
|
72
|
+
notFoundTtl: opts.notFoundTtl
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return query(args);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
generatePrismaCacheKey,
|
|
85
|
+
withTriCache
|
|
86
|
+
};
|
|
@@ -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:";
|
|
@@ -89,8 +122,10 @@ var CacheEncryption = class {
|
|
|
89
122
|
}
|
|
90
123
|
return out;
|
|
91
124
|
}
|
|
92
|
-
constructor(keyBase642,
|
|
93
|
-
|
|
125
|
+
constructor(keyBase642, loggerOrMode = consoleLogger, mode2 = "aes-256-gcm", previousKeyBase64, previousMode) {
|
|
126
|
+
const logger = typeof loggerOrMode === "object" && loggerOrMode !== null ? loggerOrMode : consoleLogger;
|
|
127
|
+
const resolvedMode = typeof loggerOrMode === "string" ? loggerOrMode : mode2;
|
|
128
|
+
this._mode = resolvedMode;
|
|
94
129
|
if (!keyBase642) {
|
|
95
130
|
if (process.env.NODE_ENV === "production") {
|
|
96
131
|
logger.warn(
|
|
@@ -102,7 +137,7 @@ var CacheEncryption = class {
|
|
|
102
137
|
}
|
|
103
138
|
try {
|
|
104
139
|
const buf = Buffer.from(keyBase642, "base64");
|
|
105
|
-
const requiredLen =
|
|
140
|
+
const requiredLen = resolvedMode === "aes-256-gcm" ? 32 : resolvedMode === "aes-128-gcm" || resolvedMode === "aes-128-ctr" ? 16 : 0;
|
|
106
141
|
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
107
142
|
throw new Error(`${mode2} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
108
143
|
}
|
|
@@ -153,6 +188,9 @@ var CacheEncryption = class {
|
|
|
153
188
|
get isEnabled() {
|
|
154
189
|
return this._key !== null;
|
|
155
190
|
}
|
|
191
|
+
get mode() {
|
|
192
|
+
return this._mode;
|
|
193
|
+
}
|
|
156
194
|
/**
|
|
157
195
|
* Snapshot the key material needed to spin up an off-main-thread worker pool.
|
|
158
196
|
* Replaces the previously-used `as unknown as { _key, _mode, … }` private-field
|
|
@@ -167,6 +205,43 @@ var CacheEncryption = class {
|
|
|
167
205
|
prevMode: this._prevMode
|
|
168
206
|
};
|
|
169
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Rotate the active encryption key dynamically at runtime.
|
|
210
|
+
* Promotes the current primary key to previousEncryptionKey for zero-downtime
|
|
211
|
+
* fallback decryption of existing cache entries in L2/Disk, and installs
|
|
212
|
+
* the new key as primary for all subsequent writes.
|
|
213
|
+
*/
|
|
214
|
+
rotateKey(newKeyBase64, newMode) {
|
|
215
|
+
if (!newKeyBase64) {
|
|
216
|
+
throw new Error("rotateKey requires a valid non-empty base64 key");
|
|
217
|
+
}
|
|
218
|
+
const mode2 = newMode ?? this._mode;
|
|
219
|
+
const buf = Buffer.from(newKeyBase64, "base64");
|
|
220
|
+
const requiredLen = mode2 === "aes-256-gcm" ? 32 : mode2 === "aes-128-gcm" || mode2 === "aes-128-ctr" ? 16 : 0;
|
|
221
|
+
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
222
|
+
throw new Error(`${mode2} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
223
|
+
}
|
|
224
|
+
if (buf.length < 1) {
|
|
225
|
+
throw new Error("XOR key must be at least 1 byte");
|
|
226
|
+
}
|
|
227
|
+
let newKeyObj = null;
|
|
228
|
+
if (mode2 !== "xor") {
|
|
229
|
+
newKeyObj = (0, import_crypto.createSecretKey)(buf);
|
|
230
|
+
const _wc = (0, import_crypto.createCipheriv)(
|
|
231
|
+
mode2,
|
|
232
|
+
newKeyObj,
|
|
233
|
+
Buffer.alloc(mode2 === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
234
|
+
);
|
|
235
|
+
_wc.update(Buffer.alloc(0));
|
|
236
|
+
_wc.final();
|
|
237
|
+
}
|
|
238
|
+
this._prevKey = this._key;
|
|
239
|
+
this._prevKeyObj = this._keyObj;
|
|
240
|
+
this._prevMode = this._mode;
|
|
241
|
+
this._key = buf;
|
|
242
|
+
this._keyObj = newKeyObj;
|
|
243
|
+
this._mode = mode2;
|
|
244
|
+
}
|
|
170
245
|
// ── String (Redis) ────────────────────────────────────────────────────────
|
|
171
246
|
/**
|
|
172
247
|
* Encrypt a string for Redis storage.
|
|
@@ -346,6 +421,48 @@ var CacheEncryption = class {
|
|
|
346
421
|
}
|
|
347
422
|
};
|
|
348
423
|
|
|
424
|
+
// src/compression.ts
|
|
425
|
+
var import_zlib = __toESM(require("zlib"), 1);
|
|
426
|
+
var PREFIX_COMPRESSED = "cmp:v1:";
|
|
427
|
+
var PREFIX_ENC_COMPRESSED = "ecp:v1:";
|
|
428
|
+
var COMPRESSION_MAGIC = Buffer.from([84, 82, 67, 1]);
|
|
429
|
+
function compressBuffer(buf, algorithm) {
|
|
430
|
+
if (algorithm === "brotli") {
|
|
431
|
+
return import_zlib.default.brotliCompressSync(buf, {
|
|
432
|
+
params: {
|
|
433
|
+
[import_zlib.default.constants.BROTLI_PARAM_QUALITY]: 4
|
|
434
|
+
// Fast level 4 compression
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
if (algorithm === "gzip") {
|
|
439
|
+
return import_zlib.default.gzipSync(buf, { level: 6 });
|
|
440
|
+
}
|
|
441
|
+
return buf;
|
|
442
|
+
}
|
|
443
|
+
function decompressBuffer(buf, algorithm = "brotli") {
|
|
444
|
+
if (algorithm === "gzip") {
|
|
445
|
+
try {
|
|
446
|
+
return import_zlib.default.gunzipSync(buf);
|
|
447
|
+
} catch {
|
|
448
|
+
try {
|
|
449
|
+
return import_zlib.default.brotliDecompressSync(buf);
|
|
450
|
+
} catch {
|
|
451
|
+
return buf;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
try {
|
|
456
|
+
return import_zlib.default.brotliDecompressSync(buf);
|
|
457
|
+
} catch {
|
|
458
|
+
try {
|
|
459
|
+
return import_zlib.default.gunzipSync(buf);
|
|
460
|
+
} catch {
|
|
461
|
+
return buf;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
349
466
|
// src/serialize-worker.ts
|
|
350
467
|
var silentLogger = {
|
|
351
468
|
debug: () => {
|
|
@@ -357,7 +474,7 @@ var silentLogger = {
|
|
|
357
474
|
error: () => {
|
|
358
475
|
}
|
|
359
476
|
};
|
|
360
|
-
var { keyBase64, mode, prevKeyBase64, prevMode } = import_worker_threads.workerData;
|
|
477
|
+
var { keyBase64, mode, prevKeyBase64, prevMode, compression, compressionThresholdBytes } = import_worker_threads.workerData;
|
|
361
478
|
var enc = new CacheEncryption(
|
|
362
479
|
keyBase64 || void 0,
|
|
363
480
|
silentLogger,
|
|
@@ -365,14 +482,61 @@ var enc = new CacheEncryption(
|
|
|
365
482
|
prevKeyBase64 || void 0,
|
|
366
483
|
prevMode
|
|
367
484
|
);
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
485
|
+
var cmpAlgo = compression ?? "none";
|
|
486
|
+
var cmpThreshold = compressionThresholdBytes ?? 1024;
|
|
487
|
+
import_worker_threads.parentPort.on(
|
|
488
|
+
"message",
|
|
489
|
+
({ id, type, payload }) => {
|
|
490
|
+
try {
|
|
491
|
+
const textPayload = typeof payload === "string" ? payload : Buffer.from(payload.buffer, payload.byteOffset, payload.byteLength).toString("utf8");
|
|
492
|
+
if (type === "encrypt") {
|
|
493
|
+
let result;
|
|
494
|
+
if (cmpAlgo !== "none" && textPayload.length > cmpThreshold) {
|
|
495
|
+
const compressed = compressBuffer(Buffer.from(textPayload, "utf8"), cmpAlgo);
|
|
496
|
+
if (enc.isEnabled) {
|
|
497
|
+
const encrypted = enc.encryptBuffer(compressed);
|
|
498
|
+
result = PREFIX_ENC_COMPRESSED + encrypted.toString("base64");
|
|
499
|
+
} else {
|
|
500
|
+
result = PREFIX_COMPRESSED + compressed.toString("base64");
|
|
501
|
+
}
|
|
502
|
+
} else {
|
|
503
|
+
result = enc.encrypt(textPayload);
|
|
504
|
+
}
|
|
505
|
+
if (result.length >= 131072) {
|
|
506
|
+
const outBuf = Buffer.from(result, "utf8");
|
|
507
|
+
const transferable = new ArrayBuffer(outBuf.byteLength);
|
|
508
|
+
new Uint8Array(transferable).set(outBuf);
|
|
509
|
+
const outputUint8 = new Uint8Array(transferable);
|
|
510
|
+
import_worker_threads.parentPort.postMessage({ id, result: outputUint8, isTransfer: true }, [transferable]);
|
|
511
|
+
} else {
|
|
512
|
+
import_worker_threads.parentPort.postMessage({ id, result });
|
|
513
|
+
}
|
|
514
|
+
} else {
|
|
515
|
+
let result;
|
|
516
|
+
if (textPayload.startsWith(PREFIX_ENC_COMPRESSED)) {
|
|
517
|
+
const rawBuf = Buffer.from(textPayload.slice(PREFIX_ENC_COMPRESSED.length), "base64");
|
|
518
|
+
const decryptedBuf = enc.decryptBuffer(rawBuf);
|
|
519
|
+
const decompressed = decompressBuffer(decryptedBuf, cmpAlgo !== "none" ? cmpAlgo : "brotli");
|
|
520
|
+
result = decompressed.toString("utf8");
|
|
521
|
+
} else if (textPayload.startsWith(PREFIX_COMPRESSED)) {
|
|
522
|
+
const rawBuf = Buffer.from(textPayload.slice(PREFIX_COMPRESSED.length), "base64");
|
|
523
|
+
const decompressed = decompressBuffer(rawBuf, cmpAlgo !== "none" ? cmpAlgo : "brotli");
|
|
524
|
+
result = decompressed.toString("utf8");
|
|
525
|
+
} else {
|
|
526
|
+
result = enc.decrypt(textPayload);
|
|
527
|
+
}
|
|
528
|
+
if (result.length >= 131072) {
|
|
529
|
+
const outBuf = Buffer.from(result, "utf8");
|
|
530
|
+
const transferable = new ArrayBuffer(outBuf.byteLength);
|
|
531
|
+
new Uint8Array(transferable).set(outBuf);
|
|
532
|
+
const outputUint8 = new Uint8Array(transferable);
|
|
533
|
+
import_worker_threads.parentPort.postMessage({ id, result: outputUint8, isTransfer: true }, [transferable]);
|
|
534
|
+
} else {
|
|
535
|
+
import_worker_threads.parentPort.postMessage({ id, result });
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
} catch (e) {
|
|
539
|
+
import_worker_threads.parentPort.postMessage({ id, error: e.message });
|
|
374
540
|
}
|
|
375
|
-
} catch (e) {
|
|
376
|
-
import_worker_threads.parentPort.postMessage({ id, error: e.message });
|
|
377
541
|
}
|
|
378
|
-
|
|
542
|
+
);
|
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-PFB5FMEY.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.0",
|
|
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,49 +18,98 @@
|
|
|
18
18
|
"nodejs"
|
|
19
19
|
],
|
|
20
20
|
"license": "MIT",
|
|
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",
|
|
21
27
|
"type": "module",
|
|
22
28
|
"main": "./dist/index.js",
|
|
23
29
|
"module": "./dist/index.js",
|
|
24
30
|
"types": "./dist/index.d.ts",
|
|
31
|
+
"bin": {
|
|
32
|
+
"tricache": "./bin/tricache.js"
|
|
33
|
+
},
|
|
25
34
|
"exports": {
|
|
26
35
|
".": {
|
|
27
36
|
"types": "./dist/index.d.ts",
|
|
28
37
|
"import": "./dist/index.js",
|
|
29
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"
|
|
30
64
|
}
|
|
31
65
|
},
|
|
32
66
|
"files": [
|
|
67
|
+
"bin",
|
|
33
68
|
"dist",
|
|
34
69
|
"src",
|
|
35
70
|
"README.md",
|
|
36
71
|
"LICENSE"
|
|
37
72
|
],
|
|
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
|
+
}
|
|
84
|
+
},
|
|
38
85
|
"dependencies": {
|
|
39
|
-
"ioredis": "
|
|
40
|
-
"msgpackr": "2.0.
|
|
86
|
+
"ioredis": "6.0.0",
|
|
87
|
+
"msgpackr": "2.0.5"
|
|
41
88
|
},
|
|
42
89
|
"devDependencies": {
|
|
43
|
-
"@
|
|
44
|
-
"
|
|
45
|
-
"
|
|
90
|
+
"@nestjs/common": "^11.0.0",
|
|
91
|
+
"@nestjs/core": "^11.0.0",
|
|
92
|
+
"@types/node": "^26.2.0",
|
|
93
|
+
"oxlint": "^1.79.0",
|
|
94
|
+
"tsup": "^8.5.1",
|
|
95
|
+
"tsx": "^4.23.12",
|
|
46
96
|
"typescript": "^6.0.3",
|
|
47
|
-
"
|
|
97
|
+
"vite": "^8.2.2",
|
|
98
|
+
"vitest": "^4.1.11"
|
|
48
99
|
},
|
|
49
100
|
"engines": {
|
|
50
101
|
"node": ">=22.13.0"
|
|
51
102
|
},
|
|
52
|
-
"homepage": "https://github.com/Kareem411/TriCache#readme",
|
|
53
|
-
"repository": {
|
|
54
|
-
"type": "git",
|
|
55
|
-
"url": "git+https://github.com/Kareem411/TriCache.git"
|
|
56
|
-
},
|
|
57
103
|
"scripts": {
|
|
58
|
-
"build": "tsup src/index.ts src/serialize-worker.ts --format esm,cjs --dts --clean",
|
|
59
|
-
"postbuild": "node --input-type=module -e \"import{rmSync}from'fs';
|
|
104
|
+
"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",
|
|
105
|
+
"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');\"",
|
|
60
106
|
"dev": "tsup src/index.ts src/serialize-worker.ts --format esm,cjs --dts --watch",
|
|
61
107
|
"typecheck": "tsc --noEmit",
|
|
108
|
+
"typecheck:tests": "tsc --noEmit -p tsconfig.test.json",
|
|
109
|
+
"typecheck:all": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
110
|
+
"lint": "oxlint src tests",
|
|
62
111
|
"test": "vitest run",
|
|
63
112
|
"test:watch": "vitest",
|
|
64
|
-
"bench": "node --expose-gc --import tsx/esm bench/benchmark.ts"
|
|
113
|
+
"bench": "node --expose-gc --max-old-space-size=8192 --import tsx/esm bench/benchmark.ts"
|
|
65
114
|
}
|
|
66
115
|
}
|