@zenfs/core
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -268,2 +268,7 @@ import { Buffer } from 'buffer'; | ||
}): Promise<Dirent[]>; | ||
export declare function readdir(path: fs.PathLike, options?: { | ||
withFileTypes?: boolean; | ||
recursive?: boolean; | ||
encoding?: BufferEncoding | 'buffer' | null; | ||
} | BufferEncoding | 'buffer' | null): Promise<string[] | Dirent[] | Buffer[]>; | ||
export declare function link(targetPath: fs.PathLike, linkPath: fs.PathLike): Promise<void>; | ||
@@ -270,0 +275,0 @@ /** |
@@ -54,3 +54,3 @@ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { | ||
import { BigIntStats } from '../stats.js'; | ||
import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import { decodeUTF8, normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import * as constants from './constants.js'; | ||
@@ -631,2 +631,3 @@ import { Dir, Dirent } from './dir.js'; | ||
export async function readdir(path, options) { | ||
options = typeof options === 'object' ? options : { encoding: options }; | ||
path = normalizePath(path); | ||
@@ -638,9 +639,5 @@ if (!(await stat(path)).hasAccess(constants.R_OK)) { | ||
const { fs, path: resolved } = resolveMount(path); | ||
let entries; | ||
try { | ||
entries = await fs.readdir(resolved); | ||
} | ||
catch (e) { | ||
const entries = await fs.readdir(resolved).catch((e) => { | ||
throw fixError(e, { [resolved]: path }); | ||
} | ||
}); | ||
for (const point of mounts.keys()) { | ||
@@ -658,3 +655,29 @@ if (point.startsWith(path)) { | ||
for (const entry of entries) { | ||
values.push(typeof options == 'object' && options?.withFileTypes ? new Dirent(entry, await stat(join(path, entry))) : entry); | ||
const fullPath = join(path, entry); | ||
const stats = options?.recursive || options?.withFileTypes ? await stat(fullPath) : null; | ||
if (options?.withFileTypes) { | ||
values.push(new Dirent(entry, stats)); | ||
} | ||
else if (options?.encoding === 'buffer') { | ||
values.push(Buffer.from(entry)); | ||
} | ||
else { | ||
values.push(entry); | ||
} | ||
if (!options?.recursive || !stats?.isDirectory()) { | ||
continue; | ||
} | ||
for (const subEntry of await readdir(fullPath, options)) { | ||
if (subEntry instanceof Dirent) { | ||
subEntry.path = join(entry, subEntry.path); | ||
values.push(subEntry); | ||
} | ||
else if (Buffer.isBuffer(subEntry)) { | ||
// Convert Buffer to string, prefix with the full path | ||
values.push(Buffer.from(join(entry, decodeUTF8(subEntry)))); | ||
} | ||
else { | ||
values.push(join(entry, subEntry)); | ||
} | ||
} | ||
} | ||
@@ -664,3 +687,2 @@ return values; | ||
readdir; | ||
// SYMLINK METHODS | ||
export async function link(targetPath, linkPath) { | ||
@@ -667,0 +689,0 @@ targetPath = normalizePath(targetPath); |
@@ -131,2 +131,7 @@ import { Buffer } from 'buffer'; | ||
}) | BufferEncoding | null): string[] | Buffer[]; | ||
export declare function readdirSync(path: fs.PathLike, options?: { | ||
withFileTypes?: boolean; | ||
recursive?: boolean; | ||
encoding?: BufferEncoding | 'buffer' | null; | ||
} | BufferEncoding | 'buffer' | null): string[] | Dirent[] | Buffer[]; | ||
export declare function linkSync(targetPath: fs.PathLike, linkPath: fs.PathLike): void; | ||
@@ -133,0 +138,0 @@ /** |
@@ -52,3 +52,3 @@ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { | ||
import { BigIntStats } from '../stats.js'; | ||
import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import { decodeUTF8, normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import * as constants from './constants.js'; | ||
@@ -435,2 +435,3 @@ import { Dir, Dirent } from './dir.js'; | ||
export function readdirSync(path, options) { | ||
options = typeof options === 'object' ? options : { encoding: options }; | ||
path = normalizePath(path); | ||
@@ -459,11 +460,32 @@ const { fs, path: resolved } = resolveMount(existsSync(path) ? realpathSync(path) : path); | ||
} | ||
return entries.map((entry) => { | ||
if (typeof options == 'object' && options?.withFileTypes) { | ||
return new Dirent(entry, statSync(join(path.toString(), entry))); | ||
// Iterate over entries and handle recursive case if needed | ||
const values = []; | ||
for (const entry of entries) { | ||
const fullPath = join(path, entry); | ||
const entryStat = statSync(fullPath); | ||
if (options?.withFileTypes) { | ||
values.push(new Dirent(entry, entryStat)); | ||
} | ||
if (options == 'buffer' || (typeof options == 'object' && options?.encoding == 'buffer')) { | ||
return Buffer.from(entry); | ||
else if (options?.encoding === 'buffer') { | ||
values.push(Buffer.from(entry)); | ||
} | ||
return entry; | ||
}); | ||
else { | ||
values.push(entry); | ||
} | ||
if (!entryStat.isDirectory() || !options?.recursive) | ||
continue; | ||
for (const subEntry of readdirSync(fullPath, options)) { | ||
if (subEntry instanceof Dirent) { | ||
subEntry.path = join(entry, subEntry.path); | ||
values.push(subEntry); | ||
} | ||
else if (Buffer.isBuffer(subEntry)) { | ||
values.push(Buffer.from(join(entry, decodeUTF8(subEntry)))); | ||
} | ||
else { | ||
values.push(join(entry, subEntry)); | ||
} | ||
} | ||
} | ||
return values; | ||
} | ||
@@ -470,0 +492,0 @@ readdirSync; |
{ | ||
"name": "@zenfs/core", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A filesystem, anywhere", | ||
"funding": { | ||
"type": "individual", | ||
"url": "https://github.com/sponsors/james-pre" | ||
}, | ||
"main": "dist/index.js", | ||
@@ -6,0 +10,0 @@ "types": "dist/index.d.ts", |
@@ -16,3 +16,3 @@ /* eslint-disable @typescript-eslint/no-redundant-type-constituents */ | ||
import { BigIntStats, type Stats } from '../stats.js'; | ||
import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import { decodeUTF8, normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import * as constants from './constants.js'; | ||
@@ -699,15 +699,21 @@ import { Dir, Dirent } from './dir.js'; | ||
options?: { withFileTypes?: boolean; recursive?: boolean; encoding?: BufferEncoding | 'buffer' | null } | BufferEncoding | 'buffer' | null | ||
): Promise<string[] | Dirent[] | Buffer[]>; | ||
export async function readdir( | ||
path: fs.PathLike, | ||
options?: { withFileTypes?: boolean; recursive?: boolean; encoding?: BufferEncoding | 'buffer' | null } | BufferEncoding | 'buffer' | null | ||
): Promise<string[] | Dirent[] | Buffer[]> { | ||
options = typeof options === 'object' ? options : { encoding: options }; | ||
path = normalizePath(path); | ||
if (!(await stat(path)).hasAccess(constants.R_OK)) { | ||
throw ErrnoError.With('EACCES', path, 'readdir'); | ||
} | ||
path = (await exists(path)) ? await realpath(path) : path; | ||
const { fs, path: resolved } = resolveMount(path); | ||
let entries: string[]; | ||
try { | ||
entries = await fs.readdir(resolved); | ||
} catch (e) { | ||
const entries = await fs.readdir(resolved).catch((e: Error) => { | ||
throw fixError(e as Error, { [resolved]: path }); | ||
} | ||
}); | ||
for (const point of mounts.keys()) { | ||
@@ -723,6 +729,33 @@ if (point.startsWith(path)) { | ||
} | ||
const values: (string | Dirent)[] = []; | ||
const values: (string | Dirent | Buffer)[] = []; | ||
for (const entry of entries) { | ||
values.push(typeof options == 'object' && options?.withFileTypes ? new Dirent(entry, await stat(join(path, entry))) : entry); | ||
const fullPath = join(path, entry); | ||
const stats = options?.recursive || options?.withFileTypes ? await stat(fullPath) : null; | ||
if (options?.withFileTypes) { | ||
values.push(new Dirent(entry, stats!)); | ||
} else if (options?.encoding === 'buffer') { | ||
values.push(Buffer.from(entry)); | ||
} else { | ||
values.push(entry); | ||
} | ||
if (!options?.recursive || !stats?.isDirectory()) { | ||
continue; | ||
} | ||
for (const subEntry of await readdir(fullPath, options)) { | ||
if (subEntry instanceof Dirent) { | ||
subEntry.path = join(entry, subEntry.path); | ||
values.push(subEntry); | ||
} else if (Buffer.isBuffer(subEntry)) { | ||
// Convert Buffer to string, prefix with the full path | ||
values.push(Buffer.from(join(entry, decodeUTF8(subEntry)))); | ||
} else { | ||
values.push(join(entry, subEntry)); | ||
} | ||
} | ||
} | ||
return values as string[] | Dirent[]; | ||
@@ -732,4 +765,2 @@ } | ||
// SYMLINK METHODS | ||
export async function link(targetPath: fs.PathLike, linkPath: fs.PathLike): Promise<void> { | ||
@@ -736,0 +767,0 @@ targetPath = normalizePath(targetPath); |
@@ -8,3 +8,3 @@ import { Buffer } from 'buffer'; | ||
import { BigIntStats, type Stats } from '../stats.js'; | ||
import { normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import { decodeUTF8, normalizeMode, normalizeOptions, normalizePath, normalizeTime } from '../utils.js'; | ||
import * as constants from './constants.js'; | ||
@@ -443,4 +443,9 @@ import { Dir, Dirent } from './dir.js'; | ||
path: fs.PathLike, | ||
options?: { withFileTypes?: boolean; recursive?: boolean; encoding?: BufferEncoding | 'buffer' | null } | BufferEncoding | 'buffer' | null | ||
): string[] | Dirent[] | Buffer[]; | ||
export function readdirSync( | ||
path: fs.PathLike, | ||
options?: { recursive?: boolean; encoding?: BufferEncoding | 'buffer' | null; withFileTypes?: boolean } | BufferEncoding | 'buffer' | null | ||
): string[] | Dirent[] | Buffer[] { | ||
options = typeof options === 'object' ? options : { encoding: options }; | ||
path = normalizePath(path); | ||
@@ -457,2 +462,3 @@ const { fs, path: resolved } = resolveMount(existsSync(path) ? realpathSync(path) : path); | ||
} | ||
for (const mount of mounts.keys()) { | ||
@@ -469,13 +475,31 @@ if (!mount.startsWith(path)) { | ||
} | ||
return entries.map((entry: string) => { | ||
if (typeof options == 'object' && options?.withFileTypes) { | ||
return new Dirent(entry, statSync(join(path.toString(), entry))); | ||
// Iterate over entries and handle recursive case if needed | ||
const values: (string | Dirent | Buffer)[] = []; | ||
for (const entry of entries) { | ||
const fullPath = join(path, entry); | ||
const entryStat = statSync(fullPath); | ||
if (options?.withFileTypes) { | ||
values.push(new Dirent(entry, entryStat)); | ||
} else if (options?.encoding === 'buffer') { | ||
values.push(Buffer.from(entry)); | ||
} else { | ||
values.push(entry); | ||
} | ||
if (!entryStat.isDirectory() || !options?.recursive) continue; | ||
if (options == 'buffer' || (typeof options == 'object' && options?.encoding == 'buffer')) { | ||
return Buffer.from(entry); | ||
for (const subEntry of readdirSync(fullPath, options)) { | ||
if (subEntry instanceof Dirent) { | ||
subEntry.path = join(entry, subEntry.path); | ||
values.push(subEntry); | ||
} else if (Buffer.isBuffer(subEntry)) { | ||
values.push(Buffer.from(join(entry, decodeUTF8(subEntry)))); | ||
} else { | ||
values.push(join(entry, subEntry)); | ||
} | ||
} | ||
} | ||
return entry; | ||
}) as string[] | Dirent[] | Buffer[]; | ||
return values as string[] | Dirent[] | Buffer[]; | ||
} | ||
@@ -482,0 +506,0 @@ readdirSync satisfies typeof fs.readdirSync; |
757067
20812