1
|
/**
|
2
|
* Myext storage through connection (server side)
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
*
|
6
|
* This code is dual-licensed under:
|
7
|
* - Asshole license 1.0,
|
8
|
* - GPLv3 or (at your option) any later version
|
9
|
*
|
10
|
* "dual-licensed" means you can choose the license you prefer.
|
11
|
*
|
12
|
* This code is released under a permissive license because I disapprove of
|
13
|
* copyright and wouldn't be willing to sue a violator. Despite not putting
|
14
|
* this code under copyleft (which is also kind of copyright), I do not want
|
15
|
* it to be made proprietary. Hence, the permissive alternative to GPL is the
|
16
|
* Asshole license 1.0 that allows me to call you an asshole if you use it.
|
17
|
* This means you're legally ok regardless of how you utilize this code but if
|
18
|
* you make it into something nonfree, you're an asshole.
|
19
|
*
|
20
|
* You should have received a copy of both GPLv3 and Asshole license 1.0
|
21
|
* together with this code. If not, please see:
|
22
|
* - https://www.gnu.org/licenses/gpl-3.0.en.html
|
23
|
* - https://koszko.org/asshole-license.txt
|
24
|
*/
|
25
|
|
26
|
"use strict";
|
27
|
|
28
|
(() => {
|
29
|
const listen_for_connection = window.listen_for_connection;
|
30
|
const get_storage = window.get_storage;
|
31
|
const TYPE_PREFIX = window.TYPE_PREFIX;
|
32
|
const CONNECTION_TYPE = window.CONNECTION_TYPE;
|
33
|
|
34
|
var storage;
|
35
|
|
36
|
async function handle_remote_call(port, message)
|
37
|
{
|
38
|
let [call_id, func, args] = message;
|
39
|
|
40
|
try {
|
41
|
let result = await Promise.resolve(storage[func](...args));
|
42
|
port.postMessage({call_id, result});
|
43
|
} catch (error) {
|
44
|
error = error + '';
|
45
|
port.postMessage({call_id, error});
|
46
|
}
|
47
|
}
|
48
|
|
49
|
function remove_storage_listener(cb) {
|
50
|
storage.remove_change_listener(cb);
|
51
|
}
|
52
|
|
53
|
function new_connection(port)
|
54
|
{
|
55
|
console.log("new remote storage connection!");
|
56
|
|
57
|
port.postMessage({
|
58
|
[TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT),
|
59
|
[TYPE_PREFIX.BAG] : storage.get_all(TYPE_PREFIX.BAG),
|
60
|
[TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE)
|
61
|
});
|
62
|
|
63
|
let handle_change = change => port.postMessage(change);
|
64
|
|
65
|
storage.add_change_listener(handle_change);
|
66
|
|
67
|
port.onMessage.addListener(m => handle_remote_call(port, m));
|
68
|
port.onDisconnect.addListener(() =>
|
69
|
remove_storage_listener(handle_change));
|
70
|
}
|
71
|
|
72
|
async function start()
|
73
|
{
|
74
|
storage = await get_storage();
|
75
|
|
76
|
listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection);
|
77
|
}
|
78
|
|
79
|
window.start_storage_server = start;
|
80
|
})();
|