(options: { port?: number, preferredPort?: number, host?: string } = {})
| 148 | } |
| 149 | |
| 150 | async start(options: { port?: number, preferredPort?: number, host?: string } = {}): Promise<void> { |
| 151 | assert(!this._started, 'server already started'); |
| 152 | this._started = true; |
| 153 | |
| 154 | const host = options.host; |
| 155 | if (options.preferredPort) { |
| 156 | try { |
| 157 | await startHttpServer(this._server, { port: options.preferredPort, host }); |
| 158 | } catch (e) { |
| 159 | if (!e || !e.message || !e.message.includes('EADDRINUSE')) |
| 160 | throw e; |
| 161 | await startHttpServer(this._server, { host }); |
| 162 | } |
| 163 | } else { |
| 164 | await startHttpServer(this._server, { port: options.port, host }); |
| 165 | } |
| 166 | |
| 167 | const address = this._server.address(); |
| 168 | assert(address, 'Could not bind server socket'); |
| 169 | if (typeof address === 'string') { |
| 170 | this._urlPrefixPrecise = address; |
| 171 | this._urlPrefixHumanReadable = address; |
| 172 | } else { |
| 173 | this._port = address.port; |
| 174 | this._urlPrefixPrecise = `http://${urlHostFromAddress(address)}:${address.port}`; |
| 175 | this._urlPrefixHumanReadable = `http://${host ?? 'localhost'}:${address.port}`; |
| 176 | this._allowedHosts = computeAllowedHosts(host, address.address); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | async stop() { |
| 181 | await new Promise(cb => this._server!.close(cb)); |
no test coverage detected