@url-sanitize/core 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Antonio Orionus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ Note: this MIT license applies to the @url-sanitize/core, @url-sanitize/cli,
24
+ @url-sanitize/fetch, and @url-sanitize/action packages. The
25
+ @url-sanitize/clearurls package ships a catalog derived from the ClearURLs
26
+ ruleset and is licensed under LGPL-3.0-only for that bundled data. See
27
+ packages/clearurls/LICENSE and docs/license-model.md for details.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # @url-sanitize/core
2
+
3
+ Pure TypeScript URL sanitization engine with explainable results and pluggable catalogs.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @url-sanitize/core @url-sanitize/clearurls
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { compileSanitizer } from '@url-sanitize/core';
15
+ import { clearurlsCatalog } from '@url-sanitize/clearurls';
16
+
17
+ const sanitize = compileSanitizer(clearurlsCatalog, {
18
+ stripReferralMarketing: false,
19
+ unwrapRedirects: true,
20
+ domainBlocking: false
21
+ });
22
+
23
+ const result = sanitize('https://example.com/?utm_source=x&id=123');
24
+ ```
25
+
26
+ ## Result shape
27
+
28
+ ```ts
29
+ type SanitizeResult =
30
+ | { kind: 'unchanged'; url: string }
31
+ | { kind: 'cleaned'; original: string; url: string;
32
+ strippedParams: string[]; matchedRules: MatchedRule[] }
33
+ | { kind: 'redirected'; original: string; url: string; via: MatchedRule }
34
+ | { kind: 'blocked'; original: string; via: MatchedRule };
35
+ ```
36
+
37
+ ## Design
38
+
39
+ - Zero runtime dependencies.
40
+ - Pure function; no I/O.
41
+ - Catalog-agnostic: any source (ClearURLs, AdGuard, custom) that produces a `SanitizerCatalog` works.
42
+ - Factory pattern (`compileSanitizer(catalog)`) — no module-level cache, no singletons.
43
+ - Discriminated union return type — exhaustive `switch (result.kind)`.
44
+
45
+ ## License
46
+
47
+ MIT.
@@ -0,0 +1,79 @@
1
+ type RuleSource = 'clearurls' | 'adguard' | 'brave' | 'firefox' | 'custom';
2
+ type SanitizerRule = {
3
+ kind: 'strip-param';
4
+ source: RuleSource;
5
+ provider: string;
6
+ urlPattern?: string;
7
+ paramPattern: string;
8
+ exceptions?: string[];
9
+ isReferralMarketing?: boolean;
10
+ } | {
11
+ kind: 'raw-replace';
12
+ source: RuleSource;
13
+ provider: string;
14
+ urlPattern?: string;
15
+ pattern: string;
16
+ replacement: string;
17
+ exceptions?: string[];
18
+ } | {
19
+ kind: 'unwrap-redirect';
20
+ source: RuleSource;
21
+ provider: string;
22
+ urlPattern?: string;
23
+ pattern: string;
24
+ captureGroup: number;
25
+ exceptions?: string[];
26
+ } | {
27
+ kind: 'block-domain';
28
+ source: RuleSource;
29
+ provider: string;
30
+ urlPattern: string;
31
+ exceptions?: string[];
32
+ };
33
+ interface SanitizerCatalog {
34
+ version: string;
35
+ generatedAt: string;
36
+ sources: Array<{
37
+ name: RuleSource;
38
+ version?: string;
39
+ hash?: string;
40
+ license?: string;
41
+ upstream?: string;
42
+ }>;
43
+ rules: SanitizerRule[];
44
+ }
45
+ interface MatchedRule {
46
+ source: RuleSource;
47
+ provider: string;
48
+ kind: SanitizerRule['kind'];
49
+ detail?: string;
50
+ }
51
+ type SanitizeResult = {
52
+ kind: 'unchanged';
53
+ url: string;
54
+ } | {
55
+ kind: 'cleaned';
56
+ original: string;
57
+ url: string;
58
+ strippedParams: string[];
59
+ matchedRules: MatchedRule[];
60
+ } | {
61
+ kind: 'redirected';
62
+ original: string;
63
+ url: string;
64
+ via: MatchedRule;
65
+ } | {
66
+ kind: 'blocked';
67
+ original: string;
68
+ via: MatchedRule;
69
+ };
70
+ interface SanitizerOptions {
71
+ stripReferralMarketing?: boolean;
72
+ unwrapRedirects?: boolean;
73
+ domainBlocking?: boolean;
74
+ }
75
+
76
+ type Sanitizer = (input: string) => SanitizeResult;
77
+ declare function compileSanitizer(catalog: SanitizerCatalog, options?: SanitizerOptions): Sanitizer;
78
+
79
+ export { type MatchedRule, type RuleSource, type SanitizeResult, type Sanitizer, type SanitizerCatalog, type SanitizerOptions, type SanitizerRule, compileSanitizer };
package/dist/index.js ADDED
@@ -0,0 +1,219 @@
1
+ // src/sanitize.ts
2
+ function safeRegex(pattern, flags = "") {
3
+ try {
4
+ return new RegExp(pattern, flags);
5
+ } catch {
6
+ return null;
7
+ }
8
+ }
9
+ function compileExceptions(patterns) {
10
+ if (!patterns) return [];
11
+ const out = [];
12
+ for (const p of patterns) {
13
+ const re = safeRegex(p, "");
14
+ if (re) out.push(re);
15
+ }
16
+ return out;
17
+ }
18
+ function compileRule(rule) {
19
+ switch (rule.kind) {
20
+ case "strip-param": {
21
+ const paramPattern = safeRegex(`^(?:${rule.paramPattern})$`, "i");
22
+ if (!paramPattern) return null;
23
+ const urlPattern = rule.urlPattern ? safeRegex(rule.urlPattern, "i") : null;
24
+ if (rule.urlPattern && !urlPattern) return null;
25
+ return {
26
+ kind: "strip-param",
27
+ source: rule.source,
28
+ provider: rule.provider,
29
+ urlPattern,
30
+ paramPattern,
31
+ exceptions: compileExceptions(rule.exceptions),
32
+ isReferralMarketing: rule.isReferralMarketing ?? false
33
+ };
34
+ }
35
+ case "raw-replace": {
36
+ const pattern = safeRegex(rule.pattern, "gi");
37
+ if (!pattern) return null;
38
+ const urlPattern = rule.urlPattern ? safeRegex(rule.urlPattern, "i") : null;
39
+ if (rule.urlPattern && !urlPattern) return null;
40
+ return {
41
+ kind: "raw-replace",
42
+ source: rule.source,
43
+ provider: rule.provider,
44
+ urlPattern,
45
+ pattern,
46
+ replacement: rule.replacement,
47
+ exceptions: compileExceptions(rule.exceptions)
48
+ };
49
+ }
50
+ case "unwrap-redirect": {
51
+ const pattern = safeRegex(rule.pattern, "i");
52
+ if (!pattern) return null;
53
+ const urlPattern = rule.urlPattern ? safeRegex(rule.urlPattern, "i") : null;
54
+ if (rule.urlPattern && !urlPattern) return null;
55
+ return {
56
+ kind: "unwrap-redirect",
57
+ source: rule.source,
58
+ provider: rule.provider,
59
+ urlPattern,
60
+ pattern,
61
+ captureGroup: rule.captureGroup,
62
+ exceptions: compileExceptions(rule.exceptions)
63
+ };
64
+ }
65
+ case "block-domain": {
66
+ const urlPattern = safeRegex(rule.urlPattern, "i");
67
+ if (!urlPattern) return null;
68
+ return {
69
+ kind: "block-domain",
70
+ source: rule.source,
71
+ provider: rule.provider,
72
+ urlPattern,
73
+ exceptions: compileExceptions(rule.exceptions)
74
+ };
75
+ }
76
+ }
77
+ }
78
+ function safeDecode(value) {
79
+ try {
80
+ return decodeURIComponent(value);
81
+ } catch {
82
+ return value;
83
+ }
84
+ }
85
+ function matchesAny(url, patterns) {
86
+ return patterns.some((re) => re.test(url));
87
+ }
88
+ function appliesTo(rule, url) {
89
+ if (rule.kind !== "block-domain" && rule.urlPattern && !rule.urlPattern.test(url)) {
90
+ return false;
91
+ }
92
+ if (rule.kind === "block-domain" && !rule.urlPattern.test(url)) return false;
93
+ if (matchesAny(url, rule.exceptions)) return false;
94
+ return true;
95
+ }
96
+ function compileSanitizer(catalog, options = {}) {
97
+ const {
98
+ stripReferralMarketing = false,
99
+ unwrapRedirects = true,
100
+ domainBlocking = false
101
+ } = options;
102
+ const compiled = [];
103
+ for (const rule of catalog.rules) {
104
+ const c = compileRule(rule);
105
+ if (c) compiled.push(c);
106
+ }
107
+ return function sanitize(input) {
108
+ let parsed;
109
+ try {
110
+ parsed = new URL(input);
111
+ } catch {
112
+ return { kind: "unchanged", url: input };
113
+ }
114
+ let current = parsed.toString();
115
+ const original = input;
116
+ const strippedParams = [];
117
+ const matchedRules = [];
118
+ for (const rule of compiled) {
119
+ if (!appliesTo(rule, current)) continue;
120
+ switch (rule.kind) {
121
+ case "block-domain": {
122
+ if (!domainBlocking) continue;
123
+ return {
124
+ kind: "blocked",
125
+ original,
126
+ via: { source: rule.source, provider: rule.provider, kind: rule.kind }
127
+ };
128
+ }
129
+ case "unwrap-redirect": {
130
+ if (!unwrapRedirects) continue;
131
+ const m = rule.pattern.exec(current);
132
+ const captured = m?.[rule.captureGroup];
133
+ if (typeof captured === "string" && captured.length > 0) {
134
+ const target = safeDecode(captured);
135
+ try {
136
+ const targetUrl = new URL(target);
137
+ return {
138
+ kind: "redirected",
139
+ original,
140
+ url: targetUrl.toString(),
141
+ via: { source: rule.source, provider: rule.provider, kind: rule.kind }
142
+ };
143
+ } catch {
144
+ }
145
+ }
146
+ continue;
147
+ }
148
+ case "raw-replace": {
149
+ const next = current.replace(rule.pattern, rule.replacement);
150
+ if (next !== current) {
151
+ current = next;
152
+ matchedRules.push({
153
+ source: rule.source,
154
+ provider: rule.provider,
155
+ kind: rule.kind
156
+ });
157
+ }
158
+ continue;
159
+ }
160
+ case "strip-param": {
161
+ if (rule.isReferralMarketing && !stripReferralMarketing) continue;
162
+ let urlObj;
163
+ try {
164
+ urlObj = new URL(current);
165
+ } catch {
166
+ continue;
167
+ }
168
+ const search = stripFromQueryString(urlObj.search, rule.paramPattern);
169
+ const hash = stripFromQueryString(urlObj.hash, rule.paramPattern);
170
+ const removed = [...search.removed, ...hash.removed];
171
+ if (removed.length === 0) continue;
172
+ urlObj.search = search.result;
173
+ urlObj.hash = hash.result;
174
+ current = urlObj.toString();
175
+ strippedParams.push(...removed);
176
+ matchedRules.push({
177
+ source: rule.source,
178
+ provider: rule.provider,
179
+ kind: rule.kind,
180
+ detail: removed.join(",")
181
+ });
182
+ continue;
183
+ }
184
+ }
185
+ }
186
+ if (current === original) return { kind: "unchanged", url: original };
187
+ return {
188
+ kind: "cleaned",
189
+ original,
190
+ url: current,
191
+ strippedParams,
192
+ matchedRules
193
+ };
194
+ };
195
+ }
196
+ function stripFromQueryString(searchOrHash, paramPattern) {
197
+ if (!searchOrHash) return { result: searchOrHash, removed: [] };
198
+ const prefix = searchOrHash.charAt(0);
199
+ const body = prefix === "?" || prefix === "#" ? searchOrHash.slice(1) : searchOrHash;
200
+ if (!body) return { result: searchOrHash, removed: [] };
201
+ const pairs = body.split("&").filter(Boolean);
202
+ const kept = [];
203
+ const removed = [];
204
+ for (const pair of pairs) {
205
+ const eqIdx = pair.indexOf("=");
206
+ const name = eqIdx === -1 ? pair : pair.slice(0, eqIdx);
207
+ if (paramPattern.test(name)) {
208
+ removed.push(name);
209
+ } else {
210
+ kept.push(pair);
211
+ }
212
+ }
213
+ if (removed.length === 0) return { result: searchOrHash, removed: [] };
214
+ if (kept.length === 0) return { result: "", removed };
215
+ return { result: prefix + kept.join("&"), removed };
216
+ }
217
+ export {
218
+ compileSanitizer
219
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@url-sanitize/core",
3
+ "version": "0.1.0",
4
+ "description": "Pure TypeScript URL sanitization engine with explainable results and pluggable catalogs.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "keywords": [
20
+ "url",
21
+ "sanitize",
22
+ "tracking",
23
+ "clearurls",
24
+ "privacy"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/antonio-orionus/url-sanitize.git",
32
+ "directory": "packages/core"
33
+ },
34
+ "sideEffects": false,
35
+ "scripts": {
36
+ "build": "tsup src/index.ts --format esm --dts --clean",
37
+ "dev": "tsup src/index.ts --format esm --dts --watch"
38
+ }
39
+ }