1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Getting available content for site from remote repositories.
|
5
|
*
|
6
|
* Copyright (C) 2021 Wojtek Kosior
|
7
|
*
|
8
|
* This program is free software: you can redistribute it and/or modify
|
9
|
* it under the terms of the GNU General Public License as published by
|
10
|
* the Free Software Foundation, either version 3 of the License, or
|
11
|
* (at your option) any later version.
|
12
|
*
|
13
|
* This program is distributed in the hope that it will be useful,
|
14
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
* GNU General Public License for more details.
|
17
|
*
|
18
|
* As additional permission under GNU GPL version 3 section 7, you
|
19
|
* may distribute forms of that code without the copy of the GNU
|
20
|
* GPL normally required by section 4, provided you include this
|
21
|
* license notice and, in case of non-source distribution, a URL
|
22
|
* through which recipients can access the Corresponding Source.
|
23
|
* If you modify file(s) with this exception, you may extend this
|
24
|
* exception to your version of the file(s), but you are not
|
25
|
* obligated to do so. If you do not wish to do so, delete this
|
26
|
* exception statement from your version.
|
27
|
*
|
28
|
* As a special exception to the GPL, any HTML file which merely
|
29
|
* makes function calls to this code, and for that purpose
|
30
|
* includes it by reference shall be deemed a separate work for
|
31
|
* copyright law purposes. If you modify this code, you may extend
|
32
|
* this exception to your version of the code, but you are not
|
33
|
* obligated to do so. If you do not wish to do so, delete this
|
34
|
* exception statement from your version.
|
35
|
*
|
36
|
* You should have received a copy of the GNU General Public License
|
37
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
38
|
*
|
39
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
40
|
* license. Although I request that you do not make use this code in a
|
41
|
* proprietary program, I am not going to enforce this in court.
|
42
|
*/
|
43
|
|
44
|
/*
|
45
|
* IMPORTS_START
|
46
|
* IMPORT make_ajax_request
|
47
|
* IMPORT observables
|
48
|
* IMPORT TYPE_PREFIX
|
49
|
* IMPORT parse_json_with_schema
|
50
|
* IMPORT matchers
|
51
|
* IMPORTS_END
|
52
|
*/
|
53
|
|
54
|
const paths = {
|
55
|
[TYPE_PREFIX.PAGE]: "/pattern",
|
56
|
[TYPE_PREFIX.BAG]: "/bag",
|
57
|
[TYPE_PREFIX.SCRIPT]: "/script",
|
58
|
[TYPE_PREFIX.URL]: "/query"
|
59
|
};
|
60
|
|
61
|
const queried_items = new Map();
|
62
|
const observable = observables.make();
|
63
|
|
64
|
function repo_query(prefix, item, repo_urls)
|
65
|
{
|
66
|
const key = prefix + item;
|
67
|
|
68
|
const results = queried_items.get(key) || {};
|
69
|
queried_items.set(key, results);
|
70
|
|
71
|
for (const repo_url of repo_urls)
|
72
|
perform_query_against(key, repo_url, results);
|
73
|
}
|
74
|
|
75
|
const page_schema = {
|
76
|
pattern: matchers.nonempty_string,
|
77
|
payload: ["optional", matchers.component, "default", undefined]
|
78
|
};
|
79
|
const bag_schema = {
|
80
|
name: matchers.nonempty_string,
|
81
|
components: ["optional", [matchers.component, "repeat"], "default", []]
|
82
|
};
|
83
|
const script_schema = {
|
84
|
name: matchers.nonempty_string,
|
85
|
location: matchers.nonempty_string,
|
86
|
sha256: matchers.sha256,
|
87
|
};
|
88
|
const search_result_schema = [page_schema, "repeat"];
|
89
|
|
90
|
const schemas = {
|
91
|
[TYPE_PREFIX.PAGE]: page_schema,
|
92
|
[TYPE_PREFIX.BAG]: bag_schema,
|
93
|
[TYPE_PREFIX.SCRIPT]: script_schema,
|
94
|
[TYPE_PREFIX.URL]: search_result_schema
|
95
|
}
|
96
|
|
97
|
async function perform_query_against(key, repo_url, results)
|
98
|
{
|
99
|
if (results[repo_url] !== undefined)
|
100
|
return;
|
101
|
|
102
|
const prefix = key[0];
|
103
|
const item = key.substring(1);
|
104
|
const result = {state: "started"};
|
105
|
results[repo_url] = result;
|
106
|
|
107
|
const broadcast_msg = {prefix, item, results: {[repo_url]: result}};
|
108
|
observables.broadcast(observable, broadcast_msg);
|
109
|
|
110
|
let state = "connection_error";
|
111
|
const query_url =
|
112
|
`${repo_url}${paths[prefix]}?n=${encodeURIComponent(item)}`;
|
113
|
|
114
|
try {
|
115
|
let xhttp = await make_ajax_request("GET", query_url);
|
116
|
if (xhttp.status === 200) {
|
117
|
state = "parse_error";
|
118
|
result.response =
|
119
|
parse_json_with_schema(schemas[prefix], xhttp.responseText);
|
120
|
state = "completed";
|
121
|
}
|
122
|
} catch (e) {
|
123
|
console.log(e);
|
124
|
}
|
125
|
|
126
|
result.state = state;
|
127
|
observables.broadcast(observable, broadcast_msg);
|
128
|
}
|
129
|
|
130
|
function subscribe_repo_query_results(cb)
|
131
|
{
|
132
|
observables.subscribe(observable, cb);
|
133
|
for (const [key, results] of queried_items.entries())
|
134
|
cb({prefix: key[0], item: key.substring(1), results});
|
135
|
}
|
136
|
|
137
|
function unsubscribe_repo_query_results(cb)
|
138
|
{
|
139
|
observables.unsubscribe(observable, cb);
|
140
|
}
|
141
|
|
142
|
/*
|
143
|
* EXPORTS_START
|
144
|
* EXPORT repo_query
|
145
|
* EXPORT subscribe_repo_query_results
|
146
|
* EXPORT unsubscribe_repo_query_results
|
147
|
* EXPORTS_END
|
148
|
*/
|