forked from Andrews54757/FastStream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiglob.mjs
641 lines (556 loc) · 15.8 KB
/
miniglob.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* eslint-disable */
//
// miniglob is a port of golang path/filepath
//
// Original Go source code Copyright (c) 2009 The Go Authors.
// All rights reserved. See LICENSE-go for complete license.
//
// This code licensed under MIT, Copyright (c) 2018 Rasmus Andersson.
// See LICENSE for complete license.
//
import {readdirSync, statSync} from 'fs';
const DIRSEP = (() => {
try {
return require('path').sep;
} catch (_) {
return '/';
}
})();
const DIRSEP_BYTE = DIRSEP.charCodeAt(0);
const DIRSEP_RE_PG = DIRSEP == ':' ? /\:+/g : DIRSEP == '\\' ? /\\+/g : /\/+/g;
const WIN32 = process.platform == 'win32';
export class PatternError extends Error {
constructor() {
super('bad pattern');
}
}
export function glob(pattern) {
if (pattern.indexOf('**') < 0) {
return glob0(pattern);
}
const matches = [];
const filesVisited = new Set();
deepglob('', pattern.split(/\*{2,}/), 0, matches, filesVisited);
return matches;
}
function log() {
// console.log.apply(console, arguments)
}
// volumeNameLen returns length of the leading volume name on Windows.
// It returns 0 elsewhere.
const volumeNameLen = WIN32 ? (path) => {
if (path.length < 2) {
return 0;
}
// with drive letter
const c = path[0];
if (path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z')) {
return 2;
}
// TODO: check for UNC
return 0;
} : (path) => 0;
// cleanGlobPath prepares path for glob matching.
// cleanGlobPath(path :string) : [prefixLen int, cleaned string]
//
const cleanGlobPath = (
WIN32 ? (path, volumeNameLen) => { // (prefixLen int, cleaned string)
let vollen = volumeNameLen(path);
if (path == '') {
return [0, '.'];
}
if (vollen+1 == path.length && isPathSep(path, path.length-1)) {
// /, \, C:\ and C:/ -- do nothing to the path
return [vollen + 1, path];
}
if (vollen == path.length && path.length == 2) { // C:
return [vollen, path + '.']; // convert C: into C:.
}
if (vollen >= path.length) {
vollen = path.length - 1;
}
return [vollen, path.substr(0, path.length-1)]; // chop off trailing separator
} : (path, volumeNameLen) => {
if (path == '') {
return [volumeNameLen, '.'];
}
if (path == DIRSEP) {
// do nothing to the path
return [volumeNameLen, path];
}
return [volumeNameLen, path.substr(0, path.length-1)]; // chop off trailing separator
}
);
function isPathSep(s, i) {
return s.charCodeAt(i) === DIRSEP_BYTE;
}
// ("/foo///") => "/foo"
function stripDirSepRight(s) {
const e = s.length - 1; let p = e;
while (s.charCodeAt(p) === DIRSEP_BYTE) {
p--;
}
return p != e ? s.substr(0, p + 1) : s;
}
// ("///foo/") => "foo/"
function stripDirSepLeft(s) {
const e = 0; let p = e;
while (s.charCodeAt(p) === DIRSEP_BYTE) {
p++;
}
return p != e ? s.substr(p) : s;
}
// deepglob(parts :string[]) string[] | null
//
function deepglob(dir, parts, partIndex, matches, filesVisited) {
if (partIndex >= parts.length) {
partIndex = parts.length - 1;
}
const part = parts[partIndex];
let pattern = part;
if (partIndex === 0) {
// first part
if (part.charCodeAt(part.length - 1) != DIRSEP_BYTE) {
// e.g. input="part**"; part="part"; pattern="part*"
pattern += '*';
} // else e.g. input="part/**"
} else if (partIndex === parts.length-1) {
// last part
if (part.charCodeAt(0) != DIRSEP_BYTE) {
// e.g. input="**part"; part="part"; pattern="*part"
pattern = '*' + pattern;
} // else e.g. input="**/part"
} else {
// mid part
if (part.charCodeAt(0) != DIRSEP_BYTE) {
// e.g. input="**part**"; part="part"; pattern="*part"
pattern = '*' + pattern;
} // else e.g. input="**/part**"
if (part.charCodeAt(part.length - 1) != DIRSEP_BYTE) {
// e.g. input="**part**"; part="part"; pattern="part*"
pattern += '*';
} // else e.g. input="**part/**"
}
function markVisit(path) {
if (filesVisited.has(path)) {
return false;
}
filesVisited.add(path);
return true;
}
function maybeAddFile(path) {
let filename = path;
const p = filename.lastIndexOf(DIRSEP);
if (p != -1) {
filename = filename.substr(p + 1);
}
const nextPartIndex = Math.min(partIndex + 1, parts.length - 1);
let pat = parts.slice(nextPartIndex).join('*');
if (pat.charCodeAt(0) == DIRSEP_BYTE) {
pat = stripDirSepLeft(pat);
} else if (pat[0] != '*') {
pat = '*' + pat;
}
if (match(pat, filename)) {
matches.push(path);
}
}
if (dir) {
if (pattern[0] != DIRSEP) {
pattern = dir + DIRSEP + pattern;
} else {
pattern = dir + pattern;
}
}
let isDirPattern = false;
if (pattern.charCodeAt(pattern.length-1) === DIRSEP_BYTE) {
isDirPattern = true;
pattern = stripDirSepRight(pattern);
}
const paths = glob0(pattern);
for (const path of paths) {
const st = statSync(path);
if (st.isDirectory()) {
if (markVisit(path)) {
walkdir(path, (path, st) => {
if (markVisit(path)) {
if (st.isDirectory()) {
deepglob(path, parts, partIndex + 1, matches, filesVisited);
} else {
maybeAddFile(path);
}
}
});
}
} else if (!isDirPattern && markVisit(path)) {
maybeAddFile(path);
}
}
return matches;
}
function walkdir(dir, callback) {
dir = pclean(dir);
const st = statSync(dir);
if (st.isDirectory()) {
_walkdir(dir, callback, new Set([st.ino]));
}
}
function _walkdir(dir, callback, visitedInodes) {
for (const name of readdirSync(dir)) {
const path = pjoin(dir, name);
const st = stat(path);
const result = callback(path, st);
if (
(result || result === undefined) &&
st && st.isDirectory() && !visitedInodes.has(st.ino)
) {
visitedInodes.add(st.ino);
_walkdir(path, callback, visitedInodes);
}
}
}
// Glob returns the names of all files matching pattern or null
// if there is no matching file. The syntax of patterns is the same
// as in Match. The pattern may describe hierarchical names such as
// /usr/*/bin/ed (assuming the Separator is '/').
//
// Glob ignores file system errors such as I/O errors reading directories.
// The only possible error is 'bad pattern', when pattern is malformed.
//
// glob(pattern :string) : string[] | null (matches)
function glob0(pattern) {
const matches = [];
if (!hasMeta(pattern)) {
if (stat(pattern)) {
return [pattern];
}
return matches;
}
// dirname, basename
let volumeLen = volumeNameLen(pattern);
let i = pattern.length - 1;
while (i >= volumeLen && !isPathSep(pattern, i)) {
i--;
}
let dir = pattern.substr(0, i+1);
const file = pattern.substr(i+1)
;[volumeLen, dir] = cleanGlobPath(dir, volumeLen);
if (!hasMeta(dir.substr(volumeLen))) {
_glob(dir, file, matches);
} else {
// Prevent infinite recursion. See golang issue 15879.
if (dir == pattern) {
throw new PatternError();
}
const m = glob0(dir); // :string[]
for (const d of m) {
_glob(d, file, matches);
}
}
return matches;
}
// glob searches for files matching pattern in the directory dir
// and appends them to matches. If the directory cannot be
// opened, it returns the existing matches. New matches are
// added in lexicographical order.
//
// _glob(dir :string, pattern :string, matches :string[])
//
function _glob(dir, pattern, matches) {
const fi = stat(dir);
if (fi === null) {
return;
}
if (!fi.isDirectory()) {
return;
}
let names;
try {
names = readdirSync(dir);
} catch (_) {
return;
}
names.sort();
for (const n of names) {
if (match(pattern, n)) {
matches.push(pjoin(dir, n));
}
}
}
// hasMeta reports whether path contains any of the magic characters
// recognized by match.
function hasMeta(path /* string*/) /* bool*/ {
for (let i = 0; i < path.length; ++i) {
switch (path.charCodeAt(i)) {
case 0x2A: // *
case 0x3F: // ?
case 0x5B: // [
case 0x7B: // {
return true;
}
}
return false;
}
// strContainsCh(s :string, c :int) : bool
function strContainsCh(s, c) {
for (let i = 0; i < s.length; ++i) {
if (s.charCodeAt(i) === c) {
return true;
}
}
return false;
}
// Match reports whether name matches the shell file name pattern.
// The pattern syntax is:
//
// pattern:
// { term }
// term:
// '*' matches any sequence of non-Separator characters
// '?' matches any single non-Separator character
// '[' [ '^' ] { character-range } ']'
// character class (must be non-empty)
// c matches character c (c != '*', '?', '\\', '[')
// '\\' c matches character c
//
// character-range:
// c matches character c (c != '\\', '-', ']')
// '\\' c matches character c
// lo '-' hi matches character c for lo <= c <= hi
//
// Match requires pattern to match all of name, not just a substring.
// The only possible returned error is ErrBadPattern, when pattern
// is malformed.
//
// On Windows, escaping is disabled. Instead, '\\' is treated as
// path separator.
//
// match(pattern :string, name :string) : bool (matched)
//
export function match(pattern, name) {
Pattern:
while (pattern.length > 0) {
let star = false; // :bool
let chunk = ''; // :string
const patternin = pattern
;[star, chunk, pattern] = scanChunk(pattern);
log(`scanChunk(%o) => %o`, patternin, [star, chunk, pattern]);
if (star && chunk == '') {
log('ret');
// Trailing * matches rest of string unless it has a /.
return !strContainsCh(name, DIRSEP_BYTE);
}
// Look for match at current position.
let [t, ok] = matchChunk(chunk, name);
log(`matchChunk(%o, %o) => %o`, chunk, name, [t, ok]);
// if we're the last chunk, make sure we've exhausted the name
// otherwise we'll give a false result even if we could still match
// using the star
if (ok && (t.length == 0 || pattern.length > 0)) {
name = t;
continue;
}
if (star) {
// Look for match skipping i+1 bytes.
// Cannot skip /.
for (let i = 0; i < name.length && name.charCodeAt(i) != DIRSEP_BYTE; i++) {
;[t, ok] = matchChunk(chunk, name.substr(i+1));
if (ok) {
// if we're the last chunk, make sure we exhausted the name
if (pattern.length == 0 && t.length > 0) {
continue;
}
name = t;
continue Pattern;
}
}
}
return false;
}
return name.length == 0;
}
// scanChunk gets the next segment of pattern, which is a non-star string
// possibly preceded by a star.
//
// scanChunk(pattern :string) : [star bool, chunk string, rest string]
function scanChunk(pattern) {
let star = false;
while (pattern.length > 0 && pattern.charCodeAt(0) == 0x2A) { // *
pattern = pattern.substr(1);
star = true;
}
let inrange = false;
let i = 0 >> 0; // int
Scan:
for (; i < pattern.length; i++) {
switch (pattern.charCodeAt(i)) {
case 0x5C: // \
if (!WIN32) {
// error check handled in matchChunk: bad pattern.
if (i + 1 < pattern.length) {
i++;
}
}
break;
case 0x5B: // [
inrange = true;
break;
case 0x5D: // ]
inrange = false;
break;
case 0x2A: // *
if (!inrange) {
break Scan;
}
break;
}
}
return [star, pattern.substr(0, i), pattern.substr(i)];
}
// matchChunk checks whether chunk matches the beginning of s.
// If so, it returns the remainder of s (after the match).
// Chunk is all single-character operators: literals, char classes, and ?.
//
// matchChunk(chunk :string, s :string) [rest string, ok bool]
//
function matchChunk(chunk, s) {
log('enter matchChunk(%o, %o)', chunk, s);
while (chunk.length > 0) {
if (s.length == 0) {
return ['', false];
}
switch (chunk.charCodeAt(0)) {
case 0x5B: { // [
// character class
// let [r, n] = utf8.DecodeRuneInString(s)
const r = s.codePointAt(0);
const n = r <= 0xFFFF ? 1 : 2;
s = s.substr(n);
chunk = chunk.substr(1);
log('"[" s reduced to %o, chunk reduced to %o, r = 0x%s', s, chunk, r.toString(16));
// We can't end right after '[', we're expecting at least
// a closing bracket and possibly a caret.
if (chunk.length == 0) {
throw new PatternError();
}
// possibly negated
const negated = chunk.charCodeAt(0) == 0x5E; // ^
if (negated) {
log('"[" negation from "^"');
chunk = chunk.substr(1);
}
// parse all ranges
let match = false;
let nrange = 0;
while (true) {
if (chunk.length > 0 && chunk.charCodeAt(0) == 0x5D /* ]*/ && nrange > 0) {
log('"[" loop break at A');
chunk = chunk.substr(1);
break;
}
let ok; let lo; // bool, unichar
const debug_chunk = chunk
;[lo, chunk, ok] = getEsc(chunk); // [ok bool, r rune, nchunk string]
log('"[" loop getEsc(%o) => %o', debug_chunk, [lo, chunk, ok]);
if (!ok) {
log('"[" loop return at B from failed getEsc');
return ['', false];
}
let hi = lo;
if (chunk.charCodeAt(0) == 0x2D /* -*/) {
;[hi, chunk, ok] = getEsc(chunk.substr(1));
if (!ok) {
return ['', false];
}
}
if (lo <= r && r <= hi) {
match = true;
}
nrange++;
}
if (match == negated) {
return ['', false];
}
break;
}
case 0x3F: // ?
if (s.charCodeAt(0) == DIRSEP_BYTE) {
return ['', false];
}
// _, n := utf8.DecodeRuneInString(s)
const r = s.codePointAt(0);
const n = r <= 0xFFFF ? 1 : 2;
s = s.substr(n);
chunk = chunk.substr(1);
break;
case 0x5C: // \
if (!WIN32) {
log('"\\" consume');
chunk = chunk.substr(1);
if (chunk.length == 0) {
throw new PatternError();
}
}
// fallthrough
default:
if (chunk.charCodeAt(0) != s.charCodeAt(0)) {
log('[def] chunk[0] != s[0] (%o != %o) (0x%s != 0x%s) -- return',
chunk[0], s[0], chunk.charCodeAt(0).toString(16), s.charCodeAt(0).toString(16));
return ['', false];
}
s = s.substr(1);
chunk = chunk.substr(1);
log('[def] s reduced to %o, chunk reduced to %o', s, chunk);
break;
}
}
return [s, true];
}
// getEsc gets a possibly-escaped character from chunk, for a character class.
//
// getEsc(chunk :string) : [ r rune, nchunk string, ok bool ]
//
function getEsc(chunk) {
let r = 0;
let nchunk = '';
const c = chunk.charCodeAt(0);
if (chunk.length == 0 || c == 0x2D /* -*/ || c == 0x5D /* ]*/) {
throw new PatternError();
}
if (c == 0x5C /* \*/ && !WIN32) {
chunk = chunk.substr(1);
if (chunk.length == 0) {
throw new PatternError();
}
}
r = chunk.codePointAt(0);
const n = r <= 0xFFFF ? 1 : 2;
if (r == 0xFFFF && n == 1) {
throw new PatternError();
}
nchunk = chunk.substr(n);
if (nchunk.length == 0) {
throw new PatternError();
}
return [r, nchunk, true];
}
function stat(path) {
try {
return statSync(path);
} catch (_) {}
return null;
}
function pjoin(path1, path2) {
return (path1 == '.' || path1 == '') ? path2 : path1 + DIRSEP + path2;
}
// pclean("foo//bar//") => "foo/bar"
function pclean(path) {
const endi = path.length - 1; let i = endi;
while (i && path.charCodeAt(i) === DIRSEP_BYTE) {
--i;
}
if (i != endi) {
path = path.substr(0, i + 1);
}
return path.split(DIRSEP_RE_PG).join(DIRSEP);
}