|
| 1 | +import _ from 'lodash' |
| 2 | +import type { Response } from 'express' |
| 3 | +import send from 'send' |
| 4 | +import os from 'os' |
| 5 | +import { fs } from '../util/fs' |
| 6 | +import path from 'path' |
| 7 | +import Debug from 'debug' |
| 8 | +import pkg from '@packages/root' |
| 9 | +import { getPathToDist, getPathToIndex, RunnerPkg } from '@packages/resolve-dist' |
| 10 | +import type { InitializeRoutes } from '../routes' |
| 11 | +import type { PlatformName } from '@packages/launcher' |
| 12 | +import type { Cfg } from '../project-base' |
| 13 | + |
| 14 | +const debug = Debug('cypress:server:runner') |
| 15 | + |
| 16 | +const PATH_TO_NON_PROXIED_ERROR = path.join(__dirname, '..', 'html', 'non_proxied_error.html') |
| 17 | + |
| 18 | +const _serveNonProxiedError = (res: Response) => { |
| 19 | + return fs.readFile(PATH_TO_NON_PROXIED_ERROR) |
| 20 | + .then((html) => { |
| 21 | + return res.type('html').end(html) |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +export interface ServeOptions extends Pick<InitializeRoutes, 'getSpec' | 'config' | 'getCurrentBrowser' | 'getRemoteState' | 'specsStore'> { |
| 26 | + testingType: Cypress.TestingType |
| 27 | +} |
| 28 | + |
| 29 | +export const serveRunner = (runnerPkg: RunnerPkg, config: Cfg, res: Response) => { |
| 30 | + // base64 before embedding so user-supplied contents can't break out of <script> |
| 31 | + // https://github.com/cypress-io/cypress/issues/4952 |
| 32 | + const base64Config = Buffer.from(JSON.stringify(config)).toString('base64') |
| 33 | + |
| 34 | + const runnerPath = process.env.CYPRESS_INTERNAL_RUNNER_PATH || getPathToIndex(runnerPkg) |
| 35 | + |
| 36 | + return res.render(runnerPath, { |
| 37 | + base64Config, |
| 38 | + projectName: config.projectName, |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +export const runner = { |
| 43 | + serve (req, res, runnerPkg: RunnerPkg, options: ServeOptions) { |
| 44 | + if (req.proxiedUrl.startsWith('/')) { |
| 45 | + debug('request was not proxied via Cypress, erroring %o', _.pick(req, 'proxiedUrl')) |
| 46 | + |
| 47 | + return _serveNonProxiedError(res) |
| 48 | + } |
| 49 | + |
| 50 | + let { config, getRemoteState, getCurrentBrowser, getSpec, specsStore } = options |
| 51 | + |
| 52 | + config = _.clone(config) |
| 53 | + // at any given point, rather than just arbitrarily modifying it. |
| 54 | + // @ts-ignore |
| 55 | + config.testingType = options.testingType |
| 56 | + |
| 57 | + // TODO #1: bug. Passing `remote.domainName` breaks CT for unknown reasons. |
| 58 | + // If you pass a remote object with a domainName key, we get cross-origin |
| 59 | + // iframe access errors. |
| 60 | + // repro: |
| 61 | + // { |
| 62 | + // "domainName": "localhost" |
| 63 | + // } |
| 64 | + // TODO: Find out what the problem. |
| 65 | + if (options.testingType === 'e2e') { |
| 66 | + config.remote = getRemoteState() |
| 67 | + } |
| 68 | + |
| 69 | + config.version = pkg.version |
| 70 | + config.platform = os.platform() as PlatformName |
| 71 | + config.arch = os.arch() |
| 72 | + config.spec = getSpec() ?? undefined |
| 73 | + config.specs = specsStore.specFiles |
| 74 | + config.browser = getCurrentBrowser() |
| 75 | + |
| 76 | + debug('serving runner index.html with config %o', |
| 77 | + _.pick(config, 'version', 'platform', 'arch', 'projectName')) |
| 78 | + |
| 79 | + // log the env object's keys without values to avoid leaking sensitive info |
| 80 | + debug('env object has the following keys: %s', _.keys(config.env).join(', ')) |
| 81 | + |
| 82 | + return serveRunner(runnerPkg, config, res) |
| 83 | + }, |
| 84 | + |
| 85 | + handle (testingType, req, res) { |
| 86 | + const pathToFile = getPathToDist(testingType === 'e2e' ? 'runner' : 'runner-ct', req.params[0]) |
| 87 | + |
| 88 | + return send(req, pathToFile) |
| 89 | + .pipe(res) |
| 90 | + }, |
| 91 | +} |
0 commit comments