@@ -40,12 +40,16 @@ export function updatePipeTable(servers: NamedPipeServer[]) {
4040export function connect ( path : string ) {
4141 return new Promise < net . Socket | undefined > ( resolve => {
4242 const client = net . connect ( path ) ;
43+ client . setTimeout ( 1000 ) ;
4344 client . on ( 'connect' , ( ) => {
4445 resolve ( client ) ;
4546 } ) ;
4647 client . on ( 'error' , ( ) => {
4748 return resolve ( undefined ) ;
4849 } ) ;
50+ client . on ( 'timeout' , ( ) => {
51+ return resolve ( undefined ) ;
52+ } ) ;
4953 } ) ;
5054}
5155
@@ -56,7 +60,7 @@ export async function searchNamedPipeServerForFile(fileName: string) {
5660 const inferredServers = servers
5761 . filter ( item => item . serverKind === 0 satisfies ts . server . ProjectKind . Inferred )
5862 . sort ( ( a , b ) => b . currentDirectory . length - a . currentDirectory . length ) ;
59- for ( const server of configuredServers ) {
63+ for ( const server of configuredServers . sort ( ( a , b ) => sortTSConfigs ( fileName , a . currentDirectory , b . currentDirectory ) ) ) {
6064 const client = await connect ( server . path ) ;
6165 if ( client ) {
6266 const projectInfo = await sendRequestWorker < { name : string ; kind : ts . server . ProjectKind ; } > ( { type : 'projectInfoForFile' , args : [ fileName ] } , client ) ;
@@ -81,9 +85,32 @@ export async function searchNamedPipeServerForFile(fileName: string) {
8185 }
8286}
8387
88+ function sortTSConfigs ( file : string , a : string , b : string ) {
89+
90+ const inA = isFileInDir ( file , path . dirname ( a ) ) ;
91+ const inB = isFileInDir ( file , path . dirname ( b ) ) ;
92+
93+ if ( inA !== inB ) {
94+ const aWeight = inA ? 1 : 0 ;
95+ const bWeight = inB ? 1 : 0 ;
96+ return bWeight - aWeight ;
97+ }
98+
99+ const aLength = a . split ( '/' ) . length ;
100+ const bLength = b . split ( '/' ) . length ;
101+
102+ return bLength - aLength ;
103+ }
104+
105+ function isFileInDir ( fileName : string , dir : string ) {
106+ const relative = path . relative ( dir , fileName ) ;
107+ return ! ! relative && ! relative . startsWith ( '..' ) && ! path . isAbsolute ( relative ) ;
108+ }
109+
84110export function sendRequestWorker < T > ( request : Request , client : net . Socket ) {
85111 return new Promise < T | undefined | null > ( resolve => {
86112 let dataChunks : Buffer [ ] = [ ] ;
113+ client . setTimeout ( 5000 ) ;
87114 client . on ( 'data' , chunk => {
88115 dataChunks . push ( chunk ) ;
89116 } ) ;
@@ -105,6 +132,14 @@ export function sendRequestWorker<T>(request: Request, client: net.Socket) {
105132 }
106133 resolve ( json ) ;
107134 } ) ;
135+ client . on ( 'error' , err => {
136+ console . error ( '[Vue Named Pipe Client] Error:' , err . message ) ;
137+ resolve ( undefined ) ;
138+ } ) ;
139+ client . on ( 'timeout' , ( ) => {
140+ console . error ( '[Vue Named Pipe Client] Timeout' ) ;
141+ resolve ( undefined ) ;
142+ } ) ;
108143 client . write ( JSON . stringify ( request ) ) ;
109144 } ) ;
110145}
0 commit comments