exportfunctionensureServingAccess( url: string, server: ViteDevServer, res: ServerResponse, next: Connect.NextFunction, ): boolean { if (isFileServingAllowed(url, server)) { returntrue } if (isFileReadable(cleanUrl(url))) { const urlMessage = `The request url "${url}" is outside of Vite serving allow list.` const hintMessage = ` ${server.config.server.fs.allow.map((i) => `- ${i}`).join('\n')} Refer to docs https://vite.dev/config/server-options.html#server-fs-allow for configurations and more details.`
server.config.logger.error(urlMessage) server.config.logger.warnOnce(hintMessage + '\n') res.statusCode = 403 res.write(renderRestrictedErrorHTML(urlMessage + '\n' + hintMessage)) res.end() } else { // if the file doesn't exist, we shouldn't restrict this path as it can // be an API call. Middlewares would issue a 404 if the file isn't handled next() } returnfalse }
asyncload(id) { if (id[0] === '\0') { // Rollup convention, this id should be handled by the // plugin that marked it with \0 return }
// raw requests, read from disk if (rawRE.test(id)) { const file = checkPublicFile(id, config) || cleanUrl(id) this.addWatchFile(file) // raw query, read file and return as string return`export default ${JSON.stringify( await fsp.readFile(file, 'utf-8'), )}` }
if (!urlRE.test(id) && !config.assetsInclude(cleanUrl(id))) { return }
id = removeUrlQuery(id) let url = awaitfileToUrl(this, id)
// Inherit HMR timestamp if this asset was invalidated if (!url.startsWith('data:') && this.environment.mode === 'dev') { const mod = this.environment.moduleGraph.getModuleById(id) if (mod && mod.lastHMRTimestamp > 0) { url = injectQuery(url, `t=${mod.lastHMRTimestamp}`) } }
return { code: `export default ${JSON.stringify(encodeURIPath(url))}`, // Force rollup to keep this module from being shared between other entry points if it's an entrypoint. // If the resulting chunk is empty, it will be removed in generateBundle. moduleSideEffects: config.command === 'build' && this.getModuleInfo(id)?.isEntry ? 'no-treeshake' : false, meta: config.command === 'build' ? { 'vite:asset': true } : undefined, } },