{"version":3,"file":"configurations.mjs","names":[],"sources":["../../../../../../encryption/src/configurations.ts"],"sourcesContent":["import AES from \"crypto-js/aes\";\nimport { DEFAULT_ITERATIONS, MAX_ITERATIONS, MIN_ITERATIONS } from \"./envelope\";\nimport { EncryptionError } from \"./errors\";\nimport { EncryptionConfigurations } from \"./types\";\n\nconst defaultConfigurations: EncryptionConfigurations = {\n  key: null as any,\n  iterations: DEFAULT_ITERATIONS,\n  legacyDecryption: false,\n  legacyDriver: AES,\n};\n\nlet configurations: EncryptionConfigurations = { ...defaultConfigurations };\n\nlet deprecatedDriverWarned = false;\n\nexport function setEncryptionConfigurations(\n  newConfigurations: EncryptionConfigurations\n) {\n  if (newConfigurations.iterations !== undefined) {\n    assertIterations(newConfigurations.iterations);\n  }\n\n  // v1.x callers configured a pluggable cipher. It can no longer change how we\n  // encrypt — AES-256-GCM is not negotiable — so it is re-pointed at the\n  // legacy decrypt path and the caller is told once.\n  if (newConfigurations.driver !== undefined) {\n    if (!deprecatedDriverWarned) {\n      deprecatedDriverWarned = true;\n      console.warn(\n        \"[@mongez/encryption] `driver` is deprecated: encryption is always AES-256-GCM. The driver you passed will only be used to read legacy (v1.x) ciphertexts.\"\n      );\n    }\n\n    newConfigurations = {\n      legacyDriver: newConfigurations.driver,\n      ...newConfigurations,\n    };\n  }\n\n  configurations = { ...configurations, ...newConfigurations };\n}\n\nexport function getEncryptionConfig(key: keyof EncryptionConfigurations): any {\n  return configurations[key];\n}\n\n/**\n * Reset every configuration value back to its import-time default.\n */\nexport function resetEncryptionConfigurations() {\n  configurations = { ...defaultConfigurations };\n}\n\n/**\n * Reject work factors that would make PBKDF2 decorative.\n */\nexport function assertIterations(iterations: number) {\n  if (\n    !Number.isInteger(iterations) ||\n    iterations < MIN_ITERATIONS ||\n    iterations > MAX_ITERATIONS\n  ) {\n    throw new EncryptionError(\n      `Invalid PBKDF2 iterations: ${iterations}. Expected an integer between ${MIN_ITERATIONS} and ${MAX_ITERATIONS}.`\n    );\n  }\n}\n"],"mappings":";;;;;AAKA,MAAM,wBAAkD;CACtD,KAAK;CACL,YAAY;CACZ,kBAAkB;CAClB,cAAc;AAChB;AAEA,IAAI,iBAA2C,EAAE,GAAG,sBAAsB;AAE1E,IAAI,yBAAyB;AAE7B,SAAgB,4BACd,mBACA;CACA,IAAI,kBAAkB,eAAe,QACnC,iBAAiB,kBAAkB,UAAU;CAM/C,IAAI,kBAAkB,WAAW,QAAW;EAC1C,IAAI,CAAC,wBAAwB;GAC3B,yBAAyB;GACzB,QAAQ,KACN,2JACF;EACF;EAEA,oBAAoB;GAClB,cAAc,kBAAkB;GAChC,GAAG;EACL;CACF;CAEA,iBAAiB;EAAE,GAAG;EAAgB,GAAG;CAAkB;AAC7D;AAEA,SAAgB,oBAAoB,KAA0C;CAC5E,OAAO,eAAe;AACxB;;;;AAKA,SAAgB,gCAAgC;CAC9C,iBAAiB,EAAE,GAAG,sBAAsB;AAC9C;;;;AAKA,SAAgB,iBAAiB,YAAoB;CACnD,IACE,CAAC,OAAO,UAAU,UAAU,KAC5B,oBACA,kBAEA,MAAM,IAAI,gBACR,8BAA8B,WAAW,gCAAgC,eAAe,OAAO,eAAe,EAChH;AAEJ"}