-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfslib_electron.js
More file actions
522 lines (471 loc) · 18.4 KB
/
fslib_electron.js
File metadata and controls
522 lines (471 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// jshint ignore: start
/*global globalObject*/
/*eslint no-console: 0*/
/*eslint strict: ["error", "global"]*/
// Ensure globalObject is available in both browser and web worker contexts
if (typeof globalObject === 'undefined') {
if (typeof window !== 'undefined') {
window.globalObject = window;
} else if (typeof self !== 'undefined') {
self.globalObject = self;
}
}
const {Constants} = require('./constants');
const {Errors, ERR_CODES} = require("./errno");
const {Utils} = require("./utils");
const {NodeTauriFS} = require("./fslib_node_ws");
const IS_WINDOWS = navigator.userAgent.includes('Windows');
let preferNodeWs = false,
forceNodeWs = false;
/**
* Electron IPC strips Error.code during serialization (both main→preload and preload→renderer
* via contextBridge). FS handlers in main.js return {__fsError, code, message} plain objects
* on failure instead of throwing. This helper unwraps those into rejected promises.
*/
function unwrapFsResult(promise) {
return promise.then(result => {
if (result && result.__fsError) {
const err = new Error(result.message);
err.code = result.code;
throw err;
}
return result;
});
}
/**
* Maps Node.js error codes to Phoenix FS errors.
* @param {Error} err - The Node.js error
* @param {string} path - The path that caused the error
* @param {string} userMessage - Additional context message
* @returns {Error} Phoenix FS error
*/
function mapNodeErrorMessage(err, path, userMessage = '') {
const code = err.code || '';
const message = err.message || '';
switch (code) {
case 'ENOENT': return new Errors.ENOENT(userMessage + ` No such File or Directory: ${path} ` + message, path);
case 'EEXIST': return new Errors.EEXIST(userMessage + ` File exists: ${path} ` + message, path);
case 'ENOTEMPTY': return new Errors.ENOTEMPTY(userMessage + ` Directory not empty: ${path} ` + message, path);
case 'ENOTDIR': return new Errors.ENOTDIR(userMessage + ` Not a Directory: ${path} ` + message, path);
case 'EACCES': return new Errors.EACCES(userMessage + ` Permission denied: ${path} ` + message, path);
case 'EPERM': return new Errors.EPERM(userMessage + ` Operation not permitted: ${path} ` + message, path);
case 'EISDIR': return new Errors.EISDIR(userMessage + ` Is a directory: ${path} ` + message, path);
case 'EBADF': return new Errors.EBADF(userMessage + ` Bad file number: ${path} ` + message, path);
case 'EROFS': return new Errors.EROFS(userMessage + ` Read-only file system: ${path} ` + message, path);
case 'ENOSPC': return new Errors.ENOSPC(userMessage + ` No space left on device: ${path} ` + message, path);
case 'EBUSY': return new Errors.EBUSY(userMessage + ` Device or resource busy: ${path} ` + message, path);
case 'EINVAL': return new Errors.EINVAL(userMessage + ` Invalid argument: ${path} ` + message, path);
default: return new Errors.EIO(userMessage + ` IO error on path: ${path} ` + message, path);
}
}
/**
* Opens the Electron file picker asynchronously with given options.
*
* @param {Object} [options] - Configuration options for the file picker.
* @param {boolean} [options.directory=false] - Whether it is directory or file to be picked.
* @param {boolean} [options.multiple=false] - Whether to allow picking multiple files.
* @param {string} [options.defaultPath] - Default directory to open in the file picker.
* @param {string} [options.title] - The title of the dialog window.
* @param {Array<{name: string, extensions: string[]}>} [options.filters] - Extension filters.
*
* @returns {Promise<null|string|Array<string>>} A promise that resolves to null if user dismissed,
* a string (selected filepath), or an array of strings (multiple selected filepaths).
*/
async function openElectronFilePickerAsync(options) {
options = options || { multiple: false };
if (!options.defaultPath) {
options.defaultPath = await globalObject.electronAPI.getDocumentsDir();
}
const dialogOptions = {
defaultPath: options.defaultPath,
title: options.title,
properties: []
};
if (options.directory) {
dialogOptions.properties.push('openDirectory');
} else {
dialogOptions.properties.push('openFile');
}
if (options.multiple) {
dialogOptions.properties.push('multiSelections');
}
if (options.filters) {
dialogOptions.filters = options.filters;
}
try {
const filePaths = await globalObject.electronAPI.showOpenDialog(dialogOptions);
if (!filePaths || filePaths.length === 0) {
return null;
}
if (options.multiple) {
return filePaths.map(p => Utils.getTauriVirtualPath(p));
}
return Utils.getTauriVirtualPath(filePaths[0]);
} catch (err) {
throw mapNodeErrorMessage(err, options.defaultPath, 'Failed to open file picker');
}
}
/**
* Opens the Electron file save dialogue asynchronously using the provided options.
*
* @param {Object} [options] - Configuration options for the file save dialogue.
* @param {string} [options.defaultPath] - Initial directory or file path.
* @param {string} [options.title] - The title of the dialog window.
* @param {Array<{name: string, extensions: string[]}>} [options.filters] - Extension filters.
*
* @returns {Promise<string|null>} A promise that resolves to the selected file path or null.
*/
async function openElectronFileSaveDialogueAsync(options) {
options = options || {};
if (!options.defaultPath) {
options.defaultPath = await globalObject.electronAPI.getDocumentsDir();
}
const dialogOptions = {
defaultPath: options.defaultPath,
title: options.title
};
if (options.filters) {
dialogOptions.filters = options.filters;
}
try {
const filePath = await globalObject.electronAPI.showSaveDialog(dialogOptions);
if (typeof filePath === 'string' && filePath) {
return Utils.getTauriVirtualPath(filePath);
}
return null;
} catch (err) {
throw mapNodeErrorMessage(err, options.defaultPath, 'Failed to open save dialog');
}
}
async function _getElectronStat(vfsPath) {
const platformPath = globalObject.fs.getTauriPlatformPath(vfsPath);
const stats = await unwrapFsResult(globalObject.electronAPI.fsStat(platformPath));
return Utils.createFromNodeStat(vfsPath, stats, Constants.ELECTRON_DEVICE_NAME);
}
function _readDirHelper(entries, path, options, callback, useDummyStats) {
let children = [];
for (const entry of entries) {
if (!options.withFileTypes) {
children.push(entry.name);
} else if (useDummyStats) {
children.push(Utils.createDummyStatObject(`${path}/${entry.name}`, true, Constants.TAURI_DEVICE_NAME));
} else {
children.push(_getElectronStat(`${path}/${entry.name}`));
}
}
if (!options.withFileTypes || useDummyStats) {
callback(null, children);
} else {
Promise.all(children)
.then((results) => {
callback(null, results);
})
.catch((err) => {
callback(mapNodeErrorMessage(err, path, 'Failed to read directory: '));
});
}
}
/**
* Reads the contents of a directory.
*/
function readdir(path, options, callback) {
path = globalObject.path.normalize(path);
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
return NodeTauriFS.readdir(path, options, callback);
}
if (IS_WINDOWS && path === Constants.TAURI_ROOT) {
// Windows drive listing not supported via Electron IPC fallback
// NodeTauriFS should handle this
callback(new Errors.ENOSYS('Windows drive listing requires NodeTauriFS'));
return;
}
const platformPath = Utils.getTauriPlatformPath(path);
unwrapFsResult(globalObject.electronAPI.fsReaddir(platformPath))
.then((entries) => {
_readDirHelper(entries, path, options, callback);
})
.catch((err) => {
callback(mapNodeErrorMessage(err, path, 'Failed to read directory: '));
});
}
/**
* Creates a directory with optional mode and recursion.
*/
function mkdirs(path, mode, recursive, callback) {
if (typeof mode !== 'number') {
callback = recursive;
recursive = mode;
mode = 0o777;
}
if (typeof recursive !== 'boolean') {
callback = recursive;
recursive = false;
}
if (typeof callback !== 'function') {
callback = function () {};
}
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
NodeTauriFS.mkdirs(path, mode, recursive, callback);
return;
}
const platformPath = Utils.getTauriPlatformPath(path);
unwrapFsResult(globalObject.electronAPI.fsMkdir(platformPath, { recursive, mode }))
.then(() => {
callback(null);
})
.catch((err) => {
callback(mapNodeErrorMessage(err, path, 'Failed to create directory: '));
});
}
/**
* Retrieves the status of a file or directory.
*/
function stat(path, callback, options = {}) {
path = globalObject.path.normalize(path);
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
return NodeTauriFS.stat(path, callback, options);
}
_getElectronStat(path)
.then(stat => {
callback(null, stat);
})
.catch((err) => {
callback(mapNodeErrorMessage(err, path, 'Failed to get stat'));
});
}
function unlink(path, callback) {
path = globalObject.path.normalize(path);
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
return NodeTauriFS.unlink(path, callback);
}
function errCallback(err) {
callback(mapNodeErrorMessage(err, path, 'Failed to unlink'));
}
_getElectronStat(path)
.then(stat => {
const platformPath = Utils.getTauriPlatformPath(path);
if (stat.isDirectory()) {
unwrapFsResult(globalObject.electronAPI.fsRmdir(platformPath, { recursive: true }))
.then(() => { callback(null); })
.catch(errCallback);
} else {
unwrapFsResult(globalObject.electronAPI.fsUnlink(platformPath))
.then(() => { callback(null); })
.catch(errCallback);
}
})
.catch(errCallback);
}
function rename(oldPath, newPath, callback) {
oldPath = globalObject.path.normalize(oldPath);
newPath = globalObject.path.normalize(newPath);
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
NodeTauriFS.rename(oldPath, newPath, callback);
return;
}
const oldPlatformPath = Utils.getTauriPlatformPath(oldPath);
const newPlatformPath = Utils.getTauriPlatformPath(newPath);
unwrapFsResult(globalObject.electronAPI.fsRename(oldPlatformPath, newPlatformPath))
.then(() => { callback(null); })
.catch(err => {
callback(mapNodeErrorMessage(err, oldPath, `Failed to rename ${oldPath} to ${newPath}`));
});
}
/**
* Processes file contents for different encodings.
*/
function _processContents(contents, encoding, callback, path) {
try {
let arrayBuffer = contents;
if (contents.buffer instanceof ArrayBuffer) {
arrayBuffer = contents.buffer;
}
if (encoding === Constants.BYTE_ARRAY_ENCODING) {
callback(null, arrayBuffer, encoding);
return;
} else if (encoding === Constants.BINARY_ENCODING) {
const contentBuffer = Buffer.from(arrayBuffer);
callback(null, contentBuffer, encoding);
return;
}
let decodedString = Utils.getDecodedString(arrayBuffer, encoding);
callback(null, decodedString, encoding);
} catch (e) {
if (ERR_CODES.ERROR_CODES[e.code]) {
callback(e);
} else {
callback(new Errors.EIO(`IO error while processing data read from file on path: ${path}`, path));
}
}
}
/**
* Reads the contents of a file.
*/
function readFile(path, options, callback) {
try {
path = globalObject.path.normalize(path);
callback = arguments[arguments.length - 1];
options = Utils.validateFileOptions(options, Constants.BINARY_ENCODING, 'r');
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
NodeTauriFS.readBinaryFile(path)
.then(contents => {
contents = contents || new ArrayBuffer(0);
_processContents(contents, options.encoding, callback, path);
})
.catch(callback);
return;
}
const platformPath = Utils.getTauriPlatformPath(path);
unwrapFsResult(globalObject.electronAPI.fsReadFile(platformPath))
.then(contents => {
// Electron returns a Buffer, convert to ArrayBuffer
let arrayBuffer;
if (contents instanceof ArrayBuffer) {
arrayBuffer = contents;
} else if (contents && contents.buffer) {
arrayBuffer = contents.buffer.slice(
contents.byteOffset,
contents.byteOffset + contents.byteLength
);
} else if (contents) {
// Handle Uint8Array or similar
arrayBuffer = new Uint8Array(contents).buffer;
} else {
arrayBuffer = new ArrayBuffer(0);
}
_processContents(arrayBuffer, options.encoding, callback, path);
})
.catch(err => {
callback(mapNodeErrorMessage(err, path, `Failed to read File at path ${path}`));
});
} catch (e) {
if (ERR_CODES.ERROR_CODES[e.code]) {
callback(e);
} else {
callback(new Errors.EIO(`IO error while processing data read from file on path: ${path}`, path));
}
}
}
/**
* Writes data to a file, replacing the file if it already exists.
*/
function writeFile(path, data, options, callback) {
try {
path = globalObject.path.normalize(path);
callback = arguments[arguments.length - 1];
options = Utils.validateFileOptions(options, Constants.BINARY_ENCODING, 'w');
let arrayBuffer;
if (data instanceof ArrayBuffer) {
arrayBuffer = data;
} else if (Buffer.isBuffer(data)) {
arrayBuffer = Utils.toArrayBuffer(data);
} else {
if (typeof data === 'number') {
data = '' + data;
}
data = data || '';
if (typeof data !== 'string') {
data = data.toString();
}
arrayBuffer = Utils.getEncodedArrayBuffer(data, options.encoding);
}
if (!globalObject.__ELECTRON__ || forceNodeWs || (preferNodeWs && NodeTauriFS.isNodeWSReady())) {
NodeTauriFS.writeBinaryFile(path, options.mode || 0o666, options.flag, arrayBuffer)
.then(() => {
callback(null);
})
.catch(callback);
return;
}
const platformPath = Utils.getTauriPlatformPath(path);
// Convert ArrayBuffer to Uint8Array for IPC transfer
const uint8Array = new Uint8Array(arrayBuffer);
unwrapFsResult(globalObject.electronAPI.fsWriteFile(platformPath, Array.from(uint8Array)))
.then(() => {
callback(null);
})
.catch(err => {
callback(mapNodeErrorMessage(err, path, `Failed to write File at path ${path}`));
});
} catch (e) {
if (ERR_CODES.ERROR_CODES[e.code]) {
callback(e);
} else {
callback(new Errors.EIO(`IO error while processing data write from file on path: ${path}`, path));
}
}
}
/**
* Forces the usage of the Node WebSocket endpoint.
*/
function forceUseNodeWSEndpoint(use) {
if (!NodeTauriFS.getNodeWSEndpoint()) {
throw new Error("Please call fs.setNodeWSEndpoint('ws://your server') before calling this function.");
}
forceNodeWs = use;
}
/**
* Sets the preference to use the Node WebSocket endpoint if available.
*/
function preferNodeWSEndpoint(use) {
if (!NodeTauriFS.getNodeWSEndpoint()) {
throw new Error("Please call fs.setNodeWSEndpoint('ws://your server') before calling this function.");
}
preferNodeWs = use;
}
function canCopy() {
// we can only copy if node tari fs is ready as Electron IPC doesn't have folder copy APIs
return NodeTauriFS.isNodeWSReady();
}
async function copy(src, dst, callback) {
if (!canCopy()) {
callback(new Errors.EIO(`IO error while copying: ${src} to ${dst}, node not ready.`, src));
return;
}
src = globalObject.path.normalize(src);
dst = globalObject.path.normalize(dst);
return NodeTauriFS.copy(src, dst, callback);
}
const ElectronFS = {
isTauriPath: Utils.isTauriPath,
isTauriSubPath: Utils.isTauriSubPath,
getTauriPlatformPath: Utils.getTauriPlatformPath,
getTauriVirtualPath: Utils.getTauriVirtualPath,
openElectronFilePickerAsync,
openElectronFileSaveDialogueAsync,
forceUseNodeWSEndpoint,
preferNodeWSEndpoint,
stat,
readdir,
mkdirs,
rename,
unlink,
readFile,
writeFile,
copy,
canCopy
};
module.exports = {
ElectronFS
};