|
| 1 | +// TODO: move this to packages/core-file-server |
| 2 | +import _ from 'lodash' |
| 3 | +import debugModule from 'debug' |
| 4 | +import url from 'url' |
| 5 | +import http from 'http' |
| 6 | +import path from 'path' |
| 7 | +import send from 'send' |
| 8 | +import { httpUtils } from '@packages/network' |
| 9 | +import { allowDestroy } from './util/server_destroy' |
| 10 | +import { id as randomId } from './util/random' |
| 11 | +import networkFailures from './util/network_failures' |
| 12 | +import type { AddressInfo } from 'net' |
| 13 | + |
| 14 | +const debug = debugModule('cypress:server:file_server') |
| 15 | + |
| 16 | +export type FileServer = { |
| 17 | + token: string |
| 18 | + port: () => number |
| 19 | + address: () => string |
| 20 | + close: () => void |
| 21 | +} |
| 22 | + |
| 23 | +const onRequest = function (req: http.IncomingMessage, res: http.ServerResponse, expectedToken: string, fileServerFolder: string) { |
| 24 | + const token = req.headers['x-cypress-authorization'] |
| 25 | + |
| 26 | + if (token !== expectedToken) { |
| 27 | + debug('authorization failed on file_server request %o', { reqUrl: req.url, expectedToken, token }) |
| 28 | + res.statusCode = 401 |
| 29 | + res.end() |
| 30 | + |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + const args = _.compact([ |
| 35 | + fileServerFolder, |
| 36 | + req.url, |
| 37 | + ]) |
| 38 | + |
| 39 | + // strip off any query params from our req's url |
| 40 | + // since we're pulling this from the file system |
| 41 | + // it does not understand query params |
| 42 | + // and make sure we decode the uri which swaps out |
| 43 | + // %20 with white space |
| 44 | + const file = decodeURI(url.parse(path.join(...args)).pathname as string) |
| 45 | + |
| 46 | + res.setHeader('x-cypress-file-path', encodeURI(file)) |
| 47 | + |
| 48 | + return send(req, url.parse(req.url as string).pathname as string, { |
| 49 | + root: path.resolve(fileServerFolder), |
| 50 | + }) |
| 51 | + .on('error', (err) => { |
| 52 | + res.setHeader('x-cypress-file-server-error', 'true') |
| 53 | + res.setHeader('content-type', 'text/html') |
| 54 | + res.statusCode = err.status |
| 55 | + |
| 56 | + return res.end(networkFailures.get(file, err.status)) |
| 57 | + }).pipe(res) |
| 58 | +} |
| 59 | + |
| 60 | +export const create = (fileServerFolder: string): Promise<FileServer> => { |
| 61 | + return new Promise(((resolve) => { |
| 62 | + const token = randomId(64) |
| 63 | + |
| 64 | + const srv = http.createServer(httpUtils.lenientOptions, (req: http.IncomingMessage, res: http.ServerResponse) => { |
| 65 | + return onRequest(req, res, token, fileServerFolder) |
| 66 | + }) |
| 67 | + |
| 68 | + allowDestroy(srv) |
| 69 | + |
| 70 | + return srv.listen(0, '127.0.0.1', () => { |
| 71 | + const server: FileServer = { |
| 72 | + token, |
| 73 | + port () { |
| 74 | + return (srv.address() as AddressInfo).port |
| 75 | + }, |
| 76 | + address () { |
| 77 | + return `http://localhost:${this.port()}` |
| 78 | + }, |
| 79 | + close () { |
| 80 | + // @ts-expect-error - destroyAsync is defined when allowDestroy is called |
| 81 | + return srv.destroyAsync() |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + return resolve(server) |
| 86 | + }) |
| 87 | + })) |
| 88 | +} |
0 commit comments