@usertour/helpers 0.0.17 → 0.0.20
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/__tests__/condition.test.cjs +928 -0
- package/dist/__tests__/condition.test.d.cts +2 -0
- package/dist/__tests__/condition.test.d.ts +2 -0
- package/dist/__tests__/condition.test.js +545 -0
- package/dist/__tests__/url.test.cjs +314 -54
- package/dist/__tests__/url.test.js +248 -4
- package/dist/chunk-PAESAL23.js +77 -0
- package/dist/chunk-TB7JGI2Q.js +97 -0
- package/dist/chunk-YYIGUZNZ.js +17 -0
- package/dist/conditions/attribute.d.cts +2 -10
- package/dist/conditions/attribute.d.ts +2 -10
- package/dist/conditions/condition.cjs +358 -37
- package/dist/conditions/condition.d.cts +11 -2
- package/dist/conditions/condition.d.ts +11 -2
- package/dist/conditions/condition.js +11 -1
- package/dist/conditions/index.cjs +108 -104
- package/dist/conditions/index.d.cts +1 -1
- package/dist/conditions/index.d.ts +1 -1
- package/dist/conditions/index.js +10 -5
- package/dist/conditions/url-v1.cjs +89 -0
- package/dist/conditions/url-v1.d.cts +3 -0
- package/dist/conditions/url-v1.d.ts +3 -0
- package/dist/{chunk-BC7KXBMF.js → conditions/url-v1.js} +4 -8
- package/dist/conditions/url-v2.cjs +101 -0
- package/dist/conditions/url-v2.d.cts +16 -0
- package/dist/conditions/url-v2.d.ts +16 -0
- package/dist/conditions/url-v2.js +7 -0
- package/dist/conditions/url.cjs +69 -50
- package/dist/conditions/url.js +2 -1
- package/dist/index.cjs +108 -104
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +13 -8
- package/package.json +2 -2
- package/dist/chunk-7CC4WXB3.js +0 -60
|
@@ -34,130 +34,97 @@ __export(conditions_exports, {
|
|
|
34
34
|
evaluateAttributeCondition: () => evaluateAttributeCondition,
|
|
35
35
|
evaluateTimeCondition: () => evaluateTimeCondition,
|
|
36
36
|
evaluateUrlCondition: () => evaluateUrlCondition,
|
|
37
|
+
filterConditionsByType: () => filterConditionsByType,
|
|
38
|
+
isConditionsActived: () => isConditionsActived,
|
|
37
39
|
isEqual: () => import_fast_deep_equal.default,
|
|
38
40
|
isMatchUrlPattern: () => isMatchUrlPattern
|
|
39
41
|
});
|
|
40
42
|
module.exports = __toCommonJS(conditions_exports);
|
|
41
43
|
|
|
42
44
|
// src/conditions/condition.ts
|
|
45
|
+
var import_types2 = require("@usertour/types");
|
|
43
46
|
var import_fast_deep_equal = __toESM(require("fast-deep-equal"), 1);
|
|
44
|
-
var compareConditionsItem = (item1, item2) => {
|
|
45
|
-
const { data = {}, ...others1 } = item1;
|
|
46
|
-
const { data: data2 = {}, ...others2 } = item2;
|
|
47
|
-
if (!(0, import_fast_deep_equal.default)(others2, others1)) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
for (const key in data) {
|
|
51
|
-
if (!(0, import_fast_deep_equal.default)(data[key], data2[key])) {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return true;
|
|
56
|
-
};
|
|
57
|
-
var conditionsIsSame = (rr1, rr2) => {
|
|
58
|
-
const r1 = [...rr1];
|
|
59
|
-
const r2 = [...rr2];
|
|
60
|
-
if (r1.length === 0 && r2.length === 0) {
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
if (r1.length !== r2.length) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
const group1 = r1.filter((item) => item.type === "group");
|
|
67
|
-
const group2 = r2.filter((item) => item.type === "group");
|
|
68
|
-
if (group1.length !== group2.length) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
for (let index = 0; index < r1.length; index++) {
|
|
72
|
-
const item1 = r1[index];
|
|
73
|
-
const item2 = r2[index];
|
|
74
|
-
if (!item1 || !item2) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
if (item1.type === "group") {
|
|
78
|
-
if (!item2.conditions) {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
const c1 = item1.conditions;
|
|
82
|
-
const c2 = item2.conditions;
|
|
83
|
-
if (item1.operators !== item2.operators) {
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
if (!conditionsIsSame(c1, c2)) {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
if (!compareConditionsItem(item1, item2)) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return true;
|
|
96
|
-
};
|
|
97
47
|
|
|
98
|
-
// src/conditions/url.ts
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
if (!
|
|
48
|
+
// src/conditions/url-v2.ts
|
|
49
|
+
function parseUrl(url) {
|
|
50
|
+
const match = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
|
|
51
|
+
if (!match)
|
|
102
52
|
return null;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return withWildcard.replace(/:[a-z0-9_]+/g, `[^${s2}]+`);
|
|
114
|
-
};
|
|
115
|
-
var replaceSpecialWords = (str) => {
|
|
53
|
+
const [, , scheme, domain, path, , query, fragment] = match;
|
|
54
|
+
return {
|
|
55
|
+
scheme: scheme || "",
|
|
56
|
+
domain: domain || "",
|
|
57
|
+
path: path || "",
|
|
58
|
+
query: query || "",
|
|
59
|
+
fragment: fragment || ""
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function escapeRegex(str) {
|
|
116
63
|
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
64
|
+
}
|
|
65
|
+
function processUrlPattern(pattern, wildcardReplacement, paramReplacement) {
|
|
66
|
+
let processed = escapeRegex(pattern);
|
|
67
|
+
processed = processed.replace(/\\\*/g, `${wildcardReplacement}*`);
|
|
68
|
+
if (paramReplacement) {
|
|
69
|
+
processed = processed.replace(/:[a-zA-Z0-9_]+/g, `[^${paramReplacement}]+`);
|
|
121
70
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
71
|
+
return processed;
|
|
72
|
+
}
|
|
73
|
+
function urlPatternToRegex(pattern) {
|
|
74
|
+
const parsed = parseUrl(pattern);
|
|
75
|
+
if (!parsed) {
|
|
125
76
|
return null;
|
|
126
77
|
}
|
|
127
|
-
const { scheme, domain, path, query, fragment } =
|
|
128
|
-
const
|
|
129
|
-
const
|
|
130
|
-
const
|
|
131
|
-
const
|
|
132
|
-
let
|
|
78
|
+
const { scheme, domain, path, query, fragment } = parsed;
|
|
79
|
+
const schemePattern = scheme ? escapeRegex(scheme) : "[a-z\\d]+";
|
|
80
|
+
const domainPattern = domain ? processUrlPattern(domain, "[^/]", ".") : "[^/]*";
|
|
81
|
+
const portPattern = "(:\\d+)?";
|
|
82
|
+
const pathPattern = path ? processUrlPattern(path, "[^?#]", "/") : "/[^?#]*";
|
|
83
|
+
let queryPattern;
|
|
133
84
|
if (query) {
|
|
85
|
+
queryPattern = "";
|
|
134
86
|
new URLSearchParams(query).forEach((value, key) => {
|
|
135
|
-
|
|
136
|
-
|
|
87
|
+
let valuePattern;
|
|
88
|
+
if (value === "") {
|
|
89
|
+
valuePattern = "=?";
|
|
90
|
+
} else if (value === "*") {
|
|
91
|
+
valuePattern = "(=[^&#]*)?";
|
|
92
|
+
} else {
|
|
93
|
+
const encodedValue = value.split(/\*/g).map((part) => encodeURI(part)).join("*");
|
|
94
|
+
valuePattern = `=${processUrlPattern(encodedValue, "[^#]")}`;
|
|
95
|
+
}
|
|
96
|
+
queryPattern += `(?=.*[?&]${escapeRegex(key)}${valuePattern}([&#]|$))`;
|
|
137
97
|
});
|
|
138
|
-
|
|
98
|
+
queryPattern += "\\?[^#]*";
|
|
99
|
+
} else {
|
|
100
|
+
queryPattern = "(\\?[^#]*)?";
|
|
139
101
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
return false;
|
|
149
|
-
}) : true;
|
|
150
|
-
const isMatchExcludesConditions = excludes.length > 0 ? excludes.some((_exclude) => {
|
|
151
|
-
const reg = parsePattern(_exclude);
|
|
152
|
-
if (reg) {
|
|
153
|
-
return reg.test(_url);
|
|
154
|
-
}
|
|
102
|
+
const fragmentPattern = fragment ? processUrlPattern(fragment, ".", "/") : "(#.*)?";
|
|
103
|
+
return new RegExp(
|
|
104
|
+
`^${schemePattern}://${domainPattern}${portPattern}${pathPattern}${queryPattern}${fragmentPattern}$`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
function matchUrlPattern(urlPattern, url) {
|
|
108
|
+
if (urlPattern.includes.length === 0 && urlPattern.excludes.length === 0) {
|
|
155
109
|
return false;
|
|
156
|
-
}
|
|
157
|
-
|
|
110
|
+
}
|
|
111
|
+
const matchesInclude = urlPattern.includes.length === 0 || urlPattern.includes.some((includePattern) => {
|
|
112
|
+
const regex = urlPatternToRegex(includePattern);
|
|
113
|
+
return regex && url.match(regex);
|
|
114
|
+
});
|
|
115
|
+
const matchesExclude = urlPattern.excludes.some((excludePattern) => {
|
|
116
|
+
const regex = urlPatternToRegex(excludePattern);
|
|
117
|
+
return regex && url.match(regex);
|
|
118
|
+
});
|
|
119
|
+
return matchesInclude && !matchesExclude;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/conditions/url.ts
|
|
123
|
+
var isMatchUrlPattern = (_url, includes, excludes) => {
|
|
124
|
+
return matchUrlPattern({ includes, excludes }, _url);
|
|
158
125
|
};
|
|
159
126
|
var evaluateUrlCondition = (rules, url) => {
|
|
160
|
-
const { excludes, includes } = rules.data;
|
|
127
|
+
const { excludes = [], includes = [] } = rules.data || {};
|
|
161
128
|
return isMatchUrlPattern(url, includes, excludes);
|
|
162
129
|
};
|
|
163
130
|
|
|
@@ -368,12 +335,49 @@ function evaluateDateTimeCondition(logic, actualValue, expectedValue) {
|
|
|
368
335
|
return false;
|
|
369
336
|
}
|
|
370
337
|
}
|
|
338
|
+
|
|
339
|
+
// src/conditions/condition.ts
|
|
340
|
+
var conditionsIsSame = (rr1, rr2) => {
|
|
341
|
+
return (0, import_fast_deep_equal.default)(rr1, rr2);
|
|
342
|
+
};
|
|
343
|
+
var isConditionsActived = (conditions) => {
|
|
344
|
+
if (!conditions || conditions.length === 0) {
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
const operator = conditions[0].operators;
|
|
348
|
+
const actives = conditions.filter((rule) => {
|
|
349
|
+
if (!rule.conditions) {
|
|
350
|
+
return rule.actived;
|
|
351
|
+
}
|
|
352
|
+
return isConditionsActived(rule.conditions);
|
|
353
|
+
});
|
|
354
|
+
return operator === "and" ? actives.length === conditions.length : actives.length > 0;
|
|
355
|
+
};
|
|
356
|
+
var filterConditionsByType = (conditions, allowedTypes) => {
|
|
357
|
+
return conditions.filter((condition) => {
|
|
358
|
+
if (condition.type === "group" && condition.conditions) {
|
|
359
|
+
const filteredGroupConditions = filterConditionsByType(condition.conditions, allowedTypes);
|
|
360
|
+
return filteredGroupConditions.length > 0;
|
|
361
|
+
}
|
|
362
|
+
return allowedTypes.includes(condition.type);
|
|
363
|
+
}).map((condition) => {
|
|
364
|
+
if (condition.type === "group" && condition.conditions) {
|
|
365
|
+
return {
|
|
366
|
+
...condition,
|
|
367
|
+
conditions: filterConditionsByType(condition.conditions, allowedTypes)
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
return condition;
|
|
371
|
+
});
|
|
372
|
+
};
|
|
371
373
|
// Annotate the CommonJS export names for ESM import in node:
|
|
372
374
|
0 && (module.exports = {
|
|
373
375
|
conditionsIsSame,
|
|
374
376
|
evaluateAttributeCondition,
|
|
375
377
|
evaluateTimeCondition,
|
|
376
378
|
evaluateUrlCondition,
|
|
379
|
+
filterConditionsByType,
|
|
380
|
+
isConditionsActived,
|
|
377
381
|
isEqual,
|
|
378
382
|
isMatchUrlPattern
|
|
379
383
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { conditionsIsSame } from './condition.cjs';
|
|
1
|
+
export { conditionsIsSame, filterConditionsByType, isConditionsActived } from './condition.cjs';
|
|
2
2
|
export { evaluateUrlCondition, isMatchUrlPattern } from './url.cjs';
|
|
3
3
|
export { evaluateTimeCondition } from './time.cjs';
|
|
4
4
|
export { evaluateAttributeCondition } from './attribute.cjs';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { conditionsIsSame } from './condition.js';
|
|
1
|
+
export { conditionsIsSame, filterConditionsByType, isConditionsActived } from './condition.js';
|
|
2
2
|
export { evaluateUrlCondition, isMatchUrlPattern } from './url.js';
|
|
3
3
|
export { evaluateTimeCondition } from './time.js';
|
|
4
4
|
export { evaluateAttributeCondition } from './attribute.js';
|
package/dist/conditions/index.js
CHANGED
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
import "../chunk-7ODE2AIC.js";
|
|
2
2
|
import {
|
|
3
3
|
conditionsIsSame,
|
|
4
|
+
filterConditionsByType,
|
|
5
|
+
isConditionsActived,
|
|
4
6
|
isEqual
|
|
5
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-TB7JGI2Q.js";
|
|
8
|
+
import {
|
|
9
|
+
evaluateUrlCondition,
|
|
10
|
+
isMatchUrlPattern
|
|
11
|
+
} from "../chunk-YYIGUZNZ.js";
|
|
12
|
+
import "../chunk-PAESAL23.js";
|
|
6
13
|
import {
|
|
7
14
|
evaluateAttributeCondition
|
|
8
15
|
} from "../chunk-PBZSPV5R.js";
|
|
9
16
|
import {
|
|
10
17
|
evaluateTimeCondition
|
|
11
18
|
} from "../chunk-CEK3SCQO.js";
|
|
12
|
-
import {
|
|
13
|
-
evaluateUrlCondition,
|
|
14
|
-
isMatchUrlPattern
|
|
15
|
-
} from "../chunk-BC7KXBMF.js";
|
|
16
19
|
import "../chunk-XEO3YXBM.js";
|
|
17
20
|
export {
|
|
18
21
|
conditionsIsSame,
|
|
19
22
|
evaluateAttributeCondition,
|
|
20
23
|
evaluateTimeCondition,
|
|
21
24
|
evaluateUrlCondition,
|
|
25
|
+
filterConditionsByType,
|
|
26
|
+
isConditionsActived,
|
|
22
27
|
isEqual,
|
|
23
28
|
isMatchUrlPattern
|
|
24
29
|
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/conditions/url-v1.ts
|
|
21
|
+
var url_v1_exports = {};
|
|
22
|
+
__export(url_v1_exports, {
|
|
23
|
+
isMatchUrlPattern: () => isMatchUrlPattern
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(url_v1_exports);
|
|
26
|
+
var parseUrl = (url) => {
|
|
27
|
+
const urlPatterns = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
|
|
28
|
+
if (!urlPatterns) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const [, , scheme = "", domain = "", path = "", , query = "", fragment = ""] = urlPatterns;
|
|
32
|
+
return { scheme, domain, path, query, fragment };
|
|
33
|
+
};
|
|
34
|
+
var replaceWildcard = (input, s1, s2) => {
|
|
35
|
+
const withSpecialWords = replaceSpecialWords(input);
|
|
36
|
+
const withWildcard = withSpecialWords.replace(/\\\*/g, `${s1}*`);
|
|
37
|
+
if (!s2) {
|
|
38
|
+
return withWildcard;
|
|
39
|
+
}
|
|
40
|
+
return withWildcard.replace(/:[a-z0-9_]+/g, `[^${s2}]+`);
|
|
41
|
+
};
|
|
42
|
+
var replaceSpecialWords = (str) => {
|
|
43
|
+
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
44
|
+
};
|
|
45
|
+
var parsePattern = (pattern) => {
|
|
46
|
+
if (!pattern || !pattern.trim()) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const _pattern = parseUrl(pattern);
|
|
50
|
+
if (!_pattern) {
|
|
51
|
+
console.error("Invalid URL pattern:", pattern);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const { scheme, domain, path, query, fragment } = _pattern;
|
|
55
|
+
const _scheme = scheme ? replaceSpecialWords(scheme) : "[a-z\\d]+";
|
|
56
|
+
const _domain = domain ? replaceWildcard(domain, "[^/]", ".") : "[^/]*";
|
|
57
|
+
const _fragment = fragment ? replaceWildcard(fragment, ".", "/") : "(#.*)?";
|
|
58
|
+
const _path = path ? replaceWildcard(path, "[^?#]", "/") : "/[^?#]*";
|
|
59
|
+
let _query = "(\\?[^#]*)?";
|
|
60
|
+
if (query) {
|
|
61
|
+
new URLSearchParams(query).forEach((value, key) => {
|
|
62
|
+
const _str = value === "" ? "=?" : value === "*" ? "(=[^&#]*)?" : `=${replaceWildcard(value, "[^#]")}`;
|
|
63
|
+
_query += `(?=.*[?&]${replaceSpecialWords(key)}${_str}([&#]|$))`;
|
|
64
|
+
});
|
|
65
|
+
_query += "\\?[^#]*";
|
|
66
|
+
}
|
|
67
|
+
return new RegExp(`^${_scheme}://${_domain}(:\\d+)?${_path}${_query}${_fragment}$`);
|
|
68
|
+
};
|
|
69
|
+
var isMatchUrlPattern = (_url, includes, excludes) => {
|
|
70
|
+
const isMatchIncludesConditions = includes.length > 0 ? includes.some((_include) => {
|
|
71
|
+
const reg = parsePattern(_include);
|
|
72
|
+
if (reg) {
|
|
73
|
+
return reg.test(_url);
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}) : true;
|
|
77
|
+
const isMatchExcludesConditions = excludes.length > 0 ? excludes.some((_exclude) => {
|
|
78
|
+
const reg = parsePattern(_exclude);
|
|
79
|
+
if (reg) {
|
|
80
|
+
return reg.test(_url);
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}) : false;
|
|
84
|
+
return isMatchIncludesConditions && !isMatchExcludesConditions;
|
|
85
|
+
};
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
isMatchUrlPattern
|
|
89
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import "../chunk-XEO3YXBM.js";
|
|
2
|
+
|
|
3
|
+
// src/conditions/url-v1.ts
|
|
2
4
|
var parseUrl = (url) => {
|
|
3
5
|
const urlPatterns = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
|
|
4
6
|
if (!urlPatterns) {
|
|
@@ -59,12 +61,6 @@ var isMatchUrlPattern = (_url, includes, excludes) => {
|
|
|
59
61
|
}) : false;
|
|
60
62
|
return isMatchIncludesConditions && !isMatchExcludesConditions;
|
|
61
63
|
};
|
|
62
|
-
var evaluateUrlCondition = (rules, url) => {
|
|
63
|
-
const { excludes, includes } = rules.data;
|
|
64
|
-
return isMatchUrlPattern(url, includes, excludes);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
64
|
export {
|
|
68
|
-
isMatchUrlPattern
|
|
69
|
-
evaluateUrlCondition
|
|
65
|
+
isMatchUrlPattern
|
|
70
66
|
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/conditions/url-v2.ts
|
|
21
|
+
var url_v2_exports = {};
|
|
22
|
+
__export(url_v2_exports, {
|
|
23
|
+
matchUrlPattern: () => matchUrlPattern
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(url_v2_exports);
|
|
26
|
+
function parseUrl(url) {
|
|
27
|
+
const match = url.match(/^(([a-z\d]+):\/\/)?([^/?#]+)?(\/[^?#]*)?(\?([^#]*))?(#.*)?$/i);
|
|
28
|
+
if (!match)
|
|
29
|
+
return null;
|
|
30
|
+
const [, , scheme, domain, path, , query, fragment] = match;
|
|
31
|
+
return {
|
|
32
|
+
scheme: scheme || "",
|
|
33
|
+
domain: domain || "",
|
|
34
|
+
path: path || "",
|
|
35
|
+
query: query || "",
|
|
36
|
+
fragment: fragment || ""
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function escapeRegex(str) {
|
|
40
|
+
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
41
|
+
}
|
|
42
|
+
function processUrlPattern(pattern, wildcardReplacement, paramReplacement) {
|
|
43
|
+
let processed = escapeRegex(pattern);
|
|
44
|
+
processed = processed.replace(/\\\*/g, `${wildcardReplacement}*`);
|
|
45
|
+
if (paramReplacement) {
|
|
46
|
+
processed = processed.replace(/:[a-zA-Z0-9_]+/g, `[^${paramReplacement}]+`);
|
|
47
|
+
}
|
|
48
|
+
return processed;
|
|
49
|
+
}
|
|
50
|
+
function urlPatternToRegex(pattern) {
|
|
51
|
+
const parsed = parseUrl(pattern);
|
|
52
|
+
if (!parsed) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const { scheme, domain, path, query, fragment } = parsed;
|
|
56
|
+
const schemePattern = scheme ? escapeRegex(scheme) : "[a-z\\d]+";
|
|
57
|
+
const domainPattern = domain ? processUrlPattern(domain, "[^/]", ".") : "[^/]*";
|
|
58
|
+
const portPattern = "(:\\d+)?";
|
|
59
|
+
const pathPattern = path ? processUrlPattern(path, "[^?#]", "/") : "/[^?#]*";
|
|
60
|
+
let queryPattern;
|
|
61
|
+
if (query) {
|
|
62
|
+
queryPattern = "";
|
|
63
|
+
new URLSearchParams(query).forEach((value, key) => {
|
|
64
|
+
let valuePattern;
|
|
65
|
+
if (value === "") {
|
|
66
|
+
valuePattern = "=?";
|
|
67
|
+
} else if (value === "*") {
|
|
68
|
+
valuePattern = "(=[^&#]*)?";
|
|
69
|
+
} else {
|
|
70
|
+
const encodedValue = value.split(/\*/g).map((part) => encodeURI(part)).join("*");
|
|
71
|
+
valuePattern = `=${processUrlPattern(encodedValue, "[^#]")}`;
|
|
72
|
+
}
|
|
73
|
+
queryPattern += `(?=.*[?&]${escapeRegex(key)}${valuePattern}([&#]|$))`;
|
|
74
|
+
});
|
|
75
|
+
queryPattern += "\\?[^#]*";
|
|
76
|
+
} else {
|
|
77
|
+
queryPattern = "(\\?[^#]*)?";
|
|
78
|
+
}
|
|
79
|
+
const fragmentPattern = fragment ? processUrlPattern(fragment, ".", "/") : "(#.*)?";
|
|
80
|
+
return new RegExp(
|
|
81
|
+
`^${schemePattern}://${domainPattern}${portPattern}${pathPattern}${queryPattern}${fragmentPattern}$`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
function matchUrlPattern(urlPattern, url) {
|
|
85
|
+
if (urlPattern.includes.length === 0 && urlPattern.excludes.length === 0) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const matchesInclude = urlPattern.includes.length === 0 || urlPattern.includes.some((includePattern) => {
|
|
89
|
+
const regex = urlPatternToRegex(includePattern);
|
|
90
|
+
return regex && url.match(regex);
|
|
91
|
+
});
|
|
92
|
+
const matchesExclude = urlPattern.excludes.some((excludePattern) => {
|
|
93
|
+
const regex = urlPatternToRegex(excludePattern);
|
|
94
|
+
return regex && url.match(regex);
|
|
95
|
+
});
|
|
96
|
+
return matchesInclude && !matchesExclude;
|
|
97
|
+
}
|
|
98
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
99
|
+
0 && (module.exports = {
|
|
100
|
+
matchUrlPattern
|
|
101
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL pattern object interface
|
|
3
|
+
*/
|
|
4
|
+
interface UrlPattern {
|
|
5
|
+
includes: string[];
|
|
6
|
+
excludes: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Check if URL matches URL pattern with includes/excludes
|
|
10
|
+
* @param urlPattern - URL pattern object with includes and excludes arrays
|
|
11
|
+
* @param url - URL to check
|
|
12
|
+
* @returns True if URL matches pattern
|
|
13
|
+
*/
|
|
14
|
+
declare function matchUrlPattern(urlPattern: UrlPattern, url: string): boolean;
|
|
15
|
+
|
|
16
|
+
export { matchUrlPattern };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL pattern object interface
|
|
3
|
+
*/
|
|
4
|
+
interface UrlPattern {
|
|
5
|
+
includes: string[];
|
|
6
|
+
excludes: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Check if URL matches URL pattern with includes/excludes
|
|
10
|
+
* @param urlPattern - URL pattern object with includes and excludes arrays
|
|
11
|
+
* @param url - URL to check
|
|
12
|
+
* @returns True if URL matches pattern
|
|
13
|
+
*/
|
|
14
|
+
declare function matchUrlPattern(urlPattern: UrlPattern, url: string): boolean;
|
|
15
|
+
|
|
16
|
+
export { matchUrlPattern };
|