aboutsummaryrefslogtreecommitdiff
path: root/html
diff options
context:
space:
mode:
Diffstat (limited to 'html')
-rw-r--r--html/install.js88
-rw-r--r--html/repo_query.js30
-rw-r--r--html/repo_query_cacher_client.js85
3 files changed, 157 insertions, 46 deletions
diff --git a/html/install.js b/html/install.js
index fddee5d..82df661 100644
--- a/html/install.js
+++ b/html/install.js
@@ -50,6 +50,9 @@
#FROM common/entities.js IMPORT item_id_string, version_string, get_files
#FROM common/misc.js IMPORT sha256_async AS compute_sha256
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
+#FROM common/entities.js IMPORT haketilo_schema_name_regex
+
+#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch
const coll = new Intl.Collator();
@@ -104,6 +107,9 @@ async function init_work() {
};
work.err = function (error, user_message) {
+ if (!this.is_ok)
+ return;
+
if (error)
console.error("Haketilo:", error);
work.is_ok = false;
@@ -171,54 +177,71 @@ function InstallView(tab_id, on_view_show, on_view_hide) {
const url = ver ?
`${this.repo_url}${item_type}/${id}/${ver.join(".")}` :
`${this.repo_url}${item_type}/${id}.json`;
- const response =
- await browser.tabs.sendMessage(tab_id, ["repo_query", url]);
- if (!work.is_ok)
- return;
- if ("error" in response) {
- return work.err(response.error,
- "Failure to communicate with repository :(");
+
+ try {
+ var response = await indirect_fetch(tab_id, url);
+ } catch(e) {
+ return work.err(e, "Failure to communicate with repository :(");
}
+ if (!work.is_ok)
+ return;
+
if (!response.ok) {
return work.err(null,
`Repository sent HTTP code ${response.status} :(`);
}
- if ("error_json" in response) {
- return work.err(response.error_json,
- "Repository's response is not valid JSON :(");
+ try {
+ var json = await response.json();
+ } catch(e) {
+ return work.err(e, "Repository's response is not valid JSON :(");
}
+ if (!work.is_ok)
+ return;
+
const captype = item_type[0].toUpperCase() + item_type.substring(1);
- const $id =
- `https://hydrilla.koszko.org/schemas/api_${item_type}_description-1.0.1.schema.json`;
- const schema = haketilo_schemas[$id];
- const result = haketilo_validator.validate(response.json, schema);
- if (result.errors.length > 0) {
- const reg = new RegExp(schema.allOf[2].properties.$schema.pattern);
- if (response.json.$schema && !reg.test(response.json.$schema)) {
+ const nonconforming_format_error_msg =
+ `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`;
+
+ try {
+ const match = haketilo_schema_name_regex.exec(json.$schema);
+ var major_schema_version = match.groups.major;
+
+ if (!["1", "2"].includes(major_schema_version)) {
const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`;
- return work.err(result.errors, msg);
+ return work.err(null, msg);
}
-
- const msg = `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`;
- return work.err(result.errors, msg);
+ } catch(e) {
+ return work.err(e, nonconforming_format_error_msg);
}
- const scripts = item_type === "resource" && response.json.scripts;
- const files = response.json.source_copyright.concat(scripts || []);
+ const schema_name = `api_${item_type}_description-${major_schema_version}.schema.json`;
+
+ const schema = haketilo_schemas[schema_name];
+ const result = haketilo_validator.validate(json, schema);
+ if (result.errors.length > 0)
+ return work.err(result.errors, nonconforming_format_error_msg);
+
+ const scripts = item_type === "resource" && json.scripts;
+ const files = json.source_copyright.concat(scripts || []);
if (item_type === "mapping") {
- for (const res_ref of Object.values(response.json.payloads || {}))
+ for (const res_ref of Object.values(json.payloads || {}))
process_item(work, "resource", res_ref.identifier);
} else {
- for (const res_ref of (response.json.dependencies || []))
+ for (const res_ref of (json.dependencies || []))
process_item(work, "resource", res_ref.identifier);
}
+ if (major_schema_version >= 2) {
+ for (const map_ref of (json.required_mappings || []))
+ process_item(work, "mapping", map_ref.identifier);
+ }
+
/*
* At this point we already have JSON definition of the item and we
* triggered processing of its dependencies. We now have to verify if
@@ -234,8 +257,8 @@ function InstallView(tab_id, on_view_show, on_view_hide) {
const msg = "Error accessing Haketilo's internal database :(";
return work.err(e, msg);
}
- if (!db_def || db_def.version < response.json.version)
- work.result.push({def: response.json, db_def});
+ if (!db_def || db_def.version < json.version)
+ work.result.push({def: json, db_def});
if (--work.waiting === 0)
work.resolve_cb(work.result);
@@ -320,14 +343,9 @@ function InstallView(tab_id, on_view_show, on_view_hide) {
return work.err(null, msg);
}
- try {
- var text = await response.text();
- if (!work.is_ok)
- return;
- } catch(e) {
- const msg = "Repository's response is not valid text :(";
- return work.err(e, msg);
- }
+ const text = await response.text();
+ if (!work.is_ok)
+ return;
const digest = await compute_sha256(text);
if (!work.is_ok)
diff --git a/html/repo_query.js b/html/repo_query.js
index 601a0aa..97f60ac 100644
--- a/html/repo_query.js
+++ b/html/repo_query.js
@@ -49,6 +49,8 @@
#FROM html/install.js IMPORT InstallView
#FROM common/jsonschema.js IMPORT haketilo_validator, haketilo_schemas
+#FROM html/repo_query_cacher_client.js IMPORT indirect_fetch
+
const coll = new Intl.Collator();
function ResultEntry(repo_entry, mapping_ref) {
@@ -76,36 +78,42 @@ function RepoEntry(query_view, repo_url) {
this.repo_url_label.innerText = repo_url;
- const query_results = async () => {
- const msg = [
- "repo_query",
- `${repo_url}query?url=${encodeURIComponent(query_view.url)}`
- ];
- const response = await browser.tabs.sendMessage(query_view.tab_id, msg);
+ const encoded_queried_url = encodeURIComponent(query_view.url);
+ const query_url = `${repo_url}query?url=${encoded_queried_url}`;
- if ("error" in response)
+ const query_results = async () => {
+ try {
+ var response = await indirect_fetch(query_view.tab_id, query_url);
+ } catch(e) {
+ console.error("Haketilo:", e);
throw "Failure to communicate with repository :(";
+ }
if (!response.ok)
throw `Repository sent HTTP code ${response.status} :(`;
- if ("error_json" in response)
+
+ try {
+ var json = await response.json();
+ } catch(e) {
+ console.error("Haketilo:", e);
throw "Repository's response is not valid JSON :(";
+ }
const $id =
`https://hydrilla.koszko.org/schemas/api_query_result-1.0.1.schema.json`;
const schema = haketilo_schemas[$id];
- const result = haketilo_validator.validate(response.json, schema);
+ const result = haketilo_validator.validate(json, schema);
if (result.errors.length > 0) {
console.error("Haketilo:", result.errors);
const reg = new RegExp(schema.properties.$schema.pattern);
- if (response.json.$schema && !reg.test(response.json.$schema))
+ if (json.$schema && !reg.test(json.$schema))
throw "Results were served using unsupported Hydrilla API version. You might need to update Haketilo.";
throw "Results were served using a nonconforming response format.";
}
- return response.json.mappings;
+ return json.mappings;
}
const populate_results = async () => {
diff --git a/html/repo_query_cacher_client.js b/html/repo_query_cacher_client.js
new file mode 100644
index 0000000..b7f49e9
--- /dev/null
+++ b/html/repo_query_cacher_client.js
@@ -0,0 +1,85 @@
+/**
+ * This file is part of Haketilo.
+ *
+ * Function: Making requests to remote repositories through a response cache in
+ * operating in the content script of a browser tab.
+ *
+ * Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * As additional permission under GNU GPL version 3 section 7, you
+ * may distribute forms of that code without the copy of the GNU
+ * GPL normally required by section 4, provided you include this
+ * license notice and, in case of non-source distribution, a URL
+ * through which recipients can access the Corresponding Source.
+ * If you modify file(s) with this exception, you may extend this
+ * exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this
+ * exception statement from your version.
+ *
+ * As a special exception to the GPL, any HTML file which merely
+ * makes function calls to this code, and for that purpose
+ * includes it by reference shall be deemed a separate work for
+ * copyright law purposes. If you modify this code, you may extend
+ * this exception to your version of the code, but you are not
+ * obligated to do so. If you do not wish to do so, delete this
+ * exception statement from your version.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
+ * license. Although I request that you do not make use of this code in a
+ * proprietary program, I am not going to enforce this in court.
+ */
+
+#FROM common/browser.js IMPORT browser
+
+/*
+ * This is a complementary function to error_data_jsonifiable() in
+ * common/misc.js.
+ */
+function error_from_jsonifiable_data(jsonifiable) {
+ const arg_props = ["message", "fileName", "lineNumber"];
+ return new window[jsonifiable.name](...arg_props.map(p => jsonifiable[p]));
+}
+
+/*
+ * Make it possible to recover a Response object. This is a complementary
+ * function to response_data_jsonifiable() in background/CORS_bypass_server.js.
+ */
+function response_from_jsonifiable_data(jsonifiable) {
+ const body = jsonifiable.body, body_len = body.length / 2;
+ const body_buf = new Uint8Array(body_len);
+
+ for (let i = 0; i < body_len; i++)
+ body_buf[i] = parseInt(`0x${body.substring(i * 2, i * 2 + 2)}`);
+
+ const init = {
+ status: jsonifiable.status,
+ statusText: jsonifiable.statusText,
+ headers: new Headers(jsonifiable.headers)
+ };
+
+ return new Response(body_buf, init);
+}
+
+async function indirect_fetch(tab_id, url) {
+ const msg = ["repo_query", url];
+ const jsonifiable = await browser.tabs.sendMessage(tab_id, msg);
+
+ if ("error" in jsonifiable)
+ throw error_from_jsonifiable_data(jsonifiable);
+
+ return response_from_jsonifiable_data(jsonifiable);
+}
+#EXPORT indirect_fetch