Skip to content

Commit 53c6eda

Browse files
committed
feat: automatically switch to node endpoint when it becomes available with fs.preferNodeWSEndpoint
1 parent 2089ab6 commit 53c6eda

6 files changed

Lines changed: 150 additions & 57 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,3 +868,34 @@ open across failures and automatically reconnects as necessary.
868868
- **Returns:**
869869
- Promise<void>
870870

871+
872+
### `fs.forceUseNodeWSEndpoint(use)`
873+
874+
Forces the usage of the Node WebSocket endpoint.
875+
Throws an error if the Node WebSocket endpoint is not set.
876+
877+
- **Parameters**
878+
- `use`: Boolean
879+
- If `true`, forces the use of the Node WebSocket endpoint.
880+
881+
- **Throws**
882+
- Throws an error if the Node WebSocket endpoint has not been set.
883+
- Call `fs.setNodeWSEndpoint(websocketEndpoint)` before calling this API.
884+
885+
---
886+
887+
### `fs.preferNodeWSEndpoint(use)`
888+
889+
Sets the preference to use the Node WebSocket endpoint if available.
890+
Throws an error if the Node WebSocket endpoint is not set.
891+
If a Node connection is not available, it falls back to Tauri.
892+
To always force the library to use the Node WebSocket endpoint for all FS APIs, use `fs.forceUseNodeWSEndpoint`.
893+
894+
- **Parameters**
895+
- `use`: Boolean
896+
- If `true`, prefers the use of the Node WebSocket endpoint when available.
897+
898+
- **Throws**
899+
- Throws an error if the Node WebSocket endpoint has not been set.
900+
- Call `fs.setNodeWSEndpoint(websocketEndpoint)` before calling this this API.
901+

dist/virtualfs.js

Lines changed: 67 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/virtualfs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fslib.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ const fileSystemLib = {
333333
forceUseNodeWSEndpoint: function (use) {
334334
return TauriFS.forceUseNodeWSEndpoint(use);
335335
},
336+
preferNodeWSEndpoint: function (use) {
337+
return TauriFS.preferNodeWSEndpoint(use);
338+
},
336339
BYTE_ARRAY_ENCODING: Constants.BYTE_ARRAY_ENCODING,
337340
MOUNT_POINT_ROOT: Constants.MOUNT_POINT_ROOT,
338341
TAURI_ROOT: Constants.TAURI_ROOT,

src/fslib_node_ws.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ function _isSocketOpen(socket) {
100100
return socket && socket.readyState === WebSocket.OPEN;
101101
}
102102

103+
let isNodeWsConnected = false;
104+
105+
/**
106+
* Returns true if the node websocket connection is established and ready to take requests.
107+
* @returns {boolean}
108+
*/
109+
function isNodeWSReady() {
110+
return isNodeWsConnected;
111+
}
112+
113+
function updateConnectionState() {
114+
isNodeWsConnected = _isSocketOpen(controlSocket) || _isSocketOpen(dataSocket);
115+
}
116+
103117
const _pendingCommandQueue = [],
104118
commandIdMap = {};
105119

@@ -188,6 +202,7 @@ async function _establishAndMaintainConnection(socketType, firstConnectCB) {
188202
.catch(console.error);
189203
}
190204
_execPendingCommands();
205+
updateConnectionState();
191206
});
192207

193208
ws.addEventListener('message', function (event) {
@@ -200,6 +215,7 @@ async function _establishAndMaintainConnection(socketType, firstConnectCB) {
200215

201216
ws.addEventListener('close', function () {
202217
wsClosePromiseResolve();
218+
updateConnectionState();
203219
});
204220
await wsClosePromise;
205221
const backoffTime = Math.min(ws.backoffTime * 2, MAX_RECONNECT_BACKOFF_TIME_MS) || 1;
@@ -383,6 +399,7 @@ const NodeTauriFS = {
383399
setNodeWSEndpoint,
384400
stopNodeWSEndpoint,
385401
getNodeWSEndpoint,
402+
isNodeWSReady,
386403
readdir,
387404
stat,
388405
readBinaryFile,

src/fslib_tauri.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const {NodeTauriFS} = require("./fslib_node_ws");
2828

2929
const TAURI_PATH_PREFIX = Constants.TAURI_ROOT+ '/';
3030
const 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
*/
368369
function 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+
*/
591599
function 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

615640
module.exports ={

0 commit comments

Comments
 (0)