Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | const fs = require('fs'); |
| 6 | const http = require('http'); |
| 7 | const path = require('path'); |
| 8 | const parseURL = require('url').parse; |
| 9 | const {argv} = require('yargs'); |
Tim van der Lippe | 23283e1 | 2021-05-10 15:08:31 +0100 | [diff] [blame] | 10 | |
| 11 | const {createInstrumenter} = require('istanbul-lib-instrument'); |
| 12 | const convertSourceMap = require('convert-source-map'); |
| 13 | const defaultIstanbulSchema = require('@istanbuljs/schema'); |
| 14 | |
Jack Franklin | a3dd06a | 2021-03-18 10:47:16 +0000 | [diff] [blame] | 15 | const {getTestRunnerConfigSetting} = require('../test/test_config_helpers.js'); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 16 | |
Jack Franklin | fe65bc6 | 2021-03-16 11:23:20 +0000 | [diff] [blame] | 17 | const serverPort = parseInt(process.env.PORT, 10) || 8090; |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 18 | const target = argv.target || process.env.TARGET || 'Default'; |
| 19 | |
| 20 | /** |
| 21 | * This configures the base of the URLs that are injected into each component |
| 22 | * doc example to load. By default it's /, so that we load /front_end/..., but |
| 23 | * this can be configured if you have a different file structure. |
| 24 | */ |
Jack Franklin | fe65bc6 | 2021-03-16 11:23:20 +0000 | [diff] [blame] | 25 | const sharedResourcesBase = |
| 26 | argv.sharedResourcesBase || getTestRunnerConfigSetting('component-server-shared-resources-path', '/'); |
Jack Franklin | 086ccd5 | 2020-11-27 11:00:14 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 29 | * The server assumes that examples live in |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 30 | * devtoolsRoot/out/Target/gen/front_end/ui/components/docs, but if you need to add a |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 31 | * prefix you can pass this argument. Passing `foo` will redirect the server to |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 32 | * look in devtoolsRoot/out/Target/gen/foo/front_end/ui/components/docs. |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 33 | */ |
Jack Franklin | fe65bc6 | 2021-03-16 11:23:20 +0000 | [diff] [blame] | 34 | const componentDocsBaseArg = argv.componentDocsBase || process.env.COMPONENT_DOCS_BASE || |
| 35 | getTestRunnerConfigSetting('component-server-base-path', ''); |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
Jack Franklin | 086ccd5 | 2020-11-27 11:00:14 +0000 | [diff] [blame] | 38 | * When you run npm run components-server we run the script as is from scripts/, |
| 39 | * but when this server is run as part of a test suite it's run from |
| 40 | * out/Default/gen/scripts, so we have to do a bit of path mangling to figure |
| 41 | * out where we are. |
| 42 | */ |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 43 | const isRunningInGen = __dirname.includes(path.join('out', path.sep, target)); |
Jack Franklin | b36ad7e | 2020-12-07 10:20:52 +0000 | [diff] [blame] | 44 | |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 45 | let pathToOutTargetDir = __dirname; |
| 46 | /** |
| 47 | * If we are in the gen directory, we need to find the out/Default folder to use |
| 48 | * as our base to find files from. We could do this with path.join(x, '..', |
| 49 | * '..') until we get the right folder, but that's brittle. It's better to |
| 50 | * search up for out/Default to be robust to any folder structures. |
| 51 | */ |
| 52 | while (isRunningInGen && !pathToOutTargetDir.endsWith(`out${path.sep}${target}`)) { |
| 53 | pathToOutTargetDir = path.resolve(pathToOutTargetDir, '..'); |
| 54 | } |
Jack Franklin | 0943df4 | 2021-02-26 11:12:13 +0000 | [diff] [blame] | 55 | |
| 56 | /* 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 */ |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 57 | const pathToBuiltOutTargetDirectory = |
Jack Franklin | 0943df4 | 2021-02-26 11:12:13 +0000 | [diff] [blame] | 58 | isRunningInGen ? pathToOutTargetDir : path.resolve(path.join(process.cwd(), 'out', target)); |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 59 | |
Jack Franklin | 90b6613 | 2021-01-05 11:33:43 +0000 | [diff] [blame] | 60 | const devtoolsRootFolder = path.resolve(path.join(pathToBuiltOutTargetDirectory, 'gen')); |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 61 | const componentDocsBaseFolder = path.join(devtoolsRootFolder, componentDocsBaseArg); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 62 | |
Jack Franklin | 90b6613 | 2021-01-05 11:33:43 +0000 | [diff] [blame] | 63 | if (!fs.existsSync(devtoolsRootFolder)) { |
| 64 | console.error(`ERROR: Generated front_end folder (${devtoolsRootFolder}) does not exist.`); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 65 | console.log( |
| 66 | 'The components server works from the built Ninja output; you may need to run Ninja to update your built DevTools.'); |
| 67 | console.log('If you build to a target other than default, you need to pass --target=X as an argument'); |
| 68 | process.exit(1); |
| 69 | } |
| 70 | |
Jack Franklin | 086ccd5 | 2020-11-27 11:00:14 +0000 | [diff] [blame] | 71 | const server = http.createServer(requestHandler); |
| 72 | server.listen(serverPort); |
| 73 | server.once('listening', () => { |
| 74 | if (process.send) { |
| 75 | process.send(serverPort); |
| 76 | } |
| 77 | console.log(`Started components server at http://localhost:${serverPort}\n`); |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 78 | console.log(`ui/components/docs location: ${ |
| 79 | path.relative(process.cwd(), path.join(componentDocsBaseFolder, 'front_end', 'ui', 'components', 'docs'))}`); |
Jack Franklin | 086ccd5 | 2020-11-27 11:00:14 +0000 | [diff] [blame] | 80 | }); |
| 81 | |
| 82 | server.once('error', error => { |
| 83 | if (process.send) { |
| 84 | process.send('ERROR'); |
| 85 | } |
| 86 | throw error; |
| 87 | }); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 88 | |
| 89 | function createComponentIndexFile(componentPath, componentExamples) { |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 90 | const componentName = componentPath.replace('/front_end/ui/components/docs/', '').replace(/_/g, ' ').replace('/', ''); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 91 | // clang-format off |
| 92 | return `<!DOCTYPE html> |
| 93 | <html> |
| 94 | <head> |
| 95 | <meta charset="UTF-8" /> |
| 96 | <meta name="viewport" content="width=device-width" /> |
| 97 | <title>DevTools component: ${componentName}</title> |
| 98 | <style> |
| 99 | h1 { text-transform: capitalize; } |
| 100 | |
| 101 | .example { |
| 102 | padding: 5px; |
| 103 | margin: 10px; |
| 104 | } |
Jack Franklin | 7a75e46 | 2021-01-08 16:17:13 +0000 | [diff] [blame] | 105 | |
| 106 | a:link, |
| 107 | a:visited { |
| 108 | color: blue; |
| 109 | text-decoration: underline; |
| 110 | } |
| 111 | |
| 112 | a:hover { |
| 113 | text-decoration: none; |
| 114 | } |
| 115 | .example summary { |
| 116 | font-size: 20px; |
| 117 | } |
| 118 | |
| 119 | .back-link { |
| 120 | padding-left: 5px; |
| 121 | font-size: 16px; |
| 122 | font-style: italic; |
| 123 | } |
| 124 | |
| 125 | iframe { display: block; width: 100%; height: 400px; } |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 126 | </style> |
| 127 | </head> |
| 128 | <body> |
Jack Franklin | 7a75e46 | 2021-01-08 16:17:13 +0000 | [diff] [blame] | 129 | <h1> |
| 130 | ${componentName} |
| 131 | <a class="back-link" href="/">Back to index</a> |
| 132 | </h1> |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 133 | ${componentExamples.map(example => { |
Jack Franklin | 90b6613 | 2021-01-05 11:33:43 +0000 | [diff] [blame] | 134 | const fullPath = path.join(componentPath, example); |
Jack Franklin | 7a75e46 | 2021-01-08 16:17:13 +0000 | [diff] [blame] | 135 | return `<details class="example"> |
| 136 | <summary><a href="${fullPath}">${example.replace('.html', '').replace(/-|_/g, ' ')}</a></summary> |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 137 | <iframe src="${fullPath}"></iframe> |
Jack Franklin | 7a75e46 | 2021-01-08 16:17:13 +0000 | [diff] [blame] | 138 | </details>`; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 139 | }).join('\n')} |
| 140 | </body> |
| 141 | </html>`; |
| 142 | // clang-format on |
| 143 | } |
| 144 | |
| 145 | function createServerIndexFile(componentNames) { |
| 146 | // clang-format off |
| 147 | return `<!DOCTYPE html> |
| 148 | <html> |
| 149 | <head> |
| 150 | <meta charset="UTF-8" /> |
| 151 | <meta name="viewport" content="width=device-width" /> |
| 152 | <title>DevTools components</title> |
| 153 | <style> |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 154 | a:link, a:visited { |
| 155 | color: blue; |
| 156 | text-transform: capitalize; |
| 157 | text-decoration: none; |
| 158 | } |
| 159 | a:hover { |
| 160 | text-decoration: underline; |
| 161 | } |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 162 | </style> |
| 163 | </head> |
| 164 | <body> |
| 165 | <h1>DevTools components</h1> |
| 166 | <ul> |
| 167 | ${componentNames.map(name => { |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 168 | const niceName = name.replace(/_/g, ' '); |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 169 | return `<li><a href='/front_end/ui/components/docs/${name}'>${niceName}</a></li>`; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 170 | }).join('\n')} |
| 171 | </ul> |
| 172 | </body> |
| 173 | </html>`; |
| 174 | // clang-format on |
| 175 | } |
| 176 | |
| 177 | async function getExamplesForPath(filePath) { |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 178 | const componentDirectory = path.join(componentDocsBaseFolder, filePath); |
Jack Franklin | c150122 | 2020-10-02 09:42:08 +0100 | [diff] [blame] | 179 | const allFiles = await fs.promises.readdir(componentDirectory); |
| 180 | const htmlExampleFiles = allFiles.filter(file => { |
| 181 | return path.extname(file) === '.html'; |
| 182 | }); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 183 | |
Jack Franklin | c150122 | 2020-10-02 09:42:08 +0100 | [diff] [blame] | 184 | return createComponentIndexFile(filePath, htmlExampleFiles); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | function respondWithHtml(response, html) { |
| 188 | response.setHeader('Content-Type', 'text/html; charset=utf-8'); |
| 189 | response.writeHead(200); |
| 190 | response.write(html, 'utf8'); |
| 191 | response.end(); |
| 192 | } |
| 193 | |
| 194 | function send404(response, message) { |
| 195 | response.writeHead(404); |
| 196 | response.write(message, 'utf8'); |
| 197 | response.end(); |
| 198 | } |
| 199 | |
Jack Franklin | 279564e | 2020-07-06 15:25:18 +0100 | [diff] [blame] | 200 | async function checkFileExists(filePath) { |
| 201 | try { |
| 202 | const errorsAccessingFile = await fs.promises.access(filePath, fs.constants.R_OK); |
| 203 | return !errorsAccessingFile; |
| 204 | } catch (e) { |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | |
Tim van der Lippe | 23283e1 | 2021-05-10 15:08:31 +0100 | [diff] [blame] | 209 | const EXCLUDED_COVERAGE_FOLDERS = new Set(['third_party', 'ui/components/docs', 'Images']); |
| 210 | |
| 211 | /** |
| 212 | * @param {string} filePath |
| 213 | * @returns {boolean} |
| 214 | */ |
| 215 | function isIncludedForCoverageComputation(filePath) { |
| 216 | for (const excludedFolder of EXCLUDED_COVERAGE_FOLDERS) { |
| 217 | if (filePath.startsWith(`/front_end/${excludedFolder}/`)) { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | const COVERAGE_INSTRUMENTER = createInstrumenter({ |
| 226 | esModules: true, |
| 227 | parserPlugins: [ |
| 228 | ...defaultIstanbulSchema.instrumenter.properties.parserPlugins.default, |
| 229 | 'topLevelAwait', |
| 230 | ], |
| 231 | }); |
| 232 | |
| 233 | const instrumentedSourceCacheForFilePaths = new Map(); |
| 234 | |
| 235 | const SHOULD_GATHER_COVERAGE_INFORMATION = process.env.COVERAGE === '1'; |
| 236 | |
| 237 | /** |
| 238 | * @param {http.IncomingMessage} request |
| 239 | * @param {http.ServerResponse} response |
| 240 | */ |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 241 | async function requestHandler(request, response) { |
| 242 | const filePath = parseURL(request.url).pathname; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 243 | if (filePath === '/favicon.ico') { |
| 244 | send404(response, '404, no favicon'); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | if (filePath === '/' || filePath === '/index.html') { |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 249 | const components = |
| 250 | await fs.promises.readdir(path.join(componentDocsBaseFolder, 'front_end', 'ui', 'components', 'docs')); |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 251 | const html = createServerIndexFile(components.filter(filePath => { |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 252 | const stats = fs.lstatSync(path.join(componentDocsBaseFolder, 'front_end', 'ui', 'components', 'docs', filePath)); |
Jack Franklin | b599716 | 2020-11-25 17:28:51 +0000 | [diff] [blame] | 253 | // Filter out some build config files (tsconfig, d.ts, etc), and just list the directories. |
| 254 | return stats.isDirectory(); |
| 255 | })); |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 256 | respondWithHtml(response, html); |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 257 | } else if (filePath.startsWith('/front_end/ui/components/docs') && path.extname(filePath) === '') { |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 258 | // This means it's a component path like /breadcrumbs. |
| 259 | const componentHtml = await getExamplesForPath(filePath); |
| 260 | respondWithHtml(response, componentHtml); |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 261 | return; |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 262 | } else if (/ui\/components\/docs\/(.+)\/(.+)\.html/.test(filePath)) { |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 263 | /** This conditional checks if we are viewing an individual example's HTML |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 264 | * file. e.g. localhost:8090/front_end/ui/components/docs/data_grid/basic.html For each |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 265 | * example we inject themeColors.css into the page so all CSS variables |
| 266 | * that components use are available. |
| 267 | */ |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 268 | |
Jack Franklin | 0155f7d | 2021-03-02 09:11:22 +0000 | [diff] [blame] | 269 | /** |
| 270 | * We also let the user provide a different base path for any shared |
| 271 | * resources that we load. But if this is provided along with the |
| 272 | * componentDocsBaseArg, and the two are the same, we don't want to use the |
| 273 | * shared resources base, as it's part of the componentDocsBaseArg and |
| 274 | * therefore the URL is already correct. |
| 275 | * |
| 276 | * If we didn't get a componentDocsBaseArg or we did and it's different to |
| 277 | * the sharedResourcesBase, we use sharedResourcesBase. |
| 278 | */ |
| 279 | const baseUrlForSharedResource = |
| 280 | componentDocsBaseArg && componentDocsBaseArg.endsWith(sharedResourcesBase) ? '/' : `/${sharedResourcesBase}`; |
| 281 | const fileContents = await fs.promises.readFile(path.join(componentDocsBaseFolder, filePath), {encoding: 'utf8'}); |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 282 | const themeColoursLink = `<link rel="stylesheet" href="${ |
Tim van der Lippe | e658313 | 2021-04-08 10:10:57 +0100 | [diff] [blame] | 283 | path.join(baseUrlForSharedResource, 'front_end', 'ui', 'legacy', 'themeColors.css')}" type="text/css" />`; |
Jack Franklin | 343c141 | 2021-02-26 15:08:10 +0000 | [diff] [blame] | 284 | const inspectorCommonLink = `<link rel="stylesheet" href="${ |
Tim van der Lippe | e658313 | 2021-04-08 10:10:57 +0100 | [diff] [blame] | 285 | path.join(baseUrlForSharedResource, 'front_end', 'ui', 'legacy', 'inspectorCommon.css')}" type="text/css" />`; |
Jack Franklin | 8742dc8 | 2021-02-26 09:17:59 +0000 | [diff] [blame] | 286 | const toggleDarkModeScript = `<script type="module" src="${ |
Tim van der Lippe | e622f55 | 2021-04-14 15:15:18 +0100 | [diff] [blame] | 287 | path.join(baseUrlForSharedResource, 'front_end', 'ui', 'components', 'docs', 'component_docs.js')}"></script>`; |
Jack Franklin | 0f019a0 | 2021-04-09 08:50:29 +0000 | [diff] [blame] | 288 | const newFileContents = fileContents.replace('</head>', `${themeColoursLink}\n${inspectorCommonLink}\n</head>`) |
| 289 | .replace('</body>', toggleDarkModeScript + '\n</body>'); |
Jack Franklin | 3642900 | 2020-11-25 15:07:26 +0000 | [diff] [blame] | 290 | respondWithHtml(response, newFileContents); |
| 291 | |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 292 | } else { |
Jack Franklin | 9d4ecf7 | 2020-09-16 14:04:30 +0100 | [diff] [blame] | 293 | // This means it's an asset like a JS file or an image. |
Jack Franklin | 49e33f9 | 2021-03-31 10:30:56 +0000 | [diff] [blame] | 294 | let fullPath = path.join(componentDocsBaseFolder, filePath); |
Jack Franklin | d034512 | 2020-12-21 09:12:04 +0000 | [diff] [blame] | 295 | if (fullPath.endsWith(path.join('locales', 'en-US.json'))) { |
| 296 | // Rewrite this path so we can load up the locale in the component-docs |
Tim van der Lippe | bb352e6 | 2021-04-01 18:57:28 +0100 | [diff] [blame] | 297 | fullPath = path.join(componentDocsBaseFolder, 'front_end', 'core', 'i18n', 'locales', 'en-US.json'); |
Jack Franklin | d034512 | 2020-12-21 09:12:04 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Jack Franklin | 90b6613 | 2021-01-05 11:33:43 +0000 | [diff] [blame] | 300 | if (!fullPath.startsWith(devtoolsRootFolder) && !fileIsInTestFolder) { |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 301 | console.error(`Path ${fullPath} is outside the DevTools Frontend root dir.`); |
| 302 | process.exit(1); |
| 303 | } |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 304 | |
Jack Franklin | 279564e | 2020-07-06 15:25:18 +0100 | [diff] [blame] | 305 | const fileExists = await checkFileExists(fullPath); |
| 306 | |
| 307 | if (!fileExists) { |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 308 | send404(response, '404, File not found'); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | let encoding = 'utf8'; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 313 | if (fullPath.endsWith('.js')) { |
| 314 | response.setHeader('Content-Type', 'text/javascript; charset=utf-8'); |
| 315 | } else if (fullPath.endsWith('.css')) { |
| 316 | response.setHeader('Content-Type', 'text/css; charset=utf-8'); |
| 317 | } else if (fullPath.endsWith('.wasm')) { |
| 318 | response.setHeader('Content-Type', 'application/wasm'); |
| 319 | encoding = 'binary'; |
| 320 | } else if (fullPath.endsWith('.svg')) { |
| 321 | response.setHeader('Content-Type', 'image/svg+xml; charset=utf-8'); |
| 322 | } else if (fullPath.endsWith('.png')) { |
| 323 | response.setHeader('Content-Type', 'image/png'); |
| 324 | encoding = 'binary'; |
| 325 | } else if (fullPath.endsWith('.jpg')) { |
| 326 | response.setHeader('Content-Type', 'image/jpg'); |
| 327 | encoding = 'binary'; |
Mathias Bynens | c38abd4 | 2020-12-24 08:20:05 +0100 | [diff] [blame] | 328 | } else if (fullPath.endsWith('.avif')) { |
| 329 | response.setHeader('Content-Type', 'image/avif'); |
| 330 | encoding = 'binary'; |
Paul Lewis | d689287 | 2021-04-01 15:56:46 +0100 | [diff] [blame] | 331 | } else if (fullPath.endsWith('.gz')) { |
| 332 | response.setHeader('Content-Type', 'application/gzip'); |
| 333 | encoding = 'binary'; |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Tim van der Lippe | 23283e1 | 2021-05-10 15:08:31 +0100 | [diff] [blame] | 336 | let fileContents = await fs.promises.readFile(fullPath, encoding); |
| 337 | const isComputingCoverageRequest = request.headers['devtools-compute-coverage'] === '1'; |
| 338 | |
| 339 | if (SHOULD_GATHER_COVERAGE_INFORMATION && fullPath.endsWith('.js') && filePath.startsWith('/front_end/') && |
| 340 | isIncludedForCoverageComputation(filePath)) { |
| 341 | const previouslyGeneratedInstrumentedSource = instrumentedSourceCacheForFilePaths.get(fullPath); |
| 342 | |
| 343 | if (previouslyGeneratedInstrumentedSource) { |
| 344 | fileContents = previouslyGeneratedInstrumentedSource; |
| 345 | } else { |
| 346 | if (!isComputingCoverageRequest) { |
| 347 | response.writeHead(400); |
| 348 | response.write(`Invalid coverage request. Attempted to load ${request.url}.`, 'utf8'); |
| 349 | response.end(); |
| 350 | |
| 351 | console.error( |
| 352 | `Invalid coverage request. Attempted to load ${request.url} which was not available in the ` + |
| 353 | 'code coverage instrumentation cache. Make sure that you call `preloadForCodeCoverage` in the describe block ' + |
| 354 | 'of your interactions test, before declaring any tests.\n'); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | fileContents = await new Promise(async (resolve, reject) => { |
| 359 | let sourceMap = convertSourceMap.fromSource(fileContents); |
| 360 | if (!sourceMap) { |
| 361 | sourceMap = convertSourceMap.fromMapFileSource(fileContents, path.dirname(fullPath)); |
| 362 | } |
| 363 | |
| 364 | COVERAGE_INSTRUMENTER.instrument(fileContents, fullPath, (error, instrumentedSource) => { |
| 365 | if (error) { |
| 366 | reject(error); |
| 367 | } else { |
| 368 | resolve(instrumentedSource); |
| 369 | } |
| 370 | }, sourceMap ? sourceMap.sourcemap : undefined); |
| 371 | }); |
| 372 | |
| 373 | instrumentedSourceCacheForFilePaths.set(fullPath, fileContents); |
| 374 | } |
| 375 | } |
| 376 | |
Jack Franklin | 1557a1c | 2020-06-08 15:22:13 +0100 | [diff] [blame] | 377 | response.writeHead(200); |
| 378 | response.write(fileContents, encoding); |
| 379 | response.end(); |
| 380 | } |
| 381 | } |