Home / Function/ normalizeHotChannel() — vite Function Reference

normalizeHotChannel() — vite Function Reference

Architecture documentation for the normalizeHotChannel() function in hmr.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  743fd3ce_caa4_d18b_5e53_7a8e20b91e4c["normalizeHotChannel()"]
  18db4f26_79f1_5b7d_b291_4feeaf95538f["hmr.ts"]
  743fd3ce_caa4_d18b_5e53_7a8e20b91e4c -->|defined in| 18db4f26_79f1_5b7d_b291_4feeaf95538f
  6ea66dc7_258c_4d4e_457c_a36e57776e88["constructor()"]
  6ea66dc7_258c_4d4e_457c_a36e57776e88 -->|calls| 743fd3ce_caa4_d18b_5e53_7a8e20b91e4c
  ed427876_97cc_17d4_d5e4_ae67bba950e3["createWebSocketServer()"]
  ed427876_97cc_17d4_d5e4_ae67bba950e3 -->|calls| 743fd3ce_caa4_d18b_5e53_7a8e20b91e4c
  57bd050c_f162_f6af_9491_3a9b3bcd5ca2["set()"]
  743fd3ce_caa4_d18b_5e53_7a8e20b91e4c -->|calls| 57bd050c_f162_f6af_9491_3a9b3bcd5ca2
  d1f8b612_857c_d18a_b763_92f90bf93d2b["get()"]
  743fd3ce_caa4_d18b_5e53_7a8e20b91e4c -->|calls| d1f8b612_857c_d18a_b763_92f90bf93d2b
  0edbbec3_6066_80b2_5f47_367d86a7705f["listen()"]
  743fd3ce_caa4_d18b_5e53_7a8e20b91e4c -->|calls| 0edbbec3_6066_80b2_5f47_367d86a7705f
  2a568e89_e3e7_9284_fd00_e1a9ca1ec09a["close()"]
  743fd3ce_caa4_d18b_5e53_7a8e20b91e4c -->|calls| 2a568e89_e3e7_9284_fd00_e1a9ca1ec09a
  style 743fd3ce_caa4_d18b_5e53_7a8e20b91e4c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/server/hmr.ts lines 171–329

export const normalizeHotChannel = (
  channel: HotChannel,
  enableHmr: boolean,
  normalizeClient = true,
): NormalizedHotChannel => {
  const normalizedListenerMap = new WeakMap<
    (data: any, client: NormalizedHotChannelClient) => void | Promise<void>,
    (data: any, client: HotChannelClient) => void | Promise<void>
  >()
  const normalizedClients = new WeakMap<
    HotChannelClient,
    NormalizedHotChannelClient
  >()

  let invokeHandlers: InvokeMethods | undefined
  let listenerForInvokeHandler:
    | ((data: InvokeSendData, client: HotChannelClient) => void)
    | undefined
  const handleInvoke = async <T extends keyof InvokeMethods>(
    payload: HotPayload,
  ) => {
    if (!invokeHandlers) {
      return {
        error: {
          name: 'TransportError',
          message: 'invokeHandlers is not set',
          stack: new Error().stack,
        },
      }
    }

    const data: InvokeSendData<T> = (payload as CustomPayload).data
    const { name, data: args } = data
    try {
      const invokeHandler = invokeHandlers[name]
      // @ts-expect-error `invokeHandler` is `InvokeMethods[T]`, so passing the args is fine
      const result = await invokeHandler(...args)
      return { result }
    } catch (error) {
      return {
        error: {
          name: error.name,
          message: error.message,
          stack: error.stack,
          ...error, // preserve enumerable properties such as RollupError.loc, frame, plugin
        },
      }
    }
  }

  return {
    ...channel,
    on: (
      event: string,
      fn: (data: any, client: NormalizedHotChannelClient) => void,
    ) => {
      if (event === 'connection' || !normalizeClient) {
        channel.on?.(event, fn as () => void)
        return
      }

      const listenerWithNormalizedClient = (
        data: any,
        client: HotChannelClient,
      ) => {
        if (!normalizedClients.has(client)) {
          normalizedClients.set(client, {
            send: (...args) => {
              let payload: HotPayload
              if (typeof args[0] === 'string') {
                payload = {
                  type: 'custom',
                  event: args[0],
                  data: args[1],
                }
              } else {
                payload = args[0]
              }
              client.send(payload)
            },
          })

Domain

Subdomains

Frequently Asked Questions

What does normalizeHotChannel() do?
normalizeHotChannel() is a function in the vite codebase, defined in packages/vite/src/node/server/hmr.ts.
Where is normalizeHotChannel() defined?
normalizeHotChannel() is defined in packages/vite/src/node/server/hmr.ts at line 171.
What does normalizeHotChannel() call?
normalizeHotChannel() calls 4 function(s): close, get, listen, set.
What calls normalizeHotChannel()?
normalizeHotChannel() is called by 2 function(s): constructor, createWebSocketServer.

Analyze Your Own Codebase

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

Try Supermodel Free