fakefilter 1.1.1581 → 1.1.1582

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.
@@ -0,0 +1,7 @@
1
+ import { FakeFilterDataset } from './types';
2
+ export declare class DomainLookup {
3
+ private readonly domains;
4
+ constructor(dataset: FakeFilterDataset);
5
+ match(domain: string): string | false;
6
+ }
7
+ export declare function getLookup(dataset: FakeFilterDataset): DomainLookup;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DomainLookup = void 0;
4
+ exports.getLookup = getLookup;
5
+ //#endregion
6
+ // Immutable lookup structure built once per dataset. Keys are normalized to
7
+ // lowercase so membership and suffix checks are case-insensitive and the input
8
+ // only needs to be normalized a single time.
9
+ class DomainLookup {
10
+ constructor(dataset) {
11
+ this.domains = new Set();
12
+ for (const key of Object.keys(dataset.domains)) {
13
+ this.domains.add(key.toLowerCase().trim());
14
+ }
15
+ }
16
+ // Returns the matched fake domain (exact host or registrable suffix) or false.
17
+ // A Set is used instead of direct property access so reserved keys such as
18
+ // `constructor` or `toString` cannot produce prototype-chain false positives.
19
+ match(domain) {
20
+ const normalized = domain.toLowerCase().trim();
21
+ // exact match
22
+ if (this.domains.has(normalized))
23
+ return normalized;
24
+ // suffix match on dot boundaries: a.b.fake.com -> fake.com
25
+ const parts = normalized.split('.');
26
+ for (let i = 1; i < parts.length; i++) {
27
+ const suffix = parts.slice(i).join('.');
28
+ if (this.domains.has(suffix))
29
+ return suffix;
30
+ }
31
+ return false;
32
+ }
33
+ }
34
+ exports.DomainLookup = DomainLookup;
35
+ // Datasets are cached by identity so repeated lookups reuse the same Set.
36
+ // The bundled default dataset is a stable singleton object, so it is cached on
37
+ // first use just like any caller-provided dataset.
38
+ const cache = new WeakMap();
39
+ function getLookup(dataset) {
40
+ let lookup = cache.get(dataset);
41
+ if (!lookup) {
42
+ lookup = new DomainLookup(dataset);
43
+ cache.set(dataset, lookup);
44
+ }
45
+ return lookup;
46
+ }
package/dist/index.js CHANGED
@@ -55,6 +55,7 @@ const assert_1 = __importDefault(require("assert"));
55
55
  const https = __importStar(require("https"));
56
56
  const http = __importStar(require("http"));
57
57
  const data_json_1 = __importDefault(require("./json/data.json"));
58
+ const domainLookup_1 = require("./domainLookup");
58
59
  const apiserver = 'https://fakefilter.net';
59
60
  // const apiserver = "http://127.0.0.1:5520" -- local tests
60
61
  const scheme = apiserver.search(/^https/) === 0 ? 'https' : 'http';
@@ -65,15 +66,7 @@ function hostnameFromEmailAddress(email) {
65
66
  }
66
67
  function isFakeDomain(domain, json = false) {
67
68
  const dataset = typeof json === 'boolean' ? data_json_1.default : json;
68
- for (const dom of Object.keys(dataset.domains)) {
69
- // exact match
70
- if (dom === domain.toLowerCase().trim())
71
- return dom;
72
- // subdomain match
73
- if (domain.search(new RegExp(`.+\\.${dom}`)) === 0)
74
- return dom;
75
- }
76
- return false;
69
+ return (0, domainLookup_1.getLookup)(dataset).match(domain);
77
70
  }
78
71
  function fetch(url, timeout = 5000, json = true) {
79
72
  return new Promise((resolve, reject) => {