UNPKG

2.55 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const util_1 = require("util");
12// This import lacks type definitions.
13const launchSauceConnect = util_1.promisify(require('sauce-connect-launcher'));
14/**
15 * Service that can be used to create a SauceConnect tunnel automatically. This can be used
16 * in case developers don't set up the tunnel using the plain SauceConnect binaries.
17 */
18function SauceConnect(emitter, logger) {
19 const log = logger.create('launcher.SauceConnect');
20 // Currently active tunnel instance. See: https://github.com/bermi/sauce-connect-launcher
21 // for public API.
22 let activeInstancePromise = null;
23 this.establishTunnel = (connectOptions) => __awaiter(this, void 0, void 0, function* () {
24 // Redirect all logging output to Karma's logger.
25 connectOptions.logger = log.debug.bind(log);
26 // In case there is already a promise for a SauceConnect tunnel, we still need to return the
27 // promise because we want to make sure that the launcher can wait in case the tunnel is
28 // still starting.
29 if (activeInstancePromise) {
30 return activeInstancePromise;
31 }
32 // Open a new SauceConnect tunnel.
33 return activeInstancePromise = launchSauceConnect(connectOptions);
34 });
35 // Close the tunnel whenever Karma emits the "exit" event. In that case, we don't need to
36 // reset the state because Karma will exit completely.
37 emitter.on('exit', (doneFn) => {
38 if (activeInstancePromise) {
39 log.debug('Shutting down Sauce Connect');
40 // Close the tunnel and notify Karma once the tunnel has been exited.
41 activeInstancePromise
42 .then(instance => instance.close(doneFn()))
43 .catch(() => doneFn());
44 }
45 else {
46 doneFn();
47 }
48 });
49}
50exports.SauceConnect = SauceConnect;
51//# sourceMappingURL=sauceconnect.js.map
\No newline at end of file