-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
280 lines (226 loc) · 5.58 KB
/
api.js
File metadata and controls
280 lines (226 loc) · 5.58 KB
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
var util = require("util");
var BaseClient = require("./client");
var extend = require("./extend");
function ApiBaseClient(settings) {
this.server = {
host: "api.browserstack.com"
};
BaseClient.call(this, settings);
}
util.inherits(ApiBaseClient, BaseClient);
// public API
extend(ApiBaseClient.prototype, {
getBrowsers: function(fn) {
this._getBrowsers(function(error, browsers) {
if (!error) {
this.updateLatest(browsers);
}
fn(error, browsers);
}.bind(this));
},
createWorker: function(options, fn) {
if (options[this.versionField] === "latest") {
return this.getLatest(options, function(error, version) {
if (error) {
return fn(error);
}
options = extend({}, options);
options[this.versionField] = version;
this.createWorker(options, fn);
}.bind(this));
}
var data = JSON.stringify(options);
this.request({
path: this.path("/worker"),
method: "POST"
}, data, fn);
},
getWorker: function(id, fn) {
this.request({
path: this.path("/worker/" + id)
}, fn);
},
changeUrl: function(id, options, fn) {
var data = JSON.stringify(options);
this.request({
path: this.path("/worker/" + id + "/url.json"),
method: "PUT"
}, data, fn);
},
terminateWorker: function(id, fn) {
this.request({
path: this.path("/worker/" + id),
method: "DELETE"
}, fn);
},
getWorkers: function(fn) {
this.request({
path: this.path("/workers")
}, fn);
},
getLatest: function(browser, fn) {
var latest = this.latest;
if (typeof browser === "function") {
fn = browser;
browser = null;
}
// there may be a lot of createWorker() calls with "latest" version
// so minimize the number of calls to getBrowsers()
if (this.latestPending) {
return setTimeout(function() {
this.getLatest(browser, fn);
}.bind(this), 50);
}
// only cache browsers for one day
if (!latest || this.latestUpdate < (new Date() - 864e5)) {
this.latestPending = true;
return this.getBrowsers(function(error) {
this.latestPending = false;
if (error) {
return fn(error);
}
this.getLatest(browser, fn);
}.bind(this));
}
process.nextTick(function() {
fn(null, browser ? latest[this.getBrowserId(browser)] : extend({}, latest));
}.bind(this));
},
takeScreenshot: function(id, fn) {
this.request({
path: this.path("/worker/" + id + "/screenshot.json")
}, fn);
}
});
// internal API
extend(ApiBaseClient.prototype, {
latest: null,
latestUpdate: 0,
latestPending: false,
path: function(path) {
return "/" + this.version + path;
},
updateLatest: function(browsers) {
var latest = this.latest = {};
var getBrowserId = this.getBrowserId.bind(this);
var versionField = this.versionField;
this.latestUpdate = new Date();
browsers.forEach(function(browser) {
var version = browser[versionField];
var browserId = getBrowserId(browser);
// ignore devices that don't have versions
if (!version) {
return;
}
// ignore pre-release versions
if (/\s/.test(version)) {
return;
}
if (parseFloat(version) > (parseFloat(latest[browserId]) || 0)) {
latest[browserId] = version;
}
});
},
getBrowserId: function(browser) {
return this._getBrowserId(browser).toLowerCase();
}
});
// Versions
ApiBaseClient.versions = {};
ApiBaseClient.latestVersion = 0;
ApiBaseClient.createVersion = function(version, prototype) {
function ApiClient(settings) {
ApiBaseClient.call(this, settings);
}
util.inherits(ApiClient, ApiBaseClient);
ApiClient.prototype.version = version;
extend(ApiClient.prototype, prototype);
ApiBaseClient.versions[version] = ApiClient;
ApiBaseClient.latestVersion = Math.max(ApiBaseClient.latestVersion, version);
};
ApiBaseClient.createVersion(1, {
useHttp: true,
versionField: "version",
_getBrowsers: function(fn) {
this.request({
path: this.path("/browsers")
}, fn);
},
_getBrowserId: function(browser) {
return browser.browser;
}
});
ApiBaseClient.createVersion(2, {
useHttp: true,
versionField: "version",
_getBrowsers: function(fn) {
this.request({
path: this.path("/browsers")
}, function(error, osBrowsers) {
if (error) {
return fn(error);
}
fn(null, [].concat.apply([],
Object.keys(osBrowsers).map(function(os) {
return osBrowsers[os].map(function(browser) {
browser.os = os;
return browser;
});
})
));
});
},
_getBrowserId: function(browser) {
return browser.os + ":" + (browser.browser || browser.device);
}
});
ApiBaseClient.createVersion(3, {
useHttp: true,
versionField: "browser_version",
_getBrowsers: function(fn) {
this.request({
path: this.path("/browsers?flat=true")
}, fn);
},
_getBrowserId: function(browser) {
var id = browser.os + ":" + browser.os_version + ":" + browser.browser;
if (browser.device) {
id += ":" + browser.device;
}
return id;
},
getApiStatus: function(fn) {
this.request({
path: this.path("/status")
}, fn);
}
});
ApiBaseClient.createVersion(4, {
versionField: "browser_version",
_getBrowsers: function(fn) {
this.request({
path: this.path("/browsers?flat=true")
}, fn);
},
_getBrowserId: function(browser) {
var id = browser.os + ":" + browser.os_version + ":" + browser.browser;
if (browser.device) {
id += ":" + browser.device;
}
return id;
},
getApiStatus: function(fn) {
this.request({
path: this.path("/status")
}, fn);
}
});
module.exports = {
createClient: function(settings) {
var ApiClient = ApiBaseClient.versions[settings.version || ApiBaseClient.latestVersion];
if (!ApiClient) {
throw new Error("Invalid version");
}
return new ApiClient(settings);
}
};