feat(providence): apply memoisation in optimised-glob

This commit is contained in:
Thijs Louisse 2024-05-17 20:38:03 +02:00
parent c7341d75ba
commit 1a1bdb76db

View file

@ -4,14 +4,17 @@ import nodeFs from 'fs';
import path from 'path'; import path from 'path';
import { toPosixPath } from './to-posix-path.js'; import { toPosixPath } from './to-posix-path.js';
import { memoize } from './memoize.js';
/** /**
* @typedef {nodeFs} FsLike * @typedef {nodeFs} FsLike
* @typedef {nodeFs.Dirent & {path:string;parentPath:string}} DirentWithPath
* @typedef {{onlyDirectories:boolean;onlyFiles:boolean;deep:number;suppressErrors:boolean;fs: FsLike;cwd:string;absolute:boolean;extglob:boolean;}} FastGlobtions * @typedef {{onlyDirectories:boolean;onlyFiles:boolean;deep:number;suppressErrors:boolean;fs: FsLike;cwd:string;absolute:boolean;extglob:boolean;}} FastGlobtions
*/ */
const [nodeMajor] = process.versions.node.split('.').map(Number); const [nodeMajor] = process.versions.node.split('.').map(Number);
export const parseGlobToRegex = memoize(
/** /**
* @param {string} glob * @param {string} glob
* @param {object} [providedOpts] * @param {object} [providedOpts]
@ -19,7 +22,7 @@ const [nodeMajor] = process.versions.node.split('.').map(Number);
* @param {boolean} [providedOpts.extglob=true] if true, supports so called "extended" globs (like bash) and single character matching, matching ranges of characters, group matching etc. * @param {boolean} [providedOpts.extglob=true] if true, supports so called "extended" globs (like bash) and single character matching, matching ranges of characters, group matching etc.
* @returns {RegExp} * @returns {RegExp}
*/ */
export function parseGlobToRegex(glob, providedOpts) { (glob, providedOpts) => {
if (typeof glob !== 'string') throw new TypeError('Expected a string'); if (typeof glob !== 'string') throw new TypeError('Expected a string');
const options = { const options = {
@ -84,7 +87,9 @@ export function parseGlobToRegex(glob, providedOpts) {
regexResultStr += '.*'; regexResultStr += '.*';
} else { } else {
const isGlobstarSegment = const isGlobstarSegment =
isMultiStar && ['/', undefined].includes(prevChar) && ['/', undefined].includes(nextChar); isMultiStar &&
['/', undefined].includes(prevChar) &&
['/', undefined].includes(nextChar);
if (isGlobstarSegment) { if (isGlobstarSegment) {
// Match zero or more path segments // Match zero or more path segments
regexResultStr += '((?:[^/]*(?:/|$))*)'; regexResultStr += '((?:[^/]*(?:/|$))*)';
@ -101,12 +106,14 @@ export function parseGlobToRegex(glob, providedOpts) {
} }
return new RegExp(`^${regexResultStr}$`); return new RegExp(`^${regexResultStr}$`);
} },
);
const getStartPath = memoize(
/** /**
* @param {string} glob * @param {string} glob
*/ */
function getStartPath(glob) { glob => {
const reservedChars = ['?', '[', ']', '{', '}', ',', '.', '*']; const reservedChars = ['?', '[', ']', '{', '}', ',', '.', '*'];
let hasFoundReservedChar = false; let hasFoundReservedChar = false;
return glob return glob
@ -118,46 +125,69 @@ function getStartPath(glob) {
}) })
.filter(Boolean) .filter(Boolean)
.join('/'); .join('/');
} },
);
let isCacheEnabled = false; let isCacheEnabled = false;
/** @type {{[path:string]:nodeFs.Dirent[]}} */ /** @type {{[path:string]:DirentWithPath[]}} */
const cache = {}; const cache = {};
const getAllDirentsFromStartPath = memoize(
/** /**
* @param {string} startPath * @param {string} startPath
* @param {{fs?:FsLike, dirents?:nodeFs.Dirent[]}} providedOptions * @param {{fs?:FsLike, dirents?:DirentWithPath[]}} providedOptions
* @returns {Promise<nodeFs.Dirent[]>} * @returns {Promise<DirentWithPath[]>}
*/ */
async function getAllFilesFromStartPath( async (startPath, { fs = /** @type {* & FsLike} */ (nodeFs), dirents = [] } = {}) => {
startPath,
{ fs = /** @type {* & FsLike} */ (nodeFs), dirents = [] } = {},
) {
if (isCacheEnabled && cache[startPath]) return cache[startPath]; if (isCacheEnabled && cache[startPath]) return cache[startPath];
// Older node doesn't support recursive option // Older node doesn't support recursive option
if (nodeMajor < 18) { if (nodeMajor < 18) {
/** @type {nodeFs.Dirent[]} */ /** @type {nodeFs.Dirent[]} */
const direntsForLvl = await fs.promises.readdir(startPath, { withFileTypes: true }); const direntsForLvl = await fs.promises.readdir(startPath, { withFileTypes: true });
for (const dirent of direntsForLvl) { for (const _dirent of direntsForLvl) {
// @ts-expect-error const dirent = /** @type {DirentWithPath} */ (_dirent);
dirent.parentPath = dirent.path = startPath; // eslint-disable-line no-multi-assign dirent.parentPath = dirent.path = startPath; // eslint-disable-line no-multi-assign
dirents.push(dirent); dirents.push(/** @type {DirentWithPath} */ (dirent));
if (dirent.isDirectory()) { if (dirent.isDirectory()) {
const subDir = path.join(startPath, dirent.name); const subDir = path.join(startPath, dirent.name);
await getAllFilesFromStartPath(subDir, { fs, dirents }); await getAllDirentsFromStartPath(subDir, { fs, dirents });
} }
} }
return /** @type {nodeFs.Dirent[]} */ (dirents);
}
// @ts-expect-error
dirents.push(...(await fs.promises.readdir(startPath, { withFileTypes: true, recursive: true })));
cache[startPath] = dirents;
return dirents; return dirents;
} }
dirents.push(
// @ts-expect-error
...(await fs.promises.readdir(startPath, { withFileTypes: true, recursive: true })),
);
cache[startPath] = dirents;
return dirents;
},
);
const getAllDirentsRelativeToCwd = memoize(
/**
* @param {string} fullStartPath
* @param {{fs?:FsLike, cwd:string}} options
* @returns {Promise<{relativeToCwdPath:string;dirent:DirentWithPath}[]>}
*/
async (fullStartPath, options) => {
const allDirentsRelativeToStartPath = await getAllDirentsFromStartPath(fullStartPath, {
fs: options.fs,
});
const allDirEntsRelativeToCwd = allDirentsRelativeToStartPath.map(dirent => ({
relativeToCwdPath: toPosixPath(
path.join(dirent.parentPath || dirent.path, dirent.name),
).replace(`${toPosixPath(options.cwd)}/`, ''),
dirent,
}));
return allDirEntsRelativeToCwd;
},
);
/** /**
* Lightweight glob implementation. * Lightweight glob implementation.
* It's a drop-in replacement for globby, but it's faster, a few hundred lines of code and has no dependencies. * It's a drop-in replacement for globby, but it's faster, a few hundred lines of code and has no dependencies.
@ -219,18 +249,11 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
const fullStartPath = path.join(options.cwd, startPath); const fullStartPath = path.join(options.cwd, startPath);
try { try {
const allDirentsRelativeToStartPath = await getAllFilesFromStartPath(fullStartPath, { const allDirEntsRelativeToCwd = await getAllDirentsRelativeToCwd(fullStartPath, {
cwd: options.cwd,
fs: options.fs, fs: options.fs,
}); });
const allDirEntsRelativeToCwd = allDirentsRelativeToStartPath.map(dirent => ({
relativeToCwdPath: toPosixPath(
// @ts-expect-error
path.join(dirent.parentPath || dirent.path, dirent.name),
).replace(`${toPosixPath(options.cwd)}/`, ''),
dirent,
}));
globEntries.push(...allDirEntsRelativeToCwd); globEntries.push(...allDirEntsRelativeToCwd);
} catch (e) { } catch (e) {
if (!options.suppressErrors) { if (!options.suppressErrors) {
@ -262,8 +285,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
filteredPaths = filteredPaths.map(f => toPosixPath(path.join(options.cwd, f))); filteredPaths = filteredPaths.map(f => toPosixPath(path.join(options.cwd, f)));
if (process.platform === 'win32') { if (process.platform === 'win32') {
const driveLetter = path.win32.resolve(options.cwd).slice(0, 1).toUpperCase(); const driveChar = path.win32.resolve(options.cwd).slice(0, 1).toUpperCase();
filteredPaths = filteredPaths.map(f => `${driveLetter}:${f}`); filteredPaths = filteredPaths.map(f => `${driveChar}:${f}`);
} }
} }
@ -273,10 +296,16 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
const result = options.unique ? Array.from(new Set(filteredPaths)) : filteredPaths; const result = options.unique ? Array.from(new Set(filteredPaths)) : filteredPaths;
return result.sort((a, b) => { const res = result.sort((a, b) => {
const pathDiff = a.split('/').length - b.split('/').length; const pathDiff = a.split('/').length - b.split('/').length;
return pathDiff !== 0 ? pathDiff : a.localeCompare(b); return pathDiff !== 0 ? pathDiff : a.localeCompare(b);
}); });
// It could happen the fs changes with the next call, so we clear the cache
getAllDirentsRelativeToCwd.clearCache();
getAllDirentsFromStartPath.clearCache();
return res;
} }
optimisedGlob.disableCache = () => { optimisedGlob.disableCache = () => {