-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathbackground.js
More file actions
More file actions
Latest commit
2393 lines (2001 loc) · 44 KB
/
Copy pathbackground.js
File metadata and controls
2393 lines (2001 loc) · 44 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
"use strict";
const api = globalThis.browser ?? globalThis.chrome;
const META_DB_NAME = "serp-domain-label-meta-v2";
const META_DB_VERSION = 1;
const META_STORE = "metadata";
const STATE_KEY = "state";
const IMPORT_KEY = "import-state";
const GENERATIONS_KEY = "known-generations";
const GENERATION_DB_PREFIX =
"serp-domain-label-generation-v2-";
const GENERATION_DB_VERSION = 1;
const LABEL_STORE = "labels";
const LOOKUP_REQUEST_LIMIT = 256;
const IMPORT_BATCH_LIMIT = 5000;
const LOOKUP_CACHE_LIMIT = 20000;
const MAX_LABEL_LENGTH = 63;
const MAX_SOURCE_COUNT = 100;
const MAX_REJECTION_SAMPLES = 20;
const DEFAULT_PROTECTED_LABELS = Object.freeze([
"www",
"www1",
"www2",
"www3",
"m"
]);
const DEFAULT_STATE = Object.freeze({
activeGeneration: null,
previousGeneration: null,
count: 0,
enabled: true,
revision: 0,
importedAt: null,
protectedLabels: [...DEFAULT_PROTECTED_LABELS]
});
let metaDatabasePromise = null;
let state = { ...DEFAULT_STATE };
let stateLoaded = false;
let initializationPromise = null;
const generationDatabasePromises = new Map();
class LruCache {
constructor(limit) {
this.limit = limit;
this.values = new Map();
}
get(key) {
if (!this.values.has(key)) {
return undefined;
}
const value = this.values.get(key);
this.values.delete(key);
this.values.set(key, value);
return value;
}
set(key, value) {
if (this.values.has(key)) {
this.values.delete(key);
}
this.values.set(key, value);
if (this.values.size > this.limit) {
const oldestKey =
this.values.keys().next().value;
this.values.delete(oldestKey);
}
}
clear() {
this.values.clear();
}
}
const lookupCache = new LruCache(
LOOKUP_CACHE_LIMIT
);
function requestAsPromise(request) {
return new Promise((resolve, reject) => {
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = () => {
reject(
request.error ??
new Error(
"An IndexedDB request failed."
)
);
};
});
}
function transactionAsPromise(transaction) {
return new Promise((resolve, reject) => {
transaction.oncomplete = () => {
resolve();
};
transaction.onerror = () => {
reject(
transaction.error ??
new Error(
"An IndexedDB transaction failed."
)
);
};
transaction.onabort = () => {
reject(
transaction.error ??
new Error(
"An IndexedDB transaction was aborted."
)
);
};
});
}
function openMetaDatabase() {
if (metaDatabasePromise) {
return metaDatabasePromise;
}
metaDatabasePromise = new Promise(
(resolve, reject) => {
const request = indexedDB.open(
META_DB_NAME,
META_DB_VERSION
);
request.onupgradeneeded = () => {
if (
!message ||
typeof message !== "object"
) {
return undefined;
}
switch (message.type) {
case "lookup":
return lookupHostnames(
message.hostnames
).then((blocked) => ({
blocked,
generation:
state.activeGeneration,
revision: state.revision
}));
case "get-status":
return getStatus();
case "set-settings":
return updateSettings(message);
case "begin-import":
return beginImport(message);
case "resume-import":
return resumeImport(message);
case "append-import-batch":
return appendImportBatch(
message
);
case "record-rejections":
return recordRejections(
message
);
case "complete-import-source":
return completeImportSource(
message
);
case "finish-import":
return finishImport(message);
case "abandon-import":
case "cancel-import":
return abandonImport(message);
case "clean-unused-databases":
return cleanUnusedDatabases();
case "delete-previous-generation":
return deletePreviousGeneration();
case "clear-data":
return clearAllData();
default:
return undefined;
}
}
);
ensureInitialized().catch((error) => {
console.error(
"Could not initialize SERP Domain Index v2:",
error
);
});