Skip to content

Commit 1c808c4

Browse files
committed
feat: adding withFileTypes option to fs.readdir native api
1 parent e4874bb commit 1c808c4

File tree

6 files changed

+58
-9
lines changed

6 files changed

+58
-9
lines changed

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_native.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ const {Constants} = require('./constants');
2727
const {Utils} =require('./utils');
2828

2929

30-
async function _listDir(handle, callback) {
30+
async function _listDir(path, handle, options, callback) {
3131
let dirEntryNames = [];
3232
try {
33-
for await (const [key] of handle.entries()) {
34-
dirEntryNames.push(key);
33+
for await (const [key, value] of handle.entries()) {
34+
let entry = key;
35+
if(options['withFileTypes']){
36+
entry = await Utils.createStatObject(globalObject.path.join(path, key), value);
37+
}
38+
dirEntryNames.push(entry);
3539
}
3640
if(callback){
3741
callback(null, dirEntryNames);
@@ -85,10 +89,10 @@ function mkdir(path, mode, callback) {
8589

8690
function readdir(path, options, callback) {
8791
path = globalObject.path.normalize(path);
88-
if (typeof options !== 'function') {
89-
throw new Errors.ENOSYS('Filer readdir options are not yet supported');
92+
if (typeof options === 'function') {
93+
callback = options;
94+
options = {};
9095
}
91-
callback = options;
9296

9397
if(path === Constants.MOUNT_POINT_ROOT ) {
9498
let mountedFolders = Object.keys(Mounts.getMountPoints());
@@ -100,7 +104,7 @@ function readdir(path, options, callback) {
100104
} else if (handle.kind === Constants.KIND_FILE) {
101105
callback(new Errors.ENOTDIR('Path is not a directory.'));
102106
}else {
103-
_listDir(handle, callback);
107+
_listDir(path, handle, options, callback);
104108
}
105109
});
106110
}

test/test.browser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ describe('Browser main tests', function () {
5454
expect(readSuccess).to.be.true;
5555
});
5656

57+
it('Should phoenix native read dir', async function () {
58+
let readSuccess = false, contentsRead;
59+
fs.readdir(`${window.mountTestPath}`, (err, contents)=>{
60+
if(!err){
61+
readSuccess = true;
62+
}
63+
contentsRead = contents;
64+
});
65+
await waitForTrue(()=>{return readSuccess;},1000);
66+
expect(readSuccess).to.be.true;
67+
expect(contentsRead.length).to.be.above(1);
68+
});
69+
70+
it('Should phoenix native read dir with withFileTypes', async function () {
71+
let readSuccess = false, contentsRead;
72+
fs.readdir(`${window.mountTestPath}`, {withFileTypes: true} , (err, contents)=>{
73+
if(!err){
74+
readSuccess = true;
75+
}
76+
contentsRead = contents;
77+
});
78+
await waitForTrue(()=>{return readSuccess;},1000);
79+
expect(readSuccess).to.be.true;
80+
expect(contentsRead[0].type).to.exist;
81+
});
82+
5783
it('Should phoenix native delete in browser', async function () {
5884
let delSuccess = false;
5985
fs.unlink(`${window.mountTestPath}/browserWrite.txt`, (err)=>{

test/test.worker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ describe('web worker tests', function () {
7272
expect(status).to.be.true;
7373
});
7474

75+
it('Should phoenix native read dir withFileTypes in worker', async function () {
76+
messageFromWorker = null;
77+
worker.postMessage('readDirMountCheck');
78+
let status = await waitForWorkerMessage('readDirMountCheck.ok', 1000);
79+
expect(status).to.be.true;
80+
});
81+
7582
it('Should phoenix native delete in worker', async function () {
7683
messageFromWorker = null;
7784
worker.postMessage('deleteMountCheck');

test/worker-task.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ function checkReadInMountPath() {
3838
});
3939
}
4040

41+
function checkReadDirInMountPath() {
42+
console.log('worker: checkReadDirInMountPath');
43+
fs.readdir(`${mountTestPath}`, {withFileTypes: true} , (err, contents)=>{
44+
if(!err && contents[0].type){
45+
postMessage('readDirMountCheck.ok');
46+
return;
47+
}
48+
console.log('file read:', err, contents);
49+
});
50+
}
51+
4152
function checkDeleteInMountPath() {
4253
console.log('worker: checkDeleteInMountPath');
4354
fs.unlink(`${mountTestPath}/workerWrite.txt`, (err)=>{
@@ -58,6 +69,7 @@ self.addEventListener('message', (event) => {
5869
case 'writeMountCheck': checkWriteInMountPath(); break;
5970
case 'readMountCheck': checkReadInMountPath(); break;
6071
case 'deleteMountCheck': checkDeleteInMountPath(); break;
72+
case 'readDirMountCheck': checkReadDirInMountPath(); break;
6173
default: console.error('unknown worker command: ', command);
6274
}
6375
}, false);

0 commit comments

Comments
 (0)