diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-02-09 17:09:13 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-02-09 18:00:16 +0100 |
commit | 1c65dd5ca24052ccf9a92939eecd0966c9635c50 (patch) | |
tree | 7575157ccd729b4ad79f6c04501023ccb0532a63 /common | |
parent | 830d22d8931a4e5f0606d401f24d7cd937f8697e (diff) | |
download | browser-extension-1c65dd5ca24052ccf9a92939eecd0966c9635c50.tar.gz browser-extension-1c65dd5ca24052ccf9a92939eecd0966c9635c50.zip |
adapt to changes in file path format
From now on we assume Hydrilla serves file contents at 'file/sha256/<hash>' instead of 'file/sha256-<hash>'.
With this commit we also stop using the "hash_key" property internally.
Diffstat (limited to 'common')
-rw-r--r-- | common/indexeddb.js | 49 |
1 files changed, 26 insertions, 23 deletions
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 <koszko@koszko.org> + * Copyright (C) 2021, 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 @@ -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-<file's-sha256-sum>`. + * "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); } |