forked from rollbar/rollbar.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserstack.browsers.js
152 lines (122 loc) · 3.95 KB
/
browserstack.browsers.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
var request = require('request');
var BROWSERSTACK_BROWSERS_ENDPOINT = 'https://www.browserstack.com/automate/browsers.json'
function annotate(browserList) {
return browserList.map(function(browser) {
browser.base = 'BrowserStack';
browser._alias = 'bs_' + browser.browser;
browser._version = parseFloat(browser.browser_version || '0');
browser._id = [
browser.browser,
browser.browser_version,
browser.device,
browser.os,
browser.os_version
].join('-').replace(/\s/g, '_');
return browser;
});
}
function versionAliases(browserList) {
var ret = [];
browserList.forEach(function(browser) {
if (browser._version) {
var copy = simpleCopy(browser);
copy._alias = 'bs_' + browser.browser + '_' + browser._version;
ret.push(copy);
}
});
return ret;
}
var browsers = [];
var sortedByBrowser = {};
try {
loadBrowsers(require('./browserstack.browsers.json'));
} catch (e) {
console.error('Could not read ./browserstack.browsers.json. Ignoring.', e);
}
function simpleCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
function loadBrowsers(browserListFromBrowserStack) {
browsers = annotate(browserListFromBrowserStack);
browsers = browsers.concat(versionAliases(browsers));
sortedByBrowser = {};
browsers.forEach(function(browser) {
var browserList = sortedByBrowser[browser._alias] = sortedByBrowser[browser._alias] || [];
browserList.push(browser);
});
// Sort all of the browsers by browser, _version
for (var browser in sortedByBrowser) {
var browserList = sortedByBrowser[browser];
var numBrowsers = browserList.length;
browserList.sort(function(a, b) {
return a._version - b._version;
});
// Store a reference in the global browsers array to the oldest, previous and latest of
// each browser.
if (numBrowsers > 0) {
var browserName = browser;
var oldest = simpleCopy(browserList[0]);
var latest = simpleCopy(browserList[browserList.length - 1]);
var previous;
oldest._alias = browserName + '_oldest';
latest._alias = browserName + '_latest';
browsers.push(oldest);
browsers.push(latest);
if (numBrowsers > 1) {
previous = simpleCopy(browserList[browserList.length - 2]);
previous._alias = browserName + '_previous';
browsers.push(previous);
}
}
}
}
function download(username, accessKey, callback) {
var auth = 'Basic ' + new Buffer(username + ':' + accessKey).toString('base64');
var req = {
url: BROWSERSTACK_BROWSERS_ENDPOINT,
headers: {
Authorization: auth
},
json: true
};
request.get(req, function(err, resp, body) {
if (err || resp.statusCode != 200) {
return callback(err || new Error('Received non 200 response from BrowserStack: ' + resp.statusCode));
}
// Save the downloaded browsers in memory
loadBrowsers(body);
fs.writeFile('./browserstack.browsers.json', JSON.stringify(body, null, ' '), callback);
});
}
/**
* Returns an array of Objects that match the requested filters.
* If no arguments are provided, an empty array is returned.
*
* E.g.
*
* filter('bs_latest') - Will return the latest browser Objects for each browser.
* filter('bs_latest', 'bs_previous') - Will return the latest and previous browser Objects for each browser.
* filter('bs_ie_latest', 'bs_oldest') - Will return the latest IE browser and all of the oldest browsers.
* filter('bs_all') - Will return the all of the browser Objects.
*
* @returns {Array}
*/
function filter() {
var ret = [];
var labels = Array.prototype.slice.call(arguments);
labels.forEach(function(label) {
browsers.forEach(function(browser) {
if (label === 'bs_all') {
ret.push(browser);
} else if (browser._alias.match(new RegExp(label.slice(2) + '$'))) {
ret.push(browser);
}
});
});
return ret;
}
module.exports = {
_browsers: browsers,
download: download,
filter: filter,
};