1
1
import path from 'node:path'
2
2
import fsp from 'node:fs/promises'
3
- import type { ServerResponse } from 'node:http'
4
3
import type { Connect } from 'dep-types/connect'
5
4
import colors from 'picocolors'
6
5
import type { ExistingRawSourceMap } from 'rolldown'
@@ -34,6 +33,7 @@ import {
34
33
ERR_OUTDATED_OPTIMIZED_DEP ,
35
34
NULL_BYTE_PLACEHOLDER ,
36
35
} from '../../../shared/constants'
36
+ import type { ResolvedConfig } from '../../config'
37
37
import { checkLoadingAccess , respondWithAccessDenied } from './static'
38
38
39
39
const debugCache = createDebugger ( 'vite:cache' )
@@ -46,23 +46,9 @@ const rawRE = /[?&]raw\b/
46
46
const inlineRE = / [ ? & ] i n l i n e \b /
47
47
const svgRE = / \. s v g \b /
48
48
49
- function deniedServingAccessForTransform (
50
- id : string ,
51
- server : ViteDevServer ,
52
- res : ServerResponse ,
53
- next : Connect . NextFunction ,
54
- ) {
49
+ function isServerAccessDeniedForTransform ( config : ResolvedConfig , id : string ) {
55
50
if ( rawRE . test ( id ) || urlRE . test ( id ) || inlineRE . test ( id ) || svgRE . test ( id ) ) {
56
- const servingAccessResult = checkLoadingAccess ( server . config , id )
57
- if ( servingAccessResult === 'denied' ) {
58
- respondWithAccessDenied ( id , server , res )
59
- return true
60
- }
61
- if ( servingAccessResult === 'fallback' ) {
62
- next ( )
63
- return true
64
- }
65
- servingAccessResult satisfies 'allowed'
51
+ return checkLoadingAccess ( config , id ) !== 'allowed'
66
52
}
67
53
return false
68
54
}
@@ -243,7 +229,7 @@ export function transformMiddleware(
243
229
allowId ( id ) {
244
230
return (
245
231
id . startsWith ( '\0' ) ||
246
- ! deniedServingAccessForTransform ( id , server , res , next )
232
+ ! isServerAccessDeniedForTransform ( server . config , id )
247
233
)
248
234
} ,
249
235
} )
@@ -317,8 +303,18 @@ export function transformMiddleware(
317
303
return next ( )
318
304
}
319
305
if ( e ?. code === ERR_DENIED_ID ) {
320
- // next() is called in ensureServingAccess
321
- return
306
+ const id : string = e . id
307
+ const servingAccessResult = checkLoadingAccess ( server . config , id )
308
+ if ( servingAccessResult === 'denied' ) {
309
+ respondWithAccessDenied ( id , server , res )
310
+ return true
311
+ }
312
+ if ( servingAccessResult === 'fallback' ) {
313
+ next ( )
314
+ return true
315
+ }
316
+ servingAccessResult satisfies 'allowed'
317
+ throw new Error ( `Unexpected access result for id ${ id } ` )
322
318
}
323
319
return next ( e )
324
320
}
0 commit comments