createWebSocketServer() — vite Function Reference
Architecture documentation for the createWebSocketServer() function in ws.ts from the vite codebase.
Entity Profile
Dependency Diagram
graph TD ed427876_97cc_17d4_d5e4_ae67bba950e3["createWebSocketServer()"] cab2c96f_4573_f58d_dd64_51e96c3e5033["ws.ts"] ed427876_97cc_17d4_d5e4_ae67bba950e3 -->|defined in| cab2c96f_4573_f58d_dd64_51e96c3e5033 24ecf2a1_3c09_d451_76f3_9485b4e993f8["_createServer()"] 24ecf2a1_3c09_d451_76f3_9485b4e993f8 -->|calls| ed427876_97cc_17d4_d5e4_ae67bba950e3 2aff86e8_0c9d_22cb_6536_c1321e1aaa1d["isObject()"] ed427876_97cc_17d4_d5e4_ae67bba950e3 -->|calls| 2aff86e8_0c9d_22cb_6536_c1321e1aaa1d 92168511_91a7_3f51_bfad_0ba065c191e7["hasValidToken()"] ed427876_97cc_17d4_d5e4_ae67bba950e3 -->|calls| 92168511_91a7_3f51_bfad_0ba065c191e7 2e1469ca_9e59_dcc2_bdc7_05126c765fd0["error()"] ed427876_97cc_17d4_d5e4_ae67bba950e3 -->|calls| 2e1469ca_9e59_dcc2_bdc7_05126c765fd0 743fd3ce_caa4_d18b_5e53_7a8e20b91e4c["normalizeHotChannel()"] ed427876_97cc_17d4_d5e4_ae67bba950e3 -->|calls| 743fd3ce_caa4_d18b_5e53_7a8e20b91e4c style ed427876_97cc_17d4_d5e4_ae67bba950e3 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/vite/src/node/server/ws.ts lines 118–467
export function createWebSocketServer(
server: HttpServer | null,
config: ResolvedConfig,
httpsOptions?: HttpsServerOptions,
): WebSocketServer {
if (config.server.ws === false) {
return {
[isWebSocketServer]: true,
get clients() {
return new Set<WebSocketClient>()
},
async close() {
// noop
},
on: noop as any as WebSocketServer['on'],
off: noop as any as WebSocketServer['off'],
setInvokeHandler: noop,
handleInvoke: async () => ({
error: {
name: 'TransportError',
message: 'handleInvoke not implemented',
stack: new Error().stack,
},
}),
listen: noop,
send: noop,
}
}
let wsHttpServer: Server | undefined = undefined
const hmr = isObject(config.server.hmr) && config.server.hmr
const hmrServer = hmr && hmr.server
const hmrPort = hmr && hmr.port
// TODO: the main server port may not have been chosen yet as it may use the next available
const portsAreCompatible = !hmrPort || hmrPort === config.server.port
const wsServer = hmrServer || (portsAreCompatible && server)
let hmrServerWsListener: (
req: InstanceType<typeof IncomingMessage>,
socket: Duplex,
head: Buffer,
) => void
const customListeners = new Map<string, Set<WebSocketCustomListener<any>>>()
const clientsMap = new WeakMap<WebSocketRaw, WebSocketClient>()
const port = hmrPort || 24678
const host = (hmr && hmr.host) || undefined
const allowedHosts =
config.server.allowedHosts === true
? config.server.allowedHosts
: Object.freeze([...config.server.allowedHosts]) // Freeze the array to allow caching
const shouldHandle = (req: IncomingMessage) => {
const protocol = req.headers['sec-websocket-protocol']!
// vite-ping is allowed to connect from anywhere
// because it needs to be connected before the client fetches the new `/@vite/client`
// this is fine because vite-ping does not receive / send any meaningful data
if (protocol === 'vite-ping') return true
if (
allowedHosts !== true &&
!isHostAllowed(req.headers.host, allowedHosts)
) {
return false
}
if (config.legacy?.skipWebSocketTokenCheck) {
return true
}
// If the Origin header is set, this request might be coming from a browser.
// Browsers always sets the Origin header for WebSocket connections.
if (req.headers.origin) {
const parsedUrl = new URL(`http://example.com${req.url!}`)
return hasValidToken(config, parsedUrl)
}
// We allow non-browser requests to connect without a token
// for backward compat and convenience
// This is fine because if you can sent a request without the SOP limitation,
// you can also send a normal HTTP request to the server.
return true
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does createWebSocketServer() do?
createWebSocketServer() is a function in the vite codebase, defined in packages/vite/src/node/server/ws.ts.
Where is createWebSocketServer() defined?
createWebSocketServer() is defined in packages/vite/src/node/server/ws.ts at line 118.
What does createWebSocketServer() call?
createWebSocketServer() calls 4 function(s): error, hasValidToken, isObject, normalizeHotChannel.
What calls createWebSocketServer()?
createWebSocketServer() is called by 1 function(s): _createServer.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free