@trieb.work/nextjs-turbo-redis-cache 1.8.0-beta.2 → 1.8.0-beta.4

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.8.0-beta.4](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.3...v1.8.0-beta.4) (2025-06-12)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improve logs ([92508b6](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/92508b6697c87cad4a8720fb5380f41fe9ec2257))
7
+
8
+ # [1.8.0-beta.3](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.2...v1.8.0-beta.3) (2025-06-12)
9
+
10
+
11
+ ### Features
12
+
13
+ * add redis commands debug logging ([4c8a0d7](https://github.com/trieb-work/nextjs-turbo-redis-cache/commit/4c8a0d7f82573bbaab63bdd380063244c641735f))
14
+
1
15
  # [1.8.0-beta.2](https://github.com/trieb-work/nextjs-turbo-redis-cache/compare/v1.8.0-beta.1...v1.8.0-beta.2) (2025-06-11)
2
16
 
3
17
 
package/dist/index.js CHANGED
@@ -272,11 +272,14 @@ var SyncedMap = class {
272
272
  if (!this.customizedSync?.withoutRedisHashmap) {
273
273
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
274
274
  operations.push(
275
- this.client.hSet(
276
- options,
277
- this.keyPrefix + this.redisKey,
278
- key,
279
- JSON.stringify(value)
275
+ redisErrorHandler(
276
+ "SyncedMap.set(), operation: hSet " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
277
+ this.client.hSet(
278
+ options,
279
+ this.keyPrefix + this.redisKey,
280
+ key,
281
+ JSON.stringify(value)
282
+ )
280
283
  )
281
284
  );
282
285
  }
@@ -286,7 +289,10 @@ var SyncedMap = class {
286
289
  value
287
290
  };
288
291
  operations.push(
289
- this.client.publish(this.syncChannel, JSON.stringify(insertMessage))
292
+ redisErrorHandler(
293
+ "SyncedMap.set(), operation: publish " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
294
+ this.client.publish(this.syncChannel, JSON.stringify(insertMessage))
295
+ )
290
296
  );
291
297
  await Promise.all(operations);
292
298
  }
@@ -303,9 +309,12 @@ var SyncedMap = class {
303
309
  this.map.delete(key);
304
310
  }
305
311
  if (!this.customizedSync?.withoutRedisHashmap) {
306
- const options = getTimeoutRedisCommandOptions(this.timeoutMs);
312
+ const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10);
307
313
  operations.push(
308
- this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray)
314
+ redisErrorHandler(
315
+ "SyncedMap.delete(), operation: hDel " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + this.redisKey + " " + keysArray,
316
+ this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray)
317
+ )
309
318
  );
310
319
  }
311
320
  if (!withoutSyncMessage) {
@@ -314,7 +323,13 @@ var SyncedMap = class {
314
323
  keys: keysArray
315
324
  };
316
325
  operations.push(
317
- this.client.publish(this.syncChannel, JSON.stringify(deletionMessage))
326
+ redisErrorHandler(
327
+ "SyncedMap.delete(), operation: publish " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + keysArray,
328
+ this.client.publish(
329
+ this.syncChannel,
330
+ JSON.stringify(deletionMessage)
331
+ )
332
+ )
318
333
  );
319
334
  }
320
335
  await Promise.all(operations);
@@ -438,6 +453,30 @@ function bufferReplacer(_, value) {
438
453
  }
439
454
 
440
455
  // src/RedisStringsHandler.ts
456
+ function redisErrorHandler(debugInfo, redisCommandResult) {
457
+ const beforeTimestamp = performance.now();
458
+ return redisCommandResult.catch((error) => {
459
+ console.error(
460
+ "Redis command error",
461
+ (performance.now() - beforeTimestamp).toFixed(2),
462
+ "ms",
463
+ debugInfo,
464
+ error
465
+ );
466
+ throw error;
467
+ });
468
+ }
469
+ setInterval(() => {
470
+ const start = performance.now();
471
+ setImmediate(() => {
472
+ const duration = performance.now() - start;
473
+ if (duration > 100) {
474
+ console.warn(
475
+ `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms`
476
+ );
477
+ }
478
+ });
479
+ }, 1e3);
441
480
  var NEXT_CACHE_IMPLICIT_TAG_ID = "_N_T_";
442
481
  var REVALIDATED_TAGS_KEY = "__revalidated_tags__";
443
482
  function getTimeoutRedisCommandOptions(timeoutMs) {
@@ -623,9 +662,12 @@ var RedisStringsHandler = class {
623
662
  debug("green", "RedisStringsHandler.get() called with", key, ctx);
624
663
  await this.assertClientIsReady();
625
664
  const clientGet = this.redisGetDeduplication ? this.deduplicatedRedisGet(key) : this.redisGet;
626
- const serializedCacheEntry = await clientGet(
627
- getTimeoutRedisCommandOptions(this.timeoutMs),
628
- this.keyPrefix + key
665
+ const serializedCacheEntry = await redisErrorHandler(
666
+ "RedisStringsHandler.get(), operation: get" + (this.redisGetDeduplication ? "deduplicated" : "") + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
667
+ clientGet(
668
+ getTimeoutRedisCommandOptions(this.timeoutMs),
669
+ this.keyPrefix + key
670
+ )
629
671
  );
630
672
  debug(
631
673
  "green",
@@ -761,13 +803,11 @@ var RedisStringsHandler = class {
761
803
  );
762
804
  const expireAt = revalidate && Number.isSafeInteger(revalidate) && revalidate > 0 ? this.estimateExpireAge(revalidate) : this.estimateExpireAge(this.defaultStaleAge);
763
805
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
764
- const setOperation = this.client.set(
765
- options,
766
- this.keyPrefix + key,
767
- serializedCacheEntry,
768
- {
806
+ const setOperation = redisErrorHandler(
807
+ "RedisStringsHandler.set(), operation: set" + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
808
+ this.client.set(options, this.keyPrefix + key, serializedCacheEntry, {
769
809
  EX: expireAt
770
- }
810
+ })
771
811
  );
772
812
  debug(
773
813
  "blue",
@@ -860,7 +900,10 @@ var RedisStringsHandler = class {
860
900
  const redisKeys = Array.from(keysToDelete);
861
901
  const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);
862
902
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
863
- const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);
903
+ const deleteKeysOperation = redisErrorHandler(
904
+ "RedisStringsHandler.revalidateTag(), operation: unlink" + this.timeoutMs + "ms " + this.keyPrefix + " " + fullRedisKeys,
905
+ this.client.unlink(options, fullRedisKeys)
906
+ );
864
907
  if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {
865
908
  for (const key of keysToDelete) {
866
909
  this.inMemoryDeduplicationCache.delete(key);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts"],"sourcesContent":["import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n","import { commandOptions, createClient, RedisClientOptions } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redisUrl to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redisUrl?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n /** Kill container on Redis client error if error threshold is reached\n * @default 0 (0 means no error threshold)\n */\n killContainerOnErrorThreshold?: number;\n /** Additional Redis client socket options\n * @example { tls: true, rejectUnauthorized: false }\n */\n socketOptions?: RedisClientOptions['socket'];\n /** Additional Redis client options to be passed directly to createClient\n * @example { username: 'user', password: 'pass' }\n */\n clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nlet killContainerOnErrorCount: number = 0;\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n private killContainerOnErrorThreshold: number;\n\n constructor({\n redisUrl = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS\n ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000)\n : 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n killContainerOnErrorThreshold = process.env\n .KILL_CONTAINER_ON_ERROR_THRESHOLD\n ? (Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0)\n : 0,\n socketOptions,\n clientOptions,\n }: CreateRedisStringsHandlerOptions) {\n try {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n this.killContainerOnErrorThreshold = killContainerOnErrorThreshold;\n\n try {\n // Create Redis client with properly typed configuration\n this.client = createClient({\n url: redisUrl,\n pingInterval: 10_000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long.\n ...(database !== 0 ? { database } : {}),\n ...(socketOptions ? { socket: { ...socketOptions } } : {}),\n ...(clientOptions || {}),\n });\n\n this.client.on('error', (error) => {\n console.error(\n 'Redis client error',\n error,\n killContainerOnErrorCount++,\n );\n setTimeout(\n () =>\n this.client.connect().catch((error) => {\n console.error(\n 'Failed to reconnect Redis client after connection loss:',\n error,\n );\n }),\n 1000,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n } catch (error) {\n console.error(\n 'RedisStringsHandler constructor error',\n error,\n killContainerOnErrorCount++,\n );\n if (\n killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler constructor error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n process.exit(1);\n }\n throw error;\n }\n }\n\n resetRequestCache(): void {}\n\n private clientReadyCalls = 0;\n\n private async assertClientIsReady(): Promise<void> {\n if (this.clientReadyCalls > 10) {\n throw new Error(\n 'assertClientIsReady called more than 10 times without being ready.',\n );\n }\n await Promise.race([\n Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]),\n new Promise((_, reject) =>\n setTimeout(() => {\n reject(\n new Error(\n 'assertClientIsReady: Timeout waiting for Redis maps to be ready',\n ),\n );\n }, this.timeoutMs * 5),\n ),\n ]);\n this.clientReadyCalls = 0;\n if (!this.client.isReady) {\n throw new Error(\n 'assertClientIsReady: Redis client is not ready yet or connection is lost.',\n );\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n try {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n } catch (error) {\n // This catch block is necessary to handle any errors that may occur during:\n // 1. Redis operations (get, unlink)\n // 2. JSON parsing of cache entries\n // 3. Tag validation and cleanup\n // If any error occurs, we return null to indicate no valid cache entry was found,\n // allowing the application to regenerate the content rather than crash\n console.error(\n 'RedisStringsHandler.get() Error occurred while getting cache entry. Returning null so site can continue to serve content while cache is disabled. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler get() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n return null;\n }\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n try {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate =\n // For fetch requests in newest versions, the revalidate context property is never used, and instead the revalidate property of the passed-in data is used\n (data.kind === 'FETCH' && data.revalidate) ||\n ctx.revalidate ||\n ctx.cacheControl?.revalidate ||\n (data as { revalidate?: number | false })?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = this.client.set(\n options,\n this.keyPrefix + key,\n serializedCacheEntry,\n {\n EX: expireAt,\n },\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n } catch (error) {\n console.error(\n 'RedisStringsHandler.set() Error occurred while setting cache entry. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler set() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n try {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n } catch (error) {\n console.error(\n 'RedisStringsHandler.revalidateTag() Error occurred while revalidating tags. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler revalidateTag() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n console.error('Failed to connect subscriber client. Retrying...');\n await this.subscriberClient.connect().catch((error) => {\n console.error('Failed to connect subscriber client.', error);\n throw error;\n });\n });\n\n // Check if keyspace event configuration is set correctly\n if (\n (process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'\n ) {\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') &&\n keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n );\n await Promise.all(operations);\n }\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAiE;;;ACA1D,SAAS,MACd,QAOa,WACV,MACG;AACN,QAAM,YAAY;AAAA,IAChB,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP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,WAAW;AACvB,aAAK,OAAO,KAAK;AACjB,mBAAW,MAAM;AACf,kBAAQ,KAAK,CAAC;AAAA,QAChB,GAAG,GAAG;AAAA,MACR;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;;;AK5vBA,IAAI;AAEJ,IAAqB,gBAArB,MAAmC;AAAA,EACjC,YAAY,SAA2C;AACrD,QAAI,CAAC,eAAe;AAClB,cAAQ,IAAI,wBAAwB;AACpC,sBAAgB,IAAI,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ANrCA,IAAO,gBAAQ;","names":["error"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts"],"sourcesContent":["import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n","import { commandOptions, createClient, RedisClientOptions } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport function redisErrorHandler<T extends Promise<unknown>>(\n debugInfo: string,\n redisCommandResult: T,\n): T {\n const beforeTimestamp = performance.now();\n return redisCommandResult.catch((error) => {\n console.error(\n 'Redis command error',\n (performance.now() - beforeTimestamp).toFixed(2),\n 'ms',\n debugInfo,\n error,\n );\n throw error;\n }) as T;\n}\n\n// This is a test to check if the event loop is lagging. Increase CPU\nsetInterval(() => {\n const start = performance.now();\n setImmediate(() => {\n const duration = performance.now() - start;\n if (duration > 100) {\n console.warn(\n `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms`,\n );\n }\n });\n}, 1000);\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redisUrl to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redisUrl?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n /** Kill container on Redis client error if error threshold is reached\n * @default 0 (0 means no error threshold)\n */\n killContainerOnErrorThreshold?: number;\n /** Additional Redis client socket options\n * @example { tls: true, rejectUnauthorized: false }\n */\n socketOptions?: RedisClientOptions['socket'];\n /** Additional Redis client options to be passed directly to createClient\n * @example { username: 'user', password: 'pass' }\n */\n clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nlet killContainerOnErrorCount: number = 0;\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n private killContainerOnErrorThreshold: number;\n\n constructor({\n redisUrl = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS\n ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000)\n : 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n killContainerOnErrorThreshold = process.env\n .KILL_CONTAINER_ON_ERROR_THRESHOLD\n ? (Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0)\n : 0,\n socketOptions,\n clientOptions,\n }: CreateRedisStringsHandlerOptions) {\n try {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n this.killContainerOnErrorThreshold = killContainerOnErrorThreshold;\n\n try {\n // Create Redis client with properly typed configuration\n this.client = createClient({\n url: redisUrl,\n pingInterval: 10_000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long.\n ...(database !== 0 ? { database } : {}),\n ...(socketOptions ? { socket: { ...socketOptions } } : {}),\n ...(clientOptions || {}),\n });\n\n this.client.on('error', (error) => {\n console.error(\n 'Redis client error',\n error,\n killContainerOnErrorCount++,\n );\n setTimeout(\n () =>\n this.client.connect().catch((error) => {\n console.error(\n 'Failed to reconnect Redis client after connection loss:',\n error,\n );\n }),\n 1000,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n } catch (error) {\n console.error(\n 'RedisStringsHandler constructor error',\n error,\n killContainerOnErrorCount++,\n );\n if (\n killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler constructor error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n process.exit(1);\n }\n throw error;\n }\n }\n\n resetRequestCache(): void {}\n\n private clientReadyCalls = 0;\n\n private async assertClientIsReady(): Promise<void> {\n if (this.clientReadyCalls > 10) {\n throw new Error(\n 'assertClientIsReady called more than 10 times without being ready.',\n );\n }\n await Promise.race([\n Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]),\n new Promise((_, reject) =>\n setTimeout(() => {\n reject(\n new Error(\n 'assertClientIsReady: Timeout waiting for Redis maps to be ready',\n ),\n );\n }, this.timeoutMs * 5),\n ),\n ]);\n this.clientReadyCalls = 0;\n if (!this.client.isReady) {\n throw new Error(\n 'assertClientIsReady: Redis client is not ready yet or connection is lost.',\n );\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n try {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await redisErrorHandler(\n 'RedisStringsHandler.get(), operation: get' +\n (this.redisGetDeduplication ? 'deduplicated' : '') +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n ),\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n } catch (error) {\n // This catch block is necessary to handle any errors that may occur during:\n // 1. Redis operations (get, unlink)\n // 2. JSON parsing of cache entries\n // 3. Tag validation and cleanup\n // If any error occurs, we return null to indicate no valid cache entry was found,\n // allowing the application to regenerate the content rather than crash\n console.error(\n 'RedisStringsHandler.get() Error occurred while getting cache entry. Returning null so site can continue to serve content while cache is disabled. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler get() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n return null;\n }\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n try {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate =\n // For fetch requests in newest versions, the revalidate context property is never used, and instead the revalidate property of the passed-in data is used\n (data.kind === 'FETCH' && data.revalidate) ||\n ctx.revalidate ||\n ctx.cacheControl?.revalidate ||\n (data as { revalidate?: number | false })?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = redisErrorHandler(\n 'RedisStringsHandler.set(), operation: set' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n this.client.set(options, this.keyPrefix + key, serializedCacheEntry, {\n EX: expireAt,\n }),\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n } catch (error) {\n console.error(\n 'RedisStringsHandler.set() Error occurred while setting cache entry. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler set() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n try {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = redisErrorHandler(\n 'RedisStringsHandler.revalidateTag(), operation: unlink' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n fullRedisKeys,\n this.client.unlink(options, fullRedisKeys),\n );\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n } catch (error) {\n console.error(\n 'RedisStringsHandler.revalidateTag() Error occurred while revalidating tags. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler revalidateTag() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport {\n Client,\n getTimeoutRedisCommandOptions,\n redisErrorHandler,\n} from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n console.error('Failed to connect subscriber client. Retrying...');\n await this.subscriberClient.connect().catch((error) => {\n console.error('Failed to connect subscriber client.', error);\n throw error;\n });\n });\n\n // Check if keyspace event configuration is set correctly\n if (\n (process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'\n ) {\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') &&\n keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n redisErrorHandler(\n 'SyncedMap.set(), operation: hSet ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n redisErrorHandler(\n 'SyncedMap.set(), operation: publish ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n ),\n );\n await Promise.all(operations);\n }\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10);\n operations.push(\n redisErrorHandler(\n 'SyncedMap.delete(), operation: hDel ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n this.redisKey +\n ' ' +\n keysArray,\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n ),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n redisErrorHandler(\n 'SyncedMap.delete(), operation: publish ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n keysArray,\n this.client.publish(\n this.syncChannel,\n JSON.stringify(deletionMessage),\n ),\n ),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAiE;;;ACA1D,SAAS,MACd,QAOa,WACV,MACG;AACN,QAAM,YAAY;AAAA,IAChB,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA,EACR;AACA,MAAI,QAAQ,IAAI,qBAAqB;AACnC,YAAQ,IAAI,UAAU,KAAK,GAAG,yBAAyB,GAAG,IAAI;AAAA,EAChE;AACF;AAEO,SAAS,aAAa,UAAkB,MAAiB;AAC9D,MAAI,QAAQ,IAAI,qCAAqC;AACnD,YAAQ,IAAI,YAAY,sBAAsB,GAAG,IAAI;AAAA,EACvD;AACF;;;ACGA,IAAM,sBAAsB;AACrB,IAAM,YAAN,MAAmB;AAAA,EAiBxB,YAAY,SAA2B;AACrC,SAAK,SAAS,QAAQ;AACtB,SAAK,YAAY,QAAQ;AACzB,SAAK,WAAW,QAAQ;AACxB,SAAK,cAAc,GAAG,QAAQ,SAAS,GAAG,mBAAmB,GAAG,QAAQ,QAAQ;AAChF,SAAK,WAAW,QAAQ;AACxB,SAAK,YAAY,QAAQ;AACzB,SAAK,YAAY,QAAQ;AACzB,SAAK,aAAa,QAAQ;AAC1B,SAAK,mBAAmB,QAAQ;AAChC,SAAK,iBAAiB,QAAQ;AAE9B,SAAK,MAAM,oBAAI,IAAe;AAC9B,SAAK,mBAAmB,KAAK,OAAO,UAAU;AAC9C,SAAK,YAAY,IAAI,QAAc,CAAC,YAAY;AAC9C,WAAK,mBAAmB;AAAA,IAC1B,CAAC;AAED,SAAK,MAAM,EAAE,MAAM,CAAC,UAAU;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ACA;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,cAAQ;AAAA,QACN;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACA,UACE,KAAK,gCAAgC,KACrC,6BAA6B,KAAK,+BAClC;AACA,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,UACA;AAAA,QACF;AACA,aAAK,OAAO,WAAW;AACvB,aAAK,OAAO,KAAK;AACjB,mBAAW,MAAM;AACf,kBAAQ,KAAK,CAAC;AAAA,QAChB,GAAG,GAAG;AAAA,MACR;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;;;AKjzBA,IAAI;AAEJ,IAAqB,gBAArB,MAAmC;AAAA,EACjC,YAAY,SAA2C;AACrD,QAAI,CAAC,eAAe;AAClB,cAAQ,IAAI,wBAAwB;AACpC,sBAAgB,IAAI,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ANrCA,IAAO,gBAAQ;","names":["error"]}
package/dist/index.mjs CHANGED
@@ -245,11 +245,14 @@ var SyncedMap = class {
245
245
  if (!this.customizedSync?.withoutRedisHashmap) {
246
246
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
247
247
  operations.push(
248
- this.client.hSet(
249
- options,
250
- this.keyPrefix + this.redisKey,
251
- key,
252
- JSON.stringify(value)
248
+ redisErrorHandler(
249
+ "SyncedMap.set(), operation: hSet " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
250
+ this.client.hSet(
251
+ options,
252
+ this.keyPrefix + this.redisKey,
253
+ key,
254
+ JSON.stringify(value)
255
+ )
253
256
  )
254
257
  );
255
258
  }
@@ -259,7 +262,10 @@ var SyncedMap = class {
259
262
  value
260
263
  };
261
264
  operations.push(
262
- this.client.publish(this.syncChannel, JSON.stringify(insertMessage))
265
+ redisErrorHandler(
266
+ "SyncedMap.set(), operation: publish " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
267
+ this.client.publish(this.syncChannel, JSON.stringify(insertMessage))
268
+ )
263
269
  );
264
270
  await Promise.all(operations);
265
271
  }
@@ -276,9 +282,12 @@ var SyncedMap = class {
276
282
  this.map.delete(key);
277
283
  }
278
284
  if (!this.customizedSync?.withoutRedisHashmap) {
279
- const options = getTimeoutRedisCommandOptions(this.timeoutMs);
285
+ const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10);
280
286
  operations.push(
281
- this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray)
287
+ redisErrorHandler(
288
+ "SyncedMap.delete(), operation: hDel " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + this.redisKey + " " + keysArray,
289
+ this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray)
290
+ )
282
291
  );
283
292
  }
284
293
  if (!withoutSyncMessage) {
@@ -287,7 +296,13 @@ var SyncedMap = class {
287
296
  keys: keysArray
288
297
  };
289
298
  operations.push(
290
- this.client.publish(this.syncChannel, JSON.stringify(deletionMessage))
299
+ redisErrorHandler(
300
+ "SyncedMap.delete(), operation: publish " + this.syncChannel + " " + this.timeoutMs + "ms " + this.keyPrefix + " " + keysArray,
301
+ this.client.publish(
302
+ this.syncChannel,
303
+ JSON.stringify(deletionMessage)
304
+ )
305
+ )
291
306
  );
292
307
  }
293
308
  await Promise.all(operations);
@@ -411,6 +426,30 @@ function bufferReplacer(_, value) {
411
426
  }
412
427
 
413
428
  // src/RedisStringsHandler.ts
429
+ function redisErrorHandler(debugInfo, redisCommandResult) {
430
+ const beforeTimestamp = performance.now();
431
+ return redisCommandResult.catch((error) => {
432
+ console.error(
433
+ "Redis command error",
434
+ (performance.now() - beforeTimestamp).toFixed(2),
435
+ "ms",
436
+ debugInfo,
437
+ error
438
+ );
439
+ throw error;
440
+ });
441
+ }
442
+ setInterval(() => {
443
+ const start = performance.now();
444
+ setImmediate(() => {
445
+ const duration = performance.now() - start;
446
+ if (duration > 100) {
447
+ console.warn(
448
+ `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms`
449
+ );
450
+ }
451
+ });
452
+ }, 1e3);
414
453
  var NEXT_CACHE_IMPLICIT_TAG_ID = "_N_T_";
415
454
  var REVALIDATED_TAGS_KEY = "__revalidated_tags__";
416
455
  function getTimeoutRedisCommandOptions(timeoutMs) {
@@ -596,9 +635,12 @@ var RedisStringsHandler = class {
596
635
  debug("green", "RedisStringsHandler.get() called with", key, ctx);
597
636
  await this.assertClientIsReady();
598
637
  const clientGet = this.redisGetDeduplication ? this.deduplicatedRedisGet(key) : this.redisGet;
599
- const serializedCacheEntry = await clientGet(
600
- getTimeoutRedisCommandOptions(this.timeoutMs),
601
- this.keyPrefix + key
638
+ const serializedCacheEntry = await redisErrorHandler(
639
+ "RedisStringsHandler.get(), operation: get" + (this.redisGetDeduplication ? "deduplicated" : "") + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
640
+ clientGet(
641
+ getTimeoutRedisCommandOptions(this.timeoutMs),
642
+ this.keyPrefix + key
643
+ )
602
644
  );
603
645
  debug(
604
646
  "green",
@@ -734,13 +776,11 @@ var RedisStringsHandler = class {
734
776
  );
735
777
  const expireAt = revalidate && Number.isSafeInteger(revalidate) && revalidate > 0 ? this.estimateExpireAge(revalidate) : this.estimateExpireAge(this.defaultStaleAge);
736
778
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
737
- const setOperation = this.client.set(
738
- options,
739
- this.keyPrefix + key,
740
- serializedCacheEntry,
741
- {
779
+ const setOperation = redisErrorHandler(
780
+ "RedisStringsHandler.set(), operation: set" + this.timeoutMs + "ms " + this.keyPrefix + " " + key,
781
+ this.client.set(options, this.keyPrefix + key, serializedCacheEntry, {
742
782
  EX: expireAt
743
- }
783
+ })
744
784
  );
745
785
  debug(
746
786
  "blue",
@@ -833,7 +873,10 @@ var RedisStringsHandler = class {
833
873
  const redisKeys = Array.from(keysToDelete);
834
874
  const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);
835
875
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
836
- const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);
876
+ const deleteKeysOperation = redisErrorHandler(
877
+ "RedisStringsHandler.revalidateTag(), operation: unlink" + this.timeoutMs + "ms " + this.keyPrefix + " " + fullRedisKeys,
878
+ this.client.unlink(options, fullRedisKeys)
879
+ );
837
880
  if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {
838
881
  for (const key of keysToDelete) {
839
882
  this.inMemoryDeduplicationCache.delete(key);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts","../src/index.ts"],"sourcesContent":["import { commandOptions, createClient, RedisClientOptions } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redisUrl to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redisUrl?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n /** Kill container on Redis client error if error threshold is reached\n * @default 0 (0 means no error threshold)\n */\n killContainerOnErrorThreshold?: number;\n /** Additional Redis client socket options\n * @example { tls: true, rejectUnauthorized: false }\n */\n socketOptions?: RedisClientOptions['socket'];\n /** Additional Redis client options to be passed directly to createClient\n * @example { username: 'user', password: 'pass' }\n */\n clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nlet killContainerOnErrorCount: number = 0;\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n private killContainerOnErrorThreshold: number;\n\n constructor({\n redisUrl = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS\n ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000)\n : 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n killContainerOnErrorThreshold = process.env\n .KILL_CONTAINER_ON_ERROR_THRESHOLD\n ? (Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0)\n : 0,\n socketOptions,\n clientOptions,\n }: CreateRedisStringsHandlerOptions) {\n try {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n this.killContainerOnErrorThreshold = killContainerOnErrorThreshold;\n\n try {\n // Create Redis client with properly typed configuration\n this.client = createClient({\n url: redisUrl,\n pingInterval: 10_000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long.\n ...(database !== 0 ? { database } : {}),\n ...(socketOptions ? { socket: { ...socketOptions } } : {}),\n ...(clientOptions || {}),\n });\n\n this.client.on('error', (error) => {\n console.error(\n 'Redis client error',\n error,\n killContainerOnErrorCount++,\n );\n setTimeout(\n () =>\n this.client.connect().catch((error) => {\n console.error(\n 'Failed to reconnect Redis client after connection loss:',\n error,\n );\n }),\n 1000,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n } catch (error) {\n console.error(\n 'RedisStringsHandler constructor error',\n error,\n killContainerOnErrorCount++,\n );\n if (\n killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler constructor error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n process.exit(1);\n }\n throw error;\n }\n }\n\n resetRequestCache(): void {}\n\n private clientReadyCalls = 0;\n\n private async assertClientIsReady(): Promise<void> {\n if (this.clientReadyCalls > 10) {\n throw new Error(\n 'assertClientIsReady called more than 10 times without being ready.',\n );\n }\n await Promise.race([\n Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]),\n new Promise((_, reject) =>\n setTimeout(() => {\n reject(\n new Error(\n 'assertClientIsReady: Timeout waiting for Redis maps to be ready',\n ),\n );\n }, this.timeoutMs * 5),\n ),\n ]);\n this.clientReadyCalls = 0;\n if (!this.client.isReady) {\n throw new Error(\n 'assertClientIsReady: Redis client is not ready yet or connection is lost.',\n );\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n try {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n } catch (error) {\n // This catch block is necessary to handle any errors that may occur during:\n // 1. Redis operations (get, unlink)\n // 2. JSON parsing of cache entries\n // 3. Tag validation and cleanup\n // If any error occurs, we return null to indicate no valid cache entry was found,\n // allowing the application to regenerate the content rather than crash\n console.error(\n 'RedisStringsHandler.get() Error occurred while getting cache entry. Returning null so site can continue to serve content while cache is disabled. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler get() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n return null;\n }\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n try {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate =\n // For fetch requests in newest versions, the revalidate context property is never used, and instead the revalidate property of the passed-in data is used\n (data.kind === 'FETCH' && data.revalidate) ||\n ctx.revalidate ||\n ctx.cacheControl?.revalidate ||\n (data as { revalidate?: number | false })?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = this.client.set(\n options,\n this.keyPrefix + key,\n serializedCacheEntry,\n {\n EX: expireAt,\n },\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n } catch (error) {\n console.error(\n 'RedisStringsHandler.set() Error occurred while setting cache entry. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler set() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n try {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n } catch (error) {\n console.error(\n 'RedisStringsHandler.revalidateTag() Error occurred while revalidating tags. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler revalidateTag() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n console.error('Failed to connect subscriber client. Retrying...');\n await this.subscriberClient.connect().catch((error) => {\n console.error('Failed to connect subscriber client.', error);\n throw error;\n });\n });\n\n // Check if keyspace event configuration is set correctly\n if (\n (process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'\n ) {\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') &&\n keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n );\n await Promise.all(operations);\n }\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n","import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHand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AAA,QACA;AAAA,QACA;AAAA,MACF;AACA,UACE,KAAK,gCAAgC,KACrC,6BAA6B,KAAK,+BAClC;AACA,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,UACA;AAAA,QACF;AACA,aAAK,OAAO,WAAW;AACvB,aAAK,OAAO,KAAK;AACjB,mBAAW,MAAM;AACf,kBAAQ,KAAK,CAAC;AAAA,QAChB,GAAG,GAAG;AAAA,MACR;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;;;AK5vBA,IAAI;AAEJ,IAAqB,gBAArB,MAAmC;AAAA,EACjC,YAAY,SAA2C;AACrD,QAAI,CAAC,eAAe;AAClB,cAAQ,IAAI,wBAAwB;AACpC,sBAAgB,IAAI,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ACrCA,IAAO,gBAAQ;","names":["error"]}
1
+ {"version":3,"sources":["../src/RedisStringsHandler.ts","../src/utils/debug.ts","../src/SyncedMap.ts","../src/DeduplicatedRequestHandler.ts","../src/utils/json.ts","../src/CachedHandler.ts","../src/index.ts"],"sourcesContent":["import { commandOptions, createClient, RedisClientOptions } from 'redis';\nimport { SyncedMap } from './SyncedMap';\nimport { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';\nimport { debug } from './utils/debug';\nimport { bufferReviver, bufferReplacer } from './utils/json';\n\nexport type CommandOptions = ReturnType<typeof commandOptions>;\nexport type Client = ReturnType<typeof createClient>;\n\nexport type CacheEntry = {\n value: unknown;\n lastModified: number;\n tags: string[];\n};\n\nexport function redisErrorHandler<T extends Promise<unknown>>(\n debugInfo: string,\n redisCommandResult: T,\n): T {\n const beforeTimestamp = performance.now();\n return redisCommandResult.catch((error) => {\n console.error(\n 'Redis command error',\n (performance.now() - beforeTimestamp).toFixed(2),\n 'ms',\n debugInfo,\n error,\n );\n throw error;\n }) as T;\n}\n\n// This is a test to check if the event loop is lagging. Increase CPU\nsetInterval(() => {\n const start = performance.now();\n setImmediate(() => {\n const duration = performance.now() - start;\n if (duration > 100) {\n console.warn(\n `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms`,\n );\n }\n });\n}, 1000);\n\nexport type CreateRedisStringsHandlerOptions = {\n /** Redis redisUrl to use.\n * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379'\n */\n redisUrl?: string;\n /** Redis database number to use. Uses DB 0 for production, DB 1 otherwise\n * @default process.env.VERCEL_ENV === 'production' ? 0 : 1\n */\n database?: number;\n /** Prefix added to all Redis keys\n * @default process.env.VERCEL_URL || 'UNDEFINED_URL_'\n */\n keyPrefix?: string;\n /** Timeout in milliseconds for Redis operations\n * @default 5000\n */\n timeoutMs?: number;\n /** Number of entries to query in one batch during full sync of shared tags hash map\n * @default 250\n */\n revalidateTagQuerySize?: number;\n /** Key used to store shared tags hash map in Redis\n * @default '__sharedTags__'\n */\n sharedTagsKey?: string;\n /** Average interval in milliseconds between tag map full re-syncs\n * @default 3600000 (1 hour)\n */\n avgResyncIntervalMs?: number;\n /** Enable deduplication of Redis get requests via internal in-memory cache\n * @default true\n */\n redisGetDeduplication?: boolean;\n /** Time in milliseconds to cache Redis get results in memory. Set this to 0 to disable in-memory caching completely\n * @default 10000\n */\n inMemoryCachingTime?: number;\n /** Default stale age in seconds for cached items\n * @default 1209600 (14 days)\n */\n defaultStaleAge?: number;\n /** Function to calculate expire age (redis TTL value) from stale age\n * @default Production: staleAge * 2, Other: staleAge * 1.2\n */\n estimateExpireAge?: (staleAge: number) => number;\n /** Kill container on Redis client error if error threshold is reached\n * @default 0 (0 means no error threshold)\n */\n killContainerOnErrorThreshold?: number;\n /** Additional Redis client socket options\n * @example { tls: true, rejectUnauthorized: false }\n */\n socketOptions?: RedisClientOptions['socket'];\n /** Additional Redis client options to be passed directly to createClient\n * @example { username: 'user', password: 'pass' }\n */\n clientOptions?: Omit<RedisClientOptions, 'url' | 'database' | 'socket'>;\n};\n\n// Identifier prefix used by Next.js to mark automatically generated cache tags\n// These tags are created internally by Next.js for route-based invalidation\nconst NEXT_CACHE_IMPLICIT_TAG_ID = '_N_T_';\n\n// Redis key used to store a map of tags and their last revalidation timestamps\n// This helps track when specific tags were last invalidated\nconst REVALIDATED_TAGS_KEY = '__revalidated_tags__';\n\nexport function getTimeoutRedisCommandOptions(\n timeoutMs: number,\n): CommandOptions {\n return commandOptions({ signal: AbortSignal.timeout(timeoutMs) });\n}\n\nlet killContainerOnErrorCount: number = 0;\nexport default class RedisStringsHandler {\n private client: Client;\n private sharedTagsMap: SyncedMap<string[]>;\n private revalidatedTagsMap: SyncedMap<number>;\n private inMemoryDeduplicationCache: SyncedMap<\n Promise<ReturnType<Client['get']>>\n >;\n private redisGet: Client['get'];\n private redisDeduplicationHandler: DeduplicatedRequestHandler<\n Client['get'],\n string | Buffer | null\n >;\n private deduplicatedRedisGet: (key: string) => Client['get'];\n private timeoutMs: number;\n private keyPrefix: string;\n private redisGetDeduplication: boolean;\n private inMemoryCachingTime: number;\n private defaultStaleAge: number;\n private estimateExpireAge: (staleAge: number) => number;\n private killContainerOnErrorThreshold: number;\n\n constructor({\n redisUrl = process.env.REDIS_URL\n ? process.env.REDIS_URL\n : process.env.REDISHOST\n ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n : 'redis://localhost:6379',\n database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n sharedTagsKey = '__sharedTags__',\n timeoutMs = process.env.REDIS_COMMAND_TIMEOUT_MS\n ? (Number.parseInt(process.env.REDIS_COMMAND_TIMEOUT_MS) ?? 5_000)\n : 5_000,\n revalidateTagQuerySize = 250,\n avgResyncIntervalMs = 60 * 60 * 1_000,\n redisGetDeduplication = true,\n inMemoryCachingTime = 10_000,\n defaultStaleAge = 60 * 60 * 24 * 14,\n estimateExpireAge = (staleAge) =>\n process.env.VERCEL_ENV === 'production' ? staleAge * 2 : staleAge * 1.2,\n killContainerOnErrorThreshold = process.env\n .KILL_CONTAINER_ON_ERROR_THRESHOLD\n ? (Number.parseInt(process.env.KILL_CONTAINER_ON_ERROR_THRESHOLD) ?? 0)\n : 0,\n socketOptions,\n clientOptions,\n }: CreateRedisStringsHandlerOptions) {\n try {\n this.keyPrefix = keyPrefix;\n this.timeoutMs = timeoutMs;\n this.redisGetDeduplication = redisGetDeduplication;\n this.inMemoryCachingTime = inMemoryCachingTime;\n this.defaultStaleAge = defaultStaleAge;\n this.estimateExpireAge = estimateExpireAge;\n this.killContainerOnErrorThreshold = killContainerOnErrorThreshold;\n\n try {\n // Create Redis client with properly typed configuration\n this.client = createClient({\n url: redisUrl,\n pingInterval: 10_000, // Useful with Redis deployments that do not use TCP Keep-Alive. Restarts the connection if it is idle for too long.\n ...(database !== 0 ? { database } : {}),\n ...(socketOptions ? { socket: { ...socketOptions } } : {}),\n ...(clientOptions || {}),\n });\n\n this.client.on('error', (error) => {\n console.error(\n 'Redis client error',\n error,\n killContainerOnErrorCount++,\n );\n setTimeout(\n () =>\n this.client.connect().catch((error) => {\n console.error(\n 'Failed to reconnect Redis client after connection loss:',\n error,\n );\n }),\n 1000,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'Redis client error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n });\n\n this.client\n .connect()\n .then(() => {\n console.info('Redis client connected.');\n })\n .catch(() => {\n this.client.connect().catch((error) => {\n console.error('Failed to connect Redis client:', error);\n this.client.disconnect();\n throw error;\n });\n });\n } catch (error: unknown) {\n console.error('Failed to initialize Redis client');\n throw error;\n }\n\n const filterKeys = (key: string): boolean =>\n key !== REVALIDATED_TAGS_KEY && key !== sharedTagsKey;\n\n this.sharedTagsMap = new SyncedMap<string[]>({\n client: this.client,\n keyPrefix,\n redisKey: sharedTagsKey,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs -\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.revalidatedTagsMap = new SyncedMap<number>({\n client: this.client,\n keyPrefix,\n redisKey: REVALIDATED_TAGS_KEY,\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n resyncIntervalMs:\n avgResyncIntervalMs +\n avgResyncIntervalMs / 10 +\n Math.random() * (avgResyncIntervalMs / 10),\n });\n\n this.inMemoryDeduplicationCache = new SyncedMap({\n client: this.client,\n keyPrefix,\n redisKey: 'inMemoryDeduplicationCache',\n database,\n timeoutMs,\n querySize: revalidateTagQuerySize,\n filterKeys,\n customizedSync: {\n withoutRedisHashmap: true,\n withoutSetSync: true,\n },\n });\n\n const redisGet: Client['get'] = this.client.get.bind(this.client);\n this.redisDeduplicationHandler = new DeduplicatedRequestHandler(\n redisGet,\n inMemoryCachingTime,\n this.inMemoryDeduplicationCache,\n );\n this.redisGet = redisGet;\n this.deduplicatedRedisGet =\n this.redisDeduplicationHandler.deduplicatedFunction;\n } catch (error) {\n console.error(\n 'RedisStringsHandler constructor error',\n error,\n killContainerOnErrorCount++,\n );\n if (\n killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler constructor error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount++,\n );\n process.exit(1);\n }\n throw error;\n }\n }\n\n resetRequestCache(): void {}\n\n private clientReadyCalls = 0;\n\n private async assertClientIsReady(): Promise<void> {\n if (this.clientReadyCalls > 10) {\n throw new Error(\n 'assertClientIsReady called more than 10 times without being ready.',\n );\n }\n await Promise.race([\n Promise.all([\n this.sharedTagsMap.waitUntilReady(),\n this.revalidatedTagsMap.waitUntilReady(),\n ]),\n new Promise((_, reject) =>\n setTimeout(() => {\n reject(\n new Error(\n 'assertClientIsReady: Timeout waiting for Redis maps to be ready',\n ),\n );\n }, this.timeoutMs * 5),\n ),\n ]);\n this.clientReadyCalls = 0;\n if (!this.client.isReady) {\n throw new Error(\n 'assertClientIsReady: Redis client is not ready yet or connection is lost.',\n );\n }\n }\n\n public async get(\n key: string,\n ctx:\n | {\n kind: 'APP_ROUTE' | 'APP_PAGE';\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n }\n | {\n kind: 'FETCH';\n revalidate: number;\n fetchUrl: string;\n fetchIdx: number;\n tags: string[];\n softTags: string[];\n isFallback: boolean;\n },\n ): Promise<CacheEntry | null> {\n try {\n if (\n ctx.kind !== 'APP_ROUTE' &&\n ctx.kind !== 'APP_PAGE' &&\n ctx.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (ctx as { kind: string })?.kind,\n );\n }\n\n debug('green', 'RedisStringsHandler.get() called with', key, ctx);\n await this.assertClientIsReady();\n\n const clientGet = this.redisGetDeduplication\n ? this.deduplicatedRedisGet(key)\n : this.redisGet;\n const serializedCacheEntry = await redisErrorHandler(\n 'RedisStringsHandler.get(), operation: get' +\n (this.redisGetDeduplication ? 'deduplicated' : '') +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n clientGet(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + key,\n ),\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (serializedCacheEntry)',\n serializedCacheEntry?.substring(0, 200),\n );\n\n if (!serializedCacheEntry) {\n return null;\n }\n\n const cacheEntry: CacheEntry | null = JSON.parse(\n serializedCacheEntry,\n bufferReviver,\n );\n\n debug(\n 'green',\n 'RedisStringsHandler.get() finished with result (cacheEntry)',\n JSON.stringify(cacheEntry).substring(0, 200),\n );\n\n if (!cacheEntry) {\n return null;\n }\n\n if (!cacheEntry?.tags) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing tags)',\n );\n }\n if (!cacheEntry?.value) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing value)',\n );\n }\n if (!cacheEntry?.lastModified) {\n console.warn(\n 'RedisStringsHandler.get() called with',\n key,\n ctx,\n 'cacheEntry is mall formed (missing lastModified)',\n );\n }\n\n if (ctx.kind === 'FETCH') {\n const combinedTags = new Set([\n ...(ctx?.softTags || []),\n ...(ctx?.tags || []),\n ]);\n\n if (combinedTags.size === 0) {\n return cacheEntry;\n }\n\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route). See revalidateTag() for more information\n //\n // This code checks if any of the cache tags associated with this entry (normally the internal tag of the parent page/api route containing the fetch request)\n // have been revalidated since the entry was last modified. If any tag was revalidated more recently than the entry's\n // lastModified timestamp, then the cached content is considered stale (therefore return null) and should be removed.\n for (const tag of combinedTags) {\n // Get the last revalidation time for this tag from our revalidatedTagsMap\n const revalidationTime = this.revalidatedTagsMap.get(tag);\n\n // If we have a revalidation time for this tag and it's more recent than when\n // this cache entry was last modified, the entry is stale\n if (revalidationTime && revalidationTime > cacheEntry.lastModified) {\n const redisKey = this.keyPrefix + key;\n\n // We don't await this cleanup since it can happen asynchronously in the background.\n // The cache entry is already considered invalid at this point.\n this.client\n .unlink(getTimeoutRedisCommandOptions(this.timeoutMs), redisKey)\n .catch((err) => {\n // If the first unlink fails, only log the error\n // Never implement a retry here as the cache entry will be updated directly after this get request\n console.error(\n 'Error occurred while unlinking stale data. Error was:',\n err,\n );\n })\n .finally(async () => {\n // Clean up our tag tracking maps after the Redis key is removed\n await this.sharedTagsMap.delete(key);\n await this.revalidatedTagsMap.delete(tag);\n });\n\n debug(\n 'green',\n 'RedisStringsHandler.get() found revalidation time for tag. Cache entry is stale and will be deleted and \"null\" will be returned.',\n tag,\n redisKey,\n revalidationTime,\n cacheEntry,\n );\n\n // Return null to indicate no valid cache entry was found\n return null;\n }\n }\n }\n\n return cacheEntry;\n } catch (error) {\n // This catch block is necessary to handle any errors that may occur during:\n // 1. Redis operations (get, unlink)\n // 2. JSON parsing of cache entries\n // 3. Tag validation and cleanup\n // If any error occurs, we return null to indicate no valid cache entry was found,\n // allowing the application to regenerate the content rather than crash\n console.error(\n 'RedisStringsHandler.get() Error occurred while getting cache entry. Returning null so site can continue to serve content while cache is disabled. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler get() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n return null;\n }\n }\n public async set(\n key: string,\n data:\n | {\n kind: 'APP_PAGE';\n status: number;\n headers: {\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n html: string;\n rscData: Buffer;\n segmentData: unknown;\n postboned: unknown;\n }\n | {\n kind: 'APP_ROUTE';\n status: number;\n headers: {\n 'cache-control'?: string;\n 'x-nextjs-stale-time': string; // timestamp in ms\n 'x-next-cache-tags': string; // comma separated paths (tags)\n };\n body: Buffer;\n }\n | {\n kind: 'FETCH';\n data: {\n headers: Record<string, string>;\n body: string; // base64 encoded\n status: number;\n url: string;\n };\n revalidate: number | false;\n },\n ctx: {\n isRoutePPREnabled: boolean;\n isFallback: boolean;\n tags?: string[];\n // Different versions of Next.js use different arguments for the same functionality\n revalidate?: number | false; // Version 15.0.3\n cacheControl?: { revalidate: 5; expire: undefined }; // Version 15.0.3\n },\n ) {\n try {\n if (\n data.kind !== 'APP_ROUTE' &&\n data.kind !== 'APP_PAGE' &&\n data.kind !== 'FETCH'\n ) {\n console.warn(\n 'RedisStringsHandler.set() called with',\n key,\n ctx,\n data,\n ' this cache handler is only designed and tested for kind APP_ROUTE and APP_PAGE and not for kind ',\n (data as { kind: string })?.kind,\n );\n }\n\n await this.assertClientIsReady();\n\n if (data.kind === 'APP_PAGE' || data.kind === 'APP_ROUTE') {\n const tags = data.headers['x-next-cache-tags']?.split(',');\n ctx.tags = [...(ctx.tags || []), ...(tags || [])];\n }\n\n // Constructing and serializing the value for storing it in redis\n const cacheEntry: CacheEntry = {\n lastModified: Date.now(),\n tags: ctx?.tags || [],\n value: data,\n };\n const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);\n\n // pre seed data into deduplicated get client. This will reduce redis load by not requesting\n // the same value from redis which was just set.\n if (this.redisGetDeduplication) {\n this.redisDeduplicationHandler.seedRequestReturn(\n key,\n serializedCacheEntry,\n );\n }\n\n // TODO: implement expiration based on cacheControl.expire argument, -> probably relevant for cacheLife and \"use cache\" etc.: https://nextjs.org/docs/app/api-reference/functions/cacheLife\n // Constructing the expire time for the cache entry\n const revalidate =\n // For fetch requests in newest versions, the revalidate context property is never used, and instead the revalidate property of the passed-in data is used\n (data.kind === 'FETCH' && data.revalidate) ||\n ctx.revalidate ||\n ctx.cacheControl?.revalidate ||\n (data as { revalidate?: number | false })?.revalidate;\n const expireAt =\n revalidate && Number.isSafeInteger(revalidate) && revalidate > 0\n ? this.estimateExpireAge(revalidate)\n : this.estimateExpireAge(this.defaultStaleAge);\n\n // Setting the cache entry in redis\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const setOperation: Promise<string | null> = redisErrorHandler(\n 'RedisStringsHandler.set(), operation: set' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n this.client.set(options, this.keyPrefix + key, serializedCacheEntry, {\n EX: expireAt,\n }),\n );\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following serializedCacheEntry',\n this.keyPrefix,\n key,\n data,\n ctx,\n serializedCacheEntry?.substring(0, 200),\n expireAt,\n );\n\n // Setting the tags for the cache entry in the sharedTagsMap (locally stored hashmap synced via redis)\n let setTagsOperation: Promise<void> | undefined;\n if (ctx.tags && ctx.tags.length > 0) {\n const currentTags = this.sharedTagsMap.get(key);\n const currentIsSameAsNew =\n currentTags?.length === ctx.tags.length &&\n currentTags.every((v) => ctx.tags!.includes(v)) &&\n ctx.tags.every((v) => currentTags.includes(v));\n\n if (!currentIsSameAsNew) {\n setTagsOperation = this.sharedTagsMap.set(\n key,\n structuredClone(ctx.tags) as string[],\n );\n }\n }\n\n debug(\n 'blue',\n 'RedisStringsHandler.set() will set the following sharedTagsMap',\n key,\n ctx.tags as string[],\n );\n\n await Promise.all([setOperation, setTagsOperation]);\n } catch (error) {\n console.error(\n 'RedisStringsHandler.set() Error occurred while setting cache entry. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler set() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\n try {\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() called with',\n tagOrTags,\n rest,\n );\n const tags = new Set([tagOrTags || []].flat());\n await this.assertClientIsReady();\n\n // find all keys that are related to this tag\n const keysToDelete: Set<string> = new Set();\n\n for (const tag of tags) {\n // INFO: implicit tags (revalidate of nested fetch in api route/page on revalidatePath call of the page/api route)\n //\n // Invalidation logic for fetch requests that are related to a invalidated page.\n // revalidateTag is called for the page tag (_N_T_...) and the fetch request needs to be invalidated as well\n // unfortunately this is not possible since the revalidateTag is not called with any data that would allow us to find the cache entry of the fetch request\n // in case of a fetch request get method call, the get method of the cache handler is called with some information about the pages/routes the fetch request is inside\n // therefore we only mark the page/route as stale here (with help of the revalidatedTagsMap)\n // and delete the cache entry of the fetch request on the next request to the get function\n if (tag.startsWith(NEXT_CACHE_IMPLICIT_TAG_ID)) {\n const now = Date.now();\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() set revalidation time for tag',\n tag,\n 'to',\n now,\n );\n await this.revalidatedTagsMap.set(tag, now);\n }\n }\n\n // Scan the whole sharedTagsMap for keys that are dependent on any of the revalidated tags\n for (const [key, sharedTags] of this.sharedTagsMap.entries()) {\n if (sharedTags.some((tag) => tags.has(tag))) {\n keysToDelete.add(key);\n }\n }\n\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() found',\n keysToDelete,\n 'keys to delete',\n );\n\n // exit early if no keys are related to this tag\n if (keysToDelete.size === 0) {\n return;\n }\n\n // prepare deletion of all keys in redis that are related to this tag\n const redisKeys = Array.from(keysToDelete);\n const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n const deleteKeysOperation = redisErrorHandler(\n 'RedisStringsHandler.revalidateTag(), operation: unlink' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n fullRedisKeys,\n this.client.unlink(options, fullRedisKeys),\n );\n\n // also delete entries from in-memory deduplication cache if they get revalidated\n if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {\n for (const key of keysToDelete) {\n this.inMemoryDeduplicationCache.delete(key);\n }\n }\n\n // prepare deletion of entries from shared tags map if they get revalidated so that the map will not grow indefinitely\n const deleteTagsOperation = this.sharedTagsMap.delete(redisKeys);\n\n // execute keys and tag maps deletion\n await Promise.all([deleteKeysOperation, deleteTagsOperation]);\n debug(\n 'red',\n 'RedisStringsHandler.revalidateTag() finished delete operations',\n );\n } catch (error) {\n console.error(\n 'RedisStringsHandler.revalidateTag() Error occurred while revalidating tags. The original error was:',\n error,\n killContainerOnErrorCount++,\n );\n if (\n this.killContainerOnErrorThreshold > 0 &&\n killContainerOnErrorCount >= this.killContainerOnErrorThreshold\n ) {\n console.error(\n 'RedisStringsHandler revalidateTag() error threshold reached, disconnecting and exiting (please implement a restart process/container watchdog to handle this error)',\n error,\n killContainerOnErrorCount,\n );\n this.client.disconnect();\n this.client.quit();\n setTimeout(() => {\n process.exit(1);\n }, 500);\n }\n throw error;\n }\n }\n}\n","export function debug(\n color:\n | 'red'\n | 'blue'\n | 'green'\n | 'yellow'\n | 'cyan'\n | 'white'\n | 'none' = 'none',\n ...args: unknown[]\n): void {\n const colorCode = {\n red: '\\x1b[31m',\n blue: '\\x1b[34m',\n green: '\\x1b[32m',\n yellow: '\\x1b[33m',\n cyan: '\\x1b[36m',\n white: '\\x1b[37m',\n none: '',\n };\n if (process.env.DEBUG_CACHE_HANDLER) {\n console.log(colorCode[color], 'DEBUG CACHE HANDLER: ', ...args);\n }\n}\n\nexport function debugVerbose(color: string, ...args: unknown[]) {\n if (process.env.DEBUG_CACHE_HANDLER_VERBOSE_VERBOSE) {\n console.log('\\x1b[35m', 'DEBUG SYNCED MAP: ', ...args);\n }\n}\n","// SyncedMap.ts\nimport {\n Client,\n getTimeoutRedisCommandOptions,\n redisErrorHandler,\n} from './RedisStringsHandler';\nimport { debugVerbose, debug } from './utils/debug';\n\ntype CustomizedSync = {\n withoutRedisHashmap?: boolean;\n withoutSetSync?: boolean;\n};\n\ntype SyncedMapOptions = {\n client: Client;\n keyPrefix: string;\n redisKey: string; // Redis Hash key\n database: number;\n timeoutMs: number;\n querySize: number;\n filterKeys: (key: string) => boolean;\n resyncIntervalMs?: number;\n customizedSync?: CustomizedSync;\n};\n\nexport type SyncMessage<V> = {\n type: 'insert' | 'delete';\n key?: string;\n value?: V;\n keys?: string[];\n};\n\nconst SYNC_CHANNEL_SUFFIX = ':sync-channel:';\nexport class SyncedMap<V> {\n private client: Client;\n private subscriberClient: Client;\n private map: Map<string, V>;\n private keyPrefix: string;\n private syncChannel: string;\n private redisKey: string;\n private database: number;\n private timeoutMs: number;\n private querySize: number;\n private filterKeys: (key: string) => boolean;\n private resyncIntervalMs?: number;\n private customizedSync?: CustomizedSync;\n\n private setupLock: Promise<void>;\n private setupLockResolve!: () => void;\n\n constructor(options: SyncedMapOptions) {\n this.client = options.client;\n this.keyPrefix = options.keyPrefix;\n this.redisKey = options.redisKey;\n this.syncChannel = `${options.keyPrefix}${SYNC_CHANNEL_SUFFIX}${options.redisKey}`;\n this.database = options.database;\n this.timeoutMs = options.timeoutMs;\n this.querySize = options.querySize;\n this.filterKeys = options.filterKeys;\n this.resyncIntervalMs = options.resyncIntervalMs;\n this.customizedSync = options.customizedSync;\n\n this.map = new Map<string, V>();\n this.subscriberClient = this.client.duplicate();\n this.setupLock = new Promise<void>((resolve) => {\n this.setupLockResolve = resolve;\n });\n\n this.setup().catch((error) => {\n console.error('Failed to setup SyncedMap:', error);\n throw error;\n });\n }\n\n private async setup() {\n let setupPromises: Promise<void>[] = [];\n if (!this.customizedSync?.withoutRedisHashmap) {\n setupPromises.push(this.initialSync());\n this.setupPeriodicResync();\n }\n setupPromises.push(this.setupPubSub());\n await Promise.all(setupPromises);\n this.setupLockResolve();\n }\n\n private async initialSync() {\n let cursor = 0;\n const hScanOptions = { COUNT: this.querySize };\n\n try {\n do {\n const remoteItems = await this.client.hScan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n this.keyPrefix + this.redisKey,\n cursor,\n hScanOptions,\n );\n for (const { field, value } of remoteItems.tuples) {\n if (this.filterKeys(field)) {\n const parsedValue = JSON.parse(value);\n this.map.set(field, parsedValue);\n }\n }\n cursor = remoteItems.cursor;\n } while (cursor !== 0);\n\n // Clean up keys not in Redis\n await this.cleanupKeysNotInRedis();\n } catch (error) {\n console.error('Error during initial sync:', error);\n throw error;\n }\n }\n\n private async cleanupKeysNotInRedis() {\n let cursor = 0;\n const scanOptions = { COUNT: this.querySize, MATCH: `${this.keyPrefix}*` };\n let remoteKeys: string[] = [];\n try {\n do {\n const remoteKeysPortion = await this.client.scan(\n getTimeoutRedisCommandOptions(this.timeoutMs),\n cursor,\n scanOptions,\n );\n remoteKeys = remoteKeys.concat(remoteKeysPortion.keys);\n cursor = remoteKeysPortion.cursor;\n } while (cursor !== 0);\n\n const remoteKeysSet = new Set(\n remoteKeys.map((key) => key.substring(this.keyPrefix.length)),\n );\n\n const keysToDelete: string[] = [];\n for (const key of this.map.keys()) {\n const keyStr = key as unknown as string;\n if (!remoteKeysSet.has(keyStr) && this.filterKeys(keyStr)) {\n keysToDelete.push(keyStr);\n }\n }\n\n if (keysToDelete.length > 0) {\n await this.delete(keysToDelete);\n }\n } catch (error) {\n console.error('Error during cleanup of keys not in Redis:', error);\n throw error;\n }\n }\n\n private setupPeriodicResync() {\n if (this.resyncIntervalMs && this.resyncIntervalMs > 0) {\n setInterval(() => {\n this.initialSync().catch((error) => {\n console.error('Error during periodic resync:', error);\n });\n }, this.resyncIntervalMs);\n }\n }\n\n private async setupPubSub() {\n const syncHandler = async (message: string) => {\n const syncMessage: SyncMessage<V> = JSON.parse(message);\n if (syncMessage.type === 'insert') {\n if (syncMessage.key !== undefined && syncMessage.value !== undefined) {\n this.map.set(syncMessage.key, syncMessage.value);\n }\n } else if (syncMessage.type === 'delete') {\n if (syncMessage.keys) {\n for (const key of syncMessage.keys) {\n this.map.delete(key);\n }\n }\n }\n };\n\n const keyEventHandler = async (key: string, message: string) => {\n debug(\n 'yellow',\n 'SyncedMap.keyEventHandler() called with message',\n this.redisKey,\n message,\n key,\n );\n // const key = message;\n if (key.startsWith(this.keyPrefix)) {\n const keyInMap = key.substring(this.keyPrefix.length);\n if (this.filterKeys(keyInMap)) {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key matches filter and will be deleted',\n this.redisKey,\n message,\n key,\n );\n await this.delete(keyInMap, true);\n }\n } else {\n debugVerbose(\n 'SyncedMap.keyEventHandler() key does not have prefix',\n this.redisKey,\n message,\n key,\n );\n }\n };\n\n try {\n await this.subscriberClient.connect().catch(async () => {\n console.error('Failed to connect subscriber client. Retrying...');\n await this.subscriberClient.connect().catch((error) => {\n console.error('Failed to connect subscriber client.', error);\n throw error;\n });\n });\n\n // Check if keyspace event configuration is set correctly\n if (\n (process.env.SKIP_KEYSPACE_CONFIG_CHECK || '').toUpperCase() !== 'TRUE'\n ) {\n const keyspaceEventConfig = (\n await this.subscriberClient.configGet('notify-keyspace-events')\n )?.['notify-keyspace-events'];\n if (!keyspaceEventConfig.includes('E')) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'E' for Keyevent events, published with __keyevent@<db>__ prefix. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n if (\n !keyspaceEventConfig.includes('A') &&\n !(\n keyspaceEventConfig.includes('x') &&\n keyspaceEventConfig.includes('e')\n )\n ) {\n throw new Error(\n 'Keyspace event configuration is set to \"' +\n keyspaceEventConfig +\n \"\\\" but has to include 'A' or 'x' and 'e' for expired and evicted events. We recommend to set it to 'Exe' like so `redis-cli -h localhost config set notify-keyspace-events Exe`\",\n );\n }\n }\n\n await Promise.all([\n // We use a custom channel for insert/delete For the following reason:\n // With custom channel we can delete multiple entries in one message. If we would listen to unlink / del we\n // could get thousands of messages for one revalidateTag (For example revalidateTag(\"algolia\") would send an enormous amount of network packages)\n // Also we can send the value in the message for insert\n this.subscriberClient.subscribe(this.syncChannel, syncHandler),\n // Subscribe to Redis keyevent notifications for evicted and expired keys\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:evicted`,\n keyEventHandler,\n ),\n this.subscriberClient.subscribe(\n `__keyevent@${this.database}__:expired`,\n keyEventHandler,\n ),\n ]);\n\n // Error handling for reconnection\n this.subscriberClient.on('error', async (err) => {\n console.error('Subscriber client error:', err);\n try {\n await this.subscriberClient.quit();\n this.subscriberClient = this.client.duplicate();\n await this.setupPubSub();\n } catch (reconnectError) {\n console.error(\n 'Failed to reconnect subscriber client:',\n reconnectError,\n );\n }\n });\n } catch (error) {\n console.error('Error setting up pub/sub client:', error);\n throw error;\n }\n }\n\n public async waitUntilReady() {\n await this.setupLock;\n }\n\n public get(key: string): V | undefined {\n debugVerbose(\n 'SyncedMap.get() called with key',\n key,\n JSON.stringify(this.map.get(key))?.substring(0, 100),\n );\n return this.map.get(key);\n }\n\n public async set(key: string, value: V): Promise<void> {\n debugVerbose(\n 'SyncedMap.set() called with key',\n key,\n JSON.stringify(value)?.substring(0, 100),\n );\n this.map.set(key, value);\n const operations = [];\n\n // This is needed if we only want to sync delete commands. This is especially useful for non serializable data like a promise map\n if (this.customizedSync?.withoutSetSync) {\n return;\n }\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs);\n operations.push(\n redisErrorHandler(\n 'SyncedMap.set(), operation: hSet ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n this.client.hSet(\n options,\n this.keyPrefix + this.redisKey,\n key as unknown as string,\n JSON.stringify(value),\n ),\n ),\n );\n }\n\n const insertMessage: SyncMessage<V> = {\n type: 'insert',\n key: key as unknown as string,\n value,\n };\n operations.push(\n redisErrorHandler(\n 'SyncedMap.set(), operation: publish ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n key,\n this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),\n ),\n );\n await Promise.all(operations);\n }\n\n public async delete(\n keys: string[] | string,\n withoutSyncMessage = false,\n ): Promise<void> {\n debugVerbose(\n 'SyncedMap.delete() called with keys',\n this.redisKey,\n keys,\n withoutSyncMessage,\n );\n\n const keysArray = Array.isArray(keys) ? keys : [keys];\n const operations = [];\n\n for (const key of keysArray) {\n this.map.delete(key);\n }\n\n if (!this.customizedSync?.withoutRedisHashmap) {\n const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10);\n operations.push(\n redisErrorHandler(\n 'SyncedMap.delete(), operation: hDel ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n this.redisKey +\n ' ' +\n keysArray,\n this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),\n ),\n );\n }\n\n if (!withoutSyncMessage) {\n const deletionMessage: SyncMessage<V> = {\n type: 'delete',\n keys: keysArray,\n };\n operations.push(\n redisErrorHandler(\n 'SyncedMap.delete(), operation: publish ' +\n this.syncChannel +\n ' ' +\n this.timeoutMs +\n 'ms' +\n ' ' +\n this.keyPrefix +\n ' ' +\n keysArray,\n this.client.publish(\n this.syncChannel,\n JSON.stringify(deletionMessage),\n ),\n ),\n );\n }\n\n await Promise.all(operations);\n debugVerbose(\n 'SyncedMap.delete() finished operations',\n this.redisKey,\n keys,\n operations.length,\n );\n }\n\n public has(key: string): boolean {\n return this.map.has(key);\n }\n\n public entries(): IterableIterator<[string, V]> {\n return this.map.entries();\n }\n}\n","import { debugVerbose } from './utils/debug';\nimport { SyncedMap } from './SyncedMap';\nexport class DeduplicatedRequestHandler<\n T extends (...args: [never, never]) => Promise<K>,\n K,\n> {\n private inMemoryDeduplicationCache: SyncedMap<Promise<K>>;\n private cachingTimeMs: number;\n private fn: T;\n\n constructor(\n fn: T,\n cachingTimeMs: number,\n inMemoryDeduplicationCache: SyncedMap<Promise<K>>,\n ) {\n this.fn = fn;\n this.cachingTimeMs = cachingTimeMs;\n this.inMemoryDeduplicationCache = inMemoryDeduplicationCache;\n }\n\n // Method to manually seed a result into the cache\n seedRequestReturn(key: string, value: K): void {\n const resultPromise = new Promise<K>((res) => res(value));\n this.inMemoryDeduplicationCache.set(key, resultPromise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.seedRequestReturn() seeded result ',\n key,\n (value as string).substring(0, 200),\n );\n\n setTimeout(() => {\n this.inMemoryDeduplicationCache.delete(key);\n }, this.cachingTimeMs);\n }\n\n // Method to handle deduplicated requests\n deduplicatedFunction = (key: string): T => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction() called with',\n key,\n );\n //eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const dedupedFn = async (...args: [never, never]): Promise<K> => {\n // If there's already a pending request with the same key, return it\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn called with',\n key,\n );\n if (\n self.inMemoryDeduplicationCache &&\n self.inMemoryDeduplicationCache.has(key)\n ) {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache',\n );\n const res = await self.inMemoryDeduplicationCache\n .get(key)!\n .then((v) => structuredClone(v));\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'found key in inMemoryDeduplicationCache and served result from there',\n JSON.stringify(res).substring(0, 200),\n );\n return res;\n }\n\n // If no pending request, call the original function and store the promise\n const promise = self.fn(...args);\n self.inMemoryDeduplicationCache.set(key, promise);\n\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'did not found key in inMemoryDeduplicationCache. Setting it now and waiting for promise to resolve',\n );\n\n try {\n const ts = performance.now();\n const result = await promise;\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'promise resolved (in ',\n performance.now() - ts,\n 'ms). Returning result',\n JSON.stringify(result).substring(0, 200),\n );\n return structuredClone(result);\n } finally {\n // Once the promise is resolved/rejected and caching timeout is over, remove it from the map\n setTimeout(() => {\n debugVerbose(\n 'DeduplicatedRequestHandler.deduplicatedFunction().dedupedFn ',\n key,\n 'deleting key from inMemoryDeduplicationCache after ',\n self.cachingTimeMs,\n 'ms',\n );\n self.inMemoryDeduplicationCache.delete(key);\n }, self.cachingTimeMs);\n }\n };\n return dedupedFn as T;\n };\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReviver(_: string, value: any): any {\n if (value && typeof value === 'object' && typeof value.$binary === 'string') {\n return Buffer.from(value.$binary, 'base64');\n }\n return value;\n}\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function bufferReplacer(_: string, value: any): any {\n if (Buffer.isBuffer(value)) {\n return {\n $binary: value.toString('base64'),\n };\n }\n if (\n value &&\n typeof value === 'object' &&\n value?.type === 'Buffer' &&\n Array.isArray(value.data)\n ) {\n return {\n $binary: Buffer.from(value.data).toString('base64'),\n };\n }\n return value;\n}\n","import RedisStringsHandler, {\n CreateRedisStringsHandlerOptions,\n} from './RedisStringsHandler';\nimport { debugVerbose } from './utils/debug';\n\nlet cachedHandler: RedisStringsHandler;\n\nexport default class CachedHandler {\n constructor(options: CreateRedisStringsHandlerOptions) {\n if (!cachedHandler) {\n console.log('created cached handler');\n cachedHandler = new RedisStringsHandler(options);\n }\n }\n get(\n ...args: Parameters<RedisStringsHandler['get']>\n ): ReturnType<RedisStringsHandler['get']> {\n debugVerbose('CachedHandler.get called with', args);\n return cachedHandler.get(...args);\n }\n set(\n ...args: Parameters<RedisStringsHandler['set']>\n ): ReturnType<RedisStringsHandler['set']> {\n debugVerbose('CachedHandler.set called with', args);\n return cachedHandler.set(...args);\n }\n revalidateTag(\n ...args: Parameters<RedisStringsHandler['revalidateTag']>\n ): ReturnType<RedisStringsHandler['revalidateTag']> {\n debugVerbose('CachedHandler.revalidateTag called with', args);\n return cachedHandler.revalidateTag(...args);\n }\n resetRequestCache(\n ...args: Parameters<RedisStringsHandler['resetRequestCache']>\n ): ReturnType<RedisStringsHandler['resetRequestCache']> {\n // debug(\"CachedHandler.resetRequestCache called with\", args);\n return cachedHandler.resetRequestCache(...args);\n }\n}\n","import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n"],"mappings":";AAAA,SAAS,gBAAgB,oBAAwC;;;ACA1D,SAAS,MACd,QAOa,WACV,MACG;AACN,QAAM,YAAY;AAAA,IAChB,KAAK;AAAA,IACL,MAAM;AAAA,IACN,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA,EACR;AACA,MAAI,QAAQ,IAAI,qBAAqB;AACnC,YAAQ,IAAI,UAAU,KAAK,GAAG,yBAAyB,GAAG,IAAI;AAAA,EAChE;AACF;AAEO,SAAS,aAAa,UAAkB,MAAiB;AAC9D,MAAI,QAAQ,IAAI,qCAAqC;AACnD,YAAQ,IAAI,YAAY,sBAAsB,GAAG,IAAI;AAAA,EACvD;AACF;;;ACGA,IAAM,sBAAsB;AACrB,IAAM,YAAN,MAAmB;AAAA,EAiBxB,YAAY,SAA2B;AACrC,SAAK,SAAS,QAAQ;AACtB,SAAK,YAAY,QAAQ;AACzB,SAAK,WAAW,QAAQ;AACxB,SAAK,cAAc,GAAG,QAAQ,SAAS,GAAG,mBAAmB,GAAG,QAAQ,QAAQ;AAChF,SAAK,WAAW,QAAQ;AACxB,SAAK,YAAY,QAAQ;AACzB,SAAK,YAAY,QAAQ;AACzB,SAAK,aAAa,QAAQ;AAC1B,SAAK,mBAAmB,QAAQ;AAChC,SAAK,iBAAiB,QAAQ;AAE9B,SAAK,MAAM,oBAAI,IAAe;AAC9B,SAAK,mBAAmB,KAAK,OAAO,UAAU;AAC9C,SAAK,YAA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,KAAK,cAAc,OAAO,SAAS;AAG/D,YAAM,QAAQ,IAAI,CAAC,qBAAqB,mBAAmB,CAAC;AAC5D;AAAA,QACE;AAAA,QACA;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,cAAQ;AAAA,QACN;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACA,UACE,KAAK,gCAAgC,KACrC,6BAA6B,KAAK,+BAClC;AACA,gBAAQ;AAAA,UACN;AAAA,UACA;AAAA,UACA;AAAA,QACF;AACA,aAAK,OAAO,WAAW;AACvB,aAAK,OAAO,KAAK;AACjB,mBAAW,MAAM;AACf,kBAAQ,KAAK,CAAC;AAAA,QAChB,GAAG,GAAG;AAAA,MACR;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACF;;;AKjzBA,IAAI;AAEJ,IAAqB,gBAArB,MAAmC;AAAA,EACjC,YAAY,SAA2C;AACrD,QAAI,CAAC,eAAe;AAClB,cAAQ,IAAI,wBAAwB;AACpC,sBAAgB,IAAI,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,OACK,MACqC;AACxC,iBAAa,iCAAiC,IAAI;AAClD,WAAO,cAAc,IAAI,GAAG,IAAI;AAAA,EAClC;AAAA,EACA,iBACK,MAC+C;AAClD,iBAAa,2CAA2C,IAAI;AAC5D,WAAO,cAAc,cAAc,GAAG,IAAI;AAAA,EAC5C;AAAA,EACA,qBACK,MACmD;AAEtD,WAAO,cAAc,kBAAkB,GAAG,IAAI;AAAA,EAChD;AACF;;;ACrCA,IAAO,gBAAQ;","names":["error"]}
@@ -0,0 +1 @@
1
+ notify-keyspace-events Exe
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trieb.work/nextjs-turbo-redis-cache",
3
- "version": "1.8.0-beta.2",
3
+ "version": "1.8.0-beta.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/trieb-work/nextjs-turbo-redis-cache.git"
@@ -13,6 +13,36 @@ export type CacheEntry = {
13
13
  tags: string[];
14
14
  };
15
15
 
16
+ export function redisErrorHandler<T extends Promise<unknown>>(
17
+ debugInfo: string,
18
+ redisCommandResult: T,
19
+ ): T {
20
+ const beforeTimestamp = performance.now();
21
+ return redisCommandResult.catch((error) => {
22
+ console.error(
23
+ 'Redis command error',
24
+ (performance.now() - beforeTimestamp).toFixed(2),
25
+ 'ms',
26
+ debugInfo,
27
+ error,
28
+ );
29
+ throw error;
30
+ }) as T;
31
+ }
32
+
33
+ // This is a test to check if the event loop is lagging. Increase CPU
34
+ setInterval(() => {
35
+ const start = performance.now();
36
+ setImmediate(() => {
37
+ const duration = performance.now() - start;
38
+ if (duration > 100) {
39
+ console.warn(
40
+ `RedisStringsHandler detected an event loop lag of: ${duration.toFixed(2)}ms`,
41
+ );
42
+ }
43
+ });
44
+ }, 1000);
45
+
16
46
  export type CreateRedisStringsHandlerOptions = {
17
47
  /** Redis redisUrl to use.
18
48
  * @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
@@ -352,9 +382,19 @@ export default class RedisStringsHandler {
352
382
  const clientGet = this.redisGetDeduplication
353
383
  ? this.deduplicatedRedisGet(key)
354
384
  : this.redisGet;
355
- const serializedCacheEntry = await clientGet(
356
- getTimeoutRedisCommandOptions(this.timeoutMs),
357
- this.keyPrefix + key,
385
+ const serializedCacheEntry = await redisErrorHandler(
386
+ 'RedisStringsHandler.get(), operation: get' +
387
+ (this.redisGetDeduplication ? 'deduplicated' : '') +
388
+ this.timeoutMs +
389
+ 'ms' +
390
+ ' ' +
391
+ this.keyPrefix +
392
+ ' ' +
393
+ key,
394
+ clientGet(
395
+ getTimeoutRedisCommandOptions(this.timeoutMs),
396
+ this.keyPrefix + key,
397
+ ),
358
398
  );
359
399
 
360
400
  debug(
@@ -595,13 +635,17 @@ export default class RedisStringsHandler {
595
635
 
596
636
  // Setting the cache entry in redis
597
637
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
598
- const setOperation: Promise<string | null> = this.client.set(
599
- options,
600
- this.keyPrefix + key,
601
- serializedCacheEntry,
602
- {
638
+ const setOperation: Promise<string | null> = redisErrorHandler(
639
+ 'RedisStringsHandler.set(), operation: set' +
640
+ this.timeoutMs +
641
+ 'ms' +
642
+ ' ' +
643
+ this.keyPrefix +
644
+ ' ' +
645
+ key,
646
+ this.client.set(options, this.keyPrefix + key, serializedCacheEntry, {
603
647
  EX: expireAt,
604
- },
648
+ }),
605
649
  );
606
650
 
607
651
  debug(
@@ -725,7 +769,16 @@ export default class RedisStringsHandler {
725
769
  const redisKeys = Array.from(keysToDelete);
726
770
  const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);
727
771
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
728
- const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);
772
+ const deleteKeysOperation = redisErrorHandler(
773
+ 'RedisStringsHandler.revalidateTag(), operation: unlink' +
774
+ this.timeoutMs +
775
+ 'ms' +
776
+ ' ' +
777
+ this.keyPrefix +
778
+ ' ' +
779
+ fullRedisKeys,
780
+ this.client.unlink(options, fullRedisKeys),
781
+ );
729
782
 
730
783
  // also delete entries from in-memory deduplication cache if they get revalidated
731
784
  if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {
package/src/SyncedMap.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  // SyncedMap.ts
2
- import { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';
2
+ import {
3
+ Client,
4
+ getTimeoutRedisCommandOptions,
5
+ redisErrorHandler,
6
+ } from './RedisStringsHandler';
3
7
  import { debugVerbose, debug } from './utils/debug';
4
8
 
5
9
  type CustomizedSync = {
@@ -304,11 +308,22 @@ export class SyncedMap<V> {
304
308
  if (!this.customizedSync?.withoutRedisHashmap) {
305
309
  const options = getTimeoutRedisCommandOptions(this.timeoutMs);
306
310
  operations.push(
307
- this.client.hSet(
308
- options,
309
- this.keyPrefix + this.redisKey,
310
- key as unknown as string,
311
- JSON.stringify(value),
311
+ redisErrorHandler(
312
+ 'SyncedMap.set(), operation: hSet ' +
313
+ this.syncChannel +
314
+ ' ' +
315
+ this.timeoutMs +
316
+ 'ms' +
317
+ ' ' +
318
+ this.keyPrefix +
319
+ ' ' +
320
+ key,
321
+ this.client.hSet(
322
+ options,
323
+ this.keyPrefix + this.redisKey,
324
+ key as unknown as string,
325
+ JSON.stringify(value),
326
+ ),
312
327
  ),
313
328
  );
314
329
  }
@@ -319,7 +334,18 @@ export class SyncedMap<V> {
319
334
  value,
320
335
  };
321
336
  operations.push(
322
- this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),
337
+ redisErrorHandler(
338
+ 'SyncedMap.set(), operation: publish ' +
339
+ this.syncChannel +
340
+ ' ' +
341
+ this.timeoutMs +
342
+ 'ms' +
343
+ ' ' +
344
+ this.keyPrefix +
345
+ ' ' +
346
+ key,
347
+ this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),
348
+ ),
323
349
  );
324
350
  await Promise.all(operations);
325
351
  }
@@ -343,9 +369,22 @@ export class SyncedMap<V> {
343
369
  }
344
370
 
345
371
  if (!this.customizedSync?.withoutRedisHashmap) {
346
- const options = getTimeoutRedisCommandOptions(this.timeoutMs);
372
+ const options = getTimeoutRedisCommandOptions(this.timeoutMs * 10);
347
373
  operations.push(
348
- this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),
374
+ redisErrorHandler(
375
+ 'SyncedMap.delete(), operation: hDel ' +
376
+ this.syncChannel +
377
+ ' ' +
378
+ this.timeoutMs +
379
+ 'ms' +
380
+ ' ' +
381
+ this.keyPrefix +
382
+ ' ' +
383
+ this.redisKey +
384
+ ' ' +
385
+ keysArray,
386
+ this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),
387
+ ),
349
388
  );
350
389
  }
351
390
 
@@ -355,7 +394,21 @@ export class SyncedMap<V> {
355
394
  keys: keysArray,
356
395
  };
357
396
  operations.push(
358
- this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),
397
+ redisErrorHandler(
398
+ 'SyncedMap.delete(), operation: publish ' +
399
+ this.syncChannel +
400
+ ' ' +
401
+ this.timeoutMs +
402
+ 'ms' +
403
+ ' ' +
404
+ this.keyPrefix +
405
+ ' ' +
406
+ keysArray,
407
+ this.client.publish(
408
+ this.syncChannel,
409
+ JSON.stringify(deletionMessage),
410
+ ),
411
+ ),
359
412
  );
360
413
  }
361
414