// Copyright 2021 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. const fs = require('fs'); const http = require('http'); const path = require('path'); const parseURL = require('url').parse; const {argv} = require('yargs'); const tracesMode = argv.traces || false; const serverPort = parseInt(process.env.PORT, 10) || (tracesMode ? 11010 : 8090); function getTestRunnerConfig() { try { return JSON.parse(process.env.TEST_RUNNER_JSON_CONFIG); } catch { // Return an empty object so any lookups return undefined return {}; } } function getTestRunnerConfigSetting(settingKey, fallbackValue) { const config = getTestRunnerConfig(); return config[settingKey] === undefined ? fallbackValue : config[settingKey]; } /** * When you run npm run components-server we run the script as is from scripts/, * but when this server is run as part of a test suite it's run from * out/Default/gen/scripts, so we have to do a bit of path mangling to figure * out where we are. */ const [target, isRunningInGen] = (() => { const regex = new RegExp(`out\\${path.sep}(.*)\\${path.sep}gen`); const match = regex.exec(__dirname); if (match) { return [match[1], true]; } return [argv.target || process.env.TARGET || 'Default', false]; })(); /** * This configures the base of the URLs that are injected into each component * doc example to load. By default it's /, so that we load /front_end/..., but * this can be configured if you have a different file structure. */ const sharedResourcesBase = argv.sharedResourcesBase || getTestRunnerConfigSetting('component-server-shared-resources-path', '/'); /** * The server assumes that examples live in * devtoolsRoot/out/Target/gen/front_end/ui/components/docs, but if you need to add a * prefix you can pass this argument. Passing `foo` will redirect the server to * look in devtoolsRoot/out/Target/gen/foo/front_end/ui/components/docs. */ const componentDocsBaseArg = argv.componentDocsBase || process.env.COMPONENT_DOCS_BASE || getTestRunnerConfigSetting('component-server-base-path', ''); let pathToOutTargetDir = __dirname; /** * If we are in the gen directory, we need to find the out/Default folder to use * as our base to find files from. We could do this with path.join(x, '..', * '..') until we get the right folder, but that's brittle. It's better to * search up for out/Default to be robust to any folder structures. */ while (isRunningInGen && !pathToOutTargetDir.endsWith(`out${path.sep}${target}`)) { pathToOutTargetDir = path.resolve(pathToOutTargetDir, '..'); } /* If we are not running in out/Default, we'll assume the script is running from the repo root, and navigate to {CWD}/out/Target */ const pathToBuiltOutTargetDirectory = isRunningInGen ? pathToOutTargetDir : path.resolve(path.join(process.cwd(), 'out', target)); let devtoolsRootFolder = path.resolve( path.join(pathToBuiltOutTargetDirectory, 'gen'), ); const fullCheckoutDevtoolsRootFolder = path.join( devtoolsRootFolder, 'third_party', 'devtools-frontend', 'src', ); if (__dirname.startsWith(fullCheckoutDevtoolsRootFolder)) { devtoolsRootFolder = fullCheckoutDevtoolsRootFolder; } const componentDocsBaseFolder = path.join( devtoolsRootFolder, componentDocsBaseArg, ); if (!fs.existsSync(devtoolsRootFolder)) { console.error( `ERROR: Generated front_end folder (${devtoolsRootFolder}) does not exist.`, ); console.log( 'The components server works from the built Ninja output; you may need to run Ninja to update your built DevTools.', ); console.log( 'If you build to a target other than default, you need to pass --target=X as an argument', ); process.exit(1); } process.on('uncaughtException', error => { console.error('uncaughtException', error); }); process.on('unhandledRejection', error => { console.error('unhandledRejection', error); }); const server = http.createServer( (req, res) => requestHandler(req, res).catch(err => send500(res, err)), ); server.listen(serverPort, 'localhost'); server.once('listening', () => { if (process.send) { process.send(serverPort); } console.log(`Started components server at http://localhost:${serverPort}\n`); console.log( `ui/components/docs location: ${ path.relative( process.cwd(), path.join( componentDocsBaseFolder, 'front_end', 'ui', 'components', 'docs', ), )}`, ); }); server.once('error', error => { if (process.send) { process.send('ERROR'); } throw error; }); // All paths that are injected globally into real DevTools, so we do the same // to avoid styles being broken in the component server. const styleSheetPaths = [ 'front_end/design_system_tokens.css', 'front_end/application_tokens.css', 'front_end/ui/components/buttons/textButton.css', 'front_end/ui/legacy/inspectorCommon.css', 'front_end/ui/components/docs/component_docs_styles.css', ]; function createComponentIndexFile(componentPath, componentExamples) { const componentName = componentPath.replace('/front_end/ui/components/docs/', '').replace(/_/g, ' ').replace('/', ''); // clang-format off return `
', toggleDarkModeScript + '\n'); respondWithHtml(response, newFileContents); } else { // This means it's an asset like a JS file or an image. let fullPath = path.join(componentDocsBaseFolder, filePath); if (fullPath.endsWith(path.join('locales', 'en-US.json')) && !componentDocsBaseFolder.includes(sharedResourcesBase)) { /** * If the path is for locales/en-US.json we special case the loading of that to fix the path so it works properly in the server. * We also make sure that we take into account the shared resources base; * but if the base folder already contains the shared resources base, we don't * add it to the path, because otherwise that would cause the shared resources * base to be duplicated in the fullPath. */ // Rewrite this path so we can load up the locale in the component-docs let prefix = componentDocsBaseFolder; if (sharedResourcesBase && !componentDocsBaseFolder.includes(sharedResourcesBase)) { prefix = path.join(componentDocsBaseFolder, sharedResourcesBase); } fullPath = path.join( prefix, 'front_end', 'core', 'i18n', 'locales', 'en-US.json', ); } if (!fullPath.startsWith(devtoolsRootFolder) && !fileIsInTestFolder) { console.error( `Path ${fullPath} is outside the DevTools Frontend root dir.`, ); process.exit(1); } const fileExists = await checkFileExists(fullPath); if (!fileExists) { send404(response, '404, File not found'); return; } let encoding = 'utf8'; if (fullPath.endsWith('.js') || fullPath.endsWith('.mjs')) { response.setHeader('Content-Type', 'text/javascript; charset=utf-8'); } else if (fullPath.endsWith('.css')) { response.setHeader('Content-Type', 'text/css; charset=utf-8'); } else if (fullPath.endsWith('.wasm')) { response.setHeader('Content-Type', 'application/wasm'); encoding = 'binary'; } else if (fullPath.endsWith('.svg')) { response.setHeader('Content-Type', 'image/svg+xml; charset=utf-8'); } else if (fullPath.endsWith('.png')) { response.setHeader('Content-Type', 'image/png'); encoding = 'binary'; } else if (fullPath.endsWith('.jpg')) { response.setHeader('Content-Type', 'image/jpg'); encoding = 'binary'; } else if (fullPath.endsWith('.avif')) { response.setHeader('Content-Type', 'image/avif'); encoding = 'binary'; } else if (fullPath.endsWith('.gz')) { response.setHeader('Content-Type', 'application/gzip'); encoding = 'binary'; } const fileContents = await fs.promises.readFile(fullPath, encoding); response.writeHead(200); response.write(fileContents, encoding); response.end(); } } function createTracesIndexFile(traceFilenames) { function pageFunction() { const origin = new URL(location.href).origin; document.body.addEventListener('click', async e => { if (!e.target.matches('button')) { return; } const filename = e.target.textContent; const traceUrl = `${origin}/t/${filename}`; const devtoolsLoadingTraceUrl = `devtools://devtools/bundled/devtools_app.html?loadTimelineFromURL=${traceUrl}`; try { await navigator.clipboard.writeText(devtoolsLoadingTraceUrl); e.target.classList.add('clicked'); setTimeout(() => e.target.classList.remove('clicked'), 1000); } catch (e) { console.error(e); } }); } // clang-format off return `