Skip to content

Commit d62b90c

Browse files
authored
fix(core): resolve vite server.https for websocket server (#381)
1 parent 12d3659 commit d62b90c

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

packages/core/src/node/https.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Port from https://github.com/vitejs/vite/blob/main/packages/vite/src/node/http.ts#L146
2+
3+
import type { Buffer } from 'node:buffer'
4+
import type { ServerOptions as HttpsServerOptions } from 'node:https'
5+
import { readFile } from 'node:fs/promises'
6+
import { resolve } from 'node:path'
7+
8+
export async function resolveHttpsConfig(https: HttpsServerOptions | undefined): Promise<HttpsServerOptions | undefined> {
9+
if (!https)
10+
return undefined
11+
12+
const [ca, cert, key, pfx] = await Promise.all([
13+
readFileIfExists(https.ca),
14+
readFileIfExists(https.cert),
15+
readFileIfExists(https.key),
16+
readFileIfExists(https.pfx),
17+
])
18+
19+
return {
20+
...https,
21+
ca,
22+
cert,
23+
key,
24+
pfx,
25+
}
26+
}
27+
28+
async function readFileIfExists<T>(value: T): Promise<T | Buffer> {
29+
if (typeof value === 'string')
30+
return readFile(resolve(value)).catch(() => value)
31+
32+
return value
33+
}

packages/core/src/node/ws.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getPort } from 'get-port-please'
1212
import { createDebug } from 'obug'
1313
import { MARK_INFO } from './constants'
1414
import { diagnostics } from './diagnostics'
15+
import { resolveHttpsConfig } from './https'
1516

1617
const debugInvoked = createDebug('vite:devtools:rpc:invoked')
1718

@@ -40,7 +41,7 @@ function buildWsUrl({ host, port, https }: { host: string, port: number, https:
4041
export async function createWsServer(options: CreateWsServerOptions) {
4142
const rpcHost = options.context.rpc as unknown as RpcFunctionsHost
4243
const host = options.websocket.host
43-
const https = options.websocket.https === false ? undefined : (options.websocket.https ?? options.context.viteConfig.server.https)
44+
const https = await resolveHttpsConfig(options.websocket.https === false ? undefined : (options.websocket.https ?? options.context.viteConfig.server.https))
4445
const port = options.websocket.port ?? await getPort({ port: 7812, host, random: true })!
4546

4647
const wsClients = new Set<WebSocket>()

0 commit comments

Comments
 (0)