Skip to content

Commit 049404c

Browse files
authored
fix: use Bun's implementation of ws instead of the bundled one (#13901)
1 parent d991d7d commit 049404c

File tree

1 file changed

+10
-2
lines changed
  • packages/vite/src/node/server

1 file changed

+10
-2
lines changed

packages/vite/src/node/server/ws.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ import { createServer as createHttpsServer } from 'node:https'
66
import type { Socket } from 'node:net'
77
import colors from 'picocolors'
88
import type { WebSocket as WebSocketRaw } from 'ws'
9-
import { WebSocketServer as WebSocketServerRaw } from 'ws'
9+
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
1010
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
1111
import type { CustomPayload, ErrorPayload, HMRPayload } from 'types/hmrPayload'
1212
import type { InferCustomEventPayload } from 'types/customEvent'
1313
import type { ResolvedConfig } from '..'
1414
import { isObject } from '../utils'
1515

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+
1624
export const HMR_HEADER = 'vite-hmr'
1725

1826
export type WebSocketCustomListener<T> = (
@@ -87,7 +95,7 @@ export function createWebSocketServer(
8795
config: ResolvedConfig,
8896
httpsOptions?: HttpsServerOptions,
8997
): WebSocketServer {
90-
let wss: WebSocketServerRaw
98+
let wss: WebSocketServerRaw_
9199
let wsHttpServer: Server | undefined = undefined
92100

93101
const hmr = isObject(config.server.hmr) && config.server.hmr

0 commit comments

Comments
 (0)