export function decryptMaterials()

in modules/cache-material/src/caching_cryptographic_materials_decorators.ts [152:187]


export function decryptMaterials<S extends SupportedAlgorithmSuites>({
  buildDecryptionMaterialCacheKey,
}: CryptographicMaterialsCacheKeyHelpersInterface<S>): GetDecryptMaterials<S> {
  return async function decryptMaterials(
    this: CachingMaterialsManager<S>,
    request: DecryptionRequest<S>
  ): Promise<DecryptionMaterial<S>> {
    const { suite } = request
    /* Check for early return (Postcondition): If I can not cache the DecryptionMaterial, do not even look. */
    if (!suite.cacheSafe) {
      const material = await this._backingMaterialsManager.decryptMaterials(
        request
      )
      return material
    }

    const cacheKey = await buildDecryptionMaterialCacheKey(
      this._partition,
      request
    )
    const entry = this._cache.getDecryptionMaterial(cacheKey)
    /* Check for early return (Postcondition): If I have a valid DecryptionMaterial, return it. */
    if (entry && !this._cacheEntryHasExceededLimits(entry)) {
      return cloneResponse(entry.response)
    } else {
      this._cache.del(cacheKey)
    }

    const material = await this._backingMaterialsManager.decryptMaterials(
      request
    )

    this._cache.putDecryptionMaterial(cacheKey, material, this._maxAge)
    return cloneResponse(material)
  }
}