Skip to content

Commit 1008a46

Browse files
committed
chore: performance improvement by pruning unnecessary fs calls
1 parent 8e44135 commit 1008a46

3 files changed

Lines changed: 25 additions & 45 deletions

File tree

dist/virtualfs.js

Lines changed: 1 addition & 1 deletion
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_mounts.js

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,24 @@ function mountNativeFolder(optionalDirHandle, callback) {
201201
});
202202
}
203203

204-
async function _verifyDirNodeCanBeRead(handle) {
204+
async function _verifyOrRequestPermission(handle) {
205+
const options = {
206+
mode: 'read'
207+
};
208+
209+
// Check if permission was already granted. If so, return true.
205210
try {
206-
if(handle.kind === Constants.KIND_DIRECTORY){
207-
let entries = handle.entries();
208-
await entries.next();
211+
let status = await handle.queryPermission(options);
212+
if (status === 'granted') {
213+
return null;
209214
}
210-
return null;
211-
} catch (e) {
215+
status = await handle.requestPermission(options);
216+
if (status === 'granted') {
217+
return null;
218+
} else {
219+
return new Errors.EACCES(`Dir permissions not granted ${handle.name}`);
220+
}
221+
} catch(e){
212222
if(e.code === e.NOT_FOUND_ERR){
213223
return new Errors.ENOENT(`Dir does not exist ${handle.name}`, e);
214224
} else {
@@ -218,12 +228,6 @@ async function _verifyDirNodeCanBeRead(handle) {
218228
}
219229

220230
async function _findLeafNode(currentNode, pathArray, currentIndex, callback) {
221-
let error = await _verifyDirNodeCanBeRead(currentNode);
222-
if(error){
223-
callback(error);
224-
return;
225-
}
226-
227231
let pathLength = pathArray.length;
228232
if(currentIndex === pathLength) {
229233
callback(null, currentNode);
@@ -255,30 +259,7 @@ async function _findLeafNode(currentNode, pathArray, currentIndex, callback) {
255259
}
256260
}
257261

258-
async function _verifyOrRequestPermission(fileHandle, callback) {
259-
const options = {
260-
mode: 'read'
261-
};
262-
263-
// Check if permission was already granted. If so, return true.
264-
try {
265-
let status = await fileHandle.queryPermission(options);
266-
if (status === 'granted') {
267-
callback(true);
268-
return;
269-
}
270-
status = await fileHandle.requestPermission(options);
271-
if (status === 'granted') {
272-
callback(true);
273-
} else {
274-
callback(false);
275-
}
276-
} catch(e){
277-
callback(false);
278-
}
279-
}
280-
281-
function getHandleFromPath(normalisedPath, callback) {
262+
async function getHandleFromPath(normalisedPath, callback) {
282263
const pathNodes = normalisedPath.split('/');
283264
const currentMounts = MountPointsStore.getMountPoints();
284265
if(pathNodes.length < 3 || pathNodes[0] !== '' || pathNodes[1] !== 'mnt'){
@@ -289,13 +270,12 @@ function getHandleFromPath(normalisedPath, callback) {
289270
callback(new Errors.ENOENT('Path does not exist: ', normalisedPath));
290271
return;
291272
}
292-
_verifyOrRequestPermission(mountPoint, (permitted)=>{
293-
if(permitted){
294-
_findLeafNode(mountPoint, pathNodes, 3, callback);
295-
} else {
296-
callback(new Errors.EACCES('permission denied on path: ' + normalisedPath));
297-
}
298-
});
273+
let error = await _verifyOrRequestPermission(mountPoint);
274+
if(error){
275+
callback(error);
276+
return;
277+
}
278+
_findLeafNode(mountPoint, pathNodes, 3, callback);
299279
}
300280

301281
async function getHandleFromPathIfPresent(normalisedPath) {

0 commit comments

Comments
 (0)