* Starts the server if it is not already running. * @param {number=} opt_timeoutMs How long to wait, in milliseconds, for the * server to start accepting requests. Defaults to 30 seconds. * @return {!Promise } A promise that will resolve to the server's base * URL when it
(opt_timeoutMs)
| 203 | * before the server has started, the promise will be rejected. |
| 204 | */ |
| 205 | start(opt_timeoutMs) { |
| 206 | if (this.address_) { |
| 207 | return this.address_ |
| 208 | } |
| 209 | |
| 210 | const timeout = opt_timeoutMs || DriverService.DEFAULT_START_TIMEOUT_MS |
| 211 | const self = this |
| 212 | |
| 213 | let resolveCommand |
| 214 | this.command_ = new Promise((resolve) => (resolveCommand = resolve)) |
| 215 | |
| 216 | this.address_ = new Promise((resolveAddress, rejectAddress) => { |
| 217 | resolveAddress( |
| 218 | Promise.resolve(this.port_).then((port) => { |
| 219 | if (port <= 0) { |
| 220 | throw Error('Port must be > 0: ' + port) |
| 221 | } |
| 222 | |
| 223 | return resolveCommandLineFlags(this.args_).then((args) => { |
| 224 | const command = exec(self.executable_, { |
| 225 | args: args, |
| 226 | env: self.env_, |
| 227 | stdio: self.stdio_, |
| 228 | }) |
| 229 | |
| 230 | resolveCommand(command) |
| 231 | |
| 232 | const earlyTermination = command.result().then(function (result) { |
| 233 | const error = |
| 234 | result.code == null |
| 235 | ? Error('Server was killed with ' + result.signal) |
| 236 | : Error('Server terminated early with status ' + result.code) |
| 237 | rejectAddress(error) |
| 238 | self.address_ = null |
| 239 | self.command_ = null |
| 240 | throw error |
| 241 | }) |
| 242 | |
| 243 | let hostname = self.hostname_ |
| 244 | if (!hostname) { |
| 245 | hostname = (!self.loopbackOnly_ && net.getAddress()) || net.getLoopbackAddress() |
| 246 | } |
| 247 | |
| 248 | const serverUrl = url.format({ |
| 249 | protocol: 'http', |
| 250 | hostname: hostname, |
| 251 | port: port + '', |
| 252 | pathname: self.path_, |
| 253 | }) |
| 254 | |
| 255 | return new Promise((fulfill, reject) => { |
| 256 | let cancelToken = earlyTermination.catch((e) => reject(Error(e.message))) |
| 257 | |
| 258 | httpUtil.waitForServer(serverUrl, timeout, cancelToken).then( |
| 259 | (_) => fulfill(serverUrl), |
| 260 | (err) => { |
| 261 | if (err instanceof httpUtil.CancellationError) { |
| 262 | fulfill(serverUrl) |