Skip to content

Commit

Permalink
feat: 🎸 add readdirSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 4b3444d commit 2178a50
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
11 changes: 10 additions & 1 deletion demo/fsa/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(window as any).process = require('process/browser');
(window as any).Buffer = require('buffer').Buffer;

import {strictEqual} from 'assert';
import {strictEqual, deepEqual} from 'assert';

import type * as fsa from '../../src/fsa/types';
import {FsaNodeFs, FsaNodeSyncAdapterWorker} from '../../src/fsa-to-node';
Expand Down Expand Up @@ -76,6 +76,15 @@ const demo = async (dir: fsa.IFileSystemDirectoryHandle) => {
await fs.promises.writeFile('/delete-me.txt', 'abc');
fs.unlinkSync('/delete-me.txt');
strictEqual(fs.existsSync('/delete-me.txt'), false);

console.log('readdirSync() - can list files in a directory');
const listInDir = fs.readdirSync('/dir');
deepEqual(listInDir, ['very-cool.txt']);

console.log('readdirSync() - can list files in a directory as Dirent[]');
const listInDir2 = fs.readdirSync('/dir', {withFileTypes: true}) as any;
deepEqual(listInDir2[0].name, 'very-cool.txt');
deepEqual(listInDir2[0].isFile(), true);
};

const main = async () => {
Expand Down
8 changes: 3 additions & 5 deletions src/fsa-to-node/FsaNodeDirent.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { NodeFileSystemDirectoryHandle, NodeFileSystemFileHandle } from '../node-to-fsa';
import type { IFileSystemHandle } from '../fsa/types';
import type { IDirent, TDataOut } from '../node/types/misc';

export class FsaNodeDirent implements IDirent {
public constructor(public readonly name: TDataOut, protected readonly handle: IFileSystemHandle) {}
public constructor(public readonly name: TDataOut, protected readonly kind: 'file' | 'directory') {}

public isDirectory(): boolean {
return this.handle instanceof NodeFileSystemDirectoryHandle;
return this.kind === 'directory';
}

public isFile(): boolean {
return this.handle instanceof NodeFileSystemFileHandle;
return this.kind === 'file';
}

public isBlockDevice(): boolean {
Expand Down
23 changes: 21 additions & 2 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getStatOptions,
getAppendFileOpts,
getDefaultOpts,
getReaddirOptions,
} from '../node/options';
import {
bufToUint8,
Expand Down Expand Up @@ -462,7 +463,7 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
if (options.withFileTypes) {
const list: misc.IDirent[] = [];
for await (const [name, handle] of dir.entries()) {
const dirent = new FsaNodeDirent(name, handle);
const dirent = new FsaNodeDirent(name, handle.kind);
list.push(dirent);
}
if (!isWin && options.encoding !== 'buffer')
Expand Down Expand Up @@ -907,8 +908,26 @@ export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchrono
this.getSyncAdapter().call('unlink', [filename]);
};

public readonly readdirSync: FsSynchronousApi['readdirSync'] = (path: misc.PathLike, options?: opts.IReaddirOptions | string): misc.TDataOut[] | misc.IDirent[] => {
const opts = getReaddirOptions(options);
const filename = pathToFilename(path);
const adapter = this.getSyncAdapter();
const list = adapter.call('readdir', [filename]);
if (opts.withFileTypes) {
const res: misc.IDirent[] = [];
for (const entry of list) res.push(new FsaNodeDirent(entry.name, entry.kind));
return res;
} else {
const res: misc.TDataOut[] = [];
for (const entry of list) {
const buffer = Buffer.from(entry.name);
res.push(bufferToEncoding(buffer, opts.encoding));
}
return res;
}
};

public readonly openSync: FsSynchronousApi['openSync'] = notSupported;
public readonly readdirSync: FsSynchronousApi['readdirSync'] = notSupported;
public readonly readSync: FsSynchronousApi['readSync'] = notSupported;
public readonly realpathSync: FsSynchronousApi['realpathSync'] = notSupported;
public readonly writeSync: FsSynchronousApi['writeSync'] = notSupported;
Expand Down
6 changes: 6 additions & 0 deletions src/fsa-to-node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface FsaNodeSyncAdapterApi {
mkdtemp(req: [filename: string, opts?: misc.TMode | opts.IOptions]): string;
trunc(req: [filename: string, len: number]): void;
unlink(req: [filename: string]): void;
readdir(req: [filename: string]): FsaNodeSyncAdapterEntry[];
}

export interface FsaNodeSyncAdapter {
Expand All @@ -33,3 +34,8 @@ export interface FsaNodeSyncAdapterStats {
kind: 'file' | 'directory';
size?: number;
}

export interface FsaNodeSyncAdapterEntry {
kind: 'file' | 'directory';
name: string;
}
11 changes: 10 additions & 1 deletion src/fsa-to-node/worker/FsaNodeSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {
FsaNodeWorkerMsgRequest,
FsaNodeWorkerMsgRootSet,
} from './types';
import type { FsLocation, FsaNodeSyncAdapterApi, FsaNodeSyncAdapterStats } from '../types';
import type { FsLocation, FsaNodeSyncAdapterApi, FsaNodeSyncAdapterStats, FsaNodeSyncAdapterEntry } from '../types';
import {IDirent} from '../../node/types/misc';

export class FsaNodeSyncWorker {
protected readonly sab: SharedArrayBuffer = new SharedArrayBuffer(1024 * 32);
Expand Down Expand Up @@ -159,5 +160,13 @@ export class FsaNodeSyncWorker {
unlink: async ([filename]): Promise<void> => {
await this.fs.promises.unlink(filename);
},
readdir: async ([filename]): Promise<FsaNodeSyncAdapterEntry[]> => {
const list = await this.fs.promises.readdir(filename, { withFileTypes: true, encoding: 'utf8' }) as IDirent[];
const res = list.map(entry => <FsaNodeSyncAdapterEntry>({
kind: entry.isDirectory() ? 'directory' : 'file',
name: entry.name,
}));
return res;
},
};
}

0 comments on commit 2178a50

Please sign in to comment.