@knapsack/https
Version:
Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.
35 lines • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSSLCert = getSSLCert;
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const auto_encrypt_localhost_1 = require("./auto-encrypt-localhost");
const constants_1 = require("../constants");
const keyFilePath = (0, path_1.join)(constants_1.settingsPath, 'localhost-key.pem');
const certFilePath = (0, path_1.join)(constants_1.settingsPath, 'localhost.pem');
async function getSSLCert(ssl) {
// passing in custom SSL certs
if (ssl && (ssl === null || ssl === void 0 ? void 0 : ssl.key) && (ssl === null || ssl === void 0 ? void 0 : ssl.cert)) {
if ((0, fs_extra_1.existsSync)(ssl.key) && (0, fs_extra_1.existsSync)(ssl.cert)) {
return {
key: (0, fs_extra_1.readFileSync)(ssl.key, 'utf-8'),
cert: (0, fs_extra_1.readFileSync)(ssl.cert, 'utf-8'),
};
}
}
// try to reuse existing SSL certs if they already exist
if ((0, fs_extra_1.existsSync)(certFilePath) && (0, fs_extra_1.existsSync)(keyFilePath)) {
return {
key: (0, fs_extra_1.readFileSync)(keyFilePath, 'utf-8'),
cert: (0, fs_extra_1.readFileSync)(certFilePath, 'utf-8'),
};
}
// as a "last resort", use mkcert to try and auto-generate SSL certs.
return (0, auto_encrypt_localhost_1.setupAndGetCert)().then((result) => {
if (result && result.key && result.cert) {
return result;
}
return null;
});
}
//# sourceMappingURL=get-ssl-cert.js.map