X-Git-Url: https://git.webkit.org/?p=WebKit.git;a=blobdiff_plain;f=Websites%2Fperf.webkit.org%2Ftools%2Fjs%2Fremote.js;h=d2510191ff29e4372f841599549d98db9eda72a3;hp=64e381d72e537bcb7efa95d931d1e70eee8560cb;hb=08c7c40bae5cad080e9bc8b6636ba33a3a4f0c9e;hpb=ce25363c708892572558e90a4c32cc85d97d2daf diff --git a/Websites/perf.webkit.org/tools/js/remote.js b/Websites/perf.webkit.org/tools/js/remote.js index 64e381d..d251019 100644 --- a/Websites/perf.webkit.org/tools/js/remote.js +++ b/Websites/perf.webkit.org/tools/js/remote.js @@ -3,56 +3,97 @@ let assert = require('assert'); let http = require('http'); let https = require('https'); +let querystring = require('querystring'); -let RemoteAPI = new (class RemoteAPI { - constructor() +class RemoteAPI { + constructor(server) { - this._server = { - scheme: 'http', - host: 'localhost', - } + this._server = null; + if (server) + this.configure(server); + } + + url(path) + { + let scheme = this._server.scheme; + let port = this._server.port; + let portSuffix = this._server.port == this._server.defaultPort ? '' : `:${port}`; + if (path.charAt(0) != '/') + path = '/' + path; + return `${scheme}://${this._server.host}${portSuffix}${path}`; } configure(server) { assert(server.scheme === 'http' || server.scheme === 'https'); - assert.equal(typeof(server.host), 'string'); - assert(!server.port || typeof(server.port) == 'number'); - assert(!server.auth || typeof(server.auth) == 'object'); - this._server = server; + assert.equal(typeof(server.host), 'string', 'host should be a string'); + assert(!server.port || typeof(server.port) == 'number', 'port should be a number'); + + let auth = null; + if (server.auth) { + assert.equal(typeof(server.auth), 'object', 'auth should be a dictionary with username and password as keys'); + assert.equal(typeof(server.auth.username), 'string', 'auth should contain a string username'); + assert.equal(typeof(server.auth.password), 'string', 'auth should contain a string password'); + auth = { + username: server.auth.username, + password: server.auth.password, + }; + } + + const defaultPort = server.scheme == 'http' ? 80 : 443; + this._server = { + scheme: server.scheme, + host: server.host, + port: server.port || defaultPort, + defaultPort: defaultPort, + auth: auth, + }; } - fetchJSON(path, data) + getJSON(path) { - let contentType = null; - if (data) { - contentType = 'application/json'; - data = JSON.stringify(data); - } - return this.sendHttpRequest(path, 'GET', contentType, data).then(function (result) { + return this.sendHttpRequest(path, 'GET', null, null).then(function (result) { return JSON.parse(result.responseText); }); } - fetchJSONWithStatus(path, data) + getJSONWithStatus(path) { - return this.fetchJSON(path, data).then(function (result) { + return this.getJSON(path).then(function (result) { if (result['status'] != 'OK') return Promise.reject(result); return result; }); } + postJSON(path, data) + { + const contentType = 'application/json'; + const payload = JSON.stringify(data); + return this.sendHttpRequest(path, 'POST', 'application/json', payload).then(function (result) { + return JSON.parse(result.responseText); + }); + } + + postFormUrlencodedData(path, data) + { + const contentType = 'application/x-www-form-urlencoded'; + const payload = querystring.stringify(data); + return this.sendHttpRequest(path, 'POST', contentType, payload).then(function (result) { + return result.responseText; + }); + } + sendHttpRequest(path, method, contentType, content) { let server = this._server; return new Promise(function (resolve, reject) { let options = { hostname: server.host, - port: server.port || 80, - auth: server.auth, + port: server.port, + auth: server.auth ? server.auth.username + ':' + server.auth.password : null, method: method, - path: path, + path: escape(path), }; let request = (server.scheme == 'http' ? http : https).request(options, function (response) { @@ -73,7 +114,7 @@ let RemoteAPI = new (class RemoteAPI { request.end(); }); } -}) +}; if (typeof module != 'undefined') module.exports.RemoteAPI = RemoteAPI;