UNPKG

@web/test-runner-saucelabs

Version:
69 lines 2.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SauceLabsLauncherManager = exports.withTimeout = void 0; const saucelabs_1 = __importDefault(require("saucelabs")); const internal_ip_1 = __importDefault(require("internal-ip")); /** * Wraps a Promise with a timeout, rejecing the promise with the timeout. */ function withTimeout(promise, message) { return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { reject(new Error(message)); }, 5 * 60 * 1000); promise .then(val => resolve(val)) .catch(err => reject(err)) .finally(() => clearTimeout(timeoutId)); }); } exports.withTimeout = withTimeout; class SauceLabsLauncherManager { constructor(options, connectOptions) { this.launchers = new Set(); this.closeConnection = async () => { if (this.connection == null && this.connectionPromise == null) { // already closed return; } if (this.connection == null) { // wait for connection to finish opening await this.connectionPromise; } const connection = this.connection; this.connection = undefined; this.connectionPromise = undefined; if (connection != null) { await connection.close(); } }; this.options = options; this.connectOptions = connectOptions; this.api = new saucelabs_1.default(this.options); process.on('SIGINT', this.closeConnection); process.on('SIGTERM', this.closeConnection); process.on('beforeExit', this.closeConnection); process.on('exit', this.closeConnection); } async registerLauncher(launcher) { this.launchers.add(launcher); if (this.connectionPromise != null) { await this.connectionPromise; return; } console.log('[Saucelabs] Setting up Sauce Connect proxy...'); this.connectionPromise = withTimeout(this.api.startSauceConnect(Object.assign(Object.assign({}, this.connectOptions), { noSslBumpDomains: `127.0.0.1,localhost,${internal_ip_1.default.v4.sync()}` })), '[Saucelabs] Timed out setting up Sauce Connect proxy after 5 minutes.'); this.connection = await this.connectionPromise; } async deregisterLauncher(launcher) { this.launchers.delete(launcher); if (this.launchers.size === 0) { return this.closeConnection(); } } } exports.SauceLabsLauncherManager = SauceLabsLauncherManager; //# sourceMappingURL=SauceLabsLauncherManager.js.map