11import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/client'
2- import type { BirpcOptions , BirpcReturn } from 'birpc'
2+ import type { BirpcReturn , EventOptions } from 'birpc'
33import type { ConnectionMeta , DevToolsRpcClientFunctions , DevToolsRpcServerFunctions } from '../types'
44import type { DevToolsClientContext , DevToolsClientRpcHost } from './docks'
55import { createRpcClient } from '@vitejs/devtools-rpc'
66import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client'
77import { RpcFunctionsCollectorBase } from 'birpc-x'
8+ import { nanoid } from 'nanoid'
89
910const CONNECTION_META_KEY = '__VITE_DEVTOOLS_CONNECTION_META__'
11+ const CONNECTION_AUTH_ID_KEY = '__VITE_DEVTOOLS_CONNECTION_AUTH_ID__'
1012
1113function isNumeric ( str : string | number | undefined ) {
1214 if ( str == null )
@@ -18,7 +20,7 @@ export interface DevToolsRpcClientOptions {
1820 connectionMeta ?: ConnectionMeta
1921 baseURL ?: string [ ]
2022 wsOptions ?: Partial < WebSocketRpcClientOptions >
21- rpcOptions ?: Partial < BirpcOptions < DevToolsRpcServerFunctions > >
23+ rpcOptions ?: EventOptions < DevToolsRpcServerFunctions , DevToolsRpcClientFunctions >
2224}
2325
2426export type DevToolsRpcClient = BirpcReturn < DevToolsRpcServerFunctions , DevToolsRpcClientFunctions >
@@ -27,13 +29,39 @@ export interface ClientRpcReturn {
2729 connectionMeta : ConnectionMeta
2830 rpc : DevToolsRpcClient
2931 clientRpc : DevToolsClientRpcHost
32+ readonly isTrusted : boolean
33+ }
34+
35+ function getConnectionAuthIdFromWindows ( ) : string {
36+ const getters = [
37+ ( ) => localStorage . getItem ( CONNECTION_AUTH_ID_KEY ) ,
38+ ( ) => ( window as any ) ?. [ CONNECTION_AUTH_ID_KEY ] ,
39+ ( ) => ( globalThis as any ) ?. [ CONNECTION_AUTH_ID_KEY ] ,
40+ ( ) => ( parent . window as any ) ?. [ CONNECTION_AUTH_ID_KEY ] ,
41+ ]
42+
43+ for ( const getter of getters ) {
44+ try {
45+ const value = getter ( )
46+ if ( value ) {
47+ if ( ! localStorage . getItem ( CONNECTION_AUTH_ID_KEY ) )
48+ localStorage . setItem ( CONNECTION_AUTH_ID_KEY , value )
49+ return value
50+ }
51+ }
52+ catch { }
53+ }
54+
55+ const uid = nanoid ( )
56+ localStorage . setItem ( CONNECTION_AUTH_ID_KEY , uid )
57+ return uid
3058}
3159
3260function findConnectionMetaFromWindows ( ) : ConnectionMeta | undefined {
3361 const getters = [
34- ( ) => ( window as any ) [ CONNECTION_META_KEY ] ,
35- ( ) => ( globalThis as any ) [ CONNECTION_META_KEY ] ,
36- ( ) => ( parent . window as any ) [ CONNECTION_META_KEY ] ,
62+ ( ) => ( window as any ) ?. [ CONNECTION_META_KEY ] ,
63+ ( ) => ( globalThis as any ) ?. [ CONNECTION_META_KEY ] ,
64+ ( ) => ( parent . window as any ) ?. [ CONNECTION_META_KEY ] ,
3765 ]
3866
3967 for ( const getter of getters ) {
@@ -83,22 +111,54 @@ export async function getDevToolsRpcClient(
83111 const context : DevToolsClientContext = {
84112 rpc : undefined ! ,
85113 }
114+ const authId = getConnectionAuthIdFromWindows ( )
115+
116+ let isTrusted = false
86117 const clientRpc : DevToolsClientRpcHost = new RpcFunctionsCollectorBase < DevToolsRpcClientFunctions , DevToolsClientContext > ( context )
118+
119+ // Builtin rpc functions
120+ clientRpc . register ( {
121+ name : 'vite:anonymous:trusted' ,
122+ type : 'event' ,
123+ handler : ( ) => {
124+ isTrusted = true
125+ return true
126+ } ,
127+ } )
128+
129+ // Create the RPC client
87130 const rpc = createRpcClient < DevToolsRpcServerFunctions , DevToolsRpcClientFunctions > (
88131 clientRpc . functions ,
89132 {
90133 preset : createWsRpcPreset ( {
91134 url,
92135 ...options . wsOptions ,
93136 } ) ,
94- rpcOptions,
137+ rpcOptions : {
138+ ...rpcOptions ,
139+ meta : {
140+ authId,
141+ get isTrusted ( ) {
142+ return isTrusted
143+ } ,
144+ } ,
145+ } ,
95146 } ,
96147 )
97148 // @ts -expect-error assign to readonly property
98149 context . rpc = rpc
99150
151+ // TODO: implement the trust logic
152+ rpc . $call ( 'vite:anonymous:auth' , {
153+ authId,
154+ ua : navigator . userAgent ,
155+ } )
156+
100157 return {
101158 connectionMeta,
159+ get isTrusted ( ) {
160+ return isTrusted
161+ } ,
102162 rpc,
103163 clientRpc,
104164 }
0 commit comments