diff options
Diffstat (limited to 'html/install.js')
-rw-r--r-- | html/install.js | 88 |
1 files changed, 53 insertions, 35 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) |