Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
feat: working sauce tunnel runner
Browse files Browse the repository at this point in the history
  • Loading branch information
w33ble committed Mar 30, 2018
1 parent c4b85a5 commit c1e39fc
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 35 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@std/esm": "^0.18.0",
"sauce-connect-launcher": "^1.2.3",
"serve-script": "^0.1.1",
"uuid": "^3.2.1",
"wd": "^1.6.1"
},
"devDependencies": {
Expand Down
5 changes: 1 addition & 4 deletions src/get_browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ const SAUCE_PORT = 80;

export default function getBrowser(sauceUser, sauceKey, capabilities, tunnelId) {
// build the wd options based on capabilities and tunnelId
const options = {
...capabilities,
'tunnel-identifier': tunnelId,
};
const options = Object.assign({}, capabilities, { 'tunnel-identifier': tunnelId });

const browser = wd.remote(SAUCE_URL, SAUCE_PORT, sauceUser, sauceKey);

Expand Down
8 changes: 2 additions & 6 deletions src/get_tunnel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ export default function getTunnel(user, key, tunnelId) {

return new Promise((resolve, reject) =>
sauceConnect(config, (err, sauceConnectProcess) => {
if (err) {
reject(err);
return;
}

resolve(sauceConnectProcess);
if (err) reject(err);
else resolve(sauceConnectProcess);
})
);
}
58 changes: 38 additions & 20 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import events from 'events';
import uuid from 'uuid/v4';
import getTunnel from './get_tunnel';
import getBrowser from './get_browser';
import runCode from './run_code';
import getTunnel from './get_tunnel.mjs';
import getBrowser from './get_browser.mjs';
import openConnection from './open_connection.mjs';

const randNum = len => parseInt(String(Math.random()).substr(2, len), 10);

export default class HorseySauce extends events.EventEmitter {
constructor(sauceUser, sauceKey) {
Expand All @@ -12,38 +13,55 @@ export default class HorseySauce extends events.EventEmitter {
this.sauceKey = sauceKey;
this.isRunning = false;
this.tunnel = null;
this.tunnelId = `horsey-sauce-${Date.now()}-${uuid()}`;
this.tunnelId = `horsey-sauce-${Date.now()}-${randNum(4)}-${randNum(6)}`;
}

run(src, capabilities) {
// for capabilities, see https://saucelabs.com/docs/additional-config
run(src, runner, capabilities = {}) {
// only allow one running process at a time
if (this.isRunning) return Promise.reject(new Error('Another test is currently running'));

this.isRunning = true;

// create the sauce connect tunnel
this.emit('tunnel-connect');
return Promise.resolve(this.tunnel || getTunnel()).then(
return Promise.resolve(
this.tunnel || getTunnel(this.sauceUser, this.sauceKey, this.tunnelId)
).then(
tunnel => {
this.tunnel = tunnel;
this.emit('tunnel', tunnel);
this.emit('tunnel-connected');

return getBrowser(this.sauceUser, this.sauceKey, capabilities, this.tunnelId)
.then(browser => {
return getBrowser(this.sauceUser, this.sauceKey, capabilities, this.tunnelId).then(
browser => {
this.isRunning = false;
this.emit('browser', browser);
this.emit('browser-ready');

// do things in the browser
return runCode(src, browser).then(() => {
browser.quit(() => {
// ignore errors here
// TODO: do any sort of post-processing...
});
});
})
.catch(() => {
return openConnection(src, browser).then(
closeConnection =>
new Promise((resolve, reject) => {
this.emit('runner-start');
runner(browser, (err, data) => {
closeConnection().then(() => {
if (err) {
this.emit('runner-error', err);
reject(err);
} else {
this.emit('runner-complete', data);
resolve(data);
}
});
});
})
);
},
err => {
this.isRunning = false;
});
this.emit('browser-error', err);
throw err;
}
);
},
err => {
this.emit('tunnel-error', err);
Expand Down
45 changes: 45 additions & 0 deletions src/open_connection.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import http from 'http';
import serve from 'serve-script';

function startServer(server, port) {
return new Promise((resolve, reject) => {
server.listen(port, err => {
if (err) reject(err);
else resolve(server.address().port);
});
});
}

function initRemote(port, browser) {
return new Promise((resolve, reject) => {
const url = `http://localhost:${port}/`;

browser.get(url, err => {
if (err) reject(err);
else resolve();
});
});
}

export default function openConnection(src, browser, options = {}) {
const config = Object.assign(
{
port: 8000,
},
options
);

const app = serve({ src });
const server = http.createServer(app);

return startServer(server, config.port)
.then(port => initRemote(port, browser))
.then(
() =>
function closeConnection() {
return new Promise(resolve => {
server.close(() => browser.quit(() => resolve()));
});
}
);
}
3 changes: 0 additions & 3 deletions src/run_code.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4728,7 +4728,7 @@ util-deprecate@^1.0.2, util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"

uuid@^3.0.0, uuid@^3.2.1:
uuid@^3.0.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"

Expand Down

0 comments on commit c1e39fc

Please sign in to comment.