Skip to content

Commit cc7670a

Browse files
antfusapphi-red
andauthoredNov 27, 2024··
fix(server): skip hot channel client normalization for wsServer (#18782)
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent 0c6cdb0 commit cc7670a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed
 

‎packages/vite/src/node/server/hmr.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export interface NormalizedHotChannel<Api = any> {
173173
export const normalizeHotChannel = (
174174
channel: HotChannel,
175175
enableHmr: boolean,
176+
normalizeClient = true,
176177
): NormalizedHotChannel => {
177178
const normalizedListenerMap = new WeakMap<
178179
(data: any, client: NormalizedHotChannelClient) => void | Promise<void>,
@@ -225,7 +226,7 @@ export const normalizeHotChannel = (
225226
event: string,
226227
fn: (data: any, client: NormalizedHotChannelClient) => void,
227228
) => {
228-
if (event === 'connection') {
229+
if (event === 'connection' || !normalizeClient) {
229230
channel.on?.(event, fn as () => void)
230231
return
231232
}
@@ -260,7 +261,7 @@ export const normalizeHotChannel = (
260261
listenersForEvents.get(event)!.add(listenerWithNormalizedClient)
261262
},
262263
off: (event: string, fn: () => void) => {
263-
if (event === 'connection') {
264+
if (event === 'connection' || !normalizeClient) {
264265
channel.off?.(event, fn as () => void)
265266
return
266267
}

‎packages/vite/src/node/server/ws.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import colors from 'picocolors'
99
import type { WebSocket as WebSocketRaw } from 'ws'
1010
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1111
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
12-
import type { ErrorPayload } from 'types/hmrPayload'
12+
import type { ErrorPayload, HotPayload } from 'types/hmrPayload'
1313
import type { InferCustomEventPayload } from 'types/customEvent'
14-
import type { HotChannelClient, ResolvedConfig } from '..'
14+
import type { ResolvedConfig } from '..'
1515
import { isObject } from '../utils'
16-
import { type NormalizedHotChannel, normalizeHotChannel } from './hmr'
16+
import type { NormalizedHotChannel, NormalizedHotChannelClient } from './hmr'
17+
import { normalizeHotChannel } from './hmr'
1718
import type { HttpServer } from '.'
1819

1920
/* In Bun, the `ws` module is overridden to hook into the native code. Using the bundled `js` version
@@ -66,7 +67,7 @@ export interface WebSocketServer extends NormalizedHotChannel {
6667
clients: Set<WebSocketClient>
6768
}
6869

69-
export interface WebSocketClient extends HotChannelClient {
70+
export interface WebSocketClient extends NormalizedHotChannelClient {
7071
/**
7172
* The raw WebSocket instance
7273
* @advanced
@@ -255,7 +256,17 @@ export function createWebSocketServer(
255256
function getSocketClient(socket: WebSocketRaw) {
256257
if (!clientsMap.has(socket)) {
257258
clientsMap.set(socket, {
258-
send: (payload) => {
259+
send: (...args: any[]) => {
260+
let payload: HotPayload
261+
if (typeof args[0] === 'string') {
262+
payload = {
263+
type: 'custom',
264+
event: args[0],
265+
data: args[1],
266+
}
267+
} else {
268+
payload = args[0]
269+
}
259270
socket.send(JSON.stringify(payload))
260271
},
261272
socket,
@@ -329,6 +340,8 @@ export function createWebSocketServer(
329340
},
330341
},
331342
config.server.hmr !== false,
343+
// Don't normalize client as we already handles the send, and to keep `.socket`
344+
false,
332345
)
333346
return {
334347
...normalizedHotChannel,

0 commit comments

Comments
 (0)
Please sign in to comment.