@@ -9,11 +9,12 @@ import colors from 'picocolors'
9
9
import type { WebSocket as WebSocketRaw } from 'ws'
10
10
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
11
11
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
12
- import type { ErrorPayload } from 'types/hmrPayload'
12
+ import type { ErrorPayload , HotPayload } from 'types/hmrPayload'
13
13
import type { InferCustomEventPayload } from 'types/customEvent'
14
- import type { HotChannelClient , ResolvedConfig } from '..'
14
+ import type { ResolvedConfig } from '..'
15
15
import { isObject } from '../utils'
16
- import { type NormalizedHotChannel , normalizeHotChannel } from './hmr'
16
+ import type { NormalizedHotChannel , NormalizedHotChannelClient } from './hmr'
17
+ import { normalizeHotChannel } from './hmr'
17
18
import type { HttpServer } from '.'
18
19
19
20
/* 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 {
66
67
clients : Set < WebSocketClient >
67
68
}
68
69
69
- export interface WebSocketClient extends HotChannelClient {
70
+ export interface WebSocketClient extends NormalizedHotChannelClient {
70
71
/**
71
72
* The raw WebSocket instance
72
73
* @advanced
@@ -255,7 +256,17 @@ export function createWebSocketServer(
255
256
function getSocketClient ( socket : WebSocketRaw ) {
256
257
if ( ! clientsMap . has ( socket ) ) {
257
258
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
+ }
259
270
socket . send ( JSON . stringify ( payload ) )
260
271
} ,
261
272
socket,
@@ -329,6 +340,8 @@ export function createWebSocketServer(
329
340
} ,
330
341
} ,
331
342
config . server . hmr !== false ,
343
+ // Don't normalize client as we already handles the send, and to keep `.socket`
344
+ false ,
332
345
)
333
346
return {
334
347
...normalizedHotChannel ,
0 commit comments