fluxion-ts 0.2.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,489 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  var fs = require('node:fs');
4
- var path = require('node:path');
4
+ var path$1 = require('node:path');
5
5
  var cluster = require('node:cluster');
6
6
  var os = require('node:os');
7
7
  var http = require('node:http');
8
8
  var https = require('node:https');
9
9
 
10
- const ANSI_BACKGROUND_OFFSET = 10;
11
-
12
- const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`;
13
-
14
- const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
15
-
16
- const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
17
-
18
- const styles$1 = {
19
- modifier: {
20
- reset: [0, 0],
21
- // 21 isn't widely supported and 22 does the same thing
22
- bold: [1, 22],
23
- dim: [2, 22],
24
- italic: [3, 23],
25
- underline: [4, 24],
26
- overline: [53, 55],
27
- inverse: [7, 27],
28
- hidden: [8, 28],
29
- strikethrough: [9, 29],
30
- },
31
- color: {
32
- black: [30, 39],
33
- red: [31, 39],
34
- green: [32, 39],
35
- yellow: [33, 39],
36
- blue: [34, 39],
37
- magenta: [35, 39],
38
- cyan: [36, 39],
39
- white: [37, 39],
40
-
41
- // Bright color
42
- blackBright: [90, 39],
43
- gray: [90, 39], // Alias of `blackBright`
44
- grey: [90, 39], // Alias of `blackBright`
45
- redBright: [91, 39],
46
- greenBright: [92, 39],
47
- yellowBright: [93, 39],
48
- blueBright: [94, 39],
49
- magentaBright: [95, 39],
50
- cyanBright: [96, 39],
51
- whiteBright: [97, 39],
52
- },
53
- bgColor: {
54
- bgBlack: [40, 49],
55
- bgRed: [41, 49],
56
- bgGreen: [42, 49],
57
- bgYellow: [43, 49],
58
- bgBlue: [44, 49],
59
- bgMagenta: [45, 49],
60
- bgCyan: [46, 49],
61
- bgWhite: [47, 49],
62
-
63
- // Bright color
64
- bgBlackBright: [100, 49],
65
- bgGray: [100, 49], // Alias of `bgBlackBright`
66
- bgGrey: [100, 49], // Alias of `bgBlackBright`
67
- bgRedBright: [101, 49],
68
- bgGreenBright: [102, 49],
69
- bgYellowBright: [103, 49],
70
- bgBlueBright: [104, 49],
71
- bgMagentaBright: [105, 49],
72
- bgCyanBright: [106, 49],
73
- bgWhiteBright: [107, 49],
74
- },
75
- };
76
-
77
- Object.keys(styles$1.modifier);
78
- const foregroundColorNames = Object.keys(styles$1.color);
79
- const backgroundColorNames = Object.keys(styles$1.bgColor);
80
- [...foregroundColorNames, ...backgroundColorNames];
81
-
82
- function assembleStyles() {
83
- const codes = new Map();
84
-
85
- for (const [groupName, group] of Object.entries(styles$1)) {
86
- for (const [styleName, style] of Object.entries(group)) {
87
- styles$1[styleName] = {
88
- open: `\u001B[${style[0]}m`,
89
- close: `\u001B[${style[1]}m`,
90
- };
91
-
92
- group[styleName] = styles$1[styleName];
93
-
94
- codes.set(style[0], style[1]);
95
- }
96
-
97
- Object.defineProperty(styles$1, groupName, {
98
- value: group,
99
- enumerable: false,
100
- });
101
- }
102
-
103
- Object.defineProperty(styles$1, 'codes', {
104
- value: codes,
105
- enumerable: false,
106
- });
107
-
108
- styles$1.color.close = '\u001B[39m';
109
- styles$1.bgColor.close = '\u001B[49m';
110
-
111
- styles$1.color.ansi = wrapAnsi16();
112
- styles$1.color.ansi256 = wrapAnsi256();
113
- styles$1.color.ansi16m = wrapAnsi16m();
114
- styles$1.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
115
- styles$1.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
116
- styles$1.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
117
-
118
- // From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
119
- Object.defineProperties(styles$1, {
120
- rgbToAnsi256: {
121
- value(red, green, blue) {
122
- // We use the extended greyscale palette here, with the exception of
123
- // black and white. normal palette only has 4 greyscale shades.
124
- if (red === green && green === blue) {
125
- if (red < 8) {
126
- return 16;
127
- }
128
-
129
- if (red > 248) {
130
- return 231;
131
- }
132
-
133
- return Math.round(((red - 8) / 247) * 24) + 232;
134
- }
135
-
136
- return 16
137
- + (36 * Math.round(red / 255 * 5))
138
- + (6 * Math.round(green / 255 * 5))
139
- + Math.round(blue / 255 * 5);
140
- },
141
- enumerable: false,
142
- },
143
- hexToRgb: {
144
- value(hex) {
145
- const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
146
- if (!matches) {
147
- return [0, 0, 0];
148
- }
149
-
150
- let [colorString] = matches;
151
-
152
- if (colorString.length === 3) {
153
- colorString = [...colorString].map(character => character + character).join('');
154
- }
155
-
156
- const integer = Number.parseInt(colorString, 16);
157
-
158
- return [
159
- /* eslint-disable no-bitwise */
160
- (integer >> 16) & 0xFF,
161
- (integer >> 8) & 0xFF,
162
- integer & 0xFF,
163
- /* eslint-enable no-bitwise */
164
- ];
165
- },
166
- enumerable: false,
167
- },
168
- hexToAnsi256: {
169
- value: hex => styles$1.rgbToAnsi256(...styles$1.hexToRgb(hex)),
170
- enumerable: false,
171
- },
172
- ansi256ToAnsi: {
173
- value(code) {
174
- if (code < 8) {
175
- return 30 + code;
176
- }
177
-
178
- if (code < 16) {
179
- return 90 + (code - 8);
180
- }
181
-
182
- let red;
183
- let green;
184
- let blue;
185
-
186
- if (code >= 232) {
187
- red = (((code - 232) * 10) + 8) / 255;
188
- green = red;
189
- blue = red;
190
- } else {
191
- code -= 16;
192
-
193
- const remainder = code % 36;
194
-
195
- red = Math.floor(code / 36) / 5;
196
- green = Math.floor(remainder / 6) / 5;
197
- blue = (remainder % 6) / 5;
198
- }
199
-
200
- const value = Math.max(red, green, blue) * 2;
201
-
202
- if (value === 0) {
203
- return 30;
204
- }
205
-
206
- // eslint-disable-next-line no-bitwise
207
- let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
208
-
209
- if (value === 2) {
210
- result += 60;
211
- }
212
-
213
- return result;
214
- },
215
- enumerable: false,
216
- },
217
- rgbToAnsi: {
218
- value: (red, green, blue) => styles$1.ansi256ToAnsi(styles$1.rgbToAnsi256(red, green, blue)),
219
- enumerable: false,
220
- },
221
- hexToAnsi: {
222
- value: hex => styles$1.ansi256ToAnsi(styles$1.hexToAnsi256(hex)),
223
- enumerable: false,
224
- },
225
- });
226
-
227
- return styles$1;
228
- }
229
-
230
- const ansiStyles = assembleStyles();
231
-
232
- /* eslint-env browser */
233
-
234
- const level = (() => {
235
- if (!('navigator' in globalThis)) {
236
- return 0;
237
- }
238
-
239
- if (globalThis.navigator.userAgentData) {
240
- const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
241
- if (brand && brand.version > 93) {
242
- return 3;
243
- }
244
- }
245
-
246
- if (/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)) {
247
- return 1;
248
- }
249
-
250
- return 0;
251
- })();
252
-
253
- const colorSupport = level !== 0 && {
254
- level};
255
-
256
- const supportsColor = {
257
- stdout: colorSupport,
258
- stderr: colorSupport,
259
- };
260
-
261
- // TODO: When targeting Node.js 16, use `String.prototype.replaceAll`.
262
- function stringReplaceAll(string, substring, replacer) {
263
- let index = string.indexOf(substring);
264
- if (index === -1) {
265
- return string;
266
- }
267
-
268
- const substringLength = substring.length;
269
- let endIndex = 0;
270
- let returnValue = '';
271
- do {
272
- returnValue += string.slice(endIndex, index) + substring + replacer;
273
- endIndex = index + substringLength;
274
- index = string.indexOf(substring, endIndex);
275
- } while (index !== -1);
276
-
277
- returnValue += string.slice(endIndex);
278
- return returnValue;
279
- }
280
-
281
- function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
282
- let endIndex = 0;
283
- let returnValue = '';
284
- do {
285
- const gotCR = string[index - 1] === '\r';
286
- returnValue += string.slice(endIndex, (gotCR ? index - 1 : index)) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
287
- endIndex = index + 1;
288
- index = string.indexOf('\n', endIndex);
289
- } while (index !== -1);
290
-
291
- returnValue += string.slice(endIndex);
292
- return returnValue;
293
- }
294
-
295
- const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
296
-
297
- const GENERATOR = Symbol('GENERATOR');
298
- const STYLER = Symbol('STYLER');
299
- const IS_EMPTY = Symbol('IS_EMPTY');
300
-
301
- // `supportsColor.level` → `ansiStyles.color[name]` mapping
302
- const levelMapping = [
303
- 'ansi',
304
- 'ansi',
305
- 'ansi256',
306
- 'ansi16m',
307
- ];
308
-
309
- const styles = Object.create(null);
310
-
311
- const applyOptions = (object, options = {}) => {
312
- if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
313
- throw new Error('The `level` option should be an integer from 0 to 3');
314
- }
315
-
316
- // Detect level if not set manually
317
- const colorLevel = stdoutColor ? stdoutColor.level : 0;
318
- object.level = options.level === undefined ? colorLevel : options.level;
319
- };
320
-
321
- const chalkFactory = options => {
322
- const chalk = (...strings) => strings.join(' ');
323
- applyOptions(chalk, options);
324
-
325
- Object.setPrototypeOf(chalk, createChalk.prototype);
326
-
327
- return chalk;
328
- };
329
-
330
- function createChalk(options) {
331
- return chalkFactory(options);
332
- }
333
-
334
- Object.setPrototypeOf(createChalk.prototype, Function.prototype);
335
-
336
- for (const [styleName, style] of Object.entries(ansiStyles)) {
337
- styles[styleName] = {
338
- get() {
339
- const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
340
- Object.defineProperty(this, styleName, {value: builder});
341
- return builder;
342
- },
343
- };
344
- }
345
-
346
- styles.visible = {
347
- get() {
348
- const builder = createBuilder(this, this[STYLER], true);
349
- Object.defineProperty(this, 'visible', {value: builder});
350
- return builder;
351
- },
352
- };
353
-
354
- const getModelAnsi = (model, level, type, ...arguments_) => {
355
- if (model === 'rgb') {
356
- if (level === 'ansi16m') {
357
- return ansiStyles[type].ansi16m(...arguments_);
358
- }
359
-
360
- if (level === 'ansi256') {
361
- return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
362
- }
363
-
364
- return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
365
- }
366
-
367
- if (model === 'hex') {
368
- return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
369
- }
370
-
371
- return ansiStyles[type][model](...arguments_);
372
- };
373
-
374
- const usedModels = ['rgb', 'hex', 'ansi256'];
375
-
376
- for (const model of usedModels) {
377
- styles[model] = {
378
- get() {
379
- const {level} = this;
380
- return function (...arguments_) {
381
- const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
382
- return createBuilder(this, styler, this[IS_EMPTY]);
383
- };
384
- },
385
- };
386
-
387
- const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
388
- styles[bgModel] = {
389
- get() {
390
- const {level} = this;
391
- return function (...arguments_) {
392
- const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
393
- return createBuilder(this, styler, this[IS_EMPTY]);
394
- };
395
- },
396
- };
397
- }
398
-
399
- const proto = Object.defineProperties(() => {}, {
400
- ...styles,
401
- level: {
402
- enumerable: true,
403
- get() {
404
- return this[GENERATOR].level;
405
- },
406
- set(level) {
407
- this[GENERATOR].level = level;
408
- },
409
- },
410
- });
411
-
412
- const createStyler = (open, close, parent) => {
413
- let openAll;
414
- let closeAll;
415
- if (parent === undefined) {
416
- openAll = open;
417
- closeAll = close;
418
- } else {
419
- openAll = parent.openAll + open;
420
- closeAll = close + parent.closeAll;
421
- }
422
-
423
- return {
424
- open,
425
- close,
426
- openAll,
427
- closeAll,
428
- parent,
429
- };
430
- };
431
-
432
- const createBuilder = (self, _styler, _isEmpty) => {
433
- // Single argument is hot path, implicit coercion is faster than anything
434
- // eslint-disable-next-line no-implicit-coercion
435
- const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
436
-
437
- // We alter the prototype because we must return a function, but there is
438
- // no way to create a function with a different prototype
439
- Object.setPrototypeOf(builder, proto);
440
-
441
- builder[GENERATOR] = self;
442
- builder[STYLER] = _styler;
443
- builder[IS_EMPTY] = _isEmpty;
444
-
445
- return builder;
446
- };
447
-
448
- const applyStyle = (self, string) => {
449
- if (self.level <= 0 || !string) {
450
- return self[IS_EMPTY] ? '' : string;
451
- }
452
-
453
- let styler = self[STYLER];
454
-
455
- if (styler === undefined) {
456
- return string;
457
- }
458
-
459
- const {openAll, closeAll} = styler;
460
- if (string.includes('\u001B')) {
461
- while (styler !== undefined) {
462
- // Replace any instances already present with a re-opening code
463
- // otherwise only the part of the string until said closing code
464
- // will be colored, and the rest will simply be 'plain'.
465
- string = stringReplaceAll(string, styler.close, styler.open);
466
-
467
- styler = styler.parent;
468
- }
469
- }
470
-
471
- // We can move both next actions out of loop, because remaining actions in loop won't have
472
- // any/visible effect on parts we add here. Close the styling before a linebreak and reopen
473
- // after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
474
- const lfIndex = string.indexOf('\n');
475
- if (lfIndex !== -1) {
476
- string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
477
- }
478
-
479
- return openAll + string + closeAll;
480
- };
481
-
482
- Object.defineProperties(createChalk.prototype, styles);
483
-
484
- const chalk = createChalk();
485
- createChalk({level: stderrColor ? stderrColor.level : 0});
486
-
487
10
  function dtm(dt = new Date()) {
488
11
  const y = dt.getFullYear();
489
12
  const m = String(dt.getMonth() + 1).padStart(2, '0');
@@ -523,20 +46,19 @@ const safeStringify = (value) => {
523
46
  }
524
47
  };
525
48
  const ColoredLevels = {
526
- INFO: chalk.hex('#0386e3')('INFO'),
527
- WARN: chalk.hex('#fb923c')('WARN'),
528
- ERROR: chalk.hex('#ef4444')('ERROR'),
529
- SUCC: chalk.hex('#22c55e')('SUCC'),
530
- DEBUG: chalk.hex('#d327e0')('DEBUG'),
531
- VERBOSE: chalk.hex('#36ffeb')('SUCC'),
49
+ INFO: 'INFO',
50
+ WARN: 'WARN',
51
+ ERROR: 'ERROR',
52
+ SUCC: 'SUCC',
53
+ DEBUG: 'DEBUG',
54
+ VERBOSE: 'VERBOSE',
532
55
  };
533
- const TimestampColor = chalk.hex('#166534');
534
56
  const oneLineLogger = (entry) => {
535
57
  const { level: rawLevel, timestamp: rawTimestamp, event: rawEvent, message: rawMessage, ...fields } = entry;
536
- const timestamp = TimestampColor(`[${rawTimestamp}]`);
58
+ const timestamp = `[${rawTimestamp}]`;
537
59
  const level = ColoredLevels[rawLevel] ?? rawLevel;
538
60
  const body = rawMessage ?? rawEvent;
539
- const fieldsText = $keys(fields).length > 0 ? ` ${chalk.dim(safeStringify(fields))}` : '';
61
+ const fieldsText = $keys(fields).length > 0 ? ` ${safeStringify(fields)}` : '';
540
62
  console.log(`${timestamp} ${level} ${body}${fieldsText}`);
541
63
  };
542
64
  /**
@@ -672,7 +194,7 @@ function readCertificateContent(content, moduleDir) {
672
194
  // Check if it looks like a file path (not a PEM certificate)
673
195
  // PEM certificates start with "-----BEGIN"
674
196
  if (!content.startsWith('-----BEGIN')) {
675
- const filePath = path.isAbsolute(content) ? content : path.join(moduleDir, content);
197
+ const filePath = path$1.isAbsolute(content) ? content : path$1.join(moduleDir, content);
676
198
  if (fs.existsSync(filePath)) {
677
199
  return fs.readFileSync(filePath);
678
200
  }
@@ -710,7 +232,20 @@ function normalizeHttpsOptions(https, moduleDir) {
710
232
  */
711
233
  function normalizeOptions(options) {
712
234
  expect.isObject(options, 'FluxionOptions must be an object');
713
- let { dir, host, port, metaPort, injections = [], moduleDir = process.cwd(), workerOptions = {}, maxRequestBytes = 8_000_000, reloadDelay = 300, apiExts = ['.ts'], routerExclude = [], https, } = options;
235
+ let { dir, host, port, metaPort, injections = [], moduleDir = process.cwd(), workerOptions = {}, maxRequestBytes = 8_000_000, reloadDelay = 300, include = ['**/*'], apiInclude = ['**/*.ts'], exclude = [
236
+ '**/node_modules/**',
237
+ '**/.git/**',
238
+ '**/dist/**',
239
+ '**/build/**',
240
+ '**/.vscode/**',
241
+ '**/.idea/**',
242
+ '**/*.log',
243
+ '**/.DS_Store',
244
+ '**/coverage/**',
245
+ '**/.nyc_output/**',
246
+ '**/*.tmp',
247
+ '**/*.temp',
248
+ ], https, } = options;
714
249
  const logger = options.logger ?? 'one-line';
715
250
  expectLoggerOption(logger);
716
251
  expect.isString(dir, 'FluxionOptions.dir must be a string');
@@ -749,8 +284,9 @@ function normalizeOptions(options) {
749
284
  workerOptions: resolveWorkerOptions(workerOptions),
750
285
  maxRequestBytes,
751
286
  logger,
752
- apiExts,
753
- routerExclude,
287
+ include,
288
+ apiInclude,
289
+ exclude,
754
290
  https: normalizeHttpsOptions(https, moduleDir),
755
291
  };
756
292
  }
@@ -1442,12 +978,14 @@ class FluxionWatcher {
1442
978
  * Recursively register all files in the options directory.
1443
979
  */
1444
980
  init() {
1445
- const dirPath = path.join(process.cwd(), this.cx.options.dir);
981
+ const dirPath = path$1.isAbsolute(this.cx.options.dir)
982
+ ? this.cx.options.dir
983
+ : path$1.join(process.cwd(), this.cx.options.dir);
1446
984
  const registerRecursive = (dir, relativePath) => {
1447
985
  const entries = fs.readdirSync(dir, { withFileTypes: true });
1448
986
  for (const entry of entries) {
1449
- const entryPath = path.join(dir, entry.name);
1450
- const entryRelativePath = path.join(relativePath, entry.name);
987
+ const entryPath = path$1.join(dir, entry.name);
988
+ const entryRelativePath = path$1.join(relativePath, entry.name);
1451
989
  if (entry.isDirectory()) {
1452
990
  registerRecursive(entryPath, entryRelativePath);
1453
991
  }
@@ -1522,76 +1060,2502 @@ class FluxionWatcher {
1522
1060
  }
1523
1061
  }
1524
1062
 
1525
- class FluxionRouter {
1526
- constructor(cx) {
1527
- /**
1528
- * This means the request has been handled by static resource handler, and no more response should be sent.
1529
- */
1530
- this.StaticHandled = Symbol.for('fluxion.router.StaticHandled');
1531
- this.handlers = new Map();
1532
- this.cx = cx;
1063
+ const balanced = (a, b, str) => {
1064
+ const ma = a instanceof RegExp ? maybeMatch(a, str) : a;
1065
+ const mb = b instanceof RegExp ? maybeMatch(b, str) : b;
1066
+ const r = ma !== null && mb != null && range(ma, mb, str);
1067
+ return (r && {
1068
+ start: r[0],
1069
+ end: r[1],
1070
+ pre: str.slice(0, r[0]),
1071
+ body: str.slice(r[0] + ma.length, r[1]),
1072
+ post: str.slice(r[1] + mb.length),
1073
+ });
1074
+ };
1075
+ const maybeMatch = (reg, str) => {
1076
+ const m = str.match(reg);
1077
+ return m ? m[0] : null;
1078
+ };
1079
+ const range = (a, b, str) => {
1080
+ let begs, beg, left, right = undefined, result;
1081
+ let ai = str.indexOf(a);
1082
+ let bi = str.indexOf(b, ai + 1);
1083
+ let i = ai;
1084
+ if (ai >= 0 && bi > 0) {
1085
+ if (a === b) {
1086
+ return [ai, bi];
1087
+ }
1088
+ begs = [];
1089
+ left = str.length;
1090
+ while (i >= 0 && !result) {
1091
+ if (i === ai) {
1092
+ begs.push(i);
1093
+ ai = str.indexOf(a, i + 1);
1094
+ }
1095
+ else if (begs.length === 1) {
1096
+ const r = begs.pop();
1097
+ if (r !== undefined)
1098
+ result = [r, bi];
1099
+ }
1100
+ else {
1101
+ beg = begs.pop();
1102
+ if (beg !== undefined && beg < left) {
1103
+ left = beg;
1104
+ right = bi;
1105
+ }
1106
+ bi = str.indexOf(b, i + 1);
1107
+ }
1108
+ i = ai < bi && ai >= 0 ? ai : bi;
1109
+ }
1110
+ if (begs.length && right !== undefined) {
1111
+ result = [left, right];
1112
+ }
1533
1113
  }
1534
- makeStaticResource(filepath) {
1535
- const fullPath = path.join(this.cx.options.dir, filepath);
1536
- return async (normalized, _req, res) => {
1537
- if (normalized.method !== 'GET' && normalized.method !== 'HEAD') {
1538
- res.statusCode = 405;
1539
- res.setHeader('Allow', 'GET, HEAD');
1540
- res.end();
1541
- return;
1114
+ return result;
1115
+ };
1116
+
1117
+ const escSlash = '\0SLASH' + Math.random() + '\0';
1118
+ const escOpen = '\0OPEN' + Math.random() + '\0';
1119
+ const escClose = '\0CLOSE' + Math.random() + '\0';
1120
+ const escComma = '\0COMMA' + Math.random() + '\0';
1121
+ const escPeriod = '\0PERIOD' + Math.random() + '\0';
1122
+ const escSlashPattern = new RegExp(escSlash, 'g');
1123
+ const escOpenPattern = new RegExp(escOpen, 'g');
1124
+ const escClosePattern = new RegExp(escClose, 'g');
1125
+ const escCommaPattern = new RegExp(escComma, 'g');
1126
+ const escPeriodPattern = new RegExp(escPeriod, 'g');
1127
+ const slashPattern = /\\\\/g;
1128
+ const openPattern = /\\{/g;
1129
+ const closePattern = /\\}/g;
1130
+ const commaPattern = /\\,/g;
1131
+ const periodPattern = /\\\./g;
1132
+ const EXPANSION_MAX = 100_000;
1133
+ function numeric(str) {
1134
+ return !isNaN(str) ? parseInt(str, 10) : str.charCodeAt(0);
1135
+ }
1136
+ function escapeBraces(str) {
1137
+ return str
1138
+ .replace(slashPattern, escSlash)
1139
+ .replace(openPattern, escOpen)
1140
+ .replace(closePattern, escClose)
1141
+ .replace(commaPattern, escComma)
1142
+ .replace(periodPattern, escPeriod);
1143
+ }
1144
+ function unescapeBraces(str) {
1145
+ return str
1146
+ .replace(escSlashPattern, '\\')
1147
+ .replace(escOpenPattern, '{')
1148
+ .replace(escClosePattern, '}')
1149
+ .replace(escCommaPattern, ',')
1150
+ .replace(escPeriodPattern, '.');
1151
+ }
1152
+ /**
1153
+ * Basically just str.split(","), but handling cases
1154
+ * where we have nested braced sections, which should be
1155
+ * treated as individual members, like {a,{b,c},d}
1156
+ */
1157
+ function parseCommaParts(str) {
1158
+ if (!str) {
1159
+ return [''];
1160
+ }
1161
+ const parts = [];
1162
+ const m = balanced('{', '}', str);
1163
+ if (!m) {
1164
+ return str.split(',');
1165
+ }
1166
+ const { pre, body, post } = m;
1167
+ const p = pre.split(',');
1168
+ p[p.length - 1] += '{' + body + '}';
1169
+ const postParts = parseCommaParts(post);
1170
+ if (post.length) {
1171
+ p[p.length - 1] += postParts.shift();
1172
+ p.push.apply(p, postParts);
1173
+ }
1174
+ parts.push.apply(parts, p);
1175
+ return parts;
1176
+ }
1177
+ function expand(str, options = {}) {
1178
+ if (!str) {
1179
+ return [];
1180
+ }
1181
+ const { max = EXPANSION_MAX } = options;
1182
+ // I don't know why Bash 4.3 does this, but it does.
1183
+ // Anything starting with {} will have the first two bytes preserved
1184
+ // but *only* at the top level, so {},a}b will not expand to anything,
1185
+ // but a{},b}c will be expanded to [a}c,abc].
1186
+ // One could argue that this is a bug in Bash, but since the goal of
1187
+ // this module is to match Bash's rules, we escape a leading {}
1188
+ if (str.slice(0, 2) === '{}') {
1189
+ str = '\\{\\}' + str.slice(2);
1190
+ }
1191
+ return expand_(escapeBraces(str), max, true).map(unescapeBraces);
1192
+ }
1193
+ function embrace(str) {
1194
+ return '{' + str + '}';
1195
+ }
1196
+ function isPadded(el) {
1197
+ return /^-?0\d/.test(el);
1198
+ }
1199
+ function lte(i, y) {
1200
+ return i <= y;
1201
+ }
1202
+ function gte(i, y) {
1203
+ return i >= y;
1204
+ }
1205
+ function expand_(str, max, isTop) {
1206
+ /** @type {string[]} */
1207
+ const expansions = [];
1208
+ const m = balanced('{', '}', str);
1209
+ if (!m)
1210
+ return [str];
1211
+ // no need to expand pre, since it is guaranteed to be free of brace-sets
1212
+ const pre = m.pre;
1213
+ const post = m.post.length ? expand_(m.post, max, false) : [''];
1214
+ if (/\$$/.test(m.pre)) {
1215
+ for (let k = 0; k < post.length && k < max; k++) {
1216
+ const expansion = pre + '{' + m.body + '}' + post[k];
1217
+ expansions.push(expansion);
1218
+ }
1219
+ }
1220
+ else {
1221
+ const isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
1222
+ const isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
1223
+ const isSequence = isNumericSequence || isAlphaSequence;
1224
+ const isOptions = m.body.indexOf(',') >= 0;
1225
+ if (!isSequence && !isOptions) {
1226
+ // {a},b}
1227
+ if (m.post.match(/,(?!,).*\}/)) {
1228
+ str = m.pre + '{' + m.body + escClose + m.post;
1229
+ return expand_(str, max, true);
1542
1230
  }
1543
- if (!fs.existsSync(fullPath)) {
1544
- res.statusCode = 404;
1545
- res.end('Not Found');
1546
- return;
1231
+ return [str];
1232
+ }
1233
+ let n;
1234
+ if (isSequence) {
1235
+ n = m.body.split(/\.\./);
1236
+ }
1237
+ else {
1238
+ n = parseCommaParts(m.body);
1239
+ if (n.length === 1 && n[0] !== undefined) {
1240
+ // x{{a,b}}y ==> x{a}y x{b}y
1241
+ n = expand_(n[0], max, false).map(embrace);
1242
+ //XXX is this necessary? Can't seem to hit it in tests.
1243
+ /* c8 ignore start */
1244
+ if (n.length === 1) {
1245
+ return post.map(p => m.pre + n[0] + p);
1246
+ }
1247
+ /* c8 ignore stop */
1547
1248
  }
1548
- const stat = fs.statSync(fullPath);
1549
- if (!stat.isFile()) {
1550
- res.statusCode = 404;
1551
- res.end('Not Found');
1552
- return;
1249
+ }
1250
+ // at this point, n is the parts, and we know it's not a comma set
1251
+ // with a single entry.
1252
+ let N;
1253
+ if (isSequence && n[0] !== undefined && n[1] !== undefined) {
1254
+ const x = numeric(n[0]);
1255
+ const y = numeric(n[1]);
1256
+ const width = Math.max(n[0].length, n[1].length);
1257
+ let incr = n.length === 3 && n[2] !== undefined ?
1258
+ Math.max(Math.abs(numeric(n[2])), 1)
1259
+ : 1;
1260
+ let test = lte;
1261
+ const reverse = y < x;
1262
+ if (reverse) {
1263
+ incr *= -1;
1264
+ test = gte;
1553
1265
  }
1554
- const extension = path.extname(filepath).toLowerCase();
1555
- const contentType = STATIC_CONTENT_TYPES[extension] ?? 'application/octet-stream';
1556
- res.statusCode = 200;
1557
- res.setHeader('Content-Type', contentType);
1558
- res.setHeader('Content-Length', String(stat.size));
1559
- if (normalized.method === 'HEAD') {
1560
- res.end();
1561
- return;
1266
+ const pad = n.some(isPadded);
1267
+ N = [];
1268
+ for (let i = x; test(i, y) && N.length < max; i += incr) {
1269
+ let c;
1270
+ if (isAlphaSequence) {
1271
+ c = String.fromCharCode(i);
1272
+ if (c === '\\') {
1273
+ c = '';
1274
+ }
1275
+ }
1276
+ else {
1277
+ c = String(i);
1278
+ if (pad) {
1279
+ const need = width - c.length;
1280
+ if (need > 0) {
1281
+ const z = new Array(need + 1).join('0');
1282
+ if (i < 0) {
1283
+ c = '-' + z + c.slice(1);
1284
+ }
1285
+ else {
1286
+ c = z + c;
1287
+ }
1288
+ }
1289
+ }
1290
+ }
1291
+ N.push(c);
1562
1292
  }
1563
- return new Promise((resolve, reject) => {
1564
- const stream = fs.createReadStream(fullPath);
1565
- stream.on('error', reject);
1566
- stream.on('end', () => resolve(this.StaticHandled));
1567
- stream.pipe(res);
1568
- });
1293
+ }
1294
+ else {
1295
+ N = [];
1296
+ for (let j = 0; j < n.length; j++) {
1297
+ N.push.apply(N, expand_(n[j], max, false));
1298
+ }
1299
+ }
1300
+ for (let j = 0; j < N.length; j++) {
1301
+ for (let k = 0; k < post.length && expansions.length < max; k++) {
1302
+ const expansion = pre + N[j] + post[k];
1303
+ if (!isTop || isSequence || expansion) {
1304
+ expansions.push(expansion);
1305
+ }
1306
+ }
1307
+ }
1308
+ }
1309
+ return expansions;
1310
+ }
1311
+
1312
+ const MAX_PATTERN_LENGTH = 1024 * 64;
1313
+ const assertValidPattern = (pattern) => {
1314
+ if (typeof pattern !== 'string') {
1315
+ throw new TypeError('invalid pattern');
1316
+ }
1317
+ if (pattern.length > MAX_PATTERN_LENGTH) {
1318
+ throw new TypeError('pattern is too long');
1319
+ }
1320
+ };
1321
+
1322
+ // translate the various posix character classes into unicode properties
1323
+ // this works across all unicode locales
1324
+ // { <posix class>: [<translation>, /u flag required, negated]
1325
+ const posixClasses = {
1326
+ '[:alnum:]': ['\\p{L}\\p{Nl}\\p{Nd}', true],
1327
+ '[:alpha:]': ['\\p{L}\\p{Nl}', true],
1328
+ '[:ascii:]': ['\\x' + '00-\\x' + '7f', false],
1329
+ '[:blank:]': ['\\p{Zs}\\t', true],
1330
+ '[:cntrl:]': ['\\p{Cc}', true],
1331
+ '[:digit:]': ['\\p{Nd}', true],
1332
+ '[:graph:]': ['\\p{Z}\\p{C}', true, true],
1333
+ '[:lower:]': ['\\p{Ll}', true],
1334
+ '[:print:]': ['\\p{C}', true],
1335
+ '[:punct:]': ['\\p{P}', true],
1336
+ '[:space:]': ['\\p{Z}\\t\\r\\n\\v\\f', true],
1337
+ '[:upper:]': ['\\p{Lu}', true],
1338
+ '[:word:]': ['\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}', true],
1339
+ '[:xdigit:]': ['A-Fa-f0-9', false],
1340
+ };
1341
+ // only need to escape a few things inside of brace expressions
1342
+ // escapes: [ \ ] -
1343
+ const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&');
1344
+ // escape all regexp magic characters
1345
+ const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
1346
+ // everything has already been escaped, we just have to join
1347
+ const rangesToString = (ranges) => ranges.join('');
1348
+ // takes a glob string at a posix brace expression, and returns
1349
+ // an equivalent regular expression source, and boolean indicating
1350
+ // whether the /u flag needs to be applied, and the number of chars
1351
+ // consumed to parse the character class.
1352
+ // This also removes out of order ranges, and returns ($.) if the
1353
+ // entire class just no good.
1354
+ const parseClass = (glob, position) => {
1355
+ const pos = position;
1356
+ /* c8 ignore start */
1357
+ if (glob.charAt(pos) !== '[') {
1358
+ throw new Error('not in a brace expression');
1359
+ }
1360
+ /* c8 ignore stop */
1361
+ const ranges = [];
1362
+ const negs = [];
1363
+ let i = pos + 1;
1364
+ let sawStart = false;
1365
+ let uflag = false;
1366
+ let escaping = false;
1367
+ let negate = false;
1368
+ let endPos = pos;
1369
+ let rangeStart = '';
1370
+ WHILE: while (i < glob.length) {
1371
+ const c = glob.charAt(i);
1372
+ if ((c === '!' || c === '^') && i === pos + 1) {
1373
+ negate = true;
1374
+ i++;
1375
+ continue;
1376
+ }
1377
+ if (c === ']' && sawStart && !escaping) {
1378
+ endPos = i + 1;
1379
+ break;
1380
+ }
1381
+ sawStart = true;
1382
+ if (c === '\\') {
1383
+ if (!escaping) {
1384
+ escaping = true;
1385
+ i++;
1386
+ continue;
1387
+ }
1388
+ // escaped \ char, fall through and treat like normal char
1389
+ }
1390
+ if (c === '[' && !escaping) {
1391
+ // either a posix class, a collation equivalent, or just a [
1392
+ for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) {
1393
+ if (glob.startsWith(cls, i)) {
1394
+ // invalid, [a-[] is fine, but not [a-[:alpha]]
1395
+ if (rangeStart) {
1396
+ return ['$.', false, glob.length - pos, true];
1397
+ }
1398
+ i += cls.length;
1399
+ if (neg)
1400
+ negs.push(unip);
1401
+ else
1402
+ ranges.push(unip);
1403
+ uflag = uflag || u;
1404
+ continue WHILE;
1405
+ }
1406
+ }
1407
+ }
1408
+ // now it's just a normal character, effectively
1409
+ escaping = false;
1410
+ if (rangeStart) {
1411
+ // throw this range away if it's not valid, but others
1412
+ // can still match.
1413
+ if (c > rangeStart) {
1414
+ ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c));
1415
+ }
1416
+ else if (c === rangeStart) {
1417
+ ranges.push(braceEscape(c));
1418
+ }
1419
+ rangeStart = '';
1420
+ i++;
1421
+ continue;
1422
+ }
1423
+ // now might be the start of a range.
1424
+ // can be either c-d or c-] or c<more...>] or c] at this point
1425
+ if (glob.startsWith('-]', i + 1)) {
1426
+ ranges.push(braceEscape(c + '-'));
1427
+ i += 2;
1428
+ continue;
1429
+ }
1430
+ if (glob.startsWith('-', i + 1)) {
1431
+ rangeStart = c;
1432
+ i += 2;
1433
+ continue;
1434
+ }
1435
+ // not the start of a range, just a single character
1436
+ ranges.push(braceEscape(c));
1437
+ i++;
1438
+ }
1439
+ if (endPos < i) {
1440
+ // didn't see the end of the class, not a valid class,
1441
+ // but might still be valid as a literal match.
1442
+ return ['', false, 0, false];
1443
+ }
1444
+ // if we got no ranges and no negates, then we have a range that
1445
+ // cannot possibly match anything, and that poisons the whole glob
1446
+ if (!ranges.length && !negs.length) {
1447
+ return ['$.', false, glob.length - pos, true];
1448
+ }
1449
+ // if we got one positive range, and it's a single character, then that's
1450
+ // not actually a magic pattern, it's just that one literal character.
1451
+ // we should not treat that as "magic", we should just return the literal
1452
+ // character. [_] is a perfectly valid way to escape glob magic chars.
1453
+ if (negs.length === 0 &&
1454
+ ranges.length === 1 &&
1455
+ /^\\?.$/.test(ranges[0]) &&
1456
+ !negate) {
1457
+ const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0];
1458
+ return [regexpEscape(r), false, endPos - pos, false];
1459
+ }
1460
+ const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']';
1461
+ const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']';
1462
+ const comb = ranges.length && negs.length ? '(' + sranges + '|' + snegs + ')'
1463
+ : ranges.length ? sranges
1464
+ : snegs;
1465
+ return [comb, uflag, endPos - pos, true];
1466
+ };
1467
+
1468
+ /**
1469
+ * Un-escape a string that has been escaped with {@link escape}.
1470
+ *
1471
+ * If the {@link MinimatchOptions.windowsPathsNoEscape} option is used, then
1472
+ * square-bracket escapes are removed, but not backslash escapes.
1473
+ *
1474
+ * For example, it will turn the string `'[*]'` into `*`, but it will not
1475
+ * turn `'\\*'` into `'*'`, because `\` is a path separator in
1476
+ * `windowsPathsNoEscape` mode.
1477
+ *
1478
+ * When `windowsPathsNoEscape` is not set, then both square-bracket escapes and
1479
+ * backslash escapes are removed.
1480
+ *
1481
+ * Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped
1482
+ * or unescaped.
1483
+ *
1484
+ * When `magicalBraces` is not set, escapes of braces (`{` and `}`) will not be
1485
+ * unescaped.
1486
+ */
1487
+ const unescape = (s, { windowsPathsNoEscape = false, magicalBraces = true, } = {}) => {
1488
+ if (magicalBraces) {
1489
+ return windowsPathsNoEscape ?
1490
+ s.replace(/\[([^/\\])\]/g, '$1')
1491
+ : s
1492
+ .replace(/((?!\\).|^)\[([^/\\])\]/g, '$1$2')
1493
+ .replace(/\\([^/])/g, '$1');
1494
+ }
1495
+ return windowsPathsNoEscape ?
1496
+ s.replace(/\[([^/\\{}])\]/g, '$1')
1497
+ : s
1498
+ .replace(/((?!\\).|^)\[([^/\\{}])\]/g, '$1$2')
1499
+ .replace(/\\([^/{}])/g, '$1');
1500
+ };
1501
+
1502
+ // parse a single path portion
1503
+ var _a;
1504
+ const types = new Set(['!', '?', '+', '*', '@']);
1505
+ const isExtglobType = (c) => types.has(c);
1506
+ const isExtglobAST = (c) => isExtglobType(c.type);
1507
+ // Map of which extglob types can adopt the children of a nested extglob
1508
+ //
1509
+ // anything but ! can adopt a matching type:
1510
+ // +(a|+(b|c)|d) => +(a|b|c|d)
1511
+ // *(a|*(b|c)|d) => *(a|b|c|d)
1512
+ // @(a|@(b|c)|d) => @(a|b|c|d)
1513
+ // ?(a|?(b|c)|d) => ?(a|b|c|d)
1514
+ //
1515
+ // * can adopt anything, because 0 or repetition is allowed
1516
+ // *(a|?(b|c)|d) => *(a|b|c|d)
1517
+ // *(a|+(b|c)|d) => *(a|b|c|d)
1518
+ // *(a|@(b|c)|d) => *(a|b|c|d)
1519
+ //
1520
+ // + can adopt @, because 1 or repetition is allowed
1521
+ // +(a|@(b|c)|d) => +(a|b|c|d)
1522
+ //
1523
+ // + and @ CANNOT adopt *, because 0 would be allowed
1524
+ // +(a|*(b|c)|d) => would match "", on *(b|c)
1525
+ // @(a|*(b|c)|d) => would match "", on *(b|c)
1526
+ //
1527
+ // + and @ CANNOT adopt ?, because 0 would be allowed
1528
+ // +(a|?(b|c)|d) => would match "", on ?(b|c)
1529
+ // @(a|?(b|c)|d) => would match "", on ?(b|c)
1530
+ //
1531
+ // ? can adopt @, because 0 or 1 is allowed
1532
+ // ?(a|@(b|c)|d) => ?(a|b|c|d)
1533
+ //
1534
+ // ? and @ CANNOT adopt * or +, because >1 would be allowed
1535
+ // ?(a|*(b|c)|d) => would match bbb on *(b|c)
1536
+ // @(a|*(b|c)|d) => would match bbb on *(b|c)
1537
+ // ?(a|+(b|c)|d) => would match bbb on +(b|c)
1538
+ // @(a|+(b|c)|d) => would match bbb on +(b|c)
1539
+ //
1540
+ // ! CANNOT adopt ! (nothing else can either)
1541
+ // !(a|!(b|c)|d) => !(a|b|c|d) would fail to match on b (not not b|c)
1542
+ //
1543
+ // ! can adopt @
1544
+ // !(a|@(b|c)|d) => !(a|b|c|d)
1545
+ //
1546
+ // ! CANNOT adopt *
1547
+ // !(a|*(b|c)|d) => !(a|b|c|d) would match on bbb, not allowed
1548
+ //
1549
+ // ! CANNOT adopt +
1550
+ // !(a|+(b|c)|d) => !(a|b|c|d) would match on bbb, not allowed
1551
+ //
1552
+ // ! CANNOT adopt ?
1553
+ // x!(a|?(b|c)|d) => x!(a|b|c|d) would fail to match "x"
1554
+ const adoptionMap = new Map([
1555
+ ['!', ['@']],
1556
+ ['?', ['?', '@']],
1557
+ ['@', ['@']],
1558
+ ['*', ['*', '+', '?', '@']],
1559
+ ['+', ['+', '@']],
1560
+ ]);
1561
+ // nested extglobs that can be adopted in, but with the addition of
1562
+ // a blank '' element.
1563
+ const adoptionWithSpaceMap = new Map([
1564
+ ['!', ['?']],
1565
+ ['@', ['?']],
1566
+ ['+', ['?', '*']],
1567
+ ]);
1568
+ // union of the previous two maps
1569
+ const adoptionAnyMap = new Map([
1570
+ ['!', ['?', '@']],
1571
+ ['?', ['?', '@']],
1572
+ ['@', ['?', '@']],
1573
+ ['*', ['*', '+', '?', '@']],
1574
+ ['+', ['+', '@', '?', '*']],
1575
+ ]);
1576
+ // Extglobs that can take over their parent if they are the only child
1577
+ // the key is parent, value maps child to resulting extglob parent type
1578
+ // '@' is omitted because it's a special case. An `@` extglob with a single
1579
+ // member can always be usurped by that subpattern.
1580
+ const usurpMap = new Map([
1581
+ ['!', new Map([['!', '@']])],
1582
+ [
1583
+ '?',
1584
+ new Map([
1585
+ ['*', '*'],
1586
+ ['+', '*'],
1587
+ ]),
1588
+ ],
1589
+ [
1590
+ '@',
1591
+ new Map([
1592
+ ['!', '!'],
1593
+ ['?', '?'],
1594
+ ['@', '@'],
1595
+ ['*', '*'],
1596
+ ['+', '+'],
1597
+ ]),
1598
+ ],
1599
+ [
1600
+ '+',
1601
+ new Map([
1602
+ ['?', '*'],
1603
+ ['*', '*'],
1604
+ ]),
1605
+ ],
1606
+ ]);
1607
+ // Patterns that get prepended to bind to the start of either the
1608
+ // entire string, or just a single path portion, to prevent dots
1609
+ // and/or traversal patterns, when needed.
1610
+ // Exts don't need the ^ or / bit, because the root binds that already.
1611
+ const startNoTraversal = '(?!(?:^|/)\\.\\.?(?:$|/))';
1612
+ const startNoDot = '(?!\\.)';
1613
+ // characters that indicate a start of pattern needs the "no dots" bit,
1614
+ // because a dot *might* be matched. ( is not in the list, because in
1615
+ // the case of a child extglob, it will handle the prevention itself.
1616
+ const addPatternStart = new Set(['[', '.']);
1617
+ // cases where traversal is A-OK, no dot prevention needed
1618
+ const justDots = new Set(['..', '.']);
1619
+ const reSpecials = new Set('().*{}+?[]^$\\!');
1620
+ const regExpEscape$1 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
1621
+ // any single thing other than /
1622
+ const qmark$1 = '[^/]';
1623
+ // * => any number of characters
1624
+ const star$1 = qmark$1 + '*?';
1625
+ // use + when we need to ensure that *something* matches, because the * is
1626
+ // the only thing in the path portion.
1627
+ const starNoEmpty = qmark$1 + '+?';
1628
+ // remove the \ chars that we added if we end up doing a nonmagic compare
1629
+ // const deslash = (s: string) => s.replace(/\\(.)/g, '$1')
1630
+ let ID = 0;
1631
+ class AST {
1632
+ type;
1633
+ #root;
1634
+ #hasMagic;
1635
+ #uflag = false;
1636
+ #parts = [];
1637
+ #parent;
1638
+ #parentIndex;
1639
+ #negs;
1640
+ #filledNegs = false;
1641
+ #options;
1642
+ #toString;
1643
+ // set to true if it's an extglob with no children
1644
+ // (which really means one child of '')
1645
+ #emptyExt = false;
1646
+ id = ++ID;
1647
+ get depth() {
1648
+ return (this.#parent?.depth ?? -1) + 1;
1649
+ }
1650
+ [Symbol.for('nodejs.util.inspect.custom')]() {
1651
+ return {
1652
+ '@@type': 'AST',
1653
+ id: this.id,
1654
+ type: this.type,
1655
+ root: this.#root.id,
1656
+ parent: this.#parent?.id,
1657
+ depth: this.depth,
1658
+ partsLength: this.#parts.length,
1659
+ parts: this.#parts,
1569
1660
  };
1570
1661
  }
1571
- /**
1572
- * 1. Check if the path exists, if not, delete the handler;
1573
- * 2. If the file extension matches `routerExclude`, delete it and return early;
1574
- * 3. If the file extension matches `apiExts`, register it as an API, otherwise register as static resource;
1575
- * @param filepath
1576
- */
1577
- register(filepath) {
1578
- const fullpath = path.join(process.cwd(), this.cx.options.dir, filepath);
1579
- if (!fs.existsSync(fullpath)) {
1580
- this.handlers.delete(filepath);
1581
- this.cx.logger.info(`[${filepath}] deleted`);
1582
- return;
1662
+ constructor(type, parent, options = {}) {
1663
+ this.type = type;
1664
+ // extglobs are inherently magical
1665
+ if (type)
1666
+ this.#hasMagic = true;
1667
+ this.#parent = parent;
1668
+ this.#root = this.#parent ? this.#parent.#root : this;
1669
+ this.#options = this.#root === this ? options : this.#root.#options;
1670
+ this.#negs = this.#root === this ? [] : this.#root.#negs;
1671
+ if (type === '!' && !this.#root.#filledNegs)
1672
+ this.#negs.push(this);
1673
+ this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0;
1674
+ }
1675
+ get hasMagic() {
1676
+ /* c8 ignore start */
1677
+ if (this.#hasMagic !== undefined)
1678
+ return this.#hasMagic;
1679
+ /* c8 ignore stop */
1680
+ for (const p of this.#parts) {
1681
+ if (typeof p === 'string')
1682
+ continue;
1683
+ if (p.type || p.hasMagic)
1684
+ return (this.#hasMagic = true);
1583
1685
  }
1584
- delete require.cache[fullpath];
1585
- const extension = path.extname(filepath).toLowerCase();
1586
- // Check if this file should be excluded from registration
1587
- if (this.cx.options.routerExclude.some((ext) => extension === ext)) {
1686
+ // note: will be undefined until we generate the regexp src and find out
1687
+ return this.#hasMagic;
1688
+ }
1689
+ // reconstructs the pattern
1690
+ toString() {
1691
+ return (this.#toString !== undefined ? this.#toString
1692
+ : !this.type ?
1693
+ (this.#toString = this.#parts.map(p => String(p)).join(''))
1694
+ : (this.#toString =
1695
+ this.type +
1696
+ '(' +
1697
+ this.#parts.map(p => String(p)).join('|') +
1698
+ ')'));
1699
+ }
1700
+ #fillNegs() {
1701
+ /* c8 ignore start */
1702
+ if (this !== this.#root)
1703
+ throw new Error('should only call on root');
1704
+ if (this.#filledNegs)
1705
+ return this;
1706
+ /* c8 ignore stop */
1707
+ // call toString() once to fill this out
1708
+ this.toString();
1709
+ this.#filledNegs = true;
1710
+ let n;
1711
+ while ((n = this.#negs.pop())) {
1712
+ if (n.type !== '!')
1713
+ continue;
1714
+ // walk up the tree, appending everthing that comes AFTER parentIndex
1715
+ let p = n;
1716
+ let pp = p.#parent;
1717
+ while (pp) {
1718
+ for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) {
1719
+ for (const part of n.#parts) {
1720
+ /* c8 ignore start */
1721
+ if (typeof part === 'string') {
1722
+ throw new Error('string part in extglob AST??');
1723
+ }
1724
+ /* c8 ignore stop */
1725
+ part.copyIn(pp.#parts[i]);
1726
+ }
1727
+ }
1728
+ p = pp;
1729
+ pp = p.#parent;
1730
+ }
1731
+ }
1732
+ return this;
1733
+ }
1734
+ push(...parts) {
1735
+ for (const p of parts) {
1736
+ if (p === '')
1737
+ continue;
1738
+ /* c8 ignore start */
1739
+ if (typeof p !== 'string' &&
1740
+ !(p instanceof _a && p.#parent === this)) {
1741
+ throw new Error('invalid part: ' + p);
1742
+ }
1743
+ /* c8 ignore stop */
1744
+ this.#parts.push(p);
1745
+ }
1746
+ }
1747
+ toJSON() {
1748
+ const ret = this.type === null ?
1749
+ this.#parts
1750
+ .slice()
1751
+ .map(p => (typeof p === 'string' ? p : p.toJSON()))
1752
+ : [this.type, ...this.#parts.map(p => p.toJSON())];
1753
+ if (this.isStart() && !this.type)
1754
+ ret.unshift([]);
1755
+ if (this.isEnd() &&
1756
+ (this === this.#root ||
1757
+ (this.#root.#filledNegs && this.#parent?.type === '!'))) {
1758
+ ret.push({});
1759
+ }
1760
+ return ret;
1761
+ }
1762
+ isStart() {
1763
+ if (this.#root === this)
1764
+ return true;
1765
+ // if (this.type) return !!this.#parent?.isStart()
1766
+ if (!this.#parent?.isStart())
1767
+ return false;
1768
+ if (this.#parentIndex === 0)
1769
+ return true;
1770
+ // if everything AHEAD of this is a negation, then it's still the "start"
1771
+ const p = this.#parent;
1772
+ for (let i = 0; i < this.#parentIndex; i++) {
1773
+ const pp = p.#parts[i];
1774
+ if (!(pp instanceof _a && pp.type === '!')) {
1775
+ return false;
1776
+ }
1777
+ }
1778
+ return true;
1779
+ }
1780
+ isEnd() {
1781
+ if (this.#root === this)
1782
+ return true;
1783
+ if (this.#parent?.type === '!')
1784
+ return true;
1785
+ if (!this.#parent?.isEnd())
1786
+ return false;
1787
+ if (!this.type)
1788
+ return this.#parent?.isEnd();
1789
+ // if not root, it'll always have a parent
1790
+ /* c8 ignore start */
1791
+ const pl = this.#parent ? this.#parent.#parts.length : 0;
1792
+ /* c8 ignore stop */
1793
+ return this.#parentIndex === pl - 1;
1794
+ }
1795
+ copyIn(part) {
1796
+ if (typeof part === 'string')
1797
+ this.push(part);
1798
+ else
1799
+ this.push(part.clone(this));
1800
+ }
1801
+ clone(parent) {
1802
+ const c = new _a(this.type, parent);
1803
+ for (const p of this.#parts) {
1804
+ c.copyIn(p);
1805
+ }
1806
+ return c;
1807
+ }
1808
+ static #parseAST(str, ast, pos, opt, extDepth) {
1809
+ const maxDepth = opt.maxExtglobRecursion ?? 2;
1810
+ let escaping = false;
1811
+ let inBrace = false;
1812
+ let braceStart = -1;
1813
+ let braceNeg = false;
1814
+ if (ast.type === null) {
1815
+ // outside of a extglob, append until we find a start
1816
+ let i = pos;
1817
+ let acc = '';
1818
+ while (i < str.length) {
1819
+ const c = str.charAt(i++);
1820
+ // still accumulate escapes at this point, but we do ignore
1821
+ // starts that are escaped
1822
+ if (escaping || c === '\\') {
1823
+ escaping = !escaping;
1824
+ acc += c;
1825
+ continue;
1826
+ }
1827
+ if (inBrace) {
1828
+ if (i === braceStart + 1) {
1829
+ if (c === '^' || c === '!') {
1830
+ braceNeg = true;
1831
+ }
1832
+ }
1833
+ else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
1834
+ inBrace = false;
1835
+ }
1836
+ acc += c;
1837
+ continue;
1838
+ }
1839
+ else if (c === '[') {
1840
+ inBrace = true;
1841
+ braceStart = i;
1842
+ braceNeg = false;
1843
+ acc += c;
1844
+ continue;
1845
+ }
1846
+ // we don't have to check for adoption here, because that's
1847
+ // done at the other recursion point.
1848
+ const doRecurse = !opt.noext &&
1849
+ isExtglobType(c) &&
1850
+ str.charAt(i) === '(' &&
1851
+ extDepth <= maxDepth;
1852
+ if (doRecurse) {
1853
+ ast.push(acc);
1854
+ acc = '';
1855
+ const ext = new _a(c, ast);
1856
+ i = _a.#parseAST(str, ext, i, opt, extDepth + 1);
1857
+ ast.push(ext);
1858
+ continue;
1859
+ }
1860
+ acc += c;
1861
+ }
1862
+ ast.push(acc);
1863
+ return i;
1864
+ }
1865
+ // some kind of extglob, pos is at the (
1866
+ // find the next | or )
1867
+ let i = pos + 1;
1868
+ let part = new _a(null, ast);
1869
+ const parts = [];
1870
+ let acc = '';
1871
+ while (i < str.length) {
1872
+ const c = str.charAt(i++);
1873
+ // still accumulate escapes at this point, but we do ignore
1874
+ // starts that are escaped
1875
+ if (escaping || c === '\\') {
1876
+ escaping = !escaping;
1877
+ acc += c;
1878
+ continue;
1879
+ }
1880
+ if (inBrace) {
1881
+ if (i === braceStart + 1) {
1882
+ if (c === '^' || c === '!') {
1883
+ braceNeg = true;
1884
+ }
1885
+ }
1886
+ else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
1887
+ inBrace = false;
1888
+ }
1889
+ acc += c;
1890
+ continue;
1891
+ }
1892
+ else if (c === '[') {
1893
+ inBrace = true;
1894
+ braceStart = i;
1895
+ braceNeg = false;
1896
+ acc += c;
1897
+ continue;
1898
+ }
1899
+ const doRecurse = !opt.noext &&
1900
+ isExtglobType(c) &&
1901
+ str.charAt(i) === '(' &&
1902
+ /* c8 ignore start - the maxDepth is sufficient here */
1903
+ (extDepth <= maxDepth || (ast && ast.#canAdoptType(c)));
1904
+ /* c8 ignore stop */
1905
+ if (doRecurse) {
1906
+ const depthAdd = ast && ast.#canAdoptType(c) ? 0 : 1;
1907
+ part.push(acc);
1908
+ acc = '';
1909
+ const ext = new _a(c, part);
1910
+ part.push(ext);
1911
+ i = _a.#parseAST(str, ext, i, opt, extDepth + depthAdd);
1912
+ continue;
1913
+ }
1914
+ if (c === '|') {
1915
+ part.push(acc);
1916
+ acc = '';
1917
+ parts.push(part);
1918
+ part = new _a(null, ast);
1919
+ continue;
1920
+ }
1921
+ if (c === ')') {
1922
+ if (acc === '' && ast.#parts.length === 0) {
1923
+ ast.#emptyExt = true;
1924
+ }
1925
+ part.push(acc);
1926
+ acc = '';
1927
+ ast.push(...parts, part);
1928
+ return i;
1929
+ }
1930
+ acc += c;
1931
+ }
1932
+ // unfinished extglob
1933
+ // if we got here, it was a malformed extglob! not an extglob, but
1934
+ // maybe something else in there.
1935
+ ast.type = null;
1936
+ ast.#hasMagic = undefined;
1937
+ ast.#parts = [str.substring(pos - 1)];
1938
+ return i;
1939
+ }
1940
+ #canAdoptWithSpace(child) {
1941
+ return this.#canAdopt(child, adoptionWithSpaceMap);
1942
+ }
1943
+ #canAdopt(child, map = adoptionMap) {
1944
+ if (!child ||
1945
+ typeof child !== 'object' ||
1946
+ child.type !== null ||
1947
+ child.#parts.length !== 1 ||
1948
+ this.type === null) {
1949
+ return false;
1950
+ }
1951
+ const gc = child.#parts[0];
1952
+ if (!gc || typeof gc !== 'object' || gc.type === null) {
1953
+ return false;
1954
+ }
1955
+ return this.#canAdoptType(gc.type, map);
1956
+ }
1957
+ #canAdoptType(c, map = adoptionAnyMap) {
1958
+ return !!map.get(this.type)?.includes(c);
1959
+ }
1960
+ #adoptWithSpace(child, index) {
1961
+ const gc = child.#parts[0];
1962
+ const blank = new _a(null, gc, this.options);
1963
+ blank.#parts.push('');
1964
+ gc.push(blank);
1965
+ this.#adopt(child, index);
1966
+ }
1967
+ #adopt(child, index) {
1968
+ const gc = child.#parts[0];
1969
+ this.#parts.splice(index, 1, ...gc.#parts);
1970
+ for (const p of gc.#parts) {
1971
+ if (typeof p === 'object')
1972
+ p.#parent = this;
1973
+ }
1974
+ this.#toString = undefined;
1975
+ }
1976
+ #canUsurpType(c) {
1977
+ const m = usurpMap.get(this.type);
1978
+ return !!m?.has(c);
1979
+ }
1980
+ #canUsurp(child) {
1981
+ if (!child ||
1982
+ typeof child !== 'object' ||
1983
+ child.type !== null ||
1984
+ child.#parts.length !== 1 ||
1985
+ this.type === null ||
1986
+ this.#parts.length !== 1) {
1987
+ return false;
1988
+ }
1989
+ const gc = child.#parts[0];
1990
+ if (!gc || typeof gc !== 'object' || gc.type === null) {
1991
+ return false;
1992
+ }
1993
+ return this.#canUsurpType(gc.type);
1994
+ }
1995
+ #usurp(child) {
1996
+ const m = usurpMap.get(this.type);
1997
+ const gc = child.#parts[0];
1998
+ const nt = m?.get(gc.type);
1999
+ /* c8 ignore start - impossible */
2000
+ if (!nt)
2001
+ return false;
2002
+ /* c8 ignore stop */
2003
+ this.#parts = gc.#parts;
2004
+ for (const p of this.#parts) {
2005
+ if (typeof p === 'object') {
2006
+ p.#parent = this;
2007
+ }
2008
+ }
2009
+ this.type = nt;
2010
+ this.#toString = undefined;
2011
+ this.#emptyExt = false;
2012
+ }
2013
+ static fromGlob(pattern, options = {}) {
2014
+ const ast = new _a(null, undefined, options);
2015
+ _a.#parseAST(pattern, ast, 0, options, 0);
2016
+ return ast;
2017
+ }
2018
+ // returns the regular expression if there's magic, or the unescaped
2019
+ // string if not.
2020
+ toMMPattern() {
2021
+ // should only be called on root
2022
+ /* c8 ignore start */
2023
+ if (this !== this.#root)
2024
+ return this.#root.toMMPattern();
2025
+ /* c8 ignore stop */
2026
+ const glob = this.toString();
2027
+ const [re, body, hasMagic, uflag] = this.toRegExpSource();
2028
+ // if we're in nocase mode, and not nocaseMagicOnly, then we do
2029
+ // still need a regular expression if we have to case-insensitively
2030
+ // match capital/lowercase characters.
2031
+ const anyMagic = hasMagic ||
2032
+ this.#hasMagic ||
2033
+ (this.#options.nocase &&
2034
+ !this.#options.nocaseMagicOnly &&
2035
+ glob.toUpperCase() !== glob.toLowerCase());
2036
+ if (!anyMagic) {
2037
+ return body;
2038
+ }
2039
+ const flags = (this.#options.nocase ? 'i' : '') + (uflag ? 'u' : '');
2040
+ return Object.assign(new RegExp(`^${re}$`, flags), {
2041
+ _src: re,
2042
+ _glob: glob,
2043
+ });
2044
+ }
2045
+ get options() {
2046
+ return this.#options;
2047
+ }
2048
+ // returns the string match, the regexp source, whether there's magic
2049
+ // in the regexp (so a regular expression is required) and whether or
2050
+ // not the uflag is needed for the regular expression (for posix classes)
2051
+ // TODO: instead of injecting the start/end at this point, just return
2052
+ // the BODY of the regexp, along with the start/end portions suitable
2053
+ // for binding the start/end in either a joined full-path makeRe context
2054
+ // (where we bind to (^|/), or a standalone matchPart context (where
2055
+ // we bind to ^, and not /). Otherwise slashes get duped!
2056
+ //
2057
+ // In part-matching mode, the start is:
2058
+ // - if not isStart: nothing
2059
+ // - if traversal possible, but not allowed: ^(?!\.\.?$)
2060
+ // - if dots allowed or not possible: ^
2061
+ // - if dots possible and not allowed: ^(?!\.)
2062
+ // end is:
2063
+ // - if not isEnd(): nothing
2064
+ // - else: $
2065
+ //
2066
+ // In full-path matching mode, we put the slash at the START of the
2067
+ // pattern, so start is:
2068
+ // - if first pattern: same as part-matching mode
2069
+ // - if not isStart(): nothing
2070
+ // - if traversal possible, but not allowed: /(?!\.\.?(?:$|/))
2071
+ // - if dots allowed or not possible: /
2072
+ // - if dots possible and not allowed: /(?!\.)
2073
+ // end is:
2074
+ // - if last pattern, same as part-matching mode
2075
+ // - else nothing
2076
+ //
2077
+ // Always put the (?:$|/) on negated tails, though, because that has to be
2078
+ // there to bind the end of the negated pattern portion, and it's easier to
2079
+ // just stick it in now rather than try to inject it later in the middle of
2080
+ // the pattern.
2081
+ //
2082
+ // We can just always return the same end, and leave it up to the caller
2083
+ // to know whether it's going to be used joined or in parts.
2084
+ // And, if the start is adjusted slightly, can do the same there:
2085
+ // - if not isStart: nothing
2086
+ // - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$)
2087
+ // - if dots allowed or not possible: (?:/|^)
2088
+ // - if dots possible and not allowed: (?:/|^)(?!\.)
2089
+ //
2090
+ // But it's better to have a simpler binding without a conditional, for
2091
+ // performance, so probably better to return both start options.
2092
+ //
2093
+ // Then the caller just ignores the end if it's not the first pattern,
2094
+ // and the start always gets applied.
2095
+ //
2096
+ // But that's always going to be $ if it's the ending pattern, or nothing,
2097
+ // so the caller can just attach $ at the end of the pattern when building.
2098
+ //
2099
+ // So the todo is:
2100
+ // - better detect what kind of start is needed
2101
+ // - return both flavors of starting pattern
2102
+ // - attach $ at the end of the pattern when creating the actual RegExp
2103
+ //
2104
+ // Ah, but wait, no, that all only applies to the root when the first pattern
2105
+ // is not an extglob. If the first pattern IS an extglob, then we need all
2106
+ // that dot prevention biz to live in the extglob portions, because eg
2107
+ // +(*|.x*) can match .xy but not .yx.
2108
+ //
2109
+ // So, return the two flavors if it's #root and the first child is not an
2110
+ // AST, otherwise leave it to the child AST to handle it, and there,
2111
+ // use the (?:^|/) style of start binding.
2112
+ //
2113
+ // Even simplified further:
2114
+ // - Since the start for a join is eg /(?!\.) and the start for a part
2115
+ // is ^(?!\.), we can just prepend (?!\.) to the pattern (either root
2116
+ // or start or whatever) and prepend ^ or / at the Regexp construction.
2117
+ toRegExpSource(allowDot) {
2118
+ const dot = allowDot ?? !!this.#options.dot;
2119
+ if (this.#root === this) {
2120
+ this.#flatten();
2121
+ this.#fillNegs();
2122
+ }
2123
+ if (!isExtglobAST(this)) {
2124
+ const noEmpty = this.isStart() &&
2125
+ this.isEnd() &&
2126
+ !this.#parts.some(s => typeof s !== 'string');
2127
+ const src = this.#parts
2128
+ .map(p => {
2129
+ const [re, _, hasMagic, uflag] = typeof p === 'string' ?
2130
+ _a.#parseGlob(p, this.#hasMagic, noEmpty)
2131
+ : p.toRegExpSource(allowDot);
2132
+ this.#hasMagic = this.#hasMagic || hasMagic;
2133
+ this.#uflag = this.#uflag || uflag;
2134
+ return re;
2135
+ })
2136
+ .join('');
2137
+ let start = '';
2138
+ if (this.isStart()) {
2139
+ if (typeof this.#parts[0] === 'string') {
2140
+ // this is the string that will match the start of the pattern,
2141
+ // so we need to protect against dots and such.
2142
+ // '.' and '..' cannot match unless the pattern is that exactly,
2143
+ // even if it starts with . or dot:true is set.
2144
+ const dotTravAllowed = this.#parts.length === 1 && justDots.has(this.#parts[0]);
2145
+ if (!dotTravAllowed) {
2146
+ const aps = addPatternStart;
2147
+ // check if we have a possibility of matching . or ..,
2148
+ // and prevent that.
2149
+ const needNoTrav =
2150
+ // dots are allowed, and the pattern starts with [ or .
2151
+ (dot && aps.has(src.charAt(0))) ||
2152
+ // the pattern starts with \., and then [ or .
2153
+ (src.startsWith('\\.') && aps.has(src.charAt(2))) ||
2154
+ // the pattern starts with \.\., and then [ or .
2155
+ (src.startsWith('\\.\\.') && aps.has(src.charAt(4)));
2156
+ // no need to prevent dots if it can't match a dot, or if a
2157
+ // sub-pattern will be preventing it anyway.
2158
+ const needNoDot = !dot && !allowDot && aps.has(src.charAt(0));
2159
+ start =
2160
+ needNoTrav ? startNoTraversal
2161
+ : needNoDot ? startNoDot
2162
+ : '';
2163
+ }
2164
+ }
2165
+ }
2166
+ // append the "end of path portion" pattern to negation tails
2167
+ let end = '';
2168
+ if (this.isEnd() &&
2169
+ this.#root.#filledNegs &&
2170
+ this.#parent?.type === '!') {
2171
+ end = '(?:$|\\/)';
2172
+ }
2173
+ const final = start + src + end;
2174
+ return [
2175
+ final,
2176
+ unescape(src),
2177
+ (this.#hasMagic = !!this.#hasMagic),
2178
+ this.#uflag,
2179
+ ];
2180
+ }
2181
+ // We need to calculate the body *twice* if it's a repeat pattern
2182
+ // at the start, once in nodot mode, then again in dot mode, so a
2183
+ // pattern like *(?) can match 'x.y'
2184
+ const repeated = this.type === '*' || this.type === '+';
2185
+ // some kind of extglob
2186
+ const start = this.type === '!' ? '(?:(?!(?:' : '(?:';
2187
+ let body = this.#partsToRegExp(dot);
2188
+ if (this.isStart() && this.isEnd() && !body && this.type !== '!') {
2189
+ // invalid extglob, has to at least be *something* present, if it's
2190
+ // the entire path portion.
2191
+ const s = this.toString();
2192
+ const me = this;
2193
+ me.#parts = [s];
2194
+ me.type = null;
2195
+ me.#hasMagic = undefined;
2196
+ return [s, unescape(this.toString()), false, false];
2197
+ }
2198
+ let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot ?
2199
+ ''
2200
+ : this.#partsToRegExp(true);
2201
+ if (bodyDotAllowed === body) {
2202
+ bodyDotAllowed = '';
2203
+ }
2204
+ if (bodyDotAllowed) {
2205
+ body = `(?:${body})(?:${bodyDotAllowed})*?`;
2206
+ }
2207
+ // an empty !() is exactly equivalent to a starNoEmpty
2208
+ let final = '';
2209
+ if (this.type === '!' && this.#emptyExt) {
2210
+ final = (this.isStart() && !dot ? startNoDot : '') + starNoEmpty;
2211
+ }
2212
+ else {
2213
+ const close = this.type === '!' ?
2214
+ // !() must match something,but !(x) can match ''
2215
+ '))' +
2216
+ (this.isStart() && !dot && !allowDot ? startNoDot : '') +
2217
+ star$1 +
2218
+ ')'
2219
+ : this.type === '@' ? ')'
2220
+ : this.type === '?' ? ')?'
2221
+ : this.type === '+' && bodyDotAllowed ? ')'
2222
+ : this.type === '*' && bodyDotAllowed ? `)?`
2223
+ : `)${this.type}`;
2224
+ final = start + body + close;
2225
+ }
2226
+ return [
2227
+ final,
2228
+ unescape(body),
2229
+ (this.#hasMagic = !!this.#hasMagic),
2230
+ this.#uflag,
2231
+ ];
2232
+ }
2233
+ #flatten() {
2234
+ if (!isExtglobAST(this)) {
2235
+ for (const p of this.#parts) {
2236
+ if (typeof p === 'object') {
2237
+ p.#flatten();
2238
+ }
2239
+ }
2240
+ }
2241
+ else {
2242
+ // do up to 10 passes to flatten as much as possible
2243
+ let iterations = 0;
2244
+ let done = false;
2245
+ do {
2246
+ done = true;
2247
+ for (let i = 0; i < this.#parts.length; i++) {
2248
+ const c = this.#parts[i];
2249
+ if (typeof c === 'object') {
2250
+ c.#flatten();
2251
+ if (this.#canAdopt(c)) {
2252
+ done = false;
2253
+ this.#adopt(c, i);
2254
+ }
2255
+ else if (this.#canAdoptWithSpace(c)) {
2256
+ done = false;
2257
+ this.#adoptWithSpace(c, i);
2258
+ }
2259
+ else if (this.#canUsurp(c)) {
2260
+ done = false;
2261
+ this.#usurp(c);
2262
+ }
2263
+ }
2264
+ }
2265
+ } while (!done && ++iterations < 10);
2266
+ }
2267
+ this.#toString = undefined;
2268
+ }
2269
+ #partsToRegExp(dot) {
2270
+ return this.#parts
2271
+ .map(p => {
2272
+ // extglob ASTs should only contain parent ASTs
2273
+ /* c8 ignore start */
2274
+ if (typeof p === 'string') {
2275
+ throw new Error('string type in extglob ast??');
2276
+ }
2277
+ /* c8 ignore stop */
2278
+ // can ignore hasMagic, because extglobs are already always magic
2279
+ const [re, _, _hasMagic, uflag] = p.toRegExpSource(dot);
2280
+ this.#uflag = this.#uflag || uflag;
2281
+ return re;
2282
+ })
2283
+ .filter(p => !(this.isStart() && this.isEnd()) || !!p)
2284
+ .join('|');
2285
+ }
2286
+ static #parseGlob(glob, hasMagic, noEmpty = false) {
2287
+ let escaping = false;
2288
+ let re = '';
2289
+ let uflag = false;
2290
+ // multiple stars that aren't globstars coalesce into one *
2291
+ let inStar = false;
2292
+ for (let i = 0; i < glob.length; i++) {
2293
+ const c = glob.charAt(i);
2294
+ if (escaping) {
2295
+ escaping = false;
2296
+ re += (reSpecials.has(c) ? '\\' : '') + c;
2297
+ continue;
2298
+ }
2299
+ if (c === '*') {
2300
+ if (inStar)
2301
+ continue;
2302
+ inStar = true;
2303
+ re += noEmpty && /^[*]+$/.test(glob) ? starNoEmpty : star$1;
2304
+ hasMagic = true;
2305
+ continue;
2306
+ }
2307
+ else {
2308
+ inStar = false;
2309
+ }
2310
+ if (c === '\\') {
2311
+ if (i === glob.length - 1) {
2312
+ re += '\\\\';
2313
+ }
2314
+ else {
2315
+ escaping = true;
2316
+ }
2317
+ continue;
2318
+ }
2319
+ if (c === '[') {
2320
+ const [src, needUflag, consumed, magic] = parseClass(glob, i);
2321
+ if (consumed) {
2322
+ re += src;
2323
+ uflag = uflag || needUflag;
2324
+ i += consumed - 1;
2325
+ hasMagic = hasMagic || magic;
2326
+ continue;
2327
+ }
2328
+ }
2329
+ if (c === '?') {
2330
+ re += qmark$1;
2331
+ hasMagic = true;
2332
+ continue;
2333
+ }
2334
+ re += regExpEscape$1(c);
2335
+ }
2336
+ return [re, unescape(glob), !!hasMagic, uflag];
2337
+ }
2338
+ }
2339
+ _a = AST;
2340
+
2341
+ /**
2342
+ * Escape all magic characters in a glob pattern.
2343
+ *
2344
+ * If the {@link MinimatchOptions.windowsPathsNoEscape}
2345
+ * option is used, then characters are escaped by wrapping in `[]`, because
2346
+ * a magic character wrapped in a character class can only be satisfied by
2347
+ * that exact character. In this mode, `\` is _not_ escaped, because it is
2348
+ * not interpreted as a magic character, but instead as a path separator.
2349
+ *
2350
+ * If the {@link MinimatchOptions.magicalBraces} option is used,
2351
+ * then braces (`{` and `}`) will be escaped.
2352
+ */
2353
+ const escape = (s, { windowsPathsNoEscape = false, magicalBraces = false, } = {}) => {
2354
+ // don't need to escape +@! because we escape the parens
2355
+ // that make those magic, and escaping ! as [!] isn't valid,
2356
+ // because [!]] is a valid glob class meaning not ']'.
2357
+ if (magicalBraces) {
2358
+ return windowsPathsNoEscape ?
2359
+ s.replace(/[?*()[\]{}]/g, '[$&]')
2360
+ : s.replace(/[?*()[\]\\{}]/g, '\\$&');
2361
+ }
2362
+ return windowsPathsNoEscape ?
2363
+ s.replace(/[?*()[\]]/g, '[$&]')
2364
+ : s.replace(/[?*()[\]\\]/g, '\\$&');
2365
+ };
2366
+
2367
+ const minimatch = (p, pattern, options = {}) => {
2368
+ assertValidPattern(pattern);
2369
+ // shortcut: comments match nothing.
2370
+ if (!options.nocomment && pattern.charAt(0) === '#') {
2371
+ return false;
2372
+ }
2373
+ return new Minimatch(pattern, options).match(p);
2374
+ };
2375
+ // Optimized checking for the most common glob patterns.
2376
+ const starDotExtRE = /^\*+([^+@!?*[(]*)$/;
2377
+ const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext);
2378
+ const starDotExtTestDot = (ext) => (f) => f.endsWith(ext);
2379
+ const starDotExtTestNocase = (ext) => {
2380
+ ext = ext.toLowerCase();
2381
+ return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext);
2382
+ };
2383
+ const starDotExtTestNocaseDot = (ext) => {
2384
+ ext = ext.toLowerCase();
2385
+ return (f) => f.toLowerCase().endsWith(ext);
2386
+ };
2387
+ const starDotStarRE = /^\*+\.\*+$/;
2388
+ const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.');
2389
+ const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.');
2390
+ const dotStarRE = /^\.\*+$/;
2391
+ const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.');
2392
+ const starRE = /^\*+$/;
2393
+ const starTest = (f) => f.length !== 0 && !f.startsWith('.');
2394
+ const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..';
2395
+ const qmarksRE = /^\?+([^+@!?*[(]*)?$/;
2396
+ const qmarksTestNocase = ([$0, ext = '']) => {
2397
+ const noext = qmarksTestNoExt([$0]);
2398
+ if (!ext)
2399
+ return noext;
2400
+ ext = ext.toLowerCase();
2401
+ return (f) => noext(f) && f.toLowerCase().endsWith(ext);
2402
+ };
2403
+ const qmarksTestNocaseDot = ([$0, ext = '']) => {
2404
+ const noext = qmarksTestNoExtDot([$0]);
2405
+ if (!ext)
2406
+ return noext;
2407
+ ext = ext.toLowerCase();
2408
+ return (f) => noext(f) && f.toLowerCase().endsWith(ext);
2409
+ };
2410
+ const qmarksTestDot = ([$0, ext = '']) => {
2411
+ const noext = qmarksTestNoExtDot([$0]);
2412
+ return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
2413
+ };
2414
+ const qmarksTest = ([$0, ext = '']) => {
2415
+ const noext = qmarksTestNoExt([$0]);
2416
+ return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
2417
+ };
2418
+ const qmarksTestNoExt = ([$0]) => {
2419
+ const len = $0.length;
2420
+ return (f) => f.length === len && !f.startsWith('.');
2421
+ };
2422
+ const qmarksTestNoExtDot = ([$0]) => {
2423
+ const len = $0.length;
2424
+ return (f) => f.length === len && f !== '.' && f !== '..';
2425
+ };
2426
+ /* c8 ignore start */
2427
+ const defaultPlatform = (typeof process === 'object' && process ?
2428
+ (typeof process.env === 'object' &&
2429
+ process.env &&
2430
+ process.env.__MINIMATCH_TESTING_PLATFORM__) ||
2431
+ process.platform
2432
+ : 'posix');
2433
+ const path = {
2434
+ win32: { sep: '\\' },
2435
+ posix: { sep: '/' },
2436
+ };
2437
+ /* c8 ignore stop */
2438
+ const sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep;
2439
+ minimatch.sep = sep;
2440
+ const GLOBSTAR = Symbol('globstar **');
2441
+ minimatch.GLOBSTAR = GLOBSTAR;
2442
+ // any single thing other than /
2443
+ // don't need to escape / when using new RegExp()
2444
+ const qmark = '[^/]';
2445
+ // * => any number of characters
2446
+ const star = qmark + '*?';
2447
+ // ** when dots are allowed. Anything goes, except .. and .
2448
+ // not (^ or / followed by one or two dots followed by $ or /),
2449
+ // followed by anything, any number of times.
2450
+ const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
2451
+ // not a ^ or / followed by a dot,
2452
+ // followed by anything, any number of times.
2453
+ const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
2454
+ const filter = (pattern, options = {}) => (p) => minimatch(p, pattern, options);
2455
+ minimatch.filter = filter;
2456
+ const ext = (a, b = {}) => Object.assign({}, a, b);
2457
+ const defaults = (def) => {
2458
+ if (!def || typeof def !== 'object' || !Object.keys(def).length) {
2459
+ return minimatch;
2460
+ }
2461
+ const orig = minimatch;
2462
+ const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
2463
+ return Object.assign(m, {
2464
+ Minimatch: class Minimatch extends orig.Minimatch {
2465
+ constructor(pattern, options = {}) {
2466
+ super(pattern, ext(def, options));
2467
+ }
2468
+ static defaults(options) {
2469
+ return orig.defaults(ext(def, options)).Minimatch;
2470
+ }
2471
+ },
2472
+ AST: class AST extends orig.AST {
2473
+ /* c8 ignore start */
2474
+ constructor(type, parent, options = {}) {
2475
+ super(type, parent, ext(def, options));
2476
+ }
2477
+ /* c8 ignore stop */
2478
+ static fromGlob(pattern, options = {}) {
2479
+ return orig.AST.fromGlob(pattern, ext(def, options));
2480
+ }
2481
+ },
2482
+ unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
2483
+ escape: (s, options = {}) => orig.escape(s, ext(def, options)),
2484
+ filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
2485
+ defaults: (options) => orig.defaults(ext(def, options)),
2486
+ makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
2487
+ braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
2488
+ match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
2489
+ sep: orig.sep,
2490
+ GLOBSTAR: GLOBSTAR,
2491
+ });
2492
+ };
2493
+ minimatch.defaults = defaults;
2494
+ // Brace expansion:
2495
+ // a{b,c}d -> abd acd
2496
+ // a{b,}c -> abc ac
2497
+ // a{0..3}d -> a0d a1d a2d a3d
2498
+ // a{b,c{d,e}f}g -> abg acdfg acefg
2499
+ // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
2500
+ //
2501
+ // Invalid sets are not expanded.
2502
+ // a{2..}b -> a{2..}b
2503
+ // a{b}c -> a{b}c
2504
+ const braceExpand = (pattern, options = {}) => {
2505
+ assertValidPattern(pattern);
2506
+ // Thanks to Yeting Li <https://github.com/yetingli> for
2507
+ // improving this regexp to avoid a ReDOS vulnerability.
2508
+ if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
2509
+ // shortcut. no need to expand.
2510
+ return [pattern];
2511
+ }
2512
+ return expand(pattern, { max: options.braceExpandMax });
2513
+ };
2514
+ minimatch.braceExpand = braceExpand;
2515
+ // parse a component of the expanded set.
2516
+ // At this point, no pattern may contain "/" in it
2517
+ // so we're going to return a 2d array, where each entry is the full
2518
+ // pattern, split on '/', and then turned into a regular expression.
2519
+ // A regexp is made at the end which joins each array with an
2520
+ // escaped /, and another full one which joins each regexp with |.
2521
+ //
2522
+ // Following the lead of Bash 4.1, note that "**" only has special meaning
2523
+ // when it is the *only* thing in a path portion. Otherwise, any series
2524
+ // of * is equivalent to a single *. Globstar behavior is enabled by
2525
+ // default, and can be disabled by setting options.noglobstar.
2526
+ const makeRe = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
2527
+ minimatch.makeRe = makeRe;
2528
+ const match = (list, pattern, options = {}) => {
2529
+ const mm = new Minimatch(pattern, options);
2530
+ list = list.filter(f => mm.match(f));
2531
+ if (mm.options.nonull && !list.length) {
2532
+ list.push(pattern);
2533
+ }
2534
+ return list;
2535
+ };
2536
+ minimatch.match = match;
2537
+ // replace stuff like \* with *
2538
+ const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
2539
+ const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
2540
+ class Minimatch {
2541
+ options;
2542
+ set;
2543
+ pattern;
2544
+ windowsPathsNoEscape;
2545
+ nonegate;
2546
+ negate;
2547
+ comment;
2548
+ empty;
2549
+ preserveMultipleSlashes;
2550
+ partial;
2551
+ globSet;
2552
+ globParts;
2553
+ nocase;
2554
+ isWindows;
2555
+ platform;
2556
+ windowsNoMagicRoot;
2557
+ maxGlobstarRecursion;
2558
+ regexp;
2559
+ constructor(pattern, options = {}) {
2560
+ assertValidPattern(pattern);
2561
+ options = options || {};
2562
+ this.options = options;
2563
+ this.maxGlobstarRecursion = options.maxGlobstarRecursion ?? 200;
2564
+ this.pattern = pattern;
2565
+ this.platform = options.platform || defaultPlatform;
2566
+ this.isWindows = this.platform === 'win32';
2567
+ // avoid the annoying deprecation flag lol
2568
+ const awe = ('allowWindow' + 'sEscape');
2569
+ this.windowsPathsNoEscape =
2570
+ !!options.windowsPathsNoEscape || options[awe] === false;
2571
+ if (this.windowsPathsNoEscape) {
2572
+ this.pattern = this.pattern.replace(/\\/g, '/');
2573
+ }
2574
+ this.preserveMultipleSlashes = !!options.preserveMultipleSlashes;
2575
+ this.regexp = null;
2576
+ this.negate = false;
2577
+ this.nonegate = !!options.nonegate;
2578
+ this.comment = false;
2579
+ this.empty = false;
2580
+ this.partial = !!options.partial;
2581
+ this.nocase = !!this.options.nocase;
2582
+ this.windowsNoMagicRoot =
2583
+ options.windowsNoMagicRoot !== undefined ?
2584
+ options.windowsNoMagicRoot
2585
+ : !!(this.isWindows && this.nocase);
2586
+ this.globSet = [];
2587
+ this.globParts = [];
2588
+ this.set = [];
2589
+ // make the set of regexps etc.
2590
+ this.make();
2591
+ }
2592
+ hasMagic() {
2593
+ if (this.options.magicalBraces && this.set.length > 1) {
2594
+ return true;
2595
+ }
2596
+ for (const pattern of this.set) {
2597
+ for (const part of pattern) {
2598
+ if (typeof part !== 'string')
2599
+ return true;
2600
+ }
2601
+ }
2602
+ return false;
2603
+ }
2604
+ debug(..._) { }
2605
+ make() {
2606
+ const pattern = this.pattern;
2607
+ const options = this.options;
2608
+ // empty patterns and comments match nothing.
2609
+ if (!options.nocomment && pattern.charAt(0) === '#') {
2610
+ this.comment = true;
2611
+ return;
2612
+ }
2613
+ if (!pattern) {
2614
+ this.empty = true;
2615
+ return;
2616
+ }
2617
+ // step 1: figure out negation, etc.
2618
+ this.parseNegate();
2619
+ // step 2: expand braces
2620
+ this.globSet = [...new Set(this.braceExpand())];
2621
+ if (options.debug) {
2622
+ //oxlint-disable-next-line no-console
2623
+ this.debug = (...args) => console.error(...args);
2624
+ }
2625
+ this.debug(this.pattern, this.globSet);
2626
+ // step 3: now we have a set, so turn each one into a series of
2627
+ // path-portion matching patterns.
2628
+ // These will be regexps, except in the case of "**", which is
2629
+ // set to the GLOBSTAR object for globstar behavior,
2630
+ // and will not contain any / characters
2631
+ //
2632
+ // First, we preprocess to make the glob pattern sets a bit simpler
2633
+ // and deduped. There are some perf-killing patterns that can cause
2634
+ // problems with a glob walk, but we can simplify them down a bit.
2635
+ const rawGlobParts = this.globSet.map(s => this.slashSplit(s));
2636
+ this.globParts = this.preprocess(rawGlobParts);
2637
+ this.debug(this.pattern, this.globParts);
2638
+ // glob --> regexps
2639
+ let set = this.globParts.map((s, _, __) => {
2640
+ if (this.isWindows && this.windowsNoMagicRoot) {
2641
+ // check if it's a drive or unc path.
2642
+ const isUNC = s[0] === '' &&
2643
+ s[1] === '' &&
2644
+ (s[2] === '?' || !globMagic.test(s[2])) &&
2645
+ !globMagic.test(s[3]);
2646
+ const isDrive = /^[a-z]:/i.test(s[0]);
2647
+ if (isUNC) {
2648
+ return [
2649
+ ...s.slice(0, 4),
2650
+ ...s.slice(4).map(ss => this.parse(ss)),
2651
+ ];
2652
+ }
2653
+ else if (isDrive) {
2654
+ return [s[0], ...s.slice(1).map(ss => this.parse(ss))];
2655
+ }
2656
+ }
2657
+ return s.map(ss => this.parse(ss));
2658
+ });
2659
+ this.debug(this.pattern, set);
2660
+ // filter out everything that didn't compile properly.
2661
+ this.set = set.filter(s => s.indexOf(false) === -1);
2662
+ // do not treat the ? in UNC paths as magic
2663
+ if (this.isWindows) {
2664
+ for (let i = 0; i < this.set.length; i++) {
2665
+ const p = this.set[i];
2666
+ if (p[0] === '' &&
2667
+ p[1] === '' &&
2668
+ this.globParts[i][2] === '?' &&
2669
+ typeof p[3] === 'string' &&
2670
+ /^[a-z]:$/i.test(p[3])) {
2671
+ p[2] = '?';
2672
+ }
2673
+ }
2674
+ }
2675
+ this.debug(this.pattern, this.set);
2676
+ }
2677
+ // various transforms to equivalent pattern sets that are
2678
+ // faster to process in a filesystem walk. The goal is to
2679
+ // eliminate what we can, and push all ** patterns as far
2680
+ // to the right as possible, even if it increases the number
2681
+ // of patterns that we have to process.
2682
+ preprocess(globParts) {
2683
+ // if we're not in globstar mode, then turn ** into *
2684
+ if (this.options.noglobstar) {
2685
+ for (const partset of globParts) {
2686
+ for (let j = 0; j < partset.length; j++) {
2687
+ if (partset[j] === '**') {
2688
+ partset[j] = '*';
2689
+ }
2690
+ }
2691
+ }
2692
+ }
2693
+ const { optimizationLevel = 1 } = this.options;
2694
+ if (optimizationLevel >= 2) {
2695
+ // aggressive optimization for the purpose of fs walking
2696
+ globParts = this.firstPhasePreProcess(globParts);
2697
+ globParts = this.secondPhasePreProcess(globParts);
2698
+ }
2699
+ else if (optimizationLevel >= 1) {
2700
+ // just basic optimizations to remove some .. parts
2701
+ globParts = this.levelOneOptimize(globParts);
2702
+ }
2703
+ else {
2704
+ // just collapse multiple ** portions into one
2705
+ globParts = this.adjascentGlobstarOptimize(globParts);
2706
+ }
2707
+ return globParts;
2708
+ }
2709
+ // just get rid of adjascent ** portions
2710
+ adjascentGlobstarOptimize(globParts) {
2711
+ return globParts.map(parts => {
2712
+ let gs = -1;
2713
+ while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
2714
+ let i = gs;
2715
+ while (parts[i + 1] === '**') {
2716
+ i++;
2717
+ }
2718
+ if (i !== gs) {
2719
+ parts.splice(gs, i - gs);
2720
+ }
2721
+ }
2722
+ return parts;
2723
+ });
2724
+ }
2725
+ // get rid of adjascent ** and resolve .. portions
2726
+ levelOneOptimize(globParts) {
2727
+ return globParts.map(parts => {
2728
+ parts = parts.reduce((set, part) => {
2729
+ const prev = set[set.length - 1];
2730
+ if (part === '**' && prev === '**') {
2731
+ return set;
2732
+ }
2733
+ if (part === '..') {
2734
+ if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
2735
+ set.pop();
2736
+ return set;
2737
+ }
2738
+ }
2739
+ set.push(part);
2740
+ return set;
2741
+ }, []);
2742
+ return parts.length === 0 ? [''] : parts;
2743
+ });
2744
+ }
2745
+ levelTwoFileOptimize(parts) {
2746
+ if (!Array.isArray(parts)) {
2747
+ parts = this.slashSplit(parts);
2748
+ }
2749
+ let didSomething = false;
2750
+ do {
2751
+ didSomething = false;
2752
+ // <pre>/<e>/<rest> -> <pre>/<rest>
2753
+ if (!this.preserveMultipleSlashes) {
2754
+ for (let i = 1; i < parts.length - 1; i++) {
2755
+ const p = parts[i];
2756
+ // don't squeeze out UNC patterns
2757
+ if (i === 1 && p === '' && parts[0] === '')
2758
+ continue;
2759
+ if (p === '.' || p === '') {
2760
+ didSomething = true;
2761
+ parts.splice(i, 1);
2762
+ i--;
2763
+ }
2764
+ }
2765
+ if (parts[0] === '.' &&
2766
+ parts.length === 2 &&
2767
+ (parts[1] === '.' || parts[1] === '')) {
2768
+ didSomething = true;
2769
+ parts.pop();
2770
+ }
2771
+ }
2772
+ // <pre>/<p>/../<rest> -> <pre>/<rest>
2773
+ let dd = 0;
2774
+ while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
2775
+ const p = parts[dd - 1];
2776
+ if (p &&
2777
+ p !== '.' &&
2778
+ p !== '..' &&
2779
+ p !== '**' &&
2780
+ !(this.isWindows && /^[a-z]:$/i.test(p))) {
2781
+ didSomething = true;
2782
+ parts.splice(dd - 1, 2);
2783
+ dd -= 2;
2784
+ }
2785
+ }
2786
+ } while (didSomething);
2787
+ return parts.length === 0 ? [''] : parts;
2788
+ }
2789
+ // First phase: single-pattern processing
2790
+ // <pre> is 1 or more portions
2791
+ // <rest> is 1 or more portions
2792
+ // <p> is any portion other than ., .., '', or **
2793
+ // <e> is . or ''
2794
+ //
2795
+ // **/.. is *brutal* for filesystem walking performance, because
2796
+ // it effectively resets the recursive walk each time it occurs,
2797
+ // and ** cannot be reduced out by a .. pattern part like a regexp
2798
+ // or most strings (other than .., ., and '') can be.
2799
+ //
2800
+ // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
2801
+ // <pre>/<e>/<rest> -> <pre>/<rest>
2802
+ // <pre>/<p>/../<rest> -> <pre>/<rest>
2803
+ // **/**/<rest> -> **/<rest>
2804
+ //
2805
+ // **/*/<rest> -> */**/<rest> <== not valid because ** doesn't follow
2806
+ // this WOULD be allowed if ** did follow symlinks, or * didn't
2807
+ firstPhasePreProcess(globParts) {
2808
+ let didSomething = false;
2809
+ do {
2810
+ didSomething = false;
2811
+ // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
2812
+ for (let parts of globParts) {
2813
+ let gs = -1;
2814
+ while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
2815
+ let gss = gs;
2816
+ while (parts[gss + 1] === '**') {
2817
+ // <pre>/**/**/<rest> -> <pre>/**/<rest>
2818
+ gss++;
2819
+ }
2820
+ // eg, if gs is 2 and gss is 4, that means we have 3 **
2821
+ // parts, and can remove 2 of them.
2822
+ if (gss > gs) {
2823
+ parts.splice(gs + 1, gss - gs);
2824
+ }
2825
+ let next = parts[gs + 1];
2826
+ const p = parts[gs + 2];
2827
+ const p2 = parts[gs + 3];
2828
+ if (next !== '..')
2829
+ continue;
2830
+ if (!p ||
2831
+ p === '.' ||
2832
+ p === '..' ||
2833
+ !p2 ||
2834
+ p2 === '.' ||
2835
+ p2 === '..') {
2836
+ continue;
2837
+ }
2838
+ didSomething = true;
2839
+ // edit parts in place, and push the new one
2840
+ parts.splice(gs, 1);
2841
+ const other = parts.slice(0);
2842
+ other[gs] = '**';
2843
+ globParts.push(other);
2844
+ gs--;
2845
+ }
2846
+ // <pre>/<e>/<rest> -> <pre>/<rest>
2847
+ if (!this.preserveMultipleSlashes) {
2848
+ for (let i = 1; i < parts.length - 1; i++) {
2849
+ const p = parts[i];
2850
+ // don't squeeze out UNC patterns
2851
+ if (i === 1 && p === '' && parts[0] === '')
2852
+ continue;
2853
+ if (p === '.' || p === '') {
2854
+ didSomething = true;
2855
+ parts.splice(i, 1);
2856
+ i--;
2857
+ }
2858
+ }
2859
+ if (parts[0] === '.' &&
2860
+ parts.length === 2 &&
2861
+ (parts[1] === '.' || parts[1] === '')) {
2862
+ didSomething = true;
2863
+ parts.pop();
2864
+ }
2865
+ }
2866
+ // <pre>/<p>/../<rest> -> <pre>/<rest>
2867
+ let dd = 0;
2868
+ while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
2869
+ const p = parts[dd - 1];
2870
+ if (p && p !== '.' && p !== '..' && p !== '**') {
2871
+ didSomething = true;
2872
+ const needDot = dd === 1 && parts[dd + 1] === '**';
2873
+ const splin = needDot ? ['.'] : [];
2874
+ parts.splice(dd - 1, 2, ...splin);
2875
+ if (parts.length === 0)
2876
+ parts.push('');
2877
+ dd -= 2;
2878
+ }
2879
+ }
2880
+ }
2881
+ } while (didSomething);
2882
+ return globParts;
2883
+ }
2884
+ // second phase: multi-pattern dedupes
2885
+ // {<pre>/*/<rest>,<pre>/<p>/<rest>} -> <pre>/*/<rest>
2886
+ // {<pre>/<rest>,<pre>/<rest>} -> <pre>/<rest>
2887
+ // {<pre>/**/<rest>,<pre>/<rest>} -> <pre>/**/<rest>
2888
+ //
2889
+ // {<pre>/**/<rest>,<pre>/**/<p>/<rest>} -> <pre>/**/<rest>
2890
+ // ^-- not valid because ** doens't follow symlinks
2891
+ secondPhasePreProcess(globParts) {
2892
+ for (let i = 0; i < globParts.length - 1; i++) {
2893
+ for (let j = i + 1; j < globParts.length; j++) {
2894
+ const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes);
2895
+ if (matched) {
2896
+ globParts[i] = [];
2897
+ globParts[j] = matched;
2898
+ break;
2899
+ }
2900
+ }
2901
+ }
2902
+ return globParts.filter(gs => gs.length);
2903
+ }
2904
+ partsMatch(a, b, emptyGSMatch = false) {
2905
+ let ai = 0;
2906
+ let bi = 0;
2907
+ let result = [];
2908
+ let which = '';
2909
+ while (ai < a.length && bi < b.length) {
2910
+ if (a[ai] === b[bi]) {
2911
+ result.push(which === 'b' ? b[bi] : a[ai]);
2912
+ ai++;
2913
+ bi++;
2914
+ }
2915
+ else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) {
2916
+ result.push(a[ai]);
2917
+ ai++;
2918
+ }
2919
+ else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) {
2920
+ result.push(b[bi]);
2921
+ bi++;
2922
+ }
2923
+ else if (a[ai] === '*' &&
2924
+ b[bi] &&
2925
+ (this.options.dot || !b[bi].startsWith('.')) &&
2926
+ b[bi] !== '**') {
2927
+ if (which === 'b')
2928
+ return false;
2929
+ which = 'a';
2930
+ result.push(a[ai]);
2931
+ ai++;
2932
+ bi++;
2933
+ }
2934
+ else if (b[bi] === '*' &&
2935
+ a[ai] &&
2936
+ (this.options.dot || !a[ai].startsWith('.')) &&
2937
+ a[ai] !== '**') {
2938
+ if (which === 'a')
2939
+ return false;
2940
+ which = 'b';
2941
+ result.push(b[bi]);
2942
+ ai++;
2943
+ bi++;
2944
+ }
2945
+ else {
2946
+ return false;
2947
+ }
2948
+ }
2949
+ // if we fall out of the loop, it means they two are identical
2950
+ // as long as their lengths match
2951
+ return a.length === b.length && result;
2952
+ }
2953
+ parseNegate() {
2954
+ if (this.nonegate)
2955
+ return;
2956
+ const pattern = this.pattern;
2957
+ let negate = false;
2958
+ let negateOffset = 0;
2959
+ for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
2960
+ negate = !negate;
2961
+ negateOffset++;
2962
+ }
2963
+ if (negateOffset)
2964
+ this.pattern = pattern.slice(negateOffset);
2965
+ this.negate = negate;
2966
+ }
2967
+ // set partial to true to test if, for example,
2968
+ // "/a/b" matches the start of "/*/b/*/d"
2969
+ // Partial means, if you run out of file before you run
2970
+ // out of pattern, then that's fine, as long as all
2971
+ // the parts match.
2972
+ matchOne(file, pattern, partial = false) {
2973
+ let fileStartIndex = 0;
2974
+ let patternStartIndex = 0;
2975
+ // UNC paths like //?/X:/... can match X:/... and vice versa
2976
+ // Drive letters in absolute drive or unc paths are always compared
2977
+ // case-insensitively.
2978
+ if (this.isWindows) {
2979
+ const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]);
2980
+ const fileUNC = !fileDrive &&
2981
+ file[0] === '' &&
2982
+ file[1] === '' &&
2983
+ file[2] === '?' &&
2984
+ /^[a-z]:$/i.test(file[3]);
2985
+ const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]);
2986
+ const patternUNC = !patternDrive &&
2987
+ pattern[0] === '' &&
2988
+ pattern[1] === '' &&
2989
+ pattern[2] === '?' &&
2990
+ typeof pattern[3] === 'string' &&
2991
+ /^[a-z]:$/i.test(pattern[3]);
2992
+ const fdi = fileUNC ? 3
2993
+ : fileDrive ? 0
2994
+ : undefined;
2995
+ const pdi = patternUNC ? 3
2996
+ : patternDrive ? 0
2997
+ : undefined;
2998
+ if (typeof fdi === 'number' && typeof pdi === 'number') {
2999
+ const [fd, pd] = [
3000
+ file[fdi],
3001
+ pattern[pdi],
3002
+ ];
3003
+ // start matching at the drive letter index of each
3004
+ if (fd.toLowerCase() === pd.toLowerCase()) {
3005
+ pattern[pdi] = fd;
3006
+ patternStartIndex = pdi;
3007
+ fileStartIndex = fdi;
3008
+ }
3009
+ }
3010
+ }
3011
+ // resolve and reduce . and .. portions in the file as well.
3012
+ // don't need to do the second phase, because it's only one string[]
3013
+ const { optimizationLevel = 1 } = this.options;
3014
+ if (optimizationLevel >= 2) {
3015
+ file = this.levelTwoFileOptimize(file);
3016
+ }
3017
+ if (pattern.includes(GLOBSTAR)) {
3018
+ return this.#matchGlobstar(file, pattern, partial, fileStartIndex, patternStartIndex);
3019
+ }
3020
+ return this.#matchOne(file, pattern, partial, fileStartIndex, patternStartIndex);
3021
+ }
3022
+ #matchGlobstar(file, pattern, partial, fileIndex, patternIndex) {
3023
+ // split the pattern into head, tail, and middle of ** delimited parts
3024
+ const firstgs = pattern.indexOf(GLOBSTAR, patternIndex);
3025
+ const lastgs = pattern.lastIndexOf(GLOBSTAR);
3026
+ // split the pattern up into globstar-delimited sections
3027
+ // the tail has to be at the end, and the others just have
3028
+ // to be found in order from the head.
3029
+ const [head, body, tail] = partial ?
3030
+ [
3031
+ pattern.slice(patternIndex, firstgs),
3032
+ pattern.slice(firstgs + 1),
3033
+ [],
3034
+ ]
3035
+ : [
3036
+ pattern.slice(patternIndex, firstgs),
3037
+ pattern.slice(firstgs + 1, lastgs),
3038
+ pattern.slice(lastgs + 1),
3039
+ ];
3040
+ // check the head, from the current file/pattern index.
3041
+ if (head.length) {
3042
+ const fileHead = file.slice(fileIndex, fileIndex + head.length);
3043
+ if (!this.#matchOne(fileHead, head, partial, 0, 0)) {
3044
+ return false;
3045
+ }
3046
+ fileIndex += head.length;
3047
+ patternIndex += head.length;
3048
+ }
3049
+ // now we know the head matches!
3050
+ // if the last portion is not empty, it MUST match the end
3051
+ // check the tail
3052
+ let fileTailMatch = 0;
3053
+ if (tail.length) {
3054
+ // if head + tail > file, then we cannot possibly match
3055
+ if (tail.length + fileIndex > file.length)
3056
+ return false;
3057
+ // try to match the tail
3058
+ let tailStart = file.length - tail.length;
3059
+ if (this.#matchOne(file, tail, partial, tailStart, 0)) {
3060
+ fileTailMatch = tail.length;
3061
+ }
3062
+ else {
3063
+ // affordance for stuff like a/**/* matching a/b/
3064
+ // if the last file portion is '', and there's more to the pattern
3065
+ // then try without the '' bit.
3066
+ if (file[file.length - 1] !== '' ||
3067
+ fileIndex + tail.length === file.length) {
3068
+ return false;
3069
+ }
3070
+ tailStart--;
3071
+ if (!this.#matchOne(file, tail, partial, tailStart, 0)) {
3072
+ return false;
3073
+ }
3074
+ fileTailMatch = tail.length + 1;
3075
+ }
3076
+ }
3077
+ // now we know the tail matches!
3078
+ // the middle is zero or more portions wrapped in **, possibly
3079
+ // containing more ** sections.
3080
+ // so a/**/b/**/c/**/d has become **/b/**/c/**
3081
+ // if it's empty, it means a/**/b, just verify we have no bad dots
3082
+ // if there's no tail, so it ends on /**, then we must have *something*
3083
+ // after the head, or it's not a matc
3084
+ if (!body.length) {
3085
+ let sawSome = !!fileTailMatch;
3086
+ for (let i = fileIndex; i < file.length - fileTailMatch; i++) {
3087
+ const f = String(file[i]);
3088
+ sawSome = true;
3089
+ if (f === '.' ||
3090
+ f === '..' ||
3091
+ (!this.options.dot && f.startsWith('.'))) {
3092
+ return false;
3093
+ }
3094
+ }
3095
+ // in partial mode, we just need to get past all file parts
3096
+ return partial || sawSome;
3097
+ }
3098
+ // now we know that there's one or more body sections, which can
3099
+ // be matched anywhere from the 0 index (because the head was pruned)
3100
+ // through to the length-fileTailMatch index.
3101
+ // split the body up into sections, and note the minimum index it can
3102
+ // be found at (start with the length of all previous segments)
3103
+ // [section, before, after]
3104
+ const bodySegments = [[[], 0]];
3105
+ let currentBody = bodySegments[0];
3106
+ let nonGsParts = 0;
3107
+ const nonGsPartsSums = [0];
3108
+ for (const b of body) {
3109
+ if (b === GLOBSTAR) {
3110
+ nonGsPartsSums.push(nonGsParts);
3111
+ currentBody = [[], 0];
3112
+ bodySegments.push(currentBody);
3113
+ }
3114
+ else {
3115
+ currentBody[0].push(b);
3116
+ nonGsParts++;
3117
+ }
3118
+ }
3119
+ let i = bodySegments.length - 1;
3120
+ const fileLength = file.length - fileTailMatch;
3121
+ for (const b of bodySegments) {
3122
+ b[1] = fileLength - (nonGsPartsSums[i--] + b[0].length);
3123
+ }
3124
+ return !!this.#matchGlobStarBodySections(file, bodySegments, fileIndex, 0, partial, 0, !!fileTailMatch);
3125
+ }
3126
+ // return false for "nope, not matching"
3127
+ // return null for "not matching, cannot keep trying"
3128
+ #matchGlobStarBodySections(file,
3129
+ // pattern section, last possible position for it
3130
+ bodySegments, fileIndex, bodyIndex, partial, globStarDepth, sawTail) {
3131
+ // take the first body segment, and walk from fileIndex to its "after"
3132
+ // value at the end
3133
+ // If it doesn't match at that position, we increment, until we hit
3134
+ // that final possible position, and give up.
3135
+ // If it does match, then advance and try to rest.
3136
+ // If any of them fail we keep walking forward.
3137
+ // this is still a bit recursively painful, but it's more constrained
3138
+ // than previous implementations, because we never test something that
3139
+ // can't possibly be a valid matching condition.
3140
+ const bs = bodySegments[bodyIndex];
3141
+ if (!bs) {
3142
+ // just make sure that there's no bad dots
3143
+ for (let i = fileIndex; i < file.length; i++) {
3144
+ sawTail = true;
3145
+ const f = file[i];
3146
+ if (f === '.' ||
3147
+ f === '..' ||
3148
+ (!this.options.dot && f.startsWith('.'))) {
3149
+ return false;
3150
+ }
3151
+ }
3152
+ return sawTail;
3153
+ }
3154
+ // have a non-globstar body section to test
3155
+ const [body, after] = bs;
3156
+ while (fileIndex <= after) {
3157
+ const m = this.#matchOne(file.slice(0, fileIndex + body.length), body, partial, fileIndex, 0);
3158
+ // if limit exceeded, no match. intentional false negative,
3159
+ // acceptable break in correctness for security.
3160
+ if (m && globStarDepth < this.maxGlobstarRecursion) {
3161
+ // match! see if the rest match. if so, we're done!
3162
+ const sub = this.#matchGlobStarBodySections(file, bodySegments, fileIndex + body.length, bodyIndex + 1, partial, globStarDepth + 1, sawTail);
3163
+ if (sub !== false) {
3164
+ return sub;
3165
+ }
3166
+ }
3167
+ const f = file[fileIndex];
3168
+ if (f === '.' ||
3169
+ f === '..' ||
3170
+ (!this.options.dot && f.startsWith('.'))) {
3171
+ return false;
3172
+ }
3173
+ fileIndex++;
3174
+ }
3175
+ // walked off. no point continuing
3176
+ return partial || null;
3177
+ }
3178
+ #matchOne(file, pattern, partial, fileIndex, patternIndex) {
3179
+ let fi;
3180
+ let pi;
3181
+ let pl;
3182
+ let fl;
3183
+ for (fi = fileIndex,
3184
+ pi = patternIndex,
3185
+ fl = file.length,
3186
+ pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
3187
+ this.debug('matchOne loop');
3188
+ let p = pattern[pi];
3189
+ let f = file[fi];
3190
+ this.debug(pattern, p, f);
3191
+ // should be impossible.
3192
+ // some invalid regexp stuff in the set.
3193
+ /* c8 ignore start */
3194
+ if (p === false || p === GLOBSTAR) {
3195
+ return false;
3196
+ }
3197
+ /* c8 ignore stop */
3198
+ // something other than **
3199
+ // non-magic patterns just have to match exactly
3200
+ // patterns with magic have been turned into regexps.
3201
+ let hit;
3202
+ if (typeof p === 'string') {
3203
+ hit = f === p;
3204
+ this.debug('string match', p, f, hit);
3205
+ }
3206
+ else {
3207
+ hit = p.test(f);
3208
+ this.debug('pattern match', p, f, hit);
3209
+ }
3210
+ if (!hit)
3211
+ return false;
3212
+ }
3213
+ // Note: ending in / means that we'll get a final ""
3214
+ // at the end of the pattern. This can only match a
3215
+ // corresponding "" at the end of the file.
3216
+ // If the file ends in /, then it can only match a
3217
+ // a pattern that ends in /, unless the pattern just
3218
+ // doesn't have any more for it. But, a/b/ should *not*
3219
+ // match "a/b/*", even though "" matches against the
3220
+ // [^/]*? pattern, except in partial mode, where it might
3221
+ // simply not be reached yet.
3222
+ // However, a/b/ should still satisfy a/*
3223
+ // now either we fell off the end of the pattern, or we're done.
3224
+ if (fi === fl && pi === pl) {
3225
+ // ran out of pattern and filename at the same time.
3226
+ // an exact hit!
3227
+ return true;
3228
+ }
3229
+ else if (fi === fl) {
3230
+ // ran out of file, but still had pattern left.
3231
+ // this is ok if we're doing the match as part of
3232
+ // a glob fs traversal.
3233
+ return partial;
3234
+ }
3235
+ else if (pi === pl) {
3236
+ // ran out of pattern, still have file left.
3237
+ // this is only acceptable if we're on the very last
3238
+ // empty segment of a file with a trailing slash.
3239
+ // a/* should match a/b/
3240
+ return fi === fl - 1 && file[fi] === '';
3241
+ /* c8 ignore start */
3242
+ }
3243
+ else {
3244
+ // should be unreachable.
3245
+ throw new Error('wtf?');
3246
+ }
3247
+ /* c8 ignore stop */
3248
+ }
3249
+ braceExpand() {
3250
+ return braceExpand(this.pattern, this.options);
3251
+ }
3252
+ parse(pattern) {
3253
+ assertValidPattern(pattern);
3254
+ const options = this.options;
3255
+ // shortcuts
3256
+ if (pattern === '**')
3257
+ return GLOBSTAR;
3258
+ if (pattern === '')
3259
+ return '';
3260
+ // far and away, the most common glob pattern parts are
3261
+ // *, *.*, and *.<ext> Add a fast check method for those.
3262
+ let m;
3263
+ let fastTest = null;
3264
+ if ((m = pattern.match(starRE))) {
3265
+ fastTest = options.dot ? starTestDot : starTest;
3266
+ }
3267
+ else if ((m = pattern.match(starDotExtRE))) {
3268
+ fastTest = (options.nocase ?
3269
+ options.dot ?
3270
+ starDotExtTestNocaseDot
3271
+ : starDotExtTestNocase
3272
+ : options.dot ? starDotExtTestDot
3273
+ : starDotExtTest)(m[1]);
3274
+ }
3275
+ else if ((m = pattern.match(qmarksRE))) {
3276
+ fastTest = (options.nocase ?
3277
+ options.dot ?
3278
+ qmarksTestNocaseDot
3279
+ : qmarksTestNocase
3280
+ : options.dot ? qmarksTestDot
3281
+ : qmarksTest)(m);
3282
+ }
3283
+ else if ((m = pattern.match(starDotStarRE))) {
3284
+ fastTest = options.dot ? starDotStarTestDot : starDotStarTest;
3285
+ }
3286
+ else if ((m = pattern.match(dotStarRE))) {
3287
+ fastTest = dotStarTest;
3288
+ }
3289
+ const re = AST.fromGlob(pattern, this.options).toMMPattern();
3290
+ if (fastTest && typeof re === 'object') {
3291
+ // Avoids overriding in frozen environments
3292
+ Reflect.defineProperty(re, 'test', { value: fastTest });
3293
+ }
3294
+ return re;
3295
+ }
3296
+ makeRe() {
3297
+ if (this.regexp || this.regexp === false)
3298
+ return this.regexp;
3299
+ // at this point, this.set is a 2d array of partial
3300
+ // pattern strings, or "**".
3301
+ //
3302
+ // It's better to use .match(). This function shouldn't
3303
+ // be used, really, but it's pretty convenient sometimes,
3304
+ // when you just want to work with a regex.
3305
+ const set = this.set;
3306
+ if (!set.length) {
3307
+ this.regexp = false;
3308
+ return this.regexp;
3309
+ }
3310
+ const options = this.options;
3311
+ const twoStar = options.noglobstar ? star
3312
+ : options.dot ? twoStarDot
3313
+ : twoStarNoDot;
3314
+ const flags = new Set(options.nocase ? ['i'] : []);
3315
+ // regexpify non-globstar patterns
3316
+ // if ** is only item, then we just do one twoStar
3317
+ // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
3318
+ // if ** is last, append (\/twoStar|) to previous
3319
+ // if ** is in the middle, append (\/|\/twoStar\/) to previous
3320
+ // then filter out GLOBSTAR symbols
3321
+ let re = set
3322
+ .map(pattern => {
3323
+ const pp = pattern.map(p => {
3324
+ if (p instanceof RegExp) {
3325
+ for (const f of p.flags.split(''))
3326
+ flags.add(f);
3327
+ }
3328
+ return (typeof p === 'string' ? regExpEscape(p)
3329
+ : p === GLOBSTAR ? GLOBSTAR
3330
+ : p._src);
3331
+ });
3332
+ pp.forEach((p, i) => {
3333
+ const next = pp[i + 1];
3334
+ const prev = pp[i - 1];
3335
+ if (p !== GLOBSTAR || prev === GLOBSTAR) {
3336
+ return;
3337
+ }
3338
+ if (prev === undefined) {
3339
+ if (next !== undefined && next !== GLOBSTAR) {
3340
+ pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
3341
+ }
3342
+ else {
3343
+ pp[i] = twoStar;
3344
+ }
3345
+ }
3346
+ else if (next === undefined) {
3347
+ pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + ')?';
3348
+ }
3349
+ else if (next !== GLOBSTAR) {
3350
+ pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
3351
+ pp[i + 1] = GLOBSTAR;
3352
+ }
3353
+ });
3354
+ const filtered = pp.filter(p => p !== GLOBSTAR);
3355
+ // For partial matches, we need to make the pattern match
3356
+ // any prefix of the full path. We do this by generating
3357
+ // alternative patterns that match progressively longer prefixes.
3358
+ if (this.partial && filtered.length >= 1) {
3359
+ const prefixes = [];
3360
+ for (let i = 1; i <= filtered.length; i++) {
3361
+ prefixes.push(filtered.slice(0, i).join('/'));
3362
+ }
3363
+ return '(?:' + prefixes.join('|') + ')';
3364
+ }
3365
+ return filtered.join('/');
3366
+ })
3367
+ .join('|');
3368
+ // need to wrap in parens if we had more than one thing with |,
3369
+ // otherwise only the first will be anchored to ^ and the last to $
3370
+ const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
3371
+ // must match entire pattern
3372
+ // ending in a * or ** will make it less strict.
3373
+ re = '^' + open + re + close + '$';
3374
+ // In partial mode, '/' should always match as it's a valid prefix for any pattern
3375
+ if (this.partial) {
3376
+ re = '^(?:\\/|' + open + re.slice(1, -1) + close + ')$';
3377
+ }
3378
+ // can match anything, as long as it's not this.
3379
+ if (this.negate)
3380
+ re = '^(?!' + re + ').+$';
3381
+ try {
3382
+ this.regexp = new RegExp(re, [...flags].join(''));
3383
+ /* c8 ignore start */
3384
+ }
3385
+ catch {
3386
+ // should be impossible
3387
+ this.regexp = false;
3388
+ }
3389
+ /* c8 ignore stop */
3390
+ return this.regexp;
3391
+ }
3392
+ slashSplit(p) {
3393
+ // if p starts with // on windows, we preserve that
3394
+ // so that UNC paths aren't broken. Otherwise, any number of
3395
+ // / characters are coalesced into one, unless
3396
+ // preserveMultipleSlashes is set to true.
3397
+ if (this.preserveMultipleSlashes) {
3398
+ return p.split('/');
3399
+ }
3400
+ else if (this.isWindows && /^\/\/[^/]+/.test(p)) {
3401
+ // add an extra '' for the one we lose
3402
+ return ['', ...p.split(/\/+/)];
3403
+ }
3404
+ else {
3405
+ return p.split(/\/+/);
3406
+ }
3407
+ }
3408
+ match(f, partial = this.partial) {
3409
+ this.debug('match', f, this.pattern);
3410
+ // short-circuit in the case of busted things.
3411
+ // comments, etc.
3412
+ if (this.comment) {
3413
+ return false;
3414
+ }
3415
+ if (this.empty) {
3416
+ return f === '';
3417
+ }
3418
+ if (f === '/' && partial) {
3419
+ return true;
3420
+ }
3421
+ const options = this.options;
3422
+ // windows: need to use /, not \
3423
+ if (this.isWindows) {
3424
+ f = f.split('\\').join('/');
3425
+ }
3426
+ // treat the test path as a set of pathparts.
3427
+ const ff = this.slashSplit(f);
3428
+ this.debug(this.pattern, 'split', ff);
3429
+ // just ONE of the pattern sets in this.set needs to match
3430
+ // in order for it to be valid. If negating, then just one
3431
+ // match means that we have failed.
3432
+ // Either way, return on the first hit.
3433
+ const set = this.set;
3434
+ this.debug(this.pattern, 'set', set);
3435
+ // Find the basename of the path by looking for the last non-empty segment
3436
+ let filename = ff[ff.length - 1];
3437
+ if (!filename) {
3438
+ for (let i = ff.length - 2; !filename && i >= 0; i--) {
3439
+ filename = ff[i];
3440
+ }
3441
+ }
3442
+ for (const pattern of set) {
3443
+ let file = ff;
3444
+ if (options.matchBase && pattern.length === 1) {
3445
+ file = [filename];
3446
+ }
3447
+ const hit = this.matchOne(file, pattern, partial);
3448
+ if (hit) {
3449
+ if (options.flipNegate) {
3450
+ return true;
3451
+ }
3452
+ return !this.negate;
3453
+ }
3454
+ }
3455
+ // didn't get any hits. this is success if it's a negative
3456
+ // pattern, failure otherwise.
3457
+ if (options.flipNegate) {
3458
+ return false;
3459
+ }
3460
+ return this.negate;
3461
+ }
3462
+ static defaults(def) {
3463
+ return minimatch.defaults(def).Minimatch;
3464
+ }
3465
+ }
3466
+ /* c8 ignore stop */
3467
+ minimatch.AST = AST;
3468
+ minimatch.Minimatch = Minimatch;
3469
+ minimatch.escape = escape;
3470
+ minimatch.unescape = unescape;
3471
+
3472
+ class FluxionRouter {
3473
+ constructor(cx) {
3474
+ /**
3475
+ * This means the request has been handled by static resource handler, and no more response should be sent.
3476
+ */
3477
+ this.StaticHandled = Symbol.for('fluxion.router.StaticHandled');
3478
+ this.handlers = new Map();
3479
+ this.cx = cx;
3480
+ }
3481
+ makeStaticResource(filepath) {
3482
+ const fullPath = path$1.isAbsolute(this.cx.options.dir)
3483
+ ? path$1.join(this.cx.options.dir, filepath)
3484
+ : path$1.join(process.cwd(), this.cx.options.dir, filepath);
3485
+ return async (normalized, _req, res) => {
3486
+ if (normalized.method !== 'GET' && normalized.method !== 'HEAD') {
3487
+ res.statusCode = 405;
3488
+ res.setHeader('Allow', 'GET, HEAD');
3489
+ res.end();
3490
+ return;
3491
+ }
3492
+ if (!fs.existsSync(fullPath)) {
3493
+ res.statusCode = 404;
3494
+ res.end('Not Found');
3495
+ return;
3496
+ }
3497
+ const stat = fs.statSync(fullPath);
3498
+ if (!stat.isFile()) {
3499
+ res.statusCode = 404;
3500
+ res.end('Not Found');
3501
+ return;
3502
+ }
3503
+ const extension = path$1.extname(filepath).toLowerCase();
3504
+ const contentType = STATIC_CONTENT_TYPES[extension] ?? 'application/octet-stream';
3505
+ res.statusCode = 200;
3506
+ res.setHeader('Content-Type', contentType);
3507
+ res.setHeader('Content-Length', String(stat.size));
3508
+ if (normalized.method === 'HEAD') {
3509
+ res.end();
3510
+ return;
3511
+ }
3512
+ return new Promise((resolve, reject) => {
3513
+ const stream = fs.createReadStream(fullPath);
3514
+ stream.on('error', reject);
3515
+ stream.on('end', () => resolve(this.StaticHandled));
3516
+ stream.pipe(res);
3517
+ });
3518
+ };
3519
+ }
3520
+ /**
3521
+ * File registration logic with fast-glob pattern matching:
3522
+ * 1. Check if the path exists, if not, delete the handler;
3523
+ * 2. If file doesn't match include patterns, skip registration;
3524
+ * 3. If file matches exclude patterns, skip registration;
3525
+ * 4. If file matches apiInclude patterns, register as API handler;
3526
+ * 5. Otherwise, register as static resource.
3527
+ * @param filepath
3528
+ */
3529
+ register(filepath) {
3530
+ const fullpath = path$1.isAbsolute(this.cx.options.dir)
3531
+ ? path$1.join(this.cx.options.dir, filepath)
3532
+ : path$1.join(process.cwd(), this.cx.options.dir, filepath);
3533
+ if (!fs.existsSync(fullpath)) {
3534
+ this.handlers.delete(filepath);
3535
+ this.cx.logger.info(`[${filepath}] deleted`);
3536
+ return;
3537
+ }
3538
+ delete require.cache[fullpath];
3539
+ // Step 2: Check if file matches include patterns (default: all files)
3540
+ // If not matching, skip registration
3541
+ const matchesInclude = this.cx.options.include.some((pattern) => minimatch(filepath, pattern));
3542
+ if (!matchesInclude) {
3543
+ this.handlers.delete(filepath);
3544
+ this.cx.logger.info(`[${filepath}] skipped (not in include)`);
3545
+ return;
3546
+ }
3547
+ // Step 3: Check if file matches exclude patterns
3548
+ // If matching, skip registration
3549
+ const matchesExclude = this.cx.options.exclude.some((pattern) => minimatch(filepath, pattern));
3550
+ if (matchesExclude) {
1588
3551
  this.handlers.delete(filepath);
1589
3552
  this.cx.logger.info(`[${filepath}] excluded`);
1590
3553
  return;
1591
3554
  }
1592
- // register as api
1593
- // ! Files with extensions matching `apiExts` are considered as API handlers.
1594
- if (this.cx.options.apiExts.some((ext) => extension === ext)) {
3555
+ // Step 4 & 5: Check if file matches apiInclude patterns
3556
+ // If matching, register as API handler; otherwise as static resource
3557
+ const matchesApiInclude = this.cx.options.apiInclude.some((pattern) => minimatch(filepath, pattern));
3558
+ if (matchesApiInclude) {
1595
3559
  const handler = loadFunction({ modulePath: fullpath });
1596
3560
  this.handlers.set(filepath, handler);
1597
3561
  this.cx.logger.info(`[${filepath}] handler registered`);