@@ -32,7 +32,8 @@ const WS_COMMAND = {
3232 RESPONSE : "response" ,
3333 LARGE_DATA_SOCKET_ANNOUNCE : "largeDataSock" ,
3434 CONTROL_SOCKET_ANNOUNCE : "controlSock" ,
35- GET_WINDOWS_DRIVES : "getWinDrives"
35+ GET_WINDOWS_DRIVES : "getWinDrives" ,
36+ READ_DIR : "readDir"
3637} ;
3738
3839// each browser context belongs to a single socket group. So multiple websocket connections can be pooled
@@ -46,22 +47,20 @@ const SOCKET_TYPE_DATA = "data",
4647const LARGE_DATA_THRESHOLD = 2 * 1024 * 1024 ; // 2MB
4748const MAX_RECONNECT_BACKOFF_TIME_MS = 1000 ;
4849
49- function mapNodeTauriErrorMessage ( nodeErrorMessage , path , userMessage = '' ) {
50- switch ( nodeErrorMessage ) {
51- case '2' : return new Errors . ENOENT ( userMessage + ` No such File or Directory: ` + path + nodeErrorMessage , path ) ;
52- case '3' : return new Errors . ENOENT ( userMessage + ` System cannot find the path specified: ` + path + nodeErrorMessage , path ) ; // windows
53- case '17' : return new Errors . EEXIST ( userMessage + ` File exists: ` + path + nodeErrorMessage , path ) ;
54- case '183' : return new Errors . EEXIST ( userMessage + ` File exists: ` + path + nodeErrorMessage , path ) ; // windows
55- case '39' : return new Errors . ENOTEMPTY ( userMessage + ` Directory not empty: ` + path + nodeErrorMessage , path ) ;
56- case '20' : return new Errors . ENOTDIR ( userMessage + ` Not a Directory: ` + path + nodeErrorMessage , path ) ;
57- case '13' : return new Errors . EACCES ( userMessage + ` Permission denied: ` + path + nodeErrorMessage , path ) ;
58- case '21' : return new Errors . EISDIR ( userMessage + ` Is a directory: ` + path + nodeErrorMessage , path ) ;
59- case '9' : return new Errors . EBADF ( userMessage + ` Bad file number: ` + path + nodeErrorMessage , path ) ;
60- case '30' : return new Errors . EROFS ( userMessage + ` Read-only file system: ` + path + nodeErrorMessage , path ) ;
61- case '28' : return new Errors . ENOSPC ( userMessage + ` No space left on device: ` + path + nodeErrorMessage , path ) ;
62- case '16' : return new Errors . EBUSY ( userMessage + ` Device or resource busy: ` + path + nodeErrorMessage , path ) ;
63- case '22' : return new Errors . EINVAL ( userMessage + ` Invalid argument: ` + path + nodeErrorMessage , path ) ;
64- default : return new Errors . EIO ( userMessage + ` IO error on path: ` + path + nodeErrorMessage , path ) ;
50+ function mapNodeTauriErrorMessage ( nodeError , path , userMessage = '' ) {
51+ switch ( nodeError . code ) {
52+ case 'ENOENT' : return new Errors . ENOENT ( userMessage + ` No such File or Directory: ` + path + nodeError . message , path ) ;
53+ case 'EEXIST' : return new Errors . EEXIST ( userMessage + ` File exists: ` + path + nodeError . message , path ) ;
54+ case '39' : return new Errors . ENOTEMPTY ( userMessage + ` Directory not empty: ` + path + nodeError . message , path ) ;
55+ case '20' : return new Errors . ENOTDIR ( userMessage + ` Not a Directory: ` + path + nodeError . message , path ) ;
56+ case '13' : return new Errors . EACCES ( userMessage + ` Permission denied: ` + path + nodeError . message , path ) ;
57+ case '21' : return new Errors . EISDIR ( userMessage + ` Is a directory: ` + path + nodeError . message , path ) ;
58+ case '9' : return new Errors . EBADF ( userMessage + ` Bad file number: ` + path + nodeError . message , path ) ;
59+ case '30' : return new Errors . EROFS ( userMessage + ` Read-only file system: ` + path + nodeError . message , path ) ;
60+ case '28' : return new Errors . ENOSPC ( userMessage + ` No space left on device: ` + path + nodeError . message , path ) ;
61+ case '16' : return new Errors . EBUSY ( userMessage + ` Device or resource busy: ` + path + nodeError . message , path ) ;
62+ case '22' : return new Errors . EINVAL ( userMessage + ` Invalid argument: ` + path + nodeError . message , path ) ;
63+ default : return new Errors . EIO ( userMessage + ` IO error on path: ` + path + nodeError . message + "\nNode Error stack: " + nodeError . stack , path ) ;
6564 }
6665}
6766
@@ -111,7 +110,7 @@ function _execCommandInternal(command, commandData, binaryData, resolve, reject)
111110 * @param binaryData {ArrayBuffer}
112111 * @returns {Promise<{metadata: Object, bufferData: ArrayBuffer}> } A promise that resolves with an object containing `metadata` and `bufferData` or rejects with error.
113112 */
114- function _execCommand ( command , commandData , binaryData ) {
113+ function _execCommand ( command , commandData = null , binaryData = undefined ) {
115114 return new Promise ( ( resolve , reject ) => {
116115 if ( ! _isSocketOpen ( controlSocket ) && ! _isSocketOpen ( dataSocket ) ) {
117116 _pendingCommandQueue . push ( { command, commandData, binaryData, resolve, reject} ) ;
@@ -290,23 +289,48 @@ function testNodeWsEndpoint(wsEndPoint, echoData, echoBuffer) {
290289 } ) ;
291290}
292291
292+ function _readDirWindowsDrives ( path , options , callback ) {
293+ _execCommand ( WS_COMMAND . GET_WINDOWS_DRIVES )
294+ . then ( ( { metadata} ) => {
295+ const drives = metadata . data . drives ;
296+ if ( ! options . withFileTypes ) {
297+ callback ( null , drives ) ;
298+ return ;
299+ }
300+ let entries = [ ] ;
301+ for ( let drive of drives ) {
302+ entries . push ( Utils . createDummyStatObject ( `${ path } /${ drive } ` , true , Constants . TAURI_WS_DEVICE_NAME ) ) ;
303+ }
304+ callback ( null , entries ) ;
305+ } )
306+ . catch ( ( err ) => {
307+ callback ( mapNodeTauriErrorMessage ( err , path , 'Failed to get drives: ' ) ) ;
308+ } ) ;
309+ }
310+
293311function readdir ( path , options , callback ) {
294312 if ( IS_WINDOWS && path === Constants . TAURI_ROOT ) {
295- _execCommand ( WS_COMMAND . GET_WINDOWS_DRIVES )
296- . then ( drives => {
297- console . log ( drives ) ;
298- // let entries = [];
299- // for(let drive of drives) {
300- // entries.push({name: drive});
301- // }
302- //_readDirHelper(entries, path, options, callback, true);
303- // console.log(entries);
304- } )
305- . catch ( ( err ) => {
306- callback ( mapNodeTauriErrorMessage ( err , path , 'Failed to get drives: ' ) ) ;
307- } ) ;
313+ _readDirWindowsDrives ( path , options , callback ) ;
308314 return ;
309315 }
316+ let platformPath = Utils . getTauriPlatformPath ( path ) ;
317+ _execCommand ( WS_COMMAND . READ_DIR , { path : platformPath , options} )
318+ . then ( ( { metadata} ) => {
319+ if ( metadata . data . contents ) {
320+ callback ( null , metadata . data . contents ) ;
321+ } else if ( metadata . data . contentStats ) {
322+ let stats = [ ] ;
323+ for ( let contentStat of metadata . data . contentStats ) {
324+ stats . push ( Utils . createFromNodeStat ( `${ path } /${ contentStat . name } ` , contentStat ) ) ;
325+ }
326+ callback ( null , stats ) ;
327+ } else {
328+ callback ( new Errors . EIO ( "Failed readdir as node ws connector returned empty" , path ) ) ;
329+ }
330+ } )
331+ . catch ( ( err ) => {
332+ callback ( mapNodeTauriErrorMessage ( err , path , 'Failed to read directory: ' ) ) ;
333+ } ) ;
310334}
311335
312336const NodeTauriFS = {
0 commit comments