-
Notifications
You must be signed in to change notification settings - Fork 45
/
utils.js
124 lines (112 loc) · 3.68 KB
/
utils.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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()
if (!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'
let locale = headless ? 'us-east1.headless.' : REGION_MAPPING[dcRegion]
let subdomain = ''
/**
* check if endpoint base has subdomain
* e.g. api.saucelabs.com
*/
if (!headless && hostname.split('.').length > 2) {
subdomain = hostname.split('.')[0] + '.'
hostname = hostname.split('.').slice(-2).join('.')
} else if (!headless && region === 'us') {
locale = ''
}
return protocol + subdomain + locale + 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
})
}
/**
* type check for endpoint parameters
* @param {*} option given command parameters
* @param {String} expectedType expected parameter type
* @return {Boolean} true if typecheck was ok
*/
export function isValidType (option, expectedType) {
if (expectedType === 'array') {
return Array.isArray(option)
}
return option && typeof option === expectedType
}
/**
* get error message from API call
* @param {*} body body message of response
* @return {String} error message
*/
export function getErrorReason (body = {}) {
if (typeof body === 'string') {
try {
body = JSON.parse(body)
} catch (e) {
body = { message: body }
}
}
/**
* due to a bug in the performance API we need to workaround this
*/
if (Array.isArray(body)) {
body = { message: body[0] }
}
return body.message || body.detail || 'unknown'
}