Revision 3a90084e
Added by koszko over 1 year ago
| Makefile.in | ||
|---|---|---|
| 79 | 79 |
clean mostlyclean: |
| 80 | 80 |
rm -rf mozilla-unpacked chromium-unpacked haketilo-$(version) |
| 81 | 81 |
rm -f mozilla-build.zip chromium-build.zip haketilo-$(version).tar.gz \ |
| 82 |
haketilo-$(version).tar |
|
| 82 |
haketilo-$(version).tar exports_init.js
|
|
| 83 | 83 |
rm -rf test/certs |
| 84 | 84 |
rm -rf $$(find . -name geckodriver.log) |
| 85 | 85 |
rm -rf $$(find . -type d -name __pycache__) |
| background/main.js | ||
|---|---|---|
| 3 | 3 |
* |
| 4 | 4 |
* Function: Main background script. |
| 5 | 5 |
* |
| 6 |
* Copyright (C) 2021 Wojtek Kosior |
|
| 6 |
* Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org> |
|
| 7 |
* Copyright (C) 2021 Jahoti <jahoti@envs.net> |
|
| 7 | 8 |
* |
| 8 | 9 |
* This program is free software: you can redistribute it and/or modify |
| 9 | 10 |
* it under the terms of the GNU General Public License as published by |
| ... | ... | |
| 43 | 44 |
|
| 44 | 45 |
/* |
| 45 | 46 |
* IMPORTS_START |
| 47 |
* IMPORT initial_data |
|
| 46 | 48 |
* IMPORT TYPE_PREFIX |
| 47 | 49 |
* IMPORT get_storage |
| 48 | 50 |
* IMPORT light_storage |
| ... | ... | |
| 70 | 72 |
|
| 71 | 73 |
await storage.clear(); |
| 72 | 74 |
|
| 73 |
/* |
|
| 74 |
* Below we add sample settings to the extension. |
|
| 75 |
*/ |
|
| 76 |
|
|
| 77 |
for (let setting of // The next line is replaced with the contents of /default_settings.json by the build script |
|
| 78 |
`DEFAULT SETTINGS` |
|
| 79 |
) {
|
|
| 75 |
/* Below we add sample settings to the extension. */ |
|
| 76 |
for (let setting of initial_data) {
|
|
| 80 | 77 |
let [key, value] = Object.entries(setting)[0]; |
| 81 | 78 |
storage.set(key[0], key.substring(1), value); |
| 82 | 79 |
} |
| build.sh | ||
|---|---|---|
| 123 | 123 |
fi |
| 124 | 124 |
done |
| 125 | 125 |
|
| 126 |
# A hack to insert the contents of default_settings.json at the appropriate |
|
| 127 |
# location in background/main.js. Uses an internal sed expression to escape |
|
| 128 |
# and indent the JSON file for use in the external sed expression. |
|
| 129 |
sed -i 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g "$BUILDDIR"/background/main.js |
|
| 130 |
|
|
| 131 |
if [ "$BROWSER" = "chromium" ]; then |
|
| 132 |
cp CHROMIUM_exports_init.js "$BUILDDIR"/exports_init.js |
|
| 133 |
else |
|
| 134 |
cp MOZILLA_exports_init.js "$BUILDDIR"/exports_init.js |
|
| 135 |
fi |
|
| 126 |
./write_exports_init.sh "$BROWSER" "$BUILDDIR" default_settings.json |
|
| 136 | 127 |
|
| 137 | 128 |
cp -r copyright licenses/ "$BUILDDIR" |
| 138 | 129 |
cp dummy "$BUILDDIR" |
| common/entities.js | ||
|---|---|---|
| 1 |
/** |
|
| 2 |
* This file is part of Haketilo. |
|
| 3 |
* |
|
| 4 |
* Function: Operations on resources and mappings. |
|
| 5 |
* |
|
| 6 |
* Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org> |
|
| 7 |
* |
|
| 8 |
* This program is free software: you can redistribute it and/or modify |
|
| 9 |
* it under the terms of the GNU General Public License as published by |
|
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
|
| 11 |
* (at your option) any later version. |
|
| 12 |
* |
|
| 13 |
* This program is distributed in the hope that it will be useful, |
|
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 16 |
* GNU General Public License for more details. |
|
| 17 |
* |
|
| 18 |
* As additional permission under GNU GPL version 3 section 7, you |
|
| 19 |
* may distribute forms of that code without the copy of the GNU |
|
| 20 |
* GPL normally required by section 4, provided you include this |
|
| 21 |
* license notice and, in case of non-source distribution, a URL |
|
| 22 |
* through which recipients can access the Corresponding Source. |
|
| 23 |
* If you modify file(s) with this exception, you may extend this |
|
| 24 |
* exception to your version of the file(s), but you are not |
|
| 25 |
* obligated to do so. If you do not wish to do so, delete this |
|
| 26 |
* exception statement from your version. |
|
| 27 |
* |
|
| 28 |
* As a special exception to the GPL, any HTML file which merely |
|
| 29 |
* makes function calls to this code, and for that purpose |
|
| 30 |
* includes it by reference shall be deemed a separate work for |
|
| 31 |
* copyright law purposes. If you modify this code, you may extend |
|
| 32 |
* this exception to your version of the code, but you are not |
|
| 33 |
* obligated to do so. If you do not wish to do so, delete this |
|
| 34 |
* exception statement from your version. |
|
| 35 |
* |
|
| 36 |
* You should have received a copy of the GNU General Public License |
|
| 37 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
| 38 |
* |
|
| 39 |
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's |
|
| 40 |
* license. Although I request that you do not make use this code in a |
|
| 41 |
* proprietary program, I am not going to enforce this in court. |
|
| 42 |
*/ |
|
| 43 |
|
|
| 44 |
/* |
|
| 45 |
* Convert ver_str into an array representation, e.g. for ver_str="4.6.13.0" |
|
| 46 |
* return [4, 6, 13, 0]. |
|
| 47 |
*/ |
|
| 48 |
const parse_version = ver_str => ver_str.split(".").map(parseInt);
|
|
| 49 |
|
|
| 50 |
/* |
|
| 51 |
* ver is an array of integers. rev is an optional integer. Produce string |
|
| 52 |
* representation of version (optionally with revision number), like: |
|
| 53 |
* 1.2.3-5 |
|
| 54 |
* No version normalization is performed. |
|
| 55 |
*/ |
|
| 56 |
const version_string = (ver, rev=0) => ver.join(".") + (rev ? `-${rev}` : "");
|
|
| 57 |
|
|
| 58 |
/* vers should be an array of comparable values. Return the greatest one. */ |
|
| 59 |
const max = vals => Array.reduce(vals, (v1, v2) => v1 > v2 ? v1 : v2); |
|
| 60 |
|
|
| 61 |
/* |
|
| 62 |
* versioned_item should be a dict with keys being version strings and values |
|
| 63 |
* being definitions of the respective versions of a single resource/mapping. |
|
| 64 |
* Example: |
|
| 65 |
* {
|
|
| 66 |
* "1": {
|
|
| 67 |
* version: [1]//, |
|
| 68 |
* // more stuff |
|
| 69 |
* }, |
|
| 70 |
* "1.1": {
|
|
| 71 |
* version: [1, 1]//, |
|
| 72 |
* // more stuff |
|
| 73 |
* } |
|
| 74 |
* } |
|
| 75 |
* |
|
| 76 |
* Returns the definition with the highest version. |
|
| 77 |
*/ |
|
| 78 |
function get_newest_version(versioned_item) |
|
| 79 |
{
|
|
| 80 |
const best_ver = max(Object.keys(versioned_item).map(parse_version)); |
|
| 81 |
return versioned_item[version_string(best_ver)]; |
|
| 82 |
} |
|
| 83 |
|
|
| 84 |
/* |
|
| 85 |
* item is a definition of a resource or mapping. Yield all file references |
|
| 86 |
* (objects with `file` and `sha256` properties) this definition has. |
|
| 87 |
*/ |
|
| 88 |
function* get_used_files(item) |
|
| 89 |
{
|
|
| 90 |
for (const file of item.source_copyright) |
|
| 91 |
yield file; |
|
| 92 |
|
|
| 93 |
if (item.type === "resource") {
|
|
| 94 |
for (const file of item.scripts || []) |
|
| 95 |
yield file; |
|
| 96 |
} |
|
| 97 |
} |
|
| 98 |
|
|
| 99 |
const entities = {
|
|
| 100 |
get_newest: get_newest_version, |
|
| 101 |
get_files: get_used_files |
|
| 102 |
}; |
|
| 103 |
|
|
| 104 |
/* |
|
| 105 |
* EXPORTS_START |
|
| 106 |
* EXPORT entities |
|
| 107 |
* EXPORTS_END |
|
| 108 |
*/ |
|
| 109 |
|
|
| 110 |
/* |
|
| 111 |
* Note: the functions below were overeagerly written and are not used now but |
|
| 112 |
* might prove useful to once we add more functionalities and are hence kept... |
|
| 113 |
*/ |
|
| 114 |
|
|
| 115 |
/* |
|
| 116 |
* Clone recursively all objects. Leave other items (arrays, strings) untouched. |
|
| 117 |
*/ |
|
| 118 |
function deep_object_copy(object) |
|
| 119 |
{
|
|
| 120 |
const orig = {object};
|
|
| 121 |
const result = {};
|
|
| 122 |
const to_copy = [[orig, {}]];
|
|
| 123 |
|
|
| 124 |
while (to_copy.length > 0) {
|
|
| 125 |
const [object, copy] = to_copy.pop(); |
|
| 126 |
|
|
| 127 |
for (const [key, value] of Object.entries(object)) {
|
|
| 128 |
copy[key] = value; |
|
| 129 |
|
|
| 130 |
if (typeof value === "object" && !Array.isArray(value)) {
|
|
| 131 |
const value_copy = {};
|
|
| 132 |
to_copy.push([value, value_copy]); |
|
| 133 |
copy[key] = value_copy; |
|
| 134 |
} |
|
| 135 |
} |
|
| 136 |
} |
|
| 137 |
|
|
| 138 |
return result.orig; |
|
| 139 |
} |
|
| 140 |
|
|
| 141 |
/* helper function for normalize_version() */ |
|
| 142 |
const version_reductor = (acc, n) => [...(n || acc.length ? [n] : []), ...acc]; |
|
| 143 |
/* |
|
| 144 |
* ver is an array of integers. Strip right-most zeroes from ver. |
|
| 145 |
* |
|
| 146 |
* Returns a *new* array. Doesn't modify its argument. |
|
| 147 |
*/ |
|
| 148 |
const normalize_version = ver => Array.reduceRight(ver, version_reductor, []); |
|
| common/indexeddb.js | ||
|---|---|---|
| 1 |
/** |
|
| 2 |
* This file is part of Haketilo. |
|
| 3 |
* |
|
| 4 |
* Function: Facilitate use of IndexedDB within Haketilo. |
|
| 5 |
* |
|
| 6 |
* Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org> |
|
| 7 |
* |
|
| 8 |
* This program is free software: you can redistribute it and/or modify |
|
| 9 |
* it under the terms of the GNU General Public License as published by |
|
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
|
| 11 |
* (at your option) any later version. |
|
| 12 |
* |
|
| 13 |
* This program is distributed in the hope that it will be useful, |
|
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 16 |
* GNU General Public License for more details. |
|
| 17 |
* |
|
| 18 |
* As additional permission under GNU GPL version 3 section 7, you |
|
| 19 |
* may distribute forms of that code without the copy of the GNU |
|
| 20 |
* GPL normally required by section 4, provided you include this |
|
| 21 |
* license notice and, in case of non-source distribution, a URL |
|
| 22 |
* through which recipients can access the Corresponding Source. |
|
| 23 |
* If you modify file(s) with this exception, you may extend this |
|
| 24 |
* exception to your version of the file(s), but you are not |
|
| 25 |
* obligated to do so. If you do not wish to do so, delete this |
|
| 26 |
* exception statement from your version. |
|
| 27 |
* |
|
| 28 |
* As a special exception to the GPL, any HTML file which merely |
|
| 29 |
* makes function calls to this code, and for that purpose |
|
| 30 |
* includes it by reference shall be deemed a separate work for |
|
| 31 |
* copyright law purposes. If you modify this code, you may extend |
|
| 32 |
* this exception to your version of the code, but you are not |
|
| 33 |
* obligated to do so. If you do not wish to do so, delete this |
|
| 34 |
* exception statement from your version. |
|
| 35 |
* |
|
| 36 |
* You should have received a copy of the GNU General Public License |
|
| 37 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
| 38 |
* |
|
| 39 |
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's |
|
| 40 |
* license. Although I request that you do not make use this code in a |
|
| 41 |
* proprietary program, I am not going to enforce this in court. |
|
| 42 |
*/ |
|
| 43 |
|
|
| 44 |
/* |
|
| 45 |
* IMPORTS_START |
|
| 46 |
* IMPORT initial_data |
|
| 47 |
* IMPORT entities |
|
| 48 |
* IMPORTS_END |
|
| 49 |
*/ |
|
| 50 |
|
|
| 51 |
/* Update when changes are made to database schema. Must have 3 elements */ |
|
| 52 |
const db_version = [1, 0, 0]; |
|
| 53 |
|
|
| 54 |
const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i]; |
|
| 55 |
const version_nr = ver => Array.reduce(ver.slice(0, 3), nr_reductor, [2, 0])[1]; |
|
| 56 |
|
|
| 57 |
const stores = [ |
|
| 58 |
["files", {keyPath: "sha256"}],
|
|
| 59 |
["file_uses", {keyPath: "sha256"}],
|
|
| 60 |
["resources", {keyPath: "identifier"}],
|
|
| 61 |
["mappings", {keyPath: "identifier"}]
|
|
| 62 |
]; |
|
| 63 |
|
|
| 64 |
let db = null; |
|
| 65 |
|
|
| 66 |
/* Generate a Promise that resolves when an IndexedDB request succeeds. */ |
|
| 67 |
async function wait_request(idb_request) |
|
| 68 |
{
|
|
| 69 |
let resolve, reject; |
|
| 70 |
const waiter = new Promise((...cbs) => [resolve, reject] = cbs); |
|
| 71 |
[idb_request.onsuccess, idb_request.onerror] = [resolve, reject]; |
|
| 72 |
return waiter; |
|
| 73 |
} |
|
| 74 |
|
|
| 75 |
/* asynchronous wrapper for IDBObjectStore's get() method. */ |
|
| 76 |
async function idb_get(transaction, store_name, key) |
|
| 77 |
{
|
|
| 78 |
const req = transaction.objectStore(store_name).get(key); |
|
| 79 |
return (await wait_request(req)).target.result; |
|
| 80 |
} |
|
| 81 |
|
|
| 82 |
/* asynchronous wrapper for IDBObjectStore's put() method. */ |
|
| 83 |
async function idb_put(transaction, store_name, object) |
|
| 84 |
{
|
|
| 85 |
return wait_request(transaction.objectStore(store_name).put(object)); |
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
/* asynchronous wrapper for IDBObjectStore's delete() method. */ |
|
| 89 |
async function idb_del(transaction, store_name, key) |
|
| 90 |
{
|
|
| 91 |
return wait_request(transaction.objectStore(store_name).delete(key)); |
|
| 92 |
} |
|
| 93 |
|
|
| 94 |
/* Open haketilo database, asynchronously return an IDBDatabase object. */ |
|
| 95 |
async function get_db(initialization_data=initial_data) |
|
| 96 |
{
|
|
| 97 |
if (db) |
|
| 98 |
return db; |
|
| 99 |
|
|
| 100 |
let resolve, reject; |
|
| 101 |
const waiter = new Promise((...cbs) => [resolve, reject] = cbs); |
|
| 102 |
|
|
| 103 |
const request = indexedDB.open("haketilo", version_nr(db_version));
|
|
| 104 |
request.onsuccess = resolve; |
|
| 105 |
request.onerror = ev => reject("db error: " + ev.target.errorCode);
|
|
| 106 |
request.onupgradeneeded = resolve; |
|
| 107 |
|
|
| 108 |
const event = await waiter; |
|
| 109 |
const opened_db = event.target.result; |
|
| 110 |
|
|
| 111 |
if (event instanceof IDBVersionChangeEvent) {
|
|
| 112 |
/* |
|
| 113 |
* When we move to a new database schema, we will add upgrade logic |
|
| 114 |
* here. |
|
| 115 |
*/ |
|
| 116 |
if (event.oldVersion > 0) |
|
| 117 |
throw "bad db version: " + event.oldVersion; |
|
| 118 |
|
|
| 119 |
let store; |
|
| 120 |
for (const [store_name, key_mode] of stores) |
|
| 121 |
store = opened_db.createObjectStore(store_name, key_mode); |
|
| 122 |
|
|
| 123 |
await new Promise(resolve => store.transaction.oncomplete = resolve); |
|
| 124 |
|
|
| 125 |
save_items(db, initialization_data); |
|
| 126 |
} |
|
| 127 |
|
|
| 128 |
db = opened_db; |
|
| 129 |
|
|
| 130 |
return db; |
|
| 131 |
} |
|
| 132 |
|
|
| 133 |
/* |
|
| 134 |
* How a sample data argument to the function below might look like: |
|
| 135 |
* |
|
| 136 |
* data = {
|
|
| 137 |
* resources: {
|
|
| 138 |
* "resource1": {
|
|
| 139 |
* "1": {
|
|
| 140 |
* // some stuff |
|
| 141 |
* }, |
|
| 142 |
* "1.1": {
|
|
| 143 |
* // some stuff |
|
| 144 |
* } |
|
| 145 |
* }, |
|
| 146 |
* "resource2": {
|
|
| 147 |
* "0.4.3": {
|
|
| 148 |
* // some stuff |
|
| 149 |
* } |
|
| 150 |
* }, |
|
| 151 |
* }, |
|
| 152 |
* mappings: {
|
|
| 153 |
* "mapping1": {
|
|
| 154 |
* "2": {
|
|
| 155 |
* // some stuff |
|
| 156 |
* } |
|
| 157 |
* }, |
|
| 158 |
* "mapping2": {
|
|
| 159 |
* "0.1": {
|
|
| 160 |
* // some stuff |
|
| 161 |
* } |
|
| 162 |
* }, |
|
| 163 |
* }, |
|
| 164 |
* files: {
|
|
| 165 |
* "sha256-f9444510dc7403e41049deb133f6892aa6a63c05591b2b59e4ee5b234d7bbd99": "console.log(\"hello\");\n", |
|
| 166 |
* "sha256-b857cd521cc82fff30f0d316deba38b980d66db29a5388eb6004579cf743c6fd": "console.log(\"bye\");" |
|
| 167 |
* } |
|
| 168 |
* } |
|
| 169 |
*/ |
|
| 170 |
async function save_items(db, data) |
|
| 171 |
{
|
|
| 172 |
const files = data.files; |
|
| 173 |
const resources = |
|
| 174 |
Object.values(data.resources || []).map(entities.get_newest); |
|
| 175 |
const mappings = |
|
| 176 |
Object.values(data.mappings || []).map(entities.get_newest); |
|
| 177 |
|
|
| 178 |
resources.concat(mappings).forEach(i => save_item(i, data.files, db)); |
|
| 179 |
} |
|
| 180 |
|
|
| 181 |
/* helper function of save_item() */ |
|
| 182 |
async function get_file_uses(transaction, file_uses_sha256, file_ref) |
|
| 183 |
{
|
|
| 184 |
let uses = file_uses_sha256[file_ref.sha256]; |
|
| 185 |
if (uses === undefined) {
|
|
| 186 |
uses = await idb_get(transaction, "file_uses", file_ref.sha256); |
|
| 187 |
if (uses) |
|
| 188 |
[uses.new, uses.initial] = [false, uses.uses]; |
|
| 189 |
else |
|
| 190 |
uses = {sha256: file_ref.sha256, uses: 0, new: true, initial: 0};
|
|
| 191 |
|
|
| 192 |
file_uses_sha256[file_ref.sha256] = uses; |
|
| 193 |
} |
|
| 194 |
|
|
| 195 |
return uses; |
|
| 196 |
} |
|
| 197 |
|
|
| 198 |
/* |
|
| 199 |
* Save given definition of a resource/mapping to IndexedDB. If the definition |
|
| 200 |
* (passed as `item`) references files that are not already present in |
|
| 201 |
* IndexedDB, those files should be present as values of the `files_sha256` |
|
| 202 |
* object with keys being their sha256 sums. |
|
| 203 |
*/ |
|
| 204 |
async function save_item(item, files_sha256, db) |
|
| 205 |
{
|
|
| 206 |
const store_name = {resource: "resources", mapping: "mappings"}[item.type];
|
|
| 207 |
const transaction = |
|
| 208 |
db.transaction([store_name, "files", "file_uses"], "readwrite"); |
|
| 209 |
|
|
| 210 |
let resolve, reject; |
|
| 211 |
const result = new Promise((...cbs) => [resolve, reject] = cbs); |
|
| 212 |
transaction.oncomplete = resolve; |
|
| 213 |
transaction.onerror = reject; |
|
| 214 |
|
|
| 215 |
const uses_sha256 = {};
|
|
| 216 |
for (const file_ref of entities.get_files(item)) |
|
| 217 |
(await get_file_uses(transaction, uses_sha256, file_ref)).uses++; |
|
| 218 |
|
|
| 219 |
const old_item = await idb_get(transaction, store_name, item.identifier); |
|
| 220 |
if (old_item !== undefined) {
|
|
| 221 |
for (const file_ref of entities.get_files(old_item)) |
|
| 222 |
(await get_file_uses(transaction, uses_sha256, file_ref)).uses--; |
|
| 223 |
} |
|
| 224 |
|
|
| 225 |
for (const uses of Object.values(uses_sha256)) {
|
|
| 226 |
if (uses.uses < 0) |
|
| 227 |
console.error("internal error: uses < 0 for file " + uses.sha256);
|
|
| 228 |
|
|
| 229 |
const [is_new, initial_uses] = [uses.new, uses.initial]; |
|
| 230 |
delete uses.new; |
|
| 231 |
delete uses.initial; |
|
| 232 |
|
|
| 233 |
if (uses.uses < 1) {
|
|
| 234 |
if (!is_new) {
|
|
| 235 |
idb_del(transaction, "file_uses", uses.sha256); |
|
| 236 |
idb_del(transaction, "files", uses.sha256); |
|
| 237 |
} |
|
| 238 |
|
|
| 239 |
continue; |
|
| 240 |
} |
|
| 241 |
|
|
| 242 |
if (uses.uses === initial_uses) |
|
| 243 |
continue; |
|
| 244 |
|
|
| 245 |
const file = files_sha256[uses.sha256]; |
|
| 246 |
if (file === undefined) |
|
| 247 |
throw "file not present: " + uses.sha256; |
|
| 248 |
|
|
| 249 |
idb_put(transaction, "files", {sha256: uses.sha256, contents: file});
|
|
| 250 |
idb_put(transaction, "file_uses", uses); |
|
| 251 |
} |
|
| 252 |
|
|
| 253 |
idb_put(transaction, store_name, item); |
|
| 254 |
|
|
| 255 |
return result; |
|
| 256 |
} |
|
| 257 |
|
|
| 258 |
const haketilodb = {
|
|
| 259 |
get: get_db, |
|
| 260 |
save_item: save_item |
|
| 261 |
}; |
|
| 262 |
|
|
| 263 |
/* |
|
| 264 |
* EXPORTS_START |
|
| 265 |
* EXPORT haketilodb |
|
| 266 |
* EXPORT idb_get |
|
| 267 |
* EXPORT idb_put |
|
| 268 |
* EXPORT idb_del |
|
| 269 |
* EXPORTS_END |
|
| 270 |
*/ |
|
| compute_scripts.awk | ||
|---|---|---|
| 163 | 163 |
} |
| 164 | 164 |
|
| 165 | 165 |
function mock_exports_init() {
|
| 166 |
provides["browser"] = "exports_init.js" |
|
| 167 |
provides["is_chrome"] = "exports_init.js" |
|
| 168 |
provides["is_mozilla"] = "exports_init.js" |
|
| 166 |
provides["browser"] = "exports_init.js" |
|
| 167 |
provides["is_chrome"] = "exports_init.js" |
|
| 168 |
provides["is_mozilla"] = "exports_init.js" |
|
| 169 |
provides["initial_data"] = "exports_init.js" |
|
| 169 | 170 |
|
| 170 | 171 |
processed["exports_init.js"] = "used" |
| 171 | 172 |
} |
| test/script_loader.py | ||
|---|---|---|
| 51 | 51 |
|
| 52 | 52 |
def wrapped_script(script_path, wrap_partially=True): |
| 53 | 53 |
if script_path == 'exports_init.js': |
| 54 |
with open(script_root / 'MOZILLA_exports_init.js') as script: |
|
| 54 |
if not (script_root / 'exports_init.js').exists(): |
|
| 55 |
subprocess.run([str(script_root / 'write_exports_init.sh'), |
|
| 56 |
'mozilla', '.', 'default_settings.json'], |
|
| 57 |
cwd=script_root, check=True) |
|
| 58 |
|
|
| 59 |
with open(script_root / 'exports_init.js') as script: |
|
| 55 | 60 |
return script.read() |
| 56 | 61 |
|
| 57 | 62 |
command = 'partially_wrapped_code' if wrap_partially else 'wrapped_code' |
| test/unit/test_indexeddb.py | ||
|---|---|---|
| 1 |
# SPDX-License-Identifier: CC0-1.0 |
|
| 2 |
|
|
| 3 |
""" |
|
| 4 |
Haketilo unit tests - IndexedDB access |
|
| 5 |
""" |
|
| 6 |
|
|
| 7 |
# This file is part of Haketilo |
|
| 8 |
# |
|
| 9 |
# Copyright (C) 2021, Wojtek Kosior <koszko@koszko.org> |
|
| 10 |
# |
|
| 11 |
# This program is free software: you can redistribute it and/or modify |
|
| 12 |
# it under the terms of the CC0 1.0 Universal License as published by |
|
| 13 |
# the Creative Commons Corporation. |
|
| 14 |
# |
|
| 15 |
# This program is distributed in the hope that it will be useful, |
|
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 18 |
# CC0 1.0 Universal License for more details. |
|
| 19 |
|
|
| 20 |
import pytest |
|
| 21 |
from hashlib import sha256 |
|
| 22 |
|
|
| 23 |
from ..script_loader import load_script |
|
| 24 |
|
|
| 25 |
@pytest.fixture(scope="session") |
|
| 26 |
def indexeddb_code(): |
|
| 27 |
yield load_script('common/indexeddb.js', ['common'])
|
|
| 28 |
|
|
| 29 |
def sample_file(contents): |
|
| 30 |
return {
|
|
| 31 |
'sha256': sha256(contents.encode()).digest().hex(), |
|
| 32 |
contents: contents |
|
| 33 |
} |
|
| 34 |
|
|
| 35 |
sample_files = {
|
|
| 36 |
'report.spdx': sample_file('<!-- dummy report -->'),
|
|
| 37 |
'LICENSES/somelicense.txt': sample_file('Permission is granted...'),
|
|
| 38 |
'hello.js': sample_file('console.log("hello!");\n'),
|
|
| 39 |
'bye.js': sample_file('console.log("bye!");\n'),
|
|
| 40 |
'README.md': sample_file('# Python Frobnicator\n...')
|
|
| 41 |
} |
|
| 42 |
|
|
| 43 |
sample_files_sha256 = \ |
|
| 44 |
dict([[file['sha256'], file] for file in sample_files.values()]) |
|
| 45 |
|
|
| 46 |
def file_ref(file_name): |
|
| 47 |
return {'file': file_name, 'sha256': sample_files[file_name]['sha256']}
|
|
| 48 |
|
|
| 49 |
def test_save_item(execute_in_page, indexeddb_code): |
|
| 50 |
""" |
|
| 51 |
indexeddb.js facilitates operating on Haketilo's internal database. |
|
| 52 |
Verify database operations work properly. |
|
| 53 |
""" |
|
| 54 |
execute_in_page(indexeddb_code, page='https://gotmyowndoma.in') |
|
| 55 |
# Don't use Haketilo's default initial data. |
|
| 56 |
execute_in_page( |
|
| 57 |
'''{
|
|
| 58 |
const _get_db = haketilodb.get; |
|
| 59 |
get_db = () => _get_db({});
|
|
| 60 |
haketilodb.get = get_db; |
|
| 61 |
}''' |
|
| 62 |
) |
|
| 63 |
|
|
| 64 |
# Start with no database. |
|
| 65 |
execute_in_page( |
|
| 66 |
'''{
|
|
| 67 |
async function delete_db() {
|
|
| 68 |
let resolve; |
|
| 69 |
const result = new Promise(_resolve => resolve = _resolve); |
|
| 70 |
const request = indexedDB.deleteDatabase("haketilo");
|
|
| 71 |
[request.onsuccess, request.onerror] = [resolve, resolve]; |
|
| 72 |
await result; |
|
| 73 |
} |
|
| 74 |
|
|
| 75 |
returnval(delete_db()); |
|
| 76 |
}''' |
|
| 77 |
) |
|
| 78 |
|
|
| 79 |
# Facilitate retrieving all IndexedDB contents. |
|
| 80 |
execute_in_page( |
|
| 81 |
''' |
|
| 82 |
async function get_database_contents(promise=Promise.resolve()) |
|
| 83 |
{
|
|
| 84 |
if (promise) |
|
| 85 |
await promise; |
|
| 86 |
|
|
| 87 |
const db = await haketilodb.get(); |
|
| 88 |
|
|
| 89 |
const transaction = db.transaction(db.objectStoreNames); |
|
| 90 |
const store_names_reqs = [...db.objectStoreNames] |
|
| 91 |
.map(sn => [sn, transaction.objectStore(sn).getAll()]) |
|
| 92 |
|
|
| 93 |
const promises = store_names_reqs |
|
| 94 |
.map(([_, req]) => wait_request(req)); |
|
| 95 |
await Promise.all(promises); |
|
| 96 |
|
|
| 97 |
const result = {};
|
|
| 98 |
store_names_reqs.forEach(([sn, req]) => result[sn] = req.result); |
|
| 99 |
return result; |
|
| 100 |
} |
|
| 101 |
''') |
|
| 102 |
|
|
| 103 |
# Sample resource definition. It'd normally contain more fields but here |
|
| 104 |
# we use a simplified version. |
|
| 105 |
sample_item = {
|
|
| 106 |
'source_copyright': [ |
|
| 107 |
file_ref('report.spdx'),
|
|
| 108 |
file_ref('LICENSES/somelicense.txt')
|
|
| 109 |
], |
|
| 110 |
'type': 'resource', |
|
| 111 |
'identifier': 'helloapple', |
|
| 112 |
'scripts': [file_ref('hello.js'), file_ref('bye.js')],
|
|
| 113 |
'type': 'resource' |
|
| 114 |
} |
|
| 115 |
next(iter(sample_item['source_copyright']))['ugly_extra_property'] = True |
|
| 116 |
|
|
| 117 |
database_contents = execute_in_page( |
|
| 118 |
'''{
|
|
| 119 |
const prom = haketilodb.get().then(db => save_item(...arguments, db)); |
|
| 120 |
returnval(get_database_contents(prom)); |
|
| 121 |
}''', |
|
| 122 |
sample_item, sample_files_sha256) |
|
| 123 |
assert len(database_contents['files']) == 4 |
|
| 124 |
assert all([sample_files_sha256[file['sha256']] == file['contents'] |
|
| 125 |
for file in database_contents['files']]) |
|
| 126 |
assert all([len(file) == 2 for file in database_contents['files']]) |
|
| 127 |
|
|
| 128 |
assert len(database_contents['file_uses']) == 4 |
|
| 129 |
assert all([uses['uses'] == 1 for uses in database_contents['file_uses']]) |
|
| 130 |
assert set([uses['sha256'] for uses in database_contents['file_uses']]) \ |
|
| 131 |
== set([file['sha256'] for file in database_contents['files']]) |
|
| 132 |
|
|
| 133 |
assert database_contents['mappings'] == [] |
|
| 134 |
assert database_contents['resources'] == [sample_item] |
|
| write_exports_init.sh | ||
|---|---|---|
| 1 |
#!/bin/sh |
|
| 2 |
|
|
| 3 |
# This file is part of Haketilo |
|
| 4 |
# |
|
| 5 |
# Copyright (C) 2021, Wojtek Kosior |
|
| 6 |
# |
|
| 7 |
# This program is free software: you can redistribute it and/or modify |
|
| 8 |
# it under the terms of the CC0 1.0 Universal License as published by |
|
| 9 |
# the Creative Commons Corporation. |
|
| 10 |
# |
|
| 11 |
# This program is distributed in the hope that it will be useful, |
|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 |
# CC0 1.0 Universal License for more details. |
|
| 15 |
|
|
| 16 |
set -e |
|
| 17 |
|
|
| 18 |
BROWSER="$1" |
|
| 19 |
BUILDDIR="$2" |
|
| 20 |
SETTINGS="$3" |
|
| 21 |
|
|
| 22 |
if [ "chromium" = "$BROWSER" ]; then |
|
| 23 |
cp CHROMIUM_exports_init.js "$BUILDDIR"/exports_init.js |
|
| 24 |
else |
|
| 25 |
cp MOZILLA_exports_init.js "$BUILDDIR"/exports_init.js |
|
| 26 |
fi |
|
| 27 |
|
|
| 28 |
printf 'window.haketilo_exports.initial_data = %s;\n' "$(cat "$SETTINGS")" \ |
|
| 29 |
>> "$BUILDDIR"/exports_init.js |
|
Also available in: Unified diff
facilitate initialization of IndexedDB for use by Haketilo