Skip to content

Commit 2089ab6

Browse files
committed
feat: fs.writeFile api and tests for node ws apis working
1 parent de500fe commit 2089ab6

6 files changed

Lines changed: 82 additions & 23 deletions

File tree

dist/phoenix-fs.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ const WS_COMMAND = {
105105
GET_WINDOWS_DRIVES: "getWinDrives",
106106
READ_DIR: "readDir",
107107
STAT: "stat",
108-
READ_BIN_FILE: "readBinFile"
108+
READ_BIN_FILE: "readBinFile",
109+
WRITE_BIN_FILE: "writeBinFile"
109110
};
110111

111112
const LARGE_DATA_THRESHOLD = 2*1024*1024; // 2MB
@@ -220,6 +221,21 @@ function _readBinaryFile(ws, metadata) {
220221
}).catch((err)=>_reportError(ws, metadata, err, `Failed to read file at path ${fullPath}`));
221222
}
222223

224+
/**
225+
*
226+
* @param ws
227+
* @param metadata
228+
* @param dataBuffer {ArrayBuffer}
229+
* @private
230+
*/
231+
function _writeBinaryFile(ws, metadata, dataBuffer) {
232+
const fullPath = metadata.data.path;
233+
fs.writeFile(fullPath, Buffer.from(dataBuffer))
234+
.then( ()=> {
235+
_sendResponse(ws, metadata);
236+
}).catch((err)=>_reportError(ws, metadata, err, `Failed to write file at path ${fullPath}`));
237+
}
238+
223239
function processWSCommand(ws, metadata, dataBuffer) {
224240
try{
225241
switch (metadata.commandCode) {
@@ -241,6 +257,9 @@ function processWSCommand(ws, metadata, dataBuffer) {
241257
case WS_COMMAND.READ_BIN_FILE:
242258
_readBinaryFile(ws, metadata);
243259
return;
260+
case WS_COMMAND.WRITE_BIN_FILE:
261+
_writeBinaryFile(ws, metadata, dataBuffer);
262+
return;
244263
case WS_COMMAND.LARGE_DATA_SOCKET_ANNOUNCE:
245264
console.log("Large Data Transfer Socket established, socket Group: ", metadata.socketGroupID);
246265
ws.isLargeData = true;

dist/virtualfs.js

Lines changed: 16 additions & 16 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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ const WS_COMMAND = {
105105
GET_WINDOWS_DRIVES: "getWinDrives",
106106
READ_DIR: "readDir",
107107
STAT: "stat",
108-
READ_BIN_FILE: "readBinFile"
108+
READ_BIN_FILE: "readBinFile",
109+
WRITE_BIN_FILE: "writeBinFile"
109110
};
110111

111112
const LARGE_DATA_THRESHOLD = 2*1024*1024; // 2MB
@@ -220,6 +221,21 @@ function _readBinaryFile(ws, metadata) {
220221
}).catch((err)=>_reportError(ws, metadata, err, `Failed to read file at path ${fullPath}`));
221222
}
222223

224+
/**
225+
*
226+
* @param ws
227+
* @param metadata
228+
* @param dataBuffer {ArrayBuffer}
229+
* @private
230+
*/
231+
function _writeBinaryFile(ws, metadata, dataBuffer) {
232+
const fullPath = metadata.data.path;
233+
fs.writeFile(fullPath, Buffer.from(dataBuffer))
234+
.then( ()=> {
235+
_sendResponse(ws, metadata);
236+
}).catch((err)=>_reportError(ws, metadata, err, `Failed to write file at path ${fullPath}`));
237+
}
238+
223239
function processWSCommand(ws, metadata, dataBuffer) {
224240
try{
225241
switch (metadata.commandCode) {
@@ -241,6 +257,9 @@ function processWSCommand(ws, metadata, dataBuffer) {
241257
case WS_COMMAND.READ_BIN_FILE:
242258
_readBinaryFile(ws, metadata);
243259
return;
260+
case WS_COMMAND.WRITE_BIN_FILE:
261+
_writeBinaryFile(ws, metadata, dataBuffer);
262+
return;
244263
case WS_COMMAND.LARGE_DATA_SOCKET_ANNOUNCE:
245264
console.log("Large Data Transfer Socket established, socket Group: ", metadata.socketGroupID);
246265
ws.isLargeData = true;

src/fslib_node_ws.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const WS_COMMAND = {
3535
GET_WINDOWS_DRIVES: "getWinDrives",
3636
READ_DIR: "readDir",
3737
STAT: "stat",
38-
READ_BIN_FILE: "readBinFile"
38+
READ_BIN_FILE: "readBinFile",
39+
WRITE_BIN_FILE: "writeBinFile"
3940
};
4041

4142
// each browser context belongs to a single socket group. So multiple websocket connections can be pooled
@@ -366,14 +367,26 @@ function readBinaryFile(path) {
366367
});
367368
}
368369

370+
function writeBinaryFile(path, dataArrayBuffer) {
371+
return new Promise((resolve, reject)=>{
372+
const platformPath = Utils.getTauriPlatformPath(path);
373+
_execCommand(WS_COMMAND.WRITE_BIN_FILE, {path: platformPath}, dataArrayBuffer)
374+
.then(resolve)
375+
.catch((err)=>{
376+
reject(mapNodeTauriErrorMessage(err, path, 'Failed to write file: '));
377+
});
378+
});
379+
}
380+
369381
const NodeTauriFS = {
370382
testNodeWsEndpoint,
371383
setNodeWSEndpoint,
372384
stopNodeWSEndpoint,
373385
getNodeWSEndpoint,
374386
readdir,
375387
stat,
376-
readBinaryFile
388+
readBinaryFile,
389+
writeBinaryFile
377390
};
378391

379392
module.exports = {

src/fslib_tauri.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ function readFile(path, options, callback) {
544544
*/
545545
function writeFile (path, data, options, callback) {
546546
try{
547+
path = globalObject.path.normalize(path);
547548
callback = arguments[arguments.length - 1];
548549
options = Utils.validateFileOptions(options, Constants.BINARY_ENCODING, 'w');
549550
let arrayBuffer;
@@ -561,9 +562,16 @@ function writeFile (path, data, options, callback) {
561562
}
562563
arrayBuffer = Utils.getEncodedArrayBuffer(data, options.encoding);
563564
}
564-
path = globalObject.path.normalize(path);
565-
const platformPath = Utils.getTauriPlatformPath(path);
565+
if(!window.__TAURI__ || preferNodeWs) {
566+
NodeTauriFS.writeBinaryFile(path, arrayBuffer)
567+
.then(() => {
568+
callback(null);
569+
})
570+
.catch(callback);
571+
return;
572+
}
566573

574+
const platformPath = Utils.getTauriPlatformPath(path);
567575
__TAURI__.fs.writeBinaryFile(platformPath, arrayBuffer)
568576
.then(() => {
569577
callback(null);

0 commit comments

Comments
 (0)