Home / File/ hostCheck.ts — vite Source File

hostCheck.ts — vite Source File

Architecture documentation for hostCheck.ts, a typescript file in the vite codebase. 3 imports, 4 dependents.

File typescript ViteCore ConfigEngine 3 imports 4 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  b99bdf00_fae6_d4c5_885f_b35d01f276cc["hostCheck.ts"]
  8377ae20_ffba_2f9c_bded_58742b7f1c3b["index.ts"]
  b99bdf00_fae6_d4c5_885f_b35d01f276cc --> 8377ae20_ffba_2f9c_bded_58742b7f1c3b
  665781c9_f03c_2e34_f349_eada05586350["host-validation-middleware"]
  b99bdf00_fae6_d4c5_885f_b35d01f276cc --> 665781c9_f03c_2e34_f349_eada05586350
  9165291b_077b_bedb_8c23_36e44bc99390["connect"]
  b99bdf00_fae6_d4c5_885f_b35d01f276cc --> 9165291b_077b_bedb_8c23_36e44bc99390
  7da774f9_eca5_d54e_6e01_6bee7d460a2b["config.ts"]
  7da774f9_eca5_d54e_6e01_6bee7d460a2b --> b99bdf00_fae6_d4c5_885f_b35d01f276cc
  e49f0ff7_5101_3a1d_5a1f_33fae58eea2d["preview.ts"]
  e49f0ff7_5101_3a1d_5a1f_33fae58eea2d --> b99bdf00_fae6_d4c5_885f_b35d01f276cc
  a423a1ed_f7d8_0eb5_9b8f_ddfa7fa8147e["index.ts"]
  a423a1ed_f7d8_0eb5_9b8f_ddfa7fa8147e --> b99bdf00_fae6_d4c5_885f_b35d01f276cc
  47cf80ca_01f8_9764_8b18_1423c86b4a7a["hostCheck.spec.ts"]
  47cf80ca_01f8_9764_8b18_1423c86b4a7a --> b99bdf00_fae6_d4c5_885f_b35d01f276cc
  style b99bdf00_fae6_d4c5_885f_b35d01f276cc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { hostValidationMiddleware as originalHostValidationMiddleware } from 'host-validation-middleware'
import type { Connect } from '#dep-types/connect'
import type { ResolvedPreviewOptions, ResolvedServerOptions } from '../..'

export function getAdditionalAllowedHosts(
  resolvedServerOptions: Pick<ResolvedServerOptions, 'host' | 'hmr' | 'origin'>,
  resolvedPreviewOptions: Pick<ResolvedPreviewOptions, 'host'>,
): string[] {
  const list = []

  // allow host option by default as that indicates that the user is
  // expecting Vite to respond on that host
  if (
    typeof resolvedServerOptions.host === 'string' &&
    resolvedServerOptions.host
  ) {
    list.push(resolvedServerOptions.host)
  }
  if (
    typeof resolvedServerOptions.hmr === 'object' &&
    resolvedServerOptions.hmr.host
  ) {
    list.push(resolvedServerOptions.hmr.host)
  }
  if (
    typeof resolvedPreviewOptions.host === 'string' &&
    resolvedPreviewOptions.host
  ) {
    list.push(resolvedPreviewOptions.host)
  }

  // allow server origin by default as that indicates that the user is
  // expecting Vite to respond on that host
  if (resolvedServerOptions.origin) {
    // some frameworks may pass the origin as a placeholder, so it's not
    // possible to parse as URL, so use a try-catch here as a best effort
    try {
      const serverOriginUrl = new URL(resolvedServerOptions.origin)
      list.push(serverOriginUrl.hostname)
    } catch {}
  }

  return list
}

export function hostValidationMiddleware(
  allowedHosts: string[],
  isPreview: boolean,
): Connect.NextHandleFunction {
  return originalHostValidationMiddleware({
    // Freeze the array to allow caching
    allowedHosts: Object.freeze([...allowedHosts]),
    generateErrorMessage(hostname) {
      const hostnameWithQuotes = JSON.stringify(hostname)
      const optionName = `${isPreview ? 'preview' : 'server'}.allowedHosts`
      return (
        `Blocked request. This host (${hostnameWithQuotes}) is not allowed.\n` +
        `To allow this host, add ${hostnameWithQuotes} to \`${optionName}\` in vite.config.js.`
      )
    },
  })
}

Domain

Subdomains

Dependencies

  • connect
  • host-validation-middleware
  • index.ts

Frequently Asked Questions

What does hostCheck.ts do?
hostCheck.ts is a source file in the vite codebase, written in typescript. It belongs to the ViteCore domain, ConfigEngine subdomain.
What functions are defined in hostCheck.ts?
hostCheck.ts defines 2 function(s): getAdditionalAllowedHosts, hostValidationMiddleware.
What does hostCheck.ts depend on?
hostCheck.ts imports 3 module(s): connect, host-validation-middleware, index.ts.
What files import hostCheck.ts?
hostCheck.ts is imported by 4 file(s): config.ts, hostCheck.spec.ts, index.ts, preview.ts.
Where is hostCheck.ts in the architecture?
hostCheck.ts is located at packages/vite/src/node/server/middlewares/hostCheck.ts (domain: ViteCore, subdomain: ConfigEngine, directory: packages/vite/src/node/server/middlewares).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free