@@ -6,13 +6,21 @@ import { createServer as createHttpsServer } from 'node:https'
6
6
import type { Socket } from 'node:net'
7
7
import colors from 'picocolors'
8
8
import type { WebSocket as WebSocketRaw } from 'ws'
9
- import { WebSocketServer as WebSocketServerRaw } from 'ws'
9
+ import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
10
10
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
11
11
import type { CustomPayload , ErrorPayload , HMRPayload } from 'types/hmrPayload'
12
12
import type { InferCustomEventPayload } from 'types/customEvent'
13
13
import type { ResolvedConfig } from '..'
14
14
import { isObject } from '../utils'
15
15
16
+ /* In Bun, the `ws` module is overridden to hook into the native code. Using the bundled `js` version
17
+ * of `ws` will not work as Bun's req.socket does not allow reading/writing to the underlying socket.
18
+ */
19
+ const WebSocketServerRaw = process . versions . bun
20
+ ? // @ts -expect-error: Bun defines `import.meta.require`
21
+ import . meta. require ( 'ws' ) . WebSocketServer
22
+ : WebSocketServerRaw_
23
+
16
24
export const HMR_HEADER = 'vite-hmr'
17
25
18
26
export type WebSocketCustomListener < T > = (
@@ -87,7 +95,7 @@ export function createWebSocketServer(
87
95
config : ResolvedConfig ,
88
96
httpsOptions ?: HttpsServerOptions ,
89
97
) : WebSocketServer {
90
- let wss : WebSocketServerRaw
98
+ let wss : WebSocketServerRaw_
91
99
let wsHttpServer : Server | undefined = undefined
92
100
93
101
const hmr = isObject ( config . server . hmr ) && config . server . hmr
0 commit comments