Skip to content

Commit 2609073

Browse files
committed
feat(kit): support reading connection meta from windows
1 parent 17e3732 commit 2609073

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

packages/core/src/node/server.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ import { dirClientStandalone } from '../dirs'
55
import { createWsServer } from './ws'
66

77
export async function createDevToolsMiddleware(options: CreateWsServerOptions) {
8-
const app = createApp()
8+
const h3 = createApp()
99

10-
const { rpc, getConnectionMeta: getMetadata } = await createWsServer(options)
10+
const { rpc, getConnectionMeta } = await createWsServer(options)
1111

12-
app.use('/.vdt-connection.json', eventHandler(async (event) => {
12+
h3.use('/.vdt-connection.json', eventHandler(async (event) => {
1313
event.node.res.setHeader('Content-Type', 'application/json')
14-
return event.node.res.end(JSON.stringify(await getMetadata()))
14+
return event.node.res.end(JSON.stringify(await getConnectionMeta()))
1515
}))
1616

17-
app.use(fromNodeMiddleware(sirv(dirClientStandalone, {
17+
h3.use(fromNodeMiddleware(sirv(dirClientStandalone, {
1818
dev: true,
1919
single: true,
2020
})))
2121

2222
return {
23-
h3: app,
24-
middleware: toNodeListener(app),
23+
h3,
2524
rpc,
25+
middleware: toNodeListener(h3),
26+
getConnectionMeta,
2627
}
2728
}

packages/kit/src/client/rpc.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { createRpcClient } from '@vitejs/devtools-rpc'
66
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client'
77
import { RpcFunctionsCollectorBase } from 'birpc-x'
88

9+
const CONNECTION_META_KEY = '__VITE_DEVTOOLS_CONNECTION_META__'
10+
911
function isNumeric(str: string | number | undefined) {
1012
if (str == null)
1113
return false
@@ -27,30 +29,48 @@ export interface ClientRpcReturn {
2729
clientRpc: DevToolsClientRpcHost
2830
}
2931

32+
function findConnectionMetaFromWindows(): ConnectionMeta | undefined {
33+
const getters = [
34+
() => (window as any)[CONNECTION_META_KEY],
35+
() => (globalThis as any)[CONNECTION_META_KEY],
36+
() => (parent.window as any)[CONNECTION_META_KEY],
37+
]
38+
39+
for (const getter of getters) {
40+
try {
41+
const value = getter()
42+
if (value)
43+
return value
44+
}
45+
catch {}
46+
}
47+
}
48+
3049
export async function getDevToolsRpcClient(
3150
options: DevToolsRpcClientOptions = {},
3251
): Promise<ClientRpcReturn> {
3352
const {
3453
baseURL = '/.devtools/',
3554
rpcOptions = {},
3655
} = options
37-
const urls = Array.isArray(baseURL) ? baseURL : [baseURL]
38-
let connectionMeta: ConnectionMeta | undefined = options.connectionMeta
56+
const bases = Array.isArray(baseURL) ? baseURL : [baseURL]
57+
let connectionMeta: ConnectionMeta | undefined = options.connectionMeta || findConnectionMetaFromWindows()
3958

4059
if (!connectionMeta) {
4160
const errors: Error[] = []
42-
for (const url of urls) {
61+
for (const base of bases) {
4362
try {
44-
connectionMeta = await fetch(`${url}.vdt-connection.json`)
63+
connectionMeta = await fetch(`${base}.vdt-connection.json`)
4564
.then(r => r.json()) as ConnectionMeta
65+
;(globalThis as any)[CONNECTION_META_KEY] = connectionMeta
4666
break
4767
}
4868
catch (e) {
4969
errors.push(e as Error)
5070
}
5171
}
5272
if (!connectionMeta) {
53-
throw new Error(`Failed to get connection meta from ${urls.join(', ')}`, {
73+
throw new Error(`Failed to get connection meta from ${bases.join(', ')}`, {
5474
cause: errors,
5575
})
5676
}

0 commit comments

Comments
 (0)