diff --git a/source/utilities/cli.ts b/source/utilities/cli.ts index 6c7e6fa0..b09b45d0 100644 --- a/source/utilities/cli.ts +++ b/source/utilities/cli.ts @@ -55,8 +55,10 @@ const helpText = chalkTemplate` -S, --symlinks Resolve symlinks instead of showing 404 errors --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS + {grey Supported formats: PEM (default) and PKCS12 (PFX)} --ssl-key Optional path to the SSL/TLS certificate\'s private key + {grey Applicable only for PEM certificates} --ssl-pass Optional path to the SSL/TLS certificate\'s passphrase diff --git a/source/utilities/server.ts b/source/utilities/server.ts index 44d2e63e..1326f1ee 100644 --- a/source/utilities/server.ts +++ b/source/utilities/server.ts @@ -62,21 +62,31 @@ export const startServer = async ( }; // Create the server. - const useSsl = args['--ssl-cert'] && args['--ssl-key']; - const httpMode = useSsl ? 'https' : 'http'; + const sslCert = args['--ssl-cert']; + const sslKey = args['--ssl-key']; const sslPass = args['--ssl-pass']; - const serverConfig = - httpMode === 'https' && args['--ssl-cert'] && args['--ssl-key'] - ? { - key: await readFile(args['--ssl-key']), - cert: await readFile(args['--ssl-cert']), - passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', - } - : {}; - const server = - httpMode === 'https' - ? https.createServer(serverConfig, serverHandler) - : http.createServer(serverHandler); + const isPFXFormat = sslCert && /[.](?pfx|p12)$/.exec(sslCert); + const useSsl = sslCert && (sslKey || sslPass || isPFXFormat); + + let serverConfig: http.ServerOptions | https.ServerOptions = {}; + if (useSsl && sslCert && sslKey) { + // Format detected is PEM due to usage of SSL Key and Optional Passphrase. + serverConfig = { + key: await readFile(sslKey), + cert: await readFile(sslCert), + passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', + }; + } else if (useSsl && sslCert && isPFXFormat) { + // Format detected is PFX. + serverConfig = { + pfx: await readFile(sslCert), + passphrase: sslPass ? await readFile(sslPass, 'utf8') : '', + }; + } + + const server = useSsl + ? https.createServer(serverConfig, serverHandler) + : http.createServer(serverHandler); // Once the server starts, return the address it is running on so the CLI // can tell the user. @@ -101,8 +111,9 @@ export const startServer = async ( else address = details.address; const ip = getNetworkAddress(); - local = `${httpMode}://${address}:${details.port}`; - network = ip ? `${httpMode}://${ip}:${details.port}` : undefined; + const protocol = useSsl ? 'https' : 'http'; + local = `${protocol}://${address}:${details.port}`; + network = ip ? `${protocol}://${ip}:${details.port}` : undefined; } return { diff --git a/tests/__snapshots__/cli.test.ts.snap b/tests/__snapshots__/cli.test.ts.snap index e7a18109..830e6c1d 100644 --- a/tests/__snapshots__/cli.test.ts.snap +++ b/tests/__snapshots__/cli.test.ts.snap @@ -44,8 +44,10 @@ exports[`utilities/cli > render help text 1`] = ` -S, --symlinks Resolve symlinks instead of showing 404 errors --ssl-cert Optional path to an SSL/TLS certificate to serve with HTTPS + Supported formats: PEM (default) and PKCS12 (PFX) --ssl-key Optional path to the SSL/TLS certificate's private key + Applicable only for PEM certificates --ssl-pass Optional path to the SSL/TLS certificate's passphrase