diff --git a/.changeset/calm-buses-connect.md b/.changeset/calm-buses-connect.md new file mode 100644 index 000000000..9f7d23893 --- /dev/null +++ b/.changeset/calm-buses-connect.md @@ -0,0 +1,5 @@ +--- +'@tanstack/devtools-event-bus': patch +--- + +Honor explicit client connection options when bundlers inject event bus defaults. diff --git a/packages/event-bus/src/client/client.ts b/packages/event-bus/src/client/client.ts index 038d3c2b4..97bc59dc2 100644 --- a/packages/event-bus/src/client/client.ts +++ b/packages/event-bus/src/client/client.ts @@ -89,18 +89,18 @@ export class ClientEventBus { this.#eventTarget.dispatchEvent(new CustomEvent('tanstack-connect-success')) } constructor({ - port = 4206, - host = 'localhost', - protocol = 'http', + port = getDefaultPort(4206), + host = getDefaultHost('localhost'), + protocol = getDefaultProtocol('http'), debug = false, connectToServerBus = false, }: ClientEventBusConfig = {}) { this.#debug = debug this.#broadcastChannel = new BroadcastChannel('tanstack-devtools') this.#eventSource = null - this.#port = getDefaultPort(port) - this.#host = getDefaultHost(host) - this.#protocol = getDefaultProtocol(protocol) + this.#port = port + this.#host = host + this.#protocol = protocol this.#socket = null this.#connectToServerBus = connectToServerBus this.#eventTarget = this.getGlobalTarget() diff --git a/packages/event-bus/tests/client.test.ts b/packages/event-bus/tests/client.test.ts index d1cebc82f..5d2de8ea4 100644 --- a/packages/event-bus/tests/client.test.ts +++ b/packages/event-bus/tests/client.test.ts @@ -101,6 +101,34 @@ describe('ClientEventBus', () => { expect(mockEventSourceInstances.length).toBe(0) bus.stop() }) + + it('should prefer explicit config over bundler-injected defaults', () => { + Object.assign(globalThis, { + __TANSTACK_DEVTOOLS_PORT__: 4206, + __TANSTACK_DEVTOOLS_HOST__: 'localhost', + __TANSTACK_DEVTOOLS_PROTOCOL__: 'http', + }) + + let bus: ClientEventBus | undefined + try { + bus = new ClientEventBus({ + connectToServerBus: true, + port: 443, + host: 'devtools.example.com', + protocol: 'https', + }) + bus.start() + + expect(mockWebSocketInstances[0].url).toBe( + 'wss://devtools.example.com:443/__devtools/ws', + ) + } finally { + bus?.stop() + Reflect.deleteProperty(globalThis, '__TANSTACK_DEVTOOLS_PORT__') + Reflect.deleteProperty(globalThis, '__TANSTACK_DEVTOOLS_HOST__') + Reflect.deleteProperty(globalThis, '__TANSTACK_DEVTOOLS_PROTOCOL__') + } + }) }) describe('connectWebSocket with protocol', () => {