Skip to content

Commit

Permalink
Fixed readdirSync
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Sep 23, 2023
1 parent 59f86be commit 0ea6bc5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/emulation/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Stats } from '../stats';
*/
async function doOp<F extends keyof FileSystem, FN extends FileSystem[F]>(fn: F, resolveSymlinks: boolean, ...[path, ...args]: Parameters<FN>): Promise<ReturnType<FN>> {
path = normalizePath(path);
const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && await exists(path) ? await realpath(path) : path);
const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && (await exists(path)) ? await realpath(path) : path);
try {
// @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple)
return fs[fn](resolvedPath, ...args) as Promise<ReturnType<FN>>;
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function exists(path: string): Promise<boolean> {
const { fs, path: resolvedPath } = resolveFS(path);
return fs.exists(resolvedPath, cred);
} catch (e) {
if((e as ApiError).errno == ErrorCode.ENOENT) {
if ((e as ApiError).errno == ErrorCode.ENOENT) {
return false;
}

Expand Down
19 changes: 16 additions & 3 deletions src/emulation/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FileFlag } from '../file';
import { FileContents, FileSystem } from '../filesystem';
import { Stats } from '../stats';
import type { symlink, ReadSyncOptions } from 'fs';
import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError } from './shared';
import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError, mounts } from './shared';

function doOp<F extends keyof FileSystem, FN extends FileSystem[F]>(fn: F, resolveSymlinks: boolean, ...[path, ...args]: Parameters<FN>): ReturnType<FN> {
path = normalizePath(path);
Expand Down Expand Up @@ -50,7 +50,7 @@ export function existsSync(path: string): boolean {
const { fs, path: resolvedPath } = resolveFS(path);
return fs.existsSync(resolvedPath, cred);
} catch (e) {
if((e as ApiError).errno == ErrorCode.ENOENT) {
if ((e as ApiError).errno == ErrorCode.ENOENT) {
return false;
}

Expand Down Expand Up @@ -356,7 +356,20 @@ export function mkdirSync(path: string, mode?: number | string): void {
* @return [String[]]
*/
export function readdirSync(path: string): string[] {
return doOp('readdirSync', true, path, cred);
path = normalizePath(path);
const entries = doOp('readdirSync', true, path, cred);
const points = [...mounts.keys()];
for (const point of points) {
if (point.startsWith(path)) {
const entry = point.slice(path.length);
if (entry.includes('/') || entry.length == 0) {
// ignore FSs mounted in subdirectories and any FS mounted to `path`.
continue;
}
entries.push(entry);
}
}
return entries;
}

// SYMLINK METHODS
Expand Down

0 comments on commit 0ea6bc5

Please sign in to comment.