revert more changes from #17188

This commit is contained in:
spalger 2018-03-15 23:54:10 -07:00
parent 381fcb22fb
commit 60ee5883b0
5 changed files with 0 additions and 182 deletions

View file

@ -1,78 +0,0 @@
import { EventEmitter } from 'events';
import { createLocalBrowserdriverApi } from './browserdriver_local_api';
import { createRemoteBrowserdriverApi } from './browserdriver_remote_api';
import { ping } from './ping';
const noop = () => {};
/**
* Api for interacting with a local or remote instance of a browser
*
* @type {Object}
*/
export class BrowserdriverApi extends EventEmitter {
static async factory(log, url, browserName) {
return (await ping(url))
? createRemoteBrowserdriverApi(log, url)
: createLocalBrowserdriverApi(log, url, browserName);
}
constructor(options = {}) {
super();
const {
url,
start = noop,
stop = noop,
browser,
} = options;
if (!url) {
throw new TypeError('url is a required parameter');
}
this._browser = browser;
this._url = url;
this._state = undefined;
this._callCustomStart = () => start(this);
this._callCustomStop = () => stop(this);
this._beforeStopFns = [];
}
getBrowserName() {
return this._browser;
}
getUrl() {
return this._url;
}
beforeStop(fn) {
this._beforeStopFns.push(fn);
}
isStopped() {
return this._state === 'stopped';
}
async start() {
if (this._state !== undefined) {
throw new Error('Driver can only be started once');
}
this._state = 'started';
await this._callCustomStart();
}
async stop() {
if (this._state !== 'started') {
throw new Error('Driver can only be stopped after being started');
}
this._state = 'stopped';
for (const fn of this._beforeStopFns.splice(0)) {
await fn();
}
await this._callCustomStop();
}
}

View file

@ -1,75 +0,0 @@
import { spawn } from 'child_process';
import { parse as parseUrl } from 'url';
import treeKill from 'tree-kill';
import { delay, fromNode as fcb } from 'bluebird';
import { path as CHROMEDRIVER_EXEC } from 'chromedriver';
import { path as FIREFOXDRIVER_EXEC } from 'geckodriver';
import { ping } from './ping';
import { BrowserdriverApi } from './browserdriver_api';
const START_TIMEOUT = 15000;
const PING_INTERVAL = 500;
export function createLocalBrowserdriverApi(log, url, browser) {
let runningDriver = null;
const driverName = browser + 'driver';
switch (browser) {
case 'firefox':
runningDriver = FIREFOXDRIVER_EXEC;
break;
default:
runningDriver = CHROMEDRIVER_EXEC;
}
let proc = null;
return new BrowserdriverApi({
url,
browser,
async start(api) {
const { port } = parseUrl(url);
log.debug('Starting local ' + driverName + ' at port %d', port);
proc = spawn(runningDriver, [`--port=${port}`], {
stdio: ['ignore', 'pipe', 'pipe'],
});
proc.stdout.on('data', chunk => {
log.debug('[' + driverName + ':stdout]', chunk.toString('utf8').trim());
});
proc.stderr.on('data', chunk => {
log.debug('[' + driverName + ':stderr]', chunk.toString('utf8').trim());
});
proc.on('exit', (code) => {
if (!api.isStopped() || code > 0) {
api.emit('error', new Error(driverName + ` exited with code ${code}`));
}
});
const pingsStartedAt = Date.now();
while (true) {
log.debug('[' + driverName + ':ping] attempting to reach at %j', url);
if (await ping(url)) {
log.debug('[' + driverName + ':ping] success');
break;
} else {
log.debug('[' + driverName + ':ping] failure');
}
if ((Date.now() - pingsStartedAt) < START_TIMEOUT) {
log.debug('[' + driverName + ':ping] waiting for %d before next ping', PING_INTERVAL);
await delay(PING_INTERVAL);
continue;
}
throw new Error(driverName + ` did not start within the ${START_TIMEOUT}ms timeout`);
}
},
async stop() {
await fcb(cb => treeKill(proc.pid, undefined, cb));
}
});
}

View file

@ -1,12 +0,0 @@
import { BrowserdriverApi } from './browserdriver_api';
export function createRemoteBrowserdriverApi(log, url) {
return new BrowserdriverApi({
url,
start() {
log.info(`Reusing instance at %j`, url);
}
});
}

View file

@ -1 +0,0 @@
export { BrowserdriverApi } from './browserdriver_api';

View file

@ -1,16 +0,0 @@
import request from 'request';
import { fromNode as fcb } from 'bluebird';
export async function ping(url) {
try {
await Promise.race([
fcb(cb => request(url, cb)),
new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('timeout')), 1000);
})
]);
return true;
} catch (err) {
return false;
}
}