Skip to content

Commit da41a59

Browse files
fix: issue with console piping and https server resolutions (#343)
* fix: issue with console piping and https server resolutions * changeset * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent e2c1609 commit da41a59

File tree

10 files changed

+1196
-26
lines changed

10 files changed

+1196
-26
lines changed

.changeset/fresh-bars-matter.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/devtools-vite': patch
3+
'@tanstack/devtools-event-bus': patch
4+
---
5+
6+
fix issues with https servers and console piping

packages/devtools-vite/src/plugin.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import { generateConsolePipeCode } from './virtual-console'
2121
import type { ServerResponse } from 'node:http'
2222
import type { Plugin } from 'vite'
2323
import type { EditorConfig } from './editor'
24-
import type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'
24+
import type {
25+
HttpServerLike,
26+
ServerEventBusConfig,
27+
} from '@tanstack/devtools-event-bus/server'
2528

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

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

114117
let devtoolsFileId: string | null = null
115118
let devtoolsPort: number | null = null
119+
let devtoolsHost: string | null = null
120+
let devtoolsProtocol: 'http' | 'https' | null = null
116121

117122
return [
118123
{
@@ -170,9 +175,24 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
170175
async configureServer(server) {
171176
if (serverBusEnabled) {
172177
const preferredPort = args?.eventBusConfig?.port ?? 4206
178+
const isHttps = !!server.config.server.https
179+
const serverHost =
180+
typeof server.config.server.host === 'string'
181+
? server.config.server.host
182+
: 'localhost'
183+
184+
devtoolsProtocol = isHttps ? 'https' : 'http'
185+
devtoolsHost = serverHost
186+
173187
const bus = new ServerEventBus({
174188
...args?.eventBusConfig,
175189
port: preferredPort,
190+
host: serverHost,
191+
// When HTTPS is enabled, piggyback on Vite's server
192+
// so WebSocket/SSE connections share the same TLS certificate
193+
...(isHttps && server.httpServer
194+
? { httpServer: server.httpServer as HttpServerLike }
195+
: {}),
176196
})
177197
// start() now handles EADDRINUSE and returns the actual port
178198
devtoolsPort = await bus.start()
@@ -608,22 +628,42 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
608628
},
609629
},
610630
{
611-
name: '@tanstack/devtools:port-injection',
631+
name: '@tanstack/devtools:connection-injection',
612632
apply(config, { command }) {
613633
return config.mode === 'development' && command === 'serve'
614634
},
615635
transform(code, id) {
616-
// Only transform @tanstack packages that contain the port placeholder
617-
if (!code.includes('__TANSTACK_DEVTOOLS_PORT__')) return
636+
// Only transform @tanstack packages that contain the connection placeholders
637+
const hasPlaceholder =
638+
code.includes('__TANSTACK_DEVTOOLS_PORT__') ||
639+
code.includes('__TANSTACK_DEVTOOLS_HOST__') ||
640+
code.includes('__TANSTACK_DEVTOOLS_PROTOCOL__')
641+
if (!hasPlaceholder) return
618642
if (
619643
!id.includes('@tanstack/devtools') &&
620644
!id.includes('@tanstack/event-bus')
621645
)
622646
return
623647

624-
// Replace placeholder with actual port (or fallback to 4206 if not resolved yet)
648+
// Replace placeholders with actual values (or fallback defaults)
625649
const portValue = devtoolsPort ?? 4206
626-
return code.replace(/__TANSTACK_DEVTOOLS_PORT__/g, String(portValue))
650+
const hostValue = devtoolsHost ?? 'localhost'
651+
const protocolValue = devtoolsProtocol ?? 'http'
652+
653+
let result = code
654+
result = result.replace(
655+
/__TANSTACK_DEVTOOLS_PORT__/g,
656+
String(portValue),
657+
)
658+
result = result.replace(
659+
/__TANSTACK_DEVTOOLS_HOST__/g,
660+
JSON.stringify(hostValue),
661+
)
662+
result = result.replace(
663+
/__TANSTACK_DEVTOOLS_PROTOCOL__/g,
664+
JSON.stringify(protocolValue),
665+
)
666+
return result
627667
},
628668
},
629669
]

packages/devtools-vite/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const handleDevToolsViteRequest = (
128128
options.onOpenSource?.(parsedData)
129129
} catch (e) {}
130130
res.write('OK')
131+
res.end()
131132
})
132133
}
133134

0 commit comments

Comments
 (0)