{"version":3,"file":"index.cjs","sources":["../utils/src//constants.ts","../utils/src//enums.ts","../utils/src//errors/ArgsError.ts","../utils/src//errors/GroupedError.ts","../utils/src//errors/PositionError.ts","../utils/src//helpers/base64.ts","../utils/src//helpers/encryption.ts","../utils/src//helpers/sizeOf.ts","../utils/src//helpers/hydrateMetadata.ts","../utils/src//helpers/isJsonValue.ts","../utils/src//helpers/prepareStoreEntries.ts"],"sourcesContent":["export const CACHEMAP = 'cachemap';\nexport const CLEAR = 'clear';\nexport const DELETE = 'delete';\nexport const ENTRIES = 'entries';\nexport const EXPORT = 'export';\nexport const GET = 'get';\nexport const HAS = 'has';\nexport const IMPORT = 'import';\nexport const MESSAGE = 'message';\nexport const METADATA = 'metadata';\nexport const SET = 'set';\nexport const SIZE = 'size';\nexport const START_REAPER = 'startReaper';\nexport const STOP_REAPER = 'stopReaper';\nexport const START_BACKUP = 'startBackup';\nexport const STOP_BACKUP = 'stopBackup';\n","export enum ValueFormat {\n  Base64 = 'base64',\n  Ecrypt = 'encrypt',\n  String = 'string',\n}\n","export class ArgsError extends Error {}\n","export class GroupedError extends Error {\n  public name = 'GroupedError';\n  public type = 'Error';\n  constructor(\n    public message: string,\n    public errors: Error[],\n  ) {\n    super(message);\n  }\n}\n","export class PositionError extends Error {}\n","import { Base64 } from 'js-base64';\nimport { type JsonValue } from 'type-fest';\n\nexport const encode = (data: JsonValue) => Base64.encode(JSON.stringify(data));\n\n// JSON.parse returns any type.\n// eslint-disable-next-line @typescript-eslint/consistent-type-assertions\nexport const decode = (encodedData: string) => JSON.parse(Base64.decode(encodedData)) as JsonValue;\n","import CryptoJS from 'crypto-js';\nimport { type JsonValue } from 'type-fest';\n\nexport const encrypt = (data: JsonValue, secret: string) =>\n  CryptoJS.AES.encrypt(JSON.stringify(data), secret).toString();\n\nexport const decrypt = (encryptedData: string, secret: string) =>\n  // JSON.parse returns any type.\n  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n  JSON.parse(CryptoJS.AES.decrypt(encryptedData, secret).toString(CryptoJS.enc.Utf8)) as JsonValue;\n","import { isObjectLike, isRegExp, isTypedArray } from 'lodash-es';\nimport { type TypedArray } from 'type-fest';\n\nconst ECMA_SIZES = {\n  BOOLEAN: 4,\n  BYTES: 4,\n  NUMBER: 8,\n  STRING: 2,\n} as const;\n\nconst convertToObject = (obj: Map<unknown, unknown> | Set<unknown>): object => {\n  if (obj instanceof Map) {\n    // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n    return Object.fromEntries(obj);\n  }\n\n  return [...obj];\n};\n\nconst visitor = (obj: object, callback: (value: unknown) => object | false | undefined) => {\n  for (const key of Object.keys(obj)) {\n    // @ts-expect-error 'string' can't be used to index type '{}'\n    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n    const newValue = callback(obj[key]) ?? obj[key];\n\n    if (newValue === false) {\n      return;\n    }\n\n    if (isObjectLike(newValue) && !isRegExp(newValue)) {\n      // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n      visitor(newValue, callback);\n    }\n  }\n};\n\nconst utf16leToBytes = (value: string) => {\n  let c: number, hi: number, lo: number;\n  const byteArray = [];\n\n  for (let index = 0; index < value.length; ++index) {\n    // Based on surrounding code, this cannot be undefined.\n    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n    c = value.codePointAt(index)!;\n    hi = c >> 8;\n    lo = c % 256;\n    byteArray.push(lo, hi);\n  }\n\n  return byteArray;\n};\n\nconst sizeComplexObject = (obj: object) => {\n  let totalSize = 0;\n\n  try {\n    if (isTypedArray(obj)) {\n      // isTypedArray does not support type guard.\n      // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n      const typedArray = obj as TypedArray;\n      return typedArray.length * typedArray.BYTES_PER_ELEMENT;\n    }\n\n    let convertedObj = obj;\n\n    if (obj instanceof Map || obj instanceof Set) {\n      convertedObj = convertToObject(obj);\n    }\n\n    if (!Array.isArray(convertedObj)) {\n      totalSize += ECMA_SIZES.BYTES;\n    }\n\n    visitor(convertedObj, (value: unknown) => {\n      if (isTypedArray(value)) {\n        // isTypedArray does not support type guard.\n        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n        const typedArray = value as TypedArray;\n        totalSize += typedArray.length * typedArray.BYTES_PER_ELEMENT;\n        return false;\n      }\n\n      if (!isObjectLike(value) || isRegExp(value)) {\n        totalSize += sizeSimpleType(value);\n        return;\n      }\n\n      if (!Array.isArray(value)) {\n        totalSize += ECMA_SIZES.BYTES;\n\n        if (value instanceof Map || value instanceof Set) {\n          return convertToObject(value);\n        }\n      }\n\n      return;\n    });\n  } catch {\n    // no catch\n  }\n\n  return totalSize;\n};\n\nconst sizeSimpleType = (value: unknown) => {\n  const objectList: object[] = [];\n  const stack = [value];\n  let bytes = 0;\n\n  while (stack.length > 0) {\n    const stackEntry = stack.pop();\n\n    switch (true) {\n      case typeof stackEntry === 'boolean': {\n        bytes += ECMA_SIZES.BOOLEAN;\n        break;\n      }\n\n      case typeof stackEntry === 'string': {\n        bytes += utf16leToBytes(stackEntry).length;\n        break;\n      }\n\n      case typeof stackEntry === 'number': {\n        bytes += ECMA_SIZES.NUMBER;\n        break;\n      }\n\n      case typeof stackEntry === 'symbol': {\n        // Based on surrounding code, value will be a symbol.\n        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n        const castValue = value as symbol;\n        // Based on surrounding code, value will not be undefined.\n        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n        const isGlobalSymbol = Symbol.keyFor(castValue)!;\n\n        bytes += isGlobalSymbol\n          ? // Based on surrounding code, value will not be undefined.\n            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n            Symbol.keyFor(castValue)!.length * ECMA_SIZES.STRING\n          : (castValue.toString().length - 8) * ECMA_SIZES.STRING;\n\n        break;\n      }\n\n      case typeof stackEntry === 'bigint': {\n        bytes += stackEntry.toString().length;\n        break;\n      }\n\n      case typeof stackEntry === 'function': {\n        // typescript is not deriving type of stack entry from case.\n        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n        bytes += (stackEntry as () => unknown).toString().length;\n        break;\n      }\n\n      case stackEntry === null: {\n        bytes += ECMA_SIZES.BYTES;\n        break;\n      }\n\n      case value instanceof RegExp: {\n        return value.toString().length;\n      }\n\n      case stackEntry !== null && typeof stackEntry === 'object' && !objectList.includes(stackEntry): {\n        const castStackEntry = stackEntry;\n        objectList.push(castStackEntry);\n\n        for (const index in castStackEntry) {\n          // @ts-expect-error 'string' can't be used to index type '{}'\n          stack.push(castStackEntry[index]);\n        }\n\n        break;\n      }\n    }\n  }\n\n  return bytes;\n};\n\nexport const sizeOf = (value: unknown) =>\n  // isObjectLike does not support type guard.\n  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n  isObjectLike(value) && !isRegExp(value) ? sizeComplexObject(value as object) : sizeSimpleType(value);\n","import { type DehydratedMetadata, type Metadata } from '@cachemap/types';\nimport { Cacheability } from 'cacheability';\n\nexport const dehydrateMetadata = (metadata: Metadata[]): DehydratedMetadata[] =>\n  metadata.map(({ cacheability, ...otherProps }) => {\n    return { ...otherProps, cacheability: { metadata: cacheability.metadata } };\n  });\n\nexport const rehydrateMetadata = (metadata: DehydratedMetadata[]): Metadata[] =>\n  metadata.map(({ cacheability, ...otherProps }) => {\n    return { ...otherProps, cacheability: new Cacheability({ metadata: cacheability.metadata }) };\n  });\n","import { type JsonValue } from 'type-fest';\n\nexport const isJsonValue = (value: unknown): value is JsonValue => {\n  try {\n    JSON.stringify(value);\n    return true;\n  } catch {\n    return false;\n  }\n};\n","import { type JsonValue } from 'type-fest';\nimport { ValueFormat } from '../enums.ts';\nimport { decode, encode } from './base64.ts';\nimport { decrypt, encrypt } from './encryption.ts';\n\nexport const prepareGetEntry = <T>(value: string, valueFormatting: ValueFormat, encryptionSecret?: string) => {\n  let getEntry: JsonValue | undefined;\n\n  switch (true) {\n    case valueFormatting === ValueFormat.String: {\n      // JSON.parse returns any type.\n      // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n      getEntry = JSON.parse(value) as JsonValue;\n      break;\n    }\n\n    case valueFormatting === ValueFormat.Base64: {\n      getEntry = decode(value);\n      break;\n    }\n\n    case valueFormatting === ValueFormat.Ecrypt && !!encryptionSecret: {\n      getEntry = decrypt(value, encryptionSecret);\n      break;\n    }\n\n    default: {\n      console.warn(\n        '> cachemap :: valueFormatting set to \"encrypt\", but no encryption secret provided, falling back to JSON.parse.',\n      );\n\n      // JSON.parse returns any type.\n      // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n      getEntry = JSON.parse(value) as JsonValue;\n    }\n  }\n\n  // Most straight forward way to allow return value to be typed.\n  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions\n  return getEntry as T;\n};\n\nexport const prepareSetEntry = (value: JsonValue, valueFormatting: ValueFormat, encryptionSecret?: string) => {\n  let setEntry: string;\n\n  switch (true) {\n    case valueFormatting === ValueFormat.String: {\n      setEntry = JSON.stringify(value);\n      break;\n    }\n\n    case valueFormatting === ValueFormat.Base64: {\n      setEntry = encode(value);\n      break;\n    }\n\n    case valueFormatting === ValueFormat.Ecrypt && !!encryptionSecret: {\n      setEntry = encrypt(value, encryptionSecret);\n      break;\n    }\n\n    default: {\n      console.warn(\n        '> cachemap :: valueFormatting set to \"encrypt\", but no encryption secret provided, falling back to stringify.',\n      );\n\n      setEntry = JSON.stringify(value);\n    }\n  }\n\n  return setEntry;\n};\n"],"names":["ValueFormat","ArgsError","Error","GroupedError","constructor","message","errors","super","name","type","PositionError","encode","data","Base64","JSON","stringify","decode","encodedData","parse","encrypt","secret","CryptoJS","AES","toString","decrypt","encryptedData","enc","Utf8","ECMA_SIZES","convertToObject","obj","Map","Object","fromEntries","visitor","callback","key","keys","newValue","isObjectLike","isRegExp","utf16leToBytes","value","c","hi","lo","byteArray","index","length","codePointAt","push","sizeSimpleType","objectList","stack","bytes","stackEntry","pop","castValue","Symbol","keyFor","RegExp","includes","castStackEntry","metadata","map","cacheability","otherProps","valueFormatting","encryptionSecret","getEntry","String","Ecrypt","console","warn","setEntry","Cacheability","totalSize","isTypedArray","typedArray","BYTES_PER_ELEMENT","convertedObj","Set","Array","isArray","sizeComplexObject"],"mappings":"qkBAAwB,iBACH,eACC,iBACC,iBACD,aACH,UACA,aACG,iBACC,mBACC,eACL,WACC,oBAGQ,2BAFA,0BAGD,yBAFA,eCbpBA,EAAKA,SAAAA,GAAAA,8DAAAA,CAIX,CAJWA,CAIX,CAAA,GCJM,MAAMC,UAAkBC,OCAxB,MAAMC,UAAqBD,MAGhCE,WAAAA,CACEC,EACOC,GAEPC,MAAMF,QAHCA,QAAAA,EAAAA,KACAC,OAAAA,EAJFE,KAAAA,KAAO,oBACPC,KAAO,OAMd,ECRK,MAAMC,UAAsBR,OCG5B,MAAMS,EAAUC,GAAoBC,EAAAA,OAAOF,OAAOG,KAAKC,UAAUH,IAI3DI,EAAUC,GAAwBH,KAAKI,MAAML,EAAAA,OAAOG,OAAOC,ICJ3DE,EAAU,CAACP,EAAiBQ,IACvCC,EAASC,IAAIH,QAAQL,KAAKC,UAAUH,GAAOQ,GAAQG,WAExCC,EAAU,CAACC,EAAuBL,IAG7CN,KAAKI,MAAMG,EAASC,IAAIE,QAAQC,EAAeL,GAAQG,SAASF,EAASK,IAAIC,OCNzEC,EACK,EADLA,EAEG,EAFHA,EAGI,EAHJA,EAII,EAGJC,EAAmBC,GACnBA,aAAeC,IAEVC,OAAOC,YAAYH,GAGrB,IAAIA,GAGPI,EAAU,CAACJ,EAAaK,KAC5B,IAAK,MAAMC,KAAOJ,OAAOK,KAAKP,GAAM,CAGlC,MAAMQ,EAAWH,EAASL,EAAIM,KAASN,EAAIM,GAE3C,IAAiB,IAAbE,EACF,OAGEC,EAAaD,aAAAA,KAAcE,EAAAA,SAASF,IAEtCJ,EAAQI,EAAUH,EAEtB,GAGIM,EAAkBC,IACtB,IAAIC,EAAWC,EAAYC,EAC3B,MAAMC,EAAY,GAElB,IAAK,IAAIC,EAAQ,EAAGA,EAAQL,EAAMM,SAAUD,EAG1CJ,EAAID,EAAMO,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