MCPcopy
hub / github.com/vitejs/vite / normalizeHotChannel

Function normalizeHotChannel

packages/vite/src/node/server/hmr.ts:212–370  ·  view source on GitHub ↗
(
  channel: HotChannel,
  enableHmr: boolean,
  normalizeClient = true,
)

Source from the content-addressed store, hash-verified

210}
211
212export const normalizeHotChannel = (
213 channel: HotChannel,
214 enableHmr: boolean,
215 normalizeClient = true,
216): NormalizedHotChannel => {
217 const normalizedListenerMap = new WeakMap<
218 (data: any, client: NormalizedHotChannelClient) => void | Promise<void>,
219 (data: any, client: HotChannelClient) => void | Promise<void>
220 >()
221 const normalizedClients = new WeakMap<
222 HotChannelClient,
223 NormalizedHotChannelClient
224 >()
225
226 let invokeHandlers: InvokeMethods | undefined
227 let listenerForInvokeHandler:
228 | ((data: InvokeSendData, client: HotChannelClient) => void)
229 | undefined
230 const handleInvoke = async <T extends keyof InvokeMethods>(
231 payload: HotPayload,
232 ) => {
233 if (!invokeHandlers) {
234 return {
235 error: {
236 name: 'TransportError',
237 message: 'invokeHandlers is not set',
238 stack: new Error().stack,
239 },
240 }
241 }
242
243 const data: InvokeSendData<T> = (payload as CustomPayload).data
244 const { name, data: args } = data
245 try {
246 const invokeHandler = invokeHandlers[name]
247 // @ts-expect-error `invokeHandler` is `InvokeMethods[T]`, so passing the args is fine
248 const result = await invokeHandler(...args)
249 return { result }
250 } catch (error) {
251 return {
252 error: {
253 name: error.name,
254 message: error.message,
255 stack: error.stack,
256 ...error, // preserve enumerable properties such as RollupError.loc, frame, plugin
257 },
258 }
259 }
260 }
261
262 return {
263 ...channel,
264 on: (
265 event: string,
266 fn: (data: any, client: NormalizedHotChannelClient) => void,
267 ) => {
268 if (event === 'connection' || !normalizeClient) {
269 channel.on?.(event, fn as () => void)

Callers 2

constructorMethod · 0.90
createWebSocketServerFunction · 0.90

Calls 5

onMethod · 0.65
offMethod · 0.65
sendMethod · 0.65
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected