@@ -9,6 +9,11 @@ import {
99} from "#vscode-jsonrpc/node" ;
1010import type { ChildProcess } from "node:child_process" ;
1111import type { Socket } from "node:net" ;
12+ import type {
13+ RootedDirectoryPath ,
14+ RootedFilePath ,
15+ RootedPath ,
16+ } from "../../ast/index.ts" ;
1217import type { FileSystemCallbacks } from "../fs.ts" ;
1318import {
1419 configureFileSystemCallbacks ,
@@ -142,24 +147,72 @@ export class Client {
142147 ) : void {
143148 if ( ! fs ) return ;
144149 for ( const name of configuration . callbackNames ) {
145- if ( name === "writeFile" ) {
146- const callback = fs . writeFile ;
147- if ( typeof callback !== "function" ) throw new Error ( "Invalid writeFile callback configuration" ) ;
148-
149- const requestType = new RequestType < { path : string ; data : string ; } , unknown , void > ( name ) ;
150- connection . onRequest ( requestType , ( arg : { path : string ; data : string ; } ) => {
151- return encodeFileSystemCallbackResult ( name , callback ( arg . path , arg . data ) ) ;
152- } ) ;
153-
154- continue ;
150+ switch ( name ) {
151+ case "readFile" : {
152+ const callback = fs . readFile ;
153+ if ( typeof callback !== "function" ) throw new Error ( "Invalid readFile callback configuration" ) ;
154+ connection . onRequest ( new RequestType < RootedFilePath , unknown , void > ( name ) , fileName => {
155+ return encodeFileSystemCallbackResult ( name , callback ( fileName ) ) ;
156+ } ) ;
157+ break ;
158+ }
159+ case "fileExists" : {
160+ const callback = fs . fileExists ;
161+ if ( typeof callback !== "function" ) throw new Error ( "Invalid fileExists callback configuration" ) ;
162+ connection . onRequest ( new RequestType < RootedFilePath , unknown , void > ( name ) , fileName => {
163+ return encodeFileSystemCallbackResult ( name , callback ( fileName ) ) ;
164+ } ) ;
165+ break ;
166+ }
167+ case "directoryExists" : {
168+ const callback = fs . directoryExists ;
169+ if ( typeof callback !== "function" ) throw new Error ( "Invalid directoryExists callback configuration" ) ;
170+ connection . onRequest ( new RequestType < RootedDirectoryPath , unknown , void > ( name ) , directoryName => {
171+ return encodeFileSystemCallbackResult ( name , callback ( directoryName ) ) ;
172+ } ) ;
173+ break ;
174+ }
175+ case "getAccessibleEntries" : {
176+ const callback = fs . getAccessibleEntries ;
177+ if ( typeof callback !== "function" ) throw new Error ( "Invalid getAccessibleEntries callback configuration" ) ;
178+ connection . onRequest ( new RequestType < RootedDirectoryPath , unknown , void > ( name ) , directoryName => {
179+ return encodeFileSystemCallbackResult ( name , callback ( directoryName ) ) ;
180+ } ) ;
181+ break ;
182+ }
183+ case "realpath" : {
184+ const callback = fs . realpath ;
185+ if ( typeof callback !== "function" ) throw new Error ( "Invalid realpath callback configuration" ) ;
186+ connection . onRequest ( new RequestType < RootedPath , unknown , void > ( name ) , path => {
187+ return encodeFileSystemCallbackResult ( name , callback ( path ) ) ;
188+ } ) ;
189+ break ;
190+ }
191+ case "stat" : {
192+ const callback = fs . stat ;
193+ if ( typeof callback !== "function" ) throw new Error ( "Invalid stat callback configuration" ) ;
194+ connection . onRequest ( new RequestType < RootedPath , unknown , void > ( name ) , path => {
195+ return encodeFileSystemCallbackResult ( name , callback ( path ) ) ;
196+ } ) ;
197+ break ;
198+ }
199+ case "writeFile" : {
200+ const callback = fs . writeFile ;
201+ if ( typeof callback !== "function" ) throw new Error ( "Invalid writeFile callback configuration" ) ;
202+ connection . onRequest ( new RequestType < { path : RootedFilePath ; data : string ; } , unknown , void > ( name ) , arg => {
203+ return encodeFileSystemCallbackResult ( name , callback ( arg . path , arg . data ) ) ;
204+ } ) ;
205+ break ;
206+ }
207+ case "removeFile" : {
208+ const callback = fs . removeFile ;
209+ if ( typeof callback !== "function" ) throw new Error ( "Invalid removeFile callback configuration" ) ;
210+ connection . onRequest ( new RequestType < RootedPath , unknown , void > ( name ) , path => {
211+ return encodeFileSystemCallbackResult ( name , callback ( path ) ) ;
212+ } ) ;
213+ break ;
214+ }
155215 }
156-
157- const callback = fs [ name ] ;
158- if ( typeof callback !== "function" ) throw new Error ( `Invalid ${ name } callback configuration` ) ;
159- const requestType = new RequestType < unknown , unknown , void > ( name ) ;
160- connection . onRequest ( requestType , ( arg : unknown ) => {
161- return encodeFileSystemCallbackResult ( name , callback ( arg as string ) ) ;
162- } ) ;
163216 }
164217 }
165218
0 commit comments