{"version":3,"sources":["../../../../src/server/use-cache/cache-life-profile.ts"],"sourcesContent":["import { INFINITE_CACHE } from '../../lib/constants'\n\nexport type CacheLife = {\n  // How long the client can cache a value without checking with the server.\n  stale?: number\n  // How frequently you want the cache to refresh on the server.\n  // Stale values may be served while revalidating.\n  revalidate?: number\n  // In the worst case scenario, where you haven't had traffic in a while,\n  // how stale can a value be until you prefer deopting to dynamic.\n  // Must be longer than revalidate.\n  expire?: number\n}\n\ntype CacheLifeProfileContext =\n  | { kind: 'inline' }\n  | { kind: 'config'; profileName: string }\n\nfunction normalizeCacheLifeValue(\n  key: keyof CacheLife,\n  value: unknown,\n  context: CacheLifeProfileContext\n): number | undefined {\n  if (value === undefined) {\n    return undefined\n  }\n\n  if (value === false) {\n    if (key === 'stale') {\n      throw new Error(\n        'Pass `Infinity` instead of `false` if you want to cache on the client forever ' +\n          'without checking with the server.'\n      )\n    } else if (key === 'revalidate') {\n      throw new Error(\n        'Pass `Infinity` instead of `false` if you do not want to revalidate by time.'\n      )\n    } else {\n      throw new Error(\n        'Pass `Infinity` instead of `false` if you want to cache on the server forever ' +\n          'without checking with the origin.'\n      )\n    }\n  }\n\n  if (typeof value !== 'number') {\n    throw new Error(`The ${key} option must be a number of seconds.`)\n  }\n\n  if (value === Infinity) {\n    // Infinity means \"never\", but turns into null when serialized as JSON\n    // (e.g. for build workers or cache handlers), unlike INFINITE_CACHE.\n    return INFINITE_CACHE\n  }\n\n  if (!Number.isFinite(value)) {\n    throw new Error(\n      context.kind === 'config'\n        ? `Invalid \"cacheLife.${context.profileName}.${key}\" provided, expected a finite number of seconds or Infinity, received ${value}`\n        : `Invalid \\`cacheLife()\\` option \"${key}\" provided, expected a finite number of seconds or Infinity, received ${value}.`\n    )\n  }\n\n  return value\n}\n\nexport function validateAndNormalizeCacheLifeProfile(\n  profile: CacheLife,\n  context: CacheLifeProfileContext\n): CacheLife {\n  // Don't mutate the profile; it may be shared, e.g. as part of the user's\n  // config object.\n  const normalizedProfile = { ...profile }\n\n  for (const key of ['stale', 'revalidate', 'expire'] as const) {\n    const value = normalizeCacheLifeValue(key, profile[key], context)\n    if (value !== undefined) {\n      normalizedProfile[key] = value\n    }\n  }\n\n  if (\n    normalizedProfile.revalidate !== undefined &&\n    normalizedProfile.expire !== undefined &&\n    normalizedProfile.revalidate > normalizedProfile.expire\n  ) {\n    throw new Error(\n      'If providing both the revalidate and expire options, ' +\n        'the expire option must be greater than the revalidate option. ' +\n        'The expire option indicates how many seconds from the start ' +\n        'until it can no longer be used.'\n    )\n  }\n\n  return normalizedProfile\n}\n"],"names":["INFINITE_CACHE","normalizeCacheLifeValue","key","value","context","undefined","Error","Infinity","Number","isFinite","kind","profileName","validateAndNormalizeCacheLifeProfile","profile","normalizedProfile","revalidate","expire"],"mappings":"AAAA,SAASA,cAAc,QAAQ,sBAAqB;AAkBpD,SAASC,wBACPC,GAAoB,EACpBC,KAAc,EACdC,OAAgC;IAEhC,IAAID,UAAUE,WAAW;QACvB,OAAOA;IACT;IAEA,IAAIF,UAAU,OAAO;QACnB,IAAID,QAAQ,SAAS;YACnB,MAAM,qBAGL,CAHK,IAAII,MACR,mFACE,sCAFE,qBAAA;uBAAA;4BAAA;8BAAA;YAGN;QACF,OAAO,IAAIJ,QAAQ,cAAc;YAC/B,MAAM,qBAEL,CAFK,IAAII,MACR,iFADI,qBAAA;uBAAA;4BAAA;8BAAA;YAEN;QACF,OAAO;YACL,MAAM,qBAGL,CAHK,IAAIA,MACR,mFACE,sCAFE,qBAAA;uBAAA;4BAAA;8BAAA;YAGN;QACF;IACF;IAEA,IAAI,OAAOH,UAAU,UAAU;QAC7B,MAAM,qBAA2D,CAA3D,IAAIG,MAAM,CAAC,IAAI,EAAEJ,IAAI,oCAAoC,CAAC,GAA1D,qBAAA;mBAAA;wBAAA;0BAAA;QAA0D;IAClE;IAEA,IAAIC,UAAUI,UAAU;QACtB,sEAAsE;QACtE,qEAAqE;QACrE,OAAOP;IACT;IAEA,IAAI,CAACQ,OAAOC,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