-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
227 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
'use strict'; | ||
const { lstatSync, readdirSync } = require('fs'); | ||
const { join, resolve } = require('path'); | ||
|
||
const { | ||
kEmptyObject, | ||
} = require('internal/util'); | ||
const { isRegExp } = require('internal/util/types'); | ||
const { | ||
validateFunction, | ||
validateObject, | ||
} = require('internal/validators'); | ||
|
||
const { | ||
ArrayPrototypeForEach, | ||
ArrayPrototypeMap, | ||
ArrayPrototypeFlatMap, | ||
ArrayPrototypePop, | ||
ArrayPrototypePush, | ||
SafeMap, | ||
SafeSet, | ||
} = primordials; | ||
|
||
let minimatch; | ||
function lazyMinimatch() { | ||
minimatch ??= require('internal/deps/minimatch/index'); | ||
return minimatch; | ||
} | ||
|
||
function testPattern(pattern, path) { | ||
if (pattern === lazyMinimatch().GLOBSTAR) { | ||
return true; | ||
} | ||
if (typeof pattern === 'string') { | ||
return true; | ||
} | ||
if (typeof pattern.test === 'function') { | ||
return pattern.test(path); | ||
} | ||
} | ||
|
||
class Cache { | ||
#caches = new SafeMap(); | ||
#statsCache = new SafeMap(); | ||
#readdirCache = new SafeMap(); | ||
|
||
stats(path) { | ||
if (this.#statsCache.has(path)) { | ||
return this.#statsCache.get(path); | ||
} | ||
let val; | ||
try { | ||
val = lstatSync(path); | ||
} catch { | ||
val = null; | ||
} | ||
this.#statsCache.set(path, val); | ||
return val; | ||
} | ||
readdir(path) { | ||
if (this.#readdirCache.has(path)) { | ||
return this.#readdirCache.get(path); | ||
} | ||
let val; | ||
try { | ||
val = readdirSync(path, { withFileTypes: true }); | ||
ArrayPrototypeForEach(val, (dirent) => this.#statsCache.set(join(path, dirent.name), dirent)); | ||
} catch { | ||
val = []; | ||
} | ||
this.#readdirCache.set(path, val); | ||
return val; | ||
} | ||
|
||
seen(pattern, index, path) { | ||
return this.#caches.get(path)?.get(pattern)?.has(index); | ||
} | ||
add(pattern, index, path) { | ||
if (!this.#caches.has(path)) { | ||
this.#caches.set(path, new SafeMap([[pattern, new SafeSet([index])]])); | ||
} else if (!this.#caches.get(path)?.has(pattern)) { | ||
this.#caches.get(path)?.set(pattern, new SafeSet([index])); | ||
} else { | ||
this.#caches.get(path)?.get(pattern)?.add(index); | ||
} | ||
} | ||
|
||
} | ||
|
||
function glob(patterns, options = kEmptyObject) { | ||
validateObject(options, 'options'); | ||
const root = options.cwd ?? '.'; | ||
const { exclude } = options; | ||
if (exclude != null) { | ||
validateFunction(exclude, 'options.exclude'); | ||
} | ||
|
||
const { Minimatch, GLOBSTAR } = lazyMinimatch(); | ||
const results = new SafeSet(); | ||
const matchers = ArrayPrototypeMap(patterns, (pattern) => new Minimatch(pattern)); | ||
const queue = ArrayPrototypeFlatMap(matchers, (matcher) => { | ||
return ArrayPrototypeMap(matcher.set, | ||
(pattern) => ({ __proto__: null, pattern, index: 0, path: '.', followSymlinks: true })); | ||
}); | ||
const cache = new Cache(matchers); | ||
|
||
while (queue.length > 0) { | ||
const { pattern, index: currentIndex, path, followSymlinks } = ArrayPrototypePop(queue); | ||
if (cache.seen(pattern, currentIndex, path)) { | ||
continue; | ||
} | ||
cache.add(pattern, currentIndex, path); | ||
|
||
const currentPattern = pattern[currentIndex]; | ||
const index = currentIndex + 1; | ||
const isLast = pattern.length === index || (pattern.length === index + 1 && pattern[index] === ''); | ||
|
||
if (currentPattern === '') { | ||
// Absolute path | ||
ArrayPrototypePush(queue, { __proto__: null, pattern, index, path: '/', followSymlinks }); | ||
continue; | ||
} | ||
|
||
if (typeof currentPattern === 'string') { | ||
const entryPath = join(path, currentPattern); | ||
if (isLast && cache.stats(resolve(root, entryPath))) { | ||
// last path | ||
results.add(entryPath); | ||
} else if (!isLast) { | ||
// Keep traversing, we only check file existence for the last path | ||
ArrayPrototypePush(queue, { __proto__: null, pattern, index, path: entryPath, followSymlinks }); | ||
} | ||
continue; | ||
} | ||
|
||
const fullpath = resolve(root, path); | ||
const stat = cache.stats(fullpath); | ||
const isDirectory = stat?.isDirectory() || (followSymlinks !== false && stat?.isSymbolicLink()); | ||
|
||
if (isDirectory && isRegExp(currentPattern)) { | ||
const entries = cache.readdir(fullpath); | ||
for (const entry of entries) { | ||
const entryPath = join(path, entry.name); | ||
if (cache.seen(pattern, index, entryPath)) { | ||
continue; | ||
} | ||
const matches = testPattern(currentPattern, entry.name); | ||
if (matches && isLast) { | ||
results.add(entryPath); | ||
} else if (matches) { | ||
ArrayPrototypePush(queue, { __proto__: null, pattern, index, path: entryPath, followSymlinks }); | ||
} | ||
} | ||
} | ||
|
||
if (currentPattern === GLOBSTAR && isDirectory) { | ||
const entries = cache.readdir(fullpath); | ||
for (const entry of entries) { | ||
if (entry.name[0] === '.' || (exclude && exclude(entry.name))) { | ||
continue; | ||
} | ||
const entryPath = join(path, entry.name); | ||
if (cache.seen(pattern, index, entryPath)) { | ||
continue; | ||
} | ||
const isSymbolicLink = entry.isSymbolicLink(); | ||
const isDirectory = entry.isDirectory(); | ||
if (isDirectory) { | ||
// Push child directory to queue at same pattern index | ||
ArrayPrototypePush(queue, { | ||
__proto__: null, pattern, index: currentIndex, path: entryPath, followSymlinks: !isSymbolicLink, | ||
}); | ||
} | ||
|
||
if (pattern.length === index || (isSymbolicLink && pattern.length === index + 1 && pattern[index] === '')) { | ||
results.add(entryPath); | ||
} else if (pattern[index] === '..') { | ||
continue; | ||
} else if (!isLast && | ||
(isDirectory || (isSymbolicLink && (typeof pattern[index] !== 'string' || pattern[0] !== GLOBSTAR)))) { | ||
ArrayPrototypePush(queue, { __proto__: null, pattern, index, path: entryPath, followSymlinks }); | ||
} | ||
} | ||
if (isLast) { | ||
results.add(path); | ||
} else { | ||
ArrayPrototypePush(queue, { __proto__: null, pattern, index, path, followSymlinks }); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
__proto__: null, | ||
results, | ||
matchers, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
glob, | ||
lazyMinimatch, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.