New buildbot syncing scripts that supports multiple builders and slaves
[WebKit.git] / Websites / perf.webkit.org / tools / js / remote.js
1 'use strict';
2
3 let assert = require('assert');
4 let http = require('http');
5 let https = require('https');
6 let querystring = require('querystring');
7
8 class RemoteAPI {
9     constructor(server)
10     {
11         this._server = null;
12         if (server)
13             this.configure(server);
14     }
15
16     url(path)
17     {
18         let scheme = this._server.scheme;
19         let port = this._server.port;
20         let portSuffix = (scheme == 'http' && port == 80) || (scheme == 'https' && port == 443) ? '' : `:${port}`;
21         if (path.charAt(0) != '/')
22             path = '/' + path;
23         return `${scheme}://${this._server.host}portSuffix${path}`;
24     }
25
26     configure(server)
27     {
28         assert(server.scheme === 'http' || server.scheme === 'https');
29         assert.equal(typeof(server.host), 'string');
30         assert(!server.port || typeof(server.port) == 'number');
31         assert(!server.auth || typeof(server.auth) == 'object');
32         this._server = server;
33     }
34
35     getJSON(path)
36     {
37         return this.sendHttpRequest(path, 'GET', null, null).then(function (result) {
38             return JSON.parse(result.responseText);
39         });
40     }
41
42     getJSONWithStatus(path)
43     {
44         return this.getJSON(path).then(function (result) {
45             if (result['status'] != 'OK')
46                 return Promise.reject(result);
47             return result;
48         });
49     }
50
51     postJSON(path, data)
52     {
53         const contentType = 'application/json';
54         const payload = JSON.stringify(data);
55         return this.sendHttpRequest(path, 'POST', 'application/json', payload).then(function (result) {
56             return JSON.parse(result.responseText);
57         });
58     }
59
60     postFormUrlencodedData(path, data)
61     {
62         const contentType = 'application/x-www-form-urlencoded';
63         const payload = querystring.stringify(data);
64         return this.sendHttpRequest(path, 'POST', contentType, payload).then(function (result) {
65             return result.responseText;
66         });
67     }
68
69     sendHttpRequest(path, method, contentType, content)
70     {
71         let server = this._server;
72         return new Promise(function (resolve, reject) {
73             let options = {
74                 hostname: server.host,
75                 port: server.port || 80,
76                 auth: server.auth ? server.auth.username + ':' + server.auth.password : null,
77                 method: method,
78                 path: path,
79             };
80
81             let request = (server.scheme == 'http' ? http : https).request(options, function (response) {
82                 let responseText = '';
83                 response.setEncoding('utf8');
84                 response.on('data', function (chunk) { responseText += chunk; });
85                 response.on('end', function () { resolve({statusCode: response.statusCode, responseText: responseText}); });
86             });
87
88             request.on('error', reject);
89
90             if (contentType)
91                 request.setHeader('Content-Type', contentType);
92
93             if (content)
94                 request.write(content);
95
96             request.end();
97         });
98     }
99 };
100
101 if (typeof module != 'undefined')
102     module.exports.RemoteAPI = RemoteAPI;