From 1c65dd5ca24052ccf9a92939eecd0966c9635c50 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 9 Feb 2022 17:09:13 +0100 Subject: adapt to changes in file path format From now on we assume Hydrilla serves file contents at 'file/sha256/' instead of 'file/sha256-'. With this commit we also stop using the "hash_key" property internally. --- background/indexeddb_files_server.js | 2 +- common/indexeddb.js | 49 +++++++++++++++++--------------- default_settings.json | 8 ++++-- html/install.js | 30 +++++++++---------- html/item_preview.js | 2 +- html/payload_create.js | 11 +++---- test/unit/test_indexeddb.py | 48 +++++++++++++++++-------------- test/unit/test_indexeddb_files_server.py | 6 ++-- test/unit/test_item_list.py | 8 ++++-- test/unit/test_item_preview.py | 2 +- test/unit/test_payload_create.py | 4 ++- test/unit/utils.py | 19 ++++++------- test/world_wide_library.py | 2 +- 13 files changed, 103 insertions(+), 88 deletions(-) diff --git a/background/indexeddb_files_server.js b/background/indexeddb_files_server.js index cf78ca6..0c3f7d8 100644 --- a/background/indexeddb_files_server.js +++ b/background/indexeddb_files_server.js @@ -59,7 +59,7 @@ async function get_resource_files(getting, id) { getting.defs_by_res_id.set(id, definition); const file_proms = (definition.scripts || []) - .map(s => haketilodb.idb_get(getting.tx, "files", s.hash_key)); + .map(s => haketilodb.idb_get(getting.tx, "files", s.sha256)); const deps_proms = (definition.dependencies || []) .map(dep_id => get_resource_files(getting, dep_id)); diff --git a/common/indexeddb.js b/common/indexeddb.js index bdf71e5..371d491 100644 --- a/common/indexeddb.js +++ b/common/indexeddb.js @@ -3,7 +3,7 @@ * * Function: Facilitate use of IndexedDB within Haketilo. * - * Copyright (C) 2021 Wojtek Kosior + * Copyright (C) 2021, 2022 Wojtek Kosior * * 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 @@ -59,8 +59,8 @@ const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i]; const version_nr = ver => ver.slice(0, 3).reduce(nr_reductor, [2, 0])[1]; const stores = [ - ["files", {keyPath: "hash_key"}], - ["file_uses", {keyPath: "hash_key"}], + ["files", {keyPath: "sha256"}], + ["file_uses", {keyPath: "sha256"}], ["resource", {keyPath: "identifier"}], ["mapping", {keyPath: "identifier"}], ["settings", {keyPath: "name"}], @@ -110,7 +110,7 @@ async function perform_upgrade(event) { for (const [store_name, key_mode] of stores) store = opened_db.createObjectStore(store_name, key_mode); - const ctx = make_context(store.transaction, initial_data.files); + const ctx = make_context(store.transaction, initial_data.file); await _save_items(initial_data.resources, initial_data.mappings, ctx); return opened_db; @@ -175,9 +175,10 @@ function make_context(transaction, files) /* * item_store_names should be an array with either string "mapping", string - * "resource" or both. files should be an object with values being contents of - * files that are to be possibly saved in this transaction and keys of the form - * `sha256-`. + * "resource" or both. files should be an object with an "sha256" property whose + * values will be yet another object with values being contents of files that + * are to be possibly saved in this transaction and keys being hexadecimal + * representations of files' SHA256 sums. * * Returned is a context object wrapping the transaction and handling the * counting of file references in IndexedDB. @@ -192,16 +193,16 @@ async function start_items_transaction(item_store_names, files) async function incr_file_uses(context, file_ref, by=1) { - const hash_key = file_ref.hash_key; - let uses = context.file_uses[hash_key]; + const sha256 = file_ref.sha256; + let uses = context.file_uses[sha256]; if (uses === undefined) { - uses = await idb_get(context.transaction, "file_uses", hash_key); + uses = await idb_get(context.transaction, "file_uses", sha256); if (uses) [uses.new, uses.initial] = [false, uses.uses]; else - uses = {hash_key, uses: 0, new: true, initial: 0}; + uses = {sha256, uses: 0, new: true, initial: 0}; - context.file_uses[hash_key] = uses; + context.file_uses[sha256] = uses; } uses.uses = uses.uses + by; @@ -213,19 +214,19 @@ async function finalize_transaction(context) { for (const uses of Object.values(context.file_uses)) { if (uses.uses < 0) - console.error("internal error: uses < 0 for file " + uses.hash_key); + console.error("internal error: uses < 0 for file " + uses.sha256); const is_new = uses.new; const initial_uses = uses.initial; - const hash_key = uses.hash_key; + const sha256 = uses.sha256; delete uses.new; delete uses.initial; if (uses.uses < 1) { if (!is_new) { - idb_del(context.transaction, "file_uses", hash_key); - idb_del(context.transaction, "files", hash_key); + idb_del(context.transaction, "file_uses", sha256); + idb_del(context.transaction, "files", sha256); } continue; @@ -239,13 +240,13 @@ async function finalize_transaction(context) if (initial_uses > 0) continue; - const file = context.files[hash_key]; + const file = context.files.sha256[sha256]; if (file === undefined) { context.transaction.abort(); - throw "file not present: " + hash_key; + throw "file not present: " + sha256; } - idb_put(context.transaction, "files", {hash_key, contents: file}); + idb_put(context.transaction, "files", {sha256, contents: file}); } return context.result; @@ -283,16 +284,18 @@ async function finalize_transaction(context) * } * }, * }, - * files: { - * "sha256-f9444510dc7403e41049deb133f6892aa6a63c05591b2b59e4ee5b234d7bbd99": "console.log(\"hello\");\n", - * "sha256-b857cd521cc82fff30f0d316deba38b980d66db29a5388eb6004579cf743c6fd": "console.log(\"bye\");" + * file: { + * sha256: { + * "f9444510dc7403e41049deb133f6892aa6a63c05591b2b59e4ee5b234d7bbd99": "console.log(\"hello\");\n", + * "b857cd521cc82fff30f0d316deba38b980d66db29a5388eb6004579cf743c6fd": "console.log(\"bye\");" + * } * } * } */ async function save_items(data) { const item_store_names = ["resource", "mapping"]; - const context = await start_items_transaction(item_store_names, data.files); + const context = await start_items_transaction(item_store_names, data.file); return _save_items(data.resources, data.mappings, context); } diff --git a/default_settings.json b/default_settings.json index d66ced1..3ce6856 100644 --- a/default_settings.json +++ b/default_settings.json @@ -50,7 +50,7 @@ "source_name": "haketilo-default-settings", "source_copyright": [{ "file": "CC0-1.0.txt", - "hash_key": "sha256-a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499" + "sha256": "a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499" }], "type": "mapping", "identifier": "haketilo-demo-message", @@ -67,7 +67,9 @@ } }, "files": { - "sha256-a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499": "Creative Commons Legal Code\n\nCC0 1.0 Universal\n\n CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE\n LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN\n ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS\n INFORMATION ON AN \"AS-IS\" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES\n REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS\n PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM\n THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED\n HEREUNDER.\n\nStatement of Purpose\n\nThe laws of most jurisdictions throughout the world automatically confer\nexclusive Copyright and Related Rights (defined below) upon the creator\nand subsequent owner(s) (each and all, an \"owner\") of an original work of\nauthorship and/or a database (each, a \"Work\").\n\nCertain owners wish to permanently relinquish those rights to a Work for\nthe purpose of contributing to a commons of creative, cultural and\nscientific works (\"Commons\") that the public can reliably and without fear\nof later claims of infringement build upon, modify, incorporate in other\nworks, reuse and redistribute as freely as possible in any form whatsoever\nand for any purposes, including without limitation commercial purposes.\nThese owners may contribute to the Commons to promote the ideal of a free\nculture and the further production of creative, cultural and scientific\nworks, or to gain reputation or greater distribution for their Work in\npart through the use and efforts of others.\n\nFor these and/or other purposes and motivations, and without any\nexpectation of additional consideration or compensation, the person\nassociating CC0 with a Work (the \"Affirmer\"), to the extent that he or she\nis an owner of Copyright and Related Rights in the Work, voluntarily\nelects to apply CC0 to the Work and publicly distribute the Work under its\nterms, with knowledge of his or her Copyright and Related Rights in the\nWork and the meaning and intended legal effect of CC0 on those rights.\n\n1. Copyright and Related Rights. A Work made available under CC0 may be\nprotected by copyright and related or neighboring rights (\"Copyright and\nRelated Rights\"). Copyright and Related Rights include, but are not\nlimited to, the following:\n\n i. the right to reproduce, adapt, distribute, perform, display,\n communicate, and translate a Work;\n ii. moral rights retained by the original author(s) and/or performer(s);\niii. publicity and privacy rights pertaining to a person's image or\n likeness depicted in a Work;\n iv. rights protecting against unfair competition in regards to a Work,\n subject to the limitations in paragraph 4(a), below;\n v. rights protecting the extraction, dissemination, use and reuse of data\n in a Work;\n vi. database rights (such as those arising under Directive 96/9/EC of the\n European Parliament and of the Council of 11 March 1996 on the legal\n protection of databases, and under any national implementation\n thereof, including any amended or successor version of such\n directive); and\nvii. other similar, equivalent or corresponding rights throughout the\n world based on applicable law or treaty, and any national\n implementations thereof.\n\n2. Waiver. To the greatest extent permitted by, but not in contravention\nof, applicable law, Affirmer hereby overtly, fully, permanently,\nirrevocably and unconditionally waives, abandons, and surrenders all of\nAffirmer's Copyright and Related Rights and associated claims and causes\nof action, whether now known or unknown (including existing as well as\nfuture claims and causes of action), in the Work (i) in all territories\nworldwide, (ii) for the maximum duration provided by applicable law or\ntreaty (including future time extensions), (iii) in any current or future\nmedium and for any number of copies, and (iv) for any purpose whatsoever,\nincluding without limitation commercial, advertising or promotional\npurposes (the \"Waiver\"). Affirmer makes the Waiver for the benefit of each\nmember of the public at large and to the detriment of Affirmer's heirs and\nsuccessors, fully intending that such Waiver shall not be subject to\nrevocation, rescission, cancellation, termination, or any other legal or\nequitable action to disrupt the quiet enjoyment of the Work by the public\nas contemplated by Affirmer's express Statement of Purpose.\n\n3. Public License Fallback. Should any part of the Waiver for any reason\nbe judged legally invalid or ineffective under applicable law, then the\nWaiver shall be preserved to the maximum extent permitted taking into\naccount Affirmer's express Statement of Purpose. In addition, to the\nextent the Waiver is so judged Affirmer hereby grants to each affected\nperson a royalty-free, non transferable, non sublicensable, non exclusive,\nirrevocable and unconditional license to exercise Affirmer's Copyright and\nRelated Rights in the Work (i) in all territories worldwide, (ii) for the\nmaximum duration provided by applicable law or treaty (including future\ntime extensions), (iii) in any current or future medium and for any number\nof copies, and (iv) for any purpose whatsoever, including without\nlimitation commercial, advertising or promotional purposes (the\n\"License\"). The License shall be deemed effective as of the date CC0 was\napplied by Affirmer to the Work. Should any part of the License for any\nreason be judged legally invalid or ineffective under applicable law, such\npartial invalidity or ineffectiveness shall not invalidate the remainder\nof the License, and in such case Affirmer hereby affirms that he or she\nwill not (i) exercise any of his or her remaining Copyright and Related\nRights in the Work or (ii) assert any associated claims and causes of\naction with respect to the Work, in either case contrary to Affirmer's\nexpress Statement of Purpose.\n\n4. Limitations and Disclaimers.\n\n a. No trademark or patent rights held by Affirmer are waived, abandoned,\n surrendered, licensed or otherwise affected by this document.\n b. Affirmer offers the Work as-is and makes no representations or\n warranties of any kind concerning the Work, express, implied,\n statutory or otherwise, including without limitation warranties of\n title, merchantability, fitness for a particular purpose, non\n infringement, or the absence of latent or other defects, accuracy, or\n the present or absence of errors, whether or not discoverable, all to\n the greatest extent permissible under applicable law.\n c. Affirmer disclaims responsibility for clearing rights of other persons\n that may apply to the Work or any use thereof, including without\n limitation any person's Copyright and Related Rights in the Work.\n Further, Affirmer disclaims responsibility for obtaining any necessary\n consents, permissions or other rights required for any use of the\n Work.\n d. Affirmer understands and acknowledges that Creative Commons is not a\n party to this document and has no duty or obligation with respect to\n this CC0 or use of the Work.\n", - "sha256-f1d9dc9f2f4edaaebd9167eb8b79a6da6434313df47921aa4ea8ae278f7739e3": "/**\n * Haketilo demo script.\n *\n * Copyright (C) 2021 Wojtek Kosior\n * Available under the terms of Creative Commons Zero\n * \n */\n\n{\n const banner = document.createElement(\"h2\");\n \n banner.textContent = \"Hoooray! Haketilo works :D\";\n \n banner.setAttribute(\"style\", `\\\n margin: 1em; \\\n border-radius: 1em 0px; \\\n background-color: #474; \\\n padding: 10px 20px; \\\n color: #eee; \\\n box-shadow: 0 6px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19); \\\n display: inline-block;\\\n `);\n \n document.body.prepend(banner);\n}\n" + "sha256": { + "a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499": "Creative Commons Legal Code\n\nCC0 1.0 Universal\n\n CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE\n LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN\n ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS\n INFORMATION ON AN \"AS-IS\" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES\n REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS\n PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM\n THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED\n HEREUNDER.\n\nStatement of Purpose\n\nThe laws of most jurisdictions throughout the world automatically confer\nexclusive Copyright and Related Rights (defined below) upon the creator\nand subsequent owner(s) (each and all, an \"owner\") of an original work of\nauthorship and/or a database (each, a \"Work\").\n\nCertain owners wish to permanently relinquish those rights to a Work for\nthe purpose of contributing to a commons of creative, cultural and\nscientific works (\"Commons\") that the public can reliably and without fear\nof later claims of infringement build upon, modify, incorporate in other\nworks, reuse and redistribute as freely as possible in any form whatsoever\nand for any purposes, including without limitation commercial purposes.\nThese owners may contribute to the Commons to promote the ideal of a free\nculture and the further production of creative, cultural and scientific\nworks, or to gain reputation or greater distribution for their Work in\npart through the use and efforts of others.\n\nFor these and/or other purposes and motivations, and without any\nexpectation of additional consideration or compensation, the person\nassociating CC0 with a Work (the \"Affirmer\"), to the extent that he or she\nis an owner of Copyright and Related Rights in the Work, voluntarily\nelects to apply CC0 to the Work and publicly distribute the Work under its\nterms, with knowledge of his or her Copyright and Related Rights in the\nWork and the meaning and intended legal effect of CC0 on those rights.\n\n1. Copyright and Related Rights. A Work made available under CC0 may be\nprotected by copyright and related or neighboring rights (\"Copyright and\nRelated Rights\"). Copyright and Related Rights include, but are not\nlimited to, the following:\n\n i. the right to reproduce, adapt, distribute, perform, display,\n communicate, and translate a Work;\n ii. moral rights retained by the original author(s) and/or performer(s);\niii. publicity and privacy rights pertaining to a person's image or\n likeness depicted in a Work;\n iv. rights protecting against unfair competition in regards to a Work,\n subject to the limitations in paragraph 4(a), below;\n v. rights protecting the extraction, dissemination, use and reuse of data\n in a Work;\n vi. database rights (such as those arising under Directive 96/9/EC of the\n European Parliament and of the Council of 11 March 1996 on the legal\n protection of databases, and under any national implementation\n thereof, including any amended or successor version of such\n directive); and\nvii. other similar, equivalent or corresponding rights throughout the\n world based on applicable law or treaty, and any national\n implementations thereof.\n\n2. Waiver. To the greatest extent permitted by, but not in contravention\nof, applicable law, Affirmer hereby overtly, fully, permanently,\nirrevocably and unconditionally waives, abandons, and surrenders all of\nAffirmer's Copyright and Related Rights and associated claims and causes\nof action, whether now known or unknown (including existing as well as\nfuture claims and causes of action), in the Work (i) in all territories\nworldwide, (ii) for the maximum duration provided by applicable law or\ntreaty (including future time extensions), (iii) in any current or future\nmedium and for any number of copies, and (iv) for any purpose whatsoever,\nincluding without limitation commercial, advertising or promotional\npurposes (the \"Waiver\"). Affirmer makes the Waiver for the benefit of each\nmember of the public at large and to the detriment of Affirmer's heirs and\nsuccessors, fully intending that such Waiver shall not be subject to\nrevocation, rescission, cancellation, termination, or any other legal or\nequitable action to disrupt the quiet enjoyment of the Work by the public\nas contemplated by Affirmer's express Statement of Purpose.\n\n3. Public License Fallback. Should any part of the Waiver for any reason\nbe judged legally invalid or ineffective under applicable law, then the\nWaiver shall be preserved to the maximum extent permitted taking into\naccount Affirmer's express Statement of Purpose. In addition, to the\nextent the Waiver is so judged Affirmer hereby grants to each affected\nperson a royalty-free, non transferable, non sublicensable, non exclusive,\nirrevocable and unconditional license to exercise Affirmer's Copyright and\nRelated Rights in the Work (i) in all territories worldwide, (ii) for the\nmaximum duration provided by applicable law or treaty (including future\ntime extensions), (iii) in any current or future medium and for any number\nof copies, and (iv) for any purpose whatsoever, including without\nlimitation commercial, advertising or promotional purposes (the\n\"License\"). The License shall be deemed effective as of the date CC0 was\napplied by Affirmer to the Work. Should any part of the License for any\nreason be judged legally invalid or ineffective under applicable law, such\npartial invalidity or ineffectiveness shall not invalidate the remainder\nof the License, and in such case Affirmer hereby affirms that he or she\nwill not (i) exercise any of his or her remaining Copyright and Related\nRights in the Work or (ii) assert any associated claims and causes of\naction with respect to the Work, in either case contrary to Affirmer's\nexpress Statement of Purpose.\n\n4. Limitations and Disclaimers.\n\n a. No trademark or patent rights held by Affirmer are waived, abandoned,\n surrendered, licensed or otherwise affected by this document.\n b. Affirmer offers the Work as-is and makes no representations or\n warranties of any kind concerning the Work, express, implied,\n statutory or otherwise, including without limitation warranties of\n title, merchantability, fitness for a particular purpose, non\n infringement, or the absence of latent or other defects, accuracy, or\n the present or absence of errors, whether or not discoverable, all to\n the greatest extent permissible under applicable law.\n c. Affirmer disclaims responsibility for clearing rights of other persons\n that may apply to the Work or any use thereof, including without\n limitation any person's Copyright and Related Rights in the Work.\n Further, Affirmer disclaims responsibility for obtaining any necessary\n consents, permissions or other rights required for any use of the\n Work.\n d. Affirmer understands and acknowledges that Creative Commons is not a\n party to this document and has no duty or obligation with respect to\n this CC0 or use of the Work.\n", + "sha256-f1d9dc9f2f4edaaebd9167eb8b79a6da6434313df47921aa4ea8ae278f7739e3": "/**\n * Haketilo demo script.\n *\n * Copyright (C) 2021 Wojtek Kosior\n * Available under the terms of Creative Commons Zero\n * \n */\n\n{\n const banner = document.createElement(\"h2\");\n \n banner.textContent = \"Hoooray! Haketilo works :D\";\n \n banner.setAttribute(\"style\", `\\\n margin: 1em; \\\n border-radius: 1em 0px; \\\n background-color: #474; \\\n padding: 10px 20px; \\\n color: #eee; \\\n box-shadow: 0 6px 8px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19); \\\n display: inline-block;\\\n `);\n \n document.body.prepend(banner);\n}\n" + } } } diff --git a/html/install.js b/html/install.js index e972924..68033bc 100644 --- a/html/install.js +++ b/html/install.js @@ -49,7 +49,7 @@ #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/misc.js IMPORT sha256_async AS sha256 +#FROM common/misc.js IMPORT sha256_async AS compute_sha256 const coll = new Intl.Collator(); @@ -134,7 +134,7 @@ function InstallView(tab_id, on_view_show, on_view_hide) { /* Make a link to view a file from the repository. */ const make_file_link = (preview_ctx, file_ref) => { const a = document.createElement("a"); - a.href = `${this.repo_url}file/${file_ref.hash_key}`; + a.href = `${this.repo_url}file/sha256/${file_ref.sha256}`; a.innerText = file_ref.file; return a; @@ -213,10 +213,6 @@ function InstallView(tab_id, on_view_show, on_view_hide) { const files = response.json.source_copyright .concat(item_type === "resource" ? response.json.scripts : []); - for (const file of files) { - file.hash_key = `sha256-${file.sha256}`; - delete file.sha256; - } if (item_type === "mapping") { for (const res_ref of Object.values(response.json.payloads)) @@ -294,7 +290,7 @@ function InstallView(tab_id, on_view_show, on_view_hide) { dialog.close(this.dialog_ctx); } - const process_file = async (work, hash_key) => { + const process_file = async (work, sha256) => { if (!work.is_ok) return; @@ -302,7 +298,7 @@ function InstallView(tab_id, on_view_show, on_view_hide) { try { var file_uses = await haketilodb.idb_get(work.file_uses_transaction, - "file_uses", hash_key); + "file_uses", sha256); if (!work.is_ok) return; } catch(e) { @@ -311,7 +307,7 @@ function InstallView(tab_id, on_view_show, on_view_hide) { } if (!file_uses) { - const url = `${this.repo_url}file/${hash_key}`; + const url = `${this.repo_url}file/sha256/${sha256}`; try { var response = await fetch(url); @@ -336,15 +332,15 @@ function InstallView(tab_id, on_view_show, on_view_hide) { return work.err(e, msg); } - const digest = await sha256(text); + const digest = await compute_sha256(text); if (!work.is_ok) return; - if (`sha256-${digest}` !== hash_key) { + if (digest !== sha256) { const msg = `${url} served a file with different SHA256 cryptographic sum :(`; return work.err(null, msg); } - work.result.push([hash_key, text]); + work.result.push([sha256, text]); } if (--work.waiting === 0) @@ -359,9 +355,9 @@ function InstallView(tab_id, on_view_show, on_view_hide) { for (const item_def of item_defs) { for (const file of get_files(item_def)) { - if (!processed_files.has(file.hash_key)) { - processed_files.add(file.hash_key); - process_file(work, file.hash_key); + if (!processed_files.has(file.sha256)) { + processed_files.add(file.sha256); + process_file(work, file.sha256); } } } @@ -379,13 +375,13 @@ function InstallView(tab_id, on_view_show, on_view_hide) { try { var files = (await get_missing_files(item_defs)) - .reduce((ac, [hk, txt]) => Object.assign(ac, {[hk]: txt}), {}); + .reduce((ac, [h, txt]) => Object.assign(ac, {[h]: txt}), {}); } catch(e) { var dialog_prom = dialog.error(this.dialog_ctx, e); } if (files !== undefined) { - const data = {files}; + const data = {file: {sha256: files}}; const names = [["mappings", "mapping"], ["resources", "resource"]]; for (const [set_name, type] of names) { diff --git a/html/item_preview.js b/html/item_preview.js index c55183c..bd4fd68 100644 --- a/html/item_preview.js +++ b/html/item_preview.js @@ -63,7 +63,7 @@ const file_preview_link = browser.runtime.getURL("html/file_preview.html"); */ function make_file_link(preview_object, file_ref) { const a = document.createElement("a"); - a.href = `${file_preview_link}#${file_ref.hash_key}`; + a.href = `${file_preview_link}#${file_ref.sha256}`; a.innerText = file_ref.file; a.target = "_blank"; return a; diff --git a/html/payload_create.js b/html/payload_create.js index 8828809..7782299 100644 --- a/html/payload_create.js +++ b/html/payload_create.js @@ -45,7 +45,7 @@ #IMPORT common/indexeddb.js AS haketilodb #FROM html/DOM_helpers.js IMPORT clone_template -#FROM common/sha256.js IMPORT sha256 +#FROM common/sha256.js IMPORT sha256 AS compute_sha256 #FROM common/patterns.js IMPORT validate_normalize_url_pattern, \ patterns_doc_url @@ -94,7 +94,7 @@ function collect_form_data(form_ctx) const script = form_ctx.script.value; if (!script) throw "The 'script' field is required!"; - const hash_key = `sha256-${sha256(script)}`; + const sha256 = compute_sha256(script); const resource = { source_name: identifier, @@ -106,7 +106,7 @@ function collect_form_data(form_ctx) version: [1], description, dependencies: [], - scripts: [{file: "payload.js", hash_key}] + scripts: [{file: "payload.js", sha256}] }; const mapping = { @@ -121,7 +121,7 @@ function collect_form_data(form_ctx) payloads }; - return {identifier, resource, mapping, files: {[hash_key]: script}}; + return {identifier, resource, mapping, files_by_sha256: {[sha256]: script}}; } function clear_form(form_ctx) @@ -137,7 +137,8 @@ async function save_payload(saving) { const db = await haketilodb.get(); const tx_starter = haketilodb.start_items_transaction; - const tx_ctx = await tx_starter(["resource", "mapping"], saving.files); + const files = {sha256: saving.files_by_sha256}; + const tx_ctx = await tx_starter(["resource", "mapping"], files); for (const type of ["resource", "mapping"]) { if (!saving[`override_${type}`] && diff --git a/test/unit/test_indexeddb.py b/test/unit/test_indexeddb.py index 7ce4781..9041116 100644 --- a/test/unit/test_indexeddb.py +++ b/test/unit/test_indexeddb.py @@ -6,7 +6,7 @@ Haketilo unit tests - IndexedDB access # This file is part of Haketilo # -# Copyright (C) 2021,2022 Wojtek Kosior +# Copyright (C) 2021, 2022 Wojtek Kosior # # This program is free software: you can redistribute it and/or modify # it under the terms of the CC0 1.0 Universal License as published by @@ -73,19 +73,19 @@ def test_haketilodb_item_modifications(driver, execute_in_page): .then(finalize_transaction); returnval(promise); }''', - sample_item, sample_files_by_hash) + sample_item, {'sha256': sample_files_by_sha256}) database_contents = get_db_contents(execute_in_page) assert len(database_contents['files']) == 4 - assert all([sample_files_by_hash[file['hash_key']] == file['contents'] + assert all([sample_files_by_sha256[file['sha256']] == file['contents'] for file in database_contents['files']]) assert all([len(file) == 2 for file in database_contents['files']]) assert len(database_contents['file_uses']) == 4 assert all([uses['uses'] == 1 for uses in database_contents['file_uses']]) - assert set([uses['hash_key'] for uses in database_contents['file_uses']]) \ - == set([file['hash_key'] for file in database_contents['files']]) + assert set([uses['sha256'] for uses in database_contents['file_uses']]) \ + == set([file['sha256'] for file in database_contents['files']]) assert database_contents['mapping'] == [] assert database_contents['resource'] == [sample_item] @@ -93,8 +93,8 @@ def test_haketilodb_item_modifications(driver, execute_in_page): # See if trying to add an item without providing all its files ends in an # exception and aborts the transaction as it should. sample_item['scripts'].append(sample_file_ref('combined.js')) - incomplete_files = {**sample_files_by_hash} - incomplete_files.pop(sample_files['combined.js']['hash_key']) + incomplete_files = {**sample_files_by_sha256} + incomplete_files.pop(sample_files['combined.js']['sha256']) exception = execute_in_page( '''{ const args = arguments; @@ -112,14 +112,14 @@ def test_haketilodb_item_modifications(driver, execute_in_page): } returnval(try_add_item()); }''', - sample_item, incomplete_files) + sample_item, {'sha256': incomplete_files}) previous_database_contents = database_contents database_contents = get_db_contents(execute_in_page) assert 'file not present' in exception for key, val in database_contents.items(): - keyfun = lambda item: item.get('hash_key') or item['identifier'] + keyfun = lambda item: item.get('sha256') or item['identifier'] assert sorted(previous_database_contents[key], key=keyfun) \ == sorted(val, key=keyfun) @@ -132,7 +132,7 @@ def test_haketilodb_item_modifications(driver, execute_in_page): .then(finalize_transaction); returnval(promise); }''', - sample_item, sample_files_by_hash) + sample_item, {'sha256': sample_files_by_sha256}) database_contents = get_db_contents(execute_in_page) @@ -141,14 +141,14 @@ def test_haketilodb_item_modifications(driver, execute_in_page): sample_files_list = [sample_files[name] for name in names] uses_list = [1, 2, 1, 1, 1] - uses = dict([(uses['hash_key'], uses['uses']) + uses = dict([(uses['sha256'], uses['uses']) for uses in database_contents['file_uses']]) - assert uses == dict([(file['hash_key'], nr) + assert uses == dict([(file['sha256'], nr) for file, nr in zip(sample_files_list, uses_list)]) - files = dict([(file['hash_key'], file['contents']) + files = dict([(file['sha256'], file['contents']) for file in database_contents['files']]) - assert files == dict([(file['hash_key'], file['contents']) + assert files == dict([(file['sha256'], file['contents']) for file in sample_files_list]) del database_contents['resource'][0]['source_copyright'][0]['extra_prop'] @@ -174,13 +174,13 @@ def test_haketilodb_item_modifications(driver, execute_in_page): sample_files_list = [sample_files[name] for name in names] uses_list = [1, 1] - uses = dict([(uses['hash_key'], uses['uses']) + uses = dict([(uses['sha256'], uses['uses']) for uses in results[0]['file_uses']]) - assert uses == dict([(file['hash_key'], 1) for file in sample_files_list]) + assert uses == dict([(file['sha256'], 1) for file in sample_files_list]) - files = dict([(file['hash_key'], file['contents']) + files = dict([(file['sha256'], file['contents']) for file in results[0]['files']]) - assert files == dict([(file['hash_key'], file['contents']) + assert files == dict([(file['sha256'], file['contents']) for file in sample_files_list]) assert results[0]['resource'] == [] @@ -206,7 +206,9 @@ def test_haketilodb_item_modifications(driver, execute_in_page): '0.1.1': sample_mapping } }, - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } clear_indexeddb(execute_in_page) @@ -350,7 +352,9 @@ def test_haketilodb_track(driver, execute_in_page, wait_elem_text): '0.1.1': sample_mapping } }, - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } driver.switch_to.window(windows[1]) execute_in_page('initial_data = arguments[0];', initial_data) @@ -423,7 +427,9 @@ def test_haketilodb_track(driver, execute_in_page, wait_elem_text): '0.1.1': sample_mapping2 } }, - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } execute_in_page('returnval(save_items(arguments[0]));', sample_data) execute_in_page('returnval(set_setting("option22", "abc"));') diff --git a/test/unit/test_indexeddb_files_server.py b/test/unit/test_indexeddb_files_server.py index ab69d9d..390bbe7 100644 --- a/test/unit/test_indexeddb_files_server.py +++ b/test/unit/test_indexeddb_files_server.py @@ -35,7 +35,7 @@ sample_files_list = [(f'file_{n}_{i}', f'contents {n} {i}') sample_files = dict(sample_files_list) -sample_files, sample_files_by_hash = make_sample_files(sample_files) +sample_files, sample_files_by_sha256 = make_sample_files(sample_files) def make_sample_resource_with_deps(n): resource = make_sample_resource(with_files=False) @@ -53,7 +53,9 @@ resources = [make_sample_resource_with_deps(n) for n in range(count)] sample_data = { 'resources': sample_data_dict(resources), 'mapping': {}, - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } def prepare_test_page(initial_indexeddb_data, execute_in_page): diff --git a/test/unit/test_item_list.py b/test/unit/test_item_list.py index 0702129..46691c3 100644 --- a/test/unit/test_item_list.py +++ b/test/unit/test_item_list.py @@ -106,7 +106,9 @@ def test_item_list_ordering(driver, execute_in_page, item_type): sample_data = { 'resources': {}, 'mappings': {}, - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } indexes_added = set() @@ -189,7 +191,9 @@ def test_item_list_displaying(driver, execute_in_page, item_type): sample_data = { 'resources': {}, 'mappings': {}, - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } sample_data[item_type + 's'] = sample_data_dict(items) diff --git a/test/unit/test_item_preview.py b/test/unit/test_item_preview.py index 8b2b161..8787d5d 100644 --- a/test/unit/test_item_preview.py +++ b/test/unit/test_item_preview.py @@ -160,7 +160,7 @@ def test_file_preview_link(driver, execute_in_page): # Cause the "link" to `bye.js` to be invalid. sample_resource = make_sample_resource() - sample_resource['scripts'][1]['hash_key'] = 'dummy nonexistent key' + sample_resource['scripts'][1]['sha256'] = 'dummy nonexistent hash' execute_in_page( ''' diff --git a/test/unit/test_payload_create.py b/test/unit/test_payload_create.py index cee3a9b..9689c37 100644 --- a/test/unit/test_payload_create.py +++ b/test/unit/test_payload_create.py @@ -19,6 +19,8 @@ Haketilo unit tests - using a form to create simple site payload import pytest import re +from hashlib import sha256 + from selenium.webdriver.support.ui import WebDriverWait from ..extension_crafting import ExtraHTML @@ -137,7 +139,7 @@ def test_payload_create_normal_usage(driver, execute_in_page): 'long_name': long_name, 'scripts': [{ 'file': 'payload.js', - 'hash_key': make_hash_key(form_data['script']) + 'sha256': sha256(form_data['script'].encode()).digest().hex() }] }] diff --git a/test/unit/utils.py b/test/unit/utils.py index 56880d5..85dee63 100644 --- a/test/unit/utils.py +++ b/test/unit/utils.py @@ -33,12 +33,9 @@ from ..script_loader import load_script patterns_doc_url = \ 'https://hydrillabugs.koszko.org/projects/haketilo/wiki/URL_patterns' -def make_hash_key(file_contents): - return f'sha256-{sha256(file_contents.encode()).digest().hex()}' - def sample_file(contents): return { - 'hash_key': make_hash_key(contents), + 'sha256': sha256(contents.encode()).digest().hex(), 'contents': contents } @@ -51,12 +48,12 @@ def make_sample_files(names_contents): sample_files = dict([(name, sample_file(contents)) for name, contents in names_contents.items()]) - sample_files_by_hash = dict([[file['hash_key'], file['contents']] - for file in sample_files.values()]) + sample_files_by_sha256 = dict([[file['sha256'], file['contents']] + for file in sample_files.values()]) - return sample_files, sample_files_by_hash + return sample_files, sample_files_by_sha256 -sample_files, sample_files_by_hash = make_sample_files({ +sample_files, sample_files_by_sha256 = make_sample_files({ 'report.spdx': '', 'LICENSES/somelicense.txt': 'Permission is granted...', 'LICENSES/CC0-1.0.txt': 'Dummy Commons...', @@ -73,7 +70,7 @@ def sample_file_ref(file_name, sample_files_dict=sample_files): """ return { 'file': file_name, - 'hash_key': sample_files_dict[file_name]['hash_key'] + 'sha256': sample_files_dict[file_name]['sha256'] } def make_sample_mapping(with_files=True): @@ -154,7 +151,9 @@ def make_complete_sample_data(): return { 'resources': sample_data_dict([make_sample_resource()]), 'mappings': sample_data_dict([make_sample_mapping()]), - 'files': sample_files_by_hash + 'file': { + 'sha256': sample_files_by_sha256 + } } def clear_indexeddb(execute_in_page): diff --git a/test/world_wide_library.py b/test/world_wide_library.py index b3febd7..04f5e8b 100644 --- a/test/world_wide_library.py +++ b/test/world_wide_library.py @@ -114,7 +114,7 @@ sample_contents = [f'Mi povas manĝi vitron, ĝi ne damaĝas min {i}' for i in range(9)] sample_hashes = [sha256(c.encode()).digest().hex() for c in sample_contents] -file_url = lambda hashed: f'https://hydril.la/file/sha256-{hashed}' +file_url = lambda hashed: f'https://hydril.la/file/sha256/{hashed}' sample_files_catalog = dict([(file_url(h), make_handler(c)) for h, c in zip(sample_hashes, sample_contents)]) -- cgit v1.2.3