Skip to content

Commit 651ea8b

Browse files
committed
fix: node tauri paths working in webworkers and tests
1 parent bc3450b commit 651ea8b

6 files changed

Lines changed: 72 additions & 20 deletions

File tree

dist/virtualfs.js

Lines changed: 6 additions & 6 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_tauri.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function mapOSTauriErrorMessage(tauriErrorMessage, path, userMessage= '') {
186186
}
187187

188188
async function _getTauriStat(vfsPath) {
189-
let stats = await window.__TAURI__.invoke("plugin:fs-extra|metadata", {
189+
let stats = await globalObject.__TAURI__.invoke("plugin:fs-extra|metadata", {
190190
path: globalObject.fs.getTauriPlatformPath(vfsPath)
191191
});
192192
return Utils.createFromTauriStat(vfsPath, stats);
@@ -252,7 +252,7 @@ function readdir(path, options, callback) {
252252
options = {};
253253
}
254254

255-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
255+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
256256
return NodeTauriFS.readdir(path, options, callback);
257257
}
258258

@@ -325,7 +325,7 @@ function mkdirs(path, mode, recursive, callback) {
325325
};
326326
}
327327

328-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
328+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
329329
NodeTauriFS.mkdirs(path, mode, recursive, callback);
330330
return;
331331
}
@@ -373,7 +373,7 @@ function mkdirs(path, mode, recursive, callback) {
373373
*/
374374
function stat(path, callback, options= {}) {
375375
path = globalObject.path.normalize(path);
376-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
376+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
377377
return NodeTauriFS.stat(path, callback, options);
378378
}
379379
_getTauriStat(path)
@@ -388,7 +388,7 @@ function stat(path, callback, options= {}) {
388388
function unlink(path, callback) {
389389
path = globalObject.path.normalize(path);
390390

391-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
391+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
392392
return NodeTauriFS.unlink(path, callback);
393393
}
394394

@@ -415,7 +415,7 @@ function rename(oldPath, newPath, callback) {
415415
oldPath = globalObject.path.normalize(oldPath);
416416
newPath = globalObject.path.normalize(newPath);
417417

418-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
418+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
419419
NodeTauriFS.rename(oldPath, newPath, callback);
420420
return;
421421
}
@@ -502,7 +502,7 @@ function readFile(path, options, callback) {
502502
callback = arguments[arguments.length - 1];
503503
options = Utils.validateFileOptions(options, Constants.BINARY_ENCODING, 'r');
504504

505-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
505+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
506506
NodeTauriFS.readBinaryFile(path)
507507
.then(contents => {
508508
// contents is Array buffer
@@ -580,7 +580,7 @@ function writeFile (path, data, options, callback) {
580580
}
581581
arrayBuffer = Utils.getEncodedArrayBuffer(data, options.encoding);
582582
}
583-
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
583+
if(!globalObject.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
584584
NodeTauriFS.writeBinaryFile(path, options.mode || 0o666, options.flag, arrayBuffer)
585585
.then(() => {
586586
callback(null);

test/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
}
6464
execNode(NODE_COMMANDS.GET_PORT)
6565
.then(message=>{
66+
window.nodeWSEndpoint = `ws://localhost:${message.port}/phoenixFS`;
6667
fs.setNodeWSEndpoint(`ws://localhost:${message.port}/phoenixFS`);
6768
window.isNodeSetup = true;
6869
});
@@ -79,7 +80,7 @@
7980

8081
<script class="mocha-init">
8182
mocha.setup('bdd');
82-
mocha.globals(['isNodeTerminated', 'isNodeSetup', 'execNode']);
83+
mocha.globals(['isNodeTerminated', 'isNodeSetup', 'execNode', 'nodeWSEndpoint']);
8384
mocha.checkLeaks();
8485
</script>
8586

test/test.worker.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
/* global expect,fs, waitForTrue, TEST_TYPE_FS_ACCESS, TEST_TYPE_FILER */
1+
/* global expect,fs, waitForTrue, TEST_TYPE_FS_ACCESS, TEST_TYPE_FILER, TEST_TYPE_TAURI_WS */
22

33
function _setupTests(testType) {
44
let testPath;
55
let worker;
66
let messageFromWorker = null;
77

8+
function consoleLogToShell(message) {
9+
return window.__TAURI__.invoke("console_log", {message});
10+
}
11+
812
async function _clean() {
913
console.log(`cleaning: `, testPath);
1014
let cleanSuccess = false;
@@ -17,7 +21,7 @@ function _setupTests(testType) {
1721
async function _init() {
1822
console.log(`mkdir: `, testPath);
1923
let cleanSuccess = false;
20-
fs.mkdirs(testPath, 777 ,true, ()=>{
24+
fs.mkdirs(testPath, 0o777 ,true, ()=>{
2125
cleanSuccess = true;
2226
});
2327
await waitForTrue(()=>{return cleanSuccess;},10000);
@@ -40,16 +44,22 @@ function _setupTests(testType) {
4044
});
4145
}
4246

43-
function _setupTestPath() {
47+
async function _setupTestPath() {
4448
switch (testType) {
4549
case TEST_TYPE_FS_ACCESS: testPath = window.mountTestPath;break;
4650
case TEST_TYPE_FILER: testPath = window.virtualTestPath;break;
51+
case TEST_TYPE_TAURI_WS:
52+
await window.waitForTrue(()=>{return window.isNodeSetup;},1000);
53+
fs.forceUseNodeWSEndpoint(true);
54+
testPath = fs.getTauriVirtualPath(`${await window.__TAURI__.path.appLocalDataDir()}test-phoenix-fs`);
55+
consoleLogToShell("using tauri websocket test path: "+ testPath);
56+
break;
4757
default: throw new Error("unknown file system impl");
4858
}
4959
}
5060

5161
before(async function () {
52-
_setupTestPath();
62+
await _setupTestPath();
5363
await _clean();
5464
await _init();
5565
await _requestWritePerm();
@@ -59,6 +69,12 @@ function _setupTests(testType) {
5969
console.log(`From Worker:`, event);
6070
messageFromWorker = event.data;
6171
};
72+
if(testType === TEST_TYPE_TAURI_WS){
73+
await window.waitForTrue(()=>{return window.isNodeSetup;},1000);
74+
worker.postMessage({command: `tauriWSInit`, wsEndpoint: window.nodeWSEndpoint});
75+
let status = await waitForWorkerMessage(`tauriWSInit.ok`, 1000);
76+
expect(status).to.be.true;
77+
}
6278
});
6379

6480
after(async function () {
@@ -138,12 +154,26 @@ function _setupTests(testType) {
138154
let status = await waitForWorkerMessage(`deleteCheck.ok`, 1000);
139155
expect(status).to.be.true;
140156
});
157+
158+
it(`Should phoenix ${testType} stat in worker`, async function () {
159+
await _writeFile();
160+
messageFromWorker = null;
161+
worker.postMessage({command: `checkStatInPath`, path: `${testPath}/workerWrite.txt`});
162+
let status = await waitForWorkerMessage(`checkStatInPath.ok`, 1000);
163+
expect(status).to.be.true;
164+
});
141165
}
142166

143167
describe(`web worker filer tests`, function () {
144168
_setupTests(TEST_TYPE_FILER);
145169
});
146170

171+
if(window.__TAURI__){
172+
describe(`web worker Tauri WS tests`, function () {
173+
_setupTests(TEST_TYPE_TAURI_WS);
174+
});
175+
}
176+
147177
if(window.supportsFsAccessAPIs) {
148178
describe(`web worker fs access tests`, function () {
149179
if(window.__TAURI__){

test/worker-task.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,33 @@ function checkDeleteInPath(path) {
5757
});
5858
}
5959

60+
function checkStatInPath(path) {
61+
console.log('worker: checkStatInPath');
62+
fs.stat(path, (err, stat)=>{
63+
if(!err && stat.dev.startsWith("tauriWS")){
64+
postMessage('checkStatInPath.ok');
65+
return;
66+
}
67+
console.error('Stats error/not as expected:', err, stat);
68+
});
69+
}
70+
71+
async function tauriWSInit(wsEndpoint) {
72+
if(Filer && Filer.fs && Filer.Shell){
73+
await fs.setNodeWSEndpoint(wsEndpoint);
74+
postMessage('tauriWSInit.ok');
75+
}
76+
}
77+
6078
self.addEventListener('message', (event) => {
6179
console.log('Worker received: ', event);
6280
const command = event.data.command;
6381
const path = event.data.path;
82+
const wsEndpoint = event.data.wsEndpoint;
6483
switch (command) {
6584
case 'fsCheck': fsCheck(); break;
85+
case 'checkStatInPath': checkStatInPath(path); break;
86+
case 'tauriWSInit': tauriWSInit(wsEndpoint); break;
6687
case 'phoenixFsCheck': phoenixFsCheck(); break;
6788
case 'writeCheck': checkWriteInPath(path); break;
6889
case 'readCheck': checkReadInPath(path); break;

0 commit comments

Comments
 (0)