Spaces:
Running
Running
File size: 8,283 Bytes
979bf48 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "createSelfSignedCertificate", {
enumerable: true,
get: function() {
return createSelfSignedCertificate;
}
});
const _nodefs = /*#__PURE__*/ _interop_require_default(require("node:fs"));
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
const _nodecrypto = require("node:crypto");
const _getcachedirectory = require("./helpers/get-cache-directory");
const _log = /*#__PURE__*/ _interop_require_wildcard(require("../build/output/log"));
const _nodechild_process = require("node:child_process");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function(nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
return {
default: obj
};
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {
__proto__: null
};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for(var key in obj){
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
const { WritableStream } = require('node:stream/web');
const MKCERT_VERSION = 'v1.4.4';
function getBinaryName() {
const platform = process.platform;
const arch = process.arch === 'x64' ? 'amd64' : process.arch;
if (platform === 'win32') {
return `mkcert-${MKCERT_VERSION}-windows-${arch}.exe`;
}
if (platform === 'darwin') {
return `mkcert-${MKCERT_VERSION}-darwin-${arch}`;
}
if (platform === 'linux') {
return `mkcert-${MKCERT_VERSION}-linux-${arch}`;
}
throw Object.defineProperty(new Error(`Unsupported platform: ${platform}`), "__NEXT_ERROR_CODE", {
value: "E141",
enumerable: false,
configurable: true
});
}
async function downloadBinary() {
try {
const binaryName = getBinaryName();
const cacheDirectory = (0, _getcachedirectory.getCacheDirectory)('mkcert');
const binaryPath = _nodepath.default.join(cacheDirectory, binaryName);
if (_nodefs.default.existsSync(binaryPath)) {
return binaryPath;
}
const downloadUrl = `https://github.com/FiloSottile/mkcert/releases/download/${MKCERT_VERSION}/${binaryName}`;
await _nodefs.default.promises.mkdir(cacheDirectory, {
recursive: true
});
_log.info(`Downloading mkcert package...`);
const response = await fetch(downloadUrl);
if (!response.ok || !response.body) {
throw Object.defineProperty(new Error(`request failed with status ${response.status}`), "__NEXT_ERROR_CODE", {
value: "E109",
enumerable: false,
configurable: true
});
}
_log.info(`Download response was successful, writing to disk`);
const binaryWriteStream = _nodefs.default.createWriteStream(binaryPath);
await response.body.pipeTo(new WritableStream({
write (chunk) {
return new Promise((resolve, reject)=>{
binaryWriteStream.write(chunk, (error)=>{
if (error) {
reject(error);
return;
}
resolve();
});
});
},
close () {
return new Promise((resolve, reject)=>{
binaryWriteStream.close((error)=>{
if (error) {
reject(error);
return;
}
resolve();
});
});
}
}));
await _nodefs.default.promises.chmod(binaryPath, 493);
return binaryPath;
} catch (err) {
_log.error('Error downloading mkcert:', err);
}
}
async function createSelfSignedCertificate(host, certDir = 'certificates') {
try {
const binaryPath = await downloadBinary();
if (!binaryPath) throw Object.defineProperty(new Error('missing mkcert binary'), "__NEXT_ERROR_CODE", {
value: "E198",
enumerable: false,
configurable: true
});
const resolvedCertDir = _nodepath.default.resolve(process.cwd(), `./${certDir}`);
await _nodefs.default.promises.mkdir(resolvedCertDir, {
recursive: true
});
const keyPath = _nodepath.default.resolve(resolvedCertDir, 'localhost-key.pem');
const certPath = _nodepath.default.resolve(resolvedCertDir, 'localhost.pem');
if (_nodefs.default.existsSync(keyPath) && _nodefs.default.existsSync(certPath)) {
const cert = new _nodecrypto.X509Certificate(_nodefs.default.readFileSync(certPath));
const key = _nodefs.default.readFileSync(keyPath);
if (cert.checkHost(host ?? 'localhost') && cert.checkPrivateKey((0, _nodecrypto.createPrivateKey)(key))) {
_log.info('Using already generated self signed certificate');
const caLocation = (0, _nodechild_process.execSync)(`"${binaryPath}" -CAROOT`).toString().trim();
return {
key: keyPath,
cert: certPath,
rootCA: `${caLocation}/rootCA.pem`
};
}
}
_log.info('Attempting to generate self signed certificate. This may prompt for your password');
const defaultHosts = [
'localhost',
'127.0.0.1',
'::1'
];
const hosts = host && !defaultHosts.includes(host) ? [
...defaultHosts,
host
] : defaultHosts;
(0, _nodechild_process.execSync)(`"${binaryPath}" -install -key-file "${keyPath}" -cert-file "${certPath}" ${hosts.join(' ')}`, {
stdio: 'ignore'
});
const caLocation = (0, _nodechild_process.execSync)(`"${binaryPath}" -CAROOT`).toString().trim();
if (!_nodefs.default.existsSync(keyPath) || !_nodefs.default.existsSync(certPath)) {
throw Object.defineProperty(new Error('Certificate files not found'), "__NEXT_ERROR_CODE", {
value: "E131",
enumerable: false,
configurable: true
});
}
_log.info(`CA Root certificate created in ${caLocation}`);
_log.info(`Certificates created in ${resolvedCertDir}`);
const gitignorePath = _nodepath.default.resolve(process.cwd(), './.gitignore');
if (_nodefs.default.existsSync(gitignorePath)) {
const gitignore = await _nodefs.default.promises.readFile(gitignorePath, 'utf8');
if (!gitignore.includes(certDir)) {
_log.info('Adding certificates to .gitignore');
await _nodefs.default.promises.appendFile(gitignorePath, `\n${certDir}`);
}
}
return {
key: keyPath,
cert: certPath,
rootCA: `${caLocation}/rootCA.pem`
};
} catch (err) {
_log.error('Failed to generate self-signed certificate. Falling back to http.', err);
}
}
//# sourceMappingURL=mkcert.js.map |