11import path from 'node:path'
22import fsp from 'node:fs/promises'
3- import type { ServerResponse } from 'node:http'
43import type { Connect } from 'dep-types/connect'
54import colors from 'picocolors'
65import type { ExistingRawSourceMap } from 'rolldown'
@@ -34,6 +33,7 @@ import {
3433 ERR_OUTDATED_OPTIMIZED_DEP ,
3534 NULL_BYTE_PLACEHOLDER ,
3635} from '../../../shared/constants'
36+ import type { ResolvedConfig } from '../../config'
3737import { checkLoadingAccess , respondWithAccessDenied } from './static'
3838
3939const debugCache = createDebugger ( 'vite:cache' )
@@ -46,23 +46,9 @@ const rawRE = /[?&]raw\b/
4646const inlineRE = / [ ? & ] i n l i n e \b /
4747const svgRE = / \. s v g \b /
4848
49- function deniedServingAccessForTransform (
50- id : string ,
51- server : ViteDevServer ,
52- res : ServerResponse ,
53- next : Connect . NextFunction ,
54- ) {
49+ function isServerAccessDeniedForTransform ( config : ResolvedConfig , id : string ) {
5550 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'
6652 }
6753 return false
6854}
@@ -243,7 +229,7 @@ export function transformMiddleware(
243229 allowId ( id ) {
244230 return (
245231 id . startsWith ( '\0' ) ||
246- ! deniedServingAccessForTransform ( id , server , res , next )
232+ ! isServerAccessDeniedForTransform ( server . config , id )
247233 )
248234 } ,
249235 } )
@@ -317,8 +303,18 @@ export function transformMiddleware(
317303 return next ( )
318304 }
319305 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 } ` )
322318 }
323319 return next ( e )
324320 }
0 commit comments