MCPcopy Index your code
hub / github.com/nodejs/node / readdir

Function readdir

lib/fs.js:1814–1856  ·  view source on GitHub ↗

* Reads the contents of a directory. * @param {string | Buffer | URL} path * @param {string | { * encoding?: string; * withFileTypes?: boolean; * recursive?: boolean; * }} [options] * @param {( * err?: Error, * files?: string[] | Buffer[] | Dirent[] * ) => any} callback * @ret

(path, options, callback)

Source from the content-addressed store, hash-verified

1812 * @returns {void}
1813 */
1814function readdir(path, options, callback) {
1815 if (typeof options === 'function') {
1816 callback = options;
1817 options = undefined;
1818 }
1819
1820 const h = vfsState.handlers;
1821 if (h !== null && vfsResult(h.readdir(path, options), callback)) return;
1822
1823 callback = makeCallback(callback);
1824 options = getOptions(options);
1825 path = getValidatedPath(path);
1826 if (options.recursive != null) {
1827 validateBoolean(options.recursive, 'options.recursive');
1828 }
1829
1830 if (options.recursive) {
1831 // Make shallow copy to prevent mutating options from affecting results
1832 options = copyObject(options);
1833
1834 readdirRecursive(path, options, callback);
1835 return;
1836 }
1837
1838 const req = new FSReqCallback();
1839 if (!options.withFileTypes) {
1840 req.oncomplete = callback;
1841 } else {
1842 req.oncomplete = (err, result) => {
1843 if (err) {
1844 callback(err);
1845 return;
1846 }
1847 getDirents(path, result, callback);
1848 };
1849 }
1850 binding.readdir(
1851 path,
1852 options.encoding,
1853 !!options.withFileTypes,
1854 req,
1855 );
1856}
1857
1858/**
1859 * Synchronously reads the contents of a directory.

Callers

nothing calls this directly

Calls 8

vfsResultFunction · 0.85
copyObjectFunction · 0.85
getDirentsFunction · 0.85
makeCallbackFunction · 0.70
readdirRecursiveFunction · 0.70
getOptionsFunction · 0.50
callbackFunction · 0.50
readdirMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…