@@ -9,11 +9,12 @@ import colors from 'picocolors'
99import type { WebSocket as WebSocketRaw } from 'ws'
1010import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1111import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
12- import type { ErrorPayload } from 'types/hmrPayload'
12+ import type { ErrorPayload , HotPayload } from 'types/hmrPayload'
1313import type { InferCustomEventPayload } from 'types/customEvent'
14- import type { HotChannelClient , ResolvedConfig } from '..'
14+ import type { ResolvedConfig } from '..'
1515import { isObject } from '../utils'
16- import { type NormalizedHotChannel , normalizeHotChannel } from './hmr'
16+ import type { NormalizedHotChannel , NormalizedHotChannelClient } from './hmr'
17+ import { normalizeHotChannel } from './hmr'
1718import 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