@@ -3,6 +3,10 @@ const path = require('path');
3
3
const { default : got } = require ( 'got' ) ;
4
4
const split = require ( 'split' ) ;
5
5
6
+ function delay ( duration ) {
7
+ return new Promise ( resolve => global . setTimeout ( resolve , duration ) ) ;
8
+ }
9
+
6
10
function ChromeDriver (
7
11
host ,
8
12
port ,
@@ -20,19 +24,18 @@ function ChromeDriver(
20
24
21
25
this . path = require . resolve ( 'electron-chromedriver/chromedriver' ) ;
22
26
this . urlBase = '/' ;
23
- this . statusUrl =
24
- 'http://' + this . host + ':' + this . port + this . urlBase + 'status' ;
27
+ this . statusUrl = `http://${ this . host } :${ this . port } ${ this . urlBase } status` ;
25
28
this . logLines = [ ] ;
26
29
}
27
30
28
31
ChromeDriver . prototype . start = function ( ) {
29
32
if ( this . process ) throw new Error ( 'ChromeDriver already started' ) ;
30
33
31
- const args = [ this . path , ' --port=' + this . port , ' --url-base=' + this . urlBase ] ;
34
+ const args = [ this . path , ` --port=${ this . port } ` , ` --url-base=${ this . urlBase } ` ] ;
32
35
33
36
if ( this . chromeDriverLogPath ) {
34
37
args . push ( '--verbose' ) ;
35
- args . push ( ' --log-path=' + this . chromeDriverLogPath ) ;
38
+ args . push ( ` --log-path=${ this . chromeDriverLogPath } ` ) ;
36
39
}
37
40
const options = {
38
41
cwd : this . workingDirectory ,
@@ -50,34 +53,30 @@ ChromeDriver.prototype.start = function () {
50
53
return this . waitUntilRunning ( ) ;
51
54
} ;
52
55
53
- ChromeDriver . prototype . waitUntilRunning = function ( ) {
54
- const self = this ;
55
- return new Promise ( function ( resolve , reject ) {
56
- const startTime = Date . now ( ) ;
57
- const checkIfRunning = function ( ) {
58
- self . isRunning ( function ( running ) {
59
- if ( ! self . process ) {
60
- return reject ( Error ( 'ChromeDriver has been stopped' ) ) ;
61
- }
62
-
63
- if ( running ) {
64
- return resolve ( ) ;
65
- }
66
-
67
- const elapsedTime = Date . now ( ) - startTime ;
68
- if ( elapsedTime > self . startTimeout ) {
69
- return reject (
70
- Error (
71
- 'ChromeDriver did not start within ' + self . startTimeout + 'ms'
72
- )
73
- ) ;
74
- }
75
-
76
- global . setTimeout ( checkIfRunning , 100 ) ;
77
- } ) ;
78
- } ;
79
- checkIfRunning ( ) ;
80
- } ) ;
56
+ ChromeDriver . prototype . waitUntilRunning = async function ( ) {
57
+ const startTime = Date . now ( ) ;
58
+ for ( ; ; ) {
59
+ const isRunning = await this . isRunning ( ) ;
60
+
61
+ if ( ! self . process ) {
62
+ throw new Error ( 'ChromeDriver has been stopped' ) ;
63
+ }
64
+
65
+ if ( self . process . exitCode !== null ) {
66
+ throw new Error ( `ChromeDriver exited with code ${ self . process . exitCode } ` ) ;
67
+ }
68
+
69
+ if ( isRunning ) {
70
+ return ;
71
+ }
72
+
73
+ const elapsedTime = Date . now ( ) - startTime ;
74
+ if ( elapsedTime > self . startTimeout ) {
75
+ throw new Error ( `ChromeDriver did not start within ${ self . startTimeout } ms` ) ;
76
+ }
77
+
78
+ await delay ( 100 ) ;
79
+ }
81
80
} ;
82
81
83
82
ChromeDriver . prototype . setupLogs = function ( ) {
@@ -120,12 +119,13 @@ ChromeDriver.prototype.stop = function () {
120
119
this . clearLogs ( ) ;
121
120
} ;
122
121
123
- ChromeDriver . prototype . isRunning = function ( callback ) {
124
- const cb = false ;
125
- got ( this . statusUrl )
126
- . json ( )
127
- . then ( ( { value } ) => callback ( value && value . ready ) )
128
- . catch ( ( ) => callback ( cb ) ) ;
122
+ ChromeDriver . prototype . isRunning = function ( ) {
123
+ try {
124
+ const { value } = got ( this . statusUrl ) . json ( ) ;
125
+ return value && value . ready ;
126
+ } catch ( _ ) {
127
+ return false ;
128
+ }
129
129
} ;
130
130
131
131
ChromeDriver . prototype . getLogs = function ( ) {
0 commit comments