-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
310 lines (241 loc) · 7.78 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'use strict'
const hasOwnProperty = Object.prototype.hasOwnProperty
const mergeDeep = require('merge-deep')
const deepEqual = require('deep-equal')
const deep = require('deep-dot')
const names = require('browser-names')
const isForkPr = require('is-fork-pr').isForkPr
const prerelease = /[^\d.]/
const numeric = /^\d+$/
module.exports = matchAll
function matchAll (available, wanted) {
const matches = []
const eqlOptions = { strict: true }
for (const original of wanted) {
const w = normalize(original)
const explicit = new Set(['version'])
// Match by properties other than version
let group = available.filter(m => match(m, w, explicit))
// Match by version
group.sort((a, b) => cmpVersion(a.version, b.version))
group = filterVersions(group, w.version != null ? w.version : 'latest')
if (group.length === 0) {
throw new NotFoundError(original)
}
// Deduplicate by properties we didn't explicitly match
for (let i = 0; i < group.length; i++) {
const a = group[i]
let winner = a
for (let j = i + 1; j < group.length; j++) {
const b = group[j]
if (same(a, b, explicit, eqlOptions)) {
// Last manifest wins (for no particular reason)
winner = b
group.splice(j--, 1)
}
}
// Don't merge options into the manifest yet, so that we can
// perform fast deduplication by object identity (below). We
// assume that `available` itself doesn't contain duplicates.
matches.push({ manifest: winner, options: w.options || {} })
}
}
consolidate(matches, eqlOptions)
return matches
}
function consolidate (matches, eqlOptions) {
const insecure = insecureEnv()
for (let i = 0; i < matches.length; i++) {
const { manifest, options } = matches[i]
// Skip browsers that need secure environment variables and are therefore
// not available on pull requests from forks. Done here (after matching)
// so that the order of precedence between browsers is consistent in
// secure and insecure envs. Could reconsider that; needs a discussion.
if (insecure && manifest.wants && manifest.wants.secureEnv) {
matches.splice(i--, 1)
continue
}
// Add user-provided options to manifest
matches[i] = mergeDeep(manifest, { options })
// Remove exact duplicates (same manifest, same options)
for (let j = i + 1; j < matches.length; j++) {
if (matches[j].manifest === manifest &&
deepEqual(matches[j].options, options, eqlOptions)) {
matches.splice(j--, 1)
}
}
}
}
function normalize (wanted) {
// For airtap < 4 compatibility
// TODO: consider adding a shorthand "device" property for ipad & iphone
if (wanted.name === 'iphone' || wanted.name === 'ipad') {
wanted = { ...wanted }
const device = wanted.name === 'iphone' ? 'iphone simulator' : 'ipad simulator'
const caps = wanted.capabilities = { ...wanted.capabilities }
const appium = caps.appium = { ...caps.appium }
wanted.name = 'ios_saf'
appium.deviceName = appium.deviceName || device
}
return wanted
}
function match (available, wanted, explicit, key) {
if (isObject(wanted)) {
if (!isObject(available)) return false
for (const k in wanted) {
const fqk = key ? key + '.' + k : k
if (!hasOwnProperty.call(wanted, k)) continue
if (fqk === 'options' || fqk === 'version') continue
explicit.add(fqk)
if (!match(available[k], wanted[k], explicit, fqk)) return false
}
return true
} else {
return matchPrimitive(available, wanted, key)
}
}
function matchPrimitive (available, wanted, key) {
if (typeof wanted === 'string') {
wanted = wanted != null ? String(wanted).toLowerCase() : ''
available = available != null ? String(available).toLowerCase() : ''
}
if (available === wanted) {
return true
}
if (key === 'name') {
for (const alias of names(wanted)) {
if (available === alias) return true
}
}
return false
}
function same (a, b, explicit, eqlOptions) {
for (const k of explicit) {
if (!deepEqual(deep(a, k), deep(b, k), eqlOptions)) {
return false
}
}
return true
}
// Assumes manifests are sorted by version.
function filterVersions (manifests, version) {
if (manifests.length === 0) {
return manifests
}
const test = range(version, manifests)
const result = manifests.filter(m => test(m.version))
return result
}
// Assumes manifests are sorted by version.
function range (version, manifests) {
if (Array.isArray(version)) {
const tests = version.map(v => range(v, manifests))
return function test (v) {
return tests.some(fn => fn(v))
}
}
if (typeof version === 'number') {
version = version.toString()
} else if (typeof version !== 'string') {
throw new InvalidVersionError(version)
}
let gte
let lte
if (version.indexOf('..') === -1) {
gte = lte = resolve(version || 'latest')
} else {
const arr = version.split('..')
gte = resolve(arr[0] || 'oldest')
lte = resolve(arr[1] || 'latest')
}
return function test (v) {
const c1 = cmpRange(v, gte, false)
if (c1 < 0) return false
const c2 = cmpRange(v, lte, true)
if (c2 > 0) return false
return true
}
function resolve (v) {
if (v === 'oldest') return manifests[0].version
if (v === 'latest') return latest(manifests, 0)
if (/^-\d+$/.test(v) && v < 0) return latest(manifests, v * -1)
return v
}
}
function latest (manifests, n) {
for (let i = manifests.length - 1; i >= 0; i--) {
if (!isPrerelease(manifests[i].version) && (!n-- || i === 0)) {
return manifests[i].version
}
// Skip same version(s)
while (i > 0 && cmpVersion(manifests[i - 1].version, manifests[i].version) === 0) {
i--
}
}
// All are prereleases, return the last
return manifests[manifests.length - 1].version
}
function isPrerelease (version) {
return !version || prerelease.test(version)
}
function cmpVersion (a, b) {
return cmpRange(a, b, false)
}
function cmpRange (a, b, prefixOnly) {
// Missing version behaves like last prerelease
if (!a) return !b ? 0 : 1
if (!b) return -1
const ap = isPrerelease(a)
const bp = isPrerelease(b)
if (ap !== bp) return ap ? 1 : -1
const av = a.split('.')
const bv = b.split('.')
for (let i = 0; i < Math.min(av.length, bv.length); i++) {
const cmp = cmpElement(av[i], bv[i])
if (cmp > 0) return 1
if (cmp < 0) return -1
}
if (prefixOnly || av.length === bv.length) {
return 0
} else {
return av.length > bv.length ? 1 : -1
}
}
function cmpElement (a, b) {
if (numeric.test(a) && numeric.test(b)) {
return a - b
} else {
return a.localeCompare(b)
}
}
function insecureEnv () {
if (process.env.AIRTAP_IS_SECURE_ENV) {
return process.env.AIRTAP_IS_SECURE_ENV === 'false'
}
if (process.env.CI) {
if (isForkPr()) return true
if (process.env.TRAVIS_SECURE_ENV_VARS === 'false') return true
}
return false
}
function isObject (o) {
return typeof o === 'object' && o !== null && !Array.isArray(o)
}
class InvalidVersionError extends TypeError {
constructor (input) {
super('Version must be a string or number')
Object.defineProperty(this, 'name', { value: 'InvalidVersionError' })
Object.defineProperty(this, 'code', { value: 'ERR_INVALID_VERSION' })
Object.defineProperty(this, 'expected', { value: true })
Object.defineProperty(this, 'input', { value: input })
}
}
class NotFoundError extends Error {
constructor (input) {
super('No matching manifest found')
Object.defineProperty(this, 'name', { value: 'NotFoundError' })
Object.defineProperty(this, 'code', { value: 'ERR_MANIFEST_NOT_FOUND' })
Object.defineProperty(this, 'expected', { value: true })
Object.defineProperty(this, 'input', { value: input })
}
}