Home / File/ send.ts — vite Source File

send.ts — vite Source File

Architecture documentation for send.ts, a typescript file in the vite codebase. 11 imports, 2 dependents.

File typescript ViteCore ConfigEngine 11 imports 2 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93["send.ts"]
  031bc221_67a8_c579_f2bf_bb30a08beeb2["utils.ts"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 031bc221_67a8_c579_f2bf_bb30a08beeb2
  23a2e685_f919_9578_27ba_bde71c122058["createDebugger"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 23a2e685_f919_9578_27ba_bde71c122058
  3f57c8be_be57_4cf4_aa11_4ed077229c70["removeTimestampQuery"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 3f57c8be_be57_4cf4_aa11_4ed077229c70
  18244f7c_8357_ba88_c896_32c6447f1faf["sourcemap.ts"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 18244f7c_8357_ba88_c896_32c6447f1faf
  0d5a3ce6_a346_6e0c_cb12_77d5afc6444c["getCodeWithSourcemap"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 0d5a3ce6_a346_6e0c_cb12_77d5afc6444c
  946bdba3_227b_3fc0_1b4c_ddbdb281f454["node:http"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 946bdba3_227b_3fc0_1b4c_ddbdb281f454
  51e96894_3556_ed5c_1ede_97d449867adf["node:path"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 51e96894_3556_ed5c_1ede_97d449867adf
  dc6f5c19_97fd_e1d7_2ae6_6a0d69e562e6["convert-source-map"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> dc6f5c19_97fd_e1d7_2ae6_6a0d69e562e6
  3dc3ca78_a56e_a577_25bd_772d579140ee["etag"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 3dc3ca78_a56e_a577_25bd_772d579140ee
  693ca867_249b_3e5a_0ce1_8930413b7fcd["rolldown"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> 693ca867_249b_3e5a_0ce1_8930413b7fcd
  ff79973e_f09f_1c6b_f6b5_d1707df47116["magic-string"]
  b6714c40_d79b_b9e8_4a00_ef2de34c1c93 --> ff79973e_f09f_1c6b_f6b5_d1707df47116
  3f56d5b2_9fca_532f_3bfc_6bfb2be77015["indexHtml.ts"]
  3f56d5b2_9fca_532f_3bfc_6bfb2be77015 --> b6714c40_d79b_b9e8_4a00_ef2de34c1c93
  9da9b065_5c17_7764_d8e4_bd411021bf8d["transform.ts"]
  9da9b065_5c17_7764_d8e4_bd411021bf8d --> b6714c40_d79b_b9e8_4a00_ef2de34c1c93
  style b6714c40_d79b_b9e8_4a00_ef2de34c1c93 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type {
  IncomingMessage,
  OutgoingHttpHeaders,
  ServerResponse,
} from 'node:http'
import path from 'node:path'
import convertSourceMap from 'convert-source-map'
import getEtag from 'etag'
import type { SourceMap } from 'rolldown'
import MagicString from 'magic-string'
import { createDebugger, removeTimestampQuery } from '../utils'
import { getCodeWithSourcemap } from './sourcemap'

const debug = createDebugger('vite:send', {
  onlyWhenFocused: true,
})

const alias: Record<string, string | undefined> = {
  js: 'text/javascript',
  css: 'text/css',
  html: 'text/html',
  json: 'application/json',
}

export interface SendOptions {
  etag?: string
  cacheControl?: string
  headers?: OutgoingHttpHeaders
  map?: SourceMap | { mappings: '' } | null
}

export function send(
  req: IncomingMessage,
  res: ServerResponse,
  content: string | Buffer,
  type: string,
  options: SendOptions,
): void {
  const {
    etag = getEtag(content, { weak: true }),
    cacheControl = 'no-cache',
    headers,
    map,
  } = options

  if (res.writableEnded) {
    return
  }

  if (req.headers['if-none-match'] === etag) {
    res.statusCode = 304
    res.end()
    return
  }

  res.setHeader('Content-Type', alias[type] || type)
  res.setHeader('Cache-Control', cacheControl)
  res.setHeader('Etag', etag)

  if (headers) {
    for (const name in headers) {
      res.setHeader(name, headers[name]!)
    }
  }

  // inject source map reference
  if (map && 'version' in map && map.mappings) {
    if (type === 'js' || type === 'css') {
      content = getCodeWithSourcemap(type, content.toString(), map)
    }
  }
  // inject fallback sourcemap for js for improved debugging
  // https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
  else if (type === 'js' && (!map || map.mappings !== '')) {
    const code = content.toString()
    // if the code has existing inline sourcemap, assume it's correct and skip
    if (convertSourceMap.mapFileCommentRegex.test(code)) {
      debug?.(`Skipped injecting fallback sourcemap for ${req.url}`)
    } else {
      const urlWithoutTimestamp = removeTimestampQuery(req.url!)
      const ms = new MagicString(code)
      content = getCodeWithSourcemap(
        type,
        code,
        ms.generateMap({
          source: path.basename(urlWithoutTimestamp),
          hires: 'boundary',
          includeContent: true,
        }) as SourceMap,
      )
    }
  }

  res.statusCode = 200
  if (req.method === 'HEAD') {
    res.end()
  } else {
    res.end(content)
  }
  return
}

Domain

Subdomains

Functions

Types

Dependencies

Frequently Asked Questions

What does send.ts do?
send.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 send.ts?
send.ts defines 1 function(s): send.
What does send.ts depend on?
send.ts imports 11 module(s): convert-source-map, createDebugger, etag, getCodeWithSourcemap, magic-string, node:http, node:path, removeTimestampQuery, and 3 more.
What files import send.ts?
send.ts is imported by 2 file(s): indexHtml.ts, transform.ts.
Where is send.ts in the architecture?
send.ts is located at packages/vite/src/node/server/send.ts (domain: ViteCore, subdomain: ConfigEngine, directory: packages/vite/src/node/server).

Analyze Your Own Codebase

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

Try Supermodel Free