All files utils.js

95.24% Statements 20/21
94.44% Branches 17/18
100% Functions 8/8
95% Lines 19/20

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 66 67 68 69 70 71 72 73 74 75                          5x 5x 5x 5x 5x 5x     5x                         10x 10x 10x                 4x                           73x 144x       73x 243x 20x   223x 22x   201x      
import crypto from 'crypto'
 
import { REGION_MAPPING, TO_STRING_TAG, PARAMETERS_MAP } from './constants'
 
/**
 * Create HMAC token to receive job assets
 *
 * @param  {string} username  username of user
 * @param  {string} key       access key of user
 * @param  {string} jobId     job id of job you want to get access to
 * @return {string}           hmac token
 */
export function createHMAC (username, key, jobId) {
    const hmac = crypto.createHmac('md5', `${username}:${key}`)
    hmac.write(jobId)
    hmac.end()
    return new Promise((resolve, reject) => hmac.on('readable', () => {
        const data = hmac.read()
        Iif (!data) {
            return reject(new Error('Could not create HMAC token'))
        }
        return resolve(data.toString('hex'))
    }))
}
 
/**
 * get sauce API url
 * @param  {string}  hostname  host name of the API that is being used
 * @param  {string}  region    region of the datacenter
 * @param  {boolean} headless  true if job is running on headless instance
 * @param  {string}  protocol  protcol to be used
 * @return {string}            endpoint base url (e.g. `https://us-east1.headless.saucelabs.com`)
 */
export function getSauceEndpoint (hostname, region, headless, protocol = 'https://') {
    const dcRegion = REGION_MAPPING[region] ? region : 'us'
    const prefix = headless ? 'us-east1.headless.' : REGION_MAPPING[dcRegion]
    return protocol + prefix + hostname
}
 
/**
 * toString method for proxy instance
 * @param  {object} scope  actual API instance
 * @return {string}        to string output
 */
export function toString (scope) {
    return `${TO_STRING_TAG} {
  user: '${scope.username}',
  key: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXX${scope._accessKey.slice(-6)}',
  region: '${scope._options.region}',
  headless: ${scope._options.headless}
}`
}
 
/**
 * get sorted list of parameters with full description
 * @param  {Array}    [parameters=[]]  parameter defined in endpoint
 * @return {[Object]}                  full description of parameters
 */
export function getParameters (parameters = []) {
    const params = parameters.map(
        (urlParameter) => urlParameter.$ref
            ? PARAMETERS_MAP.get(urlParameter.$ref.split('/').slice(-1)[0])
            : urlParameter)
 
    return params.sort((a, b) => {
        if (a.required && b.required) {
            return 0
        }
        if (a.required && !b.required) {
            return -1
        }
        return 1
    })
}