1- import type { DevToolsNodeRpcSessionMeta } from '@vitejs/devtools-kit'
1+ import type { DevToolsNodeContext , DevToolsNodeRpcSessionMeta } from '@vitejs/devtools-kit'
22import type { BirpcGroup , BirpcOptions , ChannelOptions } from 'birpc'
33import type { WebSocket } from 'ws'
44import type { RpcServerPreset } from '..'
5+ import process from 'node:process'
56import { parse , stringify } from 'structured-clone-es'
67import { WebSocketServer } from 'ws'
78import { defineRpcServerPreset } from '..'
9+ import { getInternalContext } from '../../../../core/src/node/context-internal'
810
911export interface WebSocketRpcServerOptions {
1012 port : number
1113 host ?: string
14+ context : DevToolsNodeContext
1215 onConnected ?: ( ws : WebSocket , meta : DevToolsNodeRpcSessionMeta ) => void
1316 onDisconnected ?: ( ws : WebSocket , meta : DevToolsNodeRpcSessionMeta ) => void
1417}
@@ -32,27 +35,48 @@ export const createWsRpcPreset: RpcServerPreset<
3235 host = 'localhost' ,
3336 onConnected = NOOP ,
3437 onDisconnected = NOOP ,
38+ context,
3539 } = options
3640
41+ const isClientAuthDisabled = context . mode === 'build' || context . viteConfig . devtools ?. clientAuth === false || process . env . VITE_DEVTOOLS_DISABLE_CLIENT_AUTH === 'true'
42+ if ( isClientAuthDisabled ) {
43+ console . warn ( '[Vite DevTools] Client authentication is disabled. Any browser can connect to the devtools and access to your server and filesystem.' )
44+ }
45+
46+ const internal = getInternalContext ( context )
47+
3748 const wss = new WebSocketServer ( {
3849 port,
3950 host,
4051 } )
4152
4253 return < ClientFunctions extends object , ServerFunctions extends object > (
43- rpc : BirpcGroup < ClientFunctions , ServerFunctions , false > ,
54+ rpcGroup : BirpcGroup < ClientFunctions , ServerFunctions , false > ,
4455 options ?: Pick < BirpcOptions < ClientFunctions , ServerFunctions , false > , 'serialize' | 'deserialize' > ,
4556 ) => {
4657 const {
4758 serialize = stringify ,
4859 deserialize = parse ,
4960 } = options ?? { }
5061
51- wss . on ( 'connection' , ( ws ) => {
62+ wss . on ( 'connection' , ( ws , req ) => {
63+ const url = new URL ( req . url ?? '' , 'http://localhost' )
64+ const authId = url . searchParams . get ( 'vite_devtools_auth_id' ) ?? undefined
65+ let isTrusted = false
66+ if ( isClientAuthDisabled ) {
67+ isTrusted = true
68+ }
69+ else if ( authId && internal . storage . auth . get ( ) . trusted [ authId ] ) {
70+ isTrusted = true
71+ }
72+
5273 const meta : DevToolsNodeRpcSessionMeta = {
5374 id : id ++ ,
5475 ws,
76+ isTrusted,
77+ clientAuthId : authId ,
5578 }
79+
5680 const channel : ChannelOptions = {
5781 post : ( data ) => {
5882 ws . send ( data )
@@ -67,12 +91,14 @@ export const createWsRpcPreset: RpcServerPreset<
6791 meta,
6892 }
6993
70- rpc . updateChannels ( ( channels ) => {
94+ rpcGroup . updateChannels ( ( channels ) => {
7195 channels . push ( channel )
7296 } )
7397
98+ // const rpc = rpcGroup.clients.find(client => client.$meta.id === meta.id)
99+
74100 ws . on ( 'close' , ( ) => {
75- rpc . updateChannels ( ( channels ) => {
101+ rpcGroup . updateChannels ( ( channels ) => {
76102 const index = channels . indexOf ( channel )
77103 if ( index >= 0 )
78104 channels . splice ( index , 1 )
0 commit comments