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

Function resolveOrchestrator

packages/browser/src/node/serverOrchestrator.ts:7–103  ·  view source on GitHub ↗
(
  globalServer: ParentBrowserProject,
  url: URL,
  res: ServerResponse<IncomingMessage>,
)

Source from the content-addressed store, hash-verified

5import { replacer } from './utils'
6
7export async function resolveOrchestrator(
8 globalServer: ParentBrowserProject,
9 url: URL,
10 res: ServerResponse<IncomingMessage>,
11): Promise<string | undefined> {
12 let sessionId = url.searchParams.get('sessionId')
13 // it's possible to open the page without a context
14 if (!sessionId) {
15 const contexts = [...globalServer.children].flatMap(p => [...p.state.orchestrators.keys()])
16 sessionId = contexts.at(-1) ?? 'none'
17 }
18
19 // it's ok to not have a session here, especially in the preview provider
20 // because the user could refresh the page which would remove the session id from the url
21
22 const session = globalServer.vitest._browserSessions.getSession(sessionId!)
23 const browserProject = (session?.project.browser as ProjectBrowser | undefined) || [...globalServer.children][0]
24
25 if (!browserProject) {
26 return
27 }
28
29 // ignore unknown pages
30 if (sessionId && sessionId !== 'none' && !globalServer.vitest._browserSessions.sessionIds.has(sessionId)) {
31 return
32 }
33
34 const injectorJs = typeof globalServer.injectorJs === 'string'
35 ? globalServer.injectorJs
36 : await globalServer.injectorJs
37
38 const injector = replacer(injectorJs, {
39 __VITEST_PROVIDER__: JSON.stringify(browserProject.config.browser.provider?.name || 'preview'),
40 __VITEST_CONFIG__: JSON.stringify(browserProject.wrapSerializedConfig()),
41 __VITEST_VITE_CONFIG__: JSON.stringify({
42 root: browserProject.vite.config.root,
43 }),
44 __VITEST_METHOD__: JSON.stringify('orchestrate'),
45 __VITEST_TYPE__: '"orchestrator"',
46 __VITEST_SESSION_ID__: JSON.stringify(sessionId),
47 __VITEST_TESTER_ID__: '"none"',
48 __VITEST_OTEL_CARRIER__: JSON.stringify(session?.otelCarrier ?? null),
49 __VITEST_PROVIDED_CONTEXT__: JSON.stringify(stringify(browserProject.project.getProvidedContext())),
50 __VITEST_API_TOKEN__: JSON.stringify(globalServer.vitest.config.api.token),
51 })
52
53 // disable CSP for the orchestrator as we are the ones controlling it
54 res.removeHeader('Content-Security-Policy')
55
56 if (!globalServer.orchestratorScripts) {
57 globalServer.orchestratorScripts = (await globalServer.formatScripts(
58 globalServer.config.browser.orchestratorScripts,
59 )).map((script) => {
60 let html = '<script '
61 for (const attr in script.attrs || {}) {
62 html += `${attr}="${script.attrs![attr]}" `
63 }
64 html += `>${script.children}</script>`

Callers 1

Calls 10

replacerFunction · 0.90
stringifyFunction · 0.85
keysMethod · 0.80
atMethod · 0.80
getSessionMethod · 0.80
wrapSerializedConfigMethod · 0.80
formatScriptsMethod · 0.80
getMethod · 0.45
hasMethod · 0.45
getProvidedContextMethod · 0.45

Tested by

no test coverage detected