@frontmcp/plugin-cache 1.8.7 → 1.9.1-rc.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/cache.errors.d.ts +13 -0
- package/cache.errors.d.ts.map +1 -0
- package/cache.plugin.d.ts.map +1 -1
- package/cache.types.d.ts +6 -3
- package/cache.types.d.ts.map +1 -1
- package/esm/index.mjs +171 -95
- package/esm/package.json +3 -3
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +170 -96
- package/package.json +3 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error classes for the cache plugin.
|
|
3
|
+
*
|
|
4
|
+
* @module @frontmcp/plugin-cache
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Raised when `CachePlugin` is configured with options it cannot honour, when the plugin is
|
|
8
|
+
* created (`CachePlugin.init()`), so the server never starts with a setting that silently does nothing.
|
|
9
|
+
*/
|
|
10
|
+
export declare class CachePluginConfigurationError extends Error {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=cache.errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.errors.d.ts","sourceRoot":"","sources":["../src/cache.errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,qBAAa,6BAA8B,SAAQ,KAAK;gBAC1C,OAAO,EAAE,MAAM;CAI5B"}
|
package/cache.plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.plugin.d.ts","sourceRoot":"","sources":["../src/cache.plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAOb,KAAK,SAAS,EAEd,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"cache.plugin.d.ts","sourceRoot":"","sources":["../src/cache.plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAOb,KAAK,SAAS,EAEd,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;AAKvB,OAAO,EAAE,KAAK,kBAAkB,EAAsC,MAAM,eAAe,CAAC;AAkE5F,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,aAAa,CAAC,kBAAkB,CAAC;IACxE,OAAgB,gBAAgB,GAAI,SAAS,kBAAkB,oBAuD7D;IAEF,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAEvC;IACF,OAAO,EAAE,kBAAkB,CAAC;gBAEhB,OAAO,GAAE,kBAA+C;IASpE;;OAEG;IACH,OAAO,CAAC,eAAe;IAkBvB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;OAEG;IACH,OAAO,CAAC,MAAM;IAQR,aAAa,CAAC,OAAO,EAAE,SAAS,CAAC,iBAAiB,CAAC;IA4DnD,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,iBAAiB,CAAC;IAgC1D;;;;;;;;;OASG;IACH,OAAO,CAAC,qBAAqB;IAyB7B;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAQrB;;;OAGG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;CAGvC"}
|
package/cache.types.d.ts
CHANGED
|
@@ -10,7 +10,9 @@ export interface CachePluginToolOptions {
|
|
|
10
10
|
*/
|
|
11
11
|
ttl?: number;
|
|
12
12
|
/**
|
|
13
|
-
* If true,
|
|
13
|
+
* If true, every cache hit refreshes the entry's TTL (`ttl`, or the plugin's `defaultTTL`), so an
|
|
14
|
+
* entry that keeps being read stays cached. Without it an entry expires `ttl` seconds after it was
|
|
15
|
+
* written, however often it is read. `cache: true` does not slide.
|
|
14
16
|
* Default is false.
|
|
15
17
|
*/
|
|
16
18
|
slideWindow?: boolean;
|
|
@@ -35,8 +37,9 @@ export interface BaseCachePluginOptions {
|
|
|
35
37
|
* HTTP header name that clients can send to bypass cache for a specific request.
|
|
36
38
|
* When this header is present with a truthy value ('true' or '1'), cache read/write is skipped.
|
|
37
39
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
+
* Must start with `x-frontmcp-`: the plugin reads the header from the request context, which
|
|
41
|
+
* keeps only a request's `x-frontmcp-*` headers. Any other name throws
|
|
42
|
+
* `CachePluginConfigurationError` when the plugin is created.
|
|
40
43
|
*
|
|
41
44
|
* @default 'x-frontmcp-disable-cache'
|
|
42
45
|
*
|
package/cache.types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.types.d.ts","sourceRoot":"","sources":["../src/cache.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,0BAA0B;QAClC,KAAK,CAAC,EAAE,sBAAsB,GAAG,IAAI,CAAC;KACvC;CACF;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb
|
|
1
|
+
{"version":3,"file":"cache.types.d.ts","sourceRoot":"","sources":["../src/cache.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,0BAA0B;QAClC,KAAK,CAAC,EAAE,sBAAsB,GAAG,IAAI,CAAC;KACvC;CACF;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;;;OAKG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IAExB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;;;;OAYG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,6BAA8B,SAAQ,sBAAsB;IAC3E,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,uBAAwB,SAAQ,sBAAsB;IACrE,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,EAAE,CAAC,EAAE,MAAM,CAAC;KACb,CAAC;CACH;AAED,MAAM,MAAM,wBAAwB,GAAG,sBAAsB,GAAG;IAC9D,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,6BAA8B,SAAQ,sBAAsB;IAC3E,IAAI,EAAE,cAAc,CAAC;CACtB;AAED,MAAM,MAAM,iBAAiB,GAAG,6BAA6B,GAAG,uBAAuB,CAAC;AAExF,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,iBAAiB,GAAG,6BAA6B,CAAC;AAE9G,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1E,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAE7E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
|
package/esm/index.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : undefined;
|
|
2
|
+
const require = __frontmcpModule ? __frontmcpModule.createRequire(import.meta.url || 'file:///') : undefined;
|
|
3
|
+
// __frontmcpCreateRequire
|
|
1
4
|
var __create = Object.create;
|
|
2
5
|
var __defProp = Object.defineProperty;
|
|
3
6
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -307,7 +310,6 @@ import { hkdf } from "@noble/hashes/hkdf.js";
|
|
|
307
310
|
import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils.js";
|
|
308
311
|
import { gcm } from "@noble/ciphers/aes.js";
|
|
309
312
|
import { analyzeForReDoS, REDOS_THRESHOLDS } from "@enclave-vm/ast";
|
|
310
|
-
import { z as z3 } from "@frontmcp/lazy-zod";
|
|
311
313
|
var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : void 0;
|
|
312
314
|
var require2 = __frontmcpModule ? __frontmcpModule.createRequire(import.meta.url || "file:///") : void 0;
|
|
313
315
|
var __defProp2 = Object.defineProperty;
|
|
@@ -645,8 +647,8 @@ var init_key_persistence = __esm({
|
|
|
645
647
|
*/
|
|
646
648
|
async get(kid) {
|
|
647
649
|
if (this.enableCache) {
|
|
648
|
-
const
|
|
649
|
-
if (
|
|
650
|
+
const cached = this.cache.get(kid);
|
|
651
|
+
if (cached) return cached;
|
|
650
652
|
}
|
|
651
653
|
const raw = await this.storage.get(kid);
|
|
652
654
|
if (!raw) return null;
|
|
@@ -902,6 +904,61 @@ var init_errors = __esm({
|
|
|
902
904
|
};
|
|
903
905
|
}
|
|
904
906
|
});
|
|
907
|
+
function globToRegex(pattern) {
|
|
908
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
909
|
+
throw new StoragePatternError(
|
|
910
|
+
pattern.substring(0, 50) + "...",
|
|
911
|
+
`Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
|
|
912
|
+
);
|
|
913
|
+
}
|
|
914
|
+
const wildcardCount = (pattern.match(/[*?]/g) || []).length;
|
|
915
|
+
if (wildcardCount > MAX_WILDCARDS) {
|
|
916
|
+
throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
|
|
917
|
+
}
|
|
918
|
+
if (pattern === "" || pattern === "*") {
|
|
919
|
+
return /^.*$/;
|
|
920
|
+
}
|
|
921
|
+
let regexStr = "^";
|
|
922
|
+
let prevChar = "";
|
|
923
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
924
|
+
const char = pattern[i];
|
|
925
|
+
switch (char) {
|
|
926
|
+
case "*":
|
|
927
|
+
if (prevChar !== "*") {
|
|
928
|
+
regexStr += ".*";
|
|
929
|
+
}
|
|
930
|
+
break;
|
|
931
|
+
case "?":
|
|
932
|
+
regexStr += ".";
|
|
933
|
+
break;
|
|
934
|
+
default:
|
|
935
|
+
regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
|
|
936
|
+
}
|
|
937
|
+
prevChar = char;
|
|
938
|
+
}
|
|
939
|
+
regexStr += "$";
|
|
940
|
+
try {
|
|
941
|
+
return new RegExp(regexStr);
|
|
942
|
+
} catch {
|
|
943
|
+
throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
function matchesPattern(key, pattern) {
|
|
947
|
+
const regex = globToRegex(pattern);
|
|
948
|
+
return regex.test(key);
|
|
949
|
+
}
|
|
950
|
+
var MAX_PATTERN_LENGTH;
|
|
951
|
+
var MAX_WILDCARDS;
|
|
952
|
+
var REGEX_SPECIAL_CHARS;
|
|
953
|
+
var init_pattern = __esm({
|
|
954
|
+
"libs/utils/src/storage/utils/pattern.ts"() {
|
|
955
|
+
"use strict";
|
|
956
|
+
init_errors();
|
|
957
|
+
MAX_PATTERN_LENGTH = 500;
|
|
958
|
+
MAX_WILDCARDS = 20;
|
|
959
|
+
REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
|
|
960
|
+
}
|
|
961
|
+
});
|
|
905
962
|
function validateTTL(ttlSeconds) {
|
|
906
963
|
if (typeof ttlSeconds !== "number") {
|
|
907
964
|
throw new StorageTTLError(ttlSeconds, `TTL must be a number, got ${typeof ttlSeconds}`);
|
|
@@ -1010,6 +1067,17 @@ var init_base = __esm({
|
|
|
1010
1067
|
const matchedKeys = await this.keys(pattern);
|
|
1011
1068
|
return matchedKeys.length;
|
|
1012
1069
|
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Default: not supported, since a read followed by a delete is not atomic.
|
|
1072
|
+
* Override in adapters whose backend can compare and delete in one step.
|
|
1073
|
+
*/
|
|
1074
|
+
async deleteIfEquals(_key, _expectedValue) {
|
|
1075
|
+
throw new StorageNotSupportedError(
|
|
1076
|
+
"deleteIfEquals",
|
|
1077
|
+
this.backendName,
|
|
1078
|
+
"Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-delete."
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1013
1081
|
// ============================================
|
|
1014
1082
|
// Pub/Sub (default: not supported)
|
|
1015
1083
|
// ============================================
|
|
@@ -1057,71 +1125,16 @@ var init_base = __esm({
|
|
|
1057
1125
|
};
|
|
1058
1126
|
}
|
|
1059
1127
|
});
|
|
1060
|
-
function globToRegex(pattern) {
|
|
1061
|
-
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
1062
|
-
throw new StoragePatternError(
|
|
1063
|
-
pattern.substring(0, 50) + "...",
|
|
1064
|
-
`Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
|
|
1065
|
-
);
|
|
1066
|
-
}
|
|
1067
|
-
const wildcardCount = (pattern.match(/[*?]/g) || []).length;
|
|
1068
|
-
if (wildcardCount > MAX_WILDCARDS) {
|
|
1069
|
-
throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
|
|
1070
|
-
}
|
|
1071
|
-
if (pattern === "" || pattern === "*") {
|
|
1072
|
-
return /^.*$/;
|
|
1073
|
-
}
|
|
1074
|
-
let regexStr = "^";
|
|
1075
|
-
let prevChar = "";
|
|
1076
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
1077
|
-
const char = pattern[i];
|
|
1078
|
-
switch (char) {
|
|
1079
|
-
case "*":
|
|
1080
|
-
if (prevChar !== "*") {
|
|
1081
|
-
regexStr += ".*";
|
|
1082
|
-
}
|
|
1083
|
-
break;
|
|
1084
|
-
case "?":
|
|
1085
|
-
regexStr += ".";
|
|
1086
|
-
break;
|
|
1087
|
-
default:
|
|
1088
|
-
regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
|
|
1089
|
-
}
|
|
1090
|
-
prevChar = char;
|
|
1091
|
-
}
|
|
1092
|
-
regexStr += "$";
|
|
1093
|
-
try {
|
|
1094
|
-
return new RegExp(regexStr);
|
|
1095
|
-
} catch {
|
|
1096
|
-
throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
function matchesPattern(key, pattern) {
|
|
1100
|
-
const regex = globToRegex(pattern);
|
|
1101
|
-
return regex.test(key);
|
|
1102
|
-
}
|
|
1103
|
-
var MAX_PATTERN_LENGTH;
|
|
1104
|
-
var MAX_WILDCARDS;
|
|
1105
|
-
var REGEX_SPECIAL_CHARS;
|
|
1106
|
-
var init_pattern = __esm({
|
|
1107
|
-
"libs/utils/src/storage/utils/pattern.ts"() {
|
|
1108
|
-
"use strict";
|
|
1109
|
-
init_errors();
|
|
1110
|
-
MAX_PATTERN_LENGTH = 500;
|
|
1111
|
-
MAX_WILDCARDS = 20;
|
|
1112
|
-
REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
|
|
1113
|
-
}
|
|
1114
|
-
});
|
|
1115
1128
|
var DEFAULT_SWEEP_INTERVAL_SECONDS;
|
|
1116
1129
|
var MAX_TIMEOUT_MS;
|
|
1117
1130
|
var MemoryStorageAdapter;
|
|
1118
1131
|
var init_memory = __esm({
|
|
1119
1132
|
"libs/utils/src/storage/adapters/memory.ts"() {
|
|
1120
1133
|
"use strict";
|
|
1121
|
-
init_base();
|
|
1122
1134
|
init_errors();
|
|
1123
1135
|
init_pattern();
|
|
1124
1136
|
init_ttl();
|
|
1137
|
+
init_base();
|
|
1125
1138
|
DEFAULT_SWEEP_INTERVAL_SECONDS = 60;
|
|
1126
1139
|
MAX_TIMEOUT_MS = 2147483647;
|
|
1127
1140
|
MemoryStorageAdapter = class extends BaseStorageAdapter {
|
|
@@ -1213,6 +1226,12 @@ var init_memory = __esm({
|
|
|
1213
1226
|
this.ensureConnected();
|
|
1214
1227
|
return this.deleteEntry(key);
|
|
1215
1228
|
}
|
|
1229
|
+
async deleteIfEquals(key, expectedValue) {
|
|
1230
|
+
this.ensureConnected();
|
|
1231
|
+
const entry = this.store.get(key);
|
|
1232
|
+
if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
|
|
1233
|
+
return this.deleteEntry(key);
|
|
1234
|
+
}
|
|
1216
1235
|
async exists(key) {
|
|
1217
1236
|
this.ensureConnected();
|
|
1218
1237
|
const entry = this.store.get(key);
|
|
@@ -2584,6 +2603,13 @@ var init_utils = __esm({
|
|
|
2584
2603
|
init_ttl();
|
|
2585
2604
|
}
|
|
2586
2605
|
});
|
|
2606
|
+
var COMPARE_AND_DELETE_SCRIPT;
|
|
2607
|
+
var init_compare_and_delete = __esm({
|
|
2608
|
+
"libs/utils/src/storage/utils/compare-and-delete.ts"() {
|
|
2609
|
+
"use strict";
|
|
2610
|
+
COMPARE_AND_DELETE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`;
|
|
2611
|
+
}
|
|
2612
|
+
});
|
|
2587
2613
|
var redis_exports = {};
|
|
2588
2614
|
__export(redis_exports, {
|
|
2589
2615
|
RedisStorageAdapter: () => RedisStorageAdapter,
|
|
@@ -2626,6 +2652,7 @@ var init_redis = __esm({
|
|
|
2626
2652
|
init_errors();
|
|
2627
2653
|
init_redis_error_listener();
|
|
2628
2654
|
init_utils();
|
|
2655
|
+
init_compare_and_delete();
|
|
2629
2656
|
init_base();
|
|
2630
2657
|
RedisStorageAdapter = class extends BaseStorageAdapter {
|
|
2631
2658
|
backendName = "redis";
|
|
@@ -2780,6 +2807,15 @@ var init_redis = __esm({
|
|
|
2780
2807
|
const result = await this.getConnectedClient().del(this.prefixKey(key));
|
|
2781
2808
|
return result > 0;
|
|
2782
2809
|
}
|
|
2810
|
+
async deleteIfEquals(key, expectedValue) {
|
|
2811
|
+
const deleted = await this.getConnectedClient().eval(
|
|
2812
|
+
COMPARE_AND_DELETE_SCRIPT,
|
|
2813
|
+
1,
|
|
2814
|
+
this.prefixKey(key),
|
|
2815
|
+
expectedValue
|
|
2816
|
+
);
|
|
2817
|
+
return deleted === 1;
|
|
2818
|
+
}
|
|
2783
2819
|
async exists(key) {
|
|
2784
2820
|
const result = await this.getConnectedClient().exists(this.prefixKey(key));
|
|
2785
2821
|
return result > 0;
|
|
@@ -2986,9 +3022,12 @@ var vercel_kv_exports = {};
|
|
|
2986
3022
|
__export(vercel_kv_exports, {
|
|
2987
3023
|
VercelKvStorageAdapter: () => VercelKvStorageAdapter
|
|
2988
3024
|
});
|
|
2989
|
-
function
|
|
3025
|
+
async function loadVercelKv() {
|
|
2990
3026
|
try {
|
|
2991
|
-
|
|
3027
|
+
const mod = await import("@vercel/kv");
|
|
3028
|
+
const resolved = typeof mod.createClient === "function" ? mod : mod.default;
|
|
3029
|
+
if (!resolved || typeof resolved.createClient !== "function") throw new Error("createClient missing");
|
|
3030
|
+
return resolved;
|
|
2992
3031
|
} catch {
|
|
2993
3032
|
throw new Error("@vercel/kv is required for Vercel KV storage adapter. Install it with: npm install @vercel/kv");
|
|
2994
3033
|
}
|
|
@@ -2997,9 +3036,10 @@ var VercelKvStorageAdapter;
|
|
|
2997
3036
|
var init_vercel_kv = __esm({
|
|
2998
3037
|
"libs/utils/src/storage/adapters/vercel-kv.ts"() {
|
|
2999
3038
|
"use strict";
|
|
3000
|
-
init_base();
|
|
3001
3039
|
init_errors();
|
|
3040
|
+
init_compare_and_delete();
|
|
3002
3041
|
init_ttl();
|
|
3042
|
+
init_base();
|
|
3003
3043
|
VercelKvStorageAdapter = class extends BaseStorageAdapter {
|
|
3004
3044
|
backendName = "vercel-kv";
|
|
3005
3045
|
client;
|
|
@@ -3024,17 +3064,13 @@ var init_vercel_kv = __esm({
|
|
|
3024
3064
|
async connect() {
|
|
3025
3065
|
if (this.connected) return;
|
|
3026
3066
|
try {
|
|
3027
|
-
const { createClient
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
const token = this.options.token;
|
|
3033
|
-
if (!url || !token) {
|
|
3034
|
-
throw new StorageConfigError("vercel-kv", "URL and token are required");
|
|
3035
|
-
}
|
|
3036
|
-
this.client = createClient({ url, token });
|
|
3067
|
+
const { createClient } = await loadVercelKv();
|
|
3068
|
+
const url = this.options.url;
|
|
3069
|
+
const token = this.options.token;
|
|
3070
|
+
if (!url || !token) {
|
|
3071
|
+
throw new StorageConfigError("vercel-kv", "URL and token are required");
|
|
3037
3072
|
}
|
|
3073
|
+
this.client = createClient({ url, token, cache: void 0, automaticDeserialization: false });
|
|
3038
3074
|
await this.client.exists("__healthcheck__");
|
|
3039
3075
|
this.connected = true;
|
|
3040
3076
|
} catch (e) {
|
|
@@ -3075,8 +3111,7 @@ var init_vercel_kv = __esm({
|
|
|
3075
3111
|
// Core Operations
|
|
3076
3112
|
// ============================================
|
|
3077
3113
|
async get(key) {
|
|
3078
|
-
|
|
3079
|
-
return result;
|
|
3114
|
+
return this.getConnectedClient().get(this.prefixKey(key));
|
|
3080
3115
|
}
|
|
3081
3116
|
async doSet(key, value, options) {
|
|
3082
3117
|
const client = this.getConnectedClient();
|
|
@@ -3096,6 +3131,14 @@ var init_vercel_kv = __esm({
|
|
|
3096
3131
|
const result = await this.getConnectedClient().del(this.prefixKey(key));
|
|
3097
3132
|
return result > 0;
|
|
3098
3133
|
}
|
|
3134
|
+
async deleteIfEquals(key, expectedValue) {
|
|
3135
|
+
const deleted = await this.getConnectedClient().eval(
|
|
3136
|
+
COMPARE_AND_DELETE_SCRIPT,
|
|
3137
|
+
[this.prefixKey(key)],
|
|
3138
|
+
[expectedValue]
|
|
3139
|
+
);
|
|
3140
|
+
return deleted === 1;
|
|
3141
|
+
}
|
|
3099
3142
|
async exists(key) {
|
|
3100
3143
|
const result = await this.getConnectedClient().exists(this.prefixKey(key));
|
|
3101
3144
|
return result > 0;
|
|
@@ -3212,9 +3255,10 @@ var UpstashStorageAdapter;
|
|
|
3212
3255
|
var init_upstash = __esm({
|
|
3213
3256
|
"libs/utils/src/storage/adapters/upstash.ts"() {
|
|
3214
3257
|
"use strict";
|
|
3215
|
-
init_base();
|
|
3216
3258
|
init_errors();
|
|
3259
|
+
init_compare_and_delete();
|
|
3217
3260
|
init_ttl();
|
|
3261
|
+
init_base();
|
|
3218
3262
|
PUBSUB_POLL_INTERVAL_MS = 100;
|
|
3219
3263
|
UpstashStorageAdapter = class extends BaseStorageAdapter {
|
|
3220
3264
|
backendName = "upstash";
|
|
@@ -3251,7 +3295,7 @@ var init_upstash = __esm({
|
|
|
3251
3295
|
if (!url || !token) {
|
|
3252
3296
|
throw new StorageConfigError("upstash", "URL and token are required");
|
|
3253
3297
|
}
|
|
3254
|
-
this.client = new Redis2({ url, token });
|
|
3298
|
+
this.client = new Redis2({ url, token, automaticDeserialization: false });
|
|
3255
3299
|
await this.client.exists("__healthcheck__");
|
|
3256
3300
|
this.connected = true;
|
|
3257
3301
|
} catch (e) {
|
|
@@ -3298,8 +3342,7 @@ var init_upstash = __esm({
|
|
|
3298
3342
|
// Core Operations
|
|
3299
3343
|
// ============================================
|
|
3300
3344
|
async get(key) {
|
|
3301
|
-
|
|
3302
|
-
return result;
|
|
3345
|
+
return this.getConnectedClient().get(this.prefixKey(key));
|
|
3303
3346
|
}
|
|
3304
3347
|
async doSet(key, value, options) {
|
|
3305
3348
|
const client = this.getConnectedClient();
|
|
@@ -3319,6 +3362,14 @@ var init_upstash = __esm({
|
|
|
3319
3362
|
const result = await this.getConnectedClient().del(this.prefixKey(key));
|
|
3320
3363
|
return result > 0;
|
|
3321
3364
|
}
|
|
3365
|
+
async deleteIfEquals(key, expectedValue) {
|
|
3366
|
+
const deleted = await this.getConnectedClient().eval(
|
|
3367
|
+
COMPARE_AND_DELETE_SCRIPT,
|
|
3368
|
+
[this.prefixKey(key)],
|
|
3369
|
+
[expectedValue]
|
|
3370
|
+
);
|
|
3371
|
+
return deleted === 1;
|
|
3372
|
+
}
|
|
3322
3373
|
async exists(key) {
|
|
3323
3374
|
const result = await this.getConnectedClient().exists(this.prefixKey(key));
|
|
3324
3375
|
return result > 0;
|
|
@@ -3507,7 +3558,9 @@ var init_cloudflare_kv = __esm({
|
|
|
3507
3558
|
constructor(options) {
|
|
3508
3559
|
super();
|
|
3509
3560
|
if (!options?.namespace) {
|
|
3510
|
-
throw new Error(
|
|
3561
|
+
throw new Error(
|
|
3562
|
+
"CloudflareKvStorageAdapter: a bound KV `namespace` is required (resolve it from the Worker `env`)."
|
|
3563
|
+
);
|
|
3511
3564
|
}
|
|
3512
3565
|
this.kv = options.namespace;
|
|
3513
3566
|
this.keyPrefix = options.keyPrefix ?? "";
|
|
@@ -3601,6 +3654,13 @@ var init_cloudflare_kv = __esm({
|
|
|
3601
3654
|
"Cloudflare KV has no atomic increment (a get+put is racy under eventual consistency). Use a Durable Object for atomic counters."
|
|
3602
3655
|
);
|
|
3603
3656
|
}
|
|
3657
|
+
async deleteIfEquals(_key, _expectedValue) {
|
|
3658
|
+
throw new StorageNotSupportedError(
|
|
3659
|
+
"deleteIfEquals",
|
|
3660
|
+
this.backendName,
|
|
3661
|
+
"Cloudflare KV has no compare-and-set; use a Durable Object for an atomic compare-and-delete."
|
|
3662
|
+
);
|
|
3663
|
+
}
|
|
3604
3664
|
// ── Pub/Sub (unsupported) ────────────────────────────────────────────────
|
|
3605
3665
|
getPubSubSuggestion() {
|
|
3606
3666
|
return "Cloudflare KV does not support pub/sub. Use a Durable Object (or Queues) for fan-out.";
|
|
@@ -3714,16 +3774,7 @@ function compileBlocks(entries, parseNetwork) {
|
|
|
3714
3774
|
}
|
|
3715
3775
|
init_fs2();
|
|
3716
3776
|
init_crypto();
|
|
3717
|
-
|
|
3718
|
-
os: z3.array(z3.string().min(1)).optional(),
|
|
3719
|
-
platform: z3.array(z3.string().min(1)).optional(),
|
|
3720
|
-
runtime: z3.array(z3.string().min(1)).optional(),
|
|
3721
|
-
deployment: z3.array(z3.string().min(1)).optional(),
|
|
3722
|
-
provider: z3.array(z3.string().min(1)).optional(),
|
|
3723
|
-
target: z3.array(z3.string().min(1)).optional(),
|
|
3724
|
-
surface: z3.array(z3.enum(["mcp", "cli", "http-trigger", "job", "agent"])).optional(),
|
|
3725
|
-
env: z3.array(z3.string().min(1)).optional()
|
|
3726
|
-
}).strict();
|
|
3777
|
+
init_errors();
|
|
3727
3778
|
init_errors();
|
|
3728
3779
|
init_memory();
|
|
3729
3780
|
init_redis_error_listener();
|
|
@@ -3743,6 +3794,14 @@ init_ttl();
|
|
|
3743
3794
|
init_crypto();
|
|
3744
3795
|
init_runtime();
|
|
3745
3796
|
|
|
3797
|
+
// plugins/plugin-cache/src/cache.errors.ts
|
|
3798
|
+
var CachePluginConfigurationError = class extends Error {
|
|
3799
|
+
constructor(message) {
|
|
3800
|
+
super(message);
|
|
3801
|
+
this.name = "CachePluginConfigurationError";
|
|
3802
|
+
}
|
|
3803
|
+
};
|
|
3804
|
+
|
|
3746
3805
|
// plugins/plugin-cache/src/cache.symbol.ts
|
|
3747
3806
|
var CacheStoreToken = /* @__PURE__ */ Symbol("plugin:cache:store");
|
|
3748
3807
|
|
|
@@ -3974,6 +4033,15 @@ CacheVercelKvProvider = __decorateClass([
|
|
|
3974
4033
|
|
|
3975
4034
|
// plugins/plugin-cache/src/cache.plugin.ts
|
|
3976
4035
|
var DEFAULT_BYPASS_HEADER = "x-frontmcp-disable-cache";
|
|
4036
|
+
var BYPASS_HEADER_PREFIX = "x-frontmcp-";
|
|
4037
|
+
function assertReadableBypassHeader(bypassHeader) {
|
|
4038
|
+
if (bypassHeader === void 0) return;
|
|
4039
|
+
const name = bypassHeader.toLowerCase();
|
|
4040
|
+
if (name.startsWith(BYPASS_HEADER_PREFIX) && name.length > BYPASS_HEADER_PREFIX.length) return;
|
|
4041
|
+
throw new CachePluginConfigurationError(
|
|
4042
|
+
`CachePlugin bypassHeader "${bypassHeader}" is never seen by the plugin: only request headers that start with "${BYPASS_HEADER_PREFIX}" reach it. Rename it, e.g. "${BYPASS_HEADER_PREFIX}no-cache".`
|
|
4043
|
+
);
|
|
4044
|
+
}
|
|
3977
4045
|
function matchesToolPattern(toolName, patterns) {
|
|
3978
4046
|
if (!patterns || patterns.length === 0) return false;
|
|
3979
4047
|
return patterns.some((pattern) => {
|
|
@@ -3989,6 +4057,7 @@ var CachePlugin = class extends DynamicPlugin {
|
|
|
3989
4057
|
options;
|
|
3990
4058
|
constructor(options = CachePlugin.defaultOptions) {
|
|
3991
4059
|
super();
|
|
4060
|
+
assertReadableBypassHeader(options.bypassHeader);
|
|
3992
4061
|
this.options = {
|
|
3993
4062
|
defaultTTL: 60 * 60 * 24,
|
|
3994
4063
|
...options
|
|
@@ -4046,15 +4115,13 @@ var CachePlugin = class extends DynamicPlugin {
|
|
|
4046
4115
|
const hash = this.buildCacheKey(tool.fullName, toolContext);
|
|
4047
4116
|
const cached = await cacheStore.getValue(hash);
|
|
4048
4117
|
if (cached !== void 0 && cached !== null) {
|
|
4049
|
-
|
|
4050
|
-
if (cache === true || cacheConfig?.ttl && cacheConfig?.slideWindow) {
|
|
4051
|
-
const ttl = this.getTtl(cache);
|
|
4052
|
-
await cacheStore.setValue(hash, cached, ttl);
|
|
4053
|
-
}
|
|
4054
|
-
if (!tool.safeParseOutput(cached).success) {
|
|
4118
|
+
if (isErrorResult(cached) || !tool.safeParseOutput(cached).success) {
|
|
4055
4119
|
await cacheStore.delete(hash);
|
|
4056
4120
|
return;
|
|
4057
4121
|
}
|
|
4122
|
+
if (typeof cache === "object" && cache.slideWindow) {
|
|
4123
|
+
await cacheStore.setValue(hash, cached, this.getTtl(cache));
|
|
4124
|
+
}
|
|
4058
4125
|
flowCtx.state.set("resultMeta", { ...flowCtx.state.resultMeta, cache: "hit" });
|
|
4059
4126
|
flowCtx.state.set("rawOutput", cached);
|
|
4060
4127
|
toolContext.respond(cached);
|
|
@@ -4071,6 +4138,9 @@ var CachePlugin = class extends DynamicPlugin {
|
|
|
4071
4138
|
if (!shouldCache || typeof toolContext.input === "undefined") {
|
|
4072
4139
|
return;
|
|
4073
4140
|
}
|
|
4141
|
+
if (isErrorResult(toolContext.output)) {
|
|
4142
|
+
return;
|
|
4143
|
+
}
|
|
4074
4144
|
const cacheStore = this.get(CacheStoreToken);
|
|
4075
4145
|
const ttl = this.getTtl(cache);
|
|
4076
4146
|
const hash = this.buildCacheKey(tool.fullName, toolContext);
|
|
@@ -4195,6 +4265,11 @@ CachePlugin = __decorateClass([
|
|
|
4195
4265
|
]
|
|
4196
4266
|
})
|
|
4197
4267
|
], CachePlugin);
|
|
4268
|
+
function isErrorResult(output) {
|
|
4269
|
+
if (typeof output !== "object" || output === null) return false;
|
|
4270
|
+
const result = output;
|
|
4271
|
+
return result.isError === true && Array.isArray(result.content);
|
|
4272
|
+
}
|
|
4198
4273
|
function stableValue(value) {
|
|
4199
4274
|
if (Array.isArray(value)) {
|
|
4200
4275
|
return value.map(stableValue);
|
|
@@ -4210,5 +4285,6 @@ function stableValue(value) {
|
|
|
4210
4285
|
}
|
|
4211
4286
|
export {
|
|
4212
4287
|
CachePlugin,
|
|
4288
|
+
CachePluginConfigurationError,
|
|
4213
4289
|
CachePlugin as default
|
|
4214
4290
|
};
|
package/esm/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontmcp/plugin-cache",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.1-rc.1",
|
|
4
4
|
"description": "Cache plugin for FrontMCP - Redis, Vercel KV, and in-memory caching with automatic tool result caching",
|
|
5
5
|
"author": "AgentFront <info@agentfront.dev>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@frontmcp/sdk": "1.
|
|
52
|
-
"@frontmcp/utils": "1.
|
|
51
|
+
"@frontmcp/sdk": "1.9.1-rc.1",
|
|
52
|
+
"@frontmcp/utils": "1.9.1-rc.1",
|
|
53
53
|
"ioredis": "^5.8.0"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
package/index.d.ts
CHANGED
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACjE,cAAc,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACjE,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC"}
|
package/index.js
CHANGED
|
@@ -288,6 +288,7 @@ var require_node = __commonJS({
|
|
|
288
288
|
var index_exports = {};
|
|
289
289
|
__export(index_exports, {
|
|
290
290
|
CachePlugin: () => CachePlugin,
|
|
291
|
+
CachePluginConfigurationError: () => CachePluginConfigurationError,
|
|
291
292
|
default: () => CachePlugin
|
|
292
293
|
});
|
|
293
294
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -307,7 +308,6 @@ var import_utils = require("@noble/hashes/utils.js");
|
|
|
307
308
|
var import_aes = require("@noble/ciphers/aes.js");
|
|
308
309
|
var import_crypto_provider = __toESM(require_node(), 1);
|
|
309
310
|
var import_ast = require("@enclave-vm/ast");
|
|
310
|
-
var import_lazy_zod3 = require("@frontmcp/lazy-zod");
|
|
311
311
|
var import_meta = {};
|
|
312
312
|
var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : void 0;
|
|
313
313
|
var require2 = __frontmcpModule ? __frontmcpModule.createRequire(import_meta.url || "file:///") : void 0;
|
|
@@ -646,8 +646,8 @@ var init_key_persistence = __esm({
|
|
|
646
646
|
*/
|
|
647
647
|
async get(kid) {
|
|
648
648
|
if (this.enableCache) {
|
|
649
|
-
const
|
|
650
|
-
if (
|
|
649
|
+
const cached = this.cache.get(kid);
|
|
650
|
+
if (cached) return cached;
|
|
651
651
|
}
|
|
652
652
|
const raw = await this.storage.get(kid);
|
|
653
653
|
if (!raw) return null;
|
|
@@ -903,6 +903,61 @@ var init_errors = __esm({
|
|
|
903
903
|
};
|
|
904
904
|
}
|
|
905
905
|
});
|
|
906
|
+
function globToRegex(pattern) {
|
|
907
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
908
|
+
throw new StoragePatternError(
|
|
909
|
+
pattern.substring(0, 50) + "...",
|
|
910
|
+
`Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
|
|
911
|
+
);
|
|
912
|
+
}
|
|
913
|
+
const wildcardCount = (pattern.match(/[*?]/g) || []).length;
|
|
914
|
+
if (wildcardCount > MAX_WILDCARDS) {
|
|
915
|
+
throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
|
|
916
|
+
}
|
|
917
|
+
if (pattern === "" || pattern === "*") {
|
|
918
|
+
return /^.*$/;
|
|
919
|
+
}
|
|
920
|
+
let regexStr = "^";
|
|
921
|
+
let prevChar = "";
|
|
922
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
923
|
+
const char = pattern[i];
|
|
924
|
+
switch (char) {
|
|
925
|
+
case "*":
|
|
926
|
+
if (prevChar !== "*") {
|
|
927
|
+
regexStr += ".*";
|
|
928
|
+
}
|
|
929
|
+
break;
|
|
930
|
+
case "?":
|
|
931
|
+
regexStr += ".";
|
|
932
|
+
break;
|
|
933
|
+
default:
|
|
934
|
+
regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
|
|
935
|
+
}
|
|
936
|
+
prevChar = char;
|
|
937
|
+
}
|
|
938
|
+
regexStr += "$";
|
|
939
|
+
try {
|
|
940
|
+
return new RegExp(regexStr);
|
|
941
|
+
} catch {
|
|
942
|
+
throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
function matchesPattern(key, pattern) {
|
|
946
|
+
const regex = globToRegex(pattern);
|
|
947
|
+
return regex.test(key);
|
|
948
|
+
}
|
|
949
|
+
var MAX_PATTERN_LENGTH;
|
|
950
|
+
var MAX_WILDCARDS;
|
|
951
|
+
var REGEX_SPECIAL_CHARS;
|
|
952
|
+
var init_pattern = __esm({
|
|
953
|
+
"libs/utils/src/storage/utils/pattern.ts"() {
|
|
954
|
+
"use strict";
|
|
955
|
+
init_errors();
|
|
956
|
+
MAX_PATTERN_LENGTH = 500;
|
|
957
|
+
MAX_WILDCARDS = 20;
|
|
958
|
+
REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
|
|
959
|
+
}
|
|
960
|
+
});
|
|
906
961
|
function validateTTL(ttlSeconds) {
|
|
907
962
|
if (typeof ttlSeconds !== "number") {
|
|
908
963
|
throw new StorageTTLError(ttlSeconds, `TTL must be a number, got ${typeof ttlSeconds}`);
|
|
@@ -1011,6 +1066,17 @@ var init_base = __esm({
|
|
|
1011
1066
|
const matchedKeys = await this.keys(pattern);
|
|
1012
1067
|
return matchedKeys.length;
|
|
1013
1068
|
}
|
|
1069
|
+
/**
|
|
1070
|
+
* Default: not supported, since a read followed by a delete is not atomic.
|
|
1071
|
+
* Override in adapters whose backend can compare and delete in one step.
|
|
1072
|
+
*/
|
|
1073
|
+
async deleteIfEquals(_key, _expectedValue) {
|
|
1074
|
+
throw new StorageNotSupportedError(
|
|
1075
|
+
"deleteIfEquals",
|
|
1076
|
+
this.backendName,
|
|
1077
|
+
"Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-delete."
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1014
1080
|
// ============================================
|
|
1015
1081
|
// Pub/Sub (default: not supported)
|
|
1016
1082
|
// ============================================
|
|
@@ -1058,71 +1124,16 @@ var init_base = __esm({
|
|
|
1058
1124
|
};
|
|
1059
1125
|
}
|
|
1060
1126
|
});
|
|
1061
|
-
function globToRegex(pattern) {
|
|
1062
|
-
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
1063
|
-
throw new StoragePatternError(
|
|
1064
|
-
pattern.substring(0, 50) + "...",
|
|
1065
|
-
`Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
|
|
1066
|
-
);
|
|
1067
|
-
}
|
|
1068
|
-
const wildcardCount = (pattern.match(/[*?]/g) || []).length;
|
|
1069
|
-
if (wildcardCount > MAX_WILDCARDS) {
|
|
1070
|
-
throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
|
|
1071
|
-
}
|
|
1072
|
-
if (pattern === "" || pattern === "*") {
|
|
1073
|
-
return /^.*$/;
|
|
1074
|
-
}
|
|
1075
|
-
let regexStr = "^";
|
|
1076
|
-
let prevChar = "";
|
|
1077
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
1078
|
-
const char = pattern[i];
|
|
1079
|
-
switch (char) {
|
|
1080
|
-
case "*":
|
|
1081
|
-
if (prevChar !== "*") {
|
|
1082
|
-
regexStr += ".*";
|
|
1083
|
-
}
|
|
1084
|
-
break;
|
|
1085
|
-
case "?":
|
|
1086
|
-
regexStr += ".";
|
|
1087
|
-
break;
|
|
1088
|
-
default:
|
|
1089
|
-
regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
|
|
1090
|
-
}
|
|
1091
|
-
prevChar = char;
|
|
1092
|
-
}
|
|
1093
|
-
regexStr += "$";
|
|
1094
|
-
try {
|
|
1095
|
-
return new RegExp(regexStr);
|
|
1096
|
-
} catch {
|
|
1097
|
-
throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
function matchesPattern(key, pattern) {
|
|
1101
|
-
const regex = globToRegex(pattern);
|
|
1102
|
-
return regex.test(key);
|
|
1103
|
-
}
|
|
1104
|
-
var MAX_PATTERN_LENGTH;
|
|
1105
|
-
var MAX_WILDCARDS;
|
|
1106
|
-
var REGEX_SPECIAL_CHARS;
|
|
1107
|
-
var init_pattern = __esm({
|
|
1108
|
-
"libs/utils/src/storage/utils/pattern.ts"() {
|
|
1109
|
-
"use strict";
|
|
1110
|
-
init_errors();
|
|
1111
|
-
MAX_PATTERN_LENGTH = 500;
|
|
1112
|
-
MAX_WILDCARDS = 20;
|
|
1113
|
-
REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
|
|
1114
|
-
}
|
|
1115
|
-
});
|
|
1116
1127
|
var DEFAULT_SWEEP_INTERVAL_SECONDS;
|
|
1117
1128
|
var MAX_TIMEOUT_MS;
|
|
1118
1129
|
var MemoryStorageAdapter;
|
|
1119
1130
|
var init_memory = __esm({
|
|
1120
1131
|
"libs/utils/src/storage/adapters/memory.ts"() {
|
|
1121
1132
|
"use strict";
|
|
1122
|
-
init_base();
|
|
1123
1133
|
init_errors();
|
|
1124
1134
|
init_pattern();
|
|
1125
1135
|
init_ttl();
|
|
1136
|
+
init_base();
|
|
1126
1137
|
DEFAULT_SWEEP_INTERVAL_SECONDS = 60;
|
|
1127
1138
|
MAX_TIMEOUT_MS = 2147483647;
|
|
1128
1139
|
MemoryStorageAdapter = class extends BaseStorageAdapter {
|
|
@@ -1214,6 +1225,12 @@ var init_memory = __esm({
|
|
|
1214
1225
|
this.ensureConnected();
|
|
1215
1226
|
return this.deleteEntry(key);
|
|
1216
1227
|
}
|
|
1228
|
+
async deleteIfEquals(key, expectedValue) {
|
|
1229
|
+
this.ensureConnected();
|
|
1230
|
+
const entry = this.store.get(key);
|
|
1231
|
+
if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
|
|
1232
|
+
return this.deleteEntry(key);
|
|
1233
|
+
}
|
|
1217
1234
|
async exists(key) {
|
|
1218
1235
|
this.ensureConnected();
|
|
1219
1236
|
const entry = this.store.get(key);
|
|
@@ -2585,6 +2602,13 @@ var init_utils = __esm({
|
|
|
2585
2602
|
init_ttl();
|
|
2586
2603
|
}
|
|
2587
2604
|
});
|
|
2605
|
+
var COMPARE_AND_DELETE_SCRIPT;
|
|
2606
|
+
var init_compare_and_delete = __esm({
|
|
2607
|
+
"libs/utils/src/storage/utils/compare-and-delete.ts"() {
|
|
2608
|
+
"use strict";
|
|
2609
|
+
COMPARE_AND_DELETE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`;
|
|
2610
|
+
}
|
|
2611
|
+
});
|
|
2588
2612
|
var redis_exports = {};
|
|
2589
2613
|
__export2(redis_exports, {
|
|
2590
2614
|
RedisStorageAdapter: () => RedisStorageAdapter,
|
|
@@ -2627,6 +2651,7 @@ var init_redis = __esm({
|
|
|
2627
2651
|
init_errors();
|
|
2628
2652
|
init_redis_error_listener();
|
|
2629
2653
|
init_utils();
|
|
2654
|
+
init_compare_and_delete();
|
|
2630
2655
|
init_base();
|
|
2631
2656
|
RedisStorageAdapter = class extends BaseStorageAdapter {
|
|
2632
2657
|
backendName = "redis";
|
|
@@ -2781,6 +2806,15 @@ var init_redis = __esm({
|
|
|
2781
2806
|
const result = await this.getConnectedClient().del(this.prefixKey(key));
|
|
2782
2807
|
return result > 0;
|
|
2783
2808
|
}
|
|
2809
|
+
async deleteIfEquals(key, expectedValue) {
|
|
2810
|
+
const deleted = await this.getConnectedClient().eval(
|
|
2811
|
+
COMPARE_AND_DELETE_SCRIPT,
|
|
2812
|
+
1,
|
|
2813
|
+
this.prefixKey(key),
|
|
2814
|
+
expectedValue
|
|
2815
|
+
);
|
|
2816
|
+
return deleted === 1;
|
|
2817
|
+
}
|
|
2784
2818
|
async exists(key) {
|
|
2785
2819
|
const result = await this.getConnectedClient().exists(this.prefixKey(key));
|
|
2786
2820
|
return result > 0;
|
|
@@ -2987,9 +3021,12 @@ var vercel_kv_exports = {};
|
|
|
2987
3021
|
__export2(vercel_kv_exports, {
|
|
2988
3022
|
VercelKvStorageAdapter: () => VercelKvStorageAdapter
|
|
2989
3023
|
});
|
|
2990
|
-
function
|
|
3024
|
+
async function loadVercelKv() {
|
|
2991
3025
|
try {
|
|
2992
|
-
|
|
3026
|
+
const mod = await import("@vercel/kv");
|
|
3027
|
+
const resolved = typeof mod.createClient === "function" ? mod : mod.default;
|
|
3028
|
+
if (!resolved || typeof resolved.createClient !== "function") throw new Error("createClient missing");
|
|
3029
|
+
return resolved;
|
|
2993
3030
|
} catch {
|
|
2994
3031
|
throw new Error("@vercel/kv is required for Vercel KV storage adapter. Install it with: npm install @vercel/kv");
|
|
2995
3032
|
}
|
|
@@ -2998,9 +3035,10 @@ var VercelKvStorageAdapter;
|
|
|
2998
3035
|
var init_vercel_kv = __esm({
|
|
2999
3036
|
"libs/utils/src/storage/adapters/vercel-kv.ts"() {
|
|
3000
3037
|
"use strict";
|
|
3001
|
-
init_base();
|
|
3002
3038
|
init_errors();
|
|
3039
|
+
init_compare_and_delete();
|
|
3003
3040
|
init_ttl();
|
|
3041
|
+
init_base();
|
|
3004
3042
|
VercelKvStorageAdapter = class extends BaseStorageAdapter {
|
|
3005
3043
|
backendName = "vercel-kv";
|
|
3006
3044
|
client;
|
|
@@ -3025,17 +3063,13 @@ var init_vercel_kv = __esm({
|
|
|
3025
3063
|
async connect() {
|
|
3026
3064
|
if (this.connected) return;
|
|
3027
3065
|
try {
|
|
3028
|
-
const { createClient
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
const token = this.options.token;
|
|
3034
|
-
if (!url || !token) {
|
|
3035
|
-
throw new StorageConfigError("vercel-kv", "URL and token are required");
|
|
3036
|
-
}
|
|
3037
|
-
this.client = createClient({ url, token });
|
|
3066
|
+
const { createClient } = await loadVercelKv();
|
|
3067
|
+
const url = this.options.url;
|
|
3068
|
+
const token = this.options.token;
|
|
3069
|
+
if (!url || !token) {
|
|
3070
|
+
throw new StorageConfigError("vercel-kv", "URL and token are required");
|
|
3038
3071
|
}
|
|
3072
|
+
this.client = createClient({ url, token, cache: void 0, automaticDeserialization: false });
|
|
3039
3073
|
await this.client.exists("__healthcheck__");
|
|
3040
3074
|
this.connected = true;
|
|
3041
3075
|
} catch (e) {
|
|
@@ -3076,8 +3110,7 @@ var init_vercel_kv = __esm({
|
|
|
3076
3110
|
// Core Operations
|
|
3077
3111
|
// ============================================
|
|
3078
3112
|
async get(key) {
|
|
3079
|
-
|
|
3080
|
-
return result;
|
|
3113
|
+
return this.getConnectedClient().get(this.prefixKey(key));
|
|
3081
3114
|
}
|
|
3082
3115
|
async doSet(key, value, options) {
|
|
3083
3116
|
const client = this.getConnectedClient();
|
|
@@ -3097,6 +3130,14 @@ var init_vercel_kv = __esm({
|
|
|
3097
3130
|
const result = await this.getConnectedClient().del(this.prefixKey(key));
|
|
3098
3131
|
return result > 0;
|
|
3099
3132
|
}
|
|
3133
|
+
async deleteIfEquals(key, expectedValue) {
|
|
3134
|
+
const deleted = await this.getConnectedClient().eval(
|
|
3135
|
+
COMPARE_AND_DELETE_SCRIPT,
|
|
3136
|
+
[this.prefixKey(key)],
|
|
3137
|
+
[expectedValue]
|
|
3138
|
+
);
|
|
3139
|
+
return deleted === 1;
|
|
3140
|
+
}
|
|
3100
3141
|
async exists(key) {
|
|
3101
3142
|
const result = await this.getConnectedClient().exists(this.prefixKey(key));
|
|
3102
3143
|
return result > 0;
|
|
@@ -3213,9 +3254,10 @@ var UpstashStorageAdapter;
|
|
|
3213
3254
|
var init_upstash = __esm({
|
|
3214
3255
|
"libs/utils/src/storage/adapters/upstash.ts"() {
|
|
3215
3256
|
"use strict";
|
|
3216
|
-
init_base();
|
|
3217
3257
|
init_errors();
|
|
3258
|
+
init_compare_and_delete();
|
|
3218
3259
|
init_ttl();
|
|
3260
|
+
init_base();
|
|
3219
3261
|
PUBSUB_POLL_INTERVAL_MS = 100;
|
|
3220
3262
|
UpstashStorageAdapter = class extends BaseStorageAdapter {
|
|
3221
3263
|
backendName = "upstash";
|
|
@@ -3252,7 +3294,7 @@ var init_upstash = __esm({
|
|
|
3252
3294
|
if (!url || !token) {
|
|
3253
3295
|
throw new StorageConfigError("upstash", "URL and token are required");
|
|
3254
3296
|
}
|
|
3255
|
-
this.client = new Redis2({ url, token });
|
|
3297
|
+
this.client = new Redis2({ url, token, automaticDeserialization: false });
|
|
3256
3298
|
await this.client.exists("__healthcheck__");
|
|
3257
3299
|
this.connected = true;
|
|
3258
3300
|
} catch (e) {
|
|
@@ -3299,8 +3341,7 @@ var init_upstash = __esm({
|
|
|
3299
3341
|
// Core Operations
|
|
3300
3342
|
// ============================================
|
|
3301
3343
|
async get(key) {
|
|
3302
|
-
|
|
3303
|
-
return result;
|
|
3344
|
+
return this.getConnectedClient().get(this.prefixKey(key));
|
|
3304
3345
|
}
|
|
3305
3346
|
async doSet(key, value, options) {
|
|
3306
3347
|
const client = this.getConnectedClient();
|
|
@@ -3320,6 +3361,14 @@ var init_upstash = __esm({
|
|
|
3320
3361
|
const result = await this.getConnectedClient().del(this.prefixKey(key));
|
|
3321
3362
|
return result > 0;
|
|
3322
3363
|
}
|
|
3364
|
+
async deleteIfEquals(key, expectedValue) {
|
|
3365
|
+
const deleted = await this.getConnectedClient().eval(
|
|
3366
|
+
COMPARE_AND_DELETE_SCRIPT,
|
|
3367
|
+
[this.prefixKey(key)],
|
|
3368
|
+
[expectedValue]
|
|
3369
|
+
);
|
|
3370
|
+
return deleted === 1;
|
|
3371
|
+
}
|
|
3323
3372
|
async exists(key) {
|
|
3324
3373
|
const result = await this.getConnectedClient().exists(this.prefixKey(key));
|
|
3325
3374
|
return result > 0;
|
|
@@ -3508,7 +3557,9 @@ var init_cloudflare_kv = __esm({
|
|
|
3508
3557
|
constructor(options) {
|
|
3509
3558
|
super();
|
|
3510
3559
|
if (!options?.namespace) {
|
|
3511
|
-
throw new Error(
|
|
3560
|
+
throw new Error(
|
|
3561
|
+
"CloudflareKvStorageAdapter: a bound KV `namespace` is required (resolve it from the Worker `env`)."
|
|
3562
|
+
);
|
|
3512
3563
|
}
|
|
3513
3564
|
this.kv = options.namespace;
|
|
3514
3565
|
this.keyPrefix = options.keyPrefix ?? "";
|
|
@@ -3602,6 +3653,13 @@ var init_cloudflare_kv = __esm({
|
|
|
3602
3653
|
"Cloudflare KV has no atomic increment (a get+put is racy under eventual consistency). Use a Durable Object for atomic counters."
|
|
3603
3654
|
);
|
|
3604
3655
|
}
|
|
3656
|
+
async deleteIfEquals(_key, _expectedValue) {
|
|
3657
|
+
throw new StorageNotSupportedError(
|
|
3658
|
+
"deleteIfEquals",
|
|
3659
|
+
this.backendName,
|
|
3660
|
+
"Cloudflare KV has no compare-and-set; use a Durable Object for an atomic compare-and-delete."
|
|
3661
|
+
);
|
|
3662
|
+
}
|
|
3605
3663
|
// ── Pub/Sub (unsupported) ────────────────────────────────────────────────
|
|
3606
3664
|
getPubSubSuggestion() {
|
|
3607
3665
|
return "Cloudflare KV does not support pub/sub. Use a Durable Object (or Queues) for fan-out.";
|
|
@@ -3715,16 +3773,7 @@ function compileBlocks(entries, parseNetwork) {
|
|
|
3715
3773
|
}
|
|
3716
3774
|
init_fs2();
|
|
3717
3775
|
init_crypto();
|
|
3718
|
-
|
|
3719
|
-
os: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
|
|
3720
|
-
platform: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
|
|
3721
|
-
runtime: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
|
|
3722
|
-
deployment: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
|
|
3723
|
-
provider: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
|
|
3724
|
-
target: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
|
|
3725
|
-
surface: import_lazy_zod3.z.array(import_lazy_zod3.z.enum(["mcp", "cli", "http-trigger", "job", "agent"])).optional(),
|
|
3726
|
-
env: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional()
|
|
3727
|
-
}).strict();
|
|
3776
|
+
init_errors();
|
|
3728
3777
|
init_errors();
|
|
3729
3778
|
init_memory();
|
|
3730
3779
|
init_redis_error_listener();
|
|
@@ -3744,6 +3793,14 @@ init_ttl();
|
|
|
3744
3793
|
init_crypto();
|
|
3745
3794
|
init_runtime();
|
|
3746
3795
|
|
|
3796
|
+
// plugins/plugin-cache/src/cache.errors.ts
|
|
3797
|
+
var CachePluginConfigurationError = class extends Error {
|
|
3798
|
+
constructor(message) {
|
|
3799
|
+
super(message);
|
|
3800
|
+
this.name = "CachePluginConfigurationError";
|
|
3801
|
+
}
|
|
3802
|
+
};
|
|
3803
|
+
|
|
3747
3804
|
// plugins/plugin-cache/src/cache.symbol.ts
|
|
3748
3805
|
var CacheStoreToken = /* @__PURE__ */ Symbol("plugin:cache:store");
|
|
3749
3806
|
|
|
@@ -3975,6 +4032,15 @@ CacheVercelKvProvider = __decorateClass([
|
|
|
3975
4032
|
|
|
3976
4033
|
// plugins/plugin-cache/src/cache.plugin.ts
|
|
3977
4034
|
var DEFAULT_BYPASS_HEADER = "x-frontmcp-disable-cache";
|
|
4035
|
+
var BYPASS_HEADER_PREFIX = "x-frontmcp-";
|
|
4036
|
+
function assertReadableBypassHeader(bypassHeader) {
|
|
4037
|
+
if (bypassHeader === void 0) return;
|
|
4038
|
+
const name = bypassHeader.toLowerCase();
|
|
4039
|
+
if (name.startsWith(BYPASS_HEADER_PREFIX) && name.length > BYPASS_HEADER_PREFIX.length) return;
|
|
4040
|
+
throw new CachePluginConfigurationError(
|
|
4041
|
+
`CachePlugin bypassHeader "${bypassHeader}" is never seen by the plugin: only request headers that start with "${BYPASS_HEADER_PREFIX}" reach it. Rename it, e.g. "${BYPASS_HEADER_PREFIX}no-cache".`
|
|
4042
|
+
);
|
|
4043
|
+
}
|
|
3978
4044
|
function matchesToolPattern(toolName, patterns) {
|
|
3979
4045
|
if (!patterns || patterns.length === 0) return false;
|
|
3980
4046
|
return patterns.some((pattern) => {
|
|
@@ -3990,6 +4056,7 @@ var CachePlugin = class extends import_sdk4.DynamicPlugin {
|
|
|
3990
4056
|
options;
|
|
3991
4057
|
constructor(options = CachePlugin.defaultOptions) {
|
|
3992
4058
|
super();
|
|
4059
|
+
assertReadableBypassHeader(options.bypassHeader);
|
|
3993
4060
|
this.options = {
|
|
3994
4061
|
defaultTTL: 60 * 60 * 24,
|
|
3995
4062
|
...options
|
|
@@ -4047,15 +4114,13 @@ var CachePlugin = class extends import_sdk4.DynamicPlugin {
|
|
|
4047
4114
|
const hash = this.buildCacheKey(tool.fullName, toolContext);
|
|
4048
4115
|
const cached = await cacheStore.getValue(hash);
|
|
4049
4116
|
if (cached !== void 0 && cached !== null) {
|
|
4050
|
-
|
|
4051
|
-
if (cache === true || cacheConfig?.ttl && cacheConfig?.slideWindow) {
|
|
4052
|
-
const ttl = this.getTtl(cache);
|
|
4053
|
-
await cacheStore.setValue(hash, cached, ttl);
|
|
4054
|
-
}
|
|
4055
|
-
if (!tool.safeParseOutput(cached).success) {
|
|
4117
|
+
if (isErrorResult(cached) || !tool.safeParseOutput(cached).success) {
|
|
4056
4118
|
await cacheStore.delete(hash);
|
|
4057
4119
|
return;
|
|
4058
4120
|
}
|
|
4121
|
+
if (typeof cache === "object" && cache.slideWindow) {
|
|
4122
|
+
await cacheStore.setValue(hash, cached, this.getTtl(cache));
|
|
4123
|
+
}
|
|
4059
4124
|
flowCtx.state.set("resultMeta", { ...flowCtx.state.resultMeta, cache: "hit" });
|
|
4060
4125
|
flowCtx.state.set("rawOutput", cached);
|
|
4061
4126
|
toolContext.respond(cached);
|
|
@@ -4072,6 +4137,9 @@ var CachePlugin = class extends import_sdk4.DynamicPlugin {
|
|
|
4072
4137
|
if (!shouldCache || typeof toolContext.input === "undefined") {
|
|
4073
4138
|
return;
|
|
4074
4139
|
}
|
|
4140
|
+
if (isErrorResult(toolContext.output)) {
|
|
4141
|
+
return;
|
|
4142
|
+
}
|
|
4075
4143
|
const cacheStore = this.get(CacheStoreToken);
|
|
4076
4144
|
const ttl = this.getTtl(cache);
|
|
4077
4145
|
const hash = this.buildCacheKey(tool.fullName, toolContext);
|
|
@@ -4196,6 +4264,11 @@ CachePlugin = __decorateClass([
|
|
|
4196
4264
|
]
|
|
4197
4265
|
})
|
|
4198
4266
|
], CachePlugin);
|
|
4267
|
+
function isErrorResult(output) {
|
|
4268
|
+
if (typeof output !== "object" || output === null) return false;
|
|
4269
|
+
const result = output;
|
|
4270
|
+
return result.isError === true && Array.isArray(result.content);
|
|
4271
|
+
}
|
|
4199
4272
|
function stableValue(value) {
|
|
4200
4273
|
if (Array.isArray(value)) {
|
|
4201
4274
|
return value.map(stableValue);
|
|
@@ -4211,5 +4284,6 @@ function stableValue(value) {
|
|
|
4211
4284
|
}
|
|
4212
4285
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4213
4286
|
0 && (module.exports = {
|
|
4214
|
-
CachePlugin
|
|
4287
|
+
CachePlugin,
|
|
4288
|
+
CachePluginConfigurationError
|
|
4215
4289
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontmcp/plugin-cache",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.1-rc.1",
|
|
4
4
|
"description": "Cache plugin for FrontMCP - Redis, Vercel KV, and in-memory caching with automatic tool result caching",
|
|
5
5
|
"author": "AgentFront <info@agentfront.dev>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@frontmcp/sdk": "1.
|
|
52
|
-
"@frontmcp/utils": "1.
|
|
51
|
+
"@frontmcp/sdk": "1.9.1-rc.1",
|
|
52
|
+
"@frontmcp/utils": "1.9.1-rc.1",
|
|
53
53
|
"ioredis": "^5.8.0"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|