UNPKG

@knapsack/https

Version:

Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.

133 lines 7.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.installCertutil = installCertutil; const os_1 = __importDefault(require("os")); const child_process_1 = __importDefault(require("child_process")); const log_1 = require("../log"); // Does the passed command exist? Returns: bool. function commandExists(command) { try { child_process_1.default.execFileSync('which', [command], { env: process.env }); return true; } catch (error) { return false; } } // // Private. // // On Linux, we must install nss for mkcert to work with both Chrome and Firefox. // Depending on the platform we try to do so using apt, yum, or pacman. If none of // those exist, we fail. function installCertutilOnLinux() { if (commandExists('certutil')) { // Already installed log_1.log.verbose('done.\n'); return; } log_1.log.info(' 💽️ ❨auto-encrypt-localhost❩ Installing certutil dependency (Linux) '); const options = { env: process.env }; try { if (commandExists('apt')) { log_1.log.verbose('using apt… \n'); options.env.DEBIAN_FRONTEND = 'noninteractive'; child_process_1.default.execSync('sudo apt-get install -y -q libnss3-tools', options); } else if (commandExists('yum')) { // Untested: if you test this, please let me know https://github.com/indie-mirror/https-server/issues log_1.log.verbose(' 🤪 ❨auto-encrypt-localhost❩ Attempting to install required dependency using yum. This is currently untested. If it works (or blows up) for you, I’d appreciate it if you could open an issue at https://github.com/indie-mirror/https-server/issues and let me know. Thanks! – Aral\n'); child_process_1.default.execSync('sudo yum install nss-tools', options); log_1.log.verbose(' ✔️ ❨auto-encrypt-localhost❩ Certutil installed using yum.'); } else if (commandExists('pacman')) { child_process_1.default.execSync('sudo pacman -S nss', options); log_1.log.verbose(' ✔️ ❨auto-encrypt-localhost❩ Certutil installed using pacman.'); } else { // Neither Homebrew nor MacPorts is installed. Warn the person. log_1.log.verbose(' ⚠️ ❨auto-encrypt-localhost❩ Linux: No supported package manager found for installing certutil on Linux (tried apt, yum, and pacman. Please install certutil manually and run Auto Encrypt Localhost again. For more instructions on installing mkcert dependencies, please see https://github.com/FiloSottile/mkcert/\n'); } } catch (error) { // There was an error and we couldn’t install the dependency. Warn the person. log_1.log.verbose(' ⚠️ ❨auto-encrypt-localhost❩ Linux: Failed to install nss. Please install it manually and run Auto Encrypt Localhost again if you want your certificate to work in Chrome and Firefox', error); } } // On macOS, we install nss for mkcert to work with Firefox. To // install nss, we can use either Homebrew or Macports. // If neither Homebrew or MacPorts is installed, we warn the person that // they need to install it manually if they want their certificates to work // in Firefox. function installCertutilOnDarwin() { const options = { env: process.env }; if (commandExists('brew')) { // Check if nss installed using brew (we can’t just check using commandExists as // nss is installed as keg-only and not symlinked to /usr/local due to issues // with Firefox crashing). try { // Homebrew can take a long time start, show current status. child_process_1.default.execSync('brew list nss >/dev/null 2>&1', options); log_1.log.verbose(' done.'); } catch (error) { // NSS is not installed. Install it. try { log_1.log.info(' 💽️ ❨auto-encrypt-localhost❩Installing certutil dependency (Darwin) using Homebrew… '); child_process_1.default.execSync('brew install nss >/dev/null 2>&1', options); log_1.log.verbose('done.'); } catch (err) { log_1.log.warn(' ⚠️ ❨auto-encrypt-localhost❩ macOS: Failed to install nss via Homebrew. Please install it manually and run Auto Encrypt Localhost again if you want your certificate to work in Firefox', err); } } } else if (commandExists('port')) { // Untested. This is based on the documentation at https://guide.macports.org/#using.port.installed. I don’t have MacPorts installed // and it doesn’t play well with Homebrew so I won’t be testing this anytime soon. If you do, please let me know how it works // by opening an issue on https://github.com/indie-mirror/https-server/issues log_1.log.warn(' 🤪 ❨auto-encrypt-localhost❩ Attempting to install required dependency using MacPorts. This is currently untested. If it works (or blows up) for you, I’d appreciate it if you could open an issue at https://github.com/indie-mirror/https-server/issues and let me know. Thanks! – Aral\n'); try { child_process_1.default.execSync('port installed nss', options); } catch (error) { // nss is not installed, attempt to install it using MacPorts. try { child_process_1.default.execSync('sudo port install nss', options); } catch (err) { log_1.log.error(' ⚠️ ❨auto-encrypt-localhost❩ macOS: Failed to install nss via MacPorts. Please install it manually and run Auto Encrypt Localhost again if you want your certificate to work in Firefox', error); } } } else { // Neither Homebrew nor MacPorts is installed. Warn the person. log_1.log.error(' ⚠️ ❨auto-encrypt-localhost❩ macOS: Cannot install certutil (nss) as you don’t have Homebrew or MacPorts installed.\n\n If you want your certificate to work in Firefox, please install one of those package managers and then install nss manually:\n ╰─ * Homebrew (https://brew.sh): brew install nss ╰─ * MacPorts(https://macports.org): sudo port install nss\n'); } } // Mozilla’s nss is used on Linux to install the certificate in Chrome and Firefox // and on macOS for Firefox. Ensure it exists. // Source: https://github.com/FiloSottile/mkcert/blob/master/README.md#installation function installCertutil() { log_1.log.info(` 📜 ❨auto-encrypt-localhost❩ Installing certutil if necessary… `); const platform = os_1.default.platform(); if (platform === 'linux') { installCertutilOnLinux(); } else if (platform === 'darwin') { installCertutilOnDarwin(); } else if (platform === 'win32') { // Do nothing. According to the mkcert documentation, certutil is not // required on Windows. log_1.log.verbose('done.'); } else { // Unknown platform. This should have been caught earlier. Panic. throw new Error(` 🤯 ❨auto-encrypt-localhost❩ Panic: Unknown platform detected. ${platform}`); } } //# sourceMappingURL=install-cert-util.js.map