Skip to content

Commit a7b6ce0

Browse files
committed
fix: apply vitejs#20503
1 parent f2f4529 commit a7b6ce0

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

packages/vite/src/node/server/middlewares/transform.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'node:path'
22
import fsp from 'node:fs/promises'
3-
import type { ServerResponse } from 'node:http'
43
import type { Connect } from 'dep-types/connect'
54
import colors from 'picocolors'
65
import 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'
3737
import { checkLoadingAccess, respondWithAccessDenied } from './static'
3838

3939
const debugCache = createDebugger('vite:cache')
@@ -46,23 +46,9 @@ const rawRE = /[?&]raw\b/
4646
const inlineRE = /[?&]inline\b/
4747
const svgRE = /\.svg\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
}

packages/vite/src/node/server/transformRequest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ async function loadAndTransform(
253253
if (options.allowId && !options.allowId(id)) {
254254
const err: any = new Error(`Denied ID ${id}`)
255255
err.code = ERR_DENIED_ID
256+
err.id = id
256257
throw err
257258
}
258259

0 commit comments

Comments
 (0)