From bbc9fae4291d0c2cb3976d158ecd20e0bd2a8ea0 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 15 Mar 2022 10:12:06 +0100 Subject: serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage --- html/repo_query.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'html/repo_query.js') 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 () => { -- cgit v1.2.3