Skip to content

fix(server): skip hot channel client normalization for wsServer #18782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I still think it might be better to consider WebSocketClient as a normalized client.

Another idea is to have a generic for NormalizedHotChannelClient<{ socket: ... }> so that the extra fields can be preserved during normalization.

/**
* 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