Skip to content

Commit 7954a04

Browse files
committed
feat: fs.mkdir, fs.mkdirs api and tests for node ws apis working
1 parent 53c6eda commit 7954a04

9 files changed

Lines changed: 102 additions & 48 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ Creates a directory at given path. Not that the parent dir should exist for this
566566

567567
- **Parameters:**
568568
- `path` _(string)_ - The path where the directory should be created.
569-
- `mode` _(number|function)_ (Optional, default: `0o777`) - The directory permissions.
569+
- `mode` _(number|function)_ (Optional, default: `0o666`) - The directory permissions.
570570
- `callback` _(function)_ (Optional) - Callback to execute once directory creation is done. Called with an error as the first argument on failure, and null on success.
571571

572572
- **Examples:**
@@ -591,7 +591,7 @@ Creates a directory with optional mode and recursion (create all intermediate di
591591

592592
- **Parameters:**
593593
- `path` _(string)_ - The path where the directory should be created.
594-
- `mode` _(number|function)_ (Optional, default: `0o777`) - The directory permissions.
594+
- `mode` _(number|function)_ (Optional, default: `0o666`) - The directory permissions.
595595
- `recursive` _(boolean|function)_ (Optional, default: `false`) - Whether to create directories recursively.
596596
- `callback` _(function)_ (Optional) - Callback to execute once directory creation is done. Called with an error as the first argument on failure, and null on success.
597597

@@ -836,6 +836,7 @@ Writes data to a file, replacing the file if it already exists.
836836
- If provided as an `object`, it can have the following properties:
837837
- `encoding` (`string`): The type of encoding. Default is `'binary'`.
838838
- `flag` (`string`): The file system flag. Default is `'w'`.
839+
- `mode` (`number`): (Optional, default: `0o666`) - The permissions.
839840

840841
- **callback**: (`function`)
841842
- The callback function executed once the file write operation concludes.

dist/phoenix-fs.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ const WS_COMMAND = {
106106
READ_DIR: "readDir",
107107
STAT: "stat",
108108
READ_BIN_FILE: "readBinFile",
109-
WRITE_BIN_FILE: "writeBinFile"
109+
WRITE_BIN_FILE: "writeBinFile",
110+
MKDIR: "mkdir"
110111
};
111112

112113
const LARGE_DATA_THRESHOLD = 2*1024*1024; // 2MB
@@ -229,8 +230,20 @@ function _readBinaryFile(ws, metadata) {
229230
* @private
230231
*/
231232
function _writeBinaryFile(ws, metadata, dataBuffer) {
232-
const fullPath = metadata.data.path;
233-
fs.writeFile(fullPath, Buffer.from(dataBuffer))
233+
const fullPath = metadata.data.path,
234+
mode = metadata.data.mode,
235+
flag = metadata.data.flag;
236+
fs.writeFile(fullPath, Buffer.from(dataBuffer), {mode, flag})
237+
.then( ()=> {
238+
_sendResponse(ws, metadata);
239+
}).catch((err)=>_reportError(ws, metadata, err, `Failed to write file at path ${fullPath}`));
240+
}
241+
242+
function _mkdir(ws, metadata, dataBuffer) {
243+
const fullPath = metadata.data.path,
244+
recursive = metadata.data.recursive,
245+
mode = metadata.data.mode;
246+
fs.mkdir(fullPath, {recursive, mode})
234247
.then( ()=> {
235248
_sendResponse(ws, metadata);
236249
}).catch((err)=>_reportError(ws, metadata, err, `Failed to write file at path ${fullPath}`));
@@ -260,6 +273,9 @@ function processWSCommand(ws, metadata, dataBuffer) {
260273
case WS_COMMAND.WRITE_BIN_FILE:
261274
_writeBinaryFile(ws, metadata, dataBuffer);
262275
return;
276+
case WS_COMMAND.MKDIR:
277+
_mkdir(ws, metadata, dataBuffer);
278+
return;
263279
case WS_COMMAND.LARGE_DATA_SOCKET_ANNOUNCE:
264280
console.log("Large Data Transfer Socket established, socket Group: ", metadata.socketGroupID);
265281
ws.isLargeData = true;

dist/virtualfs.js

Lines changed: 23 additions & 22 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-tauri/node-src/phoenix-fs.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ const WS_COMMAND = {
106106
READ_DIR: "readDir",
107107
STAT: "stat",
108108
READ_BIN_FILE: "readBinFile",
109-
WRITE_BIN_FILE: "writeBinFile"
109+
WRITE_BIN_FILE: "writeBinFile",
110+
MKDIR: "mkdir"
110111
};
111112

112113
const LARGE_DATA_THRESHOLD = 2*1024*1024; // 2MB
@@ -229,8 +230,20 @@ function _readBinaryFile(ws, metadata) {
229230
* @private
230231
*/
231232
function _writeBinaryFile(ws, metadata, dataBuffer) {
232-
const fullPath = metadata.data.path;
233-
fs.writeFile(fullPath, Buffer.from(dataBuffer))
233+
const fullPath = metadata.data.path,
234+
mode = metadata.data.mode,
235+
flag = metadata.data.flag;
236+
fs.writeFile(fullPath, Buffer.from(dataBuffer), {mode, flag})
237+
.then( ()=> {
238+
_sendResponse(ws, metadata);
239+
}).catch((err)=>_reportError(ws, metadata, err, `Failed to write file at path ${fullPath}`));
240+
}
241+
242+
function _mkdir(ws, metadata, dataBuffer) {
243+
const fullPath = metadata.data.path,
244+
recursive = metadata.data.recursive,
245+
mode = metadata.data.mode;
246+
fs.mkdir(fullPath, {recursive, mode})
234247
.then( ()=> {
235248
_sendResponse(ws, metadata);
236249
}).catch((err)=>_reportError(ws, metadata, err, `Failed to write file at path ${fullPath}`));
@@ -260,6 +273,9 @@ function processWSCommand(ws, metadata, dataBuffer) {
260273
case WS_COMMAND.WRITE_BIN_FILE:
261274
_writeBinaryFile(ws, metadata, dataBuffer);
262275
return;
276+
case WS_COMMAND.MKDIR:
277+
_mkdir(ws, metadata, dataBuffer);
278+
return;
263279
case WS_COMMAND.LARGE_DATA_SOCKET_ANNOUNCE:
264280
console.log("Large Data Transfer Socket established, socket Group: ", metadata.socketGroupID);
265281
ws.isLargeData = true;

src/fslib_node_ws.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const WS_COMMAND = {
3636
READ_DIR: "readDir",
3737
STAT: "stat",
3838
READ_BIN_FILE: "readBinFile",
39-
WRITE_BIN_FILE: "writeBinFile"
39+
WRITE_BIN_FILE: "writeBinFile",
40+
MKDIR: "mkdir"
4041
};
4142

4243
// each browser context belongs to a single socket group. So multiple websocket connections can be pooled
@@ -383,24 +384,37 @@ function readBinaryFile(path) {
383384
});
384385
}
385386

386-
function writeBinaryFile(path, dataArrayBuffer) {
387+
function writeBinaryFile(path, mode, flag, dataArrayBuffer) {
387388
return new Promise((resolve, reject)=>{
388389
const platformPath = Utils.getTauriPlatformPath(path);
389-
_execCommand(WS_COMMAND.WRITE_BIN_FILE, {path: platformPath}, dataArrayBuffer)
390+
_execCommand(WS_COMMAND.WRITE_BIN_FILE, {path: platformPath, mode, flag}, dataArrayBuffer)
390391
.then(resolve)
391392
.catch((err)=>{
392393
reject(mapNodeTauriErrorMessage(err, path, 'Failed to write file: '));
393394
});
394395
});
395396
}
396397

398+
function mkdirs(path, mode, recursive, callback) {
399+
const platformPath = Utils.getTauriPlatformPath(path);
400+
_execCommand(WS_COMMAND.MKDIR, {path: platformPath, mode, recursive})
401+
.then(()=>{
402+
console.log("dir created: ", platformPath);
403+
callback(null);
404+
})
405+
.catch((err)=>{
406+
callback(mapNodeTauriErrorMessage(err, path, 'Failed to mkdir: '));
407+
});
408+
}
409+
397410
const NodeTauriFS = {
398411
testNodeWsEndpoint,
399412
setNodeWSEndpoint,
400413
stopNodeWSEndpoint,
401414
getNodeWSEndpoint,
402415
isNodeWSReady,
403416
readdir,
417+
mkdirs,
404418
stat,
405419
readBinaryFile,
406420
writeBinaryFile

src/fslib_tauri.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ function readdir(path, options, callback) {
285285
* Creates a directory with optional mode and recursion(create all intermediate directories if those don't exist).
286286
*
287287
* @param {string} path - The path where the directory should be created.
288-
* @param {(number|function)} [mode=0o777] - The directory permissions. Defaults to `0o777` if not provided.
289-
* @param {(boolean|function)} [recursive=false] - Whether to create directories recursively. Defaults to `false` if not provided.
288+
* @param {(number)} [mode=0o666] - The directory permissions. Defaults to `0o666` if not provided.
289+
* @param {(boolean)} [recursive=false] - Whether to create directories recursively. Defaults to `false` if not provided.
290290
* @param {function} [callback] - Callback to execute once directory creation is done. Called with an error as the first argument on failure, and null on success.
291291
*
292292
* @example
@@ -309,7 +309,7 @@ function mkdirs(path, mode, recursive, callback) {
309309
if (typeof mode !== 'number') {
310310
callback = recursive;
311311
recursive = mode;
312-
mode = 0o777; // Default mode (or any other default you'd like to set)
312+
mode = 0o666; // Default mode (or any other default you'd like to set)
313313
}
314314

315315
// Determine if 'recursive' is provided
@@ -325,6 +325,11 @@ function mkdirs(path, mode, recursive, callback) {
325325
};
326326
}
327327

328+
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
329+
NodeTauriFS.mkdirs(path, mode, recursive, callback);
330+
return;
331+
}
332+
328333
let platformPath = Utils.getTauriPlatformPath(path);
329334
__TAURI__.fs.createDir(platformPath, {recursive})
330335
.then(()=>{
@@ -527,6 +532,7 @@ function readFile(path, options, callback) {
527532
* - If provided as an `object`, it can have the following keys:
528533
* - `encoding` (string): The type of encoding. Default is `'binary'`.
529534
* - `flag` (string): The file system flag. Default is `'w'`.
535+
* - `mode`- The permissions. Defaults to `0o666` if not provided.
530536
* @param {function} callback - The callback function to execute once the file write operation concludes.
531537
* - The callback receives one argument:
532538
* 1. An error object (or null if there were no errors).
@@ -564,7 +570,7 @@ function writeFile (path, data, options, callback) {
564570
arrayBuffer = Utils.getEncodedArrayBuffer(data, options.encoding);
565571
}
566572
if(!window.__TAURI__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
567-
NodeTauriFS.writeBinaryFile(path, arrayBuffer)
573+
NodeTauriFS.writeBinaryFile(path, options.mode || 0o666, options.flag, arrayBuffer)
568574
.then(() => {
569575
callback(null);
570576
})
@@ -626,15 +632,15 @@ const TauriFS = {
626632
getTauriVirtualPath: Utils.getTauriVirtualPath,
627633
openTauriFilePickerAsync,
628634
openTauriFileSaveDialogueAsync,
635+
forceUseNodeWSEndpoint,
636+
preferNodeWSEndpoint,
629637
stat,
630638
readdir,
631639
mkdirs,
632640
rename,
633641
unlink,
634642
readFile,
635-
writeFile,
636-
forceUseNodeWSEndpoint,
637-
preferNodeWSEndpoint
643+
writeFile
638644
};
639645

640646
module.exports ={

test/test-dir.browser.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function _setupTests(testType) {
6565
console.log(`mkdirs: `, testPath);
6666
let mkdirResolve;
6767
const mkdirPromise = new Promise((resolve) => {mkdirResolve = resolve;});
68-
fs.mkdirs(testPath, 777 ,true, ()=>{
68+
fs.mkdirs(testPath, 0o777 ,true, ()=>{
6969
mkdirResolve();
7070
});
7171
await mkdirPromise;
@@ -172,7 +172,7 @@ function _setupTests(testType) {
172172
it(`Should phoenix ${testType} mkdir(path,mode, cb) in browser if it doesnt exist`, async function () {
173173
let createSuccess = false;
174174
const dirPathToCreate = `${testPath}/testDir1`;
175-
fs.mkdir(dirPathToCreate, 777, (err)=>{
175+
fs.mkdir(dirPathToCreate, 0o777, (err)=>{
176176
if(!err){
177177
createSuccess = true;
178178
}
@@ -198,7 +198,7 @@ function _setupTests(testType) {
198198
it(`Should phoenix fail ${testType} mkdir(path,mode, cb) if already exists`, async function () {
199199
let dirPathCreated = await _writeTestDir();
200200
let error;
201-
fs.mkdir(dirPathCreated, 777, (err)=>{
201+
fs.mkdir(dirPathCreated, 0o777, (err)=>{
202202
error = err;
203203
});
204204
await waitForTrue(()=>{return !!error;},1000);
@@ -208,7 +208,7 @@ function _setupTests(testType) {
208208
it(`Should phoenix ${testType} mkdirs(path,mode, recursive, cb) even if exists`, async function () {
209209
let dirPathCreated = await _writeTestDir();
210210
let success, error;
211-
fs.mkdirs(dirPathCreated, 777, true, (err)=>{
211+
fs.mkdirs(dirPathCreated, 0o777, true, (err)=>{
212212
error = err;
213213
success = true;
214214
});
@@ -221,7 +221,7 @@ function _setupTests(testType) {
221221
let dirPathCreated = await _writeTestDir();
222222
let pathToCreate = `${dirPathCreated}/path/that/doesnt/exist`;
223223
let success, error;
224-
fs.mkdirs(pathToCreate, 777, true, (err)=>{
224+
fs.mkdirs(pathToCreate, 0o777, true, (err)=>{
225225
error = err;
226226
success = true;
227227
});
@@ -247,7 +247,7 @@ function _setupTests(testType) {
247247
let dirPathCreated = await _writeTestDir();
248248
let pathToCreate = `${dirPathCreated}/path/that/doesnt/exist`;
249249
let error;
250-
fs.mkdirs(pathToCreate, 777, (err)=>{
250+
fs.mkdirs(pathToCreate, 0o777, (err)=>{
251251
error = err;
252252
});
253253
await waitForTrue(()=>{return !!error;},1000);

test/test-file.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function _setupTests(testType) {
7373
await _clean();
7474
console.log(`mkdir: `, testPath);
7575
let cleanSuccess = false;
76-
fs.mkdirs(testPath, 777 ,true, ()=>{
76+
fs.mkdirs(testPath, 0o777 ,true, ()=>{
7777
cleanSuccess = true;
7878
});
7979
await waitForTrue(()=>{return cleanSuccess;},10000);

0 commit comments

Comments
 (0)