@knapsack/https
Version:
Automatically provisions and installs locally-trusted TLS certificates for Node.js https servers (including Express.js, etc.) using mkcert.
74 lines β’ 3.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.preInstallMkCert = preInstallMkCert;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const https_1 = __importDefault(require("https"));
const log_1 = require("../log");
const constants_1 = require("../constants");
async function download(url, filePath) {
try {
await new Promise((resolve, reject) => {
https_1.default
.get(url, (response) => {
const { statusCode } = response;
const redirectUrl = response.headers.location;
if (typeof redirectUrl !== 'string') {
return reject(new Error('redirectUrl is not a string'));
}
// Reject if itβs not one of the status codes we're expecting.
if (statusCode !== 302 && statusCode !== 200) {
return reject(new Error(`bad https download status code: ${statusCode}`));
}
const fileStream = fs_extra_1.default.createWriteStream(filePath);
https_1.default.get(redirectUrl, (resp2) => {
resp2.pipe(fileStream);
fileStream.on('finish', () => {
fileStream.close();
return resolve(true);
});
fileStream.on('error', (error) => {
fs_extra_1.default.unlinkSync(filePath);
reject(error);
});
});
})
.on('error', (error) => reject(error));
});
}
catch (error) {
log_1.log.error(`${error}`);
}
}
async function preInstallMkCert() {
/**
* Try to install the mkcert binary...
*
* 1. If you're running `knapsack start` (ie. locally)
* 2. Also, only if the binary doesn't already exist on your filesystem
*/
await fs_extra_1.default.mkdirpSync(constants_1.settingsPath);
const mkcertBinaryUrl = `https://github.com/FiloSottile/mkcert/releases/download/v${constants_1.version}/${constants_1.binaryName}`;
if (!fs_extra_1.default.existsSync(path_1.default.join(constants_1.settingsPath, constants_1.binaryName))) {
log_1.log.info(' ποΈ Downloading mkcert to automatically set up HTTPS for Knapsack');
log_1.log.info(' ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
log_1.log.info(` β°β Attempting to download the mkcert v${constants_1.version} binaryβ¦ \n`);
await download(mkcertBinaryUrl, path_1.default.join(constants_1.settingsPath, constants_1.binaryName))
.then(async () => {
if (fs_extra_1.default.existsSync(path_1.default.join(constants_1.settingsPath, constants_1.binaryName))) {
log_1.log.info(` β
Successfully downloaded the mkcert v${constants_1.version} binary!\n`);
fs_extra_1.default.chmodSync(path_1.default.join(constants_1.settingsPath, constants_1.binaryName), 0o755);
}
})
.catch((error) => {
log_1.log.error(error);
});
}
else {
log_1.log.info('β
mkcert already installed so we can auto-configure local HTTPS!');
}
}
//# sourceMappingURL=pre-install-mkcert.js.map