MCPcopy
hub / github.com/vitest-dev/vitest / handler

Function handler

packages/ui/node/index.ts:26–117  ·  view source on GitHub ↗
(server)

Source from the content-addressed store, hash-verified

24 configureServer: {
25 order: 'post',
26 handler(server) {
27 const uiOptions = ctx.config
28 const base = uiOptions.uiBase
29
30 // Serve coverage HTML at ./coverage if configured
31 const coverageHtmlDir = ctx.config.coverage?.htmlDir
32 if (coverageHtmlDir) {
33 server.middlewares.use(
34 join(base, 'coverage'),
35 sirv(coverageHtmlDir, {
36 single: true,
37 dev: true,
38 setHeaders: (res) => {
39 res.setHeader(
40 'Cache-Control',
41 'public,max-age=0,must-revalidate',
42 )
43 },
44 }),
45 )
46 }
47
48 const clientDist = resolve(fileURLToPath(import.meta.url), '../client')
49 const clientIndexHtml = fs.readFileSync(resolve(clientDist, 'index.html'), 'utf-8')
50
51 // eslint-disable-next-line prefer-arrow-callback
52 server.middlewares.use(function vitestAttachment(req, res, next) {
53 if (!req.url) {
54 return next()
55 }
56
57 const url = new URL(req.url, 'http://localhost')
58 if (url.pathname === '/__vitest_attachment__') {
59 const path = url.searchParams.get('path')
60 const contentType = url.searchParams.get('contentType')
61
62 // ignore invalid requests
63 if (!isValidApiRequest(ctx.config, req) || !contentType || !path) {
64 return next()
65 }
66
67 const fsPath = decodeURIComponent(path)
68
69 if (!isFileServingAllowed(ctx.vite.config, fsPath)) {
70 return next()
71 }
72
73 try {
74 res.writeHead(200, {
75 'content-type': contentType,
76 })
77 fs.createReadStream(fsPath)
78 .pipe(res)
79 .on('close', () => res.end())
80 }
81 catch (err) {
82 next(err)
83 }

Callers 4

onLogFunction · 0.50
createSuiteFunction · 0.50
createTaskCollectorFunction · 0.50
createTestHookFunction · 0.50

Calls 9

isFileServingAllowedFunction · 0.90
isValidApiRequestFunction · 0.85
endMethod · 0.80
joinFunction · 0.50
resolveFunction · 0.50
nextFunction · 0.50
getMethod · 0.45
onMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected