Skip to content

Commit 421fe03

Browse files
authored
fix(core): https support for devtools server (#178)
1 parent 1664e4a commit 421fe03

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

packages/core/src/node/ws.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function createWsServer(options: CreateWsServerOptions) {
2828
const rpcHost = options.context.rpc as unknown as RpcFunctionsHost
2929
const port = options.portWebSocket ?? await getPort({ port: 7812, random: true })!
3030
const host = options.hostWebSocket ?? 'localhost'
31+
const https = options.context.viteConfig.server.https
3132

3233
const wsClients = new Set<WebSocket>()
3334

@@ -42,6 +43,7 @@ export async function createWsServer(options: CreateWsServerOptions) {
4243
const preset = createWsRpcPreset({
4344
port,
4445
host,
46+
https,
4547
onConnected: (ws, req, meta) => {
4648
const url = new URL(req.url ?? '', 'http://localhost')
4749
const authId = url.searchParams.get('vite_devtools_auth_id') ?? undefined

packages/rpc/src/presets/ws/server.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { BirpcGroup, BirpcOptions, ChannelOptions } from 'birpc'
22
import type { IncomingMessage } from 'node:http'
3+
import type { ServerOptions as HttpsServerOptions } from 'node:https'
34
import type { WebSocket } from 'ws'
45
import type { RpcServerPreset } from '..'
6+
import { createServer as createHttpsServer } from 'node:https'
57
import { parse, stringify } from 'structured-clone-es'
68
import { WebSocketServer } from 'ws'
79
import { defineRpcServerPreset } from '..'
@@ -17,6 +19,7 @@ export interface DevToolsNodeRpcSessionMeta {
1719
export interface WebSocketRpcServerOptions {
1820
port: number
1921
host?: string
22+
https?: HttpsServerOptions | undefined
2023
onConnected?: (ws: WebSocket, req: IncomingMessage, meta: DevToolsNodeRpcSessionMeta) => void
2124
onDisconnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void
2225
}
@@ -37,15 +40,20 @@ export const createWsRpcPreset: RpcServerPreset<
3740
> = defineRpcServerPreset((options: WebSocketRpcServerOptions) => {
3841
const {
3942
port,
43+
https,
4044
host = 'localhost',
4145
onConnected = NOOP,
4246
onDisconnected = NOOP,
4347
} = options
4448

45-
const wss = new WebSocketServer({
46-
port,
47-
host,
48-
})
49+
const httpsServer = https ? createHttpsServer(https) : undefined
50+
51+
const wss = https
52+
? new WebSocketServer({ server: httpsServer })
53+
: new WebSocketServer({
54+
port,
55+
host,
56+
})
4957

5058
return <ClientFunctions extends object, ServerFunctions extends object>(
5159
rpcGroup: BirpcGroup<ClientFunctions, ServerFunctions, false>,
@@ -93,5 +101,7 @@ export const createWsRpcPreset: RpcServerPreset<
93101
})
94102
onConnected(ws, req, meta)
95103
})
104+
105+
httpsServer?.listen(port, host)
96106
}
97107
})

0 commit comments

Comments
 (0)