All files / src/encryption cryptoUtils.ts

80% Statements 8/10
50% Branches 2/4
100% Functions 2/2
80% Lines 8/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  1x 322x     1x                                   1x 322x           322x     322x 322x                  
 
export function isSubtleCryptoAvailable(): boolean {
  return typeof crypto !== 'undefined' && typeof crypto.subtle !== 'undefined'
}
 
export const NO_CRYPTO_LIB = 'Crypto lib not found. Either the WebCrypto "crypto.subtle" or Node.js "crypto" module must be available.'
 
export type TriplesecDecryptSignature =  (
  arg: { data: Buffer; key: Buffer }, cb: (err: Error | null, buff: Buffer | null) => void
) => void
 
export interface WebCryptoLib {
  lib: SubtleCrypto;
  name: 'subtleCrypto'
}
 
export interface NodeCryptoLib {
  lib: typeof import('crypto');
  name: 'nodeCrypto'
}
 
// Make async for future version which may lazy load.
// eslint-disable-next-line @typescript-eslint/require-await
export async function getCryptoLib(): Promise<WebCryptoLib | NodeCryptoLib> {
  Iif (isSubtleCryptoAvailable()) {
    return {
      lib: crypto.subtle,
      name: 'subtleCrypto'
    }
  } else {
    try {
      // eslint-disable-next-line max-len
      // eslint-disable-next-line import/no-nodejs-modules,no-restricted-modules,global-require,@typescript-eslint/no-var-requires
      const nodeCrypto = require('crypto') as typeof import('crypto')
      return {
        lib: nodeCrypto,
        name: 'nodeCrypto'
      }
    } catch (error) {
      throw new Error(NO_CRYPTO_LIB)
    }
  }
}