(app: Express, cliArgs?: Partial<ServerArgs>)
| 254 | } |
| 255 | |
| 256 | async function createServer(app: Express, cliArgs?: Partial<ServerArgs>): Promise<HttpServer> { |
| 257 | const {sslCertificatePath, sslKeyPath} = cliArgs ?? {}; |
| 258 | if (!sslCertificatePath && !sslKeyPath) { |
| 259 | return http.createServer(app); |
| 260 | } |
| 261 | if (!sslCertificatePath || !sslKeyPath) { |
| 262 | throw new Error(`Both certificate path and key path must be provided to enable TLS`); |
| 263 | } |
| 264 | |
| 265 | const certKey = [sslCertificatePath, sslKeyPath]; |
| 266 | const [certExists, keyExists] = await Promise.all(certKey.map((p) => fs.exists(p))); |
| 267 | for (const [exists, desc, p] of [ |
| 268 | [certExists, 'certificate', sslCertificatePath], |
| 269 | [keyExists, 'key', sslKeyPath], |
| 270 | ]) { |
| 271 | if (!exists) { |
| 272 | throw new Error(`The provided SSL ${desc} at '${p}' does not exist or is not accessible`); |
| 273 | } |
| 274 | } |
| 275 | const [cert, key] = (await Promise.all(certKey.map((p) => fs.readFile(p, 'utf8')))) as [string, string]; |
| 276 | log.debug('Enabling TLS on the server using the provided certificate'); |
| 277 | |
| 278 | return https.createServer({cert, key}, app) as unknown as HttpServer; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Attaches Appium-specific behavior to the HTTP server and returns it as {@linkcode AppiumServer}. |
no test coverage detected