Skip to content

Commit f17eb42

Browse files
authored
refactor: avoid using isFileServingAllowed from Vite (#9160)
1 parent 78cfbf9 commit f17eb42

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

packages/vitest/src/node/vite.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { InlineConfig, ViteDevServer } from 'vite'
2-
import { createServer } from 'vite'
1+
import type { InlineConfig, ResolvedConfig, ViteDevServer } from 'vite'
2+
import { cleanUrl } from '@vitest/utils/helpers'
3+
import { createServer, isFileLoadingAllowed, normalizePath } from 'vite'
34

45
export async function createViteServer(inlineConfig: InlineConfig): Promise<ViteDevServer> {
56
// Vite prints an error (https://github.com/vitejs/vite/issues/14328)
@@ -20,3 +21,48 @@ export async function createViteServer(inlineConfig: InlineConfig): Promise<Vite
2021
console.error = error
2122
return server
2223
}
24+
25+
// backward compat implementation
26+
/**
27+
* Check if the url is allowed to be served, via the `server.fs` config.
28+
* @deprecated Use the `isFileLoadingAllowed` function instead.
29+
*/
30+
export function isFileServingAllowed(
31+
config: ResolvedConfig,
32+
url: string,
33+
): boolean
34+
export function isFileServingAllowed(
35+
url: string,
36+
server: ViteDevServer,
37+
): boolean
38+
export function isFileServingAllowed(
39+
configOrUrl: ResolvedConfig | string,
40+
urlOrServer: string | ViteDevServer,
41+
): boolean {
42+
const config = (
43+
typeof urlOrServer === 'string' ? configOrUrl : urlOrServer.config
44+
) as ResolvedConfig
45+
const url = (
46+
typeof urlOrServer === 'string' ? urlOrServer : configOrUrl
47+
) as string
48+
49+
if (!config.server.fs.strict) {
50+
return true
51+
}
52+
const filePath = fsPathFromUrl(url)
53+
return isFileLoadingAllowed(config, filePath)
54+
}
55+
56+
const FS_PREFIX = '/@fs/'
57+
const VOLUME_RE = /^[A-Z]:/i
58+
59+
function fsPathFromId(id: string): string {
60+
const fsPath = normalizePath(
61+
id.startsWith(FS_PREFIX) ? id.slice(FS_PREFIX.length) : id,
62+
)
63+
return fsPath[0] === '/' || VOLUME_RE.test(fsPath) ? fsPath : `/${fsPath}`
64+
}
65+
66+
function fsPathFromUrl(url: string): string {
67+
return fsPathFromId(cleanUrl(url))
68+
}

packages/vitest/src/public/node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export type {
135135
export type { VitestPluginContext } from '../node/types/plugin'
136136
export type { TestRunResult } from '../node/types/tests'
137137
export type { WorkerContext } from '../node/types/worker'
138+
export { isFileServingAllowed } from '../node/vite'
138139
export { createViteLogger } from '../node/viteLogger'
139140
export type { WatcherTriggerPattern } from '../node/watcher'
140141

@@ -170,7 +171,7 @@ export type { SerializedError } from '@vitest/utils'
170171
export {
171172
esbuildVersion,
172173
isCSSRequest,
173-
isFileServingAllowed,
174+
isFileLoadingAllowed,
174175
parseAst,
175176
parseAstAsync,
176177
rollupVersion,

test/core/test/exports.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ it('exports snapshot', async ({ skip, task }) => {
120120
"generateFileHash": "function",
121121
"getFilePoolName": "function",
122122
"isCSSRequest": "function",
123+
"isFileLoadingAllowed": "function",
123124
"isFileServingAllowed": "function",
124125
"isValidApiRequest": "function",
125126
"parseAst": "function",
@@ -285,6 +286,7 @@ it('exports snapshot', async ({ skip, task }) => {
285286
"generateFileHash": "function",
286287
"getFilePoolName": "function",
287288
"isCSSRequest": "function",
289+
"isFileLoadingAllowed": "function",
288290
"isFileServingAllowed": "function",
289291
"isValidApiRequest": "function",
290292
"parseAst": "function",

0 commit comments

Comments
 (0)