@tokioapp/adblock 0.0.0-stage → 0.0.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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -2
  3. package/THIRD_PARTY_NOTICES.md +50 -0
  4. package/dist/content/cosmetics.d.ts +22 -0
  5. package/dist/content/cosmetics.js +173 -0
  6. package/dist/content/mainWorld.d.ts +8 -0
  7. package/dist/content/mainWorld.js +10 -0
  8. package/dist/content/preload.d.ts +1 -0
  9. package/dist/content/preload.js +34 -0
  10. package/dist/content/procedural.d.ts +19 -0
  11. package/dist/content/procedural.js +212 -0
  12. package/dist/cosmetic/engine.d.ts +95 -0
  13. package/dist/cosmetic/engine.js +268 -0
  14. package/dist/electron/blocker.d.ts +84 -0
  15. package/dist/electron/blocker.js +320 -0
  16. package/dist/electron/channels.d.ts +10 -0
  17. package/dist/electron/channels.js +6 -0
  18. package/dist/electron/scheme.d.ts +21 -0
  19. package/dist/electron/scheme.js +21 -0
  20. package/dist/engine.d.ts +86 -0
  21. package/dist/engine.js +163 -0
  22. package/dist/index.d.ts +13 -0
  23. package/dist/index.js +12 -0
  24. package/dist/lists/defaults.d.ts +6 -0
  25. package/dist/lists/defaults.js +59 -0
  26. package/dist/lists/manager.d.ts +86 -0
  27. package/dist/lists/manager.js +154 -0
  28. package/dist/lists/preprocessor.d.ts +21 -0
  29. package/dist/lists/preprocessor.js +152 -0
  30. package/dist/lists/storage.d.ts +37 -0
  31. package/dist/lists/storage.js +66 -0
  32. package/dist/network/engine.d.ts +105 -0
  33. package/dist/network/engine.js +431 -0
  34. package/dist/network/filterIndex.d.ts +40 -0
  35. package/dist/network/filterIndex.js +343 -0
  36. package/dist/network/modifiers.d.ts +23 -0
  37. package/dist/network/modifiers.js +192 -0
  38. package/dist/network/pattern.d.ts +7 -0
  39. package/dist/network/pattern.js +145 -0
  40. package/dist/network/request.d.ts +50 -0
  41. package/dist/network/request.js +136 -0
  42. package/dist/network/tokens.d.ts +5 -0
  43. package/dist/network/tokens.js +150 -0
  44. package/dist/parser/cosmetic.d.ts +21 -0
  45. package/dist/parser/cosmetic.js +171 -0
  46. package/dist/parser/hosts.d.ts +20 -0
  47. package/dist/parser/hosts.js +143 -0
  48. package/dist/parser/index.d.ts +40 -0
  49. package/dist/parser/index.js +158 -0
  50. package/dist/parser/network.d.ts +6 -0
  51. package/dist/parser/network.js +474 -0
  52. package/dist/parser/selector.d.ts +25 -0
  53. package/dist/parser/selector.js +522 -0
  54. package/dist/parser/types.d.ts +175 -0
  55. package/dist/parser/types.js +75 -0
  56. package/dist/preload.cjs +1988 -0
  57. package/dist/psl/psl.d.ts +32 -0
  58. package/dist/psl/psl.js +119 -0
  59. package/dist/resources/binary.d.ts +9 -0
  60. package/dist/resources/binary.js +54 -0
  61. package/dist/resources/defusers.d.ts +29 -0
  62. package/dist/resources/defusers.js +180 -0
  63. package/dist/resources/documents.d.ts +12 -0
  64. package/dist/resources/documents.js +56 -0
  65. package/dist/resources/ima.d.ts +6 -0
  66. package/dist/resources/ima.js +313 -0
  67. package/dist/resources/index.d.ts +14 -0
  68. package/dist/resources/index.js +85 -0
  69. package/dist/resources/surrogates.d.ts +30 -0
  70. package/dist/resources/surrogates.js +486 -0
  71. package/dist/scriptlets/abort.d.ts +5 -0
  72. package/dist/scriptlets/abort.js +97 -0
  73. package/dist/scriptlets/dom/attributes.d.ts +5 -0
  74. package/dist/scriptlets/dom/attributes.js +92 -0
  75. package/dist/scriptlets/dom/click.d.ts +2 -0
  76. package/dist/scriptlets/dom/click.js +107 -0
  77. package/dist/scriptlets/dom/cookies.d.ts +16 -0
  78. package/dist/scriptlets/dom/cookies.js +194 -0
  79. package/dist/scriptlets/dom/createHtml.d.ts +2 -0
  80. package/dist/scriptlets/dom/createHtml.js +27 -0
  81. package/dist/scriptlets/dom/dialogs.d.ts +3 -0
  82. package/dist/scriptlets/dom/dialogs.js +21 -0
  83. package/dist/scriptlets/dom/hrefSanitizer.d.ts +2 -0
  84. package/dist/scriptlets/dom/hrefSanitizer.js +91 -0
  85. package/dist/scriptlets/dom/index.d.ts +3 -0
  86. package/dist/scriptlets/dom/index.js +38 -0
  87. package/dist/scriptlets/dom/navigation.d.ts +5 -0
  88. package/dist/scriptlets/dom/navigation.js +60 -0
  89. package/dist/scriptlets/dom/nodeText.d.ts +3 -0
  90. package/dist/scriptlets/dom/nodeText.js +98 -0
  91. package/dist/scriptlets/dom/spoofCss.d.ts +2 -0
  92. package/dist/scriptlets/dom/spoofCss.js +58 -0
  93. package/dist/scriptlets/dom/storage.d.ts +5 -0
  94. package/dist/scriptlets/dom/storage.js +50 -0
  95. package/dist/scriptlets/dom/watch.d.ts +8 -0
  96. package/dist/scriptlets/dom/watch.js +41 -0
  97. package/dist/scriptlets/events.d.ts +2 -0
  98. package/dist/scriptlets/events.js +42 -0
  99. package/dist/scriptlets/helpers.d.ts +58 -0
  100. package/dist/scriptlets/helpers.js +206 -0
  101. package/dist/scriptlets/index.d.ts +6 -0
  102. package/dist/scriptlets/index.js +47 -0
  103. package/dist/scriptlets/network/edit.d.ts +9 -0
  104. package/dist/scriptlets/network/edit.js +104 -0
  105. package/dist/scriptlets/network/eval.d.ts +3 -0
  106. package/dist/scriptlets/network/eval.js +22 -0
  107. package/dist/scriptlets/network/fetch.d.ts +10 -0
  108. package/dist/scriptlets/network/fetch.js +145 -0
  109. package/dist/scriptlets/network/index.d.ts +3 -0
  110. package/dist/scriptlets/network/index.js +43 -0
  111. package/dist/scriptlets/network/jsonPath.d.ts +13 -0
  112. package/dist/scriptlets/network/jsonPath.js +482 -0
  113. package/dist/scriptlets/network/media.d.ts +3 -0
  114. package/dist/scriptlets/network/media.js +97 -0
  115. package/dist/scriptlets/network/methods.d.ts +5 -0
  116. package/dist/scriptlets/network/methods.js +139 -0
  117. package/dist/scriptlets/network/prune.d.ts +13 -0
  118. package/dist/scriptlets/network/prune.js +194 -0
  119. package/dist/scriptlets/network/rendering.d.ts +4 -0
  120. package/dist/scriptlets/network/rendering.js +46 -0
  121. package/dist/scriptlets/network/replace.d.ts +5 -0
  122. package/dist/scriptlets/network/replace.js +168 -0
  123. package/dist/scriptlets/network/request.d.ts +19 -0
  124. package/dist/scriptlets/network/request.js +124 -0
  125. package/dist/scriptlets/network/util.d.ts +19 -0
  126. package/dist/scriptlets/network/util.js +80 -0
  127. package/dist/scriptlets/network/webrtc.d.ts +3 -0
  128. package/dist/scriptlets/network/webrtc.js +62 -0
  129. package/dist/scriptlets/network/windowOpen.d.ts +2 -0
  130. package/dist/scriptlets/network/windowOpen.js +28 -0
  131. package/dist/scriptlets/network/xhr.d.ts +9 -0
  132. package/dist/scriptlets/network/xhr.js +177 -0
  133. package/dist/scriptlets/runner.d.ts +8 -0
  134. package/dist/scriptlets/runner.js +19 -0
  135. package/dist/scriptlets/setConstant.d.ts +5 -0
  136. package/dist/scriptlets/setConstant.js +117 -0
  137. package/dist/scriptlets/timers.d.ts +5 -0
  138. package/dist/scriptlets/timers.js +66 -0
  139. package/dist/scriptlets/types.d.ts +32 -0
  140. package/dist/scriptlets/types.js +1 -0
  141. package/dist/util.d.ts +5 -0
  142. package/dist/util.js +16 -0
  143. package/package.json +83 -4
@@ -0,0 +1,59 @@
1
+ const UASSETS = "https://ublockorigin.github.io/uAssets";
2
+ const UASSETS_MIRRORS = [
3
+ "https://ublockorigin.github.io/uAssetsCDN",
4
+ "https://ublockorigin.pages.dev",
5
+ "https://cdn.jsdelivr.net/gh/uBlockOrigin/uAssetsCDN@main",
6
+ ];
7
+ /** One of uBlock Origin's own lists: trusted, with its CDN mirrors. */
8
+ function ublock(id, title, name) {
9
+ return {
10
+ id,
11
+ title,
12
+ trusted: true,
13
+ urls: [
14
+ `${UASSETS}/filters/${name}.txt`,
15
+ ...UASSETS_MIRRORS.map((mirror) => `${mirror}/filters/${name}.min.txt`),
16
+ ],
17
+ };
18
+ }
19
+ function thirdParty(id, title, name) {
20
+ return {
21
+ id,
22
+ title,
23
+ urls: [
24
+ `${UASSETS}/thirdparties/${name}.txt`,
25
+ "https://cdn.jsdelivr.net/gh/uBlockOrigin/uAssetsCDN@main/thirdparties/" +
26
+ `${name}.txt`,
27
+ `https://ublockorigin.pages.dev/thirdparties/${name}.txt`,
28
+ ],
29
+ };
30
+ }
31
+ /**
32
+ * The lists uBlock Origin enables by default, from its published asset index
33
+ * (`assets/assets.json`), with the same ids.
34
+ */
35
+ export const DEFAULT_LISTS = [
36
+ ublock("ublock-filters", "uBlock filters – Ads", "filters"),
37
+ ublock("ublock-badware", "uBlock filters – Badware risks", "badware"),
38
+ ublock("ublock-privacy", "uBlock filters – Privacy", "privacy"),
39
+ ublock("ublock-unbreak", "uBlock filters – Unbreak", "unbreak"),
40
+ ublock("ublock-quick-fixes", "uBlock filters – Quick fixes", "quick-fixes"),
41
+ thirdParty("easylist", "EasyList", "easylist"),
42
+ thirdParty("easyprivacy", "EasyPrivacy", "easyprivacy"),
43
+ {
44
+ id: "urlhaus-1",
45
+ title: "Online Malicious URL Blocklist",
46
+ urls: [
47
+ "https://malware-filter.gitlab.io/urlhaus-filter/urlhaus-filter-vivaldi-online.txt",
48
+ "https://curbengh.github.io/malware-filter/urlhaus-filter-vivaldi-online.txt",
49
+ ],
50
+ },
51
+ {
52
+ id: "plowe-0",
53
+ title: "Peter Lowe’s Ad and tracking server list",
54
+ urls: [
55
+ "https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=1&mimetype=plaintext",
56
+ ],
57
+ updateAfterDays: 13,
58
+ },
59
+ ];
@@ -0,0 +1,86 @@
1
+ import type { ListStorage, StoredResource } from "./storage.js";
2
+ /** A filter list to download and apply. */
3
+ export interface ListDefinition {
4
+ /** Names the list in the storage and in reports. */
5
+ id: string;
6
+ title?: string;
7
+ /** Tried in order: the list's own URL first, then its mirrors. */
8
+ urls: readonly string[];
9
+ /**
10
+ * The list may use what uBlock Origin keeps for its own lists: trusted
11
+ * scriptlets and the `urlskip` and `uritransform` options. Only for lists
12
+ * whose authors you trust with running code in every page.
13
+ */
14
+ trusted?: boolean;
15
+ /** Days between updates when the list has no `! Expires:` header. */
16
+ updateAfterDays?: number;
17
+ }
18
+ /** The part of `fetch` the lists need, so a host can pass Electron's. */
19
+ export type FetchLike = (url: string, init: {
20
+ headers: Record<string, string>;
21
+ signal: AbortSignal;
22
+ }) => Promise<{
23
+ status: number;
24
+ ok: boolean;
25
+ headers: {
26
+ get(name: string): string | null;
27
+ };
28
+ text(): Promise<string>;
29
+ }>;
30
+ export interface ListManagerOptions {
31
+ storage: ListStorage;
32
+ /** Defaults to the global `fetch`. */
33
+ fetch?: FetchLike;
34
+ /** Milliseconds a download may take (default 30 s). */
35
+ timeoutMs?: number;
36
+ now?: () => number;
37
+ /** Told about failed downloads, which never throw past a stored copy. */
38
+ onError?: (error: Error) => void;
39
+ }
40
+ /** A list ready to parse: its lines, directives applied, sub-lists in place. */
41
+ export interface LoadedList {
42
+ id: string;
43
+ trusted: boolean;
44
+ lines: string[];
45
+ /** Whether a download changed it since the copy in the storage. */
46
+ changed: boolean;
47
+ }
48
+ /**
49
+ * How long a list stays fresh, from its `! Expires:` header (`4 days`,
50
+ * `8 hours`, `1 day (update frequency)`), in ms. Undefined without one.
51
+ */
52
+ export declare function expiresAfter(text: string): number | undefined;
53
+ /**
54
+ * Downloads filter lists and keeps them fresh: mirrors when the first URL
55
+ * fails, `! Expires:` to know when to look again, `ETag` and `Last-Modified`
56
+ * so an unchanged list isn't downloaded twice, and the host's storage so a
57
+ * restart doesn't download anything.
58
+ */
59
+ export declare class ListManager {
60
+ private readonly storage;
61
+ private readonly fetch;
62
+ private readonly timeoutMs;
63
+ private readonly now;
64
+ private readonly onError;
65
+ /** When each resource read so far stops being fresh. */
66
+ private readonly expiries;
67
+ constructor(options: ListManagerOptions);
68
+ /** When the first of the lists loaded so far needs a refresh, in ms. */
69
+ nextExpiry(): number | undefined;
70
+ /**
71
+ * Loads a list: from the storage while it is fresh, downloaded otherwise
72
+ * (always with `force`). A failed download falls back to the stored copy.
73
+ * Rejects only when there is neither.
74
+ */
75
+ load(list: ListDefinition, force?: boolean): Promise<LoadedList>;
76
+ /**
77
+ * Gives a resource's text: the stored copy while fresh, else a download
78
+ * that `If-None-Match` and `If-Modified-Since` keep small.
79
+ */
80
+ resource(key: string, urls: readonly string[], list: Pick<ListDefinition, "updateAfterDays">, force?: boolean): Promise<{
81
+ resource: StoredResource;
82
+ changed: boolean;
83
+ fetched: boolean;
84
+ }>;
85
+ private download;
86
+ }
@@ -0,0 +1,154 @@
1
+ import { toError } from "../util.js";
2
+ import { includesOf, preprocess } from "./preprocessor.js";
3
+ const HOUR = 3_600_000;
4
+ const DAY = 24 * HOUR;
5
+ const DEFAULT_UPDATE_DAYS = 5;
6
+ /**
7
+ * How long a list stays fresh, from its `! Expires:` header (`4 days`,
8
+ * `8 hours`, `1 day (update frequency)`), in ms. Undefined without one.
9
+ */
10
+ export function expiresAfter(text) {
11
+ const match = /^[!#]\s*Expires\s*:\s*(\d+)\s*(h|d)?/im.exec(text.slice(0, 4096));
12
+ if (match === null)
13
+ return undefined;
14
+ const count = Number(match[1]);
15
+ if (count === 0)
16
+ return undefined;
17
+ return count * (match[2]?.toLowerCase() === "h" ? HOUR : DAY);
18
+ }
19
+ /**
20
+ * Downloads filter lists and keeps them fresh: mirrors when the first URL
21
+ * fails, `! Expires:` to know when to look again, `ETag` and `Last-Modified`
22
+ * so an unchanged list isn't downloaded twice, and the host's storage so a
23
+ * restart doesn't download anything.
24
+ */
25
+ export class ListManager {
26
+ storage;
27
+ fetch;
28
+ timeoutMs;
29
+ now;
30
+ onError;
31
+ /** When each resource read so far stops being fresh. */
32
+ expiries = new Map();
33
+ constructor(options) {
34
+ this.storage = options.storage;
35
+ this.fetch = options.fetch ?? ((url, init) => fetch(url, init));
36
+ this.timeoutMs = options.timeoutMs ?? 30_000;
37
+ this.now = options.now ?? Date.now;
38
+ this.onError = options.onError ?? (() => { });
39
+ }
40
+ /** When the first of the lists loaded so far needs a refresh, in ms. */
41
+ nextExpiry() {
42
+ return this.expiries.size === 0
43
+ ? undefined
44
+ : Math.min(...this.expiries.values());
45
+ }
46
+ /**
47
+ * Loads a list: from the storage while it is fresh, downloaded otherwise
48
+ * (always with `force`). A failed download falls back to the stored copy.
49
+ * Rejects only when there is neither.
50
+ */
51
+ async load(list, force = false) {
52
+ const main = await this.resource(list.id, list.urls, list, force);
53
+ const included = new Map();
54
+ let changed = main.changed;
55
+ await Promise.all(includesOf(main.resource.text).map(async (path) => {
56
+ // A sub-list lives next to the list that includes it.
57
+ const urls = [main.resource.url, ...list.urls]
58
+ .map((base) => new URL(path, base).href)
59
+ .filter((url, index, all) => all.indexOf(url) === index);
60
+ try {
61
+ const sub = await this.resource(`${list.id}/${path}`, urls, list,
62
+ // An unchanged list can still have changed sub-lists.
63
+ force || main.fetched);
64
+ included.set(path, sub.resource.text);
65
+ changed ||= sub.changed;
66
+ }
67
+ catch (error) {
68
+ this.onError(toError(error));
69
+ }
70
+ }));
71
+ return {
72
+ id: list.id,
73
+ trusted: list.trusted === true,
74
+ lines: preprocess(main.resource.text, {
75
+ include: (path) => included.get(path),
76
+ }),
77
+ changed,
78
+ };
79
+ }
80
+ /**
81
+ * Gives a resource's text: the stored copy while fresh, else a download
82
+ * that `If-None-Match` and `If-Modified-Since` keep small.
83
+ */
84
+ async resource(key, urls, list, force = false) {
85
+ const stored = await this.storage.read(key);
86
+ const lifetime = (text) => expiresAfter(text) ?? (list.updateAfterDays ?? DEFAULT_UPDATE_DAYS) * DAY;
87
+ if (stored !== undefined &&
88
+ !force &&
89
+ this.now() - stored.fetchedAt < lifetime(stored.text)) {
90
+ this.expiries.set(key, stored.fetchedAt + lifetime(stored.text));
91
+ return { resource: stored, changed: false, fetched: false };
92
+ }
93
+ let resource;
94
+ try {
95
+ resource = await this.download(urls, stored);
96
+ }
97
+ catch (error) {
98
+ if (stored === undefined)
99
+ throw error;
100
+ this.onError(toError(error));
101
+ // Look again in an hour instead of at every load.
102
+ this.expiries.set(key, this.now() + HOUR);
103
+ return { resource: stored, changed: false, fetched: false };
104
+ }
105
+ await this.storage.write(key, resource);
106
+ this.expiries.set(key, resource.fetchedAt + lifetime(resource.text));
107
+ return {
108
+ resource,
109
+ changed: resource.text !== stored?.text,
110
+ fetched: true,
111
+ };
112
+ }
113
+ async download(urls, stored) {
114
+ const failures = [];
115
+ for (const url of urls) {
116
+ try {
117
+ const headers = {};
118
+ // Validators only mean something to the server that gave them.
119
+ if (stored?.url === url) {
120
+ if (stored.etag)
121
+ headers["If-None-Match"] = stored.etag;
122
+ if (stored.lastModified) {
123
+ headers["If-Modified-Since"] = stored.lastModified;
124
+ }
125
+ }
126
+ const response = await this.fetch(url, {
127
+ headers,
128
+ signal: AbortSignal.timeout(this.timeoutMs),
129
+ });
130
+ if (response.status === 304 && stored !== undefined) {
131
+ return { ...stored, fetchedAt: this.now() };
132
+ }
133
+ if (!response.ok)
134
+ throw new Error(`HTTP ${response.status}`);
135
+ const text = await response.text();
136
+ // An error page served with a 200 isn't a list.
137
+ if (text.trim() === "" || /^\s*<(?:!doctype|html)/i.test(text)) {
138
+ throw new Error("not a filter list");
139
+ }
140
+ return {
141
+ text,
142
+ url,
143
+ fetchedAt: this.now(),
144
+ etag: response.headers.get("etag") ?? undefined,
145
+ lastModified: response.headers.get("last-modified") ?? undefined,
146
+ };
147
+ }
148
+ catch (error) {
149
+ failures.push(`${url}: ${toError(error).message}`);
150
+ }
151
+ }
152
+ throw new Error(`Couldn't download ${failures.join("; ")}`);
153
+ }
154
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Evaluates an `!#if` condition: names joined by `&&` and `||`, negated with
3
+ * `!`, grouped with parentheses. False when it can't be read.
4
+ */
5
+ export declare function evaluateCondition(expression: string, environment?: Readonly<Record<string, boolean>>): boolean;
6
+ export interface PreprocessOptions {
7
+ environment?: Readonly<Record<string, boolean>>;
8
+ /**
9
+ * Gives the text of an `!#include`d sub-list, or undefined when it isn't
10
+ * available: the directive is then dropped.
11
+ */
12
+ include?: (path: string) => string | undefined;
13
+ }
14
+ /**
15
+ * Applies a list's pre-parsing directives: keeps the lines of `!#if` blocks
16
+ * whose condition holds (with `!#else`), and puts each `!#include`d sub-list
17
+ * where its directive was. Returns the lines left, directives removed.
18
+ */
19
+ export declare function preprocess(text: string, options?: PreprocessOptions, depth?: number): string[];
20
+ /** The sub-lists a list `!#include`s from blocks whose condition holds. */
21
+ export declare function includesOf(text: string, environment?: Readonly<Record<string, boolean>>): string[];
@@ -0,0 +1,152 @@
1
+ /**
2
+ * What `!#if` conditions see: uBlock Origin on a Chromium desktop browser,
3
+ * without what Electron can't do. A name that isn't here is false.
4
+ */
5
+ const ENVIRONMENT = {
6
+ ext_ublock: true,
7
+ ext_abp: false,
8
+ ext_ubol: false,
9
+ ext_devbuild: false,
10
+ env_chromium: true,
11
+ env_edge: false,
12
+ env_firefox: false,
13
+ env_mobile: false,
14
+ env_safari: false,
15
+ env_mv3: false,
16
+ false: false,
17
+ // Electron can't rewrite a response's body.
18
+ cap_html_filtering: false,
19
+ cap_user_stylesheet: true,
20
+ // Chromium doesn't tell the address a request resolved to.
21
+ cap_ipaddress: false,
22
+ adguard: false,
23
+ adguard_app_android: false,
24
+ adguard_app_ios: false,
25
+ adguard_app_mac: false,
26
+ adguard_app_windows: false,
27
+ adguard_ext_android_cb: false,
28
+ adguard_ext_chromium: true,
29
+ adguard_ext_edge: false,
30
+ adguard_ext_firefox: false,
31
+ adguard_ext_opera: true,
32
+ adguard_ext_safari: false,
33
+ };
34
+ /**
35
+ * Evaluates an `!#if` condition: names joined by `&&` and `||`, negated with
36
+ * `!`, grouped with parentheses. False when it can't be read.
37
+ */
38
+ export function evaluateCondition(expression, environment = ENVIRONMENT) {
39
+ const tokens = expression.match(/&&|\|\||[!()]|[\w-]+|\S/g) ?? [];
40
+ let position = 0;
41
+ let failed = false;
42
+ const or = () => {
43
+ let value = and();
44
+ while (tokens[position] === "||") {
45
+ position++;
46
+ // Not short-circuited: the right side must still be consumed.
47
+ const right = and();
48
+ value ||= right;
49
+ }
50
+ return value;
51
+ };
52
+ const and = () => {
53
+ let value = unary();
54
+ while (tokens[position] === "&&") {
55
+ position++;
56
+ const right = unary();
57
+ value &&= right;
58
+ }
59
+ return value;
60
+ };
61
+ const unary = () => {
62
+ const token = tokens[position++];
63
+ if (token === "!")
64
+ return !unary();
65
+ if (token === "(") {
66
+ const value = or();
67
+ if (tokens[position++] !== ")")
68
+ failed = true;
69
+ return value;
70
+ }
71
+ if (token === undefined || !/^[\w-]+$/.test(token)) {
72
+ failed = true;
73
+ return false;
74
+ }
75
+ return environment[token] === true;
76
+ };
77
+ const value = or();
78
+ return !failed && position === tokens.length && value;
79
+ }
80
+ /** Whether an `!#include` path stays in the list's own directory tree. */
81
+ function isSafeInclude(path) {
82
+ return (/^[\w.-]+(?:\/[\w.-]+)*$/.test(path) &&
83
+ !path.split("/").some((segment) => segment === ".." || segment === "."));
84
+ }
85
+ // A sub-list may include another, but not forever.
86
+ const MAX_DEPTH = 4;
87
+ /**
88
+ * Applies a list's pre-parsing directives: keeps the lines of `!#if` blocks
89
+ * whose condition holds (with `!#else`), and puts each `!#include`d sub-list
90
+ * where its directive was. Returns the lines left, directives removed.
91
+ */
92
+ export function preprocess(text, options = {}, depth = 0) {
93
+ const environment = options.environment ?? ENVIRONMENT;
94
+ const out = [];
95
+ // One entry per open `!#if`: whether its block is live, and whether the
96
+ // block around it is.
97
+ const stack = [];
98
+ let live = true;
99
+ for (const line of text.split(/\r?\n/)) {
100
+ if (!line.startsWith("!#")) {
101
+ if (live)
102
+ out.push(line);
103
+ continue;
104
+ }
105
+ const directive = /^!#(\w+)\s*(.*?)\s*$/.exec(line);
106
+ switch (directive?.[1]) {
107
+ case "if":
108
+ stack.push({ live, outer: live });
109
+ live = live && evaluateCondition(directive[2], environment);
110
+ stack[stack.length - 1].live = live;
111
+ break;
112
+ case "else": {
113
+ const block = stack.at(-1);
114
+ if (block !== undefined)
115
+ live = block.outer && !block.live;
116
+ break;
117
+ }
118
+ case "endif": {
119
+ const block = stack.pop();
120
+ if (block !== undefined)
121
+ live = block.outer;
122
+ break;
123
+ }
124
+ case "include": {
125
+ const path = directive[2];
126
+ if (!live || depth >= MAX_DEPTH || !isSafeInclude(path))
127
+ break;
128
+ const included = options.include?.(path);
129
+ if (included !== undefined) {
130
+ for (const sub of preprocess(included, options, depth + 1)) {
131
+ out.push(sub);
132
+ }
133
+ }
134
+ break;
135
+ }
136
+ }
137
+ }
138
+ return out;
139
+ }
140
+ /** The sub-lists a list `!#include`s from blocks whose condition holds. */
141
+ export function includesOf(text, environment) {
142
+ const paths = [];
143
+ preprocess(text, {
144
+ environment,
145
+ include: (path) => {
146
+ if (!paths.includes(path))
147
+ paths.push(path);
148
+ return undefined;
149
+ },
150
+ });
151
+ return paths;
152
+ }
@@ -0,0 +1,37 @@
1
+ /** A downloaded list, or one of its sub-lists, as the host app keeps it. */
2
+ export interface StoredResource {
3
+ text: string;
4
+ /** Where it was downloaded from. */
5
+ url: string;
6
+ /** When it was last downloaded or confirmed unchanged, in ms. */
7
+ fetchedAt: number;
8
+ etag?: string;
9
+ lastModified?: string;
10
+ }
11
+ /**
12
+ * Where the host app keeps the downloaded lists, so they survive a restart.
13
+ * Keys are list ids, and `<list id>/<path>` for a sub-list.
14
+ */
15
+ export interface ListStorage {
16
+ read(key: string): Promise<StoredResource | undefined>;
17
+ write(key: string, resource: StoredResource): Promise<void>;
18
+ }
19
+ /** Keeps the lists in memory: for tests, and for apps that don't persist. */
20
+ export declare class MemoryStorage implements ListStorage {
21
+ readonly resources: Map<string, StoredResource>;
22
+ read(key: string): Promise<StoredResource | undefined>;
23
+ write(key: string, resource: StoredResource): Promise<void>;
24
+ }
25
+ /**
26
+ * Keeps each list in a folder as `<key>.txt`, with its HTTP validators in
27
+ * `<key>.json`. Files are written to a temporary name and renamed, so a crash
28
+ * midway leaves the old list whole.
29
+ */
30
+ export declare class FileStorage implements ListStorage {
31
+ private readonly directory;
32
+ constructor(directory: string);
33
+ private file;
34
+ read(key: string): Promise<StoredResource | undefined>;
35
+ write(key: string, resource: StoredResource): Promise<void>;
36
+ private replace;
37
+ }
@@ -0,0 +1,66 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { isRecord } from "../util.js";
4
+ /** Keeps the lists in memory: for tests, and for apps that don't persist. */
5
+ export class MemoryStorage {
6
+ resources = new Map();
7
+ async read(key) {
8
+ return this.resources.get(key);
9
+ }
10
+ async write(key, resource) {
11
+ this.resources.set(key, resource);
12
+ }
13
+ }
14
+ /**
15
+ * Keeps each list in a folder as `<key>.txt`, with its HTTP validators in
16
+ * `<key>.json`. Files are written to a temporary name and renamed, so a crash
17
+ * midway leaves the old list whole.
18
+ */
19
+ export class FileStorage {
20
+ directory;
21
+ constructor(directory) {
22
+ this.directory = directory;
23
+ }
24
+ file(key, extension) {
25
+ const name = key.replace(/[^\w.-]+/g, "_");
26
+ return path.join(this.directory, `${name}.${extension}`);
27
+ }
28
+ async read(key) {
29
+ try {
30
+ const [text, meta] = await Promise.all([
31
+ fs.readFile(this.file(key, "txt"), "utf8"),
32
+ fs.readFile(this.file(key, "json"), "utf8"),
33
+ ]);
34
+ const parsed = JSON.parse(meta);
35
+ if (!isRecord(parsed))
36
+ return undefined;
37
+ const { url, fetchedAt, etag, lastModified } = parsed;
38
+ if (typeof url !== "string" || typeof fetchedAt !== "number") {
39
+ return undefined;
40
+ }
41
+ return {
42
+ text,
43
+ url,
44
+ fetchedAt,
45
+ etag: typeof etag === "string" ? etag : undefined,
46
+ lastModified: typeof lastModified === "string" ? lastModified : undefined,
47
+ };
48
+ }
49
+ catch {
50
+ // Missing or unreadable: the list is downloaded again.
51
+ return undefined;
52
+ }
53
+ }
54
+ async write(key, resource) {
55
+ const { text, ...meta } = resource;
56
+ await fs.mkdir(this.directory, { recursive: true });
57
+ // The text first: validators without their text would skip a download.
58
+ await this.replace(this.file(key, "txt"), text);
59
+ await this.replace(this.file(key, "json"), JSON.stringify(meta));
60
+ }
61
+ async replace(file, data) {
62
+ const temporary = `${file}.${process.pid}.tmp`;
63
+ await fs.writeFile(temporary, data);
64
+ await fs.rename(temporary, file);
65
+ }
66
+ }
@@ -0,0 +1,105 @@
1
+ import { type NetworkRule } from "../parser/types.js";
2
+ import { PublicSuffixList } from "../psl/psl.js";
3
+ import { Request, type RequestInfo } from "./request.js";
4
+ export interface NetworkEngineOptions {
5
+ psl?: PublicSuffixList;
6
+ /**
7
+ * Gives a redirect resource's own name from the name or alias a filter
8
+ * uses, or undefined when there is no such resource: the filter is then
9
+ * discarded, as uBlock Origin does. Defaults to the library's resources.
10
+ */
11
+ resource?: (token: string) => string | undefined;
12
+ }
13
+ /** What to do with a request. */
14
+ export interface Decision {
15
+ /** Block the request (or, with `redirect`, answer it locally). */
16
+ block: boolean;
17
+ /** The filter that decided: the block, or the exception that lifted one. */
18
+ filter: string | undefined;
19
+ /** The redirect resource to answer a blocked request with. */
20
+ redirect: string | undefined;
21
+ }
22
+ /** What to do with a response. */
23
+ export interface ResponseDecision {
24
+ /** Block it: a `header=` filter matched its headers. */
25
+ block: boolean;
26
+ filter: string | undefined;
27
+ /** Policies to add as `Content-Security-Policy` headers. */
28
+ csp: string[];
29
+ /** Policies to add as `Permissions-Policy` headers. */
30
+ permissions: string[];
31
+ }
32
+ export interface NetworkStats {
33
+ /** Rules in effect. */
34
+ rules: number;
35
+ /** Rules a `badfilter` removed. */
36
+ badfiltered: number;
37
+ /** `redirect=` names without a resource, with how many filters use each. */
38
+ unknownResources: Record<string, number>;
39
+ }
40
+ /**
41
+ * Decides what happens to requests: uBlock Origin's static network filtering.
42
+ * Add every list's rules, call `finalize()`, then ask.
43
+ */
44
+ export declare class NetworkEngine {
45
+ readonly psl: PublicSuffixList;
46
+ readonly stats: NetworkStats;
47
+ private readonly resource;
48
+ private pending;
49
+ /** Block and exception filters, the block half of `redirect=` included. */
50
+ private readonly blocks;
51
+ /** Redirect directives: `redirect-rule=` and the other half of `redirect=`. */
52
+ private readonly redirects;
53
+ /** `removeparam=` filters for every URL, by the parameter they remove. */
54
+ private readonly params;
55
+ /** The other `removeparam=` filters, and `urlskip=` and `uritransform=`. */
56
+ private readonly rewrites;
57
+ /** `header=`, `csp=` and `permissions=`: decided when the response comes. */
58
+ private readonly responses;
59
+ /** Exceptions that switch cosmetic filtering off for a document. */
60
+ private readonly switches;
61
+ constructor(options?: NetworkEngineOptions);
62
+ /** Adds a list's network rules. They take effect at `finalize()`. */
63
+ add(rules: readonly NetworkRule[]): void;
64
+ /** Applies the `badfilter`s and files every rule. */
65
+ finalize(): void;
66
+ /** How each kind of rule is filed, for `yarn bench`. */
67
+ describe(): Record<string, string>;
68
+ /**
69
+ * `finalize()` in steps: a host with an event loop gives it a turn between
70
+ * them, so filing 230,000 rules doesn't freeze its main process.
71
+ */
72
+ finalizing(): Generator<void>;
73
+ private file;
74
+ /** Works out what filters ask about a request, once. */
75
+ request(info: RequestInfo): Request;
76
+ /**
77
+ * Whether to block a request: an `important` block wins, then an
78
+ * exception, then a block.
79
+ */
80
+ match(request: Request): Decision;
81
+ private decide;
82
+ private modifiers;
83
+ /** The directive with the highest priority, `important` ones first. */
84
+ private redirectFor;
85
+ /**
86
+ * The URL to send a request to instead: its own without the parameters
87
+ * `removeparam=` names, or the one `urlskip=` or `uritransform=` gives.
88
+ * Undefined when the URL stays. A blocked request is only rewritten by a
89
+ * `urlskip=` that says `-blocked`.
90
+ */
91
+ rewrite(request: Request, blocked?: boolean): string | undefined;
92
+ /**
93
+ * What a response changes: a `header=` filter can block it, and a document
94
+ * gets the policies of `csp=`, `permissions=`, `inline-script` and
95
+ * `inline-font`. `header` gives a response header's value by its lowercase
96
+ * name.
97
+ */
98
+ response(request: Request, header: (name: string) => string | undefined): ResponseDecision;
99
+ /**
100
+ * Which cosmetic filtering exceptions switch off for a document, as
101
+ * `T_GENERICHIDE`, `T_SPECIFICHIDE` and `T_ELEMHIDE` bits. `request` is the
102
+ * document's own, of type `document`.
103
+ */
104
+ cosmeticSwitches(request: Request): number;
105
+ }