Skip to content
Merged
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
6 changes: 6 additions & 0 deletions .changeset/fresh-bars-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tanstack/devtools-vite': patch
'@tanstack/devtools-event-bus': patch
---

fix issues with https servers and console piping
52 changes: 46 additions & 6 deletions packages/devtools-vite/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import { generateConsolePipeCode } from './virtual-console'
import type { ServerResponse } from 'node:http'
import type { Plugin } from 'vite'
import type { EditorConfig } from './editor'
import type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'
import type {
HttpServerLike,
ServerEventBusConfig,
} from '@tanstack/devtools-event-bus/server'

export type ConsoleLevel = 'log' | 'warn' | 'error' | 'info' | 'debug'

Expand Down Expand Up @@ -113,6 +116,8 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {

let devtoolsFileId: string | null = null
let devtoolsPort: number | null = null
let devtoolsHost: string | null = null
let devtoolsProtocol: 'http' | 'https' | null = null

return [
{
Expand Down Expand Up @@ -170,9 +175,24 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
async configureServer(server) {
if (serverBusEnabled) {
const preferredPort = args?.eventBusConfig?.port ?? 4206
const isHttps = !!server.config.server.https
const serverHost =
typeof server.config.server.host === 'string'
? server.config.server.host
: 'localhost'

devtoolsProtocol = isHttps ? 'https' : 'http'
devtoolsHost = serverHost

const bus = new ServerEventBus({
...args?.eventBusConfig,
port: preferredPort,
host: serverHost,
// When HTTPS is enabled, piggyback on Vite's server
// so WebSocket/SSE connections share the same TLS certificate
...(isHttps && server.httpServer
? { httpServer: server.httpServer as HttpServerLike }
: {}),
})
// start() now handles EADDRINUSE and returns the actual port
devtoolsPort = await bus.start()
Expand Down Expand Up @@ -608,22 +628,42 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
},
},
{
name: '@tanstack/devtools:port-injection',
name: '@tanstack/devtools:connection-injection',
apply(config, { command }) {
return config.mode === 'development' && command === 'serve'
},
transform(code, id) {
// Only transform @tanstack packages that contain the port placeholder
if (!code.includes('__TANSTACK_DEVTOOLS_PORT__')) return
// Only transform @tanstack packages that contain the connection placeholders
const hasPlaceholder =
code.includes('__TANSTACK_DEVTOOLS_PORT__') ||
code.includes('__TANSTACK_DEVTOOLS_HOST__') ||
code.includes('__TANSTACK_DEVTOOLS_PROTOCOL__')
if (!hasPlaceholder) return
if (
!id.includes('@tanstack/devtools') &&
!id.includes('@tanstack/event-bus')
)
return

// Replace placeholder with actual port (or fallback to 4206 if not resolved yet)
// Replace placeholders with actual values (or fallback defaults)
const portValue = devtoolsPort ?? 4206
return code.replace(/__TANSTACK_DEVTOOLS_PORT__/g, String(portValue))
const hostValue = devtoolsHost ?? 'localhost'
const protocolValue = devtoolsProtocol ?? 'http'

let result = code
result = result.replace(
/__TANSTACK_DEVTOOLS_PORT__/g,
String(portValue),
)
result = result.replace(
/__TANSTACK_DEVTOOLS_HOST__/g,
JSON.stringify(hostValue),
)
result = result.replace(
/__TANSTACK_DEVTOOLS_PROTOCOL__/g,
JSON.stringify(protocolValue),
)
return result
},
},
]
Expand Down
1 change: 1 addition & 0 deletions packages/devtools-vite/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const handleDevToolsViteRequest = (
options.onOpenSource?.(parsedData)
} catch (e) {}
res.write('OK')
res.end()
})
}

Expand Down
Loading
Loading