{"version":3,"sources":["jsdelivr-header.js","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/index.ts","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/RedisStringsHandler.ts","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/utils/debug.ts","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/SyncedMap.ts","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/DeduplicatedRequestHandler.ts","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/utils/json.ts","/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/src/CachedHandler.ts"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA,8iBCNA,cAAA,GAAA,SAAA,cAAA,CAAA,oBAAA,IAAA,oBAAA,QAAA,IAAA,gBAAA,OAAA,QAAA,aAAA,eCAA,IAAA,aAA6C,QAAA,SCAtC,SAAS,MACd,EAOa,UACV,GAEH,MAAM,EAAY,CAChB,IAAK,QACL,KAAM,QACN,MAAO,QACP,OAAQ,QACR,KAAM,QACN,MAAO,QACP,KAAM,IAEJ,QAAQ,IAAI,qBACd,QAAQ,IAAI,EAAU,GAAQ,2BAA4B,EAE9D,CAEO,SAAS,aAAa,KAAkB,GACzC,QAAQ,IAAI,qCACd,QAAQ,IAAI,QAAY,wBAAyB,EAErD,CCDA,IAAM,oBAAsB,iBACf,UAAN,MAiBL,WAAA,CAAY,GACV,KAAK,OAAS,EAAQ,OACtB,KAAK,UAAY,EAAQ,UACzB,KAAK,SAAW,EAAQ,SACxB,KAAK,YAAc,GAAG,EAAQ,YAAY,sBAAsB,EAAQ,WACxE,KAAK,SAAW,EAAQ,SACxB,KAAK,UAAY,EAAQ,UACzB,KAAK,UAAY,EAAQ,UACzB,KAAK,WAAa,EAAQ,WAC1B,KAAK,iBAAmB,EAAQ,iBAChC,KAAK,eAAiB,EAAQ,eAE9B,KAAK,IAAM,IA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KAAK,cAAc,OAAO,SAGhD,QAAQ,IAAI,CAAC,EAAqB,IACxC,MACE,MACA,iEAEJ,GKpkBmB,cAArB,MACE,WAAA,CAAY,GACL,gBACH,QAAQ,IAAI,0BACZ,cAAgB,IAAI,oBAAoB,GAE5C,CACA,GAAA,IACK,GAGH,OADA,aAAa,gCAAiC,GACvC,cAAc,OAAO,EAC9B,CACA,GAAA,IACK,GAGH,OADA,aAAa,gCAAiC,GACvC,cAAc,OAAO,EAC9B,CACA,aAAA,IACK,GAGH,OADA,aAAa,0CAA2C,GACjD,cAAc,iBAAiB,EACxC,CACA,iBAAA,IACK,GAGH,OAAO,cAAc,qBAAqB,EAC5C,GNpCK,cAAQ","file":"/npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@iosifnicolae2/nextjs-turbo-redis-cache@1.4.1/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import CachedHandler from './CachedHandler';\nexport default CachedHandler;\nimport RedisStringsHandler from './RedisStringsHandler';\nexport { RedisStringsHandler };\n","import { commandOptions, createClient } 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 redis_url 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  redis_url?: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};\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\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\n  constructor({\n    redis_url = process.env.REDIS_URL? process.env.REDIS_URL : (\n        process.env.REDISHOST\n            ? `redis://${process.env.REDISHOST}:${process.env.REDISPORT}`\n            : 'redis://localhost:6379'\n    ),\n    database = process.env.VERCEL_ENV === 'production' ? 0 : 1,\n    keyPrefix = process.env.VERCEL_URL || 'UNDEFINED_URL_',\n    sharedTagsKey = '__sharedTags__',\n    timeoutMs = 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  }: CreateRedisStringsHandlerOptions) {\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\n    try {\n      this.client = createClient({\n        ...(database !== 0 ? { database } : {}),\n        url: redis_url,\n      });\n\n      this.client.on('error', (error) => {\n        console.error('Redis client error', error);\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  }\n\n  resetRequestCache(): void {}\n\n  private async assertClientIsReady(): Promise<void> {\n    await Promise.all([\n      this.sharedTagsMap.waitUntilReady(),\n      this.revalidatedTagsMap.waitUntilReady(),\n    ]);\n    if (!this.client.isReady) {\n      throw new Error('Redis client is not ready yet or connection is lost.');\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    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  }\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    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 = ctx.revalidate || ctx.cacheControl?.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  }\n\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  public async revalidateTag(tagOrTags: string | string[], ...rest: any[]) {\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  }\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        await this.subscriberClient.connect();\n      });\n\n      // Check if keyspace event configuration is set correctly\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 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') && keyspaceEventConfig.includes('e')\n        )\n      ) {\n        throw new Error(\n          \"Keyspace event configuration 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      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  // /api/revalidated-fetch\n  // true\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"]}