({
config,
getHeaders,
disableFsServeCheck,
}: {
config: ResolvedConfig
getHeaders: () => OutgoingHttpHeaders | undefined
disableFsServeCheck?: boolean
})
| 30 | const ERR_DENIED_FILE = 'ERR_DENIED_FILE' |
| 31 | |
| 32 | const sirvOptions = ({ |
| 33 | config, |
| 34 | getHeaders, |
| 35 | disableFsServeCheck, |
| 36 | }: { |
| 37 | config: ResolvedConfig |
| 38 | getHeaders: () => OutgoingHttpHeaders | undefined |
| 39 | disableFsServeCheck?: boolean |
| 40 | }): Options => { |
| 41 | return { |
| 42 | dev: true, |
| 43 | etag: true, |
| 44 | extensions: [], |
| 45 | setHeaders(res, pathname) { |
| 46 | // Matches js, jsx, ts, tsx, mts, mjs, cjs, cts, ctx, mtx |
| 47 | // The reason this is done, is that the .ts and .mts file extensions are |
| 48 | // reserved for the MIME type video/mp2t. In almost all cases, we can expect |
| 49 | // these files to be TypeScript files, and for Vite to serve them with |
| 50 | // this Content-Type. |
| 51 | if (knownJavascriptExtensionRE.test(pathname)) { |
| 52 | res.setHeader('Content-Type', 'text/javascript') |
| 53 | } |
| 54 | const headers = getHeaders() |
| 55 | if (headers) { |
| 56 | for (const name in headers) { |
| 57 | res.setHeader(name, headers[name]!) |
| 58 | } |
| 59 | } |
| 60 | }, |
| 61 | shouldServe: disableFsServeCheck |
| 62 | ? undefined |
| 63 | : (filePath) => { |
| 64 | const servingAccessResult = checkLoadingAccess(config, filePath) |
| 65 | if (servingAccessResult === 'denied') { |
| 66 | const error: any = new Error('denied access') |
| 67 | error.code = ERR_DENIED_FILE |
| 68 | error.path = filePath |
| 69 | throw error |
| 70 | } |
| 71 | if (servingAccessResult === 'fallback') { |
| 72 | return false |
| 73 | } |
| 74 | servingAccessResult satisfies 'allowed' |
| 75 | return true |
| 76 | }, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | export function servePublicMiddleware( |
| 81 | server: ViteDevServer, |
no test coverage detected