1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.matchPathFilter = void 0;
|
4 | const isGlob = require("is-glob");
|
5 | const micromatch = require("micromatch");
|
6 | const url = require("url");
|
7 | const errors_1 = require("./errors");
|
8 | function matchPathFilter(pathFilter = '/', uri, req) {
|
9 |
|
10 | if (isStringPath(pathFilter)) {
|
11 | return matchSingleStringPath(pathFilter, uri);
|
12 | }
|
13 |
|
14 | if (isGlobPath(pathFilter)) {
|
15 | return matchSingleGlobPath(pathFilter, uri);
|
16 | }
|
17 |
|
18 | if (Array.isArray(pathFilter)) {
|
19 | if (pathFilter.every(isStringPath)) {
|
20 | return matchMultiPath(pathFilter, uri);
|
21 | }
|
22 | if (pathFilter.every(isGlobPath)) {
|
23 | return matchMultiGlobPath(pathFilter, uri);
|
24 | }
|
25 | throw new Error(errors_1.ERRORS.ERR_CONTEXT_MATCHER_INVALID_ARRAY);
|
26 | }
|
27 |
|
28 | if (typeof pathFilter === 'function') {
|
29 | const pathname = getUrlPathName(uri);
|
30 | return pathFilter(pathname, req);
|
31 | }
|
32 | throw new Error(errors_1.ERRORS.ERR_CONTEXT_MATCHER_GENERIC);
|
33 | }
|
34 | exports.matchPathFilter = matchPathFilter;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function matchSingleStringPath(pathFilter, uri) {
|
41 | const pathname = getUrlPathName(uri);
|
42 | return pathname?.indexOf(pathFilter) === 0;
|
43 | }
|
44 | function matchSingleGlobPath(pattern, uri) {
|
45 | const pathname = getUrlPathName(uri);
|
46 | const matches = micromatch([pathname], pattern);
|
47 | return matches && matches.length > 0;
|
48 | }
|
49 | function matchMultiGlobPath(patternList, uri) {
|
50 | return matchSingleGlobPath(patternList, uri);
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function matchMultiPath(pathFilterList, uri) {
|
58 | let isMultiPath = false;
|
59 | for (const context of pathFilterList) {
|
60 | if (matchSingleStringPath(context, uri)) {
|
61 | isMultiPath = true;
|
62 | break;
|
63 | }
|
64 | }
|
65 | return isMultiPath;
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function getUrlPathName(uri) {
|
74 | return uri && url.parse(uri).pathname;
|
75 | }
|
76 | function isStringPath(pathFilter) {
|
77 | return typeof pathFilter === 'string' && !isGlob(pathFilter);
|
78 | }
|
79 | function isGlobPath(pathFilter) {
|
80 | return isGlob(pathFilter);
|
81 | }
|