Socket
Socket
Sign inDemoInstall

@nodelib/fs.scandir

Package Overview
Dependencies
3
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.5 to 3.0.0

out/scandir.d.ts

20

out/adapters/fs.d.ts
import type * as fsStat from '@nodelib/fs.stat';
import type { Dirent, ErrnoException } from '../types';
export interface ReaddirAsynchronousMethod {
(filepath: string, options: {
withFileTypes: true;
}, callback: (error: ErrnoException | null, files: Dirent[]) => void): void;
(filepath: string, callback: (error: ErrnoException | null, files: string[]) => void): void;
}
export interface ReaddirSynchronousMethod {
(filepath: string, options: {
withFileTypes: true;
}): Dirent[];
(filepath: string): string[];
}
export declare type FileSystemAdapter = fsStat.FileSystemAdapter & {
export type ReaddirAsynchronousMethod = (filepath: string, options: {
withFileTypes: true;
}, callback: (error: ErrnoException | null, files: Dirent[]) => void) => void;
export type ReaddirSynchronousMethod = (filepath: string, options: {
withFileTypes: true;
}) => Dirent[];
export type FileSystemAdapter = fsStat.FileSystemAdapter & {
readdir: ReaddirAsynchronousMethod;

@@ -17,0 +11,0 @@ readdirSync: ReaddirSynchronousMethod;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
const fs = require("fs");
const fs = require("node:fs");
exports.FILE_SYSTEM_ADAPTER = {

@@ -11,3 +11,3 @@ lstat: fs.lstat,

readdir: fs.readdir,
readdirSync: fs.readdirSync
readdirSync: fs.readdirSync,
};

@@ -18,4 +18,7 @@ function createFileSystemAdapter(fsMethods) {

}
return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
return {
...exports.FILE_SYSTEM_ADAPTER,
...fsMethods,
};
}
exports.createFileSystemAdapter = createFileSystemAdapter;

@@ -1,12 +0,5 @@

import type { FileSystemAdapter, ReaddirAsynchronousMethod, ReaddirSynchronousMethod } from './adapters/fs';
import * as async from './providers/async';
import Settings, { Options } from './settings';
import type { Dirent, Entry } from './types';
declare type AsyncCallback = async.AsyncCallback;
declare function scandir(path: string, callback: AsyncCallback): void;
declare function scandir(path: string, optionsOrSettings: Options | Settings, callback: AsyncCallback): void;
declare namespace scandir {
function __promisify__(path: string, optionsOrSettings?: Options | Settings): Promise<Entry[]>;
}
declare function scandirSync(path: string, optionsOrSettings?: Options | Settings): Entry[];
export { scandir, scandirSync, Settings, AsyncCallback, Dirent, Entry, FileSystemAdapter, ReaddirAsynchronousMethod, ReaddirSynchronousMethod, Options };
export { scandir, scandirSync } from './scandir';
export { Settings } from './settings';
export type { FileSystemAdapter, ReaddirSynchronousMethod, ReaddirAsynchronousMethod } from './adapters/fs';
export type { Dirent, Entry, AsyncCallback } from './types';
export type { Options } from './settings';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Settings = exports.scandirSync = exports.scandir = void 0;
const async = require("./providers/async");
const sync = require("./providers/sync");
const settings_1 = require("./settings");
exports.Settings = settings_1.default;
function scandir(path, optionsOrSettingsOrCallback, callback) {
if (typeof optionsOrSettingsOrCallback === 'function') {
async.read(path, getSettings(), optionsOrSettingsOrCallback);
return;
}
async.read(path, getSettings(optionsOrSettingsOrCallback), callback);
}
exports.scandir = scandir;
function scandirSync(path, optionsOrSettings) {
const settings = getSettings(optionsOrSettings);
return sync.read(path, settings);
}
exports.scandirSync = scandirSync;
function getSettings(settingsOrOptions = {}) {
if (settingsOrOptions instanceof settings_1.default) {
return settingsOrOptions;
}
return new settings_1.default(settingsOrOptions);
}
var scandir_1 = require("./scandir");
Object.defineProperty(exports, "scandir", { enumerable: true, get: function () { return scandir_1.scandir; } });
Object.defineProperty(exports, "scandirSync", { enumerable: true, get: function () { return scandir_1.scandirSync; } });
var settings_1 = require("./settings");
Object.defineProperty(exports, "Settings", { enumerable: true, get: function () { return settings_1.Settings; } });

@@ -1,7 +0,3 @@

/// <reference types="node" />
import type Settings from '../settings';
import type { Entry } from '../types';
export declare type AsyncCallback = (error: NodeJS.ErrnoException, entries: Entry[]) => void;
import type { Settings } from '../settings';
import type { AsyncCallback } from '../types';
export declare function read(directory: string, settings: Settings, callback: AsyncCallback): void;
export declare function readdirWithFileTypes(directory: string, settings: Settings, callback: AsyncCallback): void;
export declare function readdir(directory: string, settings: Settings, callback: AsyncCallback): void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readdir = exports.readdirWithFileTypes = exports.read = void 0;
exports.read = void 0;
const fsStat = require("@nodelib/fs.stat");
const rpl = require("run-parallel");
const constants_1 = require("../constants");
const utils = require("../utils");
const common = require("./common");
function read(directory, settings, callback) {
if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
readdirWithFileTypes(directory, settings, callback);
return;
}
readdir(directory, settings, callback);
}
exports.read = read;
function readdirWithFileTypes(directory, settings, callback) {
settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {

@@ -26,10 +17,10 @@ if (readdirError !== null) {

name: dirent.name,
path: common.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
path: common.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator),
}));
if (!settings.followSymbolicLinks) {
if (!settings.stats && !settings.followSymbolicLinks) {
callSuccessCallback(callback, entries);
return;
}
const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
rpl(tasks, (rplError, rplEntries) => {
const tasks = makeRplTasks(entries, settings);
rpl(tasks, (rplError) => {
if (rplError !== null) {

@@ -39,23 +30,38 @@ callFailureCallback(callback, rplError);

}
callSuccessCallback(callback, rplEntries);
callSuccessCallback(callback, entries);
});
});
}
exports.readdirWithFileTypes = readdirWithFileTypes;
function makeRplTaskEntry(entry, settings) {
exports.read = read;
function makeRplTasks(entries, settings) {
const tasks = [];
for (const entry of entries) {
const task = makeRplTask(entry, settings);
if (task !== undefined) {
tasks.push(task);
}
}
return tasks;
}
/**
* The task mutates the incoming entry object depending on the settings.
* Returns the task, or undefined if the task is empty.
*/
function makeRplTask(entry, settings) {
const action = getStatsAction(entry, settings);
if (action === undefined) {
return undefined;
}
return (done) => {
if (!entry.dirent.isSymbolicLink()) {
done(null, entry);
return;
}
settings.fs.stat(entry.path, (statError, stats) => {
if (statError !== null) {
if (settings.throwErrorOnBrokenSymbolicLink) {
done(statError);
return;
}
done(null, entry);
action((error, stats) => {
if (error !== null) {
done(settings.throwErrorOnBrokenSymbolicLink ? error : null);
return;
}
entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);
if (settings.stats) {
entry.stats = stats;
}
if (settings.followSymbolicLinks) {
entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);
}
done(null, entry);

@@ -65,38 +71,15 @@ });

}
function readdir(directory, settings, callback) {
settings.fs.readdir(directory, (readdirError, names) => {
if (readdirError !== null) {
callFailureCallback(callback, readdirError);
return;
}
const tasks = names.map((name) => {
const path = common.joinPathSegments(directory, name, settings.pathSegmentSeparator);
return (done) => {
fsStat.stat(path, settings.fsStatSettings, (error, stats) => {
if (error !== null) {
done(error);
return;
}
const entry = {
name,
path,
dirent: utils.fs.createDirentFromStats(name, stats)
};
if (settings.stats) {
entry.stats = stats;
}
done(null, entry);
});
};
});
rpl(tasks, (rplError, entries) => {
if (rplError !== null) {
callFailureCallback(callback, rplError);
return;
}
callSuccessCallback(callback, entries);
});
});
function getStatsAction(entry, settings) {
if (settings.stats) {
return (callback) => {
fsStat.stat(entry.path, settings.fsStatSettings, callback);
};
}
if (settings.followSymbolicLinks && entry.dirent.isSymbolicLink()) {
return (callback) => {
settings.fs.stat(entry.path, callback);
};
}
return undefined;
}
exports.readdir = readdir;
function callFailureCallback(callback, error) {

@@ -103,0 +86,0 @@ callback(error);

@@ -1,5 +0,3 @@

import type Settings from '../settings';
import type { Settings } from '../settings';
import type { Entry } from '../types';
export declare function read(directory: string, settings: Settings): Entry[];
export declare function readdirWithFileTypes(directory: string, settings: Settings): Entry[];
export declare function readdir(directory: string, settings: Settings): Entry[];
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readdir = exports.readdirWithFileTypes = exports.read = void 0;
exports.read = void 0;
const fsStat = require("@nodelib/fs.stat");
const constants_1 = require("../constants");
const utils = require("../utils");
const common = require("./common");
function read(directory, settings) {
if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
return readdirWithFileTypes(directory, settings);
}
return readdir(directory, settings);
}
exports.read = read;
function readdirWithFileTypes(directory, settings) {
const dirents = settings.fs.readdirSync(directory, { withFileTypes: true });

@@ -21,7 +13,10 @@ return dirents.map((dirent) => {

name: dirent.name,
path: common.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
path: common.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator),
};
if (entry.dirent.isSymbolicLink() && settings.followSymbolicLinks) {
if (settings.stats) {
entry.stats = fsStat.statSync(entry.path, settings.fsStatSettings);
}
if (settings.followSymbolicLinks && entry.dirent.isSymbolicLink()) {
try {
const stats = settings.fs.statSync(entry.path);
const stats = entry.stats ?? settings.fs.statSync(entry.path);
entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);

@@ -38,19 +33,2 @@ }

}
exports.readdirWithFileTypes = readdirWithFileTypes;
function readdir(directory, settings) {
const names = settings.fs.readdirSync(directory);
return names.map((name) => {
const entryPath = common.joinPathSegments(directory, name, settings.pathSegmentSeparator);
const stats = fsStat.statSync(entryPath, settings.fsStatSettings);
const entry = {
name,
path: entryPath,
dirent: utils.fs.createDirentFromStats(name, stats)
};
if (settings.stats) {
entry.stats = stats;
}
return entry;
});
}
exports.readdir = readdir;
exports.read = read;

@@ -10,4 +10,3 @@ import * as fsStat from '@nodelib/fs.stat';

}
export default class Settings {
private readonly _options;
export declare class Settings {
readonly followSymbolicLinks: boolean;

@@ -19,4 +18,3 @@ readonly fs: fs.FileSystemAdapter;

readonly fsStatSettings: fsStat.Settings;
constructor(_options?: Options);
private _getValue;
constructor(options?: Options);
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
exports.Settings = void 0;
const path = require("node:path");
const fsStat = require("@nodelib/fs.stat");
const fs = require("./adapters/fs");
class Settings {
constructor(_options = {}) {
this._options = _options;
this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, false);
this.fs = fs.createFileSystemAdapter(this._options.fs);
this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path.sep);
this.stats = this._getValue(this._options.stats, false);
this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
followSymbolicLinks;
fs;
pathSegmentSeparator;
stats;
throwErrorOnBrokenSymbolicLink;
fsStatSettings;
constructor(options = {}) {
this.followSymbolicLinks = options.followSymbolicLinks ?? false;
this.fs = fs.createFileSystemAdapter(options.fs);
this.pathSegmentSeparator = options.pathSegmentSeparator ?? path.sep;
this.stats = options.stats ?? false;
this.throwErrorOnBrokenSymbolicLink = options.throwErrorOnBrokenSymbolicLink ?? true;
this.fsStatSettings = new fsStat.Settings({
followSymbolicLink: this.followSymbolicLinks,
fs: this.fs,
throwErrorOnBrokenSymbolicLink: this.throwErrorOnBrokenSymbolicLink
throwErrorOnBrokenSymbolicLink: this.throwErrorOnBrokenSymbolicLink,
});
}
_getValue(option, value) {
return option !== null && option !== void 0 ? option : value;
}
}
exports.default = Settings;
exports.Settings = Settings;
/// <reference types="node" />
import type * as fs from 'fs';
/// <reference types="node" />
import type * as fs from 'node:fs';
export interface Entry {

@@ -9,13 +10,5 @@ dirent: Dirent;

}
export declare type Stats = fs.Stats;
export declare type ErrnoException = NodeJS.ErrnoException;
export interface Dirent {
isBlockDevice: () => boolean;
isCharacterDevice: () => boolean;
isDirectory: () => boolean;
isFIFO: () => boolean;
isFile: () => boolean;
isSocket: () => boolean;
isSymbolicLink: () => boolean;
name: string;
}
export type Dirent = fs.Dirent;
export type Stats = fs.Stats;
export type ErrnoException = NodeJS.ErrnoException;
export type AsyncCallback = (error: ErrnoException | null, entries: Entry[]) => void;

@@ -1,2 +0,10 @@

import type { Dirent, Stats } from '../types';
export declare function createDirentFromStats(name: string, stats: Stats): Dirent;
/// <reference types="node" />
import * as fs from 'node:fs';
import type { Dirent } from '../types';
declare const kStats: unique symbol;
export declare function createDirentFromStats(name: string, stats: fs.Stats): Dirent;
export declare class DirentFromStats extends fs.Dirent {
private readonly [kStats];
constructor(name: string, stats: fs.Stats);
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDirentFromStats = void 0;
class DirentFromStats {
exports.DirentFromStats = exports.createDirentFromStats = void 0;
const fs = require("node:fs");
const kStats = Symbol('stats');
function createDirentFromStats(name, stats) {
return new DirentFromStats(name, stats);
}
exports.createDirentFromStats = createDirentFromStats;
// Adapting an internal class in Node.js to mimic the behavior of `fs.Dirent` when creating it manually from `fs.Stats`.
// https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L199C1-L213
class DirentFromStats extends fs.Dirent {
[kStats];
constructor(name, stats) {
this.name = name;
this.isBlockDevice = stats.isBlockDevice.bind(stats);
this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
this.isDirectory = stats.isDirectory.bind(stats);
this.isFIFO = stats.isFIFO.bind(stats);
this.isFile = stats.isFile.bind(stats);
this.isSocket = stats.isSocket.bind(stats);
this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
// @ts-expect-error The constructor has parameters, but they are not represented in types.
// https://github.com/nodejs/node/blob/a4cf6b204f0b160480153dc293ae748bf15225f9/lib/internal/fs/utils.js#L164
super(name, null);
this[kStats] = stats;
}
}
function createDirentFromStats(name, stats) {
return new DirentFromStats(name, stats);
exports.DirentFromStats = DirentFromStats;
for (const key of Reflect.ownKeys(fs.Dirent.prototype)) {
const name = key;
if (name === 'constructor') {
continue;
}
DirentFromStats.prototype[name] = function () {
return this[kStats][name]();
};
}
exports.createDirentFromStats = createDirentFromStats;

@@ -1,2 +0,1 @@

import * as fs from './fs';
export { fs };
export * as fs from './fs';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fs = void 0;
const fs = require("./fs");
exports.fs = fs;
exports.fs = require("./fs");
{
"name": "@nodelib/fs.scandir",
"version": "2.1.5",
"version": "3.0.0",
"description": "List files and directories inside the specified directory",

@@ -17,6 +17,7 @@ "license": "MIT",

"engines": {
"node": ">= 8"
"node": ">=16.14.0"
},
"files": [
"out/**",
"!out/benchmark",
"!out/**/*.map",

@@ -31,16 +32,19 @@ "!out/**/*.spec.*"

"compile": "tsc -b .",
"compile:watch": "tsc -p . --watch --sourceMap",
"compile:watch": "tsc -b . --watch --sourceMap",
"test": "mocha \"out/**/*.spec.js\" -s 0",
"build": "npm run clean && npm run compile && npm run lint && npm test",
"watch": "npm run clean && npm run compile:watch"
"watch": "npm run clean && npm run compile:watch",
"bench": "npm run bench:sync && npm run bench:async",
"bench:sync": "hereby bench:sync",
"bench:async": "hereby bench:async"
},
"dependencies": {
"@nodelib/fs.stat": "2.0.5",
"run-parallel": "^1.1.9"
"@nodelib/fs.stat": "3.0.0",
"run-parallel": "^1.2.0"
},
"devDependencies": {
"@nodelib/fs.macchiato": "1.0.4",
"@nodelib/fs.macchiato": "2.0.0",
"@nodelib/fs.scandir.previous": "npm:@nodelib/fs.scandir@3",
"@types/run-parallel": "^1.1.0"
},
"gitHead": "d6a7960d5281d3dd5f8e2efba49bb552d090f562"
}
}

@@ -10,3 +10,2 @@ # @nodelib/fs.scandir

* :moneybag: Returns useful information: `name`, `path`, `dirent` and `stats` (optional).
* :gear: On Node.js 10.10+ uses the mechanism without additional calls to determine the entry type. See [`old` and `modern` mode](#old-and-modern-mode).
* :link: Can safely work with broken symbolic links.

@@ -47,3 +46,3 @@

const entries = fsScandir.scandirSync('path', {});
const entries = fsScandir.scandirSync(('path', new fsScandir.Settings());
const entries = fsScandir.scandirSync('path', new fsScandir.Settings());
```

@@ -82,3 +81,3 @@

* `path` — The path of the entry relative to call directory (`root/unknown.txt`).
* `dirent` — An instance of [`fs.Dirent`](./src/types/index.ts) class. On Node.js below 10.10 will be emulated by [`DirentFromStats`](./src/utils/fs.ts) class.
* `dirent` — An instance of [`fs.Dirent`](./src/types/index.ts) class. When the `stats` option is enabled, it will be emulated by [`DirentFromStats`](./src/utils/fs.ts) class.
* `stats` (optional) — An instance of `fs.Stats` class.

@@ -150,20 +149,2 @@

## `old` and `modern` mode
This package has two modes that are used depending on the environment and parameters of use.
### old
* Node.js below `10.10` or when the `stats` option is enabled
When working in the old mode, the directory is read first (`fs.readdir`), then the type of entries is determined (`fs.lstat` and/or `fs.stat` for symbolic links).
### modern
* Node.js 10.10+ and the `stats` option is disabled
In the modern mode, reading the directory (`fs.readdir` with the `withFileTypes` option) is combined with obtaining information about its entries. An additional call for symbolic links (`fs.stat`) is still present.
This mode makes fewer calls to the file system. It's faster.
## Changelog

@@ -170,0 +151,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc