11import type { BrowserCommands } from 'vitest/browser'
2- import type { BrowserCommand , TestProject } from 'vitest/node'
2+ import type { BrowserCommand } from 'vitest/node'
33import fs , { promises as fsp } from 'node:fs'
44import { basename , dirname , resolve } from 'node:path'
55import mime from 'mime/lite'
6- import { isFileLoadingAllowed } from 'vitest/node'
7- import { slash } from '../utils'
8-
9- function assertFileAccess ( path : string , project : TestProject ) {
10- if (
11- ! isFileLoadingAllowed ( project . vite . config , path )
12- && ! isFileLoadingAllowed ( project . vitest . vite . config , path )
13- ) {
14- throw new Error (
15- `Access denied to "${ path } ". See Vite config documentation for "server.fs": https://vitejs.dev/config/server-options.html#server-fs-strict.` ,
16- )
17- }
18- }
19-
20- function assertWrite ( path : string , project : TestProject ) {
21- if ( ! project . config . browser . api . allowWrite || ! project . vitest . config . api . allowWrite ) {
22- throw new Error ( `Cannot modify file "${ path } ". File writing is disabled because server is exposed to the internet, see https://vitest.dev/config/browser/api.` )
23- }
24- }
6+ import { assertBrowserApiWrite , assertBrowserFileAccess } from '../utils'
257
268export const readFile : BrowserCommand <
279 Parameters < BrowserCommands [ 'readFile' ] >
2810> = async ( { project } , path , options = { } ) => {
2911 const filepath = resolve ( project . config . root , path )
30- assertFileAccess ( slash ( filepath ) , project )
12+ assertBrowserFileAccess ( project , filepath )
3113 // never return a Buffer
3214 if ( typeof options === 'object' && ! options . encoding ) {
3315 options . encoding = 'utf-8'
@@ -38,9 +20,9 @@ export const readFile: BrowserCommand<
3820export const writeFile : BrowserCommand <
3921 Parameters < BrowserCommands [ 'writeFile' ] >
4022> = async ( { project } , path , data , options ) => {
41- assertWrite ( path , project )
23+ assertBrowserApiWrite ( project , path )
4224 const filepath = resolve ( project . config . root , path )
43- assertFileAccess ( slash ( filepath ) , project )
25+ assertBrowserFileAccess ( project , filepath )
4426 const dir = dirname ( filepath )
4527 if ( ! fs . existsSync ( dir ) ) {
4628 await fsp . mkdir ( dir , { recursive : true } )
@@ -51,15 +33,15 @@ export const writeFile: BrowserCommand<
5133export const removeFile : BrowserCommand <
5234 Parameters < BrowserCommands [ 'removeFile' ] >
5335> = async ( { project } , path ) => {
54- assertWrite ( path , project )
36+ assertBrowserApiWrite ( project , path )
5537 const filepath = resolve ( project . config . root , path )
56- assertFileAccess ( slash ( filepath ) , project )
38+ assertBrowserFileAccess ( project , filepath )
5739 await fsp . rm ( filepath )
5840}
5941
6042export const _fileInfo : BrowserCommand < [ path : string , encoding : BufferEncoding ] > = async ( { project } , path , encoding ) => {
6143 const filepath = resolve ( project . config . root , path )
62- assertFileAccess ( slash ( filepath ) , project )
44+ assertBrowserFileAccess ( project , filepath )
6345 const content = await fsp . readFile ( filepath , encoding || 'base64' )
6446 return {
6547 content,
0 commit comments