From 0ea719776b9a43208723fc2fc892e8d8595392ac Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 20 Apr 2023 14:00:05 +0100 Subject: [PATCH] refactor(vite): use native `isFileServingAllowed` util (#20414) --- packages/vite/src/vite-node.ts | 64 ++-------------------------------- 1 file changed, 2 insertions(+), 62 deletions(-) diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index 5df7a99300..8485a1599c 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -1,11 +1,11 @@ import { pathToFileURL } from 'node:url' -import os from 'node:os' import { createApp, createError, defineEventHandler, defineLazyEventHandler, eventHandler, toNodeListener } from 'h3' import { ViteNodeServer } from 'vite-node/server' import fse from 'fs-extra' import { isAbsolute, normalize, resolve } from 'pathe' import { addDevServerHandler } from '@nuxt/kit' -import type { ModuleNode, ViteDevServer, Plugin as VitePlugin } from 'vite' +import { isFileServingAllowed } from 'vite' +import type { ModuleNode, Plugin as VitePlugin } from 'vite' import { normalizeViteManifest } from 'vue-bundle-renderer' import { resolve as resolveModule } from 'mlly' import { distDir } from './dirs' @@ -183,63 +183,3 @@ export async function initViteNodeServer (ctx: ViteBuildContext) { `export { default } from ${JSON.stringify(pathToFileURL(manifestResolvedPath).href)}` ) } - -/** - * The following code is ported from vite - * Awaits https://github.com/vitejs/vite/pull/12894 - */ -const VOLUME_RE = /^[A-Z]:/i -const FS_PREFIX = '/@fs/' -const isWindows = os.platform() === 'win32' -const postfixRE = /[?#].*$/s -const windowsSlashRE = /\\/g - -function slash (p: string): string { - return p.replace(windowsSlashRE, '/') -} - -function normalizePath (id: string): string { - return normalize(isWindows ? slash(id) : id) -} -function fsPathFromId (id: string): string { - const fsPath = normalizePath( - id.startsWith(FS_PREFIX) ? id.slice(FS_PREFIX.length) : id - ) - return fsPath[0] === '/' || fsPath.match(VOLUME_RE) ? fsPath : `/${fsPath}` -} - -function fsPathFromUrl (url: string): string { - return fsPathFromId(cleanUrl(url)) -} - -function cleanUrl (url: string): string { - return url.replace(postfixRE, '') -} - -function isFileServingAllowed ( - url: string, - server: ViteDevServer -): boolean { - if (!server.config.server.fs.strict) { return true } - - const file = fsPathFromUrl(url) - - // @ts-expect-error private API - if (server._fsDenyGlob(file)) { return false } - - if (server.moduleGraph.safeModulesPath.has(file)) { return true } - - if (server.config.server.fs.allow.some(dir => isParentDirectory(dir, file))) { return true } - - return false -} - -function isParentDirectory (dir: string, file: string): boolean { - if (dir[dir.length - 1] !== '/') { - dir = `${dir}/` - } - return ( - file.startsWith(dir) || - (file.toLowerCase().startsWith(dir.toLowerCase())) - ) -}