( url: string, config: ResolvedConfig, )
| 34 | } |
| 35 | |
| 36 | export function checkPublicFile( |
| 37 | url: string, |
| 38 | config: ResolvedConfig, |
| 39 | ): string | undefined { |
| 40 | class="cm">// note if the file is in /public, the resolver would have returned it |
| 41 | class="cm">// as-is so it's not going to be a fully resolved path. |
| 42 | const { publicDir } = config |
| 43 | if (!publicDir || url[0] !== class="st">'/') { |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | const fileName = cleanUrl(url) |
| 48 | |
| 49 | class="cm">// short-circuit if we have an in-memory publicFiles cache |
| 50 | const publicFiles = getPublicFiles(config) |
| 51 | if (publicFiles) { |
| 52 | return publicFiles.has(fileName) |
| 53 | ? normalizePath(path.join(publicDir, fileName)) |
| 54 | : undefined |
| 55 | } |
| 56 | |
| 57 | const publicFile = normalizePath(path.join(publicDir, fileName)) |
| 58 | if (!publicFile.startsWith(withTrailingSlash(publicDir))) { |
| 59 | class="cm">// can happen if URL starts with class="st">'../' |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | return tryStatSync(publicFile)?.isFile() ? publicFile : undefined |
| 64 | } |
no test coverage detected