tricache 0.6.0 → 0.6.2
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 +119 -0
- package/dist/chunk-F24NEZGQ.js +342 -0
- package/dist/index.cjs +494 -164
- package/dist/index.d.cts +125 -0
- package/dist/index.d.ts +1552 -18
- package/dist/index.js +3524 -25
- package/dist/serialize-worker.cjs +366 -0
- package/dist/serialize-worker.d.cts +2 -0
- package/dist/serialize-worker.d.ts +2 -0
- package/dist/serialize-worker.js +35 -0
- package/package.json +5 -5
- package/src/cache-service.ts +310 -93
- package/src/serialize-worker.ts +56 -0
- package/src/smart-memory-cache.ts +42 -1
- package/src/types.ts +99 -1
- package/src/worker-pool.ts +190 -0
- package/dist/cache-service.d.ts +0 -419
- package/dist/cache-service.d.ts.map +0 -1
- package/dist/cache-service.js +0 -1788
- package/dist/cache-service.js.map +0 -1
- package/dist/disk-tier.d.ts +0 -150
- package/dist/disk-tier.d.ts.map +0 -1
- package/dist/disk-tier.js +0 -808
- package/dist/disk-tier.js.map +0 -1
- package/dist/encryption.d.ts +0 -90
- package/dist/encryption.d.ts.map +0 -1
- package/dist/encryption.js +0 -393
- package/dist/encryption.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/smart-memory-cache.d.ts +0 -208
- package/dist/smart-memory-cache.d.ts.map +0 -1
- package/dist/smart-memory-cache.js +0 -933
- package/dist/smart-memory-cache.js.map +0 -1
- package/dist/types.d.ts +0 -537
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -20
- package/dist/types.js.map +0 -1
- package/dist/wasm/bloom-filter-wasm.d.ts +0 -29
- package/dist/wasm/bloom-filter-wasm.d.ts.map +0 -1
- package/dist/wasm/bloom-filter-wasm.js +0 -83
- package/dist/wasm/bloom-filter-wasm.js.map +0 -1
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/serialize-worker.ts
|
|
4
|
+
var import_worker_threads = require("worker_threads");
|
|
5
|
+
|
|
6
|
+
// src/encryption.ts
|
|
7
|
+
var import_crypto = require("crypto");
|
|
8
|
+
var IV_BYTES = 12;
|
|
9
|
+
var TAG_BYTES = 16;
|
|
10
|
+
var PREFIX_256 = "enc:v1:";
|
|
11
|
+
var PREFIX_128 = "a128:v1:";
|
|
12
|
+
var PREFIX_XOR = "xor:v1:";
|
|
13
|
+
var MAGIC_256 = Buffer.from([84, 82, 73, 67, 49, 69, 78, 67]);
|
|
14
|
+
var MAGIC_128 = Buffer.from([84, 82, 73, 67, 49, 49, 50, 56]);
|
|
15
|
+
var MAGIC_XOR = Buffer.from([84, 82, 73, 67, 49, 88, 79, 82]);
|
|
16
|
+
var MAGIC_CTR = Buffer.from([84, 82, 73, 67, 49, 67, 84, 82]);
|
|
17
|
+
var MAGIC_LEN = 8;
|
|
18
|
+
var CTR_IV_BYTES = 16;
|
|
19
|
+
var PREFIX_CTR = "ctr:v1:";
|
|
20
|
+
var IV_POOL_COUNT = 64;
|
|
21
|
+
var CacheEncryption = class {
|
|
22
|
+
_key = null;
|
|
23
|
+
// raw bytes — used only for XOR
|
|
24
|
+
_keyObj = null;
|
|
25
|
+
// parsed KeyObject — used for all AES modes
|
|
26
|
+
_mode;
|
|
27
|
+
_prevKey = null;
|
|
28
|
+
_prevKeyObj = null;
|
|
29
|
+
_prevMode = "aes-256-gcm";
|
|
30
|
+
// Pre-allocated GCM IV pool — 12-byte IVs (96-bit, NIST recommended).
|
|
31
|
+
_ivPool = Buffer.allocUnsafe(IV_BYTES * IV_POOL_COUNT);
|
|
32
|
+
_ivOffset = IV_BYTES * IV_POOL_COUNT;
|
|
33
|
+
// start exhausted → fill on first use
|
|
34
|
+
/** Return the next unique 12-byte IV slice from the pre-allocated GCM pool. */
|
|
35
|
+
_nextIV() {
|
|
36
|
+
if (this._ivOffset >= this._ivPool.length) {
|
|
37
|
+
(0, import_crypto.randomFillSync)(this._ivPool);
|
|
38
|
+
this._ivOffset = 0;
|
|
39
|
+
}
|
|
40
|
+
const iv = this._ivPool.subarray(this._ivOffset, this._ivOffset + IV_BYTES);
|
|
41
|
+
this._ivOffset += IV_BYTES;
|
|
42
|
+
return iv;
|
|
43
|
+
}
|
|
44
|
+
// Pre-allocated CTR IV pool — 16-byte IVs (full AES block = initial counter block).
|
|
45
|
+
_ctrIvPool = Buffer.allocUnsafe(CTR_IV_BYTES * IV_POOL_COUNT);
|
|
46
|
+
_ctrIvOffset = CTR_IV_BYTES * IV_POOL_COUNT;
|
|
47
|
+
/** Return the next unique 16-byte IV slice from the pre-allocated CTR pool. */
|
|
48
|
+
_nextCtrIV() {
|
|
49
|
+
if (this._ctrIvOffset >= this._ctrIvPool.length) {
|
|
50
|
+
(0, import_crypto.randomFillSync)(this._ctrIvPool);
|
|
51
|
+
this._ctrIvOffset = 0;
|
|
52
|
+
}
|
|
53
|
+
const iv = this._ctrIvPool.subarray(this._ctrIvOffset, this._ctrIvOffset + CTR_IV_BYTES);
|
|
54
|
+
this._ctrIvOffset += CTR_IV_BYTES;
|
|
55
|
+
return iv;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* XOR `data` with the key, cycling through key bytes.
|
|
59
|
+
* Self-inverse: _xorBuffer(_xorBuffer(x)) === x.
|
|
60
|
+
*
|
|
61
|
+
* Fast path: Uint32Array views over the existing Buffer memory — zero copy,
|
|
62
|
+
* no method-call overhead. V8 JITs `out32[i] = data32[i] ^ key32[...]`
|
|
63
|
+
* to a single `XOR r32, r32` instruction. Requires 4-byte-aligned byteOffsets,
|
|
64
|
+
* which Node.js guarantees (pool rounds up to 8 bytes; direct allocs to page).
|
|
65
|
+
*
|
|
66
|
+
* WARNING: XOR is NOT cryptographic. It provides obfuscation only.
|
|
67
|
+
*/
|
|
68
|
+
_xorBuffer(data) {
|
|
69
|
+
const key = this._key;
|
|
70
|
+
const klen = key.length;
|
|
71
|
+
const len = data.length;
|
|
72
|
+
const out = Buffer.allocUnsafe(len);
|
|
73
|
+
if (klen % 4 === 0 && len >= 4 && data.byteOffset % 4 === 0 && out.byteOffset % 4 === 0 && key.byteOffset % 4 === 0) {
|
|
74
|
+
const wordCount = len >>> 2;
|
|
75
|
+
const data32 = new Uint32Array(data.buffer, data.byteOffset, wordCount);
|
|
76
|
+
const out32 = new Uint32Array(out.buffer, out.byteOffset, wordCount);
|
|
77
|
+
const key32 = new Uint32Array(key.buffer, key.byteOffset, klen >>> 2);
|
|
78
|
+
const kLen32 = key32.length;
|
|
79
|
+
for (let i = 0; i < wordCount; i++) {
|
|
80
|
+
out32[i] = data32[i] ^ key32[i % kLen32];
|
|
81
|
+
}
|
|
82
|
+
for (let i = wordCount << 2; i < len; i++) {
|
|
83
|
+
out[i] = data[i] ^ key[i % klen];
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
for (let i = 0; i < len; i++) {
|
|
88
|
+
out[i] = data[i] ^ key[i % klen];
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
constructor(keyBase642, logger, mode2 = "aes-256-gcm", previousKeyBase64, previousMode) {
|
|
93
|
+
this._mode = mode2;
|
|
94
|
+
if (!keyBase642) {
|
|
95
|
+
if (process.env.NODE_ENV === "production") {
|
|
96
|
+
logger.warn(
|
|
97
|
+
"SECURITY: encryption key not set \u2014 cache data stored unencrypted at rest",
|
|
98
|
+
{ hint: "Set encryptionKey option or CACHE_ENCRYPTION_KEY env var" }
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const buf = Buffer.from(keyBase642, "base64");
|
|
105
|
+
const requiredLen = mode2 === "aes-256-gcm" ? 32 : mode2 === "aes-128-gcm" || mode2 === "aes-128-ctr" ? 16 : 0;
|
|
106
|
+
if (requiredLen > 0 && buf.length !== requiredLen) {
|
|
107
|
+
throw new Error(`${mode2} requires exactly ${requiredLen} bytes (got ${buf.length})`);
|
|
108
|
+
}
|
|
109
|
+
if (buf.length < 1) {
|
|
110
|
+
throw new Error("XOR key must be at least 1 byte");
|
|
111
|
+
}
|
|
112
|
+
this._key = buf;
|
|
113
|
+
if (mode2 !== "xor") {
|
|
114
|
+
this._keyObj = (0, import_crypto.createSecretKey)(buf);
|
|
115
|
+
const _wc = (0, import_crypto.createCipheriv)(
|
|
116
|
+
mode2,
|
|
117
|
+
this._keyObj,
|
|
118
|
+
Buffer.alloc(mode2 === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
119
|
+
);
|
|
120
|
+
_wc.update(Buffer.alloc(0));
|
|
121
|
+
_wc.final();
|
|
122
|
+
}
|
|
123
|
+
if (mode2 === "xor") {
|
|
124
|
+
logger.warn(
|
|
125
|
+
"Cache obfuscation enabled (XOR). WARNING: XOR is NOT cryptographic \u2014 use only for dev or non-sensitive data."
|
|
126
|
+
);
|
|
127
|
+
} else {
|
|
128
|
+
logger.debug(`Cache encryption enabled (${mode2.toUpperCase()})`);
|
|
129
|
+
}
|
|
130
|
+
} catch (err) {
|
|
131
|
+
logger.error("Invalid encryption key \u2014 falling back to plaintext", {}, err);
|
|
132
|
+
}
|
|
133
|
+
if (previousKeyBase64) {
|
|
134
|
+
try {
|
|
135
|
+
this._prevMode = previousMode ?? mode2;
|
|
136
|
+
this._prevKey = Buffer.from(previousKeyBase64, "base64");
|
|
137
|
+
if (this._prevMode !== "xor") {
|
|
138
|
+
this._prevKeyObj = (0, import_crypto.createSecretKey)(this._prevKey);
|
|
139
|
+
const _wc = (0, import_crypto.createCipheriv)(
|
|
140
|
+
this._prevMode,
|
|
141
|
+
this._prevKeyObj,
|
|
142
|
+
Buffer.alloc(this._prevMode === "aes-128-ctr" ? CTR_IV_BYTES : IV_BYTES)
|
|
143
|
+
);
|
|
144
|
+
_wc.update(Buffer.alloc(0));
|
|
145
|
+
_wc.final();
|
|
146
|
+
}
|
|
147
|
+
logger.debug("Previous encryption key loaded for key rotation fallback");
|
|
148
|
+
} catch (err) {
|
|
149
|
+
logger.warn("Invalid previousEncryptionKey \u2014 rotation fallback disabled", { error: err.message });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
get isEnabled() {
|
|
154
|
+
return this._key !== null;
|
|
155
|
+
}
|
|
156
|
+
// ── String (Redis) ────────────────────────────────────────────────────────
|
|
157
|
+
/**
|
|
158
|
+
* Encrypt a string for Redis storage.
|
|
159
|
+
* Returns a mode-prefixed base64 string when a key is configured;
|
|
160
|
+
* otherwise returns the original string unchanged (backward-compatible).
|
|
161
|
+
*/
|
|
162
|
+
encrypt(plaintext) {
|
|
163
|
+
if (!this._key) return plaintext;
|
|
164
|
+
if (this._mode === "xor") {
|
|
165
|
+
const masked = this._xorBuffer(Buffer.from(plaintext, "utf8"));
|
|
166
|
+
return PREFIX_XOR + masked.toString("base64");
|
|
167
|
+
}
|
|
168
|
+
if (this._mode === "aes-128-ctr") {
|
|
169
|
+
const iv2 = this._nextCtrIV();
|
|
170
|
+
const c2 = (0, import_crypto.createCipheriv)("aes-128-ctr", this._keyObj, iv2);
|
|
171
|
+
const enc3 = c2.update(plaintext, "utf8");
|
|
172
|
+
const fin = c2.final();
|
|
173
|
+
const out2 = Buffer.allocUnsafe(CTR_IV_BYTES + enc3.length + fin.length);
|
|
174
|
+
iv2.copy(out2, 0);
|
|
175
|
+
enc3.copy(out2, CTR_IV_BYTES);
|
|
176
|
+
if (fin.length > 0) fin.copy(out2, CTR_IV_BYTES + enc3.length);
|
|
177
|
+
return PREFIX_CTR + out2.toString("base64");
|
|
178
|
+
}
|
|
179
|
+
const algo = this._mode === "aes-128-gcm" ? "aes-128-gcm" : "aes-256-gcm";
|
|
180
|
+
const prefix = this._mode === "aes-128-gcm" ? PREFIX_128 : PREFIX_256;
|
|
181
|
+
const iv = this._nextIV();
|
|
182
|
+
const c = (0, import_crypto.createCipheriv)(algo, this._keyObj, iv);
|
|
183
|
+
const enc2 = c.update(plaintext, "utf8");
|
|
184
|
+
c.final();
|
|
185
|
+
const out = Buffer.allocUnsafe(IV_BYTES + TAG_BYTES + enc2.length);
|
|
186
|
+
iv.copy(out, 0);
|
|
187
|
+
c.getAuthTag().copy(out, IV_BYTES);
|
|
188
|
+
enc2.copy(out, IV_BYTES + TAG_BYTES);
|
|
189
|
+
return prefix + out.toString("base64");
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Decrypt a Redis value.
|
|
193
|
+
* Auto-detects the envelope prefix; returns plaintext unchanged if no prefix matches.
|
|
194
|
+
* Falls back to `previousEncryptionKey` when the primary key fails (key rotation).
|
|
195
|
+
*/
|
|
196
|
+
decrypt(value) {
|
|
197
|
+
try {
|
|
198
|
+
return this._decryptWithKey(value, this._key, this._keyObj, this._mode);
|
|
199
|
+
} catch (primaryErr) {
|
|
200
|
+
if (this._prevKey) {
|
|
201
|
+
try {
|
|
202
|
+
return this._decryptWithKey(value, this._prevKey, this._prevKeyObj, this._prevMode);
|
|
203
|
+
} catch {
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
throw primaryErr;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
_decryptWithKey(value, key, keyObj, mode2) {
|
|
210
|
+
if (value.startsWith(PREFIX_XOR)) {
|
|
211
|
+
if (!key) throw new Error("Cannot decrypt: encryption key is not set");
|
|
212
|
+
const masked = Buffer.from(value.slice(PREFIX_XOR.length), "base64");
|
|
213
|
+
const klen = key.length;
|
|
214
|
+
const len = masked.length;
|
|
215
|
+
const out = Buffer.allocUnsafe(len);
|
|
216
|
+
for (let i = 0; i < len; i++) out[i] = masked[i] ^ key[i % klen];
|
|
217
|
+
return out.toString("utf8");
|
|
218
|
+
}
|
|
219
|
+
if (value.startsWith(PREFIX_CTR)) {
|
|
220
|
+
if (!keyObj) throw new Error("Cannot decrypt: encryption key is not set");
|
|
221
|
+
const combined2 = Buffer.from(value.slice(PREFIX_CTR.length), "base64");
|
|
222
|
+
const iv2 = combined2.subarray(0, CTR_IV_BYTES);
|
|
223
|
+
const ct2 = combined2.subarray(CTR_IV_BYTES);
|
|
224
|
+
const d2 = (0, import_crypto.createDecipheriv)("aes-128-ctr", keyObj, iv2);
|
|
225
|
+
const plain2 = d2.update(ct2);
|
|
226
|
+
const fin = d2.final();
|
|
227
|
+
return (fin.length > 0 ? Buffer.concat([plain2, fin]) : plain2).toString("utf8");
|
|
228
|
+
}
|
|
229
|
+
const is256 = value.startsWith(PREFIX_256);
|
|
230
|
+
const is128 = value.startsWith(PREFIX_128);
|
|
231
|
+
if (!is256 && !is128) return value;
|
|
232
|
+
if (!keyObj) throw new Error("Cannot decrypt: encryption key is not set");
|
|
233
|
+
const prefix = is128 ? PREFIX_128 : PREFIX_256;
|
|
234
|
+
const algo = is128 ? "aes-128-gcm" : "aes-256-gcm";
|
|
235
|
+
const combined = Buffer.from(value.slice(prefix.length), "base64");
|
|
236
|
+
const iv = combined.subarray(0, IV_BYTES);
|
|
237
|
+
const tag = combined.subarray(IV_BYTES, IV_BYTES + TAG_BYTES);
|
|
238
|
+
const ct = combined.subarray(IV_BYTES + TAG_BYTES);
|
|
239
|
+
const d = (0, import_crypto.createDecipheriv)(algo, keyObj, iv);
|
|
240
|
+
d.setAuthTag(tag);
|
|
241
|
+
const plain = d.update(ct);
|
|
242
|
+
d.final();
|
|
243
|
+
return plain.toString("utf8");
|
|
244
|
+
void mode2;
|
|
245
|
+
void key;
|
|
246
|
+
}
|
|
247
|
+
// ── Buffer (disk / snapshot) ──────────────────────────────────────────────
|
|
248
|
+
/** Encrypt a raw Buffer for disk/snapshot. Returns MAGIC | payload when a key is set. */
|
|
249
|
+
encryptBuffer(data) {
|
|
250
|
+
if (!this._key) return data;
|
|
251
|
+
if (this._mode === "xor") {
|
|
252
|
+
const masked = this._xorBuffer(data);
|
|
253
|
+
const out2 = Buffer.allocUnsafe(MAGIC_LEN + masked.length);
|
|
254
|
+
MAGIC_XOR.copy(out2, 0);
|
|
255
|
+
masked.copy(out2, MAGIC_LEN);
|
|
256
|
+
return out2;
|
|
257
|
+
}
|
|
258
|
+
if (this._mode === "aes-128-ctr") {
|
|
259
|
+
const iv2 = this._nextCtrIV();
|
|
260
|
+
const c2 = (0, import_crypto.createCipheriv)("aes-128-ctr", this._keyObj, iv2);
|
|
261
|
+
const enc3 = c2.update(data);
|
|
262
|
+
const fin = c2.final();
|
|
263
|
+
const out2 = Buffer.allocUnsafe(MAGIC_LEN + CTR_IV_BYTES + enc3.length + fin.length);
|
|
264
|
+
MAGIC_CTR.copy(out2, 0);
|
|
265
|
+
iv2.copy(out2, MAGIC_LEN);
|
|
266
|
+
enc3.copy(out2, MAGIC_LEN + CTR_IV_BYTES);
|
|
267
|
+
if (fin.length > 0) fin.copy(out2, MAGIC_LEN + CTR_IV_BYTES + enc3.length);
|
|
268
|
+
return out2;
|
|
269
|
+
}
|
|
270
|
+
const magic = this._mode === "aes-128-gcm" ? MAGIC_128 : MAGIC_256;
|
|
271
|
+
const algo = this._mode === "aes-128-gcm" ? "aes-128-gcm" : "aes-256-gcm";
|
|
272
|
+
const iv = this._nextIV();
|
|
273
|
+
const c = (0, import_crypto.createCipheriv)(algo, this._keyObj, iv);
|
|
274
|
+
const enc2 = c.update(data);
|
|
275
|
+
c.final();
|
|
276
|
+
const out = Buffer.allocUnsafe(MAGIC_LEN + IV_BYTES + TAG_BYTES + enc2.length);
|
|
277
|
+
magic.copy(out, 0);
|
|
278
|
+
iv.copy(out, MAGIC_LEN);
|
|
279
|
+
c.getAuthTag().copy(out, MAGIC_LEN + IV_BYTES);
|
|
280
|
+
enc2.copy(out, MAGIC_LEN + IV_BYTES + TAG_BYTES);
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Decrypt a raw Buffer from disk/snapshot.
|
|
285
|
+
* Auto-detects the magic header; returns the original buffer if no magic matches (legacy/unencrypted).
|
|
286
|
+
* Falls back to `previousEncryptionKey` when the primary key fails (key rotation).
|
|
287
|
+
*/
|
|
288
|
+
decryptBuffer(data) {
|
|
289
|
+
try {
|
|
290
|
+
return this._decryptBufferWithKey(data, this._key, this._keyObj);
|
|
291
|
+
} catch (primaryErr) {
|
|
292
|
+
if (this._prevKey) {
|
|
293
|
+
try {
|
|
294
|
+
return this._decryptBufferWithKey(data, this._prevKey, this._prevKeyObj);
|
|
295
|
+
} catch {
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
throw primaryErr;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
_decryptBufferWithKey(data, key, keyObj) {
|
|
302
|
+
if (data.length < MAGIC_LEN) return data;
|
|
303
|
+
const magic = data.subarray(0, MAGIC_LEN);
|
|
304
|
+
if (magic.equals(MAGIC_XOR)) {
|
|
305
|
+
if (!key) throw new Error("Cannot decrypt buffer: encryption key is not set");
|
|
306
|
+
const raw = data.subarray(MAGIC_LEN);
|
|
307
|
+
const klen = key.length;
|
|
308
|
+
const out = Buffer.allocUnsafe(raw.length);
|
|
309
|
+
for (let i = 0; i < raw.length; i++) out[i] = raw[i] ^ key[i % klen];
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
if (magic.equals(MAGIC_CTR)) {
|
|
313
|
+
if (!keyObj) throw new Error("Cannot decrypt buffer: encryption key is not set");
|
|
314
|
+
const iv2 = data.subarray(MAGIC_LEN, MAGIC_LEN + CTR_IV_BYTES);
|
|
315
|
+
const ct2 = data.subarray(MAGIC_LEN + CTR_IV_BYTES);
|
|
316
|
+
const d2 = (0, import_crypto.createDecipheriv)("aes-128-ctr", keyObj, iv2);
|
|
317
|
+
const plain2 = d2.update(ct2);
|
|
318
|
+
const fin = d2.final();
|
|
319
|
+
return fin.length > 0 ? Buffer.concat([plain2, fin]) : plain2;
|
|
320
|
+
}
|
|
321
|
+
const is256 = magic.equals(MAGIC_256);
|
|
322
|
+
const is128 = magic.equals(MAGIC_128);
|
|
323
|
+
if (!is256 && !is128) return data;
|
|
324
|
+
if (!keyObj) throw new Error("Cannot decrypt buffer: encryption key is not set");
|
|
325
|
+
const algo = is128 ? "aes-128-gcm" : "aes-256-gcm";
|
|
326
|
+
const iv = data.subarray(MAGIC_LEN, MAGIC_LEN + IV_BYTES);
|
|
327
|
+
const tag = data.subarray(MAGIC_LEN + IV_BYTES, MAGIC_LEN + IV_BYTES + TAG_BYTES);
|
|
328
|
+
const ct = data.subarray(MAGIC_LEN + IV_BYTES + TAG_BYTES);
|
|
329
|
+
const d = (0, import_crypto.createDecipheriv)(algo, keyObj, iv);
|
|
330
|
+
d.setAuthTag(tag);
|
|
331
|
+
const plain = d.update(ct);
|
|
332
|
+
d.final();
|
|
333
|
+
return plain;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/serialize-worker.ts
|
|
338
|
+
var silentLogger = {
|
|
339
|
+
debug: () => {
|
|
340
|
+
},
|
|
341
|
+
info: () => {
|
|
342
|
+
},
|
|
343
|
+
warn: () => {
|
|
344
|
+
},
|
|
345
|
+
error: () => {
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
var { keyBase64, mode, prevKeyBase64, prevMode } = import_worker_threads.workerData;
|
|
349
|
+
var enc = new CacheEncryption(
|
|
350
|
+
keyBase64 || void 0,
|
|
351
|
+
silentLogger,
|
|
352
|
+
mode,
|
|
353
|
+
prevKeyBase64 || void 0,
|
|
354
|
+
prevMode
|
|
355
|
+
);
|
|
356
|
+
import_worker_threads.parentPort.on("message", ({ id, type, payload }) => {
|
|
357
|
+
try {
|
|
358
|
+
if (type === "encrypt") {
|
|
359
|
+
import_worker_threads.parentPort.postMessage({ id, result: enc.encrypt(payload) });
|
|
360
|
+
} else {
|
|
361
|
+
import_worker_threads.parentPort.postMessage({ id, result: enc.decrypt(payload) });
|
|
362
|
+
}
|
|
363
|
+
} catch (e) {
|
|
364
|
+
import_worker_threads.parentPort.postMessage({ id, error: e.message });
|
|
365
|
+
}
|
|
366
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CacheEncryption
|
|
3
|
+
} from "./chunk-F24NEZGQ.js";
|
|
4
|
+
|
|
5
|
+
// src/serialize-worker.ts
|
|
6
|
+
import { parentPort, workerData } from "worker_threads";
|
|
7
|
+
var silentLogger = {
|
|
8
|
+
debug: () => {
|
|
9
|
+
},
|
|
10
|
+
info: () => {
|
|
11
|
+
},
|
|
12
|
+
warn: () => {
|
|
13
|
+
},
|
|
14
|
+
error: () => {
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var { keyBase64, mode, prevKeyBase64, prevMode } = workerData;
|
|
18
|
+
var enc = new CacheEncryption(
|
|
19
|
+
keyBase64 || void 0,
|
|
20
|
+
silentLogger,
|
|
21
|
+
mode,
|
|
22
|
+
prevKeyBase64 || void 0,
|
|
23
|
+
prevMode
|
|
24
|
+
);
|
|
25
|
+
parentPort.on("message", ({ id, type, payload }) => {
|
|
26
|
+
try {
|
|
27
|
+
if (type === "encrypt") {
|
|
28
|
+
parentPort.postMessage({ id, result: enc.encrypt(payload) });
|
|
29
|
+
} else {
|
|
30
|
+
parentPort.postMessage({ id, result: enc.decrypt(payload) });
|
|
31
|
+
}
|
|
32
|
+
} catch (e) {
|
|
33
|
+
parentPort.postMessage({ id, error: e.message });
|
|
34
|
+
}
|
|
35
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tricache",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
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",
|
|
@@ -37,16 +37,16 @@
|
|
|
37
37
|
"LICENSE"
|
|
38
38
|
],
|
|
39
39
|
"scripts": {
|
|
40
|
-
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
41
|
-
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
40
|
+
"build": "tsup src/index.ts src/serialize-worker.ts --format esm,cjs --dts --clean",
|
|
41
|
+
"dev": "tsup src/index.ts src/serialize-worker.ts --format esm,cjs --dts --watch",
|
|
42
42
|
"typecheck": "tsc --noEmit",
|
|
43
43
|
"test": "vitest run",
|
|
44
44
|
"test:watch": "vitest",
|
|
45
45
|
"bench": "node --expose-gc --import tsx/esm bench/benchmark.ts"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"ioredis": "^5.
|
|
49
|
-
"msgpackr": "^2.0.
|
|
48
|
+
"ioredis": "^5.11.0",
|
|
49
|
+
"msgpackr": "^2.0.2"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/node": "^24.0.0",
|