Skip to content

Commit

Permalink
feat: 🎸 implement basic readdir
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 16, 2023
1 parent 299feea commit 685bc7e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
46 changes: 26 additions & 20 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getDefaultOptsAndCb,
getMkdirOptions,
getReadFileOptions,
getReaddirOptsAndCb,
getRmOptsAndCb,
getRmdirOptions,
optsAndCbGenerator,
Expand Down Expand Up @@ -288,29 +289,34 @@ export class FsaNodeFs implements FsCallbackApi {
throw new Error('Not implemented');
}

readdir(path: misc.PathLike, callback: misc.TCallback<misc.TDataOut[] | misc.IDirent[]>);
readdir(
path: misc.PathLike,
options: opts.IReaddirOptions | string,
callback: misc.TCallback<misc.TDataOut[] | misc.IDirent[]>,
);
readdir(path: misc.PathLike, a?, b?) {
throw new Error('Not implemented');
}
public readonly readdir: FsCallbackApi['readdir'] = (path: misc.PathLike, a?, b?) => {
const [options, callback] = getReaddirOptsAndCb(a, b);
const filename = pathToFilename(path);
const [folder, name] = pathToLocation(filename);
folder.push(name);
this.getDir(folder, false, 'readdir')
.then(dir => (async () => {
const list: string[] = [];
for await (const key of dir.keys()) list.push(key);
return list;
})())
.then(res => callback(null, res), err => callback(err));
};

readlink(path: misc.PathLike, callback: misc.TCallback<misc.TDataOut>);
readlink(path: misc.PathLike, options: opts.IOptions, callback: misc.TCallback<misc.TDataOut>);
readlink(path: misc.PathLike, a: misc.TCallback<misc.TDataOut> | opts.IOptions, b?: misc.TCallback<misc.TDataOut>) {
throw new Error('Not implemented');
}
public readonly readlink: FsCallbackApi['readlink'] = (path: misc.PathLike, a: misc.TCallback<misc.TDataOut> | opts.IOptions, b?: misc.TCallback<misc.TDataOut>) => {
const [opts, callback] = getDefaultOptsAndCb(a, b);
const filename = pathToFilename(path);
const buffer = Buffer.from(filename);
callback(null, bufferToEncoding(buffer, opts.encoding));
};

fsync(fd: number, callback: misc.TCallback<void>): void {
throw new Error('Not implemented');
}
public readonly fsync: FsCallbackApi['fsync'] = (fd: number, callback: misc.TCallback<void>): void => {
callback(null);
};

fdatasync(fd: number, callback: misc.TCallback<void>): void {
throw new Error('Not implemented');
}
public readonly fdatasync: FsCallbackApi['fdatasync'] = (fd: number, callback: misc.TCallback<void>): void => {
callback(null);
};

public readonly ftruncate: FsCallbackApi['ftruncate'] = (fd: number, a: misc.TCallback<void> | number, b?: misc.TCallback<void>): void => {
const len: number = typeof a === 'number' ? a : 0;
Expand Down
11 changes: 11 additions & 0 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,14 @@ describe('.ftruncate()', () => {
expect(mfs.readFileSync('/mountpoint/folder/file', 'utf8')).toBe('tes');
});
});

describe('.readdir()', () => {
test('can read directory contents as strings', async () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const res = await fs.promises.readdir('/') as string[];
expect(res.length).toBe(3);
expect(res.includes('folder')).toBe(true);
expect(res.includes('empty-folder')).toBe(true);
expect(res.includes('f.html')).toBe(true);
});
});
7 changes: 7 additions & 0 deletions src/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ const readFileOptsDefaults: opts.IReadFileOptions = {
flag: 'r',
};
export const getReadFileOptions = optsGenerator<opts.IReadFileOptions>(readFileOptsDefaults);

const readdirDefaults: opts.IReaddirOptions = {
encoding: 'utf8',
withFileTypes: false,
};
export const getReaddirOptions = optsGenerator<opts.IReaddirOptions>(readdirDefaults);
export const getReaddirOptsAndCb = optsAndCbGenerator<opts.IReaddirOptions, misc.TDataOut[] | misc.IDirent[]>(getReaddirOptions);
12 changes: 2 additions & 10 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
getMkdirOptions,
getOptions,
getReadFileOptions,
getReaddirOptions,
getReaddirOptsAndCb,
getRmOptsAndCb,
getRmdirOptions,
optsAndCbGenerator,
Expand Down Expand Up @@ -179,12 +181,6 @@ export interface IMkdirOptions {
export interface IReaddirOptions extends opts.IOptions {
withFileTypes?: boolean;
}
const readdirDefaults: IReaddirOptions = {
encoding: 'utf8',
withFileTypes: false,
};
const getReaddirOptions = optsGenerator<IReaddirOptions>(readdirDefaults);
const getReaddirOptsAndCb = optsAndCbGenerator<IReaddirOptions, TDataOut[] | Dirent[]>(getReaddirOptions);

// Options for `fs.lstat`, `fs.lstatSync`, `fs.stat`, and `fs.statSync`
export interface IStatOptions {
Expand Down Expand Up @@ -470,10 +466,6 @@ export class Volume {
return node;
}

private getNode(ino: number) {
return this.inodes[ino];
}

private deleteNode(node: Node) {
node.del();
delete this.inodes[node.ino];
Expand Down

0 comments on commit 685bc7e

Please sign in to comment.