-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProxyManager.js
52 lines (48 loc) · 1.31 KB
/
ProxyManager.js
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
export default class {
static swSupported = false;
/**
* Manage many proxies safely
* @constructor
* @param {boolean} [cache=true] Cache the proxy; disabling this may slow down the proxy
*/
constructor(cache = true) {
if (!this.swSupported)
!("serviceWorker" in navigator)
? console.error("Proxies are not supported on your browser")
: (this.swSupported = true);
}
/**
* Spawn a new proxy
* @param {string} - The path to the service worker
* @param {string} - The prefix where the proxy url's are handled
* @param {boolean} [legacy=true] Do not register as a module script
*/
add(path, prefix, legacy = true) {
if (!this.swSupported) return;
navigator.serviceWorker
.register(path, {
scope: prefix,
type: legacy ? "classic" : "module",
// Don't cache http requests
updateViaCache: this.cache ? "all" : "none"
})
// Update the service worker
.then(reg => reg.update());
}
/**
* Update the config
* @param {string} - The identifier used in the message handler
* @param {object} - The new config
*/
update(id, config) {
if (!this.swSupported) return;
if (typeof object !== "object") {
console.error(`Tried to update invalid config for ${dbName}`);
return;
}
navigator.serviceWorker.controller.postMessage({
id: id,
...config
});
}
}