1
1
import type { Server } from 'node:http'
2
- import { STATUS_CODES } from 'node:http'
2
+ import { STATUS_CODES , createServer as createHttpServer } from 'node:http'
3
3
import type { ServerOptions as HttpsServerOptions } from 'node:https'
4
4
import { createServer as createHttpsServer } from 'node:https'
5
5
import type { Socket } from 'node:net'
6
6
import colors from 'picocolors'
7
- import type { ServerOptions , WebSocket as WebSocketRaw } from 'ws'
7
+ import type { WebSocket as WebSocketRaw } from 'ws'
8
8
import { WebSocketServer as WebSocketServerRaw } from 'ws'
9
9
import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
10
10
import type { CustomPayload , ErrorPayload , HMRPayload } from 'types/hmrPayload'
@@ -20,6 +20,10 @@ export type WebSocketCustomListener<T> = (
20
20
) => void
21
21
22
22
export interface WebSocketServer {
23
+ /**
24
+ * Listen on port and host
25
+ */
26
+ listen ( ) : void
23
27
/**
24
28
* Get all connected clients.
25
29
*/
@@ -83,7 +87,7 @@ export function createWebSocketServer(
83
87
httpsOptions ?: HttpsServerOptions ,
84
88
) : WebSocketServer {
85
89
let wss : WebSocketServerRaw
86
- let httpsServer : Server | undefined = undefined
90
+ let wsHttpServer : Server | undefined = undefined
87
91
88
92
const hmr = isObject ( config . server . hmr ) && config . server . hmr
89
93
const hmrServer = hmr && hmr . server
@@ -93,6 +97,8 @@ export function createWebSocketServer(
93
97
const wsServer = hmrServer || ( portsAreCompatible && server )
94
98
const customListeners = new Map < string , Set < WebSocketCustomListener < any > > > ( )
95
99
const clientsMap = new WeakMap < WebSocketRaw , WebSocketClient > ( )
100
+ const port = hmrPort || 24678
101
+ const host = ( hmr && hmr . host ) || undefined
96
102
97
103
if ( wsServer ) {
98
104
wss = new WebSocketServerRaw ( { noServer : true } )
@@ -104,39 +110,28 @@ export function createWebSocketServer(
104
110
}
105
111
} )
106
112
} else {
107
- const websocketServerOptions : ServerOptions = { }
108
- const port = hmrPort || 24678
109
- const host = ( hmr && hmr . host ) || undefined
110
- if ( httpsOptions ) {
111
- // if we're serving the middlewares over https, the ws library doesn't support automatically creating an https server, so we need to do it ourselves
112
- // create an inline https server and mount the websocket server to it
113
- httpsServer = createHttpsServer ( httpsOptions , ( req , res ) => {
114
- const statusCode = 426
115
- const body = STATUS_CODES [ statusCode ]
116
- if ( ! body )
117
- throw new Error (
118
- `No body text found for the ${ statusCode } status code` ,
119
- )
113
+ // http server request handler keeps the same with
114
+ // https://github.com/websockets/ws/blob/45e17acea791d865df6b255a55182e9c42e5877a/lib/websocket-server.js#L88-L96
115
+ const route = ( ( _ , res ) => {
116
+ const statusCode = 426
117
+ const body = STATUS_CODES [ statusCode ]
118
+ if ( ! body )
119
+ throw new Error ( `No body text found for the ${ statusCode } status code` )
120
120
121
- res . writeHead ( statusCode , {
122
- 'Content-Length' : body . length ,
123
- 'Content-Type' : 'text/plain' ,
124
- } )
125
- res . end ( body )
121
+ res . writeHead ( statusCode , {
122
+ 'Content-Length' : body . length ,
123
+ 'Content-Type' : 'text/plain' ,
126
124
} )
127
-
128
- httpsServer . listen ( port , host )
129
- websocketServerOptions . server = httpsServer
125
+ res . end ( body )
126
+ } ) as Parameters < typeof createHttpServer > [ 1 ]
127
+ if ( httpsOptions ) {
128
+ wsHttpServer = createHttpsServer ( httpsOptions , route )
130
129
} else {
131
- // we don't need to serve over https, just let ws handle its own server
132
- websocketServerOptions . port = port
133
- if ( host ) {
134
- websocketServerOptions . host = host
135
- }
130
+ wsHttpServer = createHttpServer ( route )
136
131
}
137
-
138
132
// vite dev server in middleware mode
139
- wss = new WebSocketServerRaw ( websocketServerOptions )
133
+ // need to call ws listen manually
134
+ wss = new WebSocketServerRaw ( { server : wsHttpServer } )
140
135
}
141
136
142
137
wss . on ( 'connection' , ( socket ) => {
@@ -210,6 +205,9 @@ export function createWebSocketServer(
210
205
let bufferedError : ErrorPayload | null = null
211
206
212
207
return {
208
+ listen : ( ) => {
209
+ wsHttpServer ?. listen ( port , host )
210
+ } ,
213
211
on : ( ( event : string , fn : ( ) => void ) => {
214
212
if ( wsServerEvents . includes ( event ) ) wss . on ( event , fn )
215
213
else {
@@ -266,8 +264,8 @@ export function createWebSocketServer(
266
264
if ( err ) {
267
265
reject ( err )
268
266
} else {
269
- if ( httpsServer ) {
270
- httpsServer . close ( ( err ) => {
267
+ if ( wsHttpServer ) {
268
+ wsHttpServer . close ( ( err ) => {
271
269
if ( err ) {
272
270
reject ( err )
273
271
} else {
0 commit comments