@@ -28,7 +28,8 @@ const {NodeTauriFS} = require("./fslib_node_ws");
2828
2929const TAURI_PATH_PREFIX = Constants . TAURI_ROOT + '/' ;
3030const IS_WINDOWS = navigator . userAgent . includes ( 'Windows' ) ;
31- let preferNodeWs = false ;
31+ let preferNodeWs = false ,
32+ forceNodeWs = false ;
3233
3334/**
3435 * Check if the given path is a subpath of the '/tauri' folder.
@@ -251,7 +252,7 @@ function readdir(path, options, callback) {
251252 options = { } ;
252253 }
253254
254- if ( ! window . __TAURI__ || preferNodeWs ) {
255+ if ( ! window . __TAURI__ || forceNodeWs || ( preferNodeWs && NodeTauriFS . isNodeWSReady ( ) ) ) {
255256 return NodeTauriFS . readdir ( path , options , callback ) ;
256257 }
257258
@@ -367,7 +368,7 @@ function mkdirs(path, mode, recursive, callback) {
367368 */
368369function stat ( path , callback , options = { } ) {
369370 path = globalObject . path . normalize ( path ) ;
370- if ( ! window . __TAURI__ || preferNodeWs ) {
371+ if ( ! window . __TAURI__ || forceNodeWs || ( preferNodeWs && NodeTauriFS . isNodeWSReady ( ) ) ) {
371372 return NodeTauriFS . stat ( path , callback , options ) ;
372373 }
373374 _getTauriStat ( path )
@@ -485,7 +486,7 @@ function readFile(path, options, callback) {
485486 callback = arguments [ arguments . length - 1 ] ;
486487 options = Utils . validateFileOptions ( options , Constants . BINARY_ENCODING , 'r' ) ;
487488
488- if ( ! window . __TAURI__ || preferNodeWs ) {
489+ if ( ! window . __TAURI__ || forceNodeWs || ( preferNodeWs && NodeTauriFS . isNodeWSReady ( ) ) ) {
489490 NodeTauriFS . readBinaryFile ( path )
490491 . then ( contents => {
491492 // contents is Array buffer
@@ -562,7 +563,7 @@ function writeFile (path, data, options, callback) {
562563 }
563564 arrayBuffer = Utils . getEncodedArrayBuffer ( data , options . encoding ) ;
564565 }
565- if ( ! window . __TAURI__ || preferNodeWs ) {
566+ if ( ! window . __TAURI__ || forceNodeWs || ( preferNodeWs && NodeTauriFS . isNodeWSReady ( ) ) ) {
566567 NodeTauriFS . writeBinaryFile ( path , arrayBuffer )
567568 . then ( ( ) => {
568569 callback ( null ) ;
@@ -588,7 +589,30 @@ function writeFile (path, data, options, callback) {
588589 }
589590}
590591
592+ /**
593+ * Forces the usage of the Node WebSocket endpoint.
594+ * Throws an error if the Node WebSocket endpoint is not set.
595+ *
596+ * @param {boolean } use - If `true`, forces the use of the Node WebSocket endpoint.
597+ * @throws {Error } Throws an error if the Node WebSocket endpoint has not been set.
598+ */
591599function forceUseNodeWSEndpoint ( use ) {
600+ if ( ! NodeTauriFS . getNodeWSEndpoint ( ) ) {
601+ throw new Error ( "Please call fs.setNodeWSEndpoint('ws://your server') before calling this function." ) ;
602+ }
603+ forceNodeWs = use ;
604+ }
605+
606+ /**
607+ * Sets the preference to use the Node WebSocket endpoint if available.
608+ * Throws an error if the Node WebSocket endpoint is not set.
609+ * If a Node connection is not available, it falls back to Tauri.
610+ * To always force the library to use the Node WebSocket endpoint for all FS APIs, use `forceUseNodeWSEndpoint`.
611+ *
612+ * @param {boolean } use - If `true`, prefers the use of the Node WebSocket endpoint.
613+ * @throws {Error } Throws an error if the Node WebSocket endpoint has not been set.
614+ */
615+ function preferNodeWSEndpoint ( use ) {
592616 if ( ! NodeTauriFS . getNodeWSEndpoint ( ) ) {
593617 throw new Error ( "Please call fs.setNodeWSEndpoint('ws://your server') before calling this function." ) ;
594618 }
@@ -609,7 +633,8 @@ const TauriFS = {
609633 unlink,
610634 readFile,
611635 writeFile,
612- forceUseNodeWSEndpoint
636+ forceUseNodeWSEndpoint,
637+ preferNodeWSEndpoint
613638} ;
614639
615640module . exports = {
0 commit comments