Skip to content

Commit

Permalink
fix: use Bun's implementation of ws instead of the bundled one (#13901
Browse files Browse the repository at this point in the history
)
  • Loading branch information
paperdave authored Jul 21, 2023
1 parent d991d7d commit 049404c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import { createServer as createHttpsServer } from 'node:https'
import type { Socket } from 'node:net'
import colors from 'picocolors'
import type { WebSocket as WebSocketRaw } from 'ws'
import { WebSocketServer as WebSocketServerRaw } from 'ws'
import { WebSocketServer as WebSocketServerRaw_ } from 'ws'
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
import type { CustomPayload, ErrorPayload, HMRPayload } from 'types/hmrPayload'
import type { InferCustomEventPayload } from 'types/customEvent'
import type { ResolvedConfig } from '..'
import { isObject } from '../utils'

/* In Bun, the `ws` module is overridden to hook into the native code. Using the bundled `js` version
* of `ws` will not work as Bun's req.socket does not allow reading/writing to the underlying socket.
*/
const WebSocketServerRaw = process.versions.bun
? // @ts-expect-error: Bun defines `import.meta.require`
import.meta.require('ws').WebSocketServer
: WebSocketServerRaw_

export const HMR_HEADER = 'vite-hmr'

export type WebSocketCustomListener<T> = (
Expand Down Expand Up @@ -87,7 +95,7 @@ export function createWebSocketServer(
config: ResolvedConfig,
httpsOptions?: HttpsServerOptions,
): WebSocketServer {
let wss: WebSocketServerRaw
let wss: WebSocketServerRaw_
let wsHttpServer: Server | undefined = undefined

const hmr = isObject(config.server.hmr) && config.server.hmr
Expand Down

0 comments on commit 049404c

Please sign in to comment.