Skip to content

Commit

Permalink
fix(readdir): 🐛 Error no such file or directory
Browse files Browse the repository at this point in the history
  • Loading branch information
magarcia committed Oct 22, 2019
1 parent 0b5f036 commit 8d21e9a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
33 changes: 33 additions & 0 deletions src/__tests__/union.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,39 @@ describe('union', () => {
done();
});
});

it("reads other fss when one fails", done => {
const vol = Volume.fromJSON({
"/foo/bar": "bar",
"/foo/baz": "baz"
});
const vol2 = Volume.fromJSON({
"/bar/baz": "not baz",
"/bar/qux": "baz"
});

const ufs = new Union();
ufs.use(vol as any);
ufs.use(vol2 as any);
ufs.readdir("/bar", (err, files) => {
expect(err).toBeNull();
expect(files).toEqual(["baz", "qux"]);
done();
});
});

it("throws error when all fss fail", done => {
const vol = Volume.fromJSON({});
const vol2 = Volume.fromJSON({});

const ufs = new Union();
ufs.use(vol as any);
ufs.use(vol2 as any);
ufs.readdir("/bar", (err, files) => {
expect(err).not.toBeNull();
done();
});
});
});
});

Expand Down
6 changes: 2 additions & 4 deletions src/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class Union {
}

let lastError: IUnionFsError = null;
let result: Set<string> | null = null;
let result: Set<string> = new Set();
const iterate = (i = 0, error?: IUnionFsError) => {
if(error) {
error.prev = lastError;
Expand All @@ -145,12 +145,10 @@ export class Union {

// Replace `callback` with our intermediate function.
args[lastarg] = (err, resArg: string[] | Buffer[]) => {
if(err) {
if(result.size === 0 && err) {
return iterate(i + 1, err);
}
if(resArg) {
result = result !== null ? result : new Set();

// Convert all results to Strings to make sure that they're deduped
for (const res of resArg) {
result.add(String(res));
Expand Down

0 comments on commit 8d21e9a

Please sign in to comment.