Skip to content

Commit 15612c5

Browse files
committed
fs: drop readdirRecursiveSync typeof guard, add typings
Always call the native readdirRecursiveSync binding and remove the unused JS fallback. Document readFileBuffer, writeFileBuffer, and readdirRecursiveSync on the fs internal binding typings. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
1 parent e76ef36 commit 15612c5

2 files changed

Lines changed: 30 additions & 54 deletions

File tree

lib/fs.js

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,70 +1762,35 @@ function handleFilePaths({ result, currentPath, context }) {
17621762
}
17631763

17641764
/**
1765-
* An iterative algorithm for reading the entire contents of the `basePath` directory.
1766-
* This function does not validate `basePath` as a directory. It is passed directly to
1767-
* `binding.readdir`.
1765+
* Reads the entire contents of the `basePath` directory recursively.
1766+
* This function does not validate `basePath` as a directory. It is passed
1767+
* directly to `binding.readdirRecursiveSync`.
17681768
* @param {string} basePath
17691769
* @param {{ encoding: string, withFileTypes: boolean }} options
17701770
* @returns {string[] | Dirent[]}
17711771
*/
17721772
function readdirSyncRecursive(basePath, options) {
17731773
const withFileTypes = Boolean(options.withFileTypes);
17741774
// C++ walk uses scandir dirent types and avoids per-entry stat() for the
1775-
// common case (known directory/file types). ~3x faster than the JS walk.
1776-
if (typeof binding.readdirRecursiveSync === 'function') {
1777-
const result = binding.readdirRecursiveSync(
1778-
basePath,
1779-
options.encoding,
1780-
withFileTypes,
1781-
);
1782-
if (result === undefined) {
1783-
return;
1784-
}
1785-
if (!withFileTypes) {
1786-
return result;
1787-
}
1788-
// result = [names, types, parentPaths]
1789-
const { 0: names, 1: types, 2: parentPaths } = result;
1790-
const out = new Array(names.length);
1791-
for (let i = 0; i < names.length; i++) {
1792-
out[i] = new Dirent(names[i], types[i], parentPaths[i]);
1793-
}
1794-
return out;
1795-
}
1796-
1797-
// Fallback if the native binding is unavailable.
1798-
const context = {
1799-
withFileTypes,
1800-
encoding: options.encoding,
1775+
// common case (known directory/file types).
1776+
const result = binding.readdirRecursiveSync(
18011777
basePath,
1802-
readdirResults: [],
1803-
pathsQueue: [basePath],
1804-
};
1805-
1806-
function read(path) {
1807-
const readdirResult = binding.readdir(
1808-
path,
1809-
context.encoding,
1810-
context.withFileTypes,
1811-
);
1812-
1813-
if (readdirResult === undefined) {
1814-
return;
1815-
}
1816-
1817-
processReaddirResult({
1818-
result: readdirResult,
1819-
currentPath: path,
1820-
context,
1821-
});
1778+
options.encoding,
1779+
withFileTypes,
1780+
);
1781+
if (result === undefined) {
1782+
return;
18221783
}
1823-
1824-
for (let i = 0; i < context.pathsQueue.length; i++) {
1825-
read(context.pathsQueue[i]);
1784+
if (!withFileTypes) {
1785+
return result;
18261786
}
1827-
1828-
return context.readdirResults;
1787+
// result = [names, types, parentPaths]
1788+
const { 0: names, 1: types, 2: parentPaths } = result;
1789+
const out = new Array(names.length);
1790+
for (let i = 0; i < names.length; i++) {
1791+
out[i] = new Dirent(names[i], types[i], parentPaths[i]);
1792+
}
1793+
return out;
18291794
}
18301795

18311796
/**

typings/internalBinding/fs.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ declare namespace InternalFSBinding {
180180
function readdir(path: StringOrBuffer, encoding: unknown, withFileTypes: false, usePromises: typeof kUsePromises): Promise<string[]>;
181181

182182
function readFileUtf8(path: StringOrBuffer, flags: number): string;
183+
function readFileBuffer(path: StringOrBuffer, flags: number): Buffer;
184+
185+
function readdirRecursiveSync(path: StringOrBuffer, encoding: unknown, withFileTypes: true): [string[], number[], string[]];
186+
function readdirRecursiveSync(path: StringOrBuffer, encoding: unknown, withFileTypes: false): string[];
187+
function readdirRecursiveSync(path: StringOrBuffer, encoding: unknown, withFileTypes: boolean): string[] | [string[], number[], string[]];
183188

184189
function readlink(path: StringOrBuffer, encoding: unknown, req: FSReqCallback<string | Buffer>): void;
185190
function readlink(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer;
@@ -241,6 +246,9 @@ declare namespace InternalFSBinding {
241246

242247
function writeFileUtf8(path: string, data: string, flag: number, mode: number): void;
243248
function writeFileUtf8(fd: number, data: string, flag: number, mode: number): void;
249+
250+
function writeFileBuffer(path: string, data: ArrayBufferView, flag: number, mode: number): void;
251+
function writeFileBuffer(fd: number, data: ArrayBufferView, flag: number, mode: number): void;
244252
}
245253

246254
export interface FsBinding {
@@ -283,6 +291,8 @@ export interface FsBinding {
283291
read: typeof InternalFSBinding.read;
284292
readBuffers: typeof InternalFSBinding.readBuffers;
285293
readdir: typeof InternalFSBinding.readdir;
294+
readdirRecursiveSync: typeof InternalFSBinding.readdirRecursiveSync;
295+
readFileBuffer: typeof InternalFSBinding.readFileBuffer;
286296
readFileUtf8: typeof InternalFSBinding.readFileUtf8;
287297
readlink: typeof InternalFSBinding.readlink;
288298
realpath: typeof InternalFSBinding.realpath;
@@ -295,6 +305,7 @@ export interface FsBinding {
295305
utimes: typeof InternalFSBinding.utimes;
296306
writeBuffer: typeof InternalFSBinding.writeBuffer;
297307
writeBuffers: typeof InternalFSBinding.writeBuffers;
308+
writeFileBuffer: typeof InternalFSBinding.writeFileBuffer;
298309
writeFileUtf8: typeof InternalFSBinding.writeFileUtf8;
299310
writeString: typeof InternalFSBinding.writeString;
300311

0 commit comments

Comments
 (0)