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
45export 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+ }
0 commit comments