@trieb.work/nextjs-turbo-redis-cache 1.10.0-beta.14 → 1.11.0-beta.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.
Files changed (55) hide show
  1. package/.github/workflows/ci.yml +28 -12
  2. package/CHANGELOG.md +6 -94
  3. package/README.md +94 -0
  4. package/dist/index.d.mts +22 -1
  5. package/dist/index.d.ts +22 -1
  6. package/dist/index.js +333 -15
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +330 -14
  9. package/dist/index.mjs.map +1 -1
  10. package/package.json +3 -2
  11. package/playwright.config.ts +8 -1
  12. package/src/CacheComponentsHandler.ts +471 -0
  13. package/src/RedisStringsHandler.ts +11 -11
  14. package/src/index.test.ts +1 -1
  15. package/src/index.ts +5 -0
  16. package/test/cache-components/cache-components.integration.spec.ts +188 -0
  17. package/test/integration/next-app-15-4-7/next.config.js +3 -0
  18. package/test/integration/next-app-15-4-7/pnpm-lock.yaml +1 -1
  19. package/test/integration/next-app-16-0-3/next.config.ts +3 -0
  20. package/test/integration/next-app-16-1-1-cache-components/README.md +36 -0
  21. package/test/integration/next-app-16-1-1-cache-components/cache-handler.js +3 -0
  22. package/test/integration/next-app-16-1-1-cache-components/eslint.config.mjs +18 -0
  23. package/test/integration/next-app-16-1-1-cache-components/next.config.ts +13 -0
  24. package/test/integration/next-app-16-1-1-cache-components/package.json +28 -0
  25. package/test/integration/next-app-16-1-1-cache-components/pnpm-lock.yaml +4128 -0
  26. package/test/integration/next-app-16-1-1-cache-components/postcss.config.mjs +7 -0
  27. package/test/integration/next-app-16-1-1-cache-components/public/file.svg +1 -0
  28. package/test/integration/next-app-16-1-1-cache-components/public/globe.svg +1 -0
  29. package/test/integration/next-app-16-1-1-cache-components/public/next.svg +1 -0
  30. package/test/integration/next-app-16-1-1-cache-components/public/public/file.svg +1 -0
  31. package/test/integration/next-app-16-1-1-cache-components/public/public/globe.svg +1 -0
  32. package/test/integration/next-app-16-1-1-cache-components/public/public/next.svg +1 -0
  33. package/test/integration/next-app-16-1-1-cache-components/public/public/vercel.svg +1 -0
  34. package/test/integration/next-app-16-1-1-cache-components/public/public/window.svg +1 -0
  35. package/test/integration/next-app-16-1-1-cache-components/public/vercel.svg +1 -0
  36. package/test/integration/next-app-16-1-1-cache-components/public/window.svg +1 -0
  37. package/test/integration/next-app-16-1-1-cache-components/src/app/api/cached-static-fetch/route.ts +19 -0
  38. package/test/integration/next-app-16-1-1-cache-components/src/app/api/cached-with-tag/route.ts +21 -0
  39. package/test/integration/next-app-16-1-1-cache-components/src/app/api/revalidate-tag/route.ts +19 -0
  40. package/test/integration/next-app-16-1-1-cache-components/src/app/api/revalidated-fetch/route.ts +19 -0
  41. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/cachelife-short/page.tsx +110 -0
  42. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/page.tsx +90 -0
  43. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/runtime-data-suspense/page.tsx +127 -0
  44. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/stale-while-revalidate/page.tsx +130 -0
  45. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/tag-invalidation/page.tsx +127 -0
  46. package/test/integration/next-app-16-1-1-cache-components/src/app/cache-lab/use-cache-nondeterministic/page.tsx +110 -0
  47. package/test/integration/next-app-16-1-1-cache-components/src/app/favicon.ico +0 -0
  48. package/test/integration/next-app-16-1-1-cache-components/src/app/globals.css +26 -0
  49. package/test/integration/next-app-16-1-1-cache-components/src/app/layout.tsx +57 -0
  50. package/test/integration/next-app-16-1-1-cache-components/src/app/page.tsx +755 -0
  51. package/test/integration/next-app-16-1-1-cache-components/src/app/revalidation-interface.tsx +267 -0
  52. package/test/integration/next-app-16-1-1-cache-components/src/app/update-tag-test/page.tsx +22 -0
  53. package/test/integration/next-app-16-1-1-cache-components/tsconfig.json +34 -0
  54. package/tests/cache-lab.spec.ts +157 -0
  55. package/vitest.cache-components.config.ts +16 -0
package/dist/index.js CHANGED
@@ -21,7 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  RedisStringsHandler: () => RedisStringsHandler,
24
- default: () => index_default
24
+ default: () => index_default,
25
+ getRedisCacheComponentsHandler: () => getRedisCacheComponentsHandler,
26
+ redisCacheHandler: () => redisCacheHandler
25
27
  });
26
28
  module.exports = __toCommonJS(index_exports);
27
29
 
@@ -539,16 +541,15 @@ var RedisStringsHandler = class {
539
541
  error,
540
542
  killContainerOnErrorCount++
541
543
  );
542
- setTimeout(() => {
543
- if (!this.client.isOpen && !this.client.isReady) {
544
- this.client.connect().catch((error2) => {
545
- console.error(
546
- "Failed to reconnect Redis client after connection loss:",
547
- error2
548
- );
549
- });
550
- }
551
- }, 1e3);
544
+ setTimeout(
545
+ () => this.client.connect().catch((error2) => {
546
+ console.error(
547
+ "Failed to reconnect Redis client after connection loss:",
548
+ error2
549
+ );
550
+ }),
551
+ 1e3
552
+ );
552
553
  if (this.killContainerOnErrorThreshold > 0 && killContainerOnErrorCount >= this.killContainerOnErrorThreshold) {
553
554
  console.error(
554
555
  "Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)",
@@ -564,9 +565,12 @@ var RedisStringsHandler = class {
564
565
  });
565
566
  this.client.connect().then(() => {
566
567
  debug("green", "Redis client connected.");
567
- }).catch((error) => {
568
- console.error("Failed to connect Redis client:", error);
569
- throw error;
568
+ }).catch(() => {
569
+ this.client.connect().catch((error) => {
570
+ console.error("Failed to connect Redis client:", error);
571
+ this.client.disconnect();
572
+ throw error;
573
+ });
570
574
  });
571
575
  } catch (error) {
572
576
  console.error("Failed to initialize Redis client");
@@ -975,10 +979,324 @@ var CachedHandler = class {
975
979
  }
976
980
  };
977
981
 
982
+ // src/CacheComponentsHandler.ts
983
+ var import_redis2 = require("redis");
984
+ var REVALIDATED_TAGS_KEY2 = "__cacheComponents_revalidated_tags__";
985
+ var SHARED_TAGS_KEY = "__cacheComponents_sharedTags__";
986
+ var killContainerOnErrorCount2 = 0;
987
+ async function streamToBuffer(stream) {
988
+ const reader = stream.getReader();
989
+ const chunks = [];
990
+ while (true) {
991
+ const { value, done } = await reader.read();
992
+ if (done) break;
993
+ if (value) {
994
+ chunks.push(value);
995
+ }
996
+ }
997
+ if (chunks.length === 1) {
998
+ return chunks[0];
999
+ }
1000
+ const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
1001
+ const result = new Uint8Array(totalLength);
1002
+ let offset = 0;
1003
+ for (const chunk of chunks) {
1004
+ result.set(chunk, offset);
1005
+ offset += chunk.byteLength;
1006
+ }
1007
+ return result;
1008
+ }
1009
+ function bufferToReadableStream(buffer) {
1010
+ return new ReadableStream({
1011
+ start(controller) {
1012
+ controller.enqueue(buffer);
1013
+ controller.close();
1014
+ }
1015
+ });
1016
+ }
1017
+ var RedisCacheComponentsHandler = class {
1018
+ constructor({
1019
+ redisUrl = process.env.REDIS_URL ? process.env.REDIS_URL : process.env.REDISHOST ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}` : "redis://localhost:6379",
1020
+ database = process.env.VERCEL_ENV === "production" ? 0 : 1,
1021
+ keyPrefix = process.env.VERCEL_URL || "UNDEFINED_URL_",
1022
+ getTimeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS ? Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 500 : 500,
1023
+ revalidateTagQuerySize = 250,
1024
+ avgResyncIntervalMs = 60 * 60 * 1e3,
1025
+ socketOptions,
1026
+ clientOptions,
1027
+ killContainerOnErrorThreshold = process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD ? Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0 : 0
1028
+ }) {
1029
+ try {
1030
+ this.keyPrefix = keyPrefix;
1031
+ this.getTimeoutMs = getTimeoutMs;
1032
+ this.client = (0, import_redis2.createClient)({
1033
+ url: redisUrl,
1034
+ pingInterval: 1e4,
1035
+ ...database !== 0 ? { database } : {},
1036
+ ...socketOptions ? { socket: { ...socketOptions } } : {},
1037
+ ...clientOptions || {}
1038
+ });
1039
+ this.client.on("error", (error) => {
1040
+ console.error(
1041
+ "RedisCacheComponentsHandler client error",
1042
+ error,
1043
+ killContainerOnErrorCount2++
1044
+ );
1045
+ setTimeout(
1046
+ () => this.client.connect().catch((err) => {
1047
+ console.error(
1048
+ "Failed to reconnect RedisCacheComponentsHandler client after connection loss:",
1049
+ err
1050
+ );
1051
+ }),
1052
+ 1e3
1053
+ );
1054
+ if (killContainerOnErrorThreshold > 0 && killContainerOnErrorCount2 >= killContainerOnErrorThreshold) {
1055
+ console.error(
1056
+ "RedisCacheComponentsHandler client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)",
1057
+ error,
1058
+ killContainerOnErrorCount2++
1059
+ );
1060
+ this.client.disconnect();
1061
+ this.client.quit();
1062
+ setTimeout(() => {
1063
+ process.exit(1);
1064
+ }, 500);
1065
+ }
1066
+ });
1067
+ this.client.connect().then(() => {
1068
+ debug("green", "RedisCacheComponentsHandler client connected.");
1069
+ }).catch(() => {
1070
+ this.client.connect().catch((error) => {
1071
+ console.error(
1072
+ "Failed to connect RedisCacheComponentsHandler client:",
1073
+ error
1074
+ );
1075
+ this.client.disconnect();
1076
+ throw error;
1077
+ });
1078
+ });
1079
+ const filterKeys = (key) => key !== REVALIDATED_TAGS_KEY2 && key !== SHARED_TAGS_KEY;
1080
+ this.revalidatedTagsMap = new SyncedMap({
1081
+ client: this.client,
1082
+ keyPrefix,
1083
+ redisKey: REVALIDATED_TAGS_KEY2,
1084
+ database,
1085
+ querySize: revalidateTagQuerySize,
1086
+ filterKeys,
1087
+ resyncIntervalMs: avgResyncIntervalMs + avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
1088
+ });
1089
+ this.sharedTagsMap = new SyncedMap({
1090
+ client: this.client,
1091
+ keyPrefix,
1092
+ redisKey: SHARED_TAGS_KEY,
1093
+ database,
1094
+ querySize: revalidateTagQuerySize,
1095
+ filterKeys,
1096
+ resyncIntervalMs: avgResyncIntervalMs - avgResyncIntervalMs / 10 + Math.random() * (avgResyncIntervalMs / 10)
1097
+ });
1098
+ } catch (error) {
1099
+ console.error("RedisCacheComponentsHandler constructor error", error);
1100
+ throw error;
1101
+ }
1102
+ }
1103
+ async assertClientIsReady() {
1104
+ if (!this.client.isReady && !this.client.isOpen) {
1105
+ await this.client.connect().catch((error) => {
1106
+ console.error(
1107
+ "RedisCacheComponentsHandler assertClientIsReady reconnect error:",
1108
+ error
1109
+ );
1110
+ throw error;
1111
+ });
1112
+ }
1113
+ await Promise.all([
1114
+ this.revalidatedTagsMap.waitUntilReady(),
1115
+ this.sharedTagsMap.waitUntilReady()
1116
+ ]);
1117
+ }
1118
+ async computeMaxRevalidation(tags) {
1119
+ let max = 0;
1120
+ for (const tag of tags) {
1121
+ const ts = this.revalidatedTagsMap.get(tag);
1122
+ if (ts && ts > max) {
1123
+ max = ts;
1124
+ }
1125
+ }
1126
+ return max;
1127
+ }
1128
+ async get(cacheKey, softTags) {
1129
+ const redisKey = `${this.keyPrefix}${cacheKey}`;
1130
+ try {
1131
+ await this.assertClientIsReady();
1132
+ const serialized = await redisErrorHandler(
1133
+ "RedisCacheComponentsHandler.get(), operation: get " + this.getTimeoutMs + "ms " + redisKey,
1134
+ this.client.get(
1135
+ (0, import_redis2.commandOptions)({ signal: AbortSignal.timeout(this.getTimeoutMs) }),
1136
+ redisKey
1137
+ )
1138
+ );
1139
+ if (!serialized) {
1140
+ return void 0;
1141
+ }
1142
+ const stored = JSON.parse(serialized);
1143
+ const now = Date.now();
1144
+ const expiryTime = stored.timestamp + stored.expire * 1e3;
1145
+ if (Number.isFinite(stored.expire) && stored.expire > 0 && now > expiryTime) {
1146
+ await this.client.unlink(redisKey).catch(() => {
1147
+ });
1148
+ await this.sharedTagsMap.delete(cacheKey).catch(() => {
1149
+ });
1150
+ return void 0;
1151
+ }
1152
+ const maxRevalidation = await this.computeMaxRevalidation([
1153
+ ...stored.tags || [],
1154
+ ...softTags || []
1155
+ ]);
1156
+ if (maxRevalidation > 0 && maxRevalidation > stored.timestamp) {
1157
+ await this.client.unlink(redisKey).catch(() => {
1158
+ });
1159
+ await this.sharedTagsMap.delete(cacheKey).catch(() => {
1160
+ });
1161
+ return void 0;
1162
+ }
1163
+ const valueBuffer = typeof stored.value === "string" ? new Uint8Array(Buffer.from(stored.value, "base64")) : stored.value;
1164
+ return {
1165
+ ...stored,
1166
+ value: bufferToReadableStream(valueBuffer)
1167
+ };
1168
+ } catch (error) {
1169
+ console.error(
1170
+ "RedisCacheComponentsHandler.get() Error occurred while getting cache entry. Returning undefined so site can continue to serve content while cache is disabled. The original error was:",
1171
+ error,
1172
+ killContainerOnErrorCount2++
1173
+ );
1174
+ return void 0;
1175
+ }
1176
+ }
1177
+ async set(cacheKey, pendingEntry) {
1178
+ try {
1179
+ await this.assertClientIsReady();
1180
+ const entry = await pendingEntry;
1181
+ const [storeStream] = entry.value.tee();
1182
+ const buffer = await streamToBuffer(storeStream);
1183
+ const stored = {
1184
+ value: Buffer.from(buffer).toString("base64"),
1185
+ tags: entry.tags || [],
1186
+ stale: entry.stale,
1187
+ timestamp: entry.timestamp,
1188
+ expire: entry.expire,
1189
+ revalidate: entry.revalidate
1190
+ };
1191
+ let serialized;
1192
+ try {
1193
+ const cleanStored = {
1194
+ value: stored.value,
1195
+ tags: Array.isArray(stored.tags) ? [...stored.tags] : [],
1196
+ stale: Number(stored.stale),
1197
+ timestamp: Number(stored.timestamp),
1198
+ expire: Number(stored.expire),
1199
+ revalidate: Number(stored.revalidate)
1200
+ };
1201
+ serialized = JSON.stringify(cleanStored);
1202
+ } catch (jsonError) {
1203
+ console.error("JSON.stringify error:", jsonError);
1204
+ console.error("Stored object:", stored);
1205
+ throw jsonError;
1206
+ }
1207
+ const ttlSeconds = Number.isFinite(stored.expire) && stored.expire > 0 ? Math.floor(stored.expire) : void 0;
1208
+ const redisKey = `${this.keyPrefix}${cacheKey}`;
1209
+ const setOperation = redisErrorHandler(
1210
+ "RedisCacheComponentsHandler.set(), operation: set " + redisKey,
1211
+ this.client.set(redisKey, serialized, {
1212
+ ...ttlSeconds ? { EX: ttlSeconds } : {}
1213
+ })
1214
+ );
1215
+ let tagsOperation;
1216
+ const tags = stored.tags || [];
1217
+ if (tags.length > 0) {
1218
+ const currentTags = this.sharedTagsMap.get(cacheKey);
1219
+ const currentIsSameAsNew = currentTags?.length === tags.length && currentTags.every((v) => tags.includes(v)) && tags.every((v) => currentTags.includes(v));
1220
+ if (!currentIsSameAsNew) {
1221
+ tagsOperation = this.sharedTagsMap.set(cacheKey, [...tags]);
1222
+ }
1223
+ }
1224
+ await Promise.all([setOperation, tagsOperation]);
1225
+ } catch (error) {
1226
+ console.error(
1227
+ "RedisCacheComponentsHandler.set() Error occurred while setting cache entry. The original error was:",
1228
+ error,
1229
+ killContainerOnErrorCount2++
1230
+ );
1231
+ throw error;
1232
+ }
1233
+ }
1234
+ async refreshTags() {
1235
+ await this.assertClientIsReady();
1236
+ }
1237
+ async getExpiration(tags) {
1238
+ try {
1239
+ await this.assertClientIsReady();
1240
+ return this.computeMaxRevalidation(tags || []);
1241
+ } catch (error) {
1242
+ console.error(
1243
+ "RedisCacheComponentsHandler.getExpiration() Error occurred while getting expiration for tags. The original error was:",
1244
+ error
1245
+ );
1246
+ return 0;
1247
+ }
1248
+ }
1249
+ async updateTags(tags, _durations) {
1250
+ try {
1251
+ void _durations;
1252
+ await this.assertClientIsReady();
1253
+ const now = Date.now();
1254
+ const tagsSet = new Set(tags || []);
1255
+ for (const tag of tagsSet) {
1256
+ await this.revalidatedTagsMap.set(tag, now);
1257
+ }
1258
+ const keysToDelete = /* @__PURE__ */ new Set();
1259
+ for (const [key, storedTags] of this.sharedTagsMap.entries()) {
1260
+ if (storedTags.some((tag) => tagsSet.has(tag))) {
1261
+ keysToDelete.add(key);
1262
+ }
1263
+ }
1264
+ if (keysToDelete.size === 0) {
1265
+ return;
1266
+ }
1267
+ const cacheKeys = Array.from(keysToDelete);
1268
+ const fullRedisKeys = cacheKeys.map((key) => `${this.keyPrefix}${key}`);
1269
+ await redisErrorHandler(
1270
+ "RedisCacheComponentsHandler.updateTags(), operation: unlink",
1271
+ this.client.unlink(fullRedisKeys)
1272
+ );
1273
+ const deleteTagsOperation = this.sharedTagsMap.delete(cacheKeys);
1274
+ await deleteTagsOperation;
1275
+ } catch (error) {
1276
+ console.error(
1277
+ "RedisCacheComponentsHandler.updateTags() Error occurred while updating tags. The original error was:",
1278
+ error,
1279
+ killContainerOnErrorCount2++
1280
+ );
1281
+ throw error;
1282
+ }
1283
+ }
1284
+ };
1285
+ var singletonHandler;
1286
+ function getRedisCacheComponentsHandler(options = {}) {
1287
+ if (!singletonHandler) {
1288
+ singletonHandler = new RedisCacheComponentsHandler(options);
1289
+ }
1290
+ return singletonHandler;
1291
+ }
1292
+ var redisCacheHandler = getRedisCacheComponentsHandler();
1293
+
978
1294
  // src/index.ts
979
1295
  var index_default = CachedHandler;
980
1296
  // Annotate the CommonJS export names for ESM import in node:
981
1297
  0 && (module.exports = {
982
- RedisStringsHandler
1298
+ RedisStringsHandler,
1299
+ getRedisCacheComponentsHandler,
1300
+ redisCacheHandler
983
1301
  });
984
1302
  //# sourceMappingURL=index.js.map