UNPKG

@theintern/digdug

Version:

Dig Dug. A simple abstraction library for downloading and launching WebDriver service tunnels.

166 lines 6.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var fs_1 = require("fs"); var os_1 = require("os"); var path_1 = require("path"); var common_1 = require("@theintern/common"); var Tunnel_1 = tslib_1.__importDefault(require("./Tunnel")); var util_1 = require("./lib/util"); var child_process_1 = require("child_process"); var cbtVersion = '1.2.2'; var CrossBrowserTestingTunnel = (function (_super) { tslib_1.__extends(CrossBrowserTestingTunnel, _super); function CrossBrowserTestingTunnel(options) { return _super.call(this, Object.assign({ accessKey: process.env.CBT_APIKEY, cbtVersion: cbtVersion, environmentUrl: 'https://crossbrowsertesting.com/api/v3/selenium/browsers?format=json', executable: 'node', hostname: 'hub.crossbrowsertesting.com', port: '80', username: process.env.CBT_USERNAME, }, options || {})) || this; } Object.defineProperty(CrossBrowserTestingTunnel.prototype, "auth", { get: function () { return (this.username || '') + ":" + (this.accessKey || ''); }, enumerable: false, configurable: true }); Object.defineProperty(CrossBrowserTestingTunnel.prototype, "extraCapabilities", { get: function () { return { username: this.username, password: this.accessKey, }; }, enumerable: false, configurable: true }); Object.defineProperty(CrossBrowserTestingTunnel.prototype, "isDownloaded", { get: function () { try { require('cbt_tunnels'); return true; } catch (error) { return false; } }, enumerable: false, configurable: true }); CrossBrowserTestingTunnel.prototype.download = function (forceDownload) { var _this = this; if (forceDownload === void 0) { forceDownload = false; } if (!forceDownload && this.isDownloaded) { return common_1.Task.resolve(); } return new common_1.Task(function (resolve, reject) { child_process_1.exec("npm install --no-save cbt_tunnels@'" + _this.cbtVersion + "'", function (error, _stdout, stderr) { if (error) { console.error(stderr); reject(error); } else { resolve(); } }); }); }; CrossBrowserTestingTunnel.prototype._makeArgs = function (readyFile) { if (!this.username || !this.accessKey) { throw new Error('CrossBrowserTestingTunnel requires a username and access key'); } return [ 'node_modules/.bin/cbt_tunnels', '--authkey', this.accessKey, '--username', this.username, '--ready', readyFile, ]; }; CrossBrowserTestingTunnel.prototype.sendJobState = function (jobId, data) { var payload = JSON.stringify({ action: 'set_score', score: data.status || data.success ? 'pass' : 'fail', }); var url = "https://crossbrowsertesting.com/api/v3/selenium/" + jobId; return common_1.request(url, { method: 'put', data: payload, headers: { 'Content-Length': String(Buffer.byteLength(payload, 'utf8')), 'Content-Type': 'application/json', }, username: this.username, password: this.accessKey, proxy: this.proxy, }).then(function (response) { if (response.status !== 200) { return response.text().then(function (text) { if (text) { var data_1 = JSON.parse(text); if (data_1.status) { throw new Error("Could not save test status (" + data_1.message + ")"); } throw new Error("Server reported " + response.status + " with: " + text); } else { throw new Error("Server reported " + response.status + " with no other data."); } }); } }); }; CrossBrowserTestingTunnel.prototype._start = function (executor) { var _this = this; var readyFile = path_1.join(os_1.tmpdir(), 'CrossBrowserTesting-' + Date.now()); return this._makeChild(function (child, resolve, reject) { var stdout = []; fs_1.watchFile(readyFile, { persistent: false, interval: 1007 }, function (current, previous) { if (Number(current.mtime) === Number(previous.mtime)) { return; } fs_1.unwatchFile(readyFile); readHandle.destroy(); exitHandle.destroy(); stdout = null; resolve(); }); var readHandle = util_1.on(child.stdout, 'data', function (data) { stdout.push(String(data)); }); var exitHandle = util_1.on(child, 'exit', function () { process.stderr.write(stdout.join('')); }); _this._handle = common_1.createCompositeHandle(readHandle, exitHandle); executor(child, resolve, reject); }, readyFile); }; CrossBrowserTestingTunnel.prototype._normalizeEnvironment = function (environment) { var platform = environment.api_name; return environment.browsers.map(function (browser) { var browserName = browser.type.toLowerCase(); return { platform: platform, browserName: browserName, version: browser.version, descriptor: environment, intern: { browserName: browserName, version: browser.version, browser_api_name: browser.api_name, os_api_name: platform, }, }; }); }; return CrossBrowserTestingTunnel; }(Tunnel_1.default)); exports.default = CrossBrowserTestingTunnel; //# sourceMappingURL=CrossBrowserTestingTunnel.js.map