File tree 4 files changed +23
-5
lines changed
4 files changed +23
-5
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import fs from 'node:fs'
2
2
import path from 'node:path'
3
3
import type { ResolvedConfig } from './config'
4
4
import {
5
+ ERR_SYMLINK_IN_RECURSIVE_READDIR ,
5
6
cleanUrl ,
6
7
normalizePath ,
7
8
recursiveReaddir ,
@@ -12,8 +13,16 @@ const publicFilesMap = new WeakMap<ResolvedConfig, Set<string>>()
12
13
13
14
export async function initPublicFiles (
14
15
config : ResolvedConfig ,
15
- ) : Promise < Set < string > > {
16
- const fileNames = await recursiveReaddir ( config . publicDir )
16
+ ) : Promise < Set < string > | undefined > {
17
+ let fileNames : string [ ]
18
+ try {
19
+ fileNames = await recursiveReaddir ( config . publicDir )
20
+ } catch ( e ) {
21
+ if ( e . code === ERR_SYMLINK_IN_RECURSIVE_READDIR ) {
22
+ return
23
+ }
24
+ throw e
25
+ }
17
26
const publicFiles = new Set (
18
27
fileNames . map ( ( fileName ) => fileName . slice ( config . publicDir . length ) ) ,
19
28
)
Original file line number Diff line number Diff line change @@ -645,7 +645,7 @@ export async function _createServer(
645
645
file = normalizePath ( file )
646
646
await container . watchChange ( file , { event : isUnlink ? 'delete' : 'create' } )
647
647
648
- if ( config . publicDir && file . startsWith ( config . publicDir ) ) {
648
+ if ( publicFiles && config . publicDir && file . startsWith ( config . publicDir ) ) {
649
649
publicFiles [ isUnlink ? 'delete' : 'add' ] (
650
650
file . slice ( config . publicDir . length ) ,
651
651
)
Original file line number Diff line number Diff line change @@ -54,7 +54,7 @@ const sirvOptions = ({
54
54
55
55
export function servePublicMiddleware (
56
56
server : ViteDevServer ,
57
- publicFiles : Set < string > ,
57
+ publicFiles ? : Set < string > ,
58
58
) : Connect . NextHandleFunction {
59
59
const dir = server . config . publicDir
60
60
const serve = sirv (
@@ -82,7 +82,7 @@ export function servePublicMiddleware(
82
82
// in-memory set of known public files. This set is updated on restarts.
83
83
// also skip import request and internal requests `/@fs/ /@vite-client` etc...
84
84
if (
85
- ! publicFiles . has ( toFilePath ( req . url ! ) ) ||
85
+ ( publicFiles && ! publicFiles . has ( toFilePath ( req . url ! ) ) ) ||
86
86
isImportRequest ( req . url ! ) ||
87
87
isInternalRequest ( req . url ! )
88
88
) {
Original file line number Diff line number Diff line change @@ -623,6 +623,8 @@ export function copyDir(srcDir: string, destDir: string): void {
623
623
}
624
624
}
625
625
626
+ export const ERR_SYMLINK_IN_RECURSIVE_READDIR =
627
+ 'ERR_SYMLINK_IN_RECURSIVE_READDIR'
626
628
export async function recursiveReaddir ( dir : string ) : Promise < string [ ] > {
627
629
if ( ! fs . existsSync ( dir ) ) {
628
630
return [ ]
@@ -637,6 +639,13 @@ export async function recursiveReaddir(dir: string): Promise<string[]> {
637
639
}
638
640
throw e
639
641
}
642
+ if ( dirents . some ( ( dirent ) => dirent . isSymbolicLink ( ) ) ) {
643
+ const err : any = new Error (
644
+ 'Symbolic links are not supported in recursiveReaddir' ,
645
+ )
646
+ err . code = ERR_SYMLINK_IN_RECURSIVE_READDIR
647
+ throw err
648
+ }
640
649
const files = await Promise . all (
641
650
dirents . map ( ( dirent ) => {
642
651
const res = path . resolve ( dir , dirent . name )
You can’t perform that action at this time.
0 commit comments