From 72553a2d8b5fa094a5edd5e6ec15b5125a052016 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 16 Feb 2022 00:55:04 +0100 Subject: assume and use "$schema" properties in item definitions --- html/install.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'html/install.js') diff --git a/html/install.js b/html/install.js index 2a72662..7b3f3fe 100644 --- a/html/install.js +++ b/html/install.js @@ -47,8 +47,7 @@ #FROM common/browser.js IMPORT browser #FROM html/DOM_helpers.js IMPORT clone_template, Showable -#FROM common/entities.js IMPORT item_id_string, version_string, get_files, \ - is_valid_version +#FROM common/entities.js IMPORT item_id_string, version_string, get_files #FROM common/misc.js IMPORT sha256_async AS compute_sha256 const coll = new Intl.Collator(); @@ -114,6 +113,19 @@ async function init_work() { new Promise((...cbs) => [work.resolve_cb, work.reject_cb] = cbs)]; } +function _make_url_reg(item_type) { + return new RegExp( + `^https://hydrilla\\.koszko\\.org/schemas/api_${item_type}_description-1\\.([1-9][0-9]*\\.)*schema\\.json$` + ); +} + +const _regexes = {}; + +function item_schema_url_regex(item_type) { + _regexes[item_type] = _regexes[item_type] || _make_url_reg(item_type); + return _regexes[item_type]; +} + function InstallView(tab_id, on_view_show, on_view_hide) { Showable.call(this, on_view_show, on_view_hide); @@ -191,26 +203,19 @@ function InstallView(tab_id, on_view_show, on_view_hide) { "Repository's response is not valid JSON :("); } - if (!is_valid_version(response.json.api_schema_version)) { - var bad_api_ver = ""; - } else if (response.json.api_schema_version > [1]) { - var bad_api_ver = - ` (${version_string(response.json.api_schema_version)})`; - } else { - var bad_api_ver = false; - } + const captype = item_type[0].toUpperCase() + item_type.substring(1); + const reg = item_schema_url_regex(item_type); - if (bad_api_ver !== false) { - const captype = item_type[0].toUpperCase() + item_type.substring(1); - const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version${bad_api_ver}. You might need to update Haketilo.`; + if (!response.json["$schema"]) { + const msg = `${captype} ${item_id_string(id, ver)} was served using a nonconforming response format.`; + return work.err(null, msg); + } else if (!reg.test(response.json["$schema"])) { + const msg = `${captype} ${item_id_string(id, ver)} was served using unsupported Hydrilla API version. You might need to update Haketilo.`; return work.err(null, msg); } /* TODO: JSON schema validation should be added here. */ - delete response.json.api_schema_version; - delete response.json.api_schema_revision; - const scripts = item_type === "resource" && response.json.scripts; const files = response.json.source_copyright.concat(scripts || []); -- cgit v1.2.3