Skip to content

Commit

Permalink
fix: ensure importAnalysis plugin and static middleware use the same …
Browse files Browse the repository at this point in the history
…code to determine filepath from url
  • Loading branch information
dominikg committed Jan 15, 2022
1 parent ea615b0 commit 0a45f21
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
7 changes: 3 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -21,7 +21,8 @@ import {
normalizePath,
removeImportQuery,
unwrapId,
moduleListContains
moduleListContains,
fsPathFromUrl
} from '../utils'
import {
debugHmr,
Expand Down Expand Up @@ -399,9 +400,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
let url = normalizedUrl

// record as safe modules
server?.moduleGraph.safeModulesPath.add(
cleanUrl(url).slice(4 /* '/@fs'.length */)
)
server?.moduleGraph.safeModulesPath.add(fsPathFromUrl(url))

// rewrite
if (url !== specifier) {
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -4,12 +4,11 @@ import type { Options } from 'sirv'
import sirv from 'sirv'
import type { Connect } from 'types/connect'
import type { ViteDevServer } from '../..'
import { normalizePath } from '../..'
import { FS_PREFIX } from '../../constants'
import {
cleanUrl,
ensureLeadingSlash,
fsPathFromId,
fsPathFromUrl,
isImportRequest,
isInternalRequest,
isWindows,
Expand Down Expand Up @@ -148,8 +147,7 @@ export function isFileServingAllowed(
): boolean {
if (!server.config.server.fs.strict) return true

const cleanedUrl = cleanUrl(url)
const file = ensureLeadingSlash(normalizePath(cleanedUrl))
const file = fsPathFromUrl(url)

if (server.config.server.fs.deny.some((i) => isMatch(file, i, _matchOptions)))
return false
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -147,12 +147,18 @@ export function normalizePath(id: string): string {
}

export function fsPathFromId(id: string): string {
const fsPath = normalizePath(id.slice(FS_PREFIX.length))
const fsPath = normalizePath(
id.startsWith(FS_PREFIX) ? id.slice(FS_PREFIX.length) : id
)
return fsPath.startsWith('/') || fsPath.match(VOLUME_RE)
? fsPath
: `/${fsPath}`
}

export function fsPathFromUrl(url: string): string {
return fsPathFromId(cleanUrl(url))
}

export function ensureVolumeInPath(file: string): string {
return isWindows ? path.resolve(file) : file
}
Expand Down

0 comments on commit 0a45f21

Please sign in to comment.