Revision 792fbe18
Added by koszko about 2 years ago
| content/repo_query.js | ||
|---|---|---|
| 9 | 9 |
/* |
| 10 | 10 |
* IMPORTS_START |
| 11 | 11 |
* IMPORT make_ajax_request |
| 12 |
* IMPORT observables |
|
| 13 |
* IMPORT TYPE_PREFIX |
|
| 14 |
* IMPORT parse_json_with_schema |
|
| 15 |
* IMPORT matchers |
|
| 12 | 16 |
* IMPORTS_END |
| 13 | 17 |
*/ |
| 14 | 18 |
|
| 15 |
var query_started = false; |
|
| 19 |
const paths = {
|
|
| 20 |
[TYPE_PREFIX.PAGE]: "/pattern", |
|
| 21 |
[TYPE_PREFIX.BAG]: "/bag", |
|
| 22 |
[TYPE_PREFIX.SCRIPT]: "/script", |
|
| 23 |
[TYPE_PREFIX.URL]: "/query" |
|
| 24 |
}; |
|
| 16 | 25 |
|
| 17 |
var url = undefined; |
|
| 18 |
var repos = undefined; |
|
| 19 |
var callback = undefined; |
|
| 26 |
const queried_items = new Map(); |
|
| 27 |
const observable = observables.make(); |
|
| 20 | 28 |
|
| 21 |
async function query(repo)
|
|
| 29 |
function repo_query(prefix, item, repo_urls)
|
|
| 22 | 30 |
{
|
| 23 |
const [repo_url, data] = repo;
|
|
| 31 |
const key = prefix + item;
|
|
| 24 | 32 |
|
| 25 |
let response = "Query failed";
|
|
| 26 |
const query_url = `${repo_url}/query?n=${encodeURIComponent(url)}`;
|
|
| 33 |
const results = queried_items.get(key) || {};
|
|
| 34 |
queried_items.set(key, results);
|
|
| 27 | 35 |
|
| 28 |
try {
|
|
| 29 |
let xhttp = await make_ajax_request("GET", query_url);
|
|
| 30 |
if (xhttp.status === 200) |
|
| 31 |
response = xhttp.responseText; |
|
| 32 |
console.log(xhttp); |
|
| 33 |
} catch (e) {
|
|
| 34 |
console.log(e); |
|
| 35 |
} |
|
| 36 |
for (const repo_url of repo_urls) |
|
| 37 |
perform_query_against(key, repo_url, results); |
|
| 38 |
} |
|
| 36 | 39 |
|
| 37 |
callback([repo_url, response]); |
|
| 40 |
const page_schema = {
|
|
| 41 |
pattern: matchers.nonempty_string, |
|
| 42 |
payload: ["optional", matchers.component, "default", undefined] |
|
| 43 |
}; |
|
| 44 |
const bag_schema = {
|
|
| 45 |
name: matchers.nonempty_string, |
|
| 46 |
components: ["optional", [matchers.component, "repeat"], "default", []] |
|
| 47 |
}; |
|
| 48 |
const script_schema = {
|
|
| 49 |
name: matchers.nonempty_string, |
|
| 50 |
location: matchers.nonempty_string, |
|
| 51 |
sha256: matchers.sha256, |
|
| 52 |
}; |
|
| 53 |
const search_result_schema = [page_schema, "repeat"]; |
|
| 54 |
|
|
| 55 |
const schemas = {
|
|
| 56 |
[TYPE_PREFIX.PAGE]: page_schema, |
|
| 57 |
[TYPE_PREFIX.BAG]: bag_schema, |
|
| 58 |
[TYPE_PREFIX.SCRIPT]: script_schema, |
|
| 59 |
[TYPE_PREFIX.URL]: search_result_schema |
|
| 38 | 60 |
} |
| 39 | 61 |
|
| 40 |
function start_query()
|
|
| 62 |
async function perform_query_against(key, repo_url, results)
|
|
| 41 | 63 |
{
|
| 42 |
if (query_started || !url || !repos || !callback)
|
|
| 64 |
if (results[repo_url] !== undefined)
|
|
| 43 | 65 |
return; |
| 44 | 66 |
|
| 45 |
query_started = true; |
|
| 67 |
const prefix = key[0]; |
|
| 68 |
const item = key.substring(1); |
|
| 69 |
const result = {state: "started"};
|
|
| 70 |
results[repo_url] = result; |
|
| 46 | 71 |
|
| 47 |
console.log(`about to query ${url} from ${repos}`);
|
|
| 72 |
const broadcast_msg = {prefix, item, results: {[repo_url]: result}};
|
|
| 73 |
observables.broadcast(observable, broadcast_msg); |
|
| 48 | 74 |
|
| 49 |
for (const repo of repos)
|
|
| 50 |
query(repo);
|
|
| 51 |
}
|
|
| 75 |
let state = "connection_error";
|
|
| 76 |
const query_url =
|
|
| 77 |
`${repo_url}${paths[prefix]}?n=${encodeURIComponent(item)}`;
|
|
| 52 | 78 |
|
| 53 |
function set_repo_query_url(_url) |
|
| 54 |
{
|
|
| 55 |
url = _url; |
|
| 79 |
try {
|
|
| 80 |
let xhttp = await make_ajax_request("GET", query_url);
|
|
| 81 |
if (xhttp.status === 200) {
|
|
| 82 |
state = "parse_error"; |
|
| 83 |
result.response = |
|
| 84 |
parse_json_with_schema(schemas[prefix], xhttp.responseText); |
|
| 85 |
state = "completed"; |
|
| 86 |
} |
|
| 87 |
} catch (e) {
|
|
| 88 |
console.log(e); |
|
| 89 |
} |
|
| 56 | 90 |
|
| 57 |
start_query(); |
|
| 91 |
result.state = state; |
|
| 92 |
observables.broadcast(observable, broadcast_msg); |
|
| 58 | 93 |
} |
| 59 | 94 |
|
| 60 |
function set_repo_query_repos(_repos)
|
|
| 95 |
function subscribe_repo_query_results(cb)
|
|
| 61 | 96 |
{
|
| 62 |
repos = _repos;
|
|
| 63 |
|
|
| 64 |
start_query();
|
|
| 97 |
observables.subscribe(observable, cb);
|
|
| 98 |
for (const [key, results] of queried_items.entries()) |
|
| 99 |
cb({prefix: key[0], item: key.substring(1), results});
|
|
| 65 | 100 |
} |
| 66 | 101 |
|
| 67 |
function set_repo_query_callback(_callback)
|
|
| 102 |
function unsubscribe_repo_query_results(cb)
|
|
| 68 | 103 |
{
|
| 69 |
callback = _callback; |
|
| 70 |
|
|
| 71 |
start_query(); |
|
| 104 |
observables.unsubscribe(observable, cb); |
|
| 72 | 105 |
} |
| 73 | 106 |
|
| 74 | 107 |
/* |
| 75 | 108 |
* EXPORTS_START |
| 76 |
* EXPORT set_repo_query_url
|
|
| 77 |
* EXPORT set_repo_query_repos
|
|
| 78 |
* EXPORT set_repo_query_callback
|
|
| 109 |
* EXPORT repo_query
|
|
| 110 |
* EXPORT subscribe_repo_query_results
|
|
| 111 |
* EXPORT unsubscribe_repo_query_results
|
|
| 79 | 112 |
* EXPORTS_END |
| 80 | 113 |
*/ |
Also available in: Unified diff
Facilitate installation of scripts from the repository
This commit includes: