@whotracksme/reporting 5.1.17
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/.editorconfig +12 -0
- package/.eslintignore +3 -0
- package/.eslintrc +16 -0
- package/.github/workflows/release.yml +34 -0
- package/.github/workflows/tests.yml +20 -0
- package/.prettierrc +9 -0
- package/.tool-versions +1 -0
- package/CHANGELOG.md +795 -0
- package/LICENSE +373 -0
- package/README.md +1 -0
- package/communication/README.md +11 -0
- package/communication/example/index.js +43 -0
- package/communication/example/manifest.json +16 -0
- package/communication/karma.conf.cjs +19 -0
- package/communication/package.json +52 -0
- package/communication/rollup.common-config.cjs +17 -0
- package/communication/rollup.config.cjs +10 -0
- package/communication/src/encoding.js +54 -0
- package/communication/src/errors.js +84 -0
- package/communication/src/index.js +76 -0
- package/communication/src/logger.js +41 -0
- package/communication/src/padding.js +60 -0
- package/communication/src/proxied-http.js +231 -0
- package/communication/src/server-public-key-accessor.js +123 -0
- package/communication/src/timestamps.js +37 -0
- package/communication/src/trusted-clock.js +58 -0
- package/communication/src/utils.js +28 -0
- package/communication/src/zlib.js +17 -0
- package/communication/test/helpers/in-memory-database.js +43 -0
- package/communication/test/server-public-key-accessor.spec.js +135 -0
- package/communication/test/utils.spec.js +43 -0
- package/package.json +50 -0
- package/reporting/TODO.md +2 -0
- package/reporting/assets/request/bloom_filter.dat +0 -0
- package/reporting/assets/request/update.json +1 -0
- package/reporting/example/content.js +11 -0
- package/reporting/example/index.js +132 -0
- package/reporting/example/inspector/index.html +17 -0
- package/reporting/example/inspector/index.js +27 -0
- package/reporting/example/manifests/chromium.json +37 -0
- package/reporting/example/manifests/firefox.json +35 -0
- package/reporting/example/rules.json +62 -0
- package/reporting/example/setup.js +1 -0
- package/reporting/example/tests/test1.html +10 -0
- package/reporting/karma.conf.cjs +22 -0
- package/reporting/package.json +63 -0
- package/reporting/rollup.common-config.cjs +19 -0
- package/reporting/rollup.config.cjs +20 -0
- package/reporting/src/LRU.js +149 -0
- package/reporting/src/activity-estimator.js +298 -0
- package/reporting/src/alive-check.js +139 -0
- package/reporting/src/alive-message-generator.js +142 -0
- package/reporting/src/bloom-filters.js +345 -0
- package/reporting/src/country-provider.js +295 -0
- package/reporting/src/cyrb53.js +32 -0
- package/reporting/src/digest.js +33 -0
- package/reporting/src/doublefetch-page-handler.js +548 -0
- package/reporting/src/duplicate-detector.js +233 -0
- package/reporting/src/errors.js +84 -0
- package/reporting/src/event-listener-queue.js +121 -0
- package/reporting/src/fixed-size-cache.js +63 -0
- package/reporting/src/hash-detector.js +1526 -0
- package/reporting/src/html-parser.js +17 -0
- package/reporting/src/http.js +265 -0
- package/reporting/src/index.js +14 -0
- package/reporting/src/job-scheduler.js +912 -0
- package/reporting/src/logger.js +121 -0
- package/reporting/src/md5.js +334 -0
- package/reporting/src/message-sender.js +51 -0
- package/reporting/src/network.js +140 -0
- package/reporting/src/new-page-approver.js +119 -0
- package/reporting/src/page-aggregator.js +185 -0
- package/reporting/src/page-quorum-check-handler.js +200 -0
- package/reporting/src/page-structure.js +136 -0
- package/reporting/src/pagedb.js +671 -0
- package/reporting/src/pages.js +1798 -0
- package/reporting/src/patterns-updater.js +310 -0
- package/reporting/src/patterns.js +416 -0
- package/reporting/src/persisted-bitarray.js +265 -0
- package/reporting/src/persisted-hashes.js +189 -0
- package/reporting/src/quorum-checker.js +327 -0
- package/reporting/src/random.js +39 -0
- package/reporting/src/reporting.js +290 -0
- package/reporting/src/request/config.js +116 -0
- package/reporting/src/request/content-script.js +77 -0
- package/reporting/src/request/database.js +147 -0
- package/reporting/src/request/dnt.js +24 -0
- package/reporting/src/request/hash.js +101 -0
- package/reporting/src/request/index.js +874 -0
- package/reporting/src/request/page-telemetry.js +42 -0
- package/reporting/src/request/qs-whitelist2.js +247 -0
- package/reporting/src/request/resources/prob.js +601 -0
- package/reporting/src/request/steps/block-rules.js +40 -0
- package/reporting/src/request/steps/check-context.js +37 -0
- package/reporting/src/request/steps/cookie-context.js +209 -0
- package/reporting/src/request/steps/oauth-detector.js +101 -0
- package/reporting/src/request/steps/page-logger.js +177 -0
- package/reporting/src/request/steps/redirect-tagger.js +75 -0
- package/reporting/src/request/steps/subdomain-check.js +34 -0
- package/reporting/src/request/steps/token-checker/index.js +189 -0
- package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
- package/reporting/src/request/steps/token-examiner.js +182 -0
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
- package/reporting/src/request/steps/token-telemetry/index.js +234 -0
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
- package/reporting/src/request/time.js +73 -0
- package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
- package/reporting/src/request/utils/chrome-storage-map.js +282 -0
- package/reporting/src/request/utils/subject.js +37 -0
- package/reporting/src/request/utils/temp-set.js +56 -0
- package/reporting/src/request/utils/tlds.js +55 -0
- package/reporting/src/request/utils.js +41 -0
- package/reporting/src/request-reporter.js +111 -0
- package/reporting/src/sanitizer.js +519 -0
- package/reporting/src/search-extractor.js +338 -0
- package/reporting/src/self-check.js +105 -0
- package/reporting/src/seq-executor.js +43 -0
- package/reporting/src/session-storage.js +399 -0
- package/reporting/src/url-analyzer.js +161 -0
- package/reporting/src/url-cleaner.js +60 -0
- package/reporting/src/utils/pacemaker.js +279 -0
- package/reporting/src/utils/url.js +114 -0
- package/reporting/src/utils.js +345 -0
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
- package/reporting/src/webrequest-pipeline/index.js +287 -0
- package/reporting/src/webrequest-pipeline/logger.js +14 -0
- package/reporting/src/webrequest-pipeline/page-store.js +282 -0
- package/reporting/src/webrequest-pipeline/page.js +140 -0
- package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
- package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
- package/reporting/test/README.md +2 -0
- package/reporting/test/activity-estimator.spec.js +356 -0
- package/reporting/test/alive-check.spec.js +156 -0
- package/reporting/test/alive-message-generator.spec.js +186 -0
- package/reporting/test/country-provider.spec.js +263 -0
- package/reporting/test/doublefetch-page-handler.spec.js +818 -0
- package/reporting/test/duplicate-detector.spec.js +689 -0
- package/reporting/test/helpers/dom-parsers.js +41 -0
- package/reporting/test/helpers/fake-session-storage.js +69 -0
- package/reporting/test/helpers/fast-check-utils.js +28 -0
- package/reporting/test/helpers/fetch-mock.js +57 -0
- package/reporting/test/helpers/in-memory-database.js +14 -0
- package/reporting/test/helpers/memory-map.js +72 -0
- package/reporting/test/helpers/pagedb-mocks.js +41 -0
- package/reporting/test/index.js +47 -0
- package/reporting/test/job-scheduler.spec.js +691 -0
- package/reporting/test/network.spec.js +72 -0
- package/reporting/test/page-aggregator.spec.js +104 -0
- package/reporting/test/page-quorum-check-handler.spec.js +86 -0
- package/reporting/test/pagedb.spec.js +250 -0
- package/reporting/test/pages.spec.js +693 -0
- package/reporting/test/patterns-updater.spec.js +446 -0
- package/reporting/test/patterns.spec.js +751 -0
- package/reporting/test/persisted-hashes.spec.js +169 -0
- package/reporting/test/reporting.spec.js +205 -0
- package/reporting/test/request/database.spec.js +59 -0
- package/reporting/test/request/hash.spec.js +108 -0
- package/reporting/test/request/index.spec.js +378 -0
- package/reporting/test/request/qs-whitelist2.spec.js +189 -0
- package/reporting/test/request/steps/check-context.spec.js +51 -0
- package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
- package/reporting/test/request/test-pages.js +1246 -0
- package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
- package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
- package/reporting/test/request/utils/tldts.spec.js +62 -0
- package/reporting/test/request/utils.spec.js +168 -0
- package/reporting/test/sanitizer.spec.js +1087 -0
- package/reporting/test/search-extractor.spec.js +590 -0
- package/reporting/test/seq-executor.spec.js +158 -0
- package/reporting/test/session-storage.spec.js +400 -0
- package/reporting/test/setup.js +24 -0
- package/reporting/test/url-analyzer.spec.js +451 -0
- package/reporting/test/url-cleaner.spec.js +102 -0
- package/reporting/test/utils/pacemaker.spec.js +222 -0
- package/reporting/test/utils/url.spec.js +660 -0
- package/reporting/test/utils.spec.js +299 -0
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import logger from './logger';
|
|
13
|
+
import random from './random';
|
|
14
|
+
import { fastHash } from './utils';
|
|
15
|
+
|
|
16
|
+
const SECOND = 1000;
|
|
17
|
+
const MINUTE = 60 * SECOND;
|
|
18
|
+
const HOUR = 60 * MINUTE;
|
|
19
|
+
const DAY = 24 * HOUR;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Takes an object and returns a sorted representation. Specially,
|
|
23
|
+
* it ensures that the ordering of object keys does not matter.
|
|
24
|
+
*
|
|
25
|
+
* { foo: 42, bar: 'baz' } <=> { bar: 'baz', foo: 42 }
|
|
26
|
+
*/
|
|
27
|
+
function objectToOrderedArray(x) {
|
|
28
|
+
if (x === null || x === undefined) {
|
|
29
|
+
// both are missing values in our context and therefore equal
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (
|
|
33
|
+
typeof x === 'string' ||
|
|
34
|
+
typeof x === 'number' ||
|
|
35
|
+
typeof x === 'boolean'
|
|
36
|
+
) {
|
|
37
|
+
return x;
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(x)) {
|
|
40
|
+
return x.map(objectToOrderedArray);
|
|
41
|
+
}
|
|
42
|
+
if (typeof x === 'object') {
|
|
43
|
+
// With this simple implementation, these two objects will be mapped to
|
|
44
|
+
// the same representation: { foo: 42 } and [["foo", 42]]
|
|
45
|
+
//
|
|
46
|
+
// For our purpose that is OK:
|
|
47
|
+
// 1) the collision detection will accept false-positive, since later
|
|
48
|
+
// the value will be hashed (to introduce collisions!)
|
|
49
|
+
// 2) the structure of objects with the same action should be the same;
|
|
50
|
+
// in other words, the example here should not happen
|
|
51
|
+
return Object.keys(x)
|
|
52
|
+
.sort()
|
|
53
|
+
.map((key) => [key, objectToOrderedArray(x[key])]);
|
|
54
|
+
}
|
|
55
|
+
throw new Error('Internal error: input contains unexpected types');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function extractPath(obj, path, pos = 0) {
|
|
59
|
+
const nextKey = path[pos];
|
|
60
|
+
if (nextKey === '[]') {
|
|
61
|
+
if (Array.isArray(obj)) {
|
|
62
|
+
const results = obj
|
|
63
|
+
.map((x) => extractPath(x, path, pos + 1))
|
|
64
|
+
.filter((x) => x.match);
|
|
65
|
+
if (results.length > 0) {
|
|
66
|
+
return { match: true, value: results };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return { match: false };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!Object.hasOwn(obj, nextKey)) {
|
|
73
|
+
return { match: false };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const elem = obj[nextKey];
|
|
77
|
+
if (pos === path.length - 1) {
|
|
78
|
+
return { match: true, value: elem };
|
|
79
|
+
}
|
|
80
|
+
return extractPath(elem, path, pos + 1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Two messages are considered duplicates iff the following tuple is the same:
|
|
85
|
+
* 1) action
|
|
86
|
+
* 2) timestamp ("ts" field)
|
|
87
|
+
* 3) key fields in payload
|
|
88
|
+
*
|
|
89
|
+
* By default, all fields in the payload are considered, but this can be
|
|
90
|
+
* overwritten by the "deduplicateBy" parameter. It is a comma-separated
|
|
91
|
+
* list of dot-separated paths.
|
|
92
|
+
*
|
|
93
|
+
* For instance, "k" would only extract the field named "k", so the following
|
|
94
|
+
* two messages would be considered duplicates:
|
|
95
|
+
*
|
|
96
|
+
* msg1 = {
|
|
97
|
+
* "k": "key text",
|
|
98
|
+
* "x": "some text not part of the key"
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* msg2 = {
|
|
102
|
+
* "k": "key text",
|
|
103
|
+
* "x": "another text not part of the key"
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* Both messages would not be duplicates in the default (considering the
|
|
107
|
+
* full payload), or it both keys were listed ("k,x").
|
|
108
|
+
*
|
|
109
|
+
* The path allows to extract inner fields:
|
|
110
|
+
*
|
|
111
|
+
* msg = {
|
|
112
|
+
* "a": 42,
|
|
113
|
+
* "b": {
|
|
114
|
+
* "c: "inner",
|
|
115
|
+
* },
|
|
116
|
+
* "d": "not extracted"
|
|
117
|
+
* }
|
|
118
|
+
*
|
|
119
|
+
* 'a,b.c' will extract the values 42 and "inner", but not the value for field "d".
|
|
120
|
+
*
|
|
121
|
+
* Arrays can be either aggregated with [] or accessed by an index:
|
|
122
|
+
*
|
|
123
|
+
* msg = {
|
|
124
|
+
* a: [1,2,3],
|
|
125
|
+
* b: [{
|
|
126
|
+
* c: 4,
|
|
127
|
+
* d: "ignore"
|
|
128
|
+
* }, {
|
|
129
|
+
* c: 5,
|
|
130
|
+
* d: "ignore",
|
|
131
|
+
* }]
|
|
132
|
+
* }
|
|
133
|
+
*
|
|
134
|
+
* by []: 'a,b.[].c' will extract the values [1,2,3], 4 and 5.
|
|
135
|
+
* by index: 'a,b.0.c' will extract the values [1,2,3] and 4.
|
|
136
|
+
* by index: 'a,b.1.c' will extract the values [1,2,3] and 5.
|
|
137
|
+
*/
|
|
138
|
+
export function computeDeduplicationKey({ body, deduplicateBy = '' }) {
|
|
139
|
+
let keys = body.payload;
|
|
140
|
+
if (!deduplicateBy) {
|
|
141
|
+
keys = body.payload;
|
|
142
|
+
} else {
|
|
143
|
+
keys = deduplicateBy
|
|
144
|
+
.split(',')
|
|
145
|
+
.map((path) => extractPath(body.payload, path.split('.'), 0).value);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const { action, ts } = body;
|
|
149
|
+
return JSON.stringify([action, ts, objectToOrderedArray(keys)]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Well-behaving clients should not send the same message multiple times.
|
|
154
|
+
* For example, if a user repeats a query, it should only be sent once.
|
|
155
|
+
* (Note: Do not confuse it with duplicate detection on the server side!
|
|
156
|
+
* That it a harder problem, as you cannot trust the clients.)
|
|
157
|
+
*
|
|
158
|
+
* The existance of false positives is acceptable (messages being dropped
|
|
159
|
+
* even though they have not been sent). However, it should not have
|
|
160
|
+
* a noticeable impact on the number of collected messages (on the server).
|
|
161
|
+
*/
|
|
162
|
+
export default class DuplicateDetector {
|
|
163
|
+
constructor(persistedHashes) {
|
|
164
|
+
this.persistedHashes = persistedHashes;
|
|
165
|
+
this.enabled = false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async init() {
|
|
169
|
+
this.enabled = true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
unload() {
|
|
173
|
+
if (this.enabled) {
|
|
174
|
+
this.enabled = false;
|
|
175
|
+
this.persistedHashes.flush();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Checks whether a message should be sent.
|
|
181
|
+
*
|
|
182
|
+
* If you got permission to send but the actual send failed,
|
|
183
|
+
* ensure to call the returned "rollback" callback. Otherwise, you
|
|
184
|
+
* cannot retry, as all later attempts will be rejected as duplicates.
|
|
185
|
+
*/
|
|
186
|
+
async trySend(message) {
|
|
187
|
+
if (!this.enabled) {
|
|
188
|
+
return {
|
|
189
|
+
ok: false,
|
|
190
|
+
rejectReason: 'Module is disabled',
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
let key;
|
|
195
|
+
try {
|
|
196
|
+
key = computeDeduplicationKey(message);
|
|
197
|
+
} catch (e) {
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
rejectReason: `Message must be well-formed (failed at: ${e})`,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const hash = fastHash(key, { truncate: true });
|
|
205
|
+
const expireAt = this._chooseExpiration();
|
|
206
|
+
const wasAdded = await this.persistedHashes.add(hash, expireAt);
|
|
207
|
+
if (!wasAdded) {
|
|
208
|
+
return {
|
|
209
|
+
ok: false,
|
|
210
|
+
rejectReason: `Hash for the deduplication key has been already seen (key: ${key}, hash: ${hash})`,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
ok: true,
|
|
215
|
+
rollback: async () => {
|
|
216
|
+
try {
|
|
217
|
+
await this.persistedHashes.delete(hash);
|
|
218
|
+
} catch (e) {
|
|
219
|
+
logger.warn('Failed to rollback. Message cannot be resent.', e);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
_chooseExpiration() {
|
|
226
|
+
// conservative approach: remove entries after one or two days
|
|
227
|
+
// (faster expiration should not make a functional difference,
|
|
228
|
+
// as messages include the timestamp in their hashes).
|
|
229
|
+
// Adding noise is done to make it harder to guess at what
|
|
230
|
+
// time the hash was added.
|
|
231
|
+
return Date.now() + Math.ceil((1.0 + random()) * DAY);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// The concept here is taken from ../../communication/src/errors.es
|
|
13
|
+
// (see comments there for details). In a nutshell, by classifying errors in
|
|
14
|
+
// being either recoverable or permanent, you can give hints to steer
|
|
15
|
+
// the error recovery.
|
|
16
|
+
|
|
17
|
+
class ExtendableError extends Error {
|
|
18
|
+
constructor(message, options) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
this.name = this.constructor.name;
|
|
21
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
22
|
+
Error.captureStackTrace(this, this.constructor);
|
|
23
|
+
} else {
|
|
24
|
+
this.stack = new Error(message).stack;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class RecoverableError extends ExtendableError {
|
|
30
|
+
constructor(message, options) {
|
|
31
|
+
super(message, options);
|
|
32
|
+
this.isRecoverableError = true;
|
|
33
|
+
this.isPermanentError = false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class PermanentError extends ExtendableError {
|
|
38
|
+
constructor(message, options) {
|
|
39
|
+
super(message, options);
|
|
40
|
+
this.isRecoverableError = false;
|
|
41
|
+
this.isPermanentError = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* For jobs that are ill-formed (e.g. have missing or invalid fields).
|
|
47
|
+
*/
|
|
48
|
+
export class BadJobError extends PermanentError {}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* If trying to override HTTP headers, but the platform does not support it.
|
|
52
|
+
*/
|
|
53
|
+
export class UnableToOverrideHeadersError extends PermanentError {}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Thrown when requests failed, but where the client can try to
|
|
57
|
+
* repeat the request without modification (e.g. timeouts will
|
|
58
|
+
* fall into this category).
|
|
59
|
+
*/
|
|
60
|
+
export class TemporarilyUnableToFetchUrlError extends RecoverableError {}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Thrown when requests failed, but where the client should not
|
|
64
|
+
* repeat the request without modification (e.g. most 4xx errors fall
|
|
65
|
+
* into this category).
|
|
66
|
+
*/
|
|
67
|
+
export class PermanentlyUnableToFetchUrlError extends PermanentError {}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* For 429 (too many request) errors that should be retried.
|
|
71
|
+
*/
|
|
72
|
+
export class RateLimitedByServerError extends TemporarilyUnableToFetchUrlError {}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Thrown if patterns are invalid. This could either be because they are
|
|
76
|
+
* corrupted, or if the client is too outdated.
|
|
77
|
+
*/
|
|
78
|
+
export class BadPatternError extends PermanentError {}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Thrown when an unknown transformation builtin is referrenced.
|
|
82
|
+
* In most situation, it means the client is outdated.
|
|
83
|
+
*/
|
|
84
|
+
export class UnsupportedTransformationError extends PermanentError {}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import logger from './logger';
|
|
13
|
+
import SelfCheck from './self-check';
|
|
14
|
+
|
|
15
|
+
export default class EventListenerQueue {
|
|
16
|
+
constructor({
|
|
17
|
+
connectTimeoutInMs = null,
|
|
18
|
+
maxBufferLength = 1024,
|
|
19
|
+
listeners,
|
|
20
|
+
}) {
|
|
21
|
+
if (!Array.isArray(listeners)) {
|
|
22
|
+
throw new Error('Expect a description of listeners');
|
|
23
|
+
}
|
|
24
|
+
this.detectedErrors = false;
|
|
25
|
+
|
|
26
|
+
if (connectTimeoutInMs && connectTimeoutInMs > 0) {
|
|
27
|
+
this._shutdownTimer = setTimeout(() => {
|
|
28
|
+
logger.warn(
|
|
29
|
+
`broken pipe (timed out after ${connectTimeoutInMs} ms without connection)`,
|
|
30
|
+
);
|
|
31
|
+
this.detectedErrors = true;
|
|
32
|
+
this.close();
|
|
33
|
+
}, connectTimeoutInMs);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this._bufferedEvents = [];
|
|
37
|
+
this._uninstallHandlers = [];
|
|
38
|
+
listeners.forEach(({ method, api, type }) => {
|
|
39
|
+
if (
|
|
40
|
+
chrome &&
|
|
41
|
+
chrome[api] &&
|
|
42
|
+
chrome[api][type]?.addListener &&
|
|
43
|
+
chrome[api][type]?.removeListener
|
|
44
|
+
) {
|
|
45
|
+
const handler = (...args) => {
|
|
46
|
+
this._bufferedEvents.push({ method, args });
|
|
47
|
+
if (
|
|
48
|
+
maxBufferLength &&
|
|
49
|
+
this._bufferedEvents.length > maxBufferLength
|
|
50
|
+
) {
|
|
51
|
+
this.detectedErrors = true;
|
|
52
|
+
logger.warn('buffer overrun: dropping events to', method);
|
|
53
|
+
this._bufferedEvents.shift();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
chrome[api][type].addListener(handler);
|
|
57
|
+
this._uninstallHandlers.push(() => {
|
|
58
|
+
chrome[api][type].removeListener(handler);
|
|
59
|
+
});
|
|
60
|
+
} else {
|
|
61
|
+
logger.debug(
|
|
62
|
+
'Cannot queue api',
|
|
63
|
+
method,
|
|
64
|
+
'(not available on this platform)',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
replayEvents(target) {
|
|
71
|
+
if (!this.detectedErrors) {
|
|
72
|
+
logger.debug(
|
|
73
|
+
`Successfully connected event listeners (buffer length: ${this._bufferedEvents.length})`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
while (this._bufferedEvents.length > 0) {
|
|
78
|
+
const { method, args } = this._bufferedEvents.shift();
|
|
79
|
+
try {
|
|
80
|
+
target[method](...args);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
logger.error('Error while processing event:', method, args, e);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
this.close();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
close() {
|
|
89
|
+
if (this._shutdownTimer) {
|
|
90
|
+
clearTimeout(this._shutdownTimer);
|
|
91
|
+
this._shutdownTimer = null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this._bufferedEvents = [];
|
|
95
|
+
this._uninstallHandlers.forEach((cb) => cb());
|
|
96
|
+
this._uninstallHandlers = [];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async selfChecks(check = new SelfCheck()) {
|
|
100
|
+
const isConnected =
|
|
101
|
+
this._uninstallHandlers.length === 0 &&
|
|
102
|
+
this._bufferedEvents.length === 0 &&
|
|
103
|
+
!this._shutdownTimer;
|
|
104
|
+
|
|
105
|
+
if (isConnected) {
|
|
106
|
+
if (this.detectedErrors) {
|
|
107
|
+
check.warn('connected but events were lost during startup');
|
|
108
|
+
} else {
|
|
109
|
+
check.pass('connected');
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
if (this.detectedErrors) {
|
|
113
|
+
check.fail('dropping events while waiting for connection');
|
|
114
|
+
} else {
|
|
115
|
+
check.warn('waiting for connection (but no message loss yet)');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return check;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import LRU from './LRU';
|
|
13
|
+
|
|
14
|
+
/* Fixed length lookup cache. Allows expensive operations to be cached for later lookup. Once
|
|
15
|
+
* the cache limit is exceeded, least recently used values are removed.
|
|
16
|
+
*/
|
|
17
|
+
export default class FixedSizeCache {
|
|
18
|
+
/* @param {function} buildValue - used to build a new value from key in case of cache miss.
|
|
19
|
+
* @param {number} size - maximum elements stored in cache.
|
|
20
|
+
* @param {function} buildKey - [Optional] used to extract key from argument.
|
|
21
|
+
*/
|
|
22
|
+
constructor(buildValue, size, buildKey) {
|
|
23
|
+
this._buildValue = buildValue;
|
|
24
|
+
this._buildKey = buildKey;
|
|
25
|
+
this._maxKeySize = 1000;
|
|
26
|
+
|
|
27
|
+
// Statistics
|
|
28
|
+
this._hitCounter = 0;
|
|
29
|
+
this._missCounter = 0;
|
|
30
|
+
|
|
31
|
+
this.lru = new LRU(size);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* Try to retrieve the value associated with `key` from the cache. If it's
|
|
35
|
+
* not present, build it using `buildValue` and store it in the cache.
|
|
36
|
+
*
|
|
37
|
+
* This method always returns a value either from the LRU cache, or from a
|
|
38
|
+
* direct call to `buildValue`.
|
|
39
|
+
*/
|
|
40
|
+
get(argument) {
|
|
41
|
+
const key = this._buildKey ? this._buildKey(argument) : argument;
|
|
42
|
+
let value = this.lru.get(key);
|
|
43
|
+
|
|
44
|
+
if (value !== undefined) {
|
|
45
|
+
// Cache hit
|
|
46
|
+
this._hitCounter += 1;
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
// Cache miss
|
|
50
|
+
this._missCounter += 1;
|
|
51
|
+
|
|
52
|
+
// Compute value
|
|
53
|
+
value = this._buildValue(argument);
|
|
54
|
+
|
|
55
|
+
// if key is large, don't cache
|
|
56
|
+
if (!key || key.length > this._maxKeySize) {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.lru.set(key, value);
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
}
|