(
req,
res,
dirAbs,
staticMiddleware,
injectionOpts = {},
rejectPrefixes = [],
basePath = "/",
)
| 481 | } |
| 482 | |
| 483 | async function handleStatic( |
| 484 | req, |
| 485 | res, |
| 486 | dirAbs, |
| 487 | staticMiddleware, |
| 488 | injectionOpts = {}, |
| 489 | rejectPrefixes = [], |
| 490 | basePath = "/", |
| 491 | ) { |
| 492 | const urlPath = parseUrlPath(req, res); |
| 493 | if (urlPath === null) return; |
| 494 | |
| 495 | if (!isMountedPath(urlPath, basePath)) { |
| 496 | if (matchesAnyPrefix(urlPath, rejectPrefixes)) { |
| 497 | rejectUnavailable(res); |
| 498 | return; |
| 499 | } |
| 500 | if (!redirectToMountedPath(req, res, urlPath, basePath)) notFound(res); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | const mountedUrl = stripBasePathFromUrl(req.url ?? "/", basePath); |
| 505 | const mountedPath = parseUrlPath({ ...req, url: mountedUrl }, res); |
| 506 | if (mountedPath === null) return; |
| 507 | |
| 508 | const injectRuntimeConfig = needsRuntimeInjection(injectionOpts); |
| 509 | const indexPath = resolve(dirAbs, "index.html"); |
| 510 | |
| 511 | if ( |
| 512 | injectRuntimeConfig && |
| 513 | isGetOrHead(req) && |
| 514 | (mountedPath === "/" || mountedPath === "/index.html") |
| 515 | ) { |
| 516 | if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts)) |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | const mountedReq = Object.create(req); |
| 521 | mountedReq.url = mountedUrl; |
| 522 | |
| 523 | staticMiddleware(mountedReq, res, async () => { |
| 524 | if (matchesAnyPrefix(mountedPath, rejectPrefixes)) { |
| 525 | rejectUnavailable(res); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if (isGetOrHead(req) && !looksLikeAssetRequest(mountedPath)) { |
| 530 | if (await serveInjectedIndexHtml(req, res, indexPath, injectionOpts)) { |
| 531 | return; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | notFound(res); |
| 536 | }); |
| 537 | } |
| 538 | |
| 539 | // ───────────────────────────────────────────────────────────────────────────── |
| 540 | // Server |
no test coverage detected