Skip to content

Commit

Permalink
fix(server): skip hot channel client normalization for wsServer (#18782)
Browse files Browse the repository at this point in the history
Co-authored-by: sapphi-red <[email protected]>
  • Loading branch information
antfu and sapphi-red authored Nov 27, 2024
1 parent 0c6cdb0 commit cc7670a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export interface NormalizedHotChannel<Api = any> {
export const normalizeHotChannel = (
channel: HotChannel,
enableHmr: boolean,
normalizeClient = true,
): NormalizedHotChannel => {
const normalizedListenerMap = new WeakMap<
(data: any, client: NormalizedHotChannelClient) => void | Promise<void>,
Expand Down Expand Up @@ -225,7 +226,7 @@ export const normalizeHotChannel = (
event: string,
fn: (data: any, client: NormalizedHotChannelClient) => void,
) => {
if (event === 'connection') {
if (event === 'connection' || !normalizeClient) {
channel.on?.(event, fn as () => void)
return
}
Expand Down Expand Up @@ -260,7 +261,7 @@ export const normalizeHotChannel = (
listenersForEvents.get(event)!.add(listenerWithNormalizedClient)
},
off: (event: string, fn: () => void) => {
if (event === 'connection') {
if (event === 'connection' || !normalizeClient) {
channel.off?.(event, fn as () => void)
return
}
Expand Down
23 changes: 18 additions & 5 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import colors from 'picocolors'
import type { WebSocket as WebSocketRaw } from 'ws'
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
import type { ErrorPayload } from 'types/hmrPayload'
import type { ErrorPayload, HotPayload } from 'types/hmrPayload'
import type { InferCustomEventPayload } from 'types/customEvent'
import type { HotChannelClient, ResolvedConfig } from '..'
import type { ResolvedConfig } from '..'
import { isObject } from '../utils'
import { type NormalizedHotChannel, normalizeHotChannel } from './hmr'
import type { NormalizedHotChannel, NormalizedHotChannelClient } from './hmr'
import { normalizeHotChannel } from './hmr'
import type { HttpServer } from '.'

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

export interface WebSocketClient extends HotChannelClient {
export interface WebSocketClient extends NormalizedHotChannelClient {
/**
* The raw WebSocket instance
* @advanced
Expand Down Expand Up @@ -255,7 +256,17 @@ export function createWebSocketServer(
function getSocketClient(socket: WebSocketRaw) {
if (!clientsMap.has(socket)) {
clientsMap.set(socket, {
send: (payload) => {
send: (...args: any[]) => {
let payload: HotPayload
if (typeof args[0] === 'string') {
payload = {
type: 'custom',
event: args[0],
data: args[1],
}
} else {
payload = args[0]
}
socket.send(JSON.stringify(payload))
},
socket,
Expand Down Expand Up @@ -329,6 +340,8 @@ export function createWebSocketServer(
},
},
config.server.hmr !== false,
// Don't normalize client as we already handles the send, and to keep `.socket`
false,
)
return {
...normalizedHotChannel,
Expand Down

0 comments on commit cc7670a

Please sign in to comment.