ui: Add --https flag to Vite dev server for HTTP/2 Adds a --https flag (also configurable via PERFETTO_UI_HTTPS=1) to ui/build.mjs so the Vite dev server can run over HTTPS and enable HTTP/2 multiplexing. Auto-generates and caches a self-signed localhost TLS certificate in out/ui/dev-ssl/ via openssl.
diff --git a/ui/build.mjs b/ui/build.mjs index afcf467..3a9d77f 100644 --- a/ui/build.mjs +++ b/ui/build.mjs
@@ -97,6 +97,7 @@ wasmModules: [], crossOriginIsolation: false, allowAllHosts: false, + https: false, testFilter: '', noOverrideGnArgs: false, @@ -238,6 +239,11 @@ 'can sit behind an arbitrary reverse proxy. Disables Vite\'s ' + 'DNS-rebind host-check protection.', }); + parser.add_argument('--https', { + action: 'store_true', + help: 'Serve the Vite dev server over HTTPS (enabling HTTP/2) using an ' + + 'auto-generated self-signed certificate.', + }); parser.add_argument('--test-filter', '-f', { help: "filter Jest tests by regex, e.g. 'chrome_render'", }); @@ -340,6 +346,7 @@ cfg.crossOriginIsolation = true; } cfg.allowAllHosts = !!args.allow_all_hosts; + cfg.https = !!args.https; cfg.check = !!args.typecheck; cfg.useHmr = cfg.watch && cfg.startHttpServer && !!!args.bundle; cfg.onlyWasmMemory64 = !!args.only_wasm_memory64; @@ -876,6 +883,57 @@ addTask(makeManifest, []); } +function ensureDevHttpsCert() { + const sslDir = ensureDir(pjoin(cfg.outDir, 'dev-ssl')); + const keyPath = pjoin(sslDir, 'key.pem'); + const certPath = pjoin(sslDir, 'cert.pem'); + let valid = false; + if (fs.existsSync(keyPath) && fs.existsSync(certPath)) { + try { + const cert = new crypto.X509Certificate(fs.readFileSync(certPath)); + valid = Date.parse(cert.validTo) > Date.now(); + } catch { + valid = false; + } + } + if (!valid) { + console.log(`Generating self-signed TLS cert in ${sslDir}...`); + const res = exec( + 'openssl', + [ + 'req', + '-x509', + '-newkey', + 'rsa:2048', + '-nodes', + '-keyout', + keyPath, + '-out', + certPath, + '-days', + '365', + '-subj', + '/CN=localhost', + '-addext', + 'subjectAltName=DNS:localhost,IP:127.0.0.1', + ], + {noErrCheck: true}, + ); + if (res.error?.code === 'ENOENT') { + console.error( + 'Error: --https requires `openssl` on PATH to generate a ' + + 'self-signed development certificate.', + ); + process.exit(1); + } + if (res.status !== 0) { + console.error(`openssl failed with code ${res.status}`); + process.exit(1); + } + } + return {key: keyPath, cert: certPath}; +} + // In dev (--watch --serve), Vite owns the user-facing port. It serves the // frontend entry as native ESM transformed on the fly; everything else // (wasm, fonts, css, /test/, /v1.2.3/-relative paths) is layered on as @@ -900,12 +958,15 @@ } : undefined; + const https = cfg.https ? ensureDevHttpsCert() : undefined; + const server = await createServer({ configFile: pjoin(ROOT_DIR, 'ui/vite.config.mjs'), server: { host: cfg.httpServerListenHost, port, strictPort: false, + https, headers, // By default Vite only accepts requests whose Host header matches the // bind host, to guard against DNS-rebind attacks. When --allow-all-hosts