All files / src stage.js

100% Statements 20/20
100% Branches 4/4
100% Functions 7/7
100% Lines 15/15

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                                              8x   8x 7x 7x   7x 7x     8x                                 8x   8x 7x 7x   7x 7x     8x     1x  
/**
 * @file Stage
 * @copyright Multifactor 2022 All Rights Reserved
 *
 * @description
 * Pre-compute MFKDF factors for benchmarking or performance
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
/**
 * Pre-compute an MFKDF factor setup process.
 * Useful for benchmarking or parallelization where supported.
 *
 * @param {Promise<MFKDFFactor>} factor - An async MFKDF factor setup function promise
 * @param {Buffer} [key] - MFKDF output key, needed to pre-compute factor params
 * @returns {MFKDFFactor} An MFKDF factor whose outputs have been pre-computed
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 1.4.0
 * @async
 * @memberOf stage
 */
async function setup (factor, key) {
  const result = await factor
 
  if (key) {
    const params = await result.params({ key })
    result.params = () => Promise.resolve(params)
 
    const output = await result.output()
    result.output = () => Promise.resolve(output)
  }
 
  return result
}
 
/**
 * Pre-compute an MFKDF factor derivation process.
 * Useful for benchmarking or parallelization where supported.
 *
 * @param {function(config:Object): Promise<MFKDFFactor>} factor - An async MFKDF factor derivation function
 * @param {Object} params - Factor parameters
 * @param {Buffer} [key] - MFKDF output key, needed to pre-compute factor params
 * @returns {function(config:Object): Promise<MFKDFFactor>} An async MFKDF factor derivation function whose outputs have been pre-computed
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 1.4.0
 * @async
 * @memberOf stage
 */
async function derive (factor, params, key) {
  const result = await factor(params)
 
  if (key) {
    const params = await result.params({ key })
    result.params = () => Promise.resolve(params)
 
    const output = await result.output()
    result.output = () => Promise.resolve(output)
  }
 
  return () => Promise.resolve(result)
}
 
module.exports.factor = { setup, derive }