1// synchronous utility for filtering entries and calculating subwalks
2import { GLOBSTAR } from 'minimatch';
3/**
4 * A cache of which patterns have been processed for a given Path
5 */
6export class HasWalkedCache {
7    store;
8    constructor(store = new Map()) {
9        this.store = store;
10    }
11    copy() {
12        return new HasWalkedCache(new Map(this.store));
13    }
14    hasWalked(target, pattern) {
15        return this.store.get(target.fullpath())?.has(pattern.globString());
16    }
17    storeWalked(target, pattern) {
18        const fullpath = target.fullpath();
19        const cached = this.store.get(fullpath);
20        if (cached)
21            cached.add(pattern.globString());
22        else
23            this.store.set(fullpath, new Set([pattern.globString()]));
24    }
25}
26/**
27 * A record of which paths have been matched in a given walk step,
28 * and whether they only are considered a match if they are a directory,
29 * and whether their absolute or relative path should be returned.
30 */
31export class MatchRecord {
32    store = new Map();
33    add(target, absolute, ifDir) {
34        const n = (absolute ? 2 : 0) | (ifDir ? 1 : 0);
35        const current = this.store.get(target);
36        this.store.set(target, current === undefined ? n : n & current);
37    }
38    // match, absolute, ifdir
39    entries() {
40        return [...this.store.entries()].map(([path, n]) => [
41            path,
42            !!(n & 2),
43            !!(n & 1),
44        ]);
45    }
46}
47/**
48 * A collection of patterns that must be processed in a subsequent step
49 * for a given path.
50 */
51export class SubWalks {
52    store = new Map();
53    add(target, pattern) {
54        if (!target.canReaddir()) {
55            return;
56        }
57        const subs = this.store.get(target);
58        if (subs) {
59            if (!subs.find(p => p.globString() === pattern.globString())) {
60                subs.push(pattern);
61            }
62        }
63        else
64            this.store.set(target, [pattern]);
65    }
66    get(target) {
67        const subs = this.store.get(target);
68        /* c8 ignore start */
69        if (!subs) {
70            throw new Error('attempting to walk unknown path');
71        }
72        /* c8 ignore stop */
73        return subs;
74    }
75    entries() {
76        return this.keys().map(k => [k, this.store.get(k)]);
77    }
78    keys() {
79        return [...this.store.keys()].filter(t => t.canReaddir());
80    }
81}
82/**
83 * The class that processes patterns for a given path.
84 *
85 * Handles child entry filtering, and determining whether a path's
86 * directory contents must be read.
87 */
88export class Processor {
89    hasWalkedCache;
90    matches = new MatchRecord();
91    subwalks = new SubWalks();
92    patterns;
93    follow;
94    dot;
95    opts;
96    constructor(opts, hasWalkedCache) {
97        this.opts = opts;
98        this.follow = !!opts.follow;
99        this.dot = !!opts.dot;
100        this.hasWalkedCache = hasWalkedCache
101            ? hasWalkedCache.copy()
102            : new HasWalkedCache();
103    }
104    processPatterns(target, patterns) {
105        this.patterns = patterns;
106        const processingSet = patterns.map(p => [target, p]);
107        // map of paths to the magic-starting subwalks they need to walk
108        // first item in patterns is the filter
109        for (let [t, pattern] of processingSet) {
110            this.hasWalkedCache.storeWalked(t, pattern);
111            const root = pattern.root();
112            const absolute = pattern.isAbsolute() && this.opts.absolute !== false;
113            // start absolute patterns at root
114            if (root) {
115                t = t.resolve(root === '/' && this.opts.root !== undefined
116                    ? this.opts.root
117                    : root);
118                const rest = pattern.rest();
119                if (!rest) {
120                    this.matches.add(t, true, false);
121                    continue;
122                }
123                else {
124                    pattern = rest;
125                }
126            }
127            if (t.isENOENT())
128                continue;
129            let p;
130            let rest;
131            let changed = false;
132            while (typeof (p = pattern.pattern()) === 'string' &&
133                (rest = pattern.rest())) {
134                const c = t.resolve(p);
135                t = c;
136                pattern = rest;
137                changed = true;
138            }
139            p = pattern.pattern();
140            rest = pattern.rest();
141            if (changed) {
142                if (this.hasWalkedCache.hasWalked(t, pattern))
143                    continue;
144                this.hasWalkedCache.storeWalked(t, pattern);
145            }
146            // now we have either a final string for a known entry,
147            // more strings for an unknown entry,
148            // or a pattern starting with magic, mounted on t.
149            if (typeof p === 'string') {
150                // must not be final entry, otherwise we would have
151                // concatenated it earlier.
152                const ifDir = p === '..' || p === '' || p === '.';
153                this.matches.add(t.resolve(p), absolute, ifDir);
154                continue;
155            }
156            else if (p === GLOBSTAR) {
157                // if no rest, match and subwalk pattern
158                // if rest, process rest and subwalk pattern
159                // if it's a symlink, but we didn't get here by way of a
160                // globstar match (meaning it's the first time THIS globstar
161                // has traversed a symlink), then we follow it. Otherwise, stop.
162                if (!t.isSymbolicLink() ||
163                    this.follow ||
164                    pattern.checkFollowGlobstar()) {
165                    this.subwalks.add(t, pattern);
166                }
167                const rp = rest?.pattern();
168                const rrest = rest?.rest();
169                if (!rest || ((rp === '' || rp === '.') && !rrest)) {
170                    // only HAS to be a dir if it ends in **/ or **/.
171                    // but ending in ** will match files as well.
172                    this.matches.add(t, absolute, rp === '' || rp === '.');
173                }
174                else {
175                    if (rp === '..') {
176                        // this would mean you're matching **/.. at the fs root,
177                        // and no thanks, I'm not gonna test that specific case.
178                        /* c8 ignore start */
179                        const tp = t.parent || t;
180                        /* c8 ignore stop */
181                        if (!rrest)
182                            this.matches.add(tp, absolute, true);
183                        else if (!this.hasWalkedCache.hasWalked(tp, rrest)) {
184                            this.subwalks.add(tp, rrest);
185                        }
186                    }
187                }
188            }
189            else if (p instanceof RegExp) {
190                this.subwalks.add(t, pattern);
191            }
192        }
193        return this;
194    }
195    subwalkTargets() {
196        return this.subwalks.keys();
197    }
198    child() {
199        return new Processor(this.opts, this.hasWalkedCache);
200    }
201    // return a new Processor containing the subwalks for each
202    // child entry, and a set of matches, and
203    // a hasWalkedCache that's a copy of this one
204    // then we're going to call
205    filterEntries(parent, entries) {
206        const patterns = this.subwalks.get(parent);
207        // put matches and entry walks into the results processor
208        const results = this.child();
209        for (const e of entries) {
210            for (const pattern of patterns) {
211                const absolute = pattern.isAbsolute();
212                const p = pattern.pattern();
213                const rest = pattern.rest();
214                if (p === GLOBSTAR) {
215                    results.testGlobstar(e, pattern, rest, absolute);
216                }
217                else if (p instanceof RegExp) {
218                    results.testRegExp(e, p, rest, absolute);
219                }
220                else {
221                    results.testString(e, p, rest, absolute);
222                }
223            }
224        }
225        return results;
226    }
227    testGlobstar(e, pattern, rest, absolute) {
228        if (this.dot || !e.name.startsWith('.')) {
229            if (!pattern.hasMore()) {
230                this.matches.add(e, absolute, false);
231            }
232            if (e.canReaddir()) {
233                // if we're in follow mode or it's not a symlink, just keep
234                // testing the same pattern. If there's more after the globstar,
235                // then this symlink consumes the globstar. If not, then we can
236                // follow at most ONE symlink along the way, so we mark it, which
237                // also checks to ensure that it wasn't already marked.
238                if (this.follow || !e.isSymbolicLink()) {
239                    this.subwalks.add(e, pattern);
240                }
241                else if (e.isSymbolicLink()) {
242                    if (rest && pattern.checkFollowGlobstar()) {
243                        this.subwalks.add(e, rest);
244                    }
245                    else if (pattern.markFollowGlobstar()) {
246                        this.subwalks.add(e, pattern);
247                    }
248                }
249            }
250        }
251        // if the NEXT thing matches this entry, then also add
252        // the rest.
253        if (rest) {
254            const rp = rest.pattern();
255            if (typeof rp === 'string' &&
256                // dots and empty were handled already
257                rp !== '..' &&
258                rp !== '' &&
259                rp !== '.') {
260                this.testString(e, rp, rest.rest(), absolute);
261            }
262            else if (rp === '..') {
263                /* c8 ignore start */
264                const ep = e.parent || e;
265                /* c8 ignore stop */
266                this.subwalks.add(ep, rest);
267            }
268            else if (rp instanceof RegExp) {
269                this.testRegExp(e, rp, rest.rest(), absolute);
270            }
271        }
272    }
273    testRegExp(e, p, rest, absolute) {
274        if (!p.test(e.name))
275            return;
276        if (!rest) {
277            this.matches.add(e, absolute, false);
278        }
279        else {
280            this.subwalks.add(e, rest);
281        }
282    }
283    testString(e, p, rest, absolute) {
284        // should never happen?
285        if (!e.isNamed(p))
286            return;
287        if (!rest) {
288            this.matches.add(e, absolute, false);
289        }
290        else {
291            this.subwalks.add(e, rest);
292        }
293    }
294}
295//# sourceMappingURL=processor.js.map